[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / llvm / examples / OrcV2Examples / LLJITWithExecutorProcessControl / LLJITWithExecutorProcessControl.cpp
bloba219cab6e2f773bfbf51ec54205c89d561a98dba
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:
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.
21 //===----------------------------------------------------------------------===//
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/Support/InitLLVM.h"
31 #include "llvm/Support/TargetSelect.h"
32 #include "llvm/Support/raw_ostream.h"
34 #include "../ExampleModules.h"
36 #include <future>
38 using namespace llvm;
39 using namespace llvm::orc;
41 ExitOnError ExitOnErr;
43 // Example IR modules.
45 // Note that in the conditionally compiled modules, FooMod and BarMod, functions
46 // have been given an _body suffix. This is to ensure that their names do not
47 // clash with their lazy-reexports.
48 // For clients who do not wish to rename function bodies (e.g. because they want
49 // to re-use cached objects between static and JIT compiles) techniques exist to
50 // avoid renaming. See the lazy-reexports section of the ORCv2 design doc.
52 const llvm::StringRef FooMod =
53 R"(
54 declare i32 @return1()
56 define i32 @foo_body() {
57 entry:
58 %0 = call i32 @return1()
59 ret i32 %0
61 )";
63 const llvm::StringRef BarMod =
64 R"(
65 declare i32 @return2()
67 define i32 @bar_body() {
68 entry:
69 %0 = call i32 @return2()
70 ret i32 %0
72 )";
74 const llvm::StringRef MainMod =
75 R"(
77 define i32 @entry(i32 %argc) {
78 entry:
79 %and = and i32 %argc, 1
80 %tobool = icmp eq i32 %and, 0
81 br i1 %tobool, label %if.end, label %if.then
83 if.then: ; preds = %entry
84 %call = tail call i32 @foo() #2
85 br label %return
87 if.end: ; preds = %entry
88 %call1 = tail call i32 @bar() #2
89 br label %return
91 return: ; preds = %if.end, %if.then
92 %retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.end ]
93 ret i32 %retval.0
96 declare i32 @foo()
97 declare i32 @bar()
98 )";
100 extern "C" int32_t return1() { return 1; }
101 extern "C" int32_t return2() { return 2; }
103 static void *reenter(void *Ctx, void *TrampolineAddr) {
104 std::promise<void *> LandingAddressP;
105 auto LandingAddressF = LandingAddressP.get_future();
107 auto *EPCIU = static_cast<EPCIndirectionUtils *>(Ctx);
108 EPCIU->getLazyCallThroughManager().resolveTrampolineLandingAddress(
109 ExecutorAddr::fromPtr(TrampolineAddr), [&](ExecutorAddr LandingAddress) {
110 LandingAddressP.set_value(LandingAddress.toPtr<void *>());
112 return LandingAddressF.get();
115 static void reportErrorAndExit() {
116 errs() << "Unable to lazily compile function. Exiting.\n";
117 exit(1);
120 cl::list<std::string> InputArgv(cl::Positional,
121 cl::desc("<program arguments>..."));
123 int main(int argc, char *argv[]) {
124 // Initialize LLVM.
125 InitLLVM X(argc, argv);
127 InitializeNativeTarget();
128 InitializeNativeTargetAsmPrinter();
130 cl::ParseCommandLineOptions(argc, argv, "LLJITWithLazyReexports");
131 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
133 // (1) Create LLJIT instance.
134 auto EPC = ExitOnErr(SelfExecutorProcessControl::Create());
135 auto J = ExitOnErr(
136 LLJITBuilder().setExecutorProcessControl(std::move(EPC)).create());
138 // (2) Install transform to print modules as they are compiled:
139 J->getIRTransformLayer().setTransform(
140 [](ThreadSafeModule TSM,
141 const MaterializationResponsibility &R) -> Expected<ThreadSafeModule> {
142 TSM.withModuleDo([](Module &M) { dbgs() << "---Compiling---\n" << M; });
143 return std::move(TSM); // Not a redundant move: fix build on gcc-7.5
146 // (3) Create stubs and call-through managers:
147 auto EPCIU = ExitOnErr(EPCIndirectionUtils::Create(J->getExecutionSession()));
148 ExitOnErr(EPCIU->writeResolverBlock(ExecutorAddr::fromPtr(&reenter),
149 ExecutorAddr::fromPtr(EPCIU.get())));
150 EPCIU->createLazyCallThroughManager(
151 J->getExecutionSession(), ExecutorAddr::fromPtr(&reportErrorAndExit));
152 auto ISM = EPCIU->createIndirectStubsManager();
154 // (4) Add modules.
155 ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(FooMod, "foo-mod"))));
156 ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(BarMod, "bar-mod"))));
157 ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(MainMod, "main-mod"))));
159 // (5) Add lazy reexports.
160 MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout());
161 SymbolAliasMap ReExports(
162 {{Mangle("foo"),
163 {Mangle("foo_body"),
164 JITSymbolFlags::Exported | JITSymbolFlags::Callable}},
165 {Mangle("bar"),
166 {Mangle("bar_body"),
167 JITSymbolFlags::Exported | JITSymbolFlags::Callable}}});
168 ExitOnErr(J->getMainJITDylib().define(
169 lazyReexports(EPCIU->getLazyCallThroughManager(), *ISM,
170 J->getMainJITDylib(), std::move(ReExports))));
172 // (6) Dump the ExecutionSession state.
173 dbgs() << "---Session state---\n";
174 J->getExecutionSession().dump(dbgs());
175 dbgs() << "\n";
177 // (7) Execute the JIT'd main function and pass the example's command line
178 // arguments unmodified. This should cause either ExampleMod1 or ExampleMod2
179 // to be compiled, and either "1" or "2" returned depending on the number of
180 // arguments passed.
182 // Look up the JIT'd function, cast it to a function pointer, then call it.
183 auto EntryAddr = ExitOnErr(J->lookup("entry"));
184 auto *Entry = EntryAddr.toPtr<int(int)>();
186 int Result = Entry(argc);
187 outs() << "---Result---\n"
188 << "entry(" << argc << ") = " << Result << "\n";
190 // Destroy the EPCIndirectionUtils utility.
191 ExitOnErr(EPCIU->cleanup());
193 return 0;