OSDN Git Service

[Orc] Simplify LogicalDylib and move it back inside CompileOnDemandLayer. Also
[android-x86/external-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/IRTransformLayer.h"
24 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
25 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
26
27 namespace llvm {
28
29 class OrcLazyJIT {
30 public:
31
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>)>
36     TransformFtor;
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;
42
43   OrcLazyJIT(std::unique_ptr<TargetMachine> TM,
44              std::unique_ptr<CompileCallbackMgr> CCMgr,
45              IndirectStubsManagerBuilder IndirectStubsMgrBuilder,
46              bool InlineStubs)
47       : TM(std::move(TM)), DL(this->TM->createDataLayout()),
48         CCMgr(std::move(CCMgr)),
49         ObjectLayer(),
50         CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
51         IRDumpLayer(CompileLayer, createDebugDumper()),
52         CODLayer(IRDumpLayer, extractSingleFunction, *this->CCMgr,
53                  std::move(IndirectStubsMgrBuilder), InlineStubs),
54         CXXRuntimeOverrides(
55             [this](const std::string &S) { return mangle(S); }) {}
56
57   ~OrcLazyJIT() {
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);
63   }
64
65   ModuleSetHandleT addModuleSet(std::vector<std::unique_ptr<Module>> Ms) {
66     // Attach a data-layouts if they aren't already present.
67     for (auto &M : Ms)
68       if (M->getDataLayout().isDefault())
69         M->setDataLayout(DL);
70
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
73     // JIT.
74     std::vector<std::string> CtorNames, DtorNames;
75     {
76       unsigned CtorId = 0, DtorId = 0;
77       for (auto &M : Ms) {
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));
84         }
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);
91         }
92       }
93     }
94
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.
99     auto Resolver =
100       orc::createLambdaResolver(
101         [this](const std::string &Name) -> JITSymbol {
102           if (auto Sym = CODLayer.findSymbol(Name, true))
103             return Sym;
104           return CXXRuntimeOverrides.searchOverrides(Name);
105         },
106         [](const std::string &Name) {
107           if (auto Addr =
108               RTDyldMemoryManager::getSymbolAddressInProcess(Name))
109             return JITSymbol(Addr, JITSymbolFlags::Exported);
110           return JITSymbol(nullptr);
111         }
112       );
113
114     // Add the module to the JIT.
115     auto H = CODLayer.addModuleSet(std::move(Ms),
116                                    llvm::make_unique<SectionMemoryManager>(),
117                                    std::move(Resolver));
118
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);
123
124     IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
125
126     return H;
127   }
128
129   JITSymbol findSymbol(const std::string &Name) {
130     return CODLayer.findSymbol(mangle(Name), true);
131   }
132
133   JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name) {
134     return CODLayer.findSymbolIn(H, mangle(Name), true);
135   }
136
137 private:
138
139   std::string mangle(const std::string &Name) {
140     std::string MangledName;
141     {
142       raw_string_ostream MangledNameStream(MangledName);
143       Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
144     }
145     return MangledName;
146   }
147
148   static std::set<Function*> extractSingleFunction(Function &F) {
149     std::set<Function*> Partition;
150     Partition.insert(&F);
151     return Partition;
152   }
153
154   static TransformFtor createDebugDumper();
155
156   std::unique_ptr<TargetMachine> TM;
157   DataLayout DL;
158   SectionMemoryManager CCMgrMemMgr;
159
160   std::unique_ptr<CompileCallbackMgr> CCMgr;
161   ObjLayerT ObjectLayer;
162   CompileLayerT CompileLayer;
163   IRDumpLayerT IRDumpLayer;
164   CODLayerT CODLayer;
165
166   orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
167   std::vector<orc::CtorDtorRunner<CODLayerT>> IRStaticDestructorRunners;
168 };
169
170 int runOrcLazyJIT(std::vector<std::unique_ptr<Module>> Ms, int ArgC,
171                   char* ArgV[]);
172
173 } // end namespace llvm
174
175 #endif