From d8eb9f704c00d33550f730a6a8d0070634777b1f Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Sat, 9 Jan 2016 20:55:18 +0000 Subject: [PATCH] [Orc] Enable user-supplied memory managers in the CompileOnDemand layer. Previously the CompileOnDemand layer was hard-coded to use a new SectionMemoryManager for each function when it was called. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257265 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Orc/CompileOnDemandLayer.h | 89 +++++++++++-------- lib/ExecutionEngine/Orc/OrcCBindingsStack.h | 3 +- tools/lli/OrcLazyJIT.h | 4 +- 3 files changed, 57 insertions(+), 39 deletions(-) diff --git a/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h b/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h index 7dab5d1bc67..0eb8693df42 100644 --- a/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h +++ b/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h @@ -19,7 +19,6 @@ #include "LambdaResolver.h" #include "LogicalDylib.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/Transforms/Utils/Cloning.h" #include #include @@ -61,31 +60,36 @@ private: typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT; - class ModuleOwner { + // Provide type-erasure for the Modules and MemoryManagers. + template + class ResourceOwner { public: - ModuleOwner() = default; - ModuleOwner(const ModuleOwner&) = delete; - ModuleOwner& operator=(const ModuleOwner&) = delete; - virtual ~ModuleOwner() { } - virtual Module& getModule() const = 0; + ResourceOwner() = default; + ResourceOwner(const ResourceOwner&) = delete; + ResourceOwner& operator=(const ResourceOwner&) = delete; + virtual ~ResourceOwner() { } + virtual ResourceT& getResource() const = 0; }; - template - class ModuleOwnerImpl : public ModuleOwner { + template + class ResourceOwnerImpl : public ResourceOwner { public: - ModuleOwnerImpl(ModulePtrT ModulePtr) : ModulePtr(std::move(ModulePtr)) {} - Module& getModule() const override { return *ModulePtr; } + ResourceOwnerImpl(ResourcePtrT ResourcePtr) + : ResourcePtr(std::move(ResourcePtr)) {} + ResourceT& getResource() const override { return *ResourcePtr; } private: - ModulePtrT ModulePtr; + ResourcePtrT ResourcePtr; }; - template - std::unique_ptr wrapOwnership(ModulePtrT ModulePtr) { - return llvm::make_unique>(std::move(ModulePtr)); + template + std::unique_ptr> + wrapOwnership(ResourcePtrT ResourcePtr) { + typedef ResourceOwnerImpl RO; + return llvm::make_unique(std::move(ResourcePtr)); } struct LogicalModuleResources { - std::unique_ptr SourceModuleOwner; + std::unique_ptr> SourceModule; std::set StubsToClone; std::unique_ptr StubsMgr; @@ -93,13 +97,13 @@ private: // Explicit move constructor to make MSVC happy. LogicalModuleResources(LogicalModuleResources &&Other) - : SourceModuleOwner(std::move(Other.SourceModuleOwner)), + : SourceModule(std::move(Other.SourceModule)), StubsToClone(std::move(Other.StubsToClone)), StubsMgr(std::move(Other.StubsMgr)) {} // Explicit move assignment to make MSVC happy. LogicalModuleResources& operator=(LogicalModuleResources &&Other) { - SourceModuleOwner = std::move(Other.SourceModuleOwner); + SourceModule = std::move(Other.SourceModule); StubsToClone = std::move(Other.StubsToClone); StubsMgr = std::move(Other.StubsMgr); } @@ -114,12 +118,19 @@ private: }; - - struct LogicalDylibResources { typedef std::function SymbolResolverFtor; + + typedef std::function, + std::unique_ptr)> + ModuleAdderFtor; + SymbolResolverFtor ExternalSymbolResolver; + std::unique_ptr> MemMgr; + ModuleAdderFtor ModuleAdder; }; typedef LogicalDylibfindSymbol(Name); }; + auto &MemMgrRef = *MemMgr; + LDResources.MemMgr = + wrapOwnership(std::move(MemMgr)); + + LDResources.ModuleAdder = + [&MemMgrRef](BaseLayerT &B, std::unique_ptr M, + std::unique_ptr 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. for (auto &M : Ms) addLogicalModule(LogicalDylibs.back(), std::move(M)); @@ -215,9 +235,9 @@ private: auto LMH = LD.createLogicalModule(); auto &LMResources = LD.getLogicalModuleResources(LMH); - LMResources.SourceModuleOwner = wrapOwnership(std::move(SrcMPtr)); + LMResources.SourceModule = wrapOwnership(std::move(SrcMPtr)); - Module &SrcM = LMResources.SourceModuleOwner->getModule(); + Module &SrcM = LMResources.SourceModule->getResource(); // Create the GlobalValues module. const DataLayout &DL = SrcM.getDataLayout(); @@ -326,12 +346,9 @@ private: return RuntimeDyld::SymbolInfo(nullptr); }); - std::vector> GVsMSet; - GVsMSet.push_back(std::move(GVsM)); auto GVsH = - BaseLayer.addModuleSet(std::move(GVsMSet), - llvm::make_unique(), - std::move(GVsResolver)); + LD.getDylibResources().ModuleAdder(BaseLayer, std::move(GVsM), + std::move(GVsResolver)); LD.addToLogicalModule(LMH, GVsH); } @@ -348,7 +365,7 @@ private: LogicalModuleHandle LMH, Function &F) { auto &LMResources = LD.getLogicalModuleResources(LMH); - Module &SrcM = LMResources.SourceModuleOwner->getModule(); + Module &SrcM = LMResources.SourceModule->getResource(); // If F is a declaration we must already have compiled it. if (F.isDeclaration()) @@ -386,7 +403,7 @@ private: LogicalModuleHandle LMH, const PartitionT &Part) { auto &LMResources = LD.getLogicalModuleResources(LMH); - Module &SrcM = LMResources.SourceModuleOwner->getModule(); + Module &SrcM = LMResources.SourceModule->getResource(); // Create the module. std::string NewName = SrcM.getName(); @@ -445,7 +462,6 @@ private: moveFunctionBody(*F, VMap, &Materializer); // Create memory manager and symbol resolver. - auto MemMgr = llvm::make_unique(); auto Resolver = createLambdaResolver( [this, &LD, LMH](const std::string &Name) { if (auto Symbol = LD.findSymbolInternally(LMH, Name)) @@ -459,10 +475,9 @@ private: Symbol.getFlags()); return RuntimeDyld::SymbolInfo(nullptr); }); - std::vector> PartMSet; - PartMSet.push_back(std::move(M)); - return BaseLayer.addModuleSet(std::move(PartMSet), std::move(MemMgr), - std::move(Resolver)); + + return LD.getDylibResources().ModuleAdder(BaseLayer, std::move(M), + std::move(Resolver)); } BaseLayerT &BaseLayer; diff --git a/lib/ExecutionEngine/Orc/OrcCBindingsStack.h b/lib/ExecutionEngine/Orc/OrcCBindingsStack.h index 2e17624ff47..aae6a99432b 100644 --- a/lib/ExecutionEngine/Orc/OrcCBindingsStack.h +++ b/lib/ExecutionEngine/Orc/OrcCBindingsStack.h @@ -221,7 +221,8 @@ public: ModuleHandleT addIRModuleLazy(Module* M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx) { - return addIRModule(CODLayer, std::move(M), nullptr, + return addIRModule(CODLayer, std::move(M), + llvm::make_unique(), std::move(ExternalResolver), ExternalResolverCtx); } diff --git a/tools/lli/OrcLazyJIT.h b/tools/lli/OrcLazyJIT.h index bb4da33ea9b..2a5b31d1bfb 100644 --- a/tools/lli/OrcLazyJIT.h +++ b/tools/lli/OrcLazyJIT.h @@ -105,7 +105,9 @@ public: // Add the module to the JIT. std::vector> S; S.push_back(std::move(M)); - auto H = CODLayer.addModuleSet(std::move(S), nullptr, std::move(Resolver)); + auto H = CODLayer.addModuleSet(std::move(S), + llvm::make_unique(), + std::move(Resolver)); // Run the static constructors, and save the static destructor runner for // execution when the JIT is torn down. -- 2.34.1