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