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->createBareJITDylib("<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         [this](std::unique_ptr<MaterializationUnit> MU,
116                MaterializationResponsibility MR) {
117           // FIXME: Switch to move capture once we have C++14.
118           auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
119           auto SharedMR =
120             std::make_shared<MaterializationResponsibility>(std::move(MR));
121           CompileThreads.async([SharedMU, SharedMR]() {
122             SharedMU->materialize(std::move(*SharedMR));
123           });
124         });
125     ExitOnErr(S.addSpeculationRuntime(MainJD, Mangle));
126     LocalCXXRuntimeOverrides CXXRuntimeoverrides;
127     ExitOnErr(CXXRuntimeoverrides.enable(MainJD, Mangle));
128   }
129 
createMemMgr()130   static std::unique_ptr<SectionMemoryManager> createMemMgr() {
131     return std::make_unique<SectionMemoryManager>();
132   }
133 
134   std::unique_ptr<ExecutionSession> ES;
135   DataLayout DL;
136   MangleAndInterner Mangle{*ES, DL};
137   ThreadPool CompileThreads{llvm::hardware_concurrency(NumThreads)};
138 
139   JITDylib &MainJD;
140 
141   Triple TT;
142   std::unique_ptr<LazyCallThroughManager> LCTMgr;
143   IRCompileLayer CompileLayer;
144   ImplSymbolMap Imps;
145   Speculator S;
146   RTDyldObjectLinkingLayer ObjLayer{*ES, createMemMgr};
147   IRSpeculationLayer SpeculateLayer;
148   CompileOnDemandLayer CODLayer;
149 };
150 
main(int argc,char * argv[])151 int main(int argc, char *argv[]) {
152   // Initialize LLVM.
153   InitLLVM X(argc, argv);
154 
155   InitializeNativeTarget();
156   InitializeNativeTargetAsmPrinter();
157 
158   cl::ParseCommandLineOptions(argc, argv, "SpeculativeJIT");
159   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
160 
161   if (NumThreads < 1) {
162     errs() << "Speculative compilation requires one or more dedicated compile "
163               "threads\n";
164     return 1;
165   }
166 
167   // Create a JIT instance.
168   auto SJ = ExitOnErr(SpeculativeJIT::Create());
169 
170   // Load the IR inputs.
171   for (const auto &InputFile : InputFiles) {
172     SMDiagnostic Err;
173     auto Ctx = std::make_unique<LLVMContext>();
174     auto M = parseIRFile(InputFile, Err, *Ctx);
175     if (!M) {
176       Err.print(argv[0], errs());
177       return 1;
178     }
179 
180     ExitOnErr(SJ->addModule(ThreadSafeModule(std::move(M), std::move(Ctx))));
181   }
182 
183   auto MainSym = ExitOnErr(SJ->lookup("main"));
184   auto Main =
185       jitTargetAddressToFunction<int (*)(int, char *[])>(MainSym.getAddress());
186 
187   return runAsMain(Main, InputArgv, StringRef(InputFiles.front()));
188 
189   return 0;
190 }
191