diff options
author | Lang Hames <lhames@gmail.com> | 2018-02-09 02:30:40 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2018-02-09 02:30:40 +0000 |
commit | 0976cee8e9d6dbf890cb25299baac973307e2d9a (patch) | |
tree | c5fccb3bd0a483d8a5cafb402e43729da0c6b4ad /llvm/include | |
parent | 6562b3d954b57b04daf0b1bf72c402d9aabd633e (diff) | |
download | bcm5719-llvm-0976cee8e9d6dbf890cb25299baac973307e2d9a.tar.gz bcm5719-llvm-0976cee8e9d6dbf890cb25299baac973307e2d9a.zip |
[ORC] Remove Layer handles from the layer concept.
Handles were returned by addModule and used as keys for removeModule,
findSymbolIn, and emitAndFinalize. Their job is now subsumed by VModuleKeys,
which simplify resource management by providing a consistent handle across all
layers.
llvm-svn: 324700
Diffstat (limited to 'llvm/include')
8 files changed, 156 insertions, 193 deletions
diff --git a/llvm/include/llvm-c/OrcBindings.h b/llvm/include/llvm-c/OrcBindings.h index abb3ac6a7f0..95bdef81593 100644 --- a/llvm/include/llvm-c/OrcBindings.h +++ b/llvm/include/llvm-c/OrcBindings.h @@ -31,7 +31,7 @@ extern "C" { typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef; typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef; -typedef uint32_t LLVMOrcModuleHandle; +typedef uint64_t LLVMOrcModuleHandle; typedef uint64_t LLVMOrcTargetAddress; typedef uint64_t (*LLVMOrcSymbolResolverFn)(const char *Name, void *LookupCtx); typedef uint64_t (*LLVMOrcLazyCompileCallbackFn)(LLVMOrcJITStackRef JITStack, diff --git a/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h index c15d2c26583..a30ffbe02af 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h @@ -86,8 +86,6 @@ private: return LambdaMaterializer<MaterializerFtor>(std::move(M)); } - using BaseLayerModuleHandleT = typename BaseLayerT::ModuleHandleT; - // Provide type-erasure for the Modules and MemoryManagers. template <typename ResourceT> class ResourceOwner { @@ -147,6 +145,13 @@ private: using SourceModulesList = std::vector<SourceModuleEntry>; using SourceModuleHandle = typename SourceModulesList::size_type; + LogicalDylib() = default; + + LogicalDylib(VModuleKey K, std::shared_ptr<SymbolResolver> BackingResolver, + std::unique_ptr<IndirectStubsMgrT> StubsMgr) + : K(std::move(K)), BackingResolver(std::move(BackingResolver)), + StubsMgr(std::move(StubsMgr)) {} + SourceModuleHandle addSourceModule(std::shared_ptr<Module> M) { SourceModuleHandle H = SourceModules.size(); @@ -167,8 +172,8 @@ private: bool ExportedSymbolsOnly) { if (auto Sym = StubsMgr->findStub(Name, ExportedSymbolsOnly)) return Sym; - for (auto BLH : BaseLayerHandles) - if (auto Sym = BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly)) + for (auto BLK : BaseLayerVModuleKeys) + if (auto Sym = BaseLayer.findSymbolIn(BLK, Name, ExportedSymbolsOnly)) return Sym; else if (auto Err = Sym.takeError()) return std::move(Err); @@ -176,8 +181,8 @@ private: } Error removeModulesFromBaseLayer(BaseLayerT &BaseLayer) { - for (auto &BLH : BaseLayerHandles) - if (auto Err = BaseLayer.removeModule(BLH)) + for (auto &BLK : BaseLayerVModuleKeys) + if (auto Err = BaseLayer.removeModule(BLK)) return Err; return Error::success(); } @@ -187,16 +192,11 @@ private: std::unique_ptr<IndirectStubsMgrT> StubsMgr; StaticGlobalRenamer StaticRenamer; SourceModulesList SourceModules; - std::vector<BaseLayerModuleHandleT> BaseLayerHandles; + std::vector<VModuleKey> BaseLayerVModuleKeys; }; - using LogicalDylibList = std::list<LogicalDylib>; - public: - /// @brief Handle to loaded module. - using ModuleHandleT = typename LogicalDylibList::iterator; - /// @brief Module partitioning functor. using PartitioningFtor = std::function<std::set<Function*>(Function&)>; @@ -228,36 +228,35 @@ public: ~CompileOnDemandLayer() { // FIXME: Report error on log. while (!LogicalDylibs.empty()) - consumeError(removeModule(LogicalDylibs.begin())); + consumeError(removeModule(LogicalDylibs.begin()->first)); } /// @brief Add a module to the compile-on-demand layer. - Expected<ModuleHandleT> addModule(VModuleKey K, std::shared_ptr<Module> M) { + Error addModule(VModuleKey K, std::shared_ptr<Module> M) { - LogicalDylibs.push_back(LogicalDylib()); - auto &LD = LogicalDylibs.back(); - LD.K = std::move(K); - LD.StubsMgr = CreateIndirectStubsManager(); - LD.BackingResolver = GetSymbolResolver(LD.K); + assert(!LogicalDylibs.count(K) && "VModuleKey K already in use"); + auto I = LogicalDylibs.insert( + LogicalDylibs.end(), + std::make_pair(K, LogicalDylib(K, GetSymbolResolver(K), + CreateIndirectStubsManager()))); - if (auto Err = addLogicalModule(LD, std::move(M))) - return std::move(Err); - - return std::prev(LogicalDylibs.end()); + return addLogicalModule(I->second, std::move(M)); } /// @brief Add extra modules to an existing logical module. - Error addExtraModule(ModuleHandleT H, std::shared_ptr<Module> M) { - return addLogicalModule(*H, std::move(M)); + Error addExtraModule(VModuleKey K, std::shared_ptr<Module> M) { + return addLogicalModule(LogicalDylibs[K], std::move(M)); } - /// @brief Remove the module represented by the given handle. + /// @brief Remove the module represented by the given key. /// /// This will remove all modules in the layers below that were derived from - /// the module represented by H. - Error removeModule(ModuleHandleT H) { - auto Err = H->removeModulesFromBaseLayer(BaseLayer); - LogicalDylibs.erase(H); + /// the module represented by K. + Error removeModule(VModuleKey K) { + auto I = LogicalDylibs.find(K); + assert(I != LogicalDylibs.end() && "VModuleKey K not valid here"); + auto Err = I->second.removeModulesFromBaseLayer(BaseLayer); + LogicalDylibs.erase(I); return Err; } @@ -266,11 +265,10 @@ public: /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it exists. JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) { - for (auto LDI = LogicalDylibs.begin(), LDE = LogicalDylibs.end(); - LDI != LDE; ++LDI) { - if (auto Sym = LDI->StubsMgr->findStub(Name, ExportedSymbolsOnly)) + for (auto &KV : LogicalDylibs) { + if (auto Sym = KV.second.StubsMgr->findStub(Name, ExportedSymbolsOnly)) return Sym; - if (auto Sym = findSymbolIn(LDI, Name, ExportedSymbolsOnly)) + if (auto Sym = findSymbolIn(KV.first, Name, ExportedSymbolsOnly)) return Sym; else if (auto Err = Sym.takeError()) return std::move(Err); @@ -280,9 +278,10 @@ 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(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) { - return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly); + assert(LogicalDylibs.count(K) && "VModuleKey K is not valid here"); + return LogicalDylibs[K].findSymbol(BaseLayer, Name, ExportedSymbolsOnly); } /// @brief Update the stub for the given function to point at FnBodyAddr. @@ -502,10 +501,10 @@ private: SetSymbolResolver(LD.K, std::move(GVsResolver)); - if (auto GVsHOrErr = BaseLayer.addModule(LD.K, std::move(GVsM))) - LD.BaseLayerHandles.push_back(*GVsHOrErr); - else - return GVsHOrErr.takeError(); + if (auto Err = BaseLayer.addModule(LD.K, std::move(GVsM))) + return Err; + + LD.BaseLayerVModuleKeys.push_back(LD.K); return Error::success(); } @@ -534,11 +533,11 @@ private: JITTargetAddress CalledAddr = 0; auto Part = Partition(F); - if (auto PartHOrErr = emitPartition(LD, LMId, Part)) { - auto &PartH = *PartHOrErr; + if (auto PartKeyOrErr = emitPartition(LD, LMId, Part)) { + auto &PartKey = *PartKeyOrErr; for (auto *SubF : Part) { std::string FnName = mangle(SubF->getName(), SrcM.getDataLayout()); - if (auto FnBodySym = BaseLayer.findSymbolIn(PartH, FnName, false)) { + if (auto FnBodySym = BaseLayer.findSymbolIn(PartKey, FnName, false)) { if (auto FnBodyAddrOrErr = FnBodySym.getAddress()) { JITTargetAddress FnBodyAddr = *FnBodyAddrOrErr; @@ -559,15 +558,15 @@ private: llvm_unreachable("Function not emitted for partition"); } - LD.BaseLayerHandles.push_back(PartH); + LD.BaseLayerVModuleKeys.push_back(PartKey); } else - return PartHOrErr.takeError(); + return PartKeyOrErr.takeError(); return CalledAddr; } template <typename PartitionT> - Expected<BaseLayerModuleHandleT> + Expected<VModuleKey> emitPartition(LogicalDylib &LD, typename LogicalDylib::SourceModuleHandle LMId, const PartitionT &Part) { @@ -658,7 +657,10 @@ private: }); SetSymbolResolver(K, std::move(Resolver)); - return BaseLayer.addModule(std::move(K), std::move(M)); + if (auto Err = BaseLayer.addModule(std::move(K), std::move(M))) + return std::move(Err); + + return K; } ExecutionSession &ES; @@ -669,7 +671,7 @@ private: CompileCallbackMgrT &CompileCallbackMgr; IndirectStubsManagerBuilderT CreateIndirectStubsManager; - LogicalDylibList LogicalDylibs; + std::map<VModuleKey, LogicalDylib> LogicalDylibs; bool CloneStubsIntoPartitions; }; diff --git a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h index 22071ff0cb0..074ec8bf248 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h @@ -17,13 +17,14 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/iterator_range.h" #include "llvm/ExecutionEngine/JITSymbol.h" -#include "llvm/ExecutionEngine/RuntimeDyld.h" +#include "llvm/ExecutionEngine/Orc/Core.h" #include "llvm/ExecutionEngine/Orc/OrcError.h" +#include "llvm/ExecutionEngine/RuntimeDyld.h" #include <algorithm> #include <cstdint> #include <string> -#include <vector> #include <utility> +#include <vector> namespace llvm { @@ -95,9 +96,8 @@ class CtorDtorRunner { public: /// @brief Construct a CtorDtorRunner for the given range using the given /// name mangling function. - CtorDtorRunner(std::vector<std::string> CtorDtorNames, - typename JITLayerT::ModuleHandleT H) - : CtorDtorNames(std::move(CtorDtorNames)), H(H) {} + CtorDtorRunner(std::vector<std::string> CtorDtorNames, VModuleKey K) + : CtorDtorNames(std::move(CtorDtorNames)), K(K) {} /// @brief Run the recorded constructors/destructors through the given JIT /// layer. @@ -106,7 +106,7 @@ public: for (const auto &CtorDtorName : CtorDtorNames) { dbgs() << "Searching for ctor/dtor: " << CtorDtorName << "..."; - if (auto CtorDtorSym = JITLayer.findSymbolIn(H, CtorDtorName, false)) { + if (auto CtorDtorSym = JITLayer.findSymbolIn(K, CtorDtorName, false)) { dbgs() << " found symbol..."; if (auto AddrOrErr = CtorDtorSym.getAddress()) { dbgs() << " at addr " << format("0x%016x", *AddrOrErr) << "\n"; @@ -130,7 +130,7 @@ public: private: std::vector<std::string> CtorDtorNames; - typename JITLayerT::ModuleHandleT H; + orc::VModuleKey K; }; /// @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 94a3086a4ee..ae2ad9f6368 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h @@ -36,9 +36,6 @@ template <typename BaseLayerT, typename CompileFtor> class IRCompileLayer { public: - /// @brief Handle to a compiled module. - using ModuleHandleT = typename BaseLayerT::ObjHandleT; - /// @brief Construct an IRCompileLayer with the given BaseLayer, which must /// implement the ObjectLayer concept. IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile) @@ -49,18 +46,14 @@ public: /// @brief Compile the module, and add the resulting object to the base layer /// along with the given memory manager and symbol resolver. - /// - /// @return A handle for the added module. - Expected<ModuleHandleT> addModule(VModuleKey K, std::shared_ptr<Module> M) { + Error addModule(VModuleKey K, std::shared_ptr<Module> M) { using CompileResult = decltype(Compile(*M)); auto Obj = std::make_shared<CompileResult>(Compile(*M)); return BaseLayer.addObject(std::move(K), std::move(Obj)); } - /// @brief Remove the module associated with the handle H. - Error removeModule(ModuleHandleT H) { - return BaseLayer.removeObject(H); - } + /// @brief Remove the module associated with the VModuleKey K. + Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -73,22 +66,20 @@ public: /// @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. + /// @param K The VModuleKey for the module 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, + JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) { - return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); + return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly); } /// @brief Immediately emit and finalize the module represented by the given /// handle. /// @param H Handle for module to emit/finalize. - Error emitAndFinalize(ModuleHandleT H) { - return BaseLayer.emitAndFinalize(H); - } + Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); } private: BaseLayerT &BaseLayer; diff --git a/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h index 31de6f1cd5c..e70cc891355 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h @@ -31,9 +31,6 @@ template <typename BaseLayerT, typename TransformFtor> class IRTransformLayer { public: - /// @brief Handle to a set of added modules. - using ModuleHandleT = typename BaseLayerT::ModuleHandleT; - /// @brief Construct an IRTransformLayer with the given BaseLayer IRTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform = TransformFtor()) @@ -43,12 +40,12 @@ public: /// the layer below, along with the memory manager and symbol resolver. /// /// @return A handle for the added modules. - Expected<ModuleHandleT> addModule(VModuleKey K, std::shared_ptr<Module> M) { + Error addModule(VModuleKey K, std::shared_ptr<Module> M) { return BaseLayer.addModule(std::move(K), Transform(std::move(M))); } - /// @brief Remove the module associated with the handle H. - Error removeModule(ModuleHandleT H) { return BaseLayer.removeModule(H); } + /// @brief Remove the module associated with the VModuleKey K. + Error removeModule(VModuleKey K) { return BaseLayer.removeModule(K); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -59,24 +56,22 @@ public: } /// @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 + /// represented by the VModuleKey K. This call is forwarded to the base /// layer's implementation. /// @param H The handle for the module 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, + JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) { - return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); + return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly); } /// @brief Immediately emit and finalize the module represented by the given - /// handle. + /// VModuleKey. /// @param H Handle for module to emit/finalize. - Error emitAndFinalize(ModuleHandleT H) { - return BaseLayer.emitAndFinalize(H); - } + Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); } /// @brief Access the transform functor directly. TransformFtor& getTransform() { return Transform; } diff --git a/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h index 0da6e0135b7..35792eab6f9 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h @@ -40,10 +40,6 @@ namespace orc { /// is deferred until the first time the client requests the address (via /// JITSymbol::getAddress) for a symbol contained in this layer. template <typename BaseLayerT> class LazyEmittingLayer { -public: - - using BaseLayerHandleT = typename BaseLayerT::ModuleHandleT; - private: class EmissionDeferredModule { public: @@ -65,13 +61,11 @@ private: return 0; else if (this->EmitState == NotEmitted) { this->EmitState = Emitting; - if (auto HandleOrErr = this->emitToBaseLayer(B)) - Handle = std::move(*HandleOrErr); - else - return HandleOrErr.takeError(); + if (auto Err = this->emitToBaseLayer(B)) + return std::move(Err); this->EmitState = Emitted; } - if (auto Sym = B.findSymbolIn(Handle, PName, ExportedSymbolsOnly)) + if (auto Sym = B.findSymbolIn(K, PName, ExportedSymbolsOnly)) return Sym.getAddress(); else if (auto Err = Sym.takeError()) return std::move(Err); @@ -89,13 +83,13 @@ private: // RuntimeDyld that did the lookup), so just return a nullptr here. return nullptr; case Emitted: - return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly); + return B.findSymbolIn(K, Name, ExportedSymbolsOnly); } llvm_unreachable("Invalid emit-state."); } Error removeModuleFromBaseLayer(BaseLayerT& BaseLayer) { - return EmitState != NotEmitted ? BaseLayer.removeModule(Handle) + return EmitState != NotEmitted ? BaseLayer.removeModule(K) : Error::success(); } @@ -104,10 +98,10 @@ private: "Cannot emitAndFinalize while already emitting"); if (EmitState == NotEmitted) { EmitState = Emitting; - Handle = emitToBaseLayer(BaseLayer); + emitToBaseLayer(BaseLayer); EmitState = Emitted; } - BaseLayer.emitAndFinalize(Handle); + BaseLayer.emitAndFinalize(K); } private: @@ -135,7 +129,7 @@ private: return buildMangledSymbols(Name, ExportedSymbolsOnly); } - Expected<BaseLayerHandleT> emitToBaseLayer(BaseLayerT &BaseLayer) { + Error emitToBaseLayer(BaseLayerT &BaseLayer) { // 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(); @@ -192,40 +186,37 @@ private: } enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted; - BaseLayerHandleT Handle; VModuleKey K; std::shared_ptr<Module> M; mutable std::unique_ptr<StringMap<const GlobalValue*>> MangledSymbols; }; - using ModuleListT = std::list<std::unique_ptr<EmissionDeferredModule>>; - BaseLayerT &BaseLayer; - ModuleListT ModuleList; + std::map<VModuleKey, std::unique_ptr<EmissionDeferredModule>> ModuleMap; public: - /// @brief Handle to a loaded module. - using ModuleHandleT = typename ModuleListT::iterator; - /// @brief Construct a lazy emitting layer. LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {} /// @brief Add the given module to the lazy emitting layer. - Expected<ModuleHandleT> addModule(VModuleKey K, std::shared_ptr<Module> M) { - return ModuleList.insert( - ModuleList.end(), - llvm::make_unique<EmissionDeferredModule>(std::move(K), std::move(M))); + Error addModule(VModuleKey K, std::shared_ptr<Module> M) { + assert(!ModuleMap.count(K) && "VModuleKey K already in use"); + ModuleMap[K] = + llvm::make_unique<EmissionDeferredModule>(std::move(K), std::move(M)); + return Error::success(); } /// @brief Remove the module represented by the given handle. /// /// This method will free the memory associated with the given module, both /// in this layer, and the base layer. - Error removeModule(ModuleHandleT H) { - Error Err = (*H)->removeModuleFromBaseLayer(BaseLayer); - ModuleList.erase(H); - return Err; + Error removeModule(VModuleKey K) { + auto I = ModuleMap.find(K); + assert(I != ModuleMap.end() && "VModuleKey K not valid here"); + auto EDM = std::move(I.second); + ModuleMap.erase(I); + return EDM->removeModuleFromBaseLayer(BaseLayer); } /// @brief Search for the given named symbol. @@ -240,8 +231,8 @@ public: // If not found then search the deferred modules. 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 &KV : ModuleMap) + if (auto Symbol = KV.second->find(Name, ExportedSymbolsOnly, BaseLayer)) return Symbol; // If no definition found anywhere return a null symbol. @@ -249,17 +240,18 @@ public: } /// @brief Get the address of the given symbol in the context of the of - /// compiled modules represented by the handle H. - JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, + /// compiled modules represented by the key K. + JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) { - return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer); + assert(ModuleMap.count(K) && "VModuleKey K not valid here"); + return ModuleMap[K]->find(Name, ExportedSymbolsOnly, BaseLayer); } /// @brief Immediately emit and finalize the module represented by the given - /// handle. - /// @param H Handle for module to emit/finalize. - Error emitAndFinalize(ModuleHandleT H) { - return (*H)->emitAndFinalize(BaseLayer); + /// key. + Error emitAndFinalize(VModuleKey K) { + assert(ModuleMap.count(K) && "VModuleKey K not valid here"); + return ModuleMap[K]->emitAndFinalize(BaseLayer); } }; diff --git a/llvm/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h index 46bf21437c9..82e8aac40f2 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h @@ -31,9 +31,6 @@ namespace orc { template <typename BaseLayerT, typename TransformFtor> class ObjectTransformLayer { public: - /// @brief Handle to a set of added objects. - using ObjHandleT = typename BaseLayerT::ObjHandleT; - /// @brief Construct an ObjectTransformLayer with the given BaseLayer ObjectTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform = TransformFtor()) @@ -44,13 +41,12 @@ public: /// memory manager and symbol resolver. /// /// @return A handle for the added objects. - template <typename ObjectPtr> - Expected<ObjHandleT> addObject(VModuleKey K, ObjectPtr Obj) { + template <typename ObjectPtr> Error addObject(VModuleKey K, ObjectPtr Obj) { return BaseLayer.addObject(std::move(K), Transform(std::move(Obj))); } - /// @brief Remove the object set associated with the handle H. - Error removeObject(ObjHandleT H) { return BaseLayer.removeObject(H); } + /// @brief Remove the object set associated with the VModuleKey K. + Error removeObject(VModuleKey K) { return BaseLayer.removeObject(K); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -61,29 +57,27 @@ public: } /// @brief Get the address of the given symbol in the context of the set of - /// objects represented by the handle H. This call is forwarded to the - /// base layer's implementation. + /// objects represented by the VModuleKey K. This call is forwarded to + /// the base layer's implementation. /// @param H The handle for the object 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 object set. - JITSymbol findSymbolIn(ObjHandleT H, const std::string &Name, + JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) { - return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); + return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly); } /// @brief Immediately emit and finalize the object set represented by the - /// given handle. - /// @param H Handle for object set to emit/finalize. - Error emitAndFinalize(ObjHandleT H) { - return BaseLayer.emitAndFinalize(H); - } + /// given VModuleKey K. + Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); } - /// @brief Map section addresses for the objects associated with the handle H. - void mapSectionAddress(ObjHandleT H, const void *LocalAddress, + /// @brief Map section addresses for the objects associated with the + /// VModuleKey K. + void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) { - BaseLayer.mapSectionAddress(H, LocalAddress, TargetAddr); + BaseLayer.mapSectionAddress(K, LocalAddress, TargetAddr); } /// @brief Access the transform functor directly. diff --git a/llvm/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h index 073d48e09d5..c66725766d3 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h @@ -81,12 +81,6 @@ protected: StringMap<JITEvaluatedSymbol> SymbolTable; bool Finalized = false; }; - - using LinkedObjectListT = std::list<std::unique_ptr<LinkedObject>>; - -public: - /// @brief Handle to a loaded object. - using ObjHandleT = LinkedObjectListT::iterator; }; /// @brief Bare bones object linking layer. @@ -101,12 +95,11 @@ public: using RTDyldObjectLinkingLayerBase::ObjectPtr; /// @brief Functor for receiving object-loaded notifications. - using NotifyLoadedFtor = - std::function<void(ObjHandleT, const ObjectPtr &Obj, - const RuntimeDyld::LoadedObjectInfo &)>; + using NotifyLoadedFtor = std::function<void( + VModuleKey, const ObjectPtr &Obj, const RuntimeDyld::LoadedObjectInfo &)>; /// @brief Functor for receiving finalization notifications. - using NotifyFinalizedFtor = std::function<void(ObjHandleT)>; + using NotifyFinalizedFtor = std::function<void(VModuleKey)>; private: template <typename MemoryManagerPtrT, typename FinalizerFtor> @@ -127,10 +120,6 @@ private: MemMgr->deregisterEHFrames(); } - void setHandle(ObjHandleT H) { - PFC->Handle = H; - } - Error finalize() override { assert(PFC && "mapSectionAddress called on finalized LinkedObject"); @@ -140,7 +129,7 @@ private: PFC->RTDyld = &RTDyld; this->Finalized = true; - auto Err = PFC->Finalizer(PFC->Handle, RTDyld, std::move(PFC->Obj), + auto Err = PFC->Finalizer(RTDyld, std::move(PFC->Obj), [&]() { this->updateSymbolTable(RTDyld); }); // Release resources. @@ -204,7 +193,6 @@ private: std::shared_ptr<SymbolResolver> Resolver; FinalizerFtor Finalizer; bool ProcessAllSections; - ObjHandleT Handle; RuntimeDyld *RTDyld; }; @@ -257,20 +245,16 @@ public: } /// @brief Add an object to the JIT. - /// - /// @return A handle that can be used to refer to the loaded object (for - /// symbol searching, finalization, freeing memory, etc.). - Expected<ObjHandleT> addObject(VModuleKey K, ObjectPtr Obj) { - auto Finalizer = [&](ObjHandleT H, RuntimeDyld &RTDyld, - const ObjectPtr &ObjToLoad, - std::function<void()> LOSHandleLoad) -> Error { + Error addObject(VModuleKey K, ObjectPtr Obj) { + auto Finalizer = [&, K](RuntimeDyld &RTDyld, const ObjectPtr &ObjToLoad, + std::function<void()> LOSHandleLoad) -> Error { std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info = RTDyld.loadObject(*ObjToLoad->getBinary()); LOSHandleLoad(); if (this->NotifyLoaded) - this->NotifyLoaded(H, ObjToLoad, *Info); + this->NotifyLoaded(K, ObjToLoad, *Info); RTDyld.finalizeWithMemoryManagerLocking(); @@ -279,25 +263,21 @@ public: inconvertibleErrorCode()); if (this->NotifyFinalized) - this->NotifyFinalized(H); + this->NotifyFinalized(K); return Error::success(); }; - auto LO = + assert(!LinkedObjects.count(K) && "VModuleKey already in use"); + + LinkedObjects[K] = createLinkedObject(ES, std::move(Obj), GetMemMgr(K), GetResolver(K), std::move(Finalizer), ProcessAllSections); - // LOS is an owning-ptr. Keep a non-owning one so that we can set the handle - // below. - auto *LOPtr = LO.get(); - - ObjHandleT Handle = LinkedObjList.insert(LinkedObjList.end(), std::move(LO)); - LOPtr->setHandle(Handle); - return Handle; + return Error::success(); } - /// @brief Remove the object associated with handle H. + /// @brief Remove the object associated with VModuleKey K. /// /// All memory allocated for the object will be freed, and the sections and /// symbols it provided will no longer be available. No attempt is made to @@ -305,9 +285,10 @@ public: /// indirectly) will result in undefined behavior. If dependence tracking is /// required to detect or resolve such issues it should be added at a higher /// layer. - Error removeObject(ObjHandleT H) { + Error removeObject(VModuleKey K) { + assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); // How do we invalidate the symbols in H? - LinkedObjList.erase(H); + LinkedObjects.erase(K); return Error::success(); } @@ -316,40 +297,48 @@ public: /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it exists. JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) { - for (auto I = LinkedObjList.begin(), E = LinkedObjList.end(); I != E; - ++I) - if (auto Symbol = findSymbolIn(I, Name, ExportedSymbolsOnly)) - return Symbol; + for (auto &KV : LinkedObjects) + if (auto Sym = KV.second->getSymbol(Name, ExportedSymbolsOnly)) + return Sym; + else if (auto Err = Sym.takeError()) + return std::move(Err); return nullptr; } /// @brief Search for the given named symbol in the context of the loaded - /// object represented by the handle H. - /// @param H The handle for the object to search in. + /// object represented by the VModuleKey K. + /// @param K The VModuleKey for the object 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 object. - JITSymbol findSymbolIn(ObjHandleT H, StringRef Name, + JITSymbol findSymbolIn(VModuleKey K, StringRef Name, bool ExportedSymbolsOnly) { - return (*H)->getSymbol(Name, ExportedSymbolsOnly); + assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); + return LinkedObjects[K]->getSymbol(Name, ExportedSymbolsOnly); } - /// @brief Map section addresses for the object associated with the handle H. - void mapSectionAddress(ObjHandleT H, const void *LocalAddress, + /// @brief Map section addresses for the object associated with the + /// VModuleKey K. + void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) { - (*H)->mapSectionAddress(LocalAddress, TargetAddr); + assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); + LinkedObjects[K]->mapSectionAddress(LocalAddress, TargetAddr); } /// @brief Immediately emit and finalize the object represented by the given - /// handle. - /// @param H Handle for object to emit/finalize. - Error emitAndFinalize(ObjHandleT H) { return (*H)->finalize(); } + /// VModuleKey. + /// @param K VModuleKey for object to emit/finalize. + Error emitAndFinalize(VModuleKey K) { + assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); + return LinkedObjects[K]->finalize(); + } private: ExecutionSession &ES; - LinkedObjectListT LinkedObjList; + + std::map<VModuleKey, std::unique_ptr<LinkedObject>> LinkedObjects; MemoryManagerGetter GetMemMgr; ResolverGetter GetResolver; NotifyLoadedFtor NotifyLoaded; |