llvm.org GIT mirror llvm / d1e7e3c
Revert "[ORC] Switch from shared_ptr to unique_ptr for addModule methods." This reverts commit r327566, it breaks test/ExecutionEngine/OrcMCJIT/test-global-ctors.ll. The test doesn't crash with a stack trace, unfortunately. It merely returns 1 as the exit code. ASan didn't produce a report, and I reproduced this on my Linux machine and Windows box. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327576 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 1 year, 4 months ago
17 changed file(s) with 112 addition(s) and 83 deletion(s). Raw diff Collapse all Expand all
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
7676 std::make_shared(), Resolver};
7777 }),
7878 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
79 OptimizeLayer(CompileLayer, [this](std::unique_ptr M) {
79 OptimizeLayer(CompileLayer, [this](std::shared_ptr M) {
8080 return optimizeModule(std::move(M));
8181 }) {
8282 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
103103 }
104104
105105 private:
106 std::unique_ptr optimizeModule(std::unique_ptr M) {
106 std::shared_ptr optimizeModule(std::shared_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::functionunique_ptr(std::unique_ptr)>;
57 std::functionshared_ptr(std::shared_ptr)>;
5858
5959 IRTransformLayer OptimizeLayer;
6060
7272 }),
7373 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
7474 OptimizeLayer(CompileLayer,
75 [this](std::unique_ptr M) {
75 [this](std::shared_ptr M) {
7676 return optimizeModule(std::move(M));
7777 }),
7878 CompileCallbackManager(
126126 }
127127
128128 private:
129 std::unique_ptr optimizeModule(std::unique_ptr M) {
129 std::shared_ptr optimizeModule(std::shared_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::functionunique_ptr(std::unique_ptr)>;
83 std::functionshared_ptr(std::shared_ptr)>;
8484
8585 IRTransformLayer OptimizeLayer;
8686
112112 }),
113113 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
114114 OptimizeLayer(CompileLayer,
115 [this](std::unique_ptr M) {
115 [this](std::shared_ptr M) {
116116 return optimizeModule(std::move(M));
117117 }),
118118 CompileCallbackMgr(
206206 return MangledNameStream.str();
207207 }
208208
209 std::unique_ptr optimizeModule(std::unique_ptr M) {
209 std::shared_ptr optimizeModule(std::shared_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::functionunique_ptr(std::unique_ptr)>;
88 std::functionshared_ptr(std::shared_ptr)>;
8989
9090 IRTransformLayer OptimizeLayer;
9191
120120 }),
121121 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
122122 OptimizeLayer(CompileLayer,
123 [this](std::unique_ptr M) {
123 [this](std::shared_ptr M) {
124124 return optimizeModule(std::move(M));
125125 }),
126126 Remote(Remote) {
222222 return MangledNameStream.str();
223223 }
224224
225 std::unique_ptr optimizeModule(std::unique_ptr M) {
225 std::shared_ptr optimizeModule(std::shared_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::unique_ptr SourceMod;
140 std::shared_ptr SourceMod;
141141 std::set StubsToClone;
142142 };
143143
152152 StubsMgr(std::move(StubsMgr)) {}
153153
154154 SourceModuleHandle
155 addSourceModule(std::unique_ptr M) {
155 addSourceModule(std::shared_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::unique_ptr M) {
234 Error addModule(VModuleKey K, std::shared_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::unique_ptr M) {
246 Error addExtraModule(VModuleKey K, std::shared_ptr M) {
247247 return addLogicalModule(LogicalDylibs[K], std::move(M));
248248 }
249249
310310
311311 private:
312312
313 Error addLogicalModule(LogicalDylib &LD, std::unique_ptr SrcMPtr) {
313 Error addLogicalModule(LogicalDylib &LD, std::shared_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
4238 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
4339 /// implement the ObjectLayer concept.
44 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile,
45 NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback())
46 : BaseLayer(BaseLayer), Compile(std::move(Compile)),
47 NotifyCompiled(std::move(NotifyCompiled)) {}
40 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
41 : BaseLayer(BaseLayer), Compile(std::move(Compile)) {}
4842
4943 /// @brief Get a reference to the compiler functor.
5044 CompileFtor& getCompiler() { return Compile; }
5145
52 /// @brief (Re)set the NotifyCompiled callback.
53 void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) {
54 this->NotifyCompiled = std::move(NotifyCompiled);
55 }
56
5746 /// @brief Compile the module, and add the resulting object to the base layer
5847 /// along with the given memory manager and symbol resolver.
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();
48 Error addModule(VModuleKey K, std::shared_ptr M) {
49 return BaseLayer.addObject(std::move(K), Compile(*M));
6550 }
6651
6752 /// @brief Remove the module associated with the VModuleKey K.
9681 private:
9782 BaseLayerT &BaseLayer;
9883 CompileFtor Compile;
99 NotifyCompiledCallback NotifyCompiled;
10084 };
10185
10286 } // 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::unique_ptr M) {
42 Error addModule(VModuleKey K, std::shared_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
4443 class EmissionDeferredModule {
4544 public:
46 EmissionDeferredModule(VModuleKey K, std::unique_ptr M)
45 EmissionDeferredModule(VModuleKey K, std::shared_ptr M)
4746 : K(std::move(K)), M(std::move(M)) {}
4847
4948 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
187186
188187 enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted;
189188 VModuleKey K;
190 std::unique_ptr M;
189 std::shared_ptr M;
191190 mutable std::unique_ptr> MangledSymbols;
192191 };
193192
200199 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
201200
202201 /// @brief Add the given module to the lazy emitting layer.
203 Error addModule(VModuleKey K, std::unique_ptr M) {
202 Error addModule(VModuleKey K, std::shared_ptr M) {
204203 assert(!ModuleMap.count(K) && "VModuleKey K already in use");
205204 ModuleMap[K] =
206205 llvm::make_unique(std::move(K), std::move(M));
2828 extern "C" {
2929 #endif
3030
31 typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef;
3132 typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef;
3233 typedef uint64_t LLVMOrcModuleHandle;
3334 typedef uint64_t LLVMOrcTargetAddress;
3637 void *CallbackCtx);
3738
3839 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);
3967
4068 /**
4169 * Create an ORC JIT stack.
97125 LLVMOrcErrorCode
98126 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
99127 LLVMOrcModuleHandle *RetHandle,
100 LLVMModuleRef Mod,
128 LLVMSharedModuleRef Mod,
101129 LLVMOrcSymbolResolverFn SymbolResolver,
102130 void *SymbolResolverCtx);
103131
107135 LLVMOrcErrorCode
108136 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
109137 LLVMOrcModuleHandle *RetHandle,
110 LLVMModuleRef Mod,
138 LLVMSharedModuleRef Mod,
111139 LLVMOrcSymbolResolverFn SymbolResolver,
112140 void *SymbolResolverCtx);
113141
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 }
1321
1422 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
1523 TargetMachine *TM2(unwrap(TM));
6775 LLVMOrcErrorCode
6876 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
6977 LLVMOrcModuleHandle *RetHandle,
70 LLVMModuleRef Mod,
78 LLVMSharedModuleRef Mod,
7179 LLVMOrcSymbolResolverFn SymbolResolver,
7280 void *SymbolResolverCtx) {
7381 OrcCBindingsStack &J = *unwrap(JITStack);
74 std::unique_ptr M(unwrap(Mod));
75 return J.addIRModuleEager(*RetHandle, std::move(M), SymbolResolver,
76 SymbolResolverCtx);
82 std::shared_ptr *M(unwrap(Mod));
83 return J.addIRModuleEager(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
7784 }
7885
7986 LLVMOrcErrorCode
8087 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
8188 LLVMOrcModuleHandle *RetHandle,
82 LLVMModuleRef Mod,
89 LLVMSharedModuleRef Mod,
8390 LLVMOrcSymbolResolverFn SymbolResolver,
8491 void *SymbolResolverCtx) {
8592 OrcCBindingsStack &J = *unwrap(JITStack);
86 std::unique_ptr M(unwrap(Mod));
87 return J.addIRModuleLazy(*RetHandle, std::move(M), SymbolResolver,
88 SymbolResolverCtx);
93 std::shared_ptr *M(unwrap(Mod));
94 return J.addIRModuleLazy(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
8995 }
9096
9197 LLVMOrcErrorCode
4242
4343 class OrcCBindingsStack;
4444
45 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr,
46 LLVMSharedModuleRef)
4547 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)
4648 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
4749
281283 }
282284 template
283285 LLVMOrcErrorCode
284 addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::unique_ptr M,
286 addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::shared_ptr M,
285287 std::unique_ptr MemMgr,
286288 LLVMOrcSymbolResolverFn ExternalResolver,
287289 void *ExternalResolverCtx) {
320322 }
321323
322324 LLVMOrcErrorCode addIRModuleEager(orc::VModuleKey &RetKey,
323 std::unique_ptr M,
325 std::shared_ptr M,
324326 LLVMOrcSymbolResolverFn ExternalResolver,
325327 void *ExternalResolverCtx) {
326328 return addIRModule(RetKey, CompileLayer, std::move(M),
329331 }
330332
331333 LLVMOrcErrorCode addIRModuleLazy(orc::VModuleKey &RetKey,
332 std::unique_ptr M,
334 std::shared_ptr M,
333335 LLVMOrcSymbolResolverFn ExternalResolver,
334336 void *ExternalResolverCtx) {
335337 return addIRModule(RetKey, CODLayer, std::move(M),
124124 }
125125
126126 void OrcMCJITReplacement::runStaticConstructorsDestructors(bool isDtors) {
127 for (auto &M : Modules)
127 for (auto &M : LocalModules)
128128 ExecutionEngine::runStaticConstructorsDestructors(*M, isDtors);
129129 }
130130
5353 namespace orc {
5454
5555 class OrcMCJITReplacement : public ExecutionEngine {
56
5756 // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that
5857 // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are
5958 // expecting - see finalizeMemory.
235234 return ObjectLayerT::Resources{this->MemMgr, this->Resolver};
236235 },
237236 NotifyObjectLoaded, NotifyFinalized),
238 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM),
239 [this](VModuleKey K, std::unique_ptr M) {
240 Modules.push_back(std::move(M));
241 }),
237 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
242238 LazyEmitLayer(CompileLayer) {}
243239
244240 static void Register() {
253249 } else {
254250 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
255251 }
256 cantFail(LazyEmitLayer.addModule(ES.allocateVModule(), std::move(M)));
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()));
257262 }
258263
259264 void addObjectFile(std::unique_ptr O) override {
273278 }
274279
275280 bool removeModule(Module *M) override {
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;
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;
284289 }
285290
286291 uint64_t getSymbolAddress(StringRef Name) {
432437 // delete blocks in LocalModules refer to the ShouldDelete map, so
433438 // LocalModules needs to be destructed before ShouldDelete.
434439 std::map ShouldDelete;
440 std::vector> LocalModules;
435441
436442 NotifyObjectLoadedT NotifyObjectLoaded;
437443 NotifyFinalizedT NotifyFinalized;
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);
147147
148148 // Add the module, look up main and run it.
149149 for (auto &M : Ms)
150 cantFail(J.addModule(std::move(M)));
150 cantFail(J.addModule(std::shared_ptr(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::functionunique_ptr(std::unique_ptr)>;
53 std::functionshared_ptr(std::shared_ptr)>;
5454 using IRDumpLayerT = orc::IRTransformLayer;
5555 using CODLayerT = orc::CompileOnDemandLayer;
5656 using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
105105 }
106106 }
107107
108 Error addModule(std::unique_ptr M) {
108 Error addModule(std::shared_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"
1716 #include "llvm/Object/ObjectFile.h"
1817 #include "gtest/gtest.h"
1918
302301 // Make sure that the calls from IRCompileLayer to ObjectTransformLayer
303302 // compile.
304303 cantFail(CompileLayer.addModule(ES.allocateVModule(),
305 std::unique_ptr()));
304 std::shared_ptr()));
306305
307306 // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer
308307 // compile.
7272 CompileContext *CCtx = static_cast(Ctx);
7373 auto *ET = CCtx->APIExecTest;
7474 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple());
75 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, wrap(CCtx->M.release()),
76 myResolver, nullptr);
75 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release()));
76 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, SM, myResolver, nullptr);
77 LLVMOrcDisposeSharedModuleRef(SM);
7778 CCtx->Compiled = true;
7879 LLVMOrcTargetAddress MainAddr;
7980 LLVMOrcGetSymbolAddress(JITStack, &MainAddr, "main");
9596
9697 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
9798
99 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
98100 LLVMOrcModuleHandle H;
99 LLVMOrcAddEagerlyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr);
101 LLVMOrcAddEagerlyCompiledIR(JIT, &H, SM, myResolver, nullptr);
102 LLVMOrcDisposeSharedModuleRef(SM);
100103 LLVMOrcTargetAddress MainAddr;
101104 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
102105 MainFnTy MainFn = (MainFnTy)MainAddr;
121124
122125 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
123126
127 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
124128 LLVMOrcModuleHandle H;
125 LLVMOrcAddLazilyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr);
129 LLVMOrcAddLazilyCompiledIR(JIT, &H, SM, myResolver, nullptr);
130 LLVMOrcDisposeSharedModuleRef(SM);
126131 LLVMOrcTargetAddress MainAddr;
127132 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
128133 MainFnTy MainFn = (MainFnTy)MainAddr;