1 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
2 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
3 #include "llvm/ExecutionEngine/Orc/Core.h"
4 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
5 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
6 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
7 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
8 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
9 #include "llvm/ExecutionEngine/Orc/SpeculateAnalyses.h"
10 #include "llvm/ExecutionEngine/Orc/Speculation.h"
11 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
12 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
13 #include "llvm/IRReader/IRReader.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/InitLLVM.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "llvm/Support/TargetSelect.h"
19 #include "llvm/Support/ThreadPool.h"
20
21 #include <list>
22 #include <string>
23
24 using namespace llvm;
25 using namespace llvm::orc;
26
27 static cl::list<std::string> InputFiles(cl::Positional, cl::OneOrMore,
28 cl::desc("input files"));
29
30 static cl::list<std::string> InputArgv("args", cl::Positional,
31 cl::desc("<program arguments>..."),
32 cl::ZeroOrMore, cl::PositionalEatsArgs);
33
34 static cl::opt<unsigned> NumThreads("num-threads", cl::Optional,
35 cl::desc("Number of compile threads"),
36 cl::init(4));
37
38 ExitOnError ExitOnErr;
39
40 // Add Layers
41 class SpeculativeJIT {
42 public:
Create()43 static Expected<std::unique_ptr<SpeculativeJIT>> Create() {
44 auto JTMB = orc::JITTargetMachineBuilder::detectHost();
45 if (!JTMB)
46 return JTMB.takeError();
47
48 auto DL = JTMB->getDefaultDataLayoutForTarget();
49 if (!DL)
50 return DL.takeError();
51
52 auto ES = std::make_unique<ExecutionSession>();
53
54 auto LCTMgr = createLocalLazyCallThroughManager(
55 JTMB->getTargetTriple(), *ES,
56 pointerToJITTargetAddress(explodeOnLazyCompileFailure));
57 if (!LCTMgr)
58 return LCTMgr.takeError();
59
60 auto ISMBuilder =
61 createLocalIndirectStubsManagerBuilder(JTMB->getTargetTriple());
62 if (!ISMBuilder)
63 return make_error<StringError>("No indirect stubs manager for target",
64 inconvertibleErrorCode());
65
66 auto ProcessSymbolsSearchGenerator =
67 DynamicLibrarySearchGenerator::GetForCurrentProcess(
68 DL->getGlobalPrefix());
69 if (!ProcessSymbolsSearchGenerator)
70 return ProcessSymbolsSearchGenerator.takeError();
71
72 std::unique_ptr<SpeculativeJIT> SJ(new SpeculativeJIT(
73 std::move(ES), std::move(*DL), std::move(*JTMB), std::move(*LCTMgr),
74 std::move(ISMBuilder), std::move(*ProcessSymbolsSearchGenerator)));
75 return std::move(SJ);
76 }
77
getES()78 ExecutionSession &getES() { return *ES; }
79
addModule(ThreadSafeModule TSM)80 Error addModule(ThreadSafeModule TSM) {
81 return CODLayer.add(MainJD, std::move(TSM));
82 }
83
lookup(StringRef UnmangledName)84 Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) {
85 return ES->lookup({&MainJD}, Mangle(UnmangledName));
86 }
87
~SpeculativeJIT()88 ~SpeculativeJIT() { CompileThreads.wait(); }
89
90 private:
91 using IndirectStubsManagerBuilderFunction =
92 std::function<std::unique_ptr<IndirectStubsManager>()>;
93
explodeOnLazyCompileFailure()94 static void explodeOnLazyCompileFailure() {
95 errs() << "Lazy compilation failed, Symbol Implmentation not found!\n";
96 exit(1);
97 }
98
SpeculativeJIT(std::unique_ptr<ExecutionSession> ES,DataLayout DL,orc::JITTargetMachineBuilder JTMB,std::unique_ptr<LazyCallThroughManager> LCTMgr,IndirectStubsManagerBuilderFunction ISMBuilder,std::unique_ptr<DynamicLibrarySearchGenerator> ProcessSymbolsGenerator)99 SpeculativeJIT(
100 std::unique_ptr<ExecutionSession> ES, DataLayout DL,
101 orc::JITTargetMachineBuilder JTMB,
102 std::unique_ptr<LazyCallThroughManager> LCTMgr,
103 IndirectStubsManagerBuilderFunction ISMBuilder,
104 std::unique_ptr<DynamicLibrarySearchGenerator> ProcessSymbolsGenerator)
105 : ES(std::move(ES)), DL(std::move(DL)),
106 MainJD(this->ES->createBareJITDylib("<main>")), LCTMgr(std::move(LCTMgr)),
107 CompileLayer(*this->ES, ObjLayer,
108 std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
109 S(Imps, *this->ES),
110 SpeculateLayer(*this->ES, CompileLayer, S, Mangle, BlockFreqQuery()),
111 CODLayer(*this->ES, SpeculateLayer, *this->LCTMgr,
112 std::move(ISMBuilder)) {
113 MainJD.addGenerator(std::move(ProcessSymbolsGenerator));
114 this->CODLayer.setImplMap(&Imps);
115 this->ES->setDispatchTask(
116 [this](std::unique_ptr<Task> T) {
117 CompileThreads.async(
118 [UnownedT = T.release()]() {
119 std::unique_ptr<Task> T(UnownedT);
120 T->run();
121 });
122 });
123 ExitOnErr(S.addSpeculationRuntime(MainJD, Mangle));
124 LocalCXXRuntimeOverrides CXXRuntimeoverrides;
125 ExitOnErr(CXXRuntimeoverrides.enable(MainJD, Mangle));
126 }
127
createMemMgr()128 static std::unique_ptr<SectionMemoryManager> createMemMgr() {
129 return std::make_unique<SectionMemoryManager>();
130 }
131
132 std::unique_ptr<ExecutionSession> ES;
133 DataLayout DL;
134 MangleAndInterner Mangle{*ES, DL};
135 ThreadPool CompileThreads{llvm::hardware_concurrency(NumThreads)};
136
137 JITDylib &MainJD;
138
139 Triple TT;
140 std::unique_ptr<LazyCallThroughManager> LCTMgr;
141 IRCompileLayer CompileLayer;
142 ImplSymbolMap Imps;
143 Speculator S;
144 RTDyldObjectLinkingLayer ObjLayer{*ES, createMemMgr};
145 IRSpeculationLayer SpeculateLayer;
146 CompileOnDemandLayer CODLayer;
147 };
148
main(int argc,char * argv[])149 int main(int argc, char *argv[]) {
150 // Initialize LLVM.
151 InitLLVM X(argc, argv);
152
153 InitializeNativeTarget();
154 InitializeNativeTargetAsmPrinter();
155
156 cl::ParseCommandLineOptions(argc, argv, "SpeculativeJIT");
157 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
158
159 if (NumThreads < 1) {
160 errs() << "Speculative compilation requires one or more dedicated compile "
161 "threads\n";
162 return 1;
163 }
164
165 // Create a JIT instance.
166 auto SJ = ExitOnErr(SpeculativeJIT::Create());
167
168 // Load the IR inputs.
169 for (const auto &InputFile : InputFiles) {
170 SMDiagnostic Err;
171 auto Ctx = std::make_unique<LLVMContext>();
172 auto M = parseIRFile(InputFile, Err, *Ctx);
173 if (!M) {
174 Err.print(argv[0], errs());
175 return 1;
176 }
177
178 ExitOnErr(SJ->addModule(ThreadSafeModule(std::move(M), std::move(Ctx))));
179 }
180
181 auto MainSym = ExitOnErr(SJ->lookup("main"));
182 auto Main =
183 jitTargetAddressToFunction<int (*)(int, char *[])>(MainSym.getAddress());
184
185 return runAsMain(Main, InputArgv, StringRef(InputFiles.front()));
186
187 return 0;
188 }
189