1633ea072SLang Hames //===--------------- LLJITWithCustomObjectLinkingLayer.cpp ----------------===// 2633ea072SLang Hames // 3633ea072SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4633ea072SLang Hames // See https://llvm.org/LICENSE.txt for license information. 5633ea072SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6633ea072SLang Hames // 7633ea072SLang Hames //===----------------------------------------------------------------------===// 8633ea072SLang Hames // 9633ea072SLang Hames // This file shows how to switch LLJIT to use a custom object linking layer (we 10633ea072SLang Hames // use ObjectLinkingLayer, which is backed by JITLink, as an example). 11633ea072SLang Hames // 12633ea072SLang Hames //===----------------------------------------------------------------------===// 13633ea072SLang Hames 14633ea072SLang Hames #include "llvm/ADT/StringMap.h" 15633ea072SLang Hames #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h" 16633ea072SLang Hames #include "llvm/ExecutionEngine/Orc/LLJIT.h" 17633ea072SLang Hames #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" 18633ea072SLang Hames #include "llvm/Support/InitLLVM.h" 19633ea072SLang Hames #include "llvm/Support/TargetSelect.h" 20633ea072SLang Hames #include "llvm/Support/raw_ostream.h" 21633ea072SLang Hames 22633ea072SLang Hames #include "../ExampleModules.h" 23633ea072SLang Hames 24633ea072SLang Hames using namespace llvm; 25633ea072SLang Hames using namespace llvm::orc; 26633ea072SLang Hames 27633ea072SLang Hames ExitOnError ExitOnErr; 28633ea072SLang Hames 29633ea072SLang Hames int main(int argc, char *argv[]) { 30633ea072SLang Hames // Initialize LLVM. 31633ea072SLang Hames InitLLVM X(argc, argv); 32633ea072SLang Hames 33633ea072SLang Hames InitializeNativeTarget(); 34633ea072SLang Hames InitializeNativeTargetAsmPrinter(); 35633ea072SLang Hames 36633ea072SLang Hames cl::ParseCommandLineOptions(argc, argv, "LLJITWithCustomObjectLinkingLayer"); 37633ea072SLang Hames ExitOnErr.setBanner(std::string(argv[0]) + ": "); 38633ea072SLang Hames 39633ea072SLang Hames // Detect the host and set code model to small. 40633ea072SLang Hames auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost()); 41633ea072SLang Hames JTMB.setCodeModel(CodeModel::Small); 42633ea072SLang Hames 43633ea072SLang Hames // Create an LLJIT instance with an ObjectLinkingLayer as the base layer. 44633ea072SLang Hames auto J = ExitOnErr( 45633ea072SLang Hames LLJITBuilder() 46633ea072SLang Hames .setJITTargetMachineBuilder(std::move(JTMB)) 47633ea072SLang Hames .setObjectLinkingLayerCreator( 48633ea072SLang Hames [&](ExecutionSession &ES, const Triple &TT) { 49633ea072SLang Hames return std::make_unique<ObjectLinkingLayer>( 50*962a2479SLang Hames ES, ExitOnErr(jitlink::InProcessMemoryManager::Create())); 51633ea072SLang Hames }) 52633ea072SLang Hames .create()); 53633ea072SLang Hames 54633ea072SLang Hames auto M = ExitOnErr(parseExampleModule(Add1Example, "add1")); 55633ea072SLang Hames 56633ea072SLang Hames ExitOnErr(J->addIRModule(std::move(M))); 57633ea072SLang Hames 58633ea072SLang Hames // Look up the JIT'd function, cast it to a function pointer, then call it. 59633ea072SLang Hames auto Add1Sym = ExitOnErr(J->lookup("add1")); 60633ea072SLang Hames int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress(); 61633ea072SLang Hames 62633ea072SLang Hames int Result = Add1(42); 63633ea072SLang Hames outs() << "add1(42) = " << Result << "\n"; 64633ea072SLang Hames 65633ea072SLang Hames return 0; 66633ea072SLang Hames } 67