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