1 //===--- OrcLazyJIT.h - Basic Orc-based JIT for lazy execution --*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Simple Orc-based JIT. Uses the compile-on-demand layer to break up and 11 // lazily compile modules. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_TOOLS_LLI_ORCLAZYJIT_H 16 #define LLVM_TOOLS_LLI_ORCLAZYJIT_H 17 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/LazyEmittingLayer.h" 25 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" 26 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" 27 #include "llvm/IR/LLVMContext.h" 28 29 namespace llvm { 30 31 class OrcLazyJIT { 32 public: 33 34 typedef orc::JITCompileCallbackManagerBase CompileCallbackMgr; 35 typedef orc::ObjectLinkingLayer<> ObjLayerT; 36 typedef orc::IRCompileLayer<ObjLayerT> CompileLayerT; 37 typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)> 38 TransformFtor; 39 typedef orc::IRTransformLayer<CompileLayerT, TransformFtor> IRDumpLayerT; 40 typedef orc::LazyEmittingLayer<IRDumpLayerT> LazyEmitLayerT; 41 typedef orc::CompileOnDemandLayer<LazyEmitLayerT, 42 CompileCallbackMgr> CODLayerT; 43 typedef CODLayerT::ModuleSetHandleT ModuleHandleT; 44 45 typedef std::function< 46 std::unique_ptr<CompileCallbackMgr>(IRDumpLayerT&, 47 RuntimeDyld::MemoryManager&, 48 LLVMContext&)> 49 CallbackManagerBuilder; 50 51 static CallbackManagerBuilder createCallbackManagerBuilder(Triple T); 52 OrcLazyJIT(std::unique_ptr<TargetMachine> TM,LLVMContext & Context,CallbackManagerBuilder & BuildCallbackMgr)53 OrcLazyJIT(std::unique_ptr<TargetMachine> TM, LLVMContext &Context, 54 CallbackManagerBuilder &BuildCallbackMgr) 55 : TM(std::move(TM)), 56 Mang(this->TM->getDataLayout()), 57 ObjectLayer(), 58 CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)), 59 IRDumpLayer(CompileLayer, createDebugDumper()), 60 LazyEmitLayer(IRDumpLayer), 61 CCMgr(BuildCallbackMgr(IRDumpLayer, CCMgrMemMgr, Context)), 62 CODLayer(LazyEmitLayer, *CCMgr), 63 CXXRuntimeOverrides([this](const std::string &S) { return mangle(S); }) {} 64 ~OrcLazyJIT()65 ~OrcLazyJIT() { 66 // Run any destructors registered with __cxa_atexit. 67 CXXRuntimeOverrides.runDestructors(); 68 // Run any IR destructors. 69 for (auto &DtorRunner : IRStaticDestructorRunners) 70 DtorRunner.runViaLayer(CODLayer); 71 } 72 73 template <typename PtrTy> fromTargetAddress(orc::TargetAddress Addr)74 static PtrTy fromTargetAddress(orc::TargetAddress Addr) { 75 return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr)); 76 } 77 addModule(std::unique_ptr<Module> M)78 ModuleHandleT addModule(std::unique_ptr<Module> M) { 79 // Attach a data-layout if one isn't already present. 80 if (M->getDataLayout().isDefault()) 81 M->setDataLayout(*TM->getDataLayout()); 82 83 // Record the static constructors and destructors. We have to do this before 84 // we hand over ownership of the module to the JIT. 85 std::vector<std::string> CtorNames, DtorNames; 86 for (auto Ctor : orc::getConstructors(*M)) 87 CtorNames.push_back(mangle(Ctor.Func->getName())); 88 for (auto Dtor : orc::getDestructors(*M)) 89 DtorNames.push_back(mangle(Dtor.Func->getName())); 90 91 // Symbol resolution order: 92 // 1) Search the JIT symbols. 93 // 2) Check for C++ runtime overrides. 94 // 3) Search the host process (LLI)'s symbol table. 95 auto Resolver = 96 orc::createLambdaResolver( 97 [this](const std::string &Name) { 98 99 if (auto Sym = CODLayer.findSymbol(Name, true)) 100 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags()); 101 102 if (auto Sym = CXXRuntimeOverrides.searchOverrides(Name)) 103 return Sym; 104 105 if (auto Addr = RTDyldMemoryManager::getSymbolAddressInProcess(Name)) 106 return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported); 107 108 return RuntimeDyld::SymbolInfo(nullptr); 109 }, 110 [](const std::string &Name) { return RuntimeDyld::SymbolInfo(nullptr); } 111 ); 112 113 // Add the module to the JIT. 114 std::vector<std::unique_ptr<Module>> S; 115 S.push_back(std::move(M)); 116 auto H = CODLayer.addModuleSet(std::move(S), nullptr, std::move(Resolver)); 117 118 // Run the static constructors, and save the static destructor runner for 119 // execution when the JIT is torn down. 120 orc::CtorDtorRunner<CODLayerT> CtorRunner(std::move(CtorNames), H); 121 CtorRunner.runViaLayer(CODLayer); 122 123 IRStaticDestructorRunners.push_back( 124 orc::CtorDtorRunner<CODLayerT>(std::move(DtorNames), H)); 125 126 return H; 127 } 128 findSymbol(const std::string & Name)129 orc::JITSymbol findSymbol(const std::string &Name) { 130 return CODLayer.findSymbol(mangle(Name), true); 131 } 132 findSymbolIn(ModuleHandleT H,const std::string & Name)133 orc::JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) { 134 return CODLayer.findSymbolIn(H, mangle(Name), true); 135 } 136 137 private: 138 mangle(const std::string & Name)139 std::string mangle(const std::string &Name) { 140 std::string MangledName; 141 { 142 raw_string_ostream MangledNameStream(MangledName); 143 Mang.getNameWithPrefix(MangledNameStream, Name); 144 } 145 return MangledName; 146 } 147 148 static TransformFtor createDebugDumper(); 149 150 std::unique_ptr<TargetMachine> TM; 151 Mangler Mang; 152 SectionMemoryManager CCMgrMemMgr; 153 154 ObjLayerT ObjectLayer; 155 CompileLayerT CompileLayer; 156 IRDumpLayerT IRDumpLayer; 157 LazyEmitLayerT LazyEmitLayer; 158 std::unique_ptr<CompileCallbackMgr> CCMgr; 159 CODLayerT CODLayer; 160 161 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides; 162 std::vector<orc::CtorDtorRunner<CODLayerT>> IRStaticDestructorRunners; 163 }; 164 165 int runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]); 166 167 } // end namespace llvm 168 169 #endif 170