• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->setDispatchMaterialization(
116         [this](std::unique_ptr<MaterializationUnit> MU,
117                std::unique_ptr<MaterializationResponsibility> MR) {
118           CompileThreads.async(
119               [UnownedMU = MU.release(), UnownedMR = MR.release()]() {
120                 std::unique_ptr<MaterializationUnit> MU(UnownedMU);
121                 std::unique_ptr<MaterializationResponsibility> MR(UnownedMR);
122                 MU->materialize(std::move(MR));
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