llvm.org GIT mirror llvm / 96ae7f8
[ORC] Switch from shared_ptr to unique_ptr for addModule methods. Layer implementations typically mutate module state, and this is better reflected by having layers own the Module they are operating on. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327566 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 1 year, 7 months ago
17 changed file(s) with 83 addition(s) and 112 deletion(s). Raw diff Collapse all Expand all
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
7676 std::make_shared(), Resolver};
7777 }),
7878 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
79 OptimizeLayer(CompileLayer, [this](std::shared_ptr M) {
79 OptimizeLayer(CompileLayer, [this](std::unique_ptr M) {
8080 return optimizeModule(std::move(M));
8181 }) {
8282 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
103103 }
104104
105105 private:
106 std::shared_ptr optimizeModule(std::shared_ptr M) {
106 std::unique_ptr optimizeModule(std::unique_ptr M) {
107107 // Create a function pass manager.
108108 auto FPM = llvm::make_unique(M.get());
109109
5454 IRCompileLayer CompileLayer;
5555
5656 using OptimizeFunction =
57 std::functionshared_ptr(std::shared_ptr)>;
57 std::functionunique_ptr(std::unique_ptr)>;
5858
5959 IRTransformLayer OptimizeLayer;
6060
7272 }),
7373 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
7474 OptimizeLayer(CompileLayer,
75 [this](std::shared_ptr M) {
75 [this](std::unique_ptr M) {
7676 return optimizeModule(std::move(M));
7777 }),
7878 CompileCallbackManager(
126126 }
127127
128128 private:
129 std::shared_ptr optimizeModule(std::shared_ptr M) {
129 std::unique_ptr optimizeModule(std::unique_ptr M) {
130130 // Create a function pass manager.
131131 auto FPM = llvm::make_unique(M.get());
132132
8080 IRCompileLayer CompileLayer;
8181
8282 using OptimizeFunction =
83 std::functionshared_ptr(std::shared_ptr)>;
83 std::functionunique_ptr(std::unique_ptr)>;
8484
8585 IRTransformLayer OptimizeLayer;
8686
112112 }),
113113 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
114114 OptimizeLayer(CompileLayer,
115 [this](std::shared_ptr M) {
115 [this](std::unique_ptr M) {
116116 return optimizeModule(std::move(M));
117117 }),
118118 CompileCallbackMgr(
206206 return MangledNameStream.str();
207207 }
208208
209 std::shared_ptr optimizeModule(std::shared_ptr M) {
209 std::unique_ptr optimizeModule(std::unique_ptr M) {
210210 // Create a function pass manager.
211211 auto FPM = llvm::make_unique(M.get());
212212
8585 IRCompileLayer CompileLayer;
8686
8787 using OptimizeFunction =
88 std::functionshared_ptr(std::shared_ptr)>;
88 std::functionunique_ptr(std::unique_ptr)>;
8989
9090 IRTransformLayer OptimizeLayer;
9191
120120 }),
121121 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
122122 OptimizeLayer(CompileLayer,
123 [this](std::shared_ptr M) {
123 [this](std::unique_ptr M) {
124124 return optimizeModule(std::move(M));
125125 }),
126126 Remote(Remote) {
222222 return MangledNameStream.str();
223223 }
224224
225 std::shared_ptr optimizeModule(std::shared_ptr M) {
225 std::unique_ptr optimizeModule(std::unique_ptr M) {
226226 // Create a function pass manager.
227227 auto FPM = llvm::make_unique(M.get());
228228
137137
138138 struct LogicalDylib {
139139 struct SourceModuleEntry {
140 std::shared_ptr SourceMod;
140 std::unique_ptr SourceMod;
141141 std::set StubsToClone;
142142 };
143143
152152 StubsMgr(std::move(StubsMgr)) {}
153153
154154 SourceModuleHandle
155 addSourceModule(std::shared_ptr M) {
155 addSourceModule(std::unique_ptr M) {
156156 SourceModuleHandle H = SourceModules.size();
157157 SourceModules.push_back(SourceModuleEntry());
158158 SourceModules.back().SourceMod = std::move(M);
231231 }
232232
233233 /// @brief Add a module to the compile-on-demand layer.
234 Error addModule(VModuleKey K, std::shared_ptr M) {
234 Error addModule(VModuleKey K, std::unique_ptr M) {
235235
236236 assert(!LogicalDylibs.count(K) && "VModuleKey K already in use");
237237 auto I = LogicalDylibs.insert(
243243 }
244244
245245 /// @brief Add extra modules to an existing logical module.
246 Error addExtraModule(VModuleKey K, std::shared_ptr M) {
246 Error addExtraModule(VModuleKey K, std::unique_ptr M) {
247247 return addLogicalModule(LogicalDylibs[K], std::move(M));
248248 }
249249
310310
311311 private:
312312
313 Error addLogicalModule(LogicalDylib &LD, std::shared_ptr SrcMPtr) {
313 Error addLogicalModule(LogicalDylib &LD, std::unique_ptr SrcMPtr) {
314314
315315 // Rename all static functions / globals to $static.X :
316316 // This will unique the names across all modules in the logical dylib,
3535 class IRCompileLayer {
3636 public:
3737
38 /// @brief Callback type for notifications when modules are compiled.
39 using NotifyCompiledCallback = std::function
40 std::unique_ptr)>;
41
3842 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
3943 /// implement the ObjectLayer concept.
40 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
41 : BaseLayer(BaseLayer), Compile(std::move(Compile)) {}
44 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile,
45 NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback())
46 : BaseLayer(BaseLayer), Compile(std::move(Compile)),
47 NotifyCompiled(std::move(NotifyCompiled)) {}
4248
4349 /// @brief Get a reference to the compiler functor.
4450 CompileFtor& getCompiler() { return Compile; }
4551
52 /// @brief (Re)set the NotifyCompiled callback.
53 void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) {
54 this->NotifyCompiled = std::move(NotifyCompiled);
55 }
56
4657 /// @brief Compile the module, and add the resulting object to the base layer
4758 /// along with the given memory manager and symbol resolver.
48 Error addModule(VModuleKey K, std::shared_ptr M) {
49 return BaseLayer.addObject(std::move(K), Compile(*M));
59 Error addModule(VModuleKey K, std::unique_ptr M) {
60 if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M)))
61 return Err;
62 if (NotifyCompiled)
63 NotifyCompiled(std::move(K), std::move(M));
64 return Error::success();
5065 }
5166
5267 /// @brief Remove the module associated with the VModuleKey K.
8196 private:
8297 BaseLayerT &BaseLayer;
8398 CompileFtor Compile;
99 NotifyCompiledCallback NotifyCompiled;
84100 };
85101
86102 } // end namespace orc
3939 /// the layer below, along with the memory manager and symbol resolver.
4040 ///
4141 /// @return A handle for the added modules.
42 Error addModule(VModuleKey K, std::shared_ptr M) {
42 Error addModule(VModuleKey K, std::unique_ptr M) {
4343 return BaseLayer.addModule(std::move(K), Transform(std::move(M)));
4444 }
4545
4040 /// JITSymbol::getAddress) for a symbol contained in this layer.
4141 template class LazyEmittingLayer {
4242 private:
43
4344 class EmissionDeferredModule {
4445 public:
45 EmissionDeferredModule(VModuleKey K, std::shared_ptr M)
46 EmissionDeferredModule(VModuleKey K, std::unique_ptr M)
4647 : K(std::move(K)), M(std::move(M)) {}
4748
4849 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
186187
187188 enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted;
188189 VModuleKey K;
189 std::shared_ptr M;
190 std::unique_ptr M;
190191 mutable std::unique_ptr> MangledSymbols;
191192 };
192193
199200 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
200201
201202 /// @brief Add the given module to the lazy emitting layer.
202 Error addModule(VModuleKey K, std::shared_ptr M) {
203 Error addModule(VModuleKey K, std::unique_ptr M) {
203204 assert(!ModuleMap.count(K) && "VModuleKey K already in use");
204205 ModuleMap[K] =
205206 llvm::make_unique(std::move(K), std::move(M));
2828 extern "C" {
2929 #endif
3030
31 typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef;
3231 typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef;
3332 typedef uint64_t LLVMOrcModuleHandle;
3433 typedef uint64_t LLVMOrcTargetAddress;
3736 void *CallbackCtx);
3837
3938 typedef enum { LLVMOrcErrSuccess = 0, LLVMOrcErrGeneric } LLVMOrcErrorCode;
40
41 /**
42 * Turn an LLVMModuleRef into an LLVMSharedModuleRef.
43 *
44 * The JIT uses shared ownership for LLVM modules, since it is generally
45 * difficult to know when the JIT will be finished with a module (and the JIT
46 * has no way of knowing when a user may be finished with one).
47 *
48 * Calling this method with an LLVMModuleRef creates a shared-pointer to the
49 * module, and returns a reference to this shared pointer.
50 *
51 * The shared module should be disposed when finished with by calling
52 * LLVMOrcDisposeSharedModule (not LLVMDisposeModule). The Module will be
53 * deleted when the last shared pointer owner relinquishes it.
54 */
55
56 LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod);
57
58 /**
59 * Dispose of a shared module.
60 *
61 * The module should not be accessed after this call. The module will be
62 * deleted once all clients (including the JIT itself) have released their
63 * shared pointers.
64 */
65
66 void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod);
6739
6840 /**
6941 * Create an ORC JIT stack.
12597 LLVMOrcErrorCode
12698 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
12799 LLVMOrcModuleHandle *RetHandle,
128 LLVMSharedModuleRef Mod,
100 LLVMModuleRef Mod,
129101 LLVMOrcSymbolResolverFn SymbolResolver,
130102 void *SymbolResolverCtx);
131103
135107 LLVMOrcErrorCode
136108 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
137109 LLVMOrcModuleHandle *RetHandle,
138 LLVMSharedModuleRef Mod,
110 LLVMModuleRef Mod,
139111 LLVMOrcSymbolResolverFn SymbolResolver,
140112 void *SymbolResolverCtx);
141113
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 }
2113
2214 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
2315 TargetMachine *TM2(unwrap(TM));
7567 LLVMOrcErrorCode
7668 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
7769 LLVMOrcModuleHandle *RetHandle,
78 LLVMSharedModuleRef Mod,
70 LLVMModuleRef Mod,
7971 LLVMOrcSymbolResolverFn SymbolResolver,
8072 void *SymbolResolverCtx) {
8173 OrcCBindingsStack &J = *unwrap(JITStack);
82 std::shared_ptr *M(unwrap(Mod));
83 return J.addIRModuleEager(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
74 std::unique_ptr M(unwrap(Mod));
75 return J.addIRModuleEager(*RetHandle, std::move(M), SymbolResolver,
76 SymbolResolverCtx);
8477 }
8578
8679 LLVMOrcErrorCode
8780 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
8881 LLVMOrcModuleHandle *RetHandle,
89 LLVMSharedModuleRef Mod,
82 LLVMModuleRef Mod,
9083 LLVMOrcSymbolResolverFn SymbolResolver,
9184 void *SymbolResolverCtx) {
9285 OrcCBindingsStack &J = *unwrap(JITStack);
93 std::shared_ptr *M(unwrap(Mod));
94 return J.addIRModuleLazy(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
86 std::unique_ptr M(unwrap(Mod));
87 return J.addIRModuleLazy(*RetHandle, std::move(M), SymbolResolver,
88 SymbolResolverCtx);
9589 }
9690
9791 LLVMOrcErrorCode
4242
4343 class OrcCBindingsStack;
4444
45 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr,
46 LLVMSharedModuleRef)
4745 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)
4846 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
4947
283281 }
284282 template
285283 LLVMOrcErrorCode
286 addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::shared_ptr M,
284 addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::unique_ptr M,
287285 std::unique_ptr MemMgr,
288286 LLVMOrcSymbolResolverFn ExternalResolver,
289287 void *ExternalResolverCtx) {
322320 }
323321
324322 LLVMOrcErrorCode addIRModuleEager(orc::VModuleKey &RetKey,
325 std::shared_ptr M,
323 std::unique_ptr M,
326324 LLVMOrcSymbolResolverFn ExternalResolver,
327325 void *ExternalResolverCtx) {
328326 return addIRModule(RetKey, CompileLayer, std::move(M),
331329 }
332330
333331 LLVMOrcErrorCode addIRModuleLazy(orc::VModuleKey &RetKey,
334 std::shared_ptr M,
332 std::unique_ptr M,
335333 LLVMOrcSymbolResolverFn ExternalResolver,
336334 void *ExternalResolverCtx) {
337335 return addIRModule(RetKey, CODLayer, std::move(M),
124124 }
125125
126126 void OrcMCJITReplacement::runStaticConstructorsDestructors(bool isDtors) {
127 for (auto &M : LocalModules)
127 for (auto &M : Modules)
128128 ExecutionEngine::runStaticConstructorsDestructors(*M, isDtors);
129129 }
130130
5353 namespace orc {
5454
5555 class OrcMCJITReplacement : public ExecutionEngine {
56
5657 // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that
5758 // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are
5859 // expecting - see finalizeMemory.
234235 return ObjectLayerT::Resources{this->MemMgr, this->Resolver};
235236 },
236237 NotifyObjectLoaded, NotifyFinalized),
237 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
238 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM),
239 [this](VModuleKey K, std::unique_ptr M) {
240 Modules.push_back(std::move(M));
241 }),
238242 LazyEmitLayer(CompileLayer) {}
239243
240244 static void Register() {
249253 } else {
250254 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
251255 }
252 auto *MPtr = M.release();
253 ShouldDelete[MPtr] = true;
254 auto Deleter = [this](Module *Mod) {
255 auto I = ShouldDelete.find(Mod);
256 if (I != ShouldDelete.end() && I->second)
257 delete Mod;
258 };
259 LocalModules.push_back(std::shared_ptr(MPtr, std::move(Deleter)));
260 cantFail(
261 LazyEmitLayer.addModule(ES.allocateVModule(), LocalModules.back()));
256 cantFail(LazyEmitLayer.addModule(ES.allocateVModule(), std::move(M)));
262257 }
263258
264259 void addObjectFile(std::unique_ptr O) override {
278273 }
279274
280275 bool removeModule(Module *M) override {
281 for (auto I = LocalModules.begin(), E = LocalModules.end(); I != E; ++I) {
282 if (I->get() == M) {
283 ShouldDelete[M] = false;
284 LocalModules.erase(I);
285 return true;
286 }
287 }
288 return false;
276 auto I = Modules.begin();
277 for (auto E = Modules.end(); I != E; ++I)
278 if (I->get() == M)
279 break;
280 if (I == Modules.end())
281 return false;
282 Modules.erase(I);
283 return true;
289284 }
290285
291286 uint64_t getSymbolAddress(StringRef Name) {
437432 // delete blocks in LocalModules refer to the ShouldDelete map, so
438433 // LocalModules needs to be destructed before ShouldDelete.
439434 std::map ShouldDelete;
440 std::vector> LocalModules;
441435
442436 NotifyObjectLoadedT NotifyObjectLoaded;
443437 NotifyFinalizedT NotifyFinalized;
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);
147147
148148 // Add the module, look up main and run it.
149149 for (auto &M : Ms)
150 cantFail(J.addModule(std::shared_ptr(std::move(M))));
150 cantFail(J.addModule(std::move(M)));
151151
152152 if (auto MainSym = J.findSymbol("main")) {
153153 typedef int (*MainFnPtr)(int, const char*[]);
5050 using ObjLayerT = orc::RTDyldObjectLinkingLayer;
5151 using CompileLayerT = orc::IRCompileLayer;
5252 using TransformFtor =
53 std::functionshared_ptr(std::shared_ptr)>;
53 std::functionunique_ptr(std::unique_ptr)>;
5454 using IRDumpLayerT = orc::IRTransformLayer;
5555 using CODLayerT = orc::CompileOnDemandLayer;
5656 using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
105105 }
106106 }
107107
108 Error addModule(std::shared_ptr M) {
108 Error addModule(std::unique_ptr M) {
109109 if (M->getDataLayout().isDefault())
110110 M->setDataLayout(DL);
111111
1313 #include "llvm/ExecutionEngine/Orc/NullResolver.h"
1414 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
1515 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
16 #include "llvm/IR/Module.h"
1617 #include "llvm/Object/ObjectFile.h"
1718 #include "gtest/gtest.h"
1819
301302 // Make sure that the calls from IRCompileLayer to ObjectTransformLayer
302303 // compile.
303304 cantFail(CompileLayer.addModule(ES.allocateVModule(),
304 std::shared_ptr()));
305 std::unique_ptr()));
305306
306307 // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer
307308 // compile.
7272 CompileContext *CCtx = static_cast(Ctx);
7373 auto *ET = CCtx->APIExecTest;
7474 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple());
75 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release()));
76 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, SM, myResolver, nullptr);
77 LLVMOrcDisposeSharedModuleRef(SM);
75 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, wrap(CCtx->M.release()),
76 myResolver, nullptr);
7877 CCtx->Compiled = true;
7978 LLVMOrcTargetAddress MainAddr;
8079 LLVMOrcGetSymbolAddress(JITStack, &MainAddr, "main");
9695
9796 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
9897
99 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
10098 LLVMOrcModuleHandle H;
101 LLVMOrcAddEagerlyCompiledIR(JIT, &H, SM, myResolver, nullptr);
102 LLVMOrcDisposeSharedModuleRef(SM);
99 LLVMOrcAddEagerlyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr);
103100 LLVMOrcTargetAddress MainAddr;
104101 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
105102 MainFnTy MainFn = (MainFnTy)MainAddr;
124121
125122 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
126123
127 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
128124 LLVMOrcModuleHandle H;
129 LLVMOrcAddLazilyCompiledIR(JIT, &H, SM, myResolver, nullptr);
130 LLVMOrcDisposeSharedModuleRef(SM);
125 LLVMOrcAddLazilyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr);
131126 LLVMOrcTargetAddress MainAddr;
132127 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
133128 MainFnTy MainFn = (MainFnTy)MainAddr;