llvm.org GIT mirror llvm / a5b1998
[ORC] Move ORC IR layer interface from addModuleSet to addModule and fix the module type as std::shared_ptr<Module>. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306166 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 2 years ago
21 changed file(s) with 373 addition(s) and 334 deletion(s). Raw diff Collapse all Expand all
4343 IRCompileLayer CompileLayer;
4444
4545 public:
46 using ModuleHandle = decltype(CompileLayer)::ModuleSetHandleT;
46 using ModuleHandle = decltype(CompileLayer)::ModuleHandleT;
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
7874 // Add the set to the JIT with the resolver we created above and a newly
7975 // created SectionMemoryManager.
80 return CompileLayer.addModuleSet(std::move(Ms),
81 make_unique(),
82 std::move(Resolver));
76 return CompileLayer.addModule(std::move(M),
77 make_unique(),
78 std::move(Resolver));
8379 }
8480
8581 JITSymbol findSymbol(const std::string Name) {
9086 }
9187
9288 void removeModule(ModuleHandle H) {
93 CompileLayer.removeModuleSet(H);
89 CompileLayer.removeModule(H);
9490 }
9591 };
9692
4747 IRCompileLayer CompileLayer;
4848
4949 using OptimizeFunction =
50 std::functionunique_ptr(std::unique_ptr)>;
50 std::functionshared_ptr(std::shared_ptr)>;
5151
5252 IRTransformLayer OptimizeLayer;
5353
5454 public:
55 using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT;
55 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
5656
5757 KaleidoscopeJIT()
5858 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
5959 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
6060 OptimizeLayer(CompileLayer,
61 [this](std::unique_ptr M) {
61 [this](std::shared_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
9187 // Add the set to the JIT with the resolver we created above and a newly
9288 // created SectionMemoryManager.
93 return OptimizeLayer.addModuleSet(std::move(Ms),
94 make_unique(),
95 std::move(Resolver));
89 return OptimizeLayer.addModule(std::move(M),
90 make_unique(),
91 std::move(Resolver));
9692 }
9793
9894 JITSymbol findSymbol(const std::string Name) {
10399 }
104100
105101 void removeModule(ModuleHandle H) {
106 OptimizeLayer.removeModuleSet(H);
102 OptimizeLayer.removeModule(H);
107103 }
108104
109105 private:
110 std::unique_ptr optimizeModule(std::unique_ptr M) {
106 std::shared_ptr optimizeModule(std::shared_ptr M) {
111107 // Create a function pass manager.
112108 auto FPM = llvm::make_unique(M.get());
113109
5050 IRCompileLayer CompileLayer;
5151
5252 using OptimizeFunction =
53 std::functionunique_ptr(std::unique_ptr)>;
53 std::functionshared_ptr(std::shared_ptr)>;
5454
5555 IRTransformLayer OptimizeLayer;
5656
5858 CompileOnDemandLayer CODLayer;
5959
6060 public:
61 using ModuleHandle = decltype(CODLayer)::ModuleSetHandleT;
61 using ModuleHandle = decltype(CODLayer)::ModuleHandleT;
6262
6363 KaleidoscopeJIT()
6464 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
6565 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
6666 OptimizeLayer(CompileLayer,
67 [this](std::unique_ptr M) {
67 [this](std::shared_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
104100 // Add the set to the JIT with the resolver we created above and a newly
105101 // created SectionMemoryManager.
106 return CODLayer.addModuleSet(std::move(Ms),
107 make_unique(),
108 std::move(Resolver));
102 return CODLayer.addModule(std::move(M),
103 make_unique(),
104 std::move(Resolver));
109105 }
110106
111107 JITSymbol findSymbol(const std::string Name) {
116112 }
117113
118114 void removeModule(ModuleHandle H) {
119 CODLayer.removeModuleSet(H);
115 CODLayer.removeModule(H);
120116 }
121117
122118 private:
123 std::unique_ptr optimizeModule(std::unique_ptr M) {
119 std::shared_ptr optimizeModule(std::shared_ptr M) {
124120 // Create a function pass manager.
125121 auto FPM = llvm::make_unique(M.get());
126122
7676 IRCompileLayer CompileLayer;
7777
7878 using OptimizeFunction =
79 std::functionunique_ptr(std::unique_ptr)>;
79 std::functionshared_ptr(std::shared_ptr)>;
8080
8181 IRTransformLayer OptimizeLayer;
8282
8484 std::unique_ptr IndirectStubsMgr;
8585
8686 public:
87 using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT;
87 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
8888
8989 KaleidoscopeJIT()
9090 : TM(EngineBuilder().selectTarget()),
9191 DL(TM->createDataLayout()),
9292 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
9393 OptimizeLayer(CompileLayer,
94 [this](std::unique_ptr M) {
94 [this](std::shared_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
131127 // Add the set to the JIT with the resolver we created above and a newly
132128 // created SectionMemoryManager.
133 return OptimizeLayer.addModuleSet(std::move(Ms),
134 make_unique(),
135 std::move(Resolver));
129 return OptimizeLayer.addModule(std::move(M),
130 make_unique(),
131 std::move(Resolver));
136132 }
137133
138134 Error addFunctionAST(std::unique_ptr FnAST) {
198194 }
199195
200196 void removeModule(ModuleHandle H) {
201 OptimizeLayer.removeModuleSet(H);
197 OptimizeLayer.removeModule(H);
202198 }
203199
204200 private:
209205 return MangledNameStream.str();
210206 }
211207
212 std::unique_ptr optimizeModule(std::unique_ptr M) {
208 std::shared_ptr optimizeModule(std::shared_ptr M) {
213209 // Create a function pass manager.
214210 auto FPM = llvm::make_unique(M.get());
215211
8181 IRCompileLayer CompileLayer;
8282
8383 using OptimizeFunction =
84 std::functionunique_ptr(std::unique_ptr)>;
84 std::functionshared_ptr(std::shared_ptr)>;
8585
8686 IRTransformLayer OptimizeLayer;
8787
9090 MyRemote &Remote;
9191
9292 public:
93 using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT;
93 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
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::unique_ptr M) {
101 [this](std::shared_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
159155 // Add the set to the JIT with the resolver we created above and a newly
160156 // created SectionMemoryManager.
161 return OptimizeLayer.addModuleSet(std::move(Ms),
162 std::move(MemMgr),
163 std::move(Resolver));
157 return OptimizeLayer.addModule(std::move(M),
158 std::move(MemMgr),
159 std::move(Resolver));
164160 }
165161
166162 Error addFunctionAST(std::unique_ptr FnAST) {
230226 }
231227
232228 void removeModule(ModuleHandle H) {
233 OptimizeLayer.removeModuleSet(H);
229 OptimizeLayer.removeModule(H);
234230 }
235231
236232 private:
241237 return MangledNameStream.str();
242238 }
243239
244 std::unique_ptr optimizeModule(std::unique_ptr M) {
240 std::shared_ptr optimizeModule(std::shared_ptr M) {
245241 // Create a function pass manager.
246242 auto FPM = llvm::make_unique(M.get());
247243
4040 public:
4141 using ObjLayerT = RTDyldObjectLinkingLayer;
4242 using CompileLayerT = IRCompileLayer;
43 using ModuleHandleT = CompileLayerT::ModuleSetHandleT;
43 using ModuleHandleT = CompileLayerT::ModuleHandleT;
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.addModuleSet(singletonSet(std::move(M)),
65 make_unique(),
66 std::move(Resolver));
64 auto H = CompileLayer.addModule(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.removeModuleSet(H);
74 CompileLayer.removeModule(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;
9589 }
9690
9791 JITSymbol findMangledSymbol(const std::string &Name) {
8383 return LambdaMaterializer(std::move(M));
8484 }
8585
86 using BaseLayerModuleSetHandleT = typename BaseLayerT::ModuleSetHandleT;
86 using BaseLayerModuleHandleT = typename BaseLayerT::ModuleHandleT;
8787
8888 // Provide type-erasure for the Modules and MemoryManagers.
8989 template
138138 struct LogicalDylib {
139139 using SymbolResolverFtor = std::function;
140140
141 using ModuleAdderFtor = std::function
142 BaseLayerT &, std::unique_ptr,
143 std::unique_ptr)>;
141 using ModuleAdderFtor =
142 std::function
143 BaseLayerT&,
144 std::unique_ptr,
145 std::unique_ptr)>;
144146
145147 struct SourceModuleEntry {
146148 std::unique_ptr> SourceMod;
178180
179181 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
180182 for (auto &BLH : BaseLayerHandles)
181 BaseLayer.removeModuleSet(BLH);
183 BaseLayer.removeModule(BLH);
182184 }
183185
184186 std::unique_ptr ExternalSymbolResolver;
187189 StaticGlobalRenamer StaticRenamer;
188190 ModuleAdderFtor ModuleAdder;
189191 SourceModulesList SourceModules;
190 std::vectorSetHandleT> BaseLayerHandles;
192 std::vectorHandleT> BaseLayerHandles;
191193 };
192194
193195 using LogicalDylibList = std::list;
194196
195197 public:
196 /// @brief Handle to a set of loaded modules.
197 using ModuleSetHandleT = typename LogicalDylibList::iterator;
198 /// @brief Handle to loaded module.
199 using ModuleHandleT = typename LogicalDylibList::iterator;
198200
199201 /// @brief Module partitioning functor.
200202 using PartitioningFtor = std::function(Function&)>;
215217
216218 ~CompileOnDemandLayer() {
217219 while (!LogicalDylibs.empty())
218 removeModuleSet(LogicalDylibs.begin());
219 }
220
220 removeModule(LogicalDylibs.begin());
221 }
222
221223 /// @brief Add a module to the compile-on-demand layer.
222 template
223 typename SymbolResolverPtrT>
224 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
225 MemoryManagerPtrT MemMgr,
226 SymbolResolverPtrT Resolver) {
224 template
225 ModuleHandleT addModule(std::shared_ptr M,
226 MemoryManagerPtrT MemMgr,
227 SymbolResolverPtrT Resolver) {
228
227229 LogicalDylibs.push_back(LogicalDylib());
228230 auto &LD = LogicalDylibs.back();
229231 LD.ExternalSymbolResolver = std::move(Resolver);
235237 LD.ModuleAdder =
236238 [&MemMgrRef](BaseLayerT &B, std::unique_ptr M,
237239 std::unique_ptr R) {
238 std::vector> Ms;
239 Ms.push_back(std::move(M));
240 return B.addModuleSet(std::move(Ms), &MemMgrRef, std::move(R));
240 return B.addModule(std::move(M), &MemMgrRef, std::move(R));
241241 };
242242
243243 // Process each of the modules in this module set.
244 for (auto &M : Ms)
245 addLogicalModule(LogicalDylibs.back(), std::move(M));
244 addLogicalModule(LogicalDylibs.back(), std::move(M));
246245
247246 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));
248252 }
249253
250254 /// @brief Remove the module represented by the given handle.
251255 ///
252256 /// This will remove all modules in the layers below that were derived from
253257 /// the module represented by H.
254 void removeModuleSet(ModuleSetHandleT H) {
258 void removeModule(ModuleHandleT H) {
255259 H->removeModulesFromBaseLayer(BaseLayer);
256260 LogicalDylibs.erase(H);
257261 }
273277
274278 /// @brief Get the address of a symbol provided by this layer, or some layer
275279 /// below this one.
276 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
280 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
277281 bool ExportedSymbolsOnly) {
278282 return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly);
279283 }
497501 }
498502
499503 template
500 BaseLayerModuleSetHandleT
504 BaseLayerModuleHandleT
501505 emitPartition(LogicalDylib &LD,
502506 typename LogicalDylib::SourceModuleHandle LMId,
503507 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::ModuleSetHandleT H)
96 typename JITLayerT::ModuleHandleT 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::ModuleSetHandleT H;
118 typename JITLayerT::ModuleHandleT 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 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.
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.
3433 template
3534 class IRCompileLayer {
3635 public:
37 /// @brief Handle to a set of compiled modules.
38 using ModuleSetHandleT = typename BaseLayerT::ObjHandleT;
36
37 /// @brief Handle to a compiled module.
38 using ModuleHandleT = 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 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.
48 /// @brief Compile the module, and add the resulting object to the base layer
49 /// along with the given memory manager and symbol resolver.
5150 ///
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()));
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));
6158 return BaseLayer.addObject(std::move(Obj), std::move(MemMgr),
6259 std::move(Resolver));
6360 }
6461
65 /// @brief Remove the module set associated with the handle H.
66 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeObject(H); }
62 /// @brief Remove the module associated with the handle H.
63 void removeModule(ModuleHandleT H) { BaseLayer.removeObject(H); }
6764
6865 /// @brief Search for the given named symbol.
6966 /// @param Name The name of the symbol to search for.
7370 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
7471 }
7572
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.
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.
8077 /// @param Name The name of the symbol to search for.
8178 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
8279 /// @return A handle for the given named symbol, if it is found in the
83 /// given module set.
84 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
80 /// given module.
81 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
8582 bool ExportedSymbolsOnly) {
8683 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
8784 }
8885
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) {
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) {
9390 BaseLayer.emitAndFinalize(H);
9491 }
9592
2121
2222 /// @brief IR mutating layer.
2323 ///
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.
24 /// This layer applies a user supplied transform to each module that is added,
25 /// then adds the transformed module to the layer below.
2726 template
2827 class IRTransformLayer {
2928 public:
29
3030 /// @brief Handle to a set of added modules.
31 using ModuleSetHandleT = typename BaseLayerT::ModuleSetHandleT;
31 using ModuleHandleT = typename BaseLayerT::ModuleHandleT;
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 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.
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.
4140 ///
4241 /// @return A handle for the added modules.
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));
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));
5348 }
5449
55 /// @brief Remove the module set associated with the handle H.
56 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); }
50 /// @brief Remove the module associated with the handle H.
51 void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); }
5752
5853 /// @brief Search for the given named symbol.
5954 /// @param Name The name of the symbol to search for.
6358 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
6459 }
6560
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.
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.
7065 /// @param Name The name of the symbol to search for.
7166 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
7267 /// @return A handle for the given named symbol, if it is found in the
73 /// given module set.
74 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
68 /// given module.
69 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
7570 bool ExportedSymbolsOnly) {
7671 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
7772 }
7873
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) {
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) {
8378 BaseLayer.emitAndFinalize(H);
8479 }
8580
3333
3434 /// @brief Lazy-emitting IR layer.
3535 ///
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.
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.
4040 template class LazyEmittingLayer {
4141 public:
42 using BaseLayerHandleT = typename BaseLayerT::ModuleSetHandleT;
42
43 using BaseLayerHandleT = typename BaseLayerT::ModuleHandleT;
4344
4445 private:
45 class EmissionDeferredSet {
46 class EmissionDeferredModule {
4647 public:
47 EmissionDeferredSet() = default;
48 virtual ~EmissionDeferredSet() = default;
48 EmissionDeferredModule() = default;
49 virtual ~EmissionDeferredModule() = default;
4950
5051 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
5152 switch (EmitState) {
8384 llvm_unreachable("Invalid emit-state.");
8485 }
8586
86 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
87 void removeModuleFromBaseLayer(BaseLayerT &BaseLayer) {
8788 if (EmitState != NotEmitted)
88 BaseLayer.removeModuleSet(Handle);
89 BaseLayer.removeModule(Handle);
8990 }
9091
9192 void emitAndFinalize(BaseLayerT &BaseLayer) {
99100 BaseLayer.emitAndFinalize(Handle);
100101 }
101102
102 template
103 typename SymbolResolverPtrT>
104 static std::unique_ptr
105 create(BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
103 template
104 static std::unique_ptr
105 create(BaseLayerT &B, std::shared_ptr M, MemoryManagerPtrT MemMgr,
106106 SymbolResolverPtrT Resolver);
107107
108108 protected:
115115 BaseLayerHandleT Handle;
116116 };
117117
118 template
119 typename SymbolResolverPtrT>
120 class EmissionDeferredSetImpl : public EmissionDeferredSet {
118 template
119 class EmissionDeferredModuleImpl : public EmissionDeferredModule {
121120 public:
122 EmissionDeferredSetImpl(ModuleSetT Ms,
123 MemoryManagerPtrT MemMgr,
124 SymbolResolverPtrT Resolver)
125 : Ms(std::move(Ms)), MemMgr(std::move(MemMgr)),
121 EmissionDeferredModuleImpl(std::shared_ptr M,
122 MemoryManagerPtrT MemMgr,
123 SymbolResolverPtrT Resolver)
124 : M(std::move(M)), MemMgr(std::move(MemMgr)),
126125 Resolver(std::move(Resolver)) {}
127126
128127 protected:
153152 // We don't need the mangled names set any more: Once we've emitted this
154153 // to the base layer we'll just look for symbols there.
155154 MangledSymbols.reset();
156 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
157 std::move(Resolver));
155 return BaseLayer.addModule(std::move(M), std::move(MemMgr),
156 std::move(Resolver));
158157 }
159158
160159 private:
196195
197196 auto Symbols = llvm::make_unique>();
198197
199 for (const auto &M : Ms) {
200 Mangler Mang;
201
202 for (const auto &GO : M->global_objects())
198 Mangler Mang;
199
200 for (const auto &GO : M->global_objects())
203201 if (auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
204202 ExportedSymbolsOnly))
205203 return GV;
206 }
207204
208205 MangledSymbols = std::move(Symbols);
209206 return nullptr;
210207 }
211208
212 ModuleSetT Ms;
209 std::shared_ptr M;
213210 MemoryManagerPtrT MemMgr;
214211 SymbolResolverPtrT Resolver;
215212 mutable std::unique_ptr> MangledSymbols;
216213 };
217214
218 using ModuleSetListT = std::list>>;
215 using ModuleListT = std::list>>;
219216
220217 BaseLayerT &BaseLayer;
221 ModuleSetListT ModuleSetList;
218 ModuleListT ModuleList;
222219
223220 public:
224 /// @brief Handle to a set of loaded modules.
225 using ModuleSetHandleT = typename ModuleSetListT::iterator;
221 /// @brief Handle to a loaded module.
222 using ModuleHandleT = typename ModuleListT::iterator;
226223
227224 /// @brief Construct a lazy emitting layer.
228225 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
229226
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.
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.
243240 ///
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);
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);
249246 }
250247
251248 /// @brief Search for the given named symbol.
257254 if (auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
258255 return Symbol;
259256
260 // If not found then search the deferred sets. If any of these contain a
257 // If not found then search the deferred modules. If any of these contain a
261258 // definition of 'Name' then they will return a JITSymbol that will emit
262259 // the corresponding module when the symbol address is requested.
263 for (auto &DeferredSet : ModuleSetList)
264 if (auto Symbol = DeferredSet->find(Name, ExportedSymbolsOnly, BaseLayer))
260 for (auto &DeferredMod : ModuleList)
261 if (auto Symbol = DeferredMod->find(Name, ExportedSymbolsOnly, BaseLayer))
265262 return Symbol;
266263
267264 // If no definition found anywhere return a null symbol.
268265 return nullptr;
269266 }
270267
271 /// @brief Get the address of the given symbol in the context of the set of
268 /// @brief Get the address of the given symbol in the context of the of
272269 /// compiled modules represented by the handle H.
273 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
270 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
274271 bool ExportedSymbolsOnly) {
275272 return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer);
276273 }
277274
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) {
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) {
282279 (*H)->emitAndFinalize(BaseLayer);
283280 }
284281 };
285282
286283 template
287 template
288 typename SymbolResolverPtrT>
289 std::unique_ptr::EmissionDeferredSet>
290 LazyEmittingLayer::EmissionDeferredSet::create(
291 BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
284 template
285 std::unique_ptr::EmissionDeferredModule>
286 LazyEmittingLayer::EmissionDeferredModule::create(
287 BaseLayerT &B, std::shared_ptr M, MemoryManagerPtrT MemMgr,
292288 SymbolResolverPtrT Resolver) {
293 using EDS = EmissionDeferredSetImpl
294 SymbolResolverPtrT>;
295 return llvm::make_unique(std::move(Ms), std::move(MemMgr),
289 using EDS = EmissionDeferredModuleImpl;
290 return llvm::make_unique(std::move(M), std::move(MemMgr),
296291 std::move(Resolver));
297292 }
298293
2828 extern "C" {
2929 #endif
3030
31 typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef;
32 typedef struct LLVMOpaqueSharedObjectBuffer *LLVMSharedObjectBufferRef;
3133 typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef;
3234 typedef uint32_t LLVMOrcModuleHandle;
3335 typedef uint64_t LLVMOrcTargetAddress;
3638 void *CallbackCtx);
3739
3840 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);
3980
4081 /**
4182 * Create an ORC JIT stack.
94135 * Add module to be eagerly compiled.
95136 */
96137 LLVMOrcModuleHandle
97 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
138 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
139 LLVMSharedModuleRef Mod,
98140 LLVMOrcSymbolResolverFn SymbolResolver,
99141 void *SymbolResolverCtx);
100142
102144 * Add module to be lazily compiled one function at a time.
103145 */
104146 LLVMOrcModuleHandle
105 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
147 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
148 LLVMSharedModuleRef Mod,
106149 LLVMOrcSymbolResolverFn SymbolResolver,
107150 void *SymbolResolverCtx);
108151
110153 * Add an object file.
111154 */
112155 LLVMOrcModuleHandle LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
113 LLVMObjectFileRef Obj,
156 LLVMSharedObjectBufferRef Obj,
114157 LLVMOrcSymbolResolverFn SymbolResolver,
115158 void *SymbolResolverCtx);
116159
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 }
1331
1432 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
1533 TargetMachine *TM2(unwrap(TM));
6482 }
6583
6684 LLVMOrcModuleHandle
67 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
85 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
86 LLVMSharedModuleRef Mod,
6887 LLVMOrcSymbolResolverFn SymbolResolver,
6988 void *SymbolResolverCtx) {
7089 OrcCBindingsStack &J = *unwrap(JITStack);
71 Module *M(unwrap(Mod));
72 return J.addIRModuleEager(M, SymbolResolver, SymbolResolverCtx);
90 std::shared_ptr *M(unwrap(Mod));
91 return J.addIRModuleEager(*M, SymbolResolver, SymbolResolverCtx);
7392 }
7493
7594 LLVMOrcModuleHandle
76 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
95 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
96 LLVMSharedModuleRef Mod,
7797 LLVMOrcSymbolResolverFn SymbolResolver,
7898 void *SymbolResolverCtx) {
7999 OrcCBindingsStack &J = *unwrap(JITStack);
80 Module *M(unwrap(Mod));
81 return J.addIRModuleLazy(M, SymbolResolver, SymbolResolverCtx);
100 std::shared_ptr *M(unwrap(Mod));
101 return J.addIRModuleLazy(*M, SymbolResolver, SymbolResolverCtx);
82102 }
83103
84104 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);
4448 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)
4549 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
4650
7074
7175 template class GenericHandleImpl : public GenericHandle {
7276 public:
73 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle)
77 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle)
7478 : Layer(Layer), Handle(std::move(Handle)) {}
7579
7680 JITSymbol findSymbolIn(const std::string &Name,
7882 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
7983 }
8084
81 void removeModule() override { return Layer.removeModuleSet(Handle); }
85 void removeModule() override { return Layer.removeModule(Handle); }
8286
8387 private:
8488 LayerT &Layer;
85 typename LayerT::ModuleSetHandleT Handle;
89 typename LayerT::ModuleHandleT Handle;
8690 };
8791
8892 template
8993 std::unique_ptr>
90 createGenericHandle(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle) {
94 createGenericHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) {
9195 return llvm::make_unique>(Layer,
9296 std::move(Handle));
9397 }
9498
9599 public:
96 // We need a 'ModuleSetHandleT' to conform to the layer concept.
97 using ModuleSetHandleT = unsigned;
98
99100 using ModuleHandleT = unsigned;
100101
101102 OrcCBindingsStack(TargetMachine &TM,
182183 }
183184
184185 template
185 ModuleHandleT addIRModule(LayerT &Layer, Module *M,
186 ModuleHandleT addIRModule(LayerT &Layer, std::shared_ptr M,
186187 std::unique_ptr MemMgr,
187188 LLVMOrcSymbolResolverFn ExternalResolver,
188189 void *ExternalResolverCtx) {
202203 auto Resolver = createResolver(ExternalResolver, ExternalResolverCtx);
203204
204205 // Add the module to the JIT.
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));
206 auto LH = Layer.addModule(std::move(M), std::move(MemMgr),
207 std::move(Resolver));
210208 ModuleHandleT H = createHandle(Layer, LH);
211209
212210 // Run the static constructors, and save the static destructor runner for
219217 return H;
220218 }
221219
222 ModuleHandleT addIRModuleEager(Module *M,
220 ModuleHandleT addIRModuleEager(std::shared_ptr M,
223221 LLVMOrcSymbolResolverFn ExternalResolver,
224222 void *ExternalResolverCtx) {
225223 return addIRModule(CompileLayer, std::move(M),
227225 std::move(ExternalResolver), ExternalResolverCtx);
228226 }
229227
230 ModuleHandleT addIRModuleLazy(Module *M,
228 ModuleHandleT addIRModuleLazy(std::shared_ptr M,
231229 LLVMOrcSymbolResolverFn ExternalResolver,
232230 void *ExternalResolverCtx) {
233231 return addIRModule(CODLayer, std::move(M),
256254
257255 private:
258256 template
259 unsigned createHandle(LayerT &Layer,
260 typename LayerT::ModuleSetHandleT Handle) {
257 unsigned createHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) {
261258 unsigned NewHandle;
262259 if (!FreeHandleIndexes.empty()) {
263260 NewHandle = FreeHandleIndexes.back();
190190 } else {
191191 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
192192 }
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);
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);
197202 }
198203
199204 void addObjectFile(std::unique_ptr O) override {
380385 std::map
381386 UnfinalizedSections;
382387
388 std::map Retain;
389 std::vector> LocalModules;
383390 std::vector> Archives;
384391 };
385392
5353 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
5454 switch (OrcDumpKind) {
5555 case DumpKind::NoDump:
56 return [](std::unique_ptr M) { return M; };
56 return [](std::shared_ptr M) { return M; };
5757
5858 case DumpKind::DumpFuncsToStdOut:
59 return [](std::unique_ptr M) {
59 return [](std::shared_ptr M) {
6060 printf("[ ");
6161
6262 for (const auto &F : *M) {
7575 };
7676
7777 case DumpKind::DumpModsToStdOut:
78 return [](std::unique_ptr M) {
78 return [](std::shared_ptr M) {
7979 outs() << "----- Module Start -----\n" << *M
8080 << "----- Module End -----\n";
8181
8383 };
8484
8585 case DumpKind::DumpModsToDisk:
86 return [](std::unique_ptr M) {
86 return [](std::shared_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 J.addModuleSet(std::move(Ms));
149 for (auto &M : Ms)
150 J.addModule(std::shared_ptr(std::move(M)));
150151 auto MainSym = J.findSymbol("main");
151152
152153 if (!MainSym) {
4949 using ObjLayerT = orc::RTDyldObjectLinkingLayer;
5050 using CompileLayerT = orc::IRCompileLayer;
5151 using TransformFtor =
52 std::function(std::unique_ptr)>;
52 std::function(std::shared_ptr)>;
5353 using IRDumpLayerT = orc::IRTransformLayer;
5454 using CODLayerT = orc::CompileOnDemandLayer;
5555 using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
56 using ModuleSetHandleT = CODLayerT::ModuleSetHandleT;
56 using ModuleHandleT = CODLayerT::ModuleHandleT;
5757
5858 OrcLazyJIT(std::unique_ptr TM,
5959 std::unique_ptr CCMgr,
7676 DtorRunner.runViaLayer(CODLayer);
7777 }
7878
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);
79 void addModule(std::shared_ptr M) {
80 if (M->getDataLayout().isDefault())
81 M->setDataLayout(DL);
8482
8583 // Rename, bump linkage and record static constructors and destructors.
8684 // We have to do this before we hand over ownership of the module to the
8886 std::vector CtorNames, DtorNames;
8987 {
9088 unsigned CtorId = 0, DtorId = 0;
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 }
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);
106102 }
107103 }
108104
110106 // 1) Search the JIT symbols.
111107 // 2) Check for C++ runtime overrides.
112108 // 3) Search the host process (LLI)'s symbol table.
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 );
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 );
127124
128 // Add the module to the JIT.
129 auto H = CODLayer.addModuleSet(std::move(Ms),
130 llvm::make_unique(),
131 std::move(Resolver));
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));
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), H);
135 orc::CtorDtorRunner CtorRunner(std::move(CtorNames),
136 ModulesHandle);
136137 CtorRunner.runViaLayer(CODLayer);
137138
138 IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
139
140 return H;
139 IRStaticDestructorRunners.emplace_back(std::move(DtorNames),
140 ModulesHandle);
141141 }
142142
143143 JITSymbol findSymbol(const std::string &Name) {
144144 return CODLayer.findSymbol(mangle(Name), true);
145145 }
146146
147 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name) {
147 JITSymbol findSymbolIn(ModuleHandleT 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;
181182 };
182183
183184 int runOrcLazyJIT(std::vector> Ms,
1313 namespace {
1414
1515 struct MockBaseLayer {
16 typedef int ModuleSetHandleT;
17 ModuleSetHandleT addModuleSet(
18 std::list>,
16 typedef int ModuleHandleT;
17 ModuleHandleT addModule(
18 std::shared_ptr,
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.addModuleSet(std::list>(), nullptr, nullptr);
29 L.addModule(std::unique_ptr>(), nullptr, nullptr);
3030 }
3131
3232 }
313313 // compile.
314314 NullResolver Resolver;
315315 NullManager Manager;
316 CompileLayer.addModuleSet(std::vector>(), &Manager, &Resolver);
316 CompileLayer.addModule(std::shared_ptr>(), &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 CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, wrap(CCtx->M.get()),
68 myResolver, nullptr);
67 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release()));
68 CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, SM, myResolver, nullptr);
69 LLVMOrcDisposeSharedModuleRef(SM);
6970 CCtx->Compiled = true;
7071 LLVMOrcTargetAddress MainAddr = LLVMOrcGetSymbolAddress(JITStack, "main");
7172 LLVMOrcSetIndirectStubPointer(JITStack, "foo", MainAddr);
8687
8788 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
8889
90 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
8991 LLVMOrcModuleHandle H =
90 LLVMOrcAddEagerlyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr);
92 LLVMOrcAddEagerlyCompiledIR(JIT, SM, myResolver, nullptr);
93 LLVMOrcDisposeSharedModuleRef(SM);
9194 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
9295 int Result = MainFn();
9396 EXPECT_EQ(Result, 42)
110113
111114 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
112115
116 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
113117 LLVMOrcModuleHandle H =
114 LLVMOrcAddLazilyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr);
118 LLVMOrcAddLazilyCompiledIR(JIT, SM, myResolver, nullptr);
119 LLVMOrcDisposeSharedModuleRef(SM);
115120 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
116121 int Result = MainFn();
117122 EXPECT_EQ(Result, 42)
105105 };
106106
107107 template
108 typename AddModuleSetFtor,
109 typename RemoveModuleSetFtor,
108 typename AddModuleFtor,
109 typename RemoveModuleFtor,
110110 typename FindSymbolFtor,
111111 typename FindSymbolInFtor>
112112 class MockBaseLayer {
113113 public:
114114
115 typedef HandleT ModuleSetHandleT;
115 typedef HandleT ModuleHandleT;
116116
117 MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
118 RemoveModuleSetFtor &&RemoveModuleSet,
117 MockBaseLayer(AddModuleFtor &&AddModule,
118 RemoveModuleFtor &&RemoveModule,
119119 FindSymbolFtor &&FindSymbol,
120120 FindSymbolInFtor &&FindSymbolIn)
121 : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
121 : AddModule(AddModule), RemoveModule(RemoveModule),
122122 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
123123 {}
124124
125 template SetT, typename MemoryManagerPtrT,
125 template T, typename MemoryManagerPtrT,
126126 typename SymbolResolverPtrT>
127 ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
128 SymbolResolverPtrT Resolver) {
129 return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
127 ModuleHandleT addModule(ModuleT Ms, MemoryManagerPtrT MemMgr,
128 SymbolResolverPtrT Resolver) {
129 return AddModule(std::move(Ms), std::move(MemMgr), std::move(Resolver));
130130 }
131131
132 void removeModuleSet(ModuleSetHandleT H) {
133 RemoveModuleSet(H);
132 void removeModule(ModuleHandleT H) {
133 RemoveModule(H);
134134 }
135135
136136 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
137137 return FindSymbol(Name, ExportedSymbolsOnly);
138138 }
139139
140 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
140 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
141141 bool ExportedSymbolsOnly) {
142142 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
143143 }
144144
145145 private:
146 AddModuleSetFtor AddModuleSet;
147 RemoveModuleSetFtor RemoveModuleSet;
146 AddModuleFtor AddModule;
147 RemoveModuleFtor RemoveModule;
148148 FindSymbolFtor FindSymbol;
149149 FindSymbolInFtor FindSymbolIn;
150150 };
151151
152 template
153 typename AddModuleSetFtor,
154 typename RemoveModuleSetFtor,
152 template ,
153 typename AddModuleFtor,
154 typename RemoveModuleFtor,
155155 typename FindSymbolFtor,
156156 typename FindSymbolInFtor>
157 MockBaseLayerSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
157 MockBaseLayerHandleT, AddModuleFtor, RemoveModuleFtor,
158158 FindSymbolFtor, FindSymbolInFtor>
159 createMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
160 RemoveModuleSetFtor &&RemoveModuleSet,
159 createMockBaseLayer(AddModuleFtor &&AddModule,
160 RemoveModuleFtor &&RemoveModule,
161161 FindSymbolFtor &&FindSymbol,
162162 FindSymbolInFtor &&FindSymbolIn) {
163 return MockBaseLayerSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
163 return MockBaseLayerHandleT, AddModuleFtor, RemoveModuleFtor,
164164 FindSymbolFtor, FindSymbolInFtor>(
165 std::forward(AddModuleSet),
166 std::forward<RemoveModuleSetFtor>(RemoveModuleSet),
165 std::forward<AddModuleFtor>(AddModule),
166 std::forward(RemoveModule),
167167 std::forward(FindSymbol),
168168 std::forward(FindSymbolIn));
169169 }