llvm.org GIT mirror llvm / 51e4b46
This reverts commit r306166 and r306168. Revert "[ORC] Remove redundant semicolons from DEFINE_SIMPLE_CONVERSION_FUNCTIONS uses." Revert "[ORC] Move ORC IR layer interface from addModuleSet to addModule and fix the module type as std::shared_ptr<Module>." They broke ExecutionEngine/OrcMCJIT/test-global-ctors.ll on linux. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306176 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 2 years ago
21 changed file(s) with 334 addition(s) and 373 deletion(s). Raw diff Collapse all Expand all
4343 IRCompileLayer CompileLayer;
4444
4545 public:
46 using ModuleHandle = decltype(CompileLayer)::ModuleHandleT;
46 using ModuleHandle = decltype(CompileLayer)::ModuleSetHandleT;
4747
4848 KaleidoscopeJIT()
4949 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
7171 return JITSymbol(nullptr);
7272 });
7373
74 // Build a singleton module set to hold our module.
75 std::vector> Ms;
76 Ms.push_back(std::move(M));
77
7478 // Add the set to the JIT with the resolver we created above and a newly
7579 // created SectionMemoryManager.
76 return CompileLayer.addModule(std::move(M),
77 make_unique(),
78 std::move(Resolver));
80 return CompileLayer.addModuleSet(std::move(Ms),
81 make_unique(),
82 std::move(Resolver));
7983 }
8084
8185 JITSymbol findSymbol(const std::string Name) {
8690 }
8791
8892 void removeModule(ModuleHandle H) {
89 CompileLayer.removeModule(H);
93 CompileLayer.removeModuleSet(H);
9094 }
9195 };
9296
4747 IRCompileLayer CompileLayer;
4848
4949 using OptimizeFunction =
50 std::functionshared_ptr(std::shared_ptr)>;
50 std::functionunique_ptr(std::unique_ptr)>;
5151
5252 IRTransformLayer OptimizeLayer;
5353
5454 public:
55 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
55 using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT;
5656
5757 KaleidoscopeJIT()
5858 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
5959 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
6060 OptimizeLayer(CompileLayer,
61 [this](std::shared_ptr M) {
61 [this](std::unique_ptr M) {
6262 return optimizeModule(std::move(M));
6363 }) {
6464 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
8484 return JITSymbol(nullptr);
8585 });
8686
87 // Build a singleton module set to hold our module.
88 std::vector> Ms;
89 Ms.push_back(std::move(M));
90
8791 // Add the set to the JIT with the resolver we created above and a newly
8892 // created SectionMemoryManager.
89 return OptimizeLayer.addModule(std::move(M),
90 make_unique(),
91 std::move(Resolver));
93 return OptimizeLayer.addModuleSet(std::move(Ms),
94 make_unique(),
95 std::move(Resolver));
9296 }
9397
9498 JITSymbol findSymbol(const std::string Name) {
99103 }
100104
101105 void removeModule(ModuleHandle H) {
102 OptimizeLayer.removeModule(H);
106 OptimizeLayer.removeModuleSet(H);
103107 }
104108
105109 private:
106 std::shared_ptr optimizeModule(std::shared_ptr M) {
110 std::unique_ptr optimizeModule(std::unique_ptr M) {
107111 // Create a function pass manager.
108112 auto FPM = llvm::make_unique(M.get());
109113
5050 IRCompileLayer CompileLayer;
5151
5252 using OptimizeFunction =
53 std::functionshared_ptr(std::shared_ptr)>;
53 std::functionunique_ptr(std::unique_ptr)>;
5454
5555 IRTransformLayer OptimizeLayer;
5656
5858 CompileOnDemandLayer CODLayer;
5959
6060 public:
61 using ModuleHandle = decltype(CODLayer)::ModuleHandleT;
61 using ModuleHandle = decltype(CODLayer)::ModuleSetHandleT;
6262
6363 KaleidoscopeJIT()
6464 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
6565 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
6666 OptimizeLayer(CompileLayer,
67 [this](std::shared_ptr M) {
67 [this](std::unique_ptr M) {
6868 return optimizeModule(std::move(M));
6969 }),
7070 CompileCallbackManager(
9797 return JITSymbol(nullptr);
9898 });
9999
100 // Build a singleton module set to hold our module.
101 std::vector> Ms;
102 Ms.push_back(std::move(M));
103
100104 // Add the set to the JIT with the resolver we created above and a newly
101105 // created SectionMemoryManager.
102 return CODLayer.addModule(std::move(M),
103 make_unique(),
104 std::move(Resolver));
106 return CODLayer.addModuleSet(std::move(Ms),
107 make_unique(),
108 std::move(Resolver));
105109 }
106110
107111 JITSymbol findSymbol(const std::string Name) {
112116 }
113117
114118 void removeModule(ModuleHandle H) {
115 CODLayer.removeModule(H);
119 CODLayer.removeModuleSet(H);
116120 }
117121
118122 private:
119 std::shared_ptr optimizeModule(std::shared_ptr M) {
123 std::unique_ptr optimizeModule(std::unique_ptr M) {
120124 // Create a function pass manager.
121125 auto FPM = llvm::make_unique(M.get());
122126
7676 IRCompileLayer CompileLayer;
7777
7878 using OptimizeFunction =
79 std::functionshared_ptr(std::shared_ptr)>;
79 std::functionunique_ptr(std::unique_ptr)>;
8080
8181 IRTransformLayer OptimizeLayer;
8282
8484 std::unique_ptr IndirectStubsMgr;
8585
8686 public:
87 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
87 using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT;
8888
8989 KaleidoscopeJIT()
9090 : TM(EngineBuilder().selectTarget()),
9191 DL(TM->createDataLayout()),
9292 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
9393 OptimizeLayer(CompileLayer,
94 [this](std::shared_ptr M) {
94 [this](std::unique_ptr M) {
9595 return optimizeModule(std::move(M));
9696 }),
9797 CompileCallbackMgr(
124124 return JITSymbol(nullptr);
125125 });
126126
127 // Build a singleton module set to hold our module.
128 std::vector> Ms;
129 Ms.push_back(std::move(M));
130
127131 // Add the set to the JIT with the resolver we created above and a newly
128132 // created SectionMemoryManager.
129 return OptimizeLayer.addModule(std::move(M),
130 make_unique(),
131 std::move(Resolver));
133 return OptimizeLayer.addModuleSet(std::move(Ms),
134 make_unique(),
135 std::move(Resolver));
132136 }
133137
134138 Error addFunctionAST(std::unique_ptr FnAST) {
194198 }
195199
196200 void removeModule(ModuleHandle H) {
197 OptimizeLayer.removeModule(H);
201 OptimizeLayer.removeModuleSet(H);
198202 }
199203
200204 private:
205209 return MangledNameStream.str();
206210 }
207211
208 std::shared_ptr optimizeModule(std::shared_ptr M) {
212 std::unique_ptr optimizeModule(std::unique_ptr M) {
209213 // Create a function pass manager.
210214 auto FPM = llvm::make_unique(M.get());
211215
8181 IRCompileLayer CompileLayer;
8282
8383 using OptimizeFunction =
84 std::functionshared_ptr(std::shared_ptr)>;
84 std::functionunique_ptr(std::unique_ptr)>;
8585
8686 IRTransformLayer OptimizeLayer;
8787
9090 MyRemote &Remote;
9191
9292 public:
93 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
93 using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT;
9494
9595 KaleidoscopeJIT(MyRemote &Remote)
9696 : TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "",
9898 DL(TM->createDataLayout()),
9999 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
100100 OptimizeLayer(CompileLayer,
101 [this](std::shared_ptr M) {
101 [this](std::unique_ptr M) {
102102 return optimizeModule(std::move(M));
103103 }),
104104 Remote(Remote) {
152152 exit(1);
153153 }
154154
155 // Build a singleton module set to hold our module.
156 std::vector> Ms;
157 Ms.push_back(std::move(M));
158
155159 // Add the set to the JIT with the resolver we created above and a newly
156160 // created SectionMemoryManager.
157 return OptimizeLayer.addModule(std::move(M),
158 std::move(MemMgr),
159 std::move(Resolver));
161 return OptimizeLayer.addModuleSet(std::move(Ms),
162 std::move(MemMgr),
163 std::move(Resolver));
160164 }
161165
162166 Error addFunctionAST(std::unique_ptr FnAST) {
226230 }
227231
228232 void removeModule(ModuleHandle H) {
229 OptimizeLayer.removeModule(H);
233 OptimizeLayer.removeModuleSet(H);
230234 }
231235
232236 private:
237241 return MangledNameStream.str();
238242 }
239243
240 std::shared_ptr optimizeModule(std::shared_ptr M) {
244 std::unique_ptr optimizeModule(std::unique_ptr M) {
241245 // Create a function pass manager.
242246 auto FPM = llvm::make_unique(M.get());
243247
4040 public:
4141 using ObjLayerT = RTDyldObjectLinkingLayer;
4242 using CompileLayerT = IRCompileLayer;
43 using ModuleHandleT = CompileLayerT::ModuleHandleT;
43 using ModuleHandleT = CompileLayerT::ModuleSetHandleT;
4444
4545 KaleidoscopeJIT()
4646 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
6161 return JITSymbol(nullptr);
6262 },
6363 [](const std::string &S) { return nullptr; });
64 auto H = CompileLayer.addModule(std::move(M),
65 make_unique(),
66 std::move(Resolver));
64 auto H = CompileLayer.addModuleSet(singletonSet(std::move(M)),
65 make_unique(),
66 std::move(Resolver));
6767
6868 ModuleHandles.push_back(H);
6969 return H;
7171
7272 void removeModule(ModuleHandleT H) {
7373 ModuleHandles.erase(find(ModuleHandles, H));
74 CompileLayer.removeModule(H);
74 CompileLayer.removeModuleSet(H);
7575 }
7676
7777 JITSymbol findSymbol(const std::string Name) {
8686 Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
8787 }
8888 return MangledName;
89 }
90
91 template static std::vector singletonSet(T t) {
92 std::vector Vec;
93 Vec.push_back(std::move(t));
94 return Vec;
8995 }
9096
9197 JITSymbol findMangledSymbol(const std::string &Name) {
8383 return LambdaMaterializer(std::move(M));
8484 }
8585
86 using BaseLayerModuleHandleT = typename BaseLayerT::ModuleHandleT;
86 using BaseLayerModuleSetHandleT = typename BaseLayerT::ModuleSetHandleT;
8787
8888 // Provide type-erasure for the Modules and MemoryManagers.
8989 template
138138 struct LogicalDylib {
139139 using SymbolResolverFtor = std::function;
140140
141 using ModuleAdderFtor =
142 std::function
143 BaseLayerT&,
144 std::unique_ptr,
145 std::unique_ptr)>;
141 using ModuleAdderFtor = std::function
142 BaseLayerT &, std::unique_ptr,
143 std::unique_ptr)>;
146144
147145 struct SourceModuleEntry {
148146 std::unique_ptr> SourceMod;
180178
181179 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
182180 for (auto &BLH : BaseLayerHandles)
183 BaseLayer.removeModule(BLH);
181 BaseLayer.removeModuleSet(BLH);
184182 }
185183
186184 std::unique_ptr ExternalSymbolResolver;
189187 StaticGlobalRenamer StaticRenamer;
190188 ModuleAdderFtor ModuleAdder;
191189 SourceModulesList SourceModules;
192 std::vectorHandleT> BaseLayerHandles;
190 std::vectorSetHandleT> BaseLayerHandles;
193191 };
194192
195193 using LogicalDylibList = std::list;
196194
197195 public:
198 /// @brief Handle to loaded module.
199 using ModuleHandleT = typename LogicalDylibList::iterator;
196 /// @brief Handle to a set of loaded modules.
197 using ModuleSetHandleT = typename LogicalDylibList::iterator;
200198
201199 /// @brief Module partitioning functor.
202200 using PartitioningFtor = std::function(Function&)>;
217215
218216 ~CompileOnDemandLayer() {
219217 while (!LogicalDylibs.empty())
220 removeModule(LogicalDylibs.begin());
221 }
222
218 removeModuleSet(LogicalDylibs.begin());
219 }
220
223221 /// @brief Add a module to the compile-on-demand layer.
224 template
225 ModuleHandleT addModule(std::shared_ptr M,
226 MemoryManagerPtrT MemMgr,
227 SymbolResolverPtrT Resolver) {
228
222 template
223 typename SymbolResolverPtrT>
224 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
225 MemoryManagerPtrT MemMgr,
226 SymbolResolverPtrT Resolver) {
229227 LogicalDylibs.push_back(LogicalDylib());
230228 auto &LD = LogicalDylibs.back();
231229 LD.ExternalSymbolResolver = std::move(Resolver);
237235 LD.ModuleAdder =
238236 [&MemMgrRef](BaseLayerT &B, std::unique_ptr M,
239237 std::unique_ptr R) {
240 return B.addModule(std::move(M), &MemMgrRef, std::move(R));
238 std::vector> Ms;
239 Ms.push_back(std::move(M));
240 return B.addModuleSet(std::move(Ms), &MemMgrRef, std::move(R));
241241 };
242242
243243 // Process each of the modules in this module set.
244 addLogicalModule(LogicalDylibs.back(), std::move(M));
244 for (auto &M : Ms)
245 addLogicalModule(LogicalDylibs.back(), std::move(M));
245246
246247 return std::prev(LogicalDylibs.end());
247 }
248
249 /// @brief Add extra modules to an existing logical module.
250 void addExtraModule(ModuleHandleT H, std::shared_ptr M) {
251 addLogicalModule(*H, std::move(M));
252248 }
253249
254250 /// @brief Remove the module represented by the given handle.
255251 ///
256252 /// This will remove all modules in the layers below that were derived from
257253 /// the module represented by H.
258 void removeModule(ModuleHandleT H) {
254 void removeModuleSet(ModuleSetHandleT H) {
259255 H->removeModulesFromBaseLayer(BaseLayer);
260256 LogicalDylibs.erase(H);
261257 }
277273
278274 /// @brief Get the address of a symbol provided by this layer, or some layer
279275 /// below this one.
280 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
276 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
281277 bool ExportedSymbolsOnly) {
282278 return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly);
283279 }
501497 }
502498
503499 template
504 BaseLayerModuleHandleT
500 BaseLayerModuleSetHandleT
505501 emitPartition(LogicalDylib &LD,
506502 typename LogicalDylib::SourceModuleHandle LMId,
507503 const PartitionT &Part) {
9393 /// @brief Construct a CtorDtorRunner for the given range using the given
9494 /// name mangling function.
9595 CtorDtorRunner(std::vector CtorDtorNames,
96 typename JITLayerT::ModuleHandleT H)
96 typename JITLayerT::ModuleSetHandleT H)
9797 : CtorDtorNames(std::move(CtorDtorNames)), H(H) {}
9898
9999 /// @brief Run the recorded constructors/destructors through the given JIT
115115
116116 private:
117117 std::vector CtorDtorNames;
118 typename JITLayerT::ModuleHandleT H;
118 typename JITLayerT::ModuleSetHandleT H;
119119 };
120120
121121 /// @brief Support class for static dtor execution. For hosted (in-process) JITs
2727
2828 /// @brief Eager IR compiling layer.
2929 ///
30 /// This layer immediately compiles each IR module added via addModule to an
31 /// object file and adds this module file to the layer below, which must
32 /// implement the object layer concept.
30 /// This layer accepts sets of LLVM IR Modules (via addModuleSet). It
31 /// immediately compiles each IR module to an object file (each IR Module is
32 /// compiled separately). The resulting set of object files is then added to
33 /// the layer below, which must implement the object layer concept.
3334 template
3435 class IRCompileLayer {
3536 public:
36
37 /// @brief Handle to a compiled module.
38 using ModuleHandleT = typename BaseLayerT::ObjHandleT;
37 /// @brief Handle to a set of compiled modules.
38 using ModuleSetHandleT = typename BaseLayerT::ObjHandleT;
3939
4040 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
4141 /// implement the ObjectLayer concept.
4545 /// @brief Get a reference to the compiler functor.
4646 CompileFtor& getCompiler() { return Compile; }
4747
48 /// @brief Compile the module, and add the resulting object to the base layer
49 /// along with the given memory manager and symbol resolver.
48 /// @brief Compile each module in the given module set, then add the resulting
49 /// set of objects to the base layer along with the memory manager and
50 /// symbol resolver.
5051 ///
51 /// @return A handle for the added module.
52 template
53 ModuleHandleT addModule(std::shared_ptr M,
54 MemoryManagerPtrT MemMgr,
55 SymbolResolverPtrT Resolver) {
56 using CompileResult = decltype(Compile(*M));
57 auto Obj = std::make_shared(Compile(*M));
52 /// @return A handle for the added modules.
53 template
54 typename SymbolResolverPtrT>
55 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
56 MemoryManagerPtrT MemMgr,
57 SymbolResolverPtrT Resolver) {
58 assert(Ms.size() == 1);
59 using CompileResult = decltype(Compile(*Ms.front()));
60 auto Obj = std::make_shared(Compile(*Ms.front()));
5861 return BaseLayer.addObject(std::move(Obj), std::move(MemMgr),
5962 std::move(Resolver));
6063 }
6164
62 /// @brief Remove the module associated with the handle H.
63 void removeModule(ModuleHandleT H) { BaseLayer.removeObject(H); }
65 /// @brief Remove the module set associated with the handle H.
66 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeObject(H); }
6467
6568 /// @brief Search for the given named symbol.
6669 /// @param Name The name of the symbol to search for.
7073 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
7174 }
7275
73 /// @brief Get the address of the given symbol in compiled module represented
74 /// by the handle H. This call is forwarded to the base layer's
75 /// implementation.
76 /// @param H The handle for the module to search in.
76 /// @brief Get the address of the given symbol in the context of the set of
77 /// compiled modules represented by the handle H. This call is
78 /// forwarded to the base layer's implementation.
79 /// @param H The handle for the module set to search in.
7780 /// @param Name The name of the symbol to search for.
7881 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
7982 /// @return A handle for the given named symbol, if it is found in the
80 /// given module.
81 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
83 /// given module set.
84 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
8285 bool ExportedSymbolsOnly) {
8386 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
8487 }
8588
86 /// @brief Immediately emit and finalize the module represented by the given
87 /// handle.
88 /// @param H Handle for module to emit/finalize.
89 void emitAndFinalize(ModuleHandleT H) {
89 /// @brief Immediately emit and finalize the moduleOB set represented by the
90 /// given handle.
91 /// @param H Handle for module set to emit/finalize.
92 void emitAndFinalize(ModuleSetHandleT H) {
9093 BaseLayer.emitAndFinalize(H);
9194 }
9295
2121
2222 /// @brief IR mutating layer.
2323 ///
24 /// This layer applies a user supplied transform to each module that is added,
25 /// then adds the transformed module to the layer below.
24 /// This layer accepts sets of LLVM IR Modules (via addModuleSet). It
25 /// immediately applies the user supplied functor to each module, then adds
26 /// the set of transformed modules to the layer below.
2627 template
2728 class IRTransformLayer {
2829 public:
29
3030 /// @brief Handle to a set of added modules.
31 using ModuleHandleT = typename BaseLayerT::ModuleHandleT;
31 using ModuleSetHandleT = typename BaseLayerT::ModuleSetHandleT;
3232
3333 /// @brief Construct an IRTransformLayer with the given BaseLayer
3434 IRTransformLayer(BaseLayerT &BaseLayer,
3535 TransformFtor Transform = TransformFtor())
3636 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
3737
38 /// @brief Apply the transform functor to the module, then add the module to
39 /// the layer below, along with the memory manager and symbol resolver.
38 /// @brief Apply the transform functor to each module in the module set, then
39 /// add the resulting set of modules to the base layer, along with the
40 /// memory manager and symbol resolver.
4041 ///
4142 /// @return A handle for the added modules.
42 template
43 ModuleHandleT addModule(std::shared_ptr M,
44 MemoryManagerPtrT MemMgr,
45 SymbolResolverPtrT Resolver) {
46 return BaseLayer.addModule(Transform(std::move(M)), std::move(MemMgr),
47 std::move(Resolver));
43 template
44 typename SymbolResolverPtrT>
45 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
46 MemoryManagerPtrT MemMgr,
47 SymbolResolverPtrT Resolver) {
48 for (auto I = Ms.begin(), E = Ms.end(); I != E; ++I)
49 *I = Transform(std::move(*I));
50
51 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
52 std::move(Resolver));
4853 }
4954
50 /// @brief Remove the module associated with the handle H.
51 void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); }
55 /// @brief Remove the module set associated with the handle H.
56 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); }
5257
5358 /// @brief Search for the given named symbol.
5459 /// @param Name The name of the symbol to search for.
5863 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
5964 }
6065
61 /// @brief Get the address of the given symbol in the context of the module
62 /// represented by the handle H. This call is forwarded to the base
63 /// layer's implementation.
64 /// @param H The handle for the module to search in.
66 /// @brief Get the address of the given symbol in the context of the set of
67 /// modules represented by the handle H. This call is forwarded to the
68 /// base layer's implementation.
69 /// @param H The handle for the module set to search in.
6570 /// @param Name The name of the symbol to search for.
6671 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
6772 /// @return A handle for the given named symbol, if it is found in the
68 /// given module.
69 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
73 /// given module set.
74 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
7075 bool ExportedSymbolsOnly) {
7176 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
7277 }
7378
74 /// @brief Immediately emit and finalize the module represented by the given
75 /// handle.
76 /// @param H Handle for module to emit/finalize.
77 void emitAndFinalize(ModuleHandleT H) {
79 /// @brief Immediately emit and finalize the module set represented by the
80 /// given handle.
81 /// @param H Handle for module set to emit/finalize.
82 void emitAndFinalize(ModuleSetHandleT H) {
7883 BaseLayer.emitAndFinalize(H);
7984 }
8085
3333
3434 /// @brief Lazy-emitting IR layer.
3535 ///
36 /// This layer accepts LLVM IR Modules (via addModule), but does not
37 /// immediately emit them the layer below. Instead, emissing to the base layer
38 /// is deferred until the first time the client requests the address (via
39 /// JITSymbol::getAddress) for a symbol contained in this layer.
36 /// This layer accepts sets of LLVM IR Modules (via addModuleSet), but does
37 /// not immediately emit them the layer below. Instead, emissing to the base
38 /// layer is deferred until the first time the client requests the address
39 /// (via JITSymbol::getAddress) for a symbol contained in this layer.
4040 template class LazyEmittingLayer {
4141 public:
42
43 using BaseLayerHandleT = typename BaseLayerT::ModuleHandleT;
42 using BaseLayerHandleT = typename BaseLayerT::ModuleSetHandleT;
4443
4544 private:
46 class EmissionDeferredModule {
45 class EmissionDeferredSet {
4746 public:
48 EmissionDeferredModule() = default;
49 virtual ~EmissionDeferredModule() = default;
47 EmissionDeferredSet() = default;
48 virtual ~EmissionDeferredSet() = default;
5049
5150 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
5251 switch (EmitState) {
8483 llvm_unreachable("Invalid emit-state.");
8584 }
8685
87 void removeModuleFromBaseLayer(BaseLayerT &BaseLayer) {
86 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
8887 if (EmitState != NotEmitted)
89 BaseLayer.removeModule(Handle);
88 BaseLayer.removeModuleSet(Handle);
9089 }
9190
9291 void emitAndFinalize(BaseLayerT &BaseLayer) {
10099 BaseLayer.emitAndFinalize(Handle);
101100 }
102101
103 template
104 static std::unique_ptr
105 create(BaseLayerT &B, std::shared_ptr M, MemoryManagerPtrT MemMgr,
102 template ,
103 typename SymbolResolverPtrT>
104 static std::unique_ptr
105 create(BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
106106 SymbolResolverPtrT Resolver);
107107
108108 protected:
115115 BaseLayerHandleT Handle;
116116 };
117117
118 template
119 class EmissionDeferredModuleImpl : public EmissionDeferredModule {
118 template
119 typename SymbolResolverPtrT>
120 class EmissionDeferredSetImpl : public EmissionDeferredSet {
120121 public:
121 EmissionDeferredModuleImpl(std::shared_ptr M,
122 MemoryManagerPtrT MemMgr,
123 SymbolResolverPtrT Resolver)
124 : M(std::move(M)), MemMgr(std::move(MemMgr)),
122 EmissionDeferredSetImpl(ModuleSetT Ms,
123 MemoryManagerPtrT MemMgr,
124 SymbolResolverPtrT Resolver)
125 : Ms(std::move(Ms)), MemMgr(std::move(MemMgr)),
125126 Resolver(std::move(Resolver)) {}
126127
127128 protected:
152153 // We don't need the mangled names set any more: Once we've emitted this
153154 // to the base layer we'll just look for symbols there.
154155 MangledSymbols.reset();
155 return BaseLayer.addModule(std::move(M), std::move(MemMgr),
156 std::move(Resolver));
156 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
157 std::move(Resolver));
157158 }
158159
159160 private:
195196
196197 auto Symbols = llvm::make_unique>();
197198
198 Mangler Mang;
199
200 for (const auto &GO : M->global_objects())
199 for (const auto &M : Ms) {
200 Mangler Mang;
201
202 for (const auto &GO : M->global_objects())
201203 if (auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
202204 ExportedSymbolsOnly))
203205 return GV;
206 }
204207
205208 MangledSymbols = std::move(Symbols);
206209 return nullptr;
207210 }
208211
209 std::shared_ptr M;
212 ModuleSetT Ms;
210213 MemoryManagerPtrT MemMgr;
211214 SymbolResolverPtrT Resolver;
212215 mutable std::unique_ptr> MangledSymbols;
213216 };
214217
215 using ModuleListT = std::list>>;
218 using ModuleSetListT = std::list>>;
216219
217220 BaseLayerT &BaseLayer;
218 ModuleListT ModuleList;
221 ModuleSetListT ModuleSetList;
219222
220223 public:
221 /// @brief Handle to a loaded module.
222 using ModuleHandleT = typename ModuleListT::iterator;
224 /// @brief Handle to a set of loaded modules.
225 using ModuleSetHandleT = typename ModuleSetListT::iterator;
223226
224227 /// @brief Construct a lazy emitting layer.
225228 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
226229
227 /// @brief Add the given module to the lazy emitting layer.
228 template
229 ModuleHandleT addModule(std::shared_ptr M,
230 MemoryManagerPtrT MemMgr,
231 SymbolResolverPtrT Resolver) {
232 return ModuleList.insert(
233 ModuleList.end(),
234 EmissionDeferredModule::create(BaseLayer, std::move(M),
235 std::move(MemMgr),
236 std::move(Resolver)));
237 }
238
239 /// @brief Remove the module represented by the given handle.
230 /// @brief Add the given set of modules to the lazy emitting layer.
231 template
232 typename SymbolResolverPtrT>
233 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
234 MemoryManagerPtrT MemMgr,
235 SymbolResolverPtrT Resolver) {
236 return ModuleSetList.insert(
237 ModuleSetList.end(),
238 EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MemMgr),
239 std::move(Resolver)));
240 }
241
242 /// @brief Remove the module set represented by the given handle.
240243 ///
241 /// This method will free the memory associated with the given module, both
242 /// in this layer, and the base layer.
243 void removeModule(ModuleHandleT H) {
244 (*H)->removeModuleFromBaseLayer(BaseLayer);
245 ModuleList.erase(H);
244 /// This method will free the memory associated with the given module set,
245 /// both in this layer, and the base layer.
246 void removeModuleSet(ModuleSetHandleT H) {
247 (*H)->removeModulesFromBaseLayer(BaseLayer);
248 ModuleSetList.erase(H);
246249 }
247250
248251 /// @brief Search for the given named symbol.
254257 if (auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
255258 return Symbol;
256259
257 // If not found then search the deferred modules. If any of these contain a
260 // If not found then search the deferred sets. If any of these contain a
258261 // definition of 'Name' then they will return a JITSymbol that will emit
259262 // the corresponding module when the symbol address is requested.
260 for (auto &DeferredMod : ModuleList)
261 if (auto Symbol = DeferredMod->find(Name, ExportedSymbolsOnly, BaseLayer))
263 for (auto &DeferredSet : ModuleSetList)
264 if (auto Symbol = DeferredSet->find(Name, ExportedSymbolsOnly, BaseLayer))
262265 return Symbol;
263266
264267 // If no definition found anywhere return a null symbol.
265268 return nullptr;
266269 }
267270
268 /// @brief Get the address of the given symbol in the context of the of
271 /// @brief Get the address of the given symbol in the context of the set of
269272 /// compiled modules represented by the handle H.
270 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
273 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
271274 bool ExportedSymbolsOnly) {
272275 return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer);
273276 }
274277
275 /// @brief Immediately emit and finalize the module represented by the given
276 /// handle.
277 /// @param H Handle for module to emit/finalize.
278 void emitAndFinalize(ModuleHandleT H) {
278 /// @brief Immediately emit and finalize the moduleOB set represented by the
279 /// given handle.
280 /// @param H Handle for module set to emit/finalize.
281 void emitAndFinalize(ModuleSetHandleT H) {
279282 (*H)->emitAndFinalize(BaseLayer);
280283 }
281284 };
282285
283286 template
284 template
285 std::unique_ptr::EmissionDeferredModule>
286 LazyEmittingLayer::EmissionDeferredModule::create(
287 BaseLayerT &B, std::shared_ptr M, MemoryManagerPtrT MemMgr,
287 template ,
288 typename SymbolResolverPtrT>
289 std::unique_ptr::EmissionDeferredSet>
290 LazyEmittingLayer::EmissionDeferredSet::create(
291 BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
288292 SymbolResolverPtrT Resolver) {
289 using EDS = EmissionDeferredModuleImpl;
290 return llvm::make_unique(std::move(M), std::move(MemMgr),
293 using EDS = EmissionDeferredSetImpl,
294 SymbolResolverPtrT>;
295 return llvm::make_unique(std::move(Ms), std::move(MemMgr),
291296 std::move(Resolver));
292297 }
293298
2828 extern "C" {
2929 #endif
3030
31 typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef;
32 typedef struct LLVMOpaqueSharedObjectBuffer *LLVMSharedObjectBufferRef;
3331 typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef;
3432 typedef uint32_t LLVMOrcModuleHandle;
3533 typedef uint64_t LLVMOrcTargetAddress;
3836 void *CallbackCtx);
3937
4038 typedef enum { LLVMOrcErrSuccess = 0, LLVMOrcErrGeneric } LLVMOrcErrorCode;
41
42 /**
43 * Turn an LLVMModuleRef into an LLVMSharedModuleRef.
44 *
45 * The JIT uses shared ownership for LLVM modules, since it is generally
46 * difficult to know when the JIT will be finished with a module (and the JIT
47 * has no way of knowing when a user may be finished with one).
48 *
49 * Calling this method with an LLVMModuleRef creates a shared-pointer to the
50 * module, and returns a reference to this shared pointer.
51 *
52 * The shared module should be disposed when finished with by calling
53 * LLVMOrcDisposeSharedModule (not LLVMDisposeModule). The Module will be
54 * deleted when the last shared pointer owner relinquishes it.
55 */
56
57 LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod);
58
59 /**
60 * Dispose of a shared module.
61 *
62 * The module should not be accessed after this call. The module will be
63 * deleted once all clients (including the JIT itself) have released their
64 * shared pointers.
65 */
66
67 void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod);
68
69 /**
70 * Get an LLVMSharedObjectBufferRef from an LLVMMemoryBufferRef.
71 */
72 LLVMSharedObjectBufferRef
73 LLVMOrcMakeSharedObjectBuffer(LLVMMemoryBufferRef ObjBuffer);
74
75 /**
76 * Dispose of a shared object buffer.
77 */
78 void
79 LLVMOrcDisposeSharedObjectBufferRef(LLVMSharedObjectBufferRef SharedObjBuffer);
8039
8140 /**
8241 * Create an ORC JIT stack.
13594 * Add module to be eagerly compiled.
13695 */
13796 LLVMOrcModuleHandle
138 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
139 LLVMSharedModuleRef Mod,
97 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
14098 LLVMOrcSymbolResolverFn SymbolResolver,
14199 void *SymbolResolverCtx);
142100
144102 * Add module to be lazily compiled one function at a time.
145103 */
146104 LLVMOrcModuleHandle
147 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
148 LLVMSharedModuleRef Mod,
105 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
149106 LLVMOrcSymbolResolverFn SymbolResolver,
150107 void *SymbolResolverCtx);
151108
153110 * Add an object file.
154111 */
155112 LLVMOrcModuleHandle LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
156 LLVMSharedObjectBufferRef Obj,
113 LLVMObjectFileRef Obj,
157114 LLVMOrcSymbolResolverFn SymbolResolver,
158115 void *SymbolResolverCtx);
159116
1010 #include "llvm-c/OrcBindings.h"
1111
1212 using namespace llvm;
13
14 LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod) {
15 return wrap(new std::shared_ptr(unwrap(Mod)));
16 }
17
18 void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod) {
19 delete unwrap(SharedMod);
20 }
21
22 LLVMSharedObjectBufferRef
23 LLVMOrcMakeSharedObjectBuffer(LLVMMemoryBufferRef ObjBuffer) {
24 return wrap(new std::shared_ptr(unwrap(ObjBuffer)));
25 }
26
27 void
28 LLVMOrcDisposeSharedObjectBufferRef(LLVMSharedObjectBufferRef SharedObjBuffer) {
29 delete unwrap(SharedObjBuffer);
30 }
3113
3214 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
3315 TargetMachine *TM2(unwrap(TM));
8264 }
8365
8466 LLVMOrcModuleHandle
85 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
86 LLVMSharedModuleRef Mod,
67 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
8768 LLVMOrcSymbolResolverFn SymbolResolver,
8869 void *SymbolResolverCtx) {
8970 OrcCBindingsStack &J = *unwrap(JITStack);
90 std::shared_ptr *M(unwrap(Mod));
91 return J.addIRModuleEager(*M, SymbolResolver, SymbolResolverCtx);
71 Module *M(unwrap(Mod));
72 return J.addIRModuleEager(M, SymbolResolver, SymbolResolverCtx);
9273 }
9374
9475 LLVMOrcModuleHandle
95 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
96 LLVMSharedModuleRef Mod,
76 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
9777 LLVMOrcSymbolResolverFn SymbolResolver,
9878 void *SymbolResolverCtx) {
9979 OrcCBindingsStack &J = *unwrap(JITStack);
100 std::shared_ptr *M(unwrap(Mod));
101 return J.addIRModuleLazy(*M, SymbolResolver, SymbolResolverCtx);
80 Module *M(unwrap(Mod));
81 return J.addIRModuleLazy(M, SymbolResolver, SymbolResolverCtx);
10282 }
10383
10484 void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) {
4141
4242 class OrcCBindingsStack;
4343
44 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr,
45 LLVMSharedModuleRef)
46 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr,
47 LLVMSharedObjectBufferRef)
4844 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)
4945 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
5046
7470
7571 template class GenericHandleImpl : public GenericHandle {
7672 public:
77 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle)
73 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle)
7874 : Layer(Layer), Handle(std::move(Handle)) {}
7975
8076 JITSymbol findSymbolIn(const std::string &Name,
8278 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
8379 }
8480
85 void removeModule() override { return Layer.removeModule(Handle); }
81 void removeModule() override { return Layer.removeModuleSet(Handle); }
8682
8783 private:
8884 LayerT &Layer;
89 typename LayerT::ModuleHandleT Handle;
85 typename LayerT::ModuleSetHandleT Handle;
9086 };
9187
9288 template
9389 std::unique_ptr>
94 createGenericHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) {
90 createGenericHandle(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle) {
9591 return llvm::make_unique>(Layer,
9692 std::move(Handle));
9793 }
9894
9995 public:
96 // We need a 'ModuleSetHandleT' to conform to the layer concept.
97 using ModuleSetHandleT = unsigned;
98
10099 using ModuleHandleT = unsigned;
101100
102101 OrcCBindingsStack(TargetMachine &TM,
183182 }
184183
185184 template
186 ModuleHandleT addIRModule(LayerT &Layer, std::shared_ptr M,
185 ModuleHandleT addIRModule(LayerT &Layer, Module *M,
187186 std::unique_ptr MemMgr,
188187 LLVMOrcSymbolResolverFn ExternalResolver,
189188 void *ExternalResolverCtx) {
203202 auto Resolver = createResolver(ExternalResolver, ExternalResolverCtx);
204203
205204 // Add the module to the JIT.
206 auto LH = Layer.addModule(std::move(M), std::move(MemMgr),
207 std::move(Resolver));
205 std::vector S;
206 S.push_back(std::move(M));
207
208 auto LH = Layer.addModuleSet(std::move(S), std::move(MemMgr),
209 std::move(Resolver));
208210 ModuleHandleT H = createHandle(Layer, LH);
209211
210212 // Run the static constructors, and save the static destructor runner for
217219 return H;
218220 }
219221
220 ModuleHandleT addIRModuleEager(std::shared_ptr M,
222 ModuleHandleT addIRModuleEager(Module *M,
221223 LLVMOrcSymbolResolverFn ExternalResolver,
222224 void *ExternalResolverCtx) {
223225 return addIRModule(CompileLayer, std::move(M),
225227 std::move(ExternalResolver), ExternalResolverCtx);
226228 }
227229
228 ModuleHandleT addIRModuleLazy(std::shared_ptr M,
230 ModuleHandleT addIRModuleLazy(Module *M,
229231 LLVMOrcSymbolResolverFn ExternalResolver,
230232 void *ExternalResolverCtx) {
231233 return addIRModule(CODLayer, std::move(M),
254256
255257 private:
256258 template
257 unsigned createHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) {
259 unsigned createHandle(LayerT &Layer,
260 typename LayerT::ModuleSetHandleT Handle) {
258261 unsigned NewHandle;
259262 if (!FreeHandleIndexes.empty()) {
260263 NewHandle = FreeHandleIndexes.back();
190190 } else {
191191 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
192192 }
193 auto *MPtr = M.release();
194 Retain[MPtr] = false;
195 auto Deleter =
196 [this](Module *Mod) {
197 if (!Retain[Mod])
198 delete Mod;
199 };
200 LocalModules.push_back(std::shared_ptr(MPtr, std::move(Deleter)));
201 LazyEmitLayer.addModule(LocalModules.back(), &MemMgr, &Resolver);
193 Modules.push_back(std::move(M));
194 std::vector Ms;
195 Ms.push_back(&*Modules.back());
196 LazyEmitLayer.addModuleSet(std::move(Ms), &MemMgr, &Resolver);
202197 }
203198
204199 void addObjectFile(std::unique_ptr O) override {
385380 std::map
386381 UnfinalizedSections;
387382
388 std::map Retain;
389 std::vector> LocalModules;
390383 std::vector> Archives;
391384 };
392385
5353 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
5454 switch (OrcDumpKind) {
5555 case DumpKind::NoDump:
56 return [](std::shared_ptr M) { return M; };
56 return [](std::unique_ptr M) { return M; };
5757
5858 case DumpKind::DumpFuncsToStdOut:
59 return [](std::shared_ptr M) {
59 return [](std::unique_ptr M) {
6060 printf("[ ");
6161
6262 for (const auto &F : *M) {
7575 };
7676
7777 case DumpKind::DumpModsToStdOut:
78 return [](std::shared_ptr M) {
78 return [](std::unique_ptr M) {
7979 outs() << "----- Module Start -----\n" << *M
8080 << "----- Module End -----\n";
8181
8383 };
8484
8585 case DumpKind::DumpModsToDisk:
86 return [](std::shared_ptr M) {
86 return [](std::unique_ptr M) {
8787 std::error_code EC;
8888 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
8989 sys::fs::F_Text);
146146 OrcInlineStubs);
147147
148148 // Add the module, look up main and run it.
149 for (auto &M : Ms)
150 J.addModule(std::shared_ptr(std::move(M)));
149 J.addModuleSet(std::move(Ms));
151150 auto MainSym = J.findSymbol("main");
152151
153152 if (!MainSym) {
4949 using ObjLayerT = orc::RTDyldObjectLinkingLayer;
5050 using CompileLayerT = orc::IRCompileLayer;
5151 using TransformFtor =
52 std::function(std::shared_ptr)>;
52 std::function(std::unique_ptr)>;
5353 using IRDumpLayerT = orc::IRTransformLayer;
5454 using CODLayerT = orc::CompileOnDemandLayer;
5555 using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
56 using ModuleHandleT = CODLayerT::ModuleHandleT;
56 using ModuleSetHandleT = CODLayerT::ModuleSetHandleT;
5757
5858 OrcLazyJIT(std::unique_ptr TM,
5959 std::unique_ptr CCMgr,
7676 DtorRunner.runViaLayer(CODLayer);
7777 }
7878
79 void addModule(std::shared_ptr M) {
80 if (M->getDataLayout().isDefault())
81 M->setDataLayout(DL);
79 ModuleSetHandleT addModuleSet(std::vector> Ms) {
80 // Attach a data-layouts if they aren't already present.
81 for (auto &M : Ms)
82 if (M->getDataLayout().isDefault())
83 M->setDataLayout(DL);
8284
8385 // Rename, bump linkage and record static constructors and destructors.
8486 // We have to do this before we hand over ownership of the module to the
8688 std::vector CtorNames, DtorNames;
8789 {
8890 unsigned CtorId = 0, DtorId = 0;
89 for (auto Ctor : orc::getConstructors(*M)) {
90 std::string NewCtorName = ("$static_ctor." + Twine(CtorId++)).str();
91 Ctor.Func->setName(NewCtorName);
92 Ctor.Func->setLinkage(GlobalValue::ExternalLinkage);
93 Ctor.Func->setVisibility(GlobalValue::HiddenVisibility);
94 CtorNames.push_back(mangle(NewCtorName));
95 }
96 for (auto Dtor : orc::getDestructors(*M)) {
97 std::string NewDtorName = ("$static_dtor." + Twine(DtorId++)).str();
98 Dtor.Func->setLinkage(GlobalValue::ExternalLinkage);
99 Dtor.Func->setVisibility(GlobalValue::HiddenVisibility);
100 DtorNames.push_back(mangle(Dtor.Func->getName()));
101 Dtor.Func->setName(NewDtorName);
91 for (auto &M : Ms) {
92 for (auto Ctor : orc::getConstructors(*M)) {
93 std::string NewCtorName = ("$static_ctor." + Twine(CtorId++)).str();
94 Ctor.Func->setName(NewCtorName);
95 Ctor.Func->setLinkage(GlobalValue::ExternalLinkage);
96 Ctor.Func->setVisibility(GlobalValue::HiddenVisibility);
97 CtorNames.push_back(mangle(NewCtorName));
98 }
99 for (auto Dtor : orc::getDestructors(*M)) {
100 std::string NewDtorName = ("$static_dtor." + Twine(DtorId++)).str();
101 Dtor.Func->setLinkage(GlobalValue::ExternalLinkage);
102 Dtor.Func->setVisibility(GlobalValue::HiddenVisibility);
103 DtorNames.push_back(mangle(Dtor.Func->getName()));
104 Dtor.Func->setName(NewDtorName);
105 }
102106 }
103107 }
104108
106110 // 1) Search the JIT symbols.
107111 // 2) Check for C++ runtime overrides.
108112 // 3) Search the host process (LLI)'s symbol table.
109 if (ModulesHandle == CODLayerT::ModuleHandleT()) {
110 auto Resolver =
111 orc::createLambdaResolver(
112 [this](const std::string &Name) -> JITSymbol {
113 if (auto Sym = CODLayer.findSymbol(Name, true))
114 return Sym;
115 return CXXRuntimeOverrides.searchOverrides(Name);
116 },
117 [](const std::string &Name) {
118 if (auto Addr =
119 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
120 return JITSymbol(Addr, JITSymbolFlags::Exported);
121 return JITSymbol(nullptr);
122 }
123 );
113 auto Resolver =
114 orc::createLambdaResolver(
115 [this](const std::string &Name) -> JITSymbol {
116 if (auto Sym = CODLayer.findSymbol(Name, true))
117 return Sym;
118 return CXXRuntimeOverrides.searchOverrides(Name);
119 },
120 [](const std::string &Name) {
121 if (auto Addr =
122 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
123 return JITSymbol(Addr, JITSymbolFlags::Exported);
124 return JITSymbol(nullptr);
125 }
126 );
124127
125 // Add the module to the JIT.
126 ModulesHandle =
127 CODLayer.addModule(std::move(M),
128 llvm::make_unique(),
129 std::move(Resolver));
130 } else
131 CODLayer.addExtraModule(ModulesHandle, std::move(M));
128 // Add the module to the JIT.
129 auto H = CODLayer.addModuleSet(std::move(Ms),
130 llvm::make_unique(),
131 std::move(Resolver));
132132
133133 // Run the static constructors, and save the static destructor runner for
134134 // execution when the JIT is torn down.
135 orc::CtorDtorRunner CtorRunner(std::move(CtorNames),
136 ModulesHandle);
135 orc::CtorDtorRunner CtorRunner(std::move(CtorNames), H);
137136 CtorRunner.runViaLayer(CODLayer);
138137
139 IRStaticDestructorRunners.emplace_back(std::move(DtorNames),
140 ModulesHandle);
138 IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
139
140 return H;
141141 }
142142
143143 JITSymbol findSymbol(const std::string &Name) {
144144 return CODLayer.findSymbol(mangle(Name), true);
145145 }
146146
147 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
147 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name) {
148148 return CODLayer.findSymbolIn(H, mangle(Name), true);
149149 }
150150
178178
179179 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
180180 std::vector> IRStaticDestructorRunners;
181 CODLayerT::ModuleHandleT ModulesHandle;
182181 };
183182
184183 int runOrcLazyJIT(std::vector> Ms,
1313 namespace {
1414
1515 struct MockBaseLayer {
16 typedef int ModuleHandleT;
17 ModuleHandleT addModule(
18 std::shared_ptr,
16 typedef int ModuleSetHandleT;
17 ModuleSetHandleT addModuleSet(
18 std::list>,
1919 std::unique_ptr MemMgr,
2020 std::unique_ptr Resolver) {
2121 EXPECT_FALSE(MemMgr);
2626 TEST(LazyEmittingLayerTest, Empty) {
2727 MockBaseLayer M;
2828 llvm::orc::LazyEmittingLayer L(M);
29 L.addModule(std::unique_ptr>(), nullptr, nullptr);
29 L.addModuleSet(std::list>(), nullptr, nullptr);
3030 }
3131
3232 }
313313 // compile.
314314 NullResolver Resolver;
315315 NullManager Manager;
316 CompileLayer.addModule(std::shared_ptr>(), &Manager, &Resolver);
316 CompileLayer.addModuleSet(std::vector>(), &Manager, &Resolver);
317317
318318 // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer
319319 // compile.
6464 CompileContext *CCtx = static_cast(Ctx);
6565 auto *ET = CCtx->APIExecTest;
6666 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple());
67 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release()));
68 CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, SM, myResolver, nullptr);
69 LLVMOrcDisposeSharedModuleRef(SM);
67 CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, wrap(CCtx->M.get()),
68 myResolver, nullptr);
7069 CCtx->Compiled = true;
7170 LLVMOrcTargetAddress MainAddr = LLVMOrcGetSymbolAddress(JITStack, "main");
7271 LLVMOrcSetIndirectStubPointer(JITStack, "foo", MainAddr);
8786
8887 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
8988
90 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
9189 LLVMOrcModuleHandle H =
92 LLVMOrcAddEagerlyCompiledIR(JIT, SM, myResolver, nullptr);
93 LLVMOrcDisposeSharedModuleRef(SM);
90 LLVMOrcAddEagerlyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr);
9491 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
9592 int Result = MainFn();
9693 EXPECT_EQ(Result, 42)
113110
114111 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
115112
116 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
117113 LLVMOrcModuleHandle H =
118 LLVMOrcAddLazilyCompiledIR(JIT, SM, myResolver, nullptr);
119 LLVMOrcDisposeSharedModuleRef(SM);
114 LLVMOrcAddLazilyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr);
120115 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
121116 int Result = MainFn();
122117 EXPECT_EQ(Result, 42)
105105 };
106106
107107 template
108 typename AddModuleFtor,
109 typename RemoveModuleFtor,
108 typename AddModuleSetFtor,
109 typename RemoveModuleSetFtor,
110110 typename FindSymbolFtor,
111111 typename FindSymbolInFtor>
112112 class MockBaseLayer {
113113 public:
114114
115 typedef HandleT ModuleHandleT;
115 typedef HandleT ModuleSetHandleT;
116116
117 MockBaseLayer(AddModuleFtor &&AddModule,
118 RemoveModuleFtor &&RemoveModule,
117 MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
118 RemoveModuleSetFtor &&RemoveModuleSet,
119119 FindSymbolFtor &&FindSymbol,
120120 FindSymbolInFtor &&FindSymbolIn)
121 : AddModule(AddModule), RemoveModule(RemoveModule),
121 : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
122122 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
123123 {}
124124
125 template T, typename MemoryManagerPtrT,
125 template SetT, typename MemoryManagerPtrT,
126126 typename SymbolResolverPtrT>
127 ModuleHandleT addModule(ModuleT Ms, MemoryManagerPtrT MemMgr,
128 SymbolResolverPtrT Resolver) {
129 return AddModule(std::move(Ms), std::move(MemMgr), std::move(Resolver));
127 ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
128 SymbolResolverPtrT Resolver) {
129 return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
130130 }
131131
132 void removeModule(ModuleHandleT H) {
133 RemoveModule(H);
132 void removeModuleSet(ModuleSetHandleT H) {
133 RemoveModuleSet(H);
134134 }
135135
136136 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
137137 return FindSymbol(Name, ExportedSymbolsOnly);
138138 }
139139
140 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
140 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
141141 bool ExportedSymbolsOnly) {
142142 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
143143 }
144144
145145 private:
146 AddModuleFtor AddModule;
147 RemoveModuleFtor RemoveModule;
146 AddModuleSetFtor AddModuleSet;
147 RemoveModuleSetFtor RemoveModuleSet;
148148 FindSymbolFtor FindSymbol;
149149 FindSymbolInFtor FindSymbolIn;
150150 };
151151
152 template
153 typename AddModuleFtor,
154 typename RemoveModuleFtor,
152 template ,
153 typename AddModuleSetFtor,
154 typename RemoveModuleSetFtor,
155155 typename FindSymbolFtor,
156156 typename FindSymbolInFtor>
157 MockBaseLayerHandleT, AddModuleFtor, RemoveModuleFtor,
157 MockBaseLayerSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
158158 FindSymbolFtor, FindSymbolInFtor>
159 createMockBaseLayer(AddModuleFtor &&AddModule,
160 RemoveModuleFtor &&RemoveModule,
159 createMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
160 RemoveModuleSetFtor &&RemoveModuleSet,
161161 FindSymbolFtor &&FindSymbol,
162162 FindSymbolInFtor &&FindSymbolIn) {
163 return MockBaseLayerHandleT, AddModuleFtor, RemoveModuleFtor,
163 return MockBaseLayerSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
164164 FindSymbolFtor, FindSymbolInFtor>(
165 std::forward(AddModule),
166 std::forward<RemoveModuleFtor>(RemoveModule),
165 std::forward<AddModuleSetFtor>(AddModuleSet),
166 std::forward(RemoveModuleSet),
167167 std::forward(FindSymbol),
168168 std::forward(FindSymbolIn));
169169 }