| 1 |
//===- LLJITWithExecutorProcessControl.cpp - LLJIT example with EPC utils -===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// In this example we will use the lazy re-exports utility to lazily compile |
| 10 |
// IR modules. We will do this in seven steps: |
| 11 |
// |
| 12 |
// 1. Create an LLJIT instance. |
| 13 |
// 2. Install a transform so that we can see what is being compiled. |
| 14 |
// 3. Create an indirect stubs manager and lazy call-through manager. |
| 15 |
// 4. Add two modules that will be conditionally compiled, plus a main module. |
| 16 |
// 5. Add lazy-rexports of the symbols in the conditionally compiled modules. |
| 17 |
// 6. Dump the ExecutionSession state to see the symbol table prior to |
| 18 |
// executing any code. |
| 19 |
// 7. Verify that only modules containing executed code are compiled. |
| 20 |
// |
| 21 |
//===----------------------------------------------------------------------===// |
| 22 |
|
| 23 |
#include "llvm/ADT/StringMap.h" |
| 24 |
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h" |
| 25 |
#include "llvm/ExecutionEngine/Orc/EPCIndirectionUtils.h" |
| 26 |
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h" |
| 27 |
#include "llvm/ExecutionEngine/Orc/LLJIT.h" |
| 28 |
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" |
| 29 |
#include "llvm/ExecutionEngine/Orc/OrcABISupport.h" |
| 30 |
#include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h" |
| 31 |
#include "llvm/Support/InitLLVM.h" |
| 32 |
#include "llvm/Support/TargetSelect.h" |
| 33 |
#include "llvm/Support/raw_ostream.h" |
| 34 |
|
| 35 |
#include "../ExampleModules.h" |
| 36 |
|
| 37 |
#include <future> |
| 38 |
|
| 39 |
using namespace llvm; |
| 40 |
using namespace llvm::orc; |
| 41 |
|
| 42 |
ExitOnError ExitOnErr; |
| 43 |
|
| 44 |
// Example IR modules. |
| 45 |
// |
| 46 |
// Note that in the conditionally compiled modules, FooMod and BarMod, functions |
| 47 |
// have been given an _body suffix. This is to ensure that their names do not |
| 48 |
// clash with their lazy-reexports. |
| 49 |
// For clients who do not wish to rename function bodies (e.g. because they want |
| 50 |
// to re-use cached objects between static and JIT compiles) techniques exist to |
| 51 |
// avoid renaming. See the lazy-reexports section of the ORCv2 design doc. |
| 52 |
|
| 53 |
const llvm::StringRef FooMod = |
| 54 |
R"( |
| 55 |
declare i32 @return1() |
| 56 |
|
| 57 |
define i32 @foo_body() { |
| 58 |
entry: |
| 59 |
%0 = call i32 @return1() |
| 60 |
ret i32 %0 |
| 61 |
} |
| 62 |
)"; |
| 63 |
|
| 64 |
const llvm::StringRef BarMod = |
| 65 |
R"( |
| 66 |
declare i32 @return2() |
| 67 |
|
| 68 |
define i32 @bar_body() { |
| 69 |
entry: |
| 70 |
%0 = call i32 @return2() |
| 71 |
ret i32 %0 |
| 72 |
} |
| 73 |
)"; |
| 74 |
|
| 75 |
const llvm::StringRef MainMod = |
| 76 |
R"( |
| 77 |
|
| 78 |
define i32 @entry(i32 %argc) { |
| 79 |
entry: |
| 80 |
%and = and i32 %argc, 1 |
| 81 |
%tobool = icmp eq i32 %and, 0 |
| 82 |
br i1 %tobool, label %if.end, label %if.then |
| 83 |
|
| 84 |
if.then: ; preds = %entry |
| 85 |
%call = tail call i32 @foo() #2 |
| 86 |
br label %return |
| 87 |
|
| 88 |
if.end: ; preds = %entry |
| 89 |
%call1 = tail call i32 @bar() #2 |
| 90 |
br label %return |
| 91 |
|
| 92 |
return: ; preds = %if.end, %if.then |
| 93 |
%retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.end ] |
| 94 |
ret i32 %retval.0 |
| 95 |
} |
| 96 |
|
| 97 |
declare i32 @foo() |
| 98 |
declare i32 @bar() |
| 99 |
)"; |
| 100 |
|
| 101 |
extern "C" int32_t return1() { return 1; } |
| 102 |
extern "C" int32_t return2() { return 2; } |
| 103 |
|
| 104 |
static void *reenter(void *Ctx, void *TrampolineAddr) { |
| 105 |
std::promise<void *> LandingAddressP; |
| 106 |
auto LandingAddressF = LandingAddressP.get_future(); |
| 107 |
|
| 108 |
auto *EPCIU = static_cast<EPCIndirectionUtils *>(Ctx); |
| 109 |
EPCIU->getLazyCallThroughManager().resolveTrampolineLandingAddress( |
| 110 |
ExecutorAddr::fromPtr(TrampolineAddr), [&](ExecutorAddr LandingAddress) { |
| 111 |
LandingAddressP.set_value(LandingAddress.toPtr<void *>()); |
| 112 |
}); |
| 113 |
return LandingAddressF.get(); |
| 114 |
} |
| 115 |
|
| 116 |
static void reportErrorAndExit() { |
| 117 |
errs() << "Unable to lazily compile function. Exiting.\n"; |
| 118 |
exit(1); |
| 119 |
} |
| 120 |
|
| 121 |
static cl::list<std::string> InputArgv(cl::Positional, |
| 122 |
cl::desc("<program arguments>...")); |
| 123 |
|
| 124 |
int main(int argc, char *argv[]) { |
| 125 |
// Initialize LLVM. |
| 126 |
InitLLVM X(argc, argv); |
| 127 |
|
| 128 |
InitializeNativeTarget(); |
| 129 |
InitializeNativeTargetAsmPrinter(); |
| 130 |
|
| 131 |
cl::ParseCommandLineOptions(argc, argv, "LLJITWithLazyReexports"); |
| 132 |
ExitOnErr.setBanner(std::string(argv[0]) + ": "); |
| 133 |
|
| 134 |
// (1) Create LLJIT instance. |
| 135 |
auto EPC = ExitOnErr(SelfExecutorProcessControl::Create()); |
| 136 |
auto J = ExitOnErr( |
| 137 |
LLJITBuilder().setExecutorProcessControl(std::move(EPC)).create()); |
| 138 |
|
| 139 |
// (2) Install transform to print modules as they are compiled: |
| 140 |
J->getIRTransformLayer().setTransform( |
| 141 |
[](ThreadSafeModule TSM, |
| 142 |
const MaterializationResponsibility &R) -> Expected<ThreadSafeModule> { |
| 143 |
TSM.withModuleDo([](Module &M) { dbgs() << "---Compiling---\n" << M; }); |
| 144 |
return std::move(TSM); // Not a redundant move: fix build on gcc-7.5 |
| 145 |
}); |
| 146 |
|
| 147 |
// (3) Create stubs and call-through managers: |
| 148 |
auto EPCIU = ExitOnErr(EPCIndirectionUtils::Create(J->getExecutionSession())); |
| 149 |
ExitOnErr(EPCIU->writeResolverBlock(ExecutorAddr::fromPtr(&reenter), |
| 150 |
ExecutorAddr::fromPtr(EPCIU.get()))); |
| 151 |
EPCIU->createLazyCallThroughManager( |
| 152 |
J->getExecutionSession(), ExecutorAddr::fromPtr(&reportErrorAndExit)); |
| 153 |
auto ISM = EPCIU->createIndirectStubsManager(); |
| 154 |
|
| 155 |
// (4) Add modules. |
| 156 |
ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(FooMod, "foo-mod")))); |
| 157 |
ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(BarMod, "bar-mod")))); |
| 158 |
ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(MainMod, "main-mod")))); |
| 159 |
|
| 160 |
// (5) Add lazy reexports. |
| 161 |
MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout()); |
| 162 |
SymbolAliasMap ReExports( |
| 163 |
{{Mangle("foo"), |
| 164 |
{Mangle("foo_body"), |
| 165 |
JITSymbolFlags::Exported | JITSymbolFlags::Callable}}, |
| 166 |
{Mangle("bar"), |
| 167 |
{Mangle("bar_body"), |
| 168 |
JITSymbolFlags::Exported | JITSymbolFlags::Callable}}}); |
| 169 |
ExitOnErr(J->getMainJITDylib().define( |
| 170 |
lazyReexports(EPCIU->getLazyCallThroughManager(), *ISM, |
| 171 |
J->getMainJITDylib(), std::move(ReExports)))); |
| 172 |
|
| 173 |
// (6) Dump the ExecutionSession state. |
| 174 |
dbgs() << "---Session state---\n"; |
| 175 |
J->getExecutionSession().dump(dbgs()); |
| 176 |
dbgs() << "\n"; |
| 177 |
|
| 178 |
// (7) Execute the JIT'd main function and pass the example's command line |
| 179 |
// arguments unmodified. This should cause either ExampleMod1 or ExampleMod2 |
| 180 |
// to be compiled, and either "1" or "2" returned depending on the number of |
| 181 |
// arguments passed. |
| 182 |
|
| 183 |
// Look up the JIT'd function, cast it to a function pointer, then call it. |
| 184 |
auto EntryAddr = ExitOnErr(J->lookup("entry")); |
| 185 |
auto *Entry = EntryAddr.toPtr<int(int)>(); |
| 186 |
|
| 187 |
int Result = Entry(argc); |
| 188 |
outs() << "---Result---\n" |
| 189 |
<< "entry(" << argc << ") = " << Result << "\n"; |
| 190 |
|
| 191 |
// Destroy the EPCIndirectionUtils utility. |
| 192 |
ExitOnErr(EPCIU->cleanup()); |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |