1 //===-- LLJITWithJITLink.cpp - Configure LLJIT to use ObjectLinkingLayer --===//
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 ObjectLinkingLayer (which is
10 // backed by JITLink) rather than RTDyldObjectLinkingLayer (which is backed by
11 // RuntimeDyld). Using JITLink as the underlying allocator enables use of
12 // small code model in JIT'd code.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
18 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
19 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
20 #include "llvm/Support/InitLLVM.h"
21 #include "llvm/Support/TargetSelect.h"
22 #include "llvm/Support/raw_ostream.h"
24 #include "../ExampleModules.h"
27 using namespace llvm::orc
;
29 ExitOnError ExitOnErr
;
31 int main(int argc
, char *argv
[]) {
33 InitLLVM
X(argc
, argv
);
35 InitializeNativeTarget();
36 InitializeNativeTargetAsmPrinter();
38 cl::ParseCommandLineOptions(argc
, argv
, "HowToUseLLJIT");
39 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
41 // Define an in-process JITLink memory manager.
42 jitlink::InProcessMemoryManager MemMgr
;
44 // Detect the host and set code model to small.
45 auto JTMB
= ExitOnErr(JITTargetMachineBuilder::detectHost());
46 JTMB
.setCodeModel(CodeModel::Small
);
48 // Create an LLJIT instance with an ObjectLinkingLayer as the base layer.
50 ExitOnErr(LLJITBuilder()
51 .setJITTargetMachineBuilder(std::move(JTMB
))
52 .setObjectLinkingLayerCreator([&](ExecutionSession
&ES
,
54 return std::make_unique
<ObjectLinkingLayer
>(ES
, MemMgr
);
58 auto M
= ExitOnErr(parseExampleModule(Add1Example
, "add1"));
60 ExitOnErr(J
->addIRModule(std::move(M
)));
62 // Look up the JIT'd function, cast it to a function pointer, then call it.
63 auto Add1Sym
= ExitOnErr(J
->lookup("add1"));
64 int (*Add1
)(int) = (int (*)(int))Add1Sym
.getAddress();
66 int Result
= Add1(42);
67 outs() << "add1(42) = " << Result
<< "\n";