diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h index f99722f60e91..3274433178ba 100644 --- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h +++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h @@ -44,7 +44,7 @@ private: IRCompileLayer CompileLayer; public: - using ModuleHandle = decltype(CompileLayer)::ModuleHandleT; + using ModuleHandle = decltype(CompileLayer)::ModuleSetHandleT; KaleidoscopeJIT() : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), @@ -72,11 +72,15 @@ public: return JITSymbol(nullptr); }); + // Build a singleton module set to hold our module. + std::vector> Ms; + Ms.push_back(std::move(M)); + // Add the set to the JIT with the resolver we created above and a newly // created SectionMemoryManager. - return CompileLayer.addModule(std::move(M), - make_unique(), - std::move(Resolver)); + return CompileLayer.addModuleSet(std::move(Ms), + make_unique(), + std::move(Resolver)); } JITSymbol findSymbol(const std::string Name) { @@ -87,7 +91,7 @@ public: } void removeModule(ModuleHandle H) { - CompileLayer.removeModule(H); + CompileLayer.removeModuleSet(H); } }; diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h index 2cd4ed79aafa..f71b322bff70 100644 --- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h +++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h @@ -48,18 +48,18 @@ private: IRCompileLayer CompileLayer; using OptimizeFunction = - std::function(std::shared_ptr)>; + std::function(std::unique_ptr)>; IRTransformLayer OptimizeLayer; public: - using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT; + using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT; KaleidoscopeJIT() : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), CompileLayer(ObjectLayer, SimpleCompiler(*TM)), OptimizeLayer(CompileLayer, - [this](std::shared_ptr M) { + [this](std::unique_ptr M) { return optimizeModule(std::move(M)); }) { llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); @@ -85,11 +85,15 @@ public: return JITSymbol(nullptr); }); + // Build a singleton module set to hold our module. + std::vector> Ms; + Ms.push_back(std::move(M)); + // Add the set to the JIT with the resolver we created above and a newly // created SectionMemoryManager. - return OptimizeLayer.addModule(std::move(M), - make_unique(), - std::move(Resolver)); + return OptimizeLayer.addModuleSet(std::move(Ms), + make_unique(), + std::move(Resolver)); } JITSymbol findSymbol(const std::string Name) { @@ -100,11 +104,11 @@ public: } void removeModule(ModuleHandle H) { - OptimizeLayer.removeModule(H); + OptimizeLayer.removeModuleSet(H); } private: - std::shared_ptr optimizeModule(std::shared_ptr M) { + std::unique_ptr optimizeModule(std::unique_ptr M) { // Create a function pass manager. auto FPM = llvm::make_unique(M.get()); diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h index f6fb3071d526..c851b6097799 100644 --- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h +++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h @@ -51,7 +51,7 @@ private: IRCompileLayer CompileLayer; using OptimizeFunction = - std::function(std::shared_ptr)>; + std::function(std::unique_ptr)>; IRTransformLayer OptimizeLayer; @@ -59,13 +59,13 @@ private: CompileOnDemandLayer CODLayer; public: - using ModuleHandle = decltype(CODLayer)::ModuleHandleT; + using ModuleHandle = decltype(CODLayer)::ModuleSetHandleT; KaleidoscopeJIT() : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), CompileLayer(ObjectLayer, SimpleCompiler(*TM)), OptimizeLayer(CompileLayer, - [this](std::shared_ptr M) { + [this](std::unique_ptr M) { return optimizeModule(std::move(M)); }), CompileCallbackManager( @@ -98,11 +98,15 @@ public: return JITSymbol(nullptr); }); + // Build a singleton module set to hold our module. + std::vector> Ms; + Ms.push_back(std::move(M)); + // Add the set to the JIT with the resolver we created above and a newly // created SectionMemoryManager. - return CODLayer.addModule(std::move(M), - make_unique(), - std::move(Resolver)); + return CODLayer.addModuleSet(std::move(Ms), + make_unique(), + std::move(Resolver)); } JITSymbol findSymbol(const std::string Name) { @@ -113,11 +117,11 @@ public: } void removeModule(ModuleHandle H) { - CODLayer.removeModule(H); + CODLayer.removeModuleSet(H); } private: - std::shared_ptr optimizeModule(std::shared_ptr M) { + std::unique_ptr optimizeModule(std::unique_ptr M) { // Create a function pass manager. auto FPM = llvm::make_unique(M.get()); diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h index d45874e9a693..58642237d4f1 100644 --- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h +++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h @@ -77,7 +77,7 @@ private: IRCompileLayer CompileLayer; using OptimizeFunction = - std::function(std::shared_ptr)>; + std::function(std::unique_ptr)>; IRTransformLayer OptimizeLayer; @@ -85,14 +85,14 @@ private: std::unique_ptr IndirectStubsMgr; public: - using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT; + using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT; KaleidoscopeJIT() : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), CompileLayer(ObjectLayer, SimpleCompiler(*TM)), OptimizeLayer(CompileLayer, - [this](std::shared_ptr M) { + [this](std::unique_ptr M) { return optimizeModule(std::move(M)); }), CompileCallbackMgr( @@ -125,11 +125,15 @@ public: return JITSymbol(nullptr); }); + // Build a singleton module set to hold our module. + std::vector> Ms; + Ms.push_back(std::move(M)); + // Add the set to the JIT with the resolver we created above and a newly // created SectionMemoryManager. - return OptimizeLayer.addModule(std::move(M), - make_unique(), - std::move(Resolver)); + return OptimizeLayer.addModuleSet(std::move(Ms), + make_unique(), + std::move(Resolver)); } Error addFunctionAST(std::unique_ptr FnAST) { @@ -195,7 +199,7 @@ public: } void removeModule(ModuleHandle H) { - OptimizeLayer.removeModule(H); + OptimizeLayer.removeModuleSet(H); } private: @@ -206,7 +210,7 @@ private: return MangledNameStream.str(); } - std::shared_ptr optimizeModule(std::shared_ptr M) { + std::unique_ptr optimizeModule(std::unique_ptr M) { // Create a function pass manager. auto FPM = llvm::make_unique(M.get()); diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h index e889c6d34322..84916b8f2b88 100644 --- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h +++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h @@ -82,7 +82,7 @@ private: IRCompileLayer CompileLayer; using OptimizeFunction = - std::function(std::shared_ptr)>; + std::function(std::unique_ptr)>; IRTransformLayer OptimizeLayer; @@ -91,7 +91,7 @@ private: MyRemote &Remote; public: - using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT; + using ModuleHandle = decltype(OptimizeLayer)::ModuleSetHandleT; KaleidoscopeJIT(MyRemote &Remote) : TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "", @@ -99,7 +99,7 @@ public: DL(TM->createDataLayout()), CompileLayer(ObjectLayer, SimpleCompiler(*TM)), OptimizeLayer(CompileLayer, - [this](std::shared_ptr M) { + [this](std::unique_ptr M) { return optimizeModule(std::move(M)); }), Remote(Remote) { @@ -153,11 +153,15 @@ public: exit(1); } + // Build a singleton module set to hold our module. + std::vector> Ms; + Ms.push_back(std::move(M)); + // Add the set to the JIT with the resolver we created above and a newly // created SectionMemoryManager. - return OptimizeLayer.addModule(std::move(M), - std::move(MemMgr), - std::move(Resolver)); + return OptimizeLayer.addModuleSet(std::move(Ms), + std::move(MemMgr), + std::move(Resolver)); } Error addFunctionAST(std::unique_ptr FnAST) { @@ -227,7 +231,7 @@ public: } void removeModule(ModuleHandle H) { - OptimizeLayer.removeModule(H); + OptimizeLayer.removeModuleSet(H); } private: @@ -238,7 +242,7 @@ private: return MangledNameStream.str(); } - std::shared_ptr optimizeModule(std::shared_ptr M) { + std::unique_ptr optimizeModule(std::unique_ptr M) { // Create a function pass manager. auto FPM = llvm::make_unique(M.get()); diff --git a/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h index fe73d717976d..1e2d567c057e 100644 --- a/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h +++ b/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h @@ -41,7 +41,7 @@ class KaleidoscopeJIT { public: using ObjLayerT = RTDyldObjectLinkingLayer; using CompileLayerT = IRCompileLayer; - using ModuleHandleT = CompileLayerT::ModuleHandleT; + using ModuleHandleT = CompileLayerT::ModuleSetHandleT; KaleidoscopeJIT() : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), @@ -62,9 +62,9 @@ public: return JITSymbol(nullptr); }, [](const std::string &S) { return nullptr; }); - auto H = CompileLayer.addModule(std::move(M), - make_unique(), - std::move(Resolver)); + auto H = CompileLayer.addModuleSet(singletonSet(std::move(M)), + make_unique(), + std::move(Resolver)); ModuleHandles.push_back(H); return H; @@ -72,7 +72,7 @@ public: void removeModule(ModuleHandleT H) { ModuleHandles.erase(find(ModuleHandles, H)); - CompileLayer.removeModule(H); + CompileLayer.removeModuleSet(H); } JITSymbol findSymbol(const std::string Name) { @@ -89,6 +89,12 @@ private: return MangledName; } + template static std::vector singletonSet(T t) { + std::vector Vec; + Vec.push_back(std::move(t)); + return Vec; + } + JITSymbol findMangledSymbol(const std::string &Name) { #ifdef LLVM_ON_WIN32 // The symbol lookup of ObjectLinkingLayer uses the SymbolRef::SF_Exported diff --git a/llvm/include/llvm-c/OrcBindings.h b/llvm/include/llvm-c/OrcBindings.h index d86ea8808889..de2969ab1c9b 100644 --- a/llvm/include/llvm-c/OrcBindings.h +++ b/llvm/include/llvm-c/OrcBindings.h @@ -29,8 +29,6 @@ extern "C" { #endif -typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef; -typedef struct LLVMOpaqueSharedObjectBuffer *LLVMSharedObjectBufferRef; typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef; typedef uint32_t LLVMOrcModuleHandle; typedef uint64_t LLVMOrcTargetAddress; @@ -40,45 +38,6 @@ typedef uint64_t (*LLVMOrcLazyCompileCallbackFn)(LLVMOrcJITStackRef JITStack, typedef enum { LLVMOrcErrSuccess = 0, LLVMOrcErrGeneric } LLVMOrcErrorCode; -/** - * Turn an LLVMModuleRef into an LLVMSharedModuleRef. - * - * The JIT uses shared ownership for LLVM modules, since it is generally - * difficult to know when the JIT will be finished with a module (and the JIT - * has no way of knowing when a user may be finished with one). - * - * Calling this method with an LLVMModuleRef creates a shared-pointer to the - * module, and returns a reference to this shared pointer. - * - * The shared module should be disposed when finished with by calling - * LLVMOrcDisposeSharedModule (not LLVMDisposeModule). The Module will be - * deleted when the last shared pointer owner relinquishes it. - */ - -LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod); - -/** - * Dispose of a shared module. - * - * The module should not be accessed after this call. The module will be - * deleted once all clients (including the JIT itself) have released their - * shared pointers. - */ - -void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod); - -/** - * Get an LLVMSharedObjectBufferRef from an LLVMMemoryBufferRef. - */ -LLVMSharedObjectBufferRef -LLVMOrcMakeSharedObjectBuffer(LLVMMemoryBufferRef ObjBuffer); - -/** - * Dispose of a shared object buffer. - */ -void -LLVMOrcDisposeSharedObjectBufferRef(LLVMSharedObjectBufferRef SharedObjBuffer); - /** * Create an ORC JIT stack. * @@ -136,8 +95,7 @@ LLVMOrcErrorCode LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack, * Add module to be eagerly compiled. */ LLVMOrcModuleHandle -LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, - LLVMSharedModuleRef Mod, +LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx); @@ -145,8 +103,7 @@ LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, * Add module to be lazily compiled one function at a time. */ LLVMOrcModuleHandle -LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, - LLVMSharedModuleRef Mod, +LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx); @@ -154,7 +111,7 @@ LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, * Add an object file. */ LLVMOrcModuleHandle LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack, - LLVMSharedObjectBufferRef Obj, + LLVMObjectFileRef Obj, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx); diff --git a/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h index 8ac1b6bca0a7..f2dc13be10fd 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h @@ -84,7 +84,7 @@ private: return LambdaMaterializer(std::move(M)); } - using BaseLayerModuleHandleT = typename BaseLayerT::ModuleHandleT; + using BaseLayerModuleSetHandleT = typename BaseLayerT::ModuleSetHandleT; // Provide type-erasure for the Modules and MemoryManagers. template @@ -139,11 +139,9 @@ private: struct LogicalDylib { using SymbolResolverFtor = std::function; - using ModuleAdderFtor = - std::function, - std::unique_ptr)>; + using ModuleAdderFtor = std::function, + std::unique_ptr)>; struct SourceModuleEntry { std::unique_ptr> SourceMod; @@ -181,7 +179,7 @@ private: void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) { for (auto &BLH : BaseLayerHandles) - BaseLayer.removeModule(BLH); + BaseLayer.removeModuleSet(BLH); } std::unique_ptr ExternalSymbolResolver; @@ -190,14 +188,14 @@ private: StaticGlobalRenamer StaticRenamer; ModuleAdderFtor ModuleAdder; SourceModulesList SourceModules; - std::vector BaseLayerHandles; + std::vector BaseLayerHandles; }; using LogicalDylibList = std::list; public: - /// @brief Handle to loaded module. - using ModuleHandleT = typename LogicalDylibList::iterator; + /// @brief Handle to a set of loaded modules. + using ModuleSetHandleT = typename LogicalDylibList::iterator; /// @brief Module partitioning functor. using PartitioningFtor = std::function(Function&)>; @@ -218,15 +216,15 @@ public: ~CompileOnDemandLayer() { while (!LogicalDylibs.empty()) - removeModule(LogicalDylibs.begin()); + removeModuleSet(LogicalDylibs.begin()); } - + /// @brief Add a module to the compile-on-demand layer. - template - ModuleHandleT addModule(std::shared_ptr M, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - + template + ModuleSetHandleT addModuleSet(ModuleSetT Ms, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { LogicalDylibs.push_back(LogicalDylib()); auto &LD = LogicalDylibs.back(); LD.ExternalSymbolResolver = std::move(Resolver); @@ -238,25 +236,23 @@ public: LD.ModuleAdder = [&MemMgrRef](BaseLayerT &B, std::unique_ptr M, std::unique_ptr R) { - return B.addModule(std::move(M), &MemMgrRef, std::move(R)); + std::vector> Ms; + Ms.push_back(std::move(M)); + return B.addModuleSet(std::move(Ms), &MemMgrRef, std::move(R)); }; // Process each of the modules in this module set. - addLogicalModule(LogicalDylibs.back(), std::move(M)); + for (auto &M : Ms) + addLogicalModule(LogicalDylibs.back(), std::move(M)); return std::prev(LogicalDylibs.end()); } - /// @brief Add extra modules to an existing logical module. - void addExtraModule(ModuleHandleT H, std::shared_ptr M) { - addLogicalModule(*H, std::move(M)); - } - /// @brief Remove the module represented by the given handle. /// /// This will remove all modules in the layers below that were derived from /// the module represented by H. - void removeModule(ModuleHandleT H) { + void removeModuleSet(ModuleSetHandleT H) { H->removeModulesFromBaseLayer(BaseLayer); LogicalDylibs.erase(H); } @@ -278,7 +274,7 @@ public: /// @brief Get the address of a symbol provided by this layer, or some layer /// below this one. - JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, + JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly); } @@ -502,7 +498,7 @@ private: } template - BaseLayerModuleHandleT + BaseLayerModuleSetHandleT emitPartition(LogicalDylib &LD, typename LogicalDylib::SourceModuleHandle LMId, const PartitionT &Part) { diff --git a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h index bf8cca406844..36a41891614a 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h @@ -94,7 +94,7 @@ public: /// @brief Construct a CtorDtorRunner for the given range using the given /// name mangling function. CtorDtorRunner(std::vector CtorDtorNames, - typename JITLayerT::ModuleHandleT H) + typename JITLayerT::ModuleSetHandleT H) : CtorDtorNames(std::move(CtorDtorNames)), H(H) {} /// @brief Run the recorded constructors/destructors through the given JIT @@ -116,7 +116,7 @@ public: private: std::vector CtorDtorNames; - typename JITLayerT::ModuleHandleT H; + typename JITLayerT::ModuleSetHandleT H; }; /// @brief Support class for static dtor execution. For hosted (in-process) JITs diff --git a/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h index 99ccd4d221a5..fc1db346af6d 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h @@ -28,15 +28,15 @@ namespace orc { /// @brief Eager IR compiling layer. /// -/// This layer immediately compiles each IR module added via addModule to an -/// object file and adds this module file to the layer below, which must -/// implement the object layer concept. +/// This layer accepts sets of LLVM IR Modules (via addModuleSet). It +/// immediately compiles each IR module to an object file (each IR Module is +/// compiled separately). The resulting set of object files is then added to +/// the layer below, which must implement the object layer concept. template class IRCompileLayer { public: - - /// @brief Handle to a compiled module. - using ModuleHandleT = typename BaseLayerT::ObjHandleT; + /// @brief Handle to a set of compiled modules. + using ModuleSetHandleT = typename BaseLayerT::ObjHandleT; /// @brief Construct an IRCompileLayer with the given BaseLayer, which must /// implement the ObjectLayer concept. @@ -46,22 +46,25 @@ public: /// @brief Get a reference to the compiler functor. CompileFtor& getCompiler() { return Compile; } - /// @brief Compile the module, and add the resulting object to the base layer - /// along with the given memory manager and symbol resolver. + /// @brief Compile each module in the given module set, then add the resulting + /// set of objects to the base layer along with the memory manager and + /// symbol resolver. /// - /// @return A handle for the added module. - template - ModuleHandleT addModule(std::shared_ptr M, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - using CompileResult = decltype(Compile(*M)); - auto Obj = std::make_shared(Compile(*M)); + /// @return A handle for the added modules. + template + ModuleSetHandleT addModuleSet(ModuleSetT Ms, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + assert(Ms.size() == 1); + using CompileResult = decltype(Compile(*Ms.front())); + auto Obj = std::make_shared(Compile(*Ms.front())); return BaseLayer.addObject(std::move(Obj), std::move(MemMgr), std::move(Resolver)); } - /// @brief Remove the module associated with the handle H. - void removeModule(ModuleHandleT H) { BaseLayer.removeObject(H); } + /// @brief Remove the module set associated with the handle H. + void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeObject(H); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -71,23 +74,23 @@ public: return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); } - /// @brief Get the address of the given symbol in compiled module represented - /// by the handle H. This call is forwarded to the base layer's - /// implementation. - /// @param H The handle for the module to search in. + /// @brief Get the address of the given symbol in the context of the set of + /// compiled modules represented by the handle H. This call is + /// forwarded to the base layer's implementation. + /// @param H The handle for the module set to search in. /// @param Name The name of the symbol to search for. /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it is found in the - /// given module. - JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, + /// given module set. + JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); } - /// @brief Immediately emit and finalize the module represented by the given - /// handle. - /// @param H Handle for module to emit/finalize. - void emitAndFinalize(ModuleHandleT H) { + /// @brief Immediately emit and finalize the moduleOB set represented by the + /// given handle. + /// @param H Handle for module set to emit/finalize. + void emitAndFinalize(ModuleSetHandleT H) { BaseLayer.emitAndFinalize(H); } diff --git a/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h index 8e9be6b6f4fe..057309ee09b7 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h @@ -22,34 +22,39 @@ namespace orc { /// @brief IR mutating layer. /// -/// This layer applies a user supplied transform to each module that is added, -/// then adds the transformed module to the layer below. +/// This layer accepts sets of LLVM IR Modules (via addModuleSet). It +/// immediately applies the user supplied functor to each module, then adds +/// the set of transformed modules to the layer below. template class IRTransformLayer { public: - /// @brief Handle to a set of added modules. - using ModuleHandleT = typename BaseLayerT::ModuleHandleT; + using ModuleSetHandleT = typename BaseLayerT::ModuleSetHandleT; /// @brief Construct an IRTransformLayer with the given BaseLayer IRTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform = TransformFtor()) : BaseLayer(BaseLayer), Transform(std::move(Transform)) {} - /// @brief Apply the transform functor to the module, then add the module to - /// the layer below, along with the memory manager and symbol resolver. + /// @brief Apply the transform functor to each module in the module set, then + /// add the resulting set of modules to the base layer, along with the + /// memory manager and symbol resolver. /// /// @return A handle for the added modules. - template - ModuleHandleT addModule(std::shared_ptr M, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - return BaseLayer.addModule(Transform(std::move(M)), std::move(MemMgr), - std::move(Resolver)); + template + ModuleSetHandleT addModuleSet(ModuleSetT Ms, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + for (auto I = Ms.begin(), E = Ms.end(); I != E; ++I) + *I = Transform(std::move(*I)); + + return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr), + std::move(Resolver)); } - /// @brief Remove the module associated with the handle H. - void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); } + /// @brief Remove the module set associated with the handle H. + void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -59,23 +64,23 @@ public: return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); } - /// @brief Get the address of the given symbol in the context of the module - /// represented by the handle H. This call is forwarded to the base - /// layer's implementation. - /// @param H The handle for the module to search in. + /// @brief Get the address of the given symbol in the context of the set of + /// modules represented by the handle H. This call is forwarded to the + /// base layer's implementation. + /// @param H The handle for the module set to search in. /// @param Name The name of the symbol to search for. /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it is found in the - /// given module. - JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, + /// given module set. + JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); } - /// @brief Immediately emit and finalize the module represented by the given - /// handle. - /// @param H Handle for module to emit/finalize. - void emitAndFinalize(ModuleHandleT H) { + /// @brief Immediately emit and finalize the module set represented by the + /// given handle. + /// @param H Handle for module set to emit/finalize. + void emitAndFinalize(ModuleSetHandleT H) { BaseLayer.emitAndFinalize(H); } diff --git a/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h index 38769aac12af..b44e69d1412b 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h @@ -34,20 +34,19 @@ namespace orc { /// @brief Lazy-emitting IR layer. /// -/// This layer accepts LLVM IR Modules (via addModule), but does not -/// immediately emit them the layer below. Instead, emissing to the base layer -/// is deferred until the first time the client requests the address (via -/// JITSymbol::getAddress) for a symbol contained in this layer. +/// This layer accepts sets of LLVM IR Modules (via addModuleSet), but does +/// not immediately emit them the layer below. Instead, emissing to the base +/// layer is deferred until the first time the client requests the address +/// (via JITSymbol::getAddress) for a symbol contained in this layer. template class LazyEmittingLayer { public: - - using BaseLayerHandleT = typename BaseLayerT::ModuleHandleT; + using BaseLayerHandleT = typename BaseLayerT::ModuleSetHandleT; private: - class EmissionDeferredModule { + class EmissionDeferredSet { public: - EmissionDeferredModule() = default; - virtual ~EmissionDeferredModule() = default; + EmissionDeferredSet() = default; + virtual ~EmissionDeferredSet() = default; JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) { switch (EmitState) { @@ -85,9 +84,9 @@ private: llvm_unreachable("Invalid emit-state."); } - void removeModuleFromBaseLayer(BaseLayerT &BaseLayer) { + void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) { if (EmitState != NotEmitted) - BaseLayer.removeModule(Handle); + BaseLayer.removeModuleSet(Handle); } void emitAndFinalize(BaseLayerT &BaseLayer) { @@ -101,9 +100,10 @@ private: BaseLayer.emitAndFinalize(Handle); } - template - static std::unique_ptr - create(BaseLayerT &B, std::shared_ptr M, MemoryManagerPtrT MemMgr, + template + static std::unique_ptr + create(BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver); protected: @@ -116,13 +116,14 @@ private: BaseLayerHandleT Handle; }; - template - class EmissionDeferredModuleImpl : public EmissionDeferredModule { + template + class EmissionDeferredSetImpl : public EmissionDeferredSet { public: - EmissionDeferredModuleImpl(std::shared_ptr M, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) - : M(std::move(M)), MemMgr(std::move(MemMgr)), + EmissionDeferredSetImpl(ModuleSetT Ms, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) + : Ms(std::move(Ms)), MemMgr(std::move(MemMgr)), Resolver(std::move(Resolver)) {} protected: @@ -153,8 +154,8 @@ private: // We don't need the mangled names set any more: Once we've emitted this // to the base layer we'll just look for symbols there. MangledSymbols.reset(); - return BaseLayer.addModule(std::move(M), std::move(MemMgr), - std::move(Resolver)); + return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr), + std::move(Resolver)); } private: @@ -196,54 +197,56 @@ private: auto Symbols = llvm::make_unique>(); - Mangler Mang; + for (const auto &M : Ms) { + Mangler Mang; - for (const auto &GO : M->global_objects()) + for (const auto &GO : M->global_objects()) if (auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName, ExportedSymbolsOnly)) return GV; + } MangledSymbols = std::move(Symbols); return nullptr; } - std::shared_ptr M; + ModuleSetT Ms; MemoryManagerPtrT MemMgr; SymbolResolverPtrT Resolver; mutable std::unique_ptr> MangledSymbols; }; - using ModuleListT = std::list>; + using ModuleSetListT = std::list>; BaseLayerT &BaseLayer; - ModuleListT ModuleList; + ModuleSetListT ModuleSetList; public: - /// @brief Handle to a loaded module. - using ModuleHandleT = typename ModuleListT::iterator; + /// @brief Handle to a set of loaded modules. + using ModuleSetHandleT = typename ModuleSetListT::iterator; /// @brief Construct a lazy emitting layer. LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {} - /// @brief Add the given module to the lazy emitting layer. - template - ModuleHandleT addModule(std::shared_ptr M, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - return ModuleList.insert( - ModuleList.end(), - EmissionDeferredModule::create(BaseLayer, std::move(M), - std::move(MemMgr), - std::move(Resolver))); + /// @brief Add the given set of modules to the lazy emitting layer. + template + ModuleSetHandleT addModuleSet(ModuleSetT Ms, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + return ModuleSetList.insert( + ModuleSetList.end(), + EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MemMgr), + std::move(Resolver))); } - /// @brief Remove the module represented by the given handle. + /// @brief Remove the module set represented by the given handle. /// - /// This method will free the memory associated with the given module, both - /// in this layer, and the base layer. - void removeModule(ModuleHandleT H) { - (*H)->removeModuleFromBaseLayer(BaseLayer); - ModuleList.erase(H); + /// This method will free the memory associated with the given module set, + /// both in this layer, and the base layer. + void removeModuleSet(ModuleSetHandleT H) { + (*H)->removeModulesFromBaseLayer(BaseLayer); + ModuleSetList.erase(H); } /// @brief Search for the given named symbol. @@ -255,40 +258,42 @@ public: if (auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly)) return Symbol; - // If not found then search the deferred modules. If any of these contain a + // If not found then search the deferred sets. If any of these contain a // definition of 'Name' then they will return a JITSymbol that will emit // the corresponding module when the symbol address is requested. - for (auto &DeferredMod : ModuleList) - if (auto Symbol = DeferredMod->find(Name, ExportedSymbolsOnly, BaseLayer)) + for (auto &DeferredSet : ModuleSetList) + if (auto Symbol = DeferredSet->find(Name, ExportedSymbolsOnly, BaseLayer)) return Symbol; // If no definition found anywhere return a null symbol. return nullptr; } - /// @brief Get the address of the given symbol in the context of the of + /// @brief Get the address of the given symbol in the context of the set of /// compiled modules represented by the handle H. - JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, + JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer); } - /// @brief Immediately emit and finalize the module represented by the given - /// handle. - /// @param H Handle for module to emit/finalize. - void emitAndFinalize(ModuleHandleT H) { + /// @brief Immediately emit and finalize the moduleOB set represented by the + /// given handle. + /// @param H Handle for module set to emit/finalize. + void emitAndFinalize(ModuleSetHandleT H) { (*H)->emitAndFinalize(BaseLayer); } }; template -template -std::unique_ptr::EmissionDeferredModule> -LazyEmittingLayer::EmissionDeferredModule::create( - BaseLayerT &B, std::shared_ptr M, MemoryManagerPtrT MemMgr, +template +std::unique_ptr::EmissionDeferredSet> +LazyEmittingLayer::EmissionDeferredSet::create( + BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver) { - using EDS = EmissionDeferredModuleImpl; - return llvm::make_unique(std::move(M), std::move(MemMgr), + using EDS = EmissionDeferredSetImpl; + return llvm::make_unique(std::move(Ms), std::move(MemMgr), std::move(Resolver)); } diff --git a/llvm/lib/ExecutionEngine/Orc/OrcCBindings.cpp b/llvm/lib/ExecutionEngine/Orc/OrcCBindings.cpp index 5fe259f80b6f..8dcd49aaab5b 100644 --- a/llvm/lib/ExecutionEngine/Orc/OrcCBindings.cpp +++ b/llvm/lib/ExecutionEngine/Orc/OrcCBindings.cpp @@ -12,24 +12,6 @@ using namespace llvm; -LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod) { - return wrap(new std::shared_ptr(unwrap(Mod))); -} - -void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod) { - delete unwrap(SharedMod); -} - -LLVMSharedObjectBufferRef -LLVMOrcMakeSharedObjectBuffer(LLVMMemoryBufferRef ObjBuffer) { - return wrap(new std::shared_ptr(unwrap(ObjBuffer))); -} - -void -LLVMOrcDisposeSharedObjectBufferRef(LLVMSharedObjectBufferRef SharedObjBuffer) { - delete unwrap(SharedObjBuffer); -} - LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) { TargetMachine *TM2(unwrap(TM)); @@ -83,23 +65,21 @@ LLVMOrcErrorCode LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack, } LLVMOrcModuleHandle -LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, - LLVMSharedModuleRef Mod, +LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx) { OrcCBindingsStack &J = *unwrap(JITStack); - std::shared_ptr *M(unwrap(Mod)); - return J.addIRModuleEager(*M, SymbolResolver, SymbolResolverCtx); + Module *M(unwrap(Mod)); + return J.addIRModuleEager(M, SymbolResolver, SymbolResolverCtx); } LLVMOrcModuleHandle -LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, - LLVMSharedModuleRef Mod, +LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx) { OrcCBindingsStack &J = *unwrap(JITStack); - std::shared_ptr *M(unwrap(Mod)); - return J.addIRModuleLazy(*M, SymbolResolver, SymbolResolverCtx); + Module *M(unwrap(Mod)); + return J.addIRModuleLazy(M, SymbolResolver, SymbolResolverCtx); } void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) { diff --git a/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h b/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h index 931d0a9eb2ad..96bd15eb091d 100644 --- a/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h +++ b/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h @@ -42,10 +42,6 @@ namespace llvm { class OrcCBindingsStack; -DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr, - LLVMSharedModuleRef) -DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr, - LLVMSharedObjectBufferRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef) @@ -75,7 +71,7 @@ private: template class GenericHandleImpl : public GenericHandle { public: - GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle) + GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle) : Layer(Layer), Handle(std::move(Handle)) {} JITSymbol findSymbolIn(const std::string &Name, @@ -83,21 +79,24 @@ private: return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly); } - void removeModule() override { return Layer.removeModule(Handle); } + void removeModule() override { return Layer.removeModuleSet(Handle); } private: LayerT &Layer; - typename LayerT::ModuleHandleT Handle; + typename LayerT::ModuleSetHandleT Handle; }; template std::unique_ptr> - createGenericHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) { + createGenericHandle(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle) { return llvm::make_unique>(Layer, std::move(Handle)); } public: + // We need a 'ModuleSetHandleT' to conform to the layer concept. + using ModuleSetHandleT = unsigned; + using ModuleHandleT = unsigned; OrcCBindingsStack(TargetMachine &TM, @@ -184,7 +183,7 @@ public: } template - ModuleHandleT addIRModule(LayerT &Layer, std::shared_ptr M, + ModuleHandleT addIRModule(LayerT &Layer, Module *M, std::unique_ptr MemMgr, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx) { @@ -204,8 +203,11 @@ public: auto Resolver = createResolver(ExternalResolver, ExternalResolverCtx); // Add the module to the JIT. - auto LH = Layer.addModule(std::move(M), std::move(MemMgr), - std::move(Resolver)); + std::vector S; + S.push_back(std::move(M)); + + auto LH = Layer.addModuleSet(std::move(S), std::move(MemMgr), + std::move(Resolver)); ModuleHandleT H = createHandle(Layer, LH); // Run the static constructors, and save the static destructor runner for @@ -218,7 +220,7 @@ public: return H; } - ModuleHandleT addIRModuleEager(std::shared_ptr M, + ModuleHandleT addIRModuleEager(Module *M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx) { return addIRModule(CompileLayer, std::move(M), @@ -226,7 +228,7 @@ public: std::move(ExternalResolver), ExternalResolverCtx); } - ModuleHandleT addIRModuleLazy(std::shared_ptr M, + ModuleHandleT addIRModuleLazy(Module *M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx) { return addIRModule(CODLayer, std::move(M), @@ -255,7 +257,8 @@ public: private: template - unsigned createHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) { + unsigned createHandle(LayerT &Layer, + typename LayerT::ModuleSetHandleT Handle) { unsigned NewHandle; if (!FreeHandleIndexes.empty()) { NewHandle = FreeHandleIndexes.back(); diff --git a/llvm/lib/ExecutionEngine/Orc/OrcMCJITReplacement.h b/llvm/lib/ExecutionEngine/Orc/OrcMCJITReplacement.h index 944eba7bc899..000ab0006ba3 100644 --- a/llvm/lib/ExecutionEngine/Orc/OrcMCJITReplacement.h +++ b/llvm/lib/ExecutionEngine/Orc/OrcMCJITReplacement.h @@ -191,15 +191,10 @@ public: } else { assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch"); } - auto *MPtr = M.release(); - Retain[MPtr] = false; - auto Deleter = - [this](Module *Mod) { - if (!Retain[Mod]) - delete Mod; - }; - LocalModules.push_back(std::shared_ptr(MPtr, std::move(Deleter))); - LazyEmitLayer.addModule(LocalModules.back(), &MemMgr, &Resolver); + Modules.push_back(std::move(M)); + std::vector Ms; + Ms.push_back(&*Modules.back()); + LazyEmitLayer.addModuleSet(std::move(Ms), &MemMgr, &Resolver); } void addObjectFile(std::unique_ptr O) override { @@ -386,8 +381,6 @@ private: std::map UnfinalizedSections; - std::map Retain; - std::vector> LocalModules; std::vector> Archives; }; diff --git a/llvm/tools/lli/OrcLazyJIT.cpp b/llvm/tools/lli/OrcLazyJIT.cpp index 2e15894152f9..899a7acdb1ca 100644 --- a/llvm/tools/lli/OrcLazyJIT.cpp +++ b/llvm/tools/lli/OrcLazyJIT.cpp @@ -54,10 +54,10 @@ static cl::opt OrcInlineStubs("orc-lazy-inline-stubs", OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() { switch (OrcDumpKind) { case DumpKind::NoDump: - return [](std::shared_ptr M) { return M; }; + return [](std::unique_ptr M) { return M; }; case DumpKind::DumpFuncsToStdOut: - return [](std::shared_ptr M) { + return [](std::unique_ptr M) { printf("[ "); for (const auto &F : *M) { @@ -76,7 +76,7 @@ OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() { }; case DumpKind::DumpModsToStdOut: - return [](std::shared_ptr M) { + return [](std::unique_ptr M) { outs() << "----- Module Start -----\n" << *M << "----- Module End -----\n"; @@ -84,7 +84,7 @@ OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() { }; case DumpKind::DumpModsToDisk: - return [](std::shared_ptr M) { + return [](std::unique_ptr M) { std::error_code EC; raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC, sys::fs::F_Text); @@ -147,8 +147,7 @@ int llvm::runOrcLazyJIT(std::vector> Ms, OrcInlineStubs); // Add the module, look up main and run it. - for (auto &M : Ms) - J.addModule(std::shared_ptr(std::move(M))); + J.addModuleSet(std::move(Ms)); auto MainSym = J.findSymbol("main"); if (!MainSym) { diff --git a/llvm/tools/lli/OrcLazyJIT.h b/llvm/tools/lli/OrcLazyJIT.h index fc02a10b514e..20551494647f 100644 --- a/llvm/tools/lli/OrcLazyJIT.h +++ b/llvm/tools/lli/OrcLazyJIT.h @@ -50,11 +50,11 @@ public: using ObjLayerT = orc::RTDyldObjectLinkingLayer; using CompileLayerT = orc::IRCompileLayer; using TransformFtor = - std::function(std::shared_ptr)>; + std::function(std::unique_ptr)>; using IRDumpLayerT = orc::IRTransformLayer; using CODLayerT = orc::CompileOnDemandLayer; using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT; - using ModuleHandleT = CODLayerT::ModuleHandleT; + using ModuleSetHandleT = CODLayerT::ModuleSetHandleT; OrcLazyJIT(std::unique_ptr TM, std::unique_ptr CCMgr, @@ -77,9 +77,11 @@ public: DtorRunner.runViaLayer(CODLayer); } - void addModule(std::shared_ptr M) { - if (M->getDataLayout().isDefault()) - M->setDataLayout(DL); + ModuleSetHandleT addModuleSet(std::vector> Ms) { + // Attach a data-layouts if they aren't already present. + for (auto &M : Ms) + if (M->getDataLayout().isDefault()) + M->setDataLayout(DL); // Rename, bump linkage and record static constructors and destructors. // We have to do this before we hand over ownership of the module to the @@ -87,19 +89,21 @@ public: std::vector CtorNames, DtorNames; { unsigned CtorId = 0, DtorId = 0; - for (auto Ctor : orc::getConstructors(*M)) { - std::string NewCtorName = ("$static_ctor." + Twine(CtorId++)).str(); - Ctor.Func->setName(NewCtorName); - Ctor.Func->setLinkage(GlobalValue::ExternalLinkage); - Ctor.Func->setVisibility(GlobalValue::HiddenVisibility); - CtorNames.push_back(mangle(NewCtorName)); - } - for (auto Dtor : orc::getDestructors(*M)) { - std::string NewDtorName = ("$static_dtor." + Twine(DtorId++)).str(); - Dtor.Func->setLinkage(GlobalValue::ExternalLinkage); - Dtor.Func->setVisibility(GlobalValue::HiddenVisibility); - DtorNames.push_back(mangle(Dtor.Func->getName())); - Dtor.Func->setName(NewDtorName); + for (auto &M : Ms) { + for (auto Ctor : orc::getConstructors(*M)) { + std::string NewCtorName = ("$static_ctor." + Twine(CtorId++)).str(); + Ctor.Func->setName(NewCtorName); + Ctor.Func->setLinkage(GlobalValue::ExternalLinkage); + Ctor.Func->setVisibility(GlobalValue::HiddenVisibility); + CtorNames.push_back(mangle(NewCtorName)); + } + for (auto Dtor : orc::getDestructors(*M)) { + std::string NewDtorName = ("$static_dtor." + Twine(DtorId++)).str(); + Dtor.Func->setLinkage(GlobalValue::ExternalLinkage); + Dtor.Func->setVisibility(GlobalValue::HiddenVisibility); + DtorNames.push_back(mangle(Dtor.Func->getName())); + Dtor.Func->setName(NewDtorName); + } } } @@ -107,45 +111,41 @@ public: // 1) Search the JIT symbols. // 2) Check for C++ runtime overrides. // 3) Search the host process (LLI)'s symbol table. - if (ModulesHandle == CODLayerT::ModuleHandleT()) { - auto Resolver = - orc::createLambdaResolver( - [this](const std::string &Name) -> JITSymbol { - if (auto Sym = CODLayer.findSymbol(Name, true)) - return Sym; - return CXXRuntimeOverrides.searchOverrides(Name); - }, - [](const std::string &Name) { - if (auto Addr = - RTDyldMemoryManager::getSymbolAddressInProcess(Name)) - return JITSymbol(Addr, JITSymbolFlags::Exported); - return JITSymbol(nullptr); - } - ); + auto Resolver = + orc::createLambdaResolver( + [this](const std::string &Name) -> JITSymbol { + if (auto Sym = CODLayer.findSymbol(Name, true)) + return Sym; + return CXXRuntimeOverrides.searchOverrides(Name); + }, + [](const std::string &Name) { + if (auto Addr = + RTDyldMemoryManager::getSymbolAddressInProcess(Name)) + return JITSymbol(Addr, JITSymbolFlags::Exported); + return JITSymbol(nullptr); + } + ); - // Add the module to the JIT. - ModulesHandle = - CODLayer.addModule(std::move(M), - llvm::make_unique(), - std::move(Resolver)); - } else - CODLayer.addExtraModule(ModulesHandle, std::move(M)); + // Add the module to the JIT. + auto H = CODLayer.addModuleSet(std::move(Ms), + llvm::make_unique(), + std::move(Resolver)); // Run the static constructors, and save the static destructor runner for // execution when the JIT is torn down. - orc::CtorDtorRunner CtorRunner(std::move(CtorNames), - ModulesHandle); + orc::CtorDtorRunner CtorRunner(std::move(CtorNames), H); CtorRunner.runViaLayer(CODLayer); - IRStaticDestructorRunners.emplace_back(std::move(DtorNames), - ModulesHandle); + IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H); + + return H; } JITSymbol findSymbol(const std::string &Name) { return CODLayer.findSymbol(mangle(Name), true); } - JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) { + JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name) { return CODLayer.findSymbolIn(H, mangle(Name), true); } @@ -179,7 +179,6 @@ private: orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides; std::vector> IRStaticDestructorRunners; - CODLayerT::ModuleHandleT ModulesHandle; }; int runOrcLazyJIT(std::vector> Ms, diff --git a/llvm/unittests/ExecutionEngine/Orc/LazyEmittingLayerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/LazyEmittingLayerTest.cpp index f65dc0cd609d..213c460aa676 100644 --- a/llvm/unittests/ExecutionEngine/Orc/LazyEmittingLayerTest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/LazyEmittingLayerTest.cpp @@ -14,9 +14,9 @@ namespace { struct MockBaseLayer { - typedef int ModuleHandleT; - ModuleHandleT addModule( - std::shared_ptr, + typedef int ModuleSetHandleT; + ModuleSetHandleT addModuleSet( + std::list>, std::unique_ptr MemMgr, std::unique_ptr Resolver) { EXPECT_FALSE(MemMgr); @@ -27,7 +27,7 @@ struct MockBaseLayer { TEST(LazyEmittingLayerTest, Empty) { MockBaseLayer M; llvm::orc::LazyEmittingLayer L(M); - L.addModule(std::unique_ptr(), nullptr, nullptr); + L.addModuleSet(std::list>(), nullptr, nullptr); } } diff --git a/llvm/unittests/ExecutionEngine/Orc/ObjectTransformLayerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/ObjectTransformLayerTest.cpp index 2fdf9e8b7379..6da2894ae0ef 100644 --- a/llvm/unittests/ExecutionEngine/Orc/ObjectTransformLayerTest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/ObjectTransformLayerTest.cpp @@ -314,7 +314,7 @@ TEST(ObjectTransformLayerTest, Main) { // compile. NullResolver Resolver; NullManager Manager; - CompileLayer.addModule(std::shared_ptr(), &Manager, &Resolver); + CompileLayer.addModuleSet(std::vector(), &Manager, &Resolver); // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer // compile. diff --git a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp index 2900a9c92766..e8ba16a472b7 100644 --- a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp @@ -65,9 +65,8 @@ protected: CompileContext *CCtx = static_cast(Ctx); auto *ET = CCtx->APIExecTest; CCtx->M = ET->createTestModule(ET->TM->getTargetTriple()); - LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release())); - CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, SM, myResolver, nullptr); - LLVMOrcDisposeSharedModuleRef(SM); + CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, wrap(CCtx->M.get()), + myResolver, nullptr); CCtx->Compiled = true; LLVMOrcTargetAddress MainAddr = LLVMOrcGetSymbolAddress(JITStack, "main"); LLVMOrcSetIndirectStubPointer(JITStack, "foo", MainAddr); @@ -88,10 +87,8 @@ TEST_F(OrcCAPIExecutionTest, TestEagerIRCompilation) { LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); - LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release())); LLVMOrcModuleHandle H = - LLVMOrcAddEagerlyCompiledIR(JIT, SM, myResolver, nullptr); - LLVMOrcDisposeSharedModuleRef(SM); + LLVMOrcAddEagerlyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr); MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main"); int Result = MainFn(); EXPECT_EQ(Result, 42) @@ -114,10 +111,8 @@ TEST_F(OrcCAPIExecutionTest, TestLazyIRCompilation) { LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); - LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release())); LLVMOrcModuleHandle H = - LLVMOrcAddLazilyCompiledIR(JIT, SM, myResolver, nullptr); - LLVMOrcDisposeSharedModuleRef(SM); + LLVMOrcAddLazilyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr); MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main"); int Result = MainFn(); EXPECT_EQ(Result, 42) diff --git a/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h b/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h index d7049ef00e6d..24320034a17a 100644 --- a/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h +++ b/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h @@ -106,65 +106,65 @@ public: }; template class MockBaseLayer { public: - typedef HandleT ModuleHandleT; + typedef HandleT ModuleSetHandleT; - MockBaseLayer(AddModuleFtor &&AddModule, - RemoveModuleFtor &&RemoveModule, + MockBaseLayer(AddModuleSetFtor &&AddModuleSet, + RemoveModuleSetFtor &&RemoveModuleSet, FindSymbolFtor &&FindSymbol, FindSymbolInFtor &&FindSymbolIn) - : AddModule(AddModule), RemoveModule(RemoveModule), + : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet), FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn) {} - template - ModuleHandleT addModule(ModuleT Ms, MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - return AddModule(std::move(Ms), std::move(MemMgr), std::move(Resolver)); + ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver)); } - void removeModule(ModuleHandleT H) { - RemoveModule(H); + void removeModuleSet(ModuleSetHandleT H) { + RemoveModuleSet(H); } JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { return FindSymbol(Name, ExportedSymbolsOnly); } - JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, + JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return FindSymbolIn(H, Name, ExportedSymbolsOnly); } private: - AddModuleFtor AddModule; - RemoveModuleFtor RemoveModule; + AddModuleSetFtor AddModuleSet; + RemoveModuleSetFtor RemoveModuleSet; FindSymbolFtor FindSymbol; FindSymbolInFtor FindSymbolIn; }; -template -MockBaseLayer -createMockBaseLayer(AddModuleFtor &&AddModule, - RemoveModuleFtor &&RemoveModule, +createMockBaseLayer(AddModuleSetFtor &&AddModuleSet, + RemoveModuleSetFtor &&RemoveModuleSet, FindSymbolFtor &&FindSymbol, FindSymbolInFtor &&FindSymbolIn) { - return MockBaseLayer( - std::forward(AddModule), - std::forward(RemoveModule), + std::forward(AddModuleSet), + std::forward(RemoveModuleSet), std::forward(FindSymbol), std::forward(FindSymbolIn)); }