1 //===--- OrcLazyJIT.h - Basic Orc-based JIT for lazy execution --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Simple Orc-based JIT. Uses the compile-on-demand layer to break up and
11 // lazily compile modules.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLI_ORCLAZYJIT_H
16 #define LLVM_TOOLS_LLI_ORCLAZYJIT_H
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
20 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
21 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
22 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
23 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
24 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
25 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
26 #include "llvm/IR/LLVMContext.h"
33 typedef orc::JITCompileCallbackManagerBase CompileCallbackMgr;
34 typedef orc::ObjectLinkingLayer<> ObjLayerT;
35 typedef orc::IRCompileLayer<ObjLayerT> CompileLayerT;
36 typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)>
38 typedef orc::IRTransformLayer<CompileLayerT, TransformFtor> IRDumpLayerT;
39 typedef orc::CompileOnDemandLayer<IRDumpLayerT, CompileCallbackMgr> CODLayerT;
40 typedef CODLayerT::ModuleSetHandleT ModuleHandleT;
42 typedef std::function<
43 std::unique_ptr<CompileCallbackMgr>(IRDumpLayerT&,
44 RuntimeDyld::MemoryManager&,
46 CallbackManagerBuilder;
48 static CallbackManagerBuilder createCallbackManagerBuilder(Triple T);
51 OrcLazyJIT(std::unique_ptr<TargetMachine> TM, const DataLayout &DL,
52 LLVMContext &Context, CallbackManagerBuilder &BuildCallbackMgr)
53 : DL(DL), TM(std::move(TM)), ObjectLayer(),
54 CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
55 IRDumpLayer(CompileLayer, createDebugDumper()),
56 CCMgr(BuildCallbackMgr(IRDumpLayer, CCMgrMemMgr, Context)),
57 CODLayer(IRDumpLayer, *CCMgr, extractSingleFunction, false),
59 [this](const std::string &S) { return mangle(S); }) {}
62 // Run any destructors registered with __cxa_atexit.
63 CXXRuntimeOverrides.runDestructors();
64 // Run any IR destructors.
65 for (auto &DtorRunner : IRStaticDestructorRunners)
66 DtorRunner.runViaLayer(CODLayer);
69 template <typename PtrTy>
70 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
71 return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
74 ModuleHandleT addModule(std::unique_ptr<Module> M) {
75 // Attach a data-layout if one isn't already present.
76 if (M->getDataLayout().isDefault())
79 // Record the static constructors and destructors. We have to do this before
80 // we hand over ownership of the module to the JIT.
81 std::vector<std::string> CtorNames, DtorNames;
82 for (auto Ctor : orc::getConstructors(*M))
83 CtorNames.push_back(mangle(Ctor.Func->getName()));
84 for (auto Dtor : orc::getDestructors(*M))
85 DtorNames.push_back(mangle(Dtor.Func->getName()));
87 // Symbol resolution order:
88 // 1) Search the JIT symbols.
89 // 2) Check for C++ runtime overrides.
90 // 3) Search the host process (LLI)'s symbol table.
91 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver =
92 orc::createLambdaResolver(
93 [this](const std::string &Name) {
94 if (auto Sym = CODLayer.findSymbol(Name, true))
95 return RuntimeDyld::SymbolInfo(Sym.getAddress(),
97 if (auto Sym = CXXRuntimeOverrides.searchOverrides(Name))
101 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
102 return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
104 return RuntimeDyld::SymbolInfo(nullptr);
106 [](const std::string &Name) {
107 return RuntimeDyld::SymbolInfo(nullptr);
111 // Add the module to the JIT.
112 std::vector<std::unique_ptr<Module>> S;
113 S.push_back(std::move(M));
114 auto H = CODLayer.addModuleSet(std::move(S), nullptr, std::move(Resolver));
116 // Run the static constructors, and save the static destructor runner for
117 // execution when the JIT is torn down.
118 orc::CtorDtorRunner<CODLayerT> CtorRunner(std::move(CtorNames), H);
119 CtorRunner.runViaLayer(CODLayer);
121 IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
126 orc::JITSymbol findSymbol(const std::string &Name) {
127 return CODLayer.findSymbol(mangle(Name), true);
130 orc::JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
131 return CODLayer.findSymbolIn(H, mangle(Name), true);
136 std::string mangle(const std::string &Name) {
137 std::string MangledName;
139 raw_string_ostream MangledNameStream(MangledName);
140 Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
145 static std::set<Function*> extractSingleFunction(Function &F) {
146 std::set<Function*> Partition;
147 Partition.insert(&F);
151 static TransformFtor createDebugDumper();
153 std::unique_ptr<TargetMachine> TM;
154 SectionMemoryManager CCMgrMemMgr;
156 ObjLayerT ObjectLayer;
157 CompileLayerT CompileLayer;
158 IRDumpLayerT IRDumpLayer;
159 std::unique_ptr<CompileCallbackMgr> CCMgr;
162 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
163 std::vector<orc::CtorDtorRunner<CODLayerT>> IRStaticDestructorRunners;
166 int runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]);
168 } // end namespace llvm