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"
32 typedef orc::JITCompileCallbackManager CompileCallbackMgr;
33 typedef orc::ObjectLinkingLayer<> ObjLayerT;
34 typedef orc::IRCompileLayer<ObjLayerT> CompileLayerT;
35 typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)>
37 typedef orc::IRTransformLayer<CompileLayerT, TransformFtor> IRDumpLayerT;
38 typedef orc::CompileOnDemandLayer<IRDumpLayerT, CompileCallbackMgr> CODLayerT;
39 typedef CODLayerT::IndirectStubsManagerBuilderT
40 IndirectStubsManagerBuilder;
41 typedef CODLayerT::ModuleSetHandleT ModuleSetHandleT;
43 OrcLazyJIT(std::unique_ptr<TargetMachine> TM,
44 std::unique_ptr<CompileCallbackMgr> CCMgr,
45 IndirectStubsManagerBuilder IndirectStubsMgrBuilder,
47 : TM(std::move(TM)), DL(this->TM->createDataLayout()),
48 CCMgr(std::move(CCMgr)),
50 CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
51 IRDumpLayer(CompileLayer, createDebugDumper()),
52 CODLayer(IRDumpLayer, extractSingleFunction, *this->CCMgr,
53 std::move(IndirectStubsMgrBuilder), InlineStubs),
55 [this](const std::string &S) { return mangle(S); }) {}
58 // Run any destructors registered with __cxa_atexit.
59 CXXRuntimeOverrides.runDestructors();
60 // Run any IR destructors.
61 for (auto &DtorRunner : IRStaticDestructorRunners)
62 DtorRunner.runViaLayer(CODLayer);
65 ModuleSetHandleT addModuleSet(std::vector<std::unique_ptr<Module>> Ms) {
66 // Attach a data-layouts if they aren't already present.
68 if (M->getDataLayout().isDefault())
71 // Rename, bump linkage and record static constructors and destructors.
72 // We have to do this before we hand over ownership of the module to the
74 std::vector<std::string> CtorNames, DtorNames;
76 unsigned CtorId = 0, DtorId = 0;
78 for (auto Ctor : orc::getConstructors(*M)) {
79 std::string NewCtorName = ("$static_ctor." + Twine(CtorId++)).str();
80 Ctor.Func->setName(NewCtorName);
81 Ctor.Func->setLinkage(GlobalValue::ExternalLinkage);
82 Ctor.Func->setVisibility(GlobalValue::HiddenVisibility);
83 CtorNames.push_back(mangle(NewCtorName));
85 for (auto Dtor : orc::getDestructors(*M)) {
86 std::string NewDtorName = ("$static_dtor." + Twine(DtorId++)).str();
87 Dtor.Func->setLinkage(GlobalValue::ExternalLinkage);
88 Dtor.Func->setVisibility(GlobalValue::HiddenVisibility);
89 DtorNames.push_back(mangle(Dtor.Func->getName()));
90 Dtor.Func->setName(NewDtorName);
95 // Symbol resolution order:
96 // 1) Search the JIT symbols.
97 // 2) Check for C++ runtime overrides.
98 // 3) Search the host process (LLI)'s symbol table.
100 orc::createLambdaResolver(
101 [this](const std::string &Name) -> JITSymbol {
102 if (auto Sym = CODLayer.findSymbol(Name, true))
104 return CXXRuntimeOverrides.searchOverrides(Name);
106 [](const std::string &Name) {
108 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
109 return JITSymbol(Addr, JITSymbolFlags::Exported);
110 return JITSymbol(nullptr);
114 // Add the module to the JIT.
115 auto H = CODLayer.addModuleSet(std::move(Ms),
116 llvm::make_unique<SectionMemoryManager>(),
117 std::move(Resolver));
119 // Run the static constructors, and save the static destructor runner for
120 // execution when the JIT is torn down.
121 orc::CtorDtorRunner<CODLayerT> CtorRunner(std::move(CtorNames), H);
122 CtorRunner.runViaLayer(CODLayer);
124 IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
129 JITSymbol findSymbol(const std::string &Name) {
130 return CODLayer.findSymbol(mangle(Name), true);
133 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name) {
134 return CODLayer.findSymbolIn(H, mangle(Name), true);
139 std::string mangle(const std::string &Name) {
140 std::string MangledName;
142 raw_string_ostream MangledNameStream(MangledName);
143 Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
148 static std::set<Function*> extractSingleFunction(Function &F) {
149 std::set<Function*> Partition;
150 Partition.insert(&F);
154 static TransformFtor createDebugDumper();
156 std::unique_ptr<TargetMachine> TM;
158 SectionMemoryManager CCMgrMemMgr;
160 std::unique_ptr<CompileCallbackMgr> CCMgr;
161 ObjLayerT ObjectLayer;
162 CompileLayerT CompileLayer;
163 IRDumpLayerT IRDumpLayer;
166 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
167 std::vector<orc::CtorDtorRunner<CODLayerT>> IRStaticDestructorRunners;
170 int runOrcLazyJIT(std::vector<std::unique_ptr<Module>> Ms, int ArgC,
173 } // end namespace llvm