1 //===--------------- LLJITWithCustomObjectLinkingLayer.cpp ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file shows how to switch LLJIT to use a custom object linking layer (we
10 // use ObjectLinkingLayer, which is backed by JITLink, as an example).
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
16 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
17 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
18 #include "llvm/Support/InitLLVM.h"
19 #include "llvm/Support/TargetSelect.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include "../ExampleModules.h"
25 using namespace llvm::orc;
27 ExitOnError ExitOnErr;
29 int main(int argc, char *argv[]) {
31 InitLLVM X(argc, argv);
33 InitializeNativeTarget();
34 InitializeNativeTargetAsmPrinter();
36 cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT");
37 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
39 // Detect the host and set code model to small.
40 auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
41 JTMB.setCodeModel(CodeModel::Small);
43 // Create an LLJIT instance with an ObjectLinkingLayer as the base layer.
46 .setJITTargetMachineBuilder(std::move(JTMB))
47 .setObjectLinkingLayerCreator(
48 [&](ExecutionSession &ES, const Triple &TT) {
49 return std::make_unique<ObjectLinkingLayer>(
50 ES, std::make_unique<jitlink::InProcessMemoryManager>());
54 auto M = ExitOnErr(parseExampleModule(Add1Example, "add1"));
56 ExitOnErr(J->addIRModule(std::move(M)));
58 // Look up the JIT'd function, cast it to a function pointer, then call it.
59 auto Add1Sym = ExitOnErr(J->lookup("add1"));
60 int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress();
62 int Result = Add1(42);
63 outs() << "add1(42) = " << Result << "\n";