OSDN Git Service

[ORC] Remove Layer handles from the layer concept.
authorLang Hames <lhames@gmail.com>
Fri, 9 Feb 2018 02:30:40 +0000 (02:30 +0000)
committerLang Hames <lhames@gmail.com>
Fri, 9 Feb 2018 02:30:40 +0000 (02:30 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324700 91177308-0d34-0410-b5e6-96231b3b80d8

19 files changed:
examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h
examples/Kaleidoscope/include/KaleidoscopeJIT.h
include/llvm-c/OrcBindings.h
include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
include/llvm/ExecutionEngine/Orc/ExecutionUtils.h
include/llvm/ExecutionEngine/Orc/IRCompileLayer.h
include/llvm/ExecutionEngine/Orc/IRTransformLayer.h
include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h
include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h
include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h
lib/ExecutionEngine/Orc/OrcCBindingsStack.h
lib/ExecutionEngine/Orc/OrcMCJITReplacement.h
tools/lli/OrcLazyJIT.h
unittests/ExecutionEngine/Orc/ObjectTransformLayerTest.cpp
unittests/ExecutionEngine/Orc/RTDyldObjectLinkingLayerTest.cpp

index 9170943..a5553f4 100644 (file)
@@ -47,7 +47,6 @@ private:
   IRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
 
 public:
-  using ModuleHandle = decltype(CompileLayer)::ModuleHandleT;
 
   KaleidoscopeJIT()
       : ES(SSP),
@@ -74,9 +73,11 @@ public:
 
   TargetMachine &getTargetMachine() { return *TM; }
 
-  ModuleHandle addModule(std::unique_ptr<Module> M) {
+  VModuleKey addModule(std::unique_ptr<Module> M) {
     // Add the module to the JIT with a new VModuleKey.
-    return cantFail(CompileLayer.addModule(ES.allocateVModule(), std::move(M)));
+    auto K = ES.allocateVModule();
+    cantFail(CompileLayer.addModule(K, std::move(M)));
+    return K;
   }
 
   JITSymbol findSymbol(const std::string Name) {
@@ -90,8 +91,8 @@ public:
     return cantFail(findSymbol(Name).getAddress());
   }
 
-  void removeModule(ModuleHandle H) {
-    cantFail(CompileLayer.removeModule(H));
+  void removeModule(VModuleKey K) {
+    cantFail(CompileLayer.removeModule(K));
   }
 };
 
index 4b75493..c04244a 100644 (file)
@@ -56,7 +56,6 @@ private:
   IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer;
 
 public:
-  using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
 
   KaleidoscopeJIT()
       : ES(SSP),
@@ -86,10 +85,11 @@ public:
 
   TargetMachine &getTargetMachine() { return *TM; }
 
-  ModuleHandle addModule(std::unique_ptr<Module> M) {
+  VModuleKey addModule(std::unique_ptr<Module> M) {
     // Add the module to the JIT with a new VModuleKey.
-    return cantFail(
-        OptimizeLayer.addModule(ES.allocateVModule(), std::move(M)));
+    auto K = ES.allocateVModule();
+    cantFail(OptimizeLayer.addModule(K, std::move(M)));
+    return K;
   }
 
   JITSymbol findSymbol(const std::string Name) {
@@ -99,8 +99,8 @@ public:
     return OptimizeLayer.findSymbol(MangledNameStream.str(), true);
   }
 
-  void removeModule(ModuleHandle H) {
-    cantFail(OptimizeLayer.removeModule(H));
+  void removeModule(VModuleKey K) {
+    cantFail(OptimizeLayer.removeModule(K));
   }
 
 private:
index 43de6d9..767a118 100644 (file)
@@ -63,7 +63,6 @@ private:
   CompileOnDemandLayer<decltype(OptimizeLayer)> CODLayer;
 
 public:
-  using ModuleHandle = decltype(CODLayer)::ModuleHandleT;
 
   KaleidoscopeJIT()
       : ES(SSP), TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
@@ -92,7 +91,7 @@ public:
 
   TargetMachine &getTargetMachine() { return *TM; }
 
-  ModuleHandle addModule(std::unique_ptr<Module> M) {
+  VModuleKey addModule(std::unique_ptr<Module> M) {
     // Create a new VModuleKey.
     VModuleKey K = ES.allocateVModule();
 
@@ -111,7 +110,8 @@ public:
         [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); });
 
     // Add the module to the JIT with the new key.
-    return cantFail(CODLayer.addModule(K, std::move(M)));
+    cantFail(CODLayer.addModule(K, std::move(M)));
+    return K;
   }
 
   JITSymbol findSymbol(const std::string Name) {
@@ -121,8 +121,8 @@ public:
     return CODLayer.findSymbol(MangledNameStream.str(), true);
   }
 
-  void removeModule(ModuleHandle H) {
-    cantFail(CODLayer.removeModule(H));
+  void removeModule(VModuleKey K) {
+    cantFail(CODLayer.removeModule(K));
   }
 
 private:
index a95efd4..1fa169b 100644 (file)
@@ -89,7 +89,6 @@ private:
   std::unique_ptr<IndirectStubsManager> IndirectStubsMgr;
 
 public:
-  using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
 
   KaleidoscopeJIT()
       : ES(SSP),
@@ -127,10 +126,11 @@ public:
 
   TargetMachine &getTargetMachine() { return *TM; }
 
-  ModuleHandle addModule(std::unique_ptr<Module> M) {
+  VModuleKey addModule(std::unique_ptr<Module> M) {
     // Add the module to the JIT with a new VModuleKey.
-    return cantFail(
-        OptimizeLayer.addModule(ES.allocateVModule(), std::move(M)));
+    auto K = ES.allocateVModule();
+    cantFail(OptimizeLayer.addModule(K, std::move(M)));
+    return K;
   }
 
   Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) {
@@ -195,8 +195,8 @@ public:
     return OptimizeLayer.findSymbol(mangle(Name), true);
   }
 
-  void removeModule(ModuleHandle H) {
-    cantFail(OptimizeLayer.removeModule(H));
+  void removeModule(VModuleKey K) {
+    cantFail(OptimizeLayer.removeModule(K));
   }
 
 private:
index 2e9f5c3..806573f 100644 (file)
@@ -95,7 +95,6 @@ private:
   MyRemote &Remote;
 
 public:
-  using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
 
   KaleidoscopeJIT(MyRemote &Remote)
       : ES(SSP),
@@ -139,10 +138,11 @@ public:
 
   TargetMachine &getTargetMachine() { return *TM; }
 
-  ModuleHandle addModule(std::unique_ptr<Module> M) {
+  VModuleKey addModule(std::unique_ptr<Module> M) {
     // Add the module with a new VModuleKey.
-    return cantFail(
-        OptimizeLayer.addModule(ES.allocateVModule(), std::move(M)));
+    auto K = ES.allocateVModule();
+    cantFail(OptimizeLayer.addModule(K, std::move(M)));
+    return K;
   }
 
   Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) {
@@ -211,8 +211,8 @@ public:
     return OptimizeLayer.findSymbol(mangle(Name), true);
   }
 
-  void removeModule(ModuleHandle H) {
-    cantFail(OptimizeLayer.removeModule(H));
+  void removeModule(VModuleKey K) {
+    cantFail(OptimizeLayer.removeModule(K));
   }
 
 private:
index a1cbe02..c2f9d80 100644 (file)
@@ -42,7 +42,6 @@ class KaleidoscopeJIT {
 public:
   using ObjLayerT = RTDyldObjectLinkingLayer;
   using CompileLayerT = IRCompileLayer<ObjLayerT, SimpleCompiler>;
-  using ModuleHandleT = CompileLayerT::ModuleHandleT;
 
   KaleidoscopeJIT()
       : ES(SSP),
@@ -62,16 +61,16 @@ public:
 
   TargetMachine &getTargetMachine() { return *TM; }
 
-  ModuleHandleT addModule(std::unique_ptr<Module> M) {
-    auto H =
-        cantFail(CompileLayer.addModule(ES.allocateVModule(), std::move(M)));
-    ModuleHandles.push_back(H);
-    return H;
+  VModuleKey addModule(std::unique_ptr<Module> M) {
+    auto K = ES.allocateVModule();
+    cantFail(CompileLayer.addModule(K, std::move(M)));
+    ModuleKeys.push_back(K);
+    return K;
   }
 
-  void removeModule(ModuleHandleT H) {
-    ModuleHandles.erase(find(ModuleHandles, H));
-    cantFail(CompileLayer.removeModule(H));
+  void removeModule(VModuleKey K) {
+    ModuleKeys.erase(find(ModuleKeys, K));
+    cantFail(CompileLayer.removeModule(K));
   }
 
   JITSymbol findSymbol(const std::string Name) {
@@ -105,7 +104,7 @@ private:
     // Search modules in reverse order: from last added to first added.
     // This is the opposite of the usual search order for dlsym, but makes more
     // sense in a REPL where we want to bind to the newest available definition.
-    for (auto H : make_range(ModuleHandles.rbegin(), ModuleHandles.rend()))
+    for (auto H : make_range(ModuleKeys.rbegin(), ModuleKeys.rend()))
       if (auto Sym = CompileLayer.findSymbolIn(H, Name, ExportedSymbolsOnly))
         return Sym;
 
@@ -133,7 +132,7 @@ private:
   const DataLayout DL;
   ObjLayerT ObjectLayer;
   CompileLayerT CompileLayer;
-  std::vector<ModuleHandleT> ModuleHandles;
+  std::vector<VModuleKey> ModuleKeys;
 };
 
 } // end namespace orc
index abb3ac6..95bdef8 100644 (file)
@@ -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,
index c15d2c2..a30ffbe 100644 (file)
@@ -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;
 };
 
index 22071ff..074ec8b 100644 (file)
 #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
index 94a3086..ae2ad9f 100644 (file)
@@ -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;
index 31de6f1..e70cc89 100644 (file)
@@ -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; }
index 0da6e01..35792ea 100644 (file)
@@ -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);
   }
 };
 
index 46bf214..82e8aac 100644 (file)
@@ -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.
index 073d48e..c667257 100644 (file)
@@ -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;
index 049222d..22b6189 100644 (file)
@@ -33,6 +33,7 @@
 #include <algorithm>
 #include <cstdint>
 #include <functional>
+#include <map>
 #include <memory>
 #include <set>
 #include <string>
@@ -49,75 +50,56 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
 
 namespace detail {
 
+// FIXME: Kill this off once the Layer concept becomes an interface.
+class GenericLayer {
+public:
+  virtual ~GenericLayer() = default;
 
-  class GenericHandle {
-  public:
-    GenericHandle(orc::VModuleKey K) : K(K) {}
-
-    virtual ~GenericHandle() = default;
-
-    virtual JITSymbol findSymbolIn(const std::string &Name,
-                                   bool ExportedSymbolsOnly) = 0;
-    virtual Error removeModule(orc::ExecutionSession &ES) = 0;
-
-    orc::VModuleKey K;
+  virtual JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
+                                 bool ExportedSymbolsOnly) = 0;
+  virtual Error removeModule(orc::VModuleKey K) = 0;
   };
 
-  template <typename LayerT> class GenericHandleImpl : public GenericHandle {
+  template <typename LayerT> class GenericLayerImpl : public GenericLayer {
   public:
-    GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle,
-                      orc::VModuleKey K)
-        : GenericHandle(std::move(K)), Layer(Layer), Handle(std::move(Handle)) {
-    }
+    GenericLayerImpl(LayerT &Layer) : Layer(Layer) {}
 
-    JITSymbol findSymbolIn(const std::string &Name,
+    JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
                            bool ExportedSymbolsOnly) override {
-      return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
+      return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly);
     }
 
-    Error removeModule(orc::ExecutionSession &ES) override {
-      auto Err = Layer.removeModule(Handle);
-      ES.releaseVModule(K);
-      return Err;
+    Error removeModule(orc::VModuleKey K) override {
+      return Layer.removeModule(K);
     }
 
   private:
     LayerT &Layer;
-    typename LayerT::ModuleHandleT Handle;
   };
 
   template <>
-  class GenericHandleImpl<orc::RTDyldObjectLinkingLayer>
-    : public GenericHandle {
+  class GenericLayerImpl<orc::RTDyldObjectLinkingLayer> : public GenericLayer {
   private:
     using LayerT = orc::RTDyldObjectLinkingLayer;
   public:
-    GenericHandleImpl(LayerT &Layer, typename LayerT::ObjHandleT Handle,
-                      orc::VModuleKey K)
-        : GenericHandle(std::move(K)), Layer(Layer), Handle(std::move(Handle)) {
-    }
+    GenericLayerImpl(LayerT &Layer) : Layer(Layer) {}
 
-    JITSymbol findSymbolIn(const std::string &Name,
+    JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
                            bool ExportedSymbolsOnly) override {
-      return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
+      return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly);
     }
 
-    Error removeModule(orc::ExecutionSession &ES) override {
-      auto Err = Layer.removeObject(Handle);
-      ES.releaseVModule(K);
-      return Err;
+    Error removeModule(orc::VModuleKey K) override {
+      return Layer.removeObject(K);
     }
 
   private:
     LayerT &Layer;
-    typename LayerT::ObjHandleT Handle;
   };
 
-  template <typename LayerT, typename HandleT>
-  std::unique_ptr<GenericHandleImpl<LayerT>>
-  createGenericHandle(LayerT &Layer, HandleT Handle, orc::VModuleKey K) {
-    return llvm::make_unique<GenericHandleImpl<LayerT>>(
-        Layer, std::move(Handle), std::move(K));
+  template <typename LayerT>
+  std::unique_ptr<GenericLayerImpl<LayerT>> createGenericLayer(LayerT &Layer) {
+    return llvm::make_unique<GenericLayerImpl<LayerT>>(Layer);
   }
 
 } // end namespace detail
@@ -215,7 +197,6 @@ private:
   };
 
 public:
-  using ModuleHandleT = unsigned;
 
   OrcCBindingsStack(TargetMachine &TM,
                     std::unique_ptr<CompileCallbackMgr> CCMgr,
@@ -304,8 +285,7 @@ public:
   }
   template <typename LayerT>
   LLVMOrcErrorCode
-  addIRModule(ModuleHandleT &RetHandle, LayerT &Layer,
-              std::shared_ptr<Module> M,
+  addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::shared_ptr<Module> M,
               std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
               LLVMOrcSymbolResolverFn ExternalResolver,
               void *ExternalResolverCtx) {
@@ -323,54 +303,54 @@ public:
       DtorNames.push_back(mangle(Dtor.Func->getName()));
 
     // Add the module to the JIT.
-    ModuleHandleT H;
-    orc::VModuleKey K = ES.allocateVModule();
-    Resolvers[K] = std::make_shared<CBindingsResolver>(*this, ExternalResolver,
-                                                       ExternalResolverCtx);
-    if (auto LHOrErr = Layer.addModule(K, std::move(M)))
-      H = createHandle(Layer, *LHOrErr, K);
-    else
-      return mapError(LHOrErr.takeError());
+    RetKey = ES.allocateVModule();
+    Resolvers[RetKey] = std::make_shared<CBindingsResolver>(
+        *this, ExternalResolver, ExternalResolverCtx);
+    if (auto Err = Layer.addModule(RetKey, std::move(M)))
+      return mapError(std::move(Err));
+
+    KeyLayers[RetKey] = detail::createGenericLayer(Layer);
 
     // Run the static constructors, and save the static destructor runner for
     // execution when the JIT is torn down.
-    orc::CtorDtorRunner<OrcCBindingsStack> CtorRunner(std::move(CtorNames), H);
+    orc::CtorDtorRunner<OrcCBindingsStack> CtorRunner(std::move(CtorNames),
+                                                      RetKey);
     if (auto Err = CtorRunner.runViaLayer(*this))
       return mapError(std::move(Err));
 
-    IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
+    IRStaticDestructorRunners.emplace_back(std::move(DtorNames), RetKey);
 
-    RetHandle = H;
     return LLVMOrcErrSuccess;
   }
 
-  LLVMOrcErrorCode addIRModuleEager(ModuleHandleT &RetHandle,
+  LLVMOrcErrorCode addIRModuleEager(orc::VModuleKey &RetKey,
                                     std::shared_ptr<Module> M,
                                     LLVMOrcSymbolResolverFn ExternalResolver,
                                     void *ExternalResolverCtx) {
-    return addIRModule(RetHandle, CompileLayer, std::move(M),
+    return addIRModule(RetKey, CompileLayer, std::move(M),
                        llvm::make_unique<SectionMemoryManager>(),
                        std::move(ExternalResolver), ExternalResolverCtx);
   }
 
-  LLVMOrcErrorCode addIRModuleLazy(ModuleHandleT &RetHandle,
+  LLVMOrcErrorCode addIRModuleLazy(orc::VModuleKey &RetKey,
                                    std::shared_ptr<Module> M,
                                    LLVMOrcSymbolResolverFn ExternalResolver,
                                    void *ExternalResolverCtx) {
-    return addIRModule(RetHandle, CODLayer, std::move(M),
+    return addIRModule(RetKey, CODLayer, std::move(M),
                        llvm::make_unique<SectionMemoryManager>(),
                        std::move(ExternalResolver), ExternalResolverCtx);
   }
 
-  LLVMOrcErrorCode removeModule(ModuleHandleT H) {
-    if (auto Err = GenericHandles[H]->removeModule(ES))
+  LLVMOrcErrorCode removeModule(orc::VModuleKey K) {
+    // FIXME: Should error release the module key?
+    if (auto Err = KeyLayers[K]->removeModule(K))
       return mapError(std::move(Err));
-    GenericHandles[H] = nullptr;
-    FreeHandleIndexes.push_back(H);
+    ES.releaseVModule(K);
+    KeyLayers.erase(K);
     return LLVMOrcErrSuccess;
   }
 
-  LLVMOrcErrorCode addObject(ModuleHandleT &RetHandle,
+  LLVMOrcErrorCode addObject(orc::VModuleKey &RetKey,
                              std::unique_ptr<MemoryBuffer> ObjBuffer,
                              LLVMOrcSymbolResolverFn ExternalResolver,
                              void *ExternalResolverCtx) {
@@ -380,17 +360,14 @@ public:
       auto OwningObj =
         std::make_shared<OwningObject>(std::move(Obj), std::move(ObjBuffer));
 
-      orc::VModuleKey K = ES.allocateVModule();
-      Resolvers[K] = std::make_shared<CBindingsResolver>(
+      RetKey = ES.allocateVModule();
+      Resolvers[RetKey] = std::make_shared<CBindingsResolver>(
           *this, ExternalResolver, ExternalResolverCtx);
 
-      ModuleHandleT H;
-      if (auto HOrErr = ObjectLayer.addObject(K, std::move(OwningObj)))
-        H = createHandle(ObjectLayer, *HOrErr, K);
-      else
-        return mapError(HOrErr.takeError());
+      if (auto Err = ObjectLayer.addObject(RetKey, std::move(OwningObj)))
+        return mapError(std::move(Err));
 
-      RetHandle = H;
+      KeyLayers[RetKey] = detail::createGenericLayer(ObjectLayer);
 
       return LLVMOrcErrSuccess;
     } else
@@ -404,9 +381,9 @@ public:
     return CODLayer.findSymbol(mangle(Name), ExportedSymbolsOnly);
   }
 
-  JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
+  JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
                          bool ExportedSymbolsOnly) {
-    return GenericHandles[H]->findSymbolIn(Name, ExportedSymbolsOnly);
+    return KeyLayers[K]->findSymbolIn(K, Name, ExportedSymbolsOnly);
   }
 
   LLVMOrcErrorCode findSymbolAddress(JITTargetAddress &RetAddr,
@@ -432,22 +409,6 @@ public:
   const std::string &getErrorMessage() const { return ErrMsg; }
 
 private:
-  template <typename LayerT, typename HandleT>
-  unsigned createHandle(LayerT &Layer, HandleT Handle, orc::VModuleKey K) {
-    unsigned NewHandle;
-    if (!FreeHandleIndexes.empty()) {
-      NewHandle = FreeHandleIndexes.back();
-      FreeHandleIndexes.pop_back();
-      GenericHandles[NewHandle] =
-          detail::createGenericHandle(Layer, std::move(Handle), std::move(K));
-      return NewHandle;
-    } else {
-      NewHandle = GenericHandles.size();
-      GenericHandles.push_back(
-          detail::createGenericHandle(Layer, std::move(Handle), std::move(K)));
-    }
-    return NewHandle;
-  }
 
   LLVMOrcErrorCode mapError(Error Err) {
     LLVMOrcErrorCode Result = LLVMOrcErrSuccess;
@@ -479,8 +440,7 @@ private:
   CompileLayerT CompileLayer;
   CODLayerT CODLayer;
 
-  std::vector<std::unique_ptr<detail::GenericHandle>> GenericHandles;
-  std::vector<unsigned> FreeHandleIndexes;
+  std::map<orc::VModuleKey, std::unique_ptr<detail::GenericLayer>> KeyLayers;
 
   orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
   std::vector<orc::CtorDtorRunner<OrcCBindingsStack>> IRStaticDestructorRunners;
index cc5a8a5..f024674 100644 (file)
@@ -393,10 +393,10 @@ private:
 
     NotifyObjectLoadedT(OrcMCJITReplacement &M) : M(M) {}
 
-    void operator()(RTDyldObjectLinkingLayerBase::ObjHandleT H,
+    void operator()(VModuleKey K,
                     const RTDyldObjectLinkingLayer::ObjectPtr &Obj,
                     const RuntimeDyld::LoadedObjectInfo &Info) const {
-      M.UnfinalizedSections[H] = std::move(M.SectionsAllocatedSinceLastLoad);
+      M.UnfinalizedSections[K] = std::move(M.SectionsAllocatedSinceLastLoad);
       M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
       M.MemMgr->notifyObjectLoaded(&M, *Obj->getBinary());
     }
@@ -408,9 +408,7 @@ private:
   public:
     NotifyFinalizedT(OrcMCJITReplacement &M) : M(M) {}
 
-    void operator()(RTDyldObjectLinkingLayerBase::ObjHandleT H) {
-      M.UnfinalizedSections.erase(H);
-    }
+    void operator()(VModuleKey K) { M.UnfinalizedSections.erase(K); }
 
   private:
     OrcMCJITReplacement &M;
@@ -455,15 +453,8 @@ private:
   // that have been emitted but not yet finalized so that we can forward the
   // mapSectionAddress calls appropriately.
   using SectionAddrSet = std::set<const void *>;
-  struct ObjHandleCompare {
-    bool operator()(ObjectLayerT::ObjHandleT H1,
-                    ObjectLayerT::ObjHandleT H2) const {
-      return &*H1 < &*H2;
-    }
-  };
   SectionAddrSet SectionsAllocatedSinceLastLoad;
-  std::map<ObjectLayerT::ObjHandleT, SectionAddrSet, ObjHandleCompare>
-      UnfinalizedSections;
+  std::map<VModuleKey, SectionAddrSet> UnfinalizedSections;
 
   std::vector<object::OwningBinary<object::Archive>> Archives;
 };
index 8a5ab98..120fe29 100644 (file)
@@ -55,7 +55,6 @@ public:
   using IRDumpLayerT = orc::IRTransformLayer<CompileLayerT, TransformFtor>;
   using CODLayerT = orc::CompileOnDemandLayer<IRDumpLayerT, CompileCallbackMgr>;
   using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
-  using ModuleHandleT = CODLayerT::ModuleHandleT;
 
   OrcLazyJIT(std::unique_ptr<TargetMachine> TM,
              std::unique_ptr<CompileCallbackMgr> CCMgr,
@@ -138,7 +137,7 @@ public:
     //   1) Search the JIT symbols.
     //   2) Check for C++ runtime overrides.
     //   3) Search the host process (LLI)'s symbol table.
-    if (!ModulesHandle) {
+    if (!ModulesKey) {
       auto LegacyLookupInDylib = [this](const std::string &Name) -> JITSymbol {
         if (auto Sym = CODLayer.findSymbol(Name, true))
           return Sym;
@@ -160,9 +159,9 @@ public:
         return nullptr;
       };
 
-      auto K = ES.allocateVModule();
-      assert(!Resolvers.count(K) && "Resolver already present");
-      Resolvers[K] = orc::createSymbolResolver(
+      ModulesKey = ES.allocateVModule();
+      assert(!Resolvers.count(*ModulesKey) && "Resolver already present");
+      Resolvers[*ModulesKey] = orc::createSymbolResolver(
           [LegacyLookupInDylib](orc::SymbolFlagsMap &SymbolFlags,
                                 const orc::SymbolNameSet &Symbols) {
             auto NotFoundViaLegacyLookup = lookupFlagsWithLegacyFn(
@@ -181,24 +180,20 @@ public:
           });
 
       // Add the module to the JIT.
-      if (auto ModulesHandleOrErr =
-              CODLayer.addModule(std::move(K), std::move(M)))
-        ModulesHandle = std::move(*ModulesHandleOrErr);
-      else
-        return ModulesHandleOrErr.takeError();
+      if (auto Err = CODLayer.addModule(*ModulesKey, std::move(M)))
+        return Err;
 
-    } else if (auto Err = CODLayer.addExtraModule(*ModulesHandle, std::move(M)))
+    } else if (auto Err = CODLayer.addExtraModule(*ModulesKey, std::move(M)))
       return Err;
 
     // Run the static constructors, and save the static destructor runner for
     // execution when the JIT is torn down.
     orc::CtorDtorRunner<CODLayerT> CtorRunner(std::move(CtorNames),
-                                              *ModulesHandle);
+                                              *ModulesKey);
     if (auto Err = CtorRunner.runViaLayer(CODLayer))
       return Err;
 
-    IRStaticDestructorRunners.emplace_back(std::move(DtorNames),
-                                           *ModulesHandle);
+    IRStaticDestructorRunners.emplace_back(std::move(DtorNames), *ModulesKey);
 
     return Error::success();
   }
@@ -207,8 +202,8 @@ public:
     return CODLayer.findSymbol(mangle(Name), true);
   }
 
-  JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
-    return CODLayer.findSymbolIn(H, mangle(Name), true);
+  JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name) {
+    return CODLayer.findSymbolIn(K, mangle(Name), true);
   }
 
 private:
@@ -246,7 +241,7 @@ private:
 
   orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
   std::vector<orc::CtorDtorRunner<CODLayerT>> IRStaticDestructorRunners;
-  llvm::Optional<CODLayerT::ModuleHandleT> ModulesHandle;
+  llvm::Optional<orc::VModuleKey> ModulesKey;
 };
 
 int runOrcLazyJIT(std::vector<std::unique_ptr<Module>> Ms,
index 9de1c22..61e5749 100644 (file)
@@ -43,17 +43,13 @@ struct AllocatingTransform {
 //      transform layer called the base layer and forwarded any return value.
 class MockBaseLayer {
 public:
-  typedef int ObjHandleT;
-
   MockBaseLayer() : MockSymbol(nullptr) { resetExpectations(); }
 
-  template <typename ObjPtrT>
-  llvm::Expected<ObjHandleT> addObject(VModuleKey K, ObjPtrT Obj) {
+  template <typename ObjPtrT> llvm::Error addObject(VModuleKey K, ObjPtrT Obj) {
     EXPECT_EQ(MockKey, K) << "Key should pass through";
     EXPECT_EQ(MockObject + 1, *Obj) << "Transform should be applied";
     LastCalled = "addObject";
-    MockObjHandle = 111;
-    return MockObjHandle;
+    return llvm::Error::success();
   }
 
   template <typename ObjPtrT> void expectAddObject(VModuleKey K, ObjPtrT Obj) {
@@ -61,20 +57,18 @@ public:
     MockObject = *Obj;
   }
 
-
-  void verifyAddObject(ObjHandleT Returned) {
+  void verifyAddObject() {
     EXPECT_EQ("addObject", LastCalled);
-    EXPECT_EQ(MockObjHandle, Returned) << "Return should pass through";
     resetExpectations();
   }
 
-  llvm::Error removeObject(ObjHandleT H) {
-    EXPECT_EQ(MockObjHandle, H);
+  llvm::Error removeObject(VModuleKey K) {
+    EXPECT_EQ(MockKey, K);
     LastCalled = "removeObject";
     return llvm::Error::success();
   }
 
-  void expectRemoveObject(ObjHandleT H) { MockObjHandle = H; }
+  void expectRemoveObject(VModuleKey K) { MockKey = K; }
   void verifyRemoveObject() {
     EXPECT_EQ("removeObject", LastCalled);
     resetExpectations();
@@ -100,18 +94,18 @@ public:
     resetExpectations();
   }
 
-  llvm::JITSymbol findSymbolIn(ObjHandleT H, const std::string &Name,
+  llvm::JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
                                bool ExportedSymbolsOnly) {
-    EXPECT_EQ(MockObjHandle, H) << "Handle should pass through";
+    EXPECT_EQ(MockKey, K) << "VModuleKey should pass through";
     EXPECT_EQ(MockName, Name) << "Name should pass through";
     EXPECT_EQ(MockBool, ExportedSymbolsOnly) << "Flag should pass through";
     LastCalled = "findSymbolIn";
     MockSymbol = llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
     return llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
   }
-  void expectFindSymbolIn(ObjHandleT H, const std::string &Name,
+  void expectFindSymbolIn(VModuleKey K, const std::string &Name,
                           bool ExportedSymbolsOnly) {
-    MockObjHandle = H;
+    MockKey = K;
     MockName = Name;
     MockBool = ExportedSymbolsOnly;
   }
@@ -123,29 +117,29 @@ public:
     resetExpectations();
   }
 
-  llvm::Error emitAndFinalize(ObjHandleT H) {
-    EXPECT_EQ(MockObjHandle, H) << "Handle should pass through";
+  llvm::Error emitAndFinalize(VModuleKey K) {
+    EXPECT_EQ(MockKey, K) << "VModuleKey should pass through";
     LastCalled = "emitAndFinalize";
     return llvm::Error::success();
   }
 
-  void expectEmitAndFinalize(ObjHandleT H) { MockObjHandle = H; }
+  void expectEmitAndFinalize(VModuleKey K) { MockKey = K; }
 
   void verifyEmitAndFinalize() {
     EXPECT_EQ("emitAndFinalize", LastCalled);
     resetExpectations();
   }
 
-  void mapSectionAddress(ObjHandleT H, const void *LocalAddress,
+  void mapSectionAddress(VModuleKey K, const void *LocalAddress,
                          llvm::JITTargetAddress TargetAddr) {
-    EXPECT_EQ(MockObjHandle, H);
+    EXPECT_EQ(MockKey, K);
     EXPECT_EQ(MockLocalAddress, LocalAddress);
     EXPECT_EQ(MockTargetAddress, TargetAddr);
     LastCalled = "mapSectionAddress";
   }
-  void expectMapSectionAddress(ObjHandleT H, const void *LocalAddress,
+  void expectMapSectionAddress(VModuleKey K, const void *LocalAddress,
                                llvm::JITTargetAddress TargetAddr) {
-    MockObjHandle = H;
+    MockKey = K;
     MockLocalAddress = LocalAddress;
     MockTargetAddress = TargetAddr;
   }
@@ -159,7 +153,6 @@ private:
   std::string LastCalled;
   VModuleKey MockKey;
   MockObjectFile MockObject;
-  ObjHandleT MockObjHandle;
   std::string MockName;
   bool MockBool;
   llvm::JITSymbol MockSymbol;
@@ -172,7 +165,6 @@ private:
     LastCalled = "nothing";
     MockKey = 0;
     MockObject = 0;
-    MockObjHandle = 0;
     MockName = "bogus";
     MockSymbol = llvm::JITSymbol(nullptr);
     MockLocalAddress = nullptr;
@@ -206,20 +198,20 @@ TEST(ObjectTransformLayerTest, Main) {
   auto K1 = ES.allocateVModule();
   auto Obj1 = std::make_shared<MockObjectFile>(211);
   M.expectAddObject(K1, Obj1);
-  auto H = cantFail(T1.addObject(K1, std::move(Obj1)));
-  M.verifyAddObject(H);
+  cantFail(T1.addObject(K1, std::move(Obj1)));
+  M.verifyAddObject();
 
   // Test addObjectSet with T2 (mutating)
   auto K2 = ES.allocateVModule();
   auto Obj2 = std::make_shared<MockObjectFile>(222);
   M.expectAddObject(K2, Obj2);
-  H = cantFail(T2.addObject(K2, Obj2));
-  M.verifyAddObject(H);
+  cantFail(T2.addObject(K2, Obj2));
+  M.verifyAddObject();
   EXPECT_EQ(223, *Obj2) << "Expected mutation";
 
   // Test removeObjectSet
-  M.expectRemoveObject(H);
-  cantFail(T1.removeObject(H));
+  M.expectRemoveObject(K2);
+  cantFail(T1.removeObject(K2));
   M.verifyRemoveObject();
 
   // Test findSymbol
@@ -232,20 +224,20 @@ TEST(ObjectTransformLayerTest, Main) {
   // Test findSymbolIn
   Name = "bar";
   ExportedOnly = false;
-  M.expectFindSymbolIn(H, Name, ExportedOnly);
-  llvm::JITSymbol Sym2 = T1.findSymbolIn(H, Name, ExportedOnly);
+  M.expectFindSymbolIn(K1, Name, ExportedOnly);
+  llvm::JITSymbol Sym2 = T1.findSymbolIn(K1, Name, ExportedOnly);
   M.verifyFindSymbolIn(std::move(Sym2));
 
   // Test emitAndFinalize
-  M.expectEmitAndFinalize(H);
-  cantFail(T2.emitAndFinalize(H));
+  M.expectEmitAndFinalize(K1);
+  cantFail(T2.emitAndFinalize(K1));
   M.verifyEmitAndFinalize();
 
   // Test mapSectionAddress
   char Buffer[24];
   llvm::JITTargetAddress MockAddress = 255;
-  M.expectMapSectionAddress(H, Buffer, MockAddress);
-  T1.mapSectionAddress(H, Buffer, MockAddress);
+  M.expectMapSectionAddress(K1, Buffer, MockAddress);
+  T1.mapSectionAddress(K1, Buffer, MockAddress);
   M.verifyMapSectionAddress();
 
   // Verify transform getter (non-const)
@@ -317,11 +309,11 @@ TEST(ObjectTransformLayerTest, Main) {
 
   // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer
   // compile.
-  decltype(TransformLayer)::ObjHandleT H2;
-  cantFail(TransformLayer.emitAndFinalize(H2));
-  TransformLayer.findSymbolIn(H2, Name, false);
+  VModuleKey DummyKey = ES.allocateVModule();
+  cantFail(TransformLayer.emitAndFinalize(DummyKey));
+  TransformLayer.findSymbolIn(DummyKey, Name, false);
   TransformLayer.findSymbol(Name, true);
-  TransformLayer.mapSectionAddress(H2, nullptr, 0);
-  cantFail(TransformLayer.removeObject(H2));
+  TransformLayer.mapSectionAddress(DummyKey, nullptr, 0);
+  cantFail(TransformLayer.removeObject(DummyKey));
 }
 }
index 3e7d3f6..049bdac 100644 (file)
@@ -100,21 +100,23 @@ TEST(RTDyldObjectLinkingLayerTest, TestSetProcessAllSections) {
 
   {
     // Test with ProcessAllSections = false (the default).
-    auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), Obj));
-    cantFail(ObjLayer.emitAndFinalize(H));
+    auto K = ES.allocateVModule();
+    cantFail(ObjLayer.addObject(K, Obj));
+    cantFail(ObjLayer.emitAndFinalize(K));
     EXPECT_EQ(DebugSectionSeen, false)
       << "Unexpected debug info section";
-    cantFail(ObjLayer.removeObject(H));
+    cantFail(ObjLayer.removeObject(K));
   }
 
   {
     // Test with ProcessAllSections = true.
     ObjLayer.setProcessAllSections(true);
-    auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), Obj));
-    cantFail(ObjLayer.emitAndFinalize(H));
+    auto K = ES.allocateVModule();
+    cantFail(ObjLayer.addObject(K, Obj));
+    cantFail(ObjLayer.emitAndFinalize(K));
     EXPECT_EQ(DebugSectionSeen, true)
       << "Expected debug info section not seen";
-    cantFail(ObjLayer.removeObject(H));
+    cantFail(ObjLayer.removeObject(K));
   }
 }
 
@@ -198,9 +200,9 @@ TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoDuplicateFinalization) {
         return lookupWithLegacyFn(Query, Symbols, LegacyLookup);
       });
 
-  auto H = cantFail(ObjLayer.addObject(K2, std::move(Obj2)));
-  cantFail(ObjLayer.emitAndFinalize(H));
-  cantFail(ObjLayer.removeObject(H));
+  cantFail(ObjLayer.addObject(K2, std::move(Obj2)));
+  cantFail(ObjLayer.emitAndFinalize(K2));
+  cantFail(ObjLayer.removeObject(K2));
 
   // Finalization of module 2 should trigger finalization of module 1.
   // Verify that finalize on SMMW is only called once.
@@ -264,10 +266,11 @@ TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoPrematureAllocation) {
     std::make_shared<object::OwningBinary<object::ObjectFile>>(
       Compile(*MB2.getModule()));
 
-  auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj1)));
+  auto K = ES.allocateVModule();
+  cantFail(ObjLayer.addObject(K, std::move(Obj1)));
   cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj2)));
-  cantFail(ObjLayer.emitAndFinalize(H));
-  cantFail(ObjLayer.removeObject(H));
+  cantFail(ObjLayer.emitAndFinalize(K));
+  cantFail(ObjLayer.removeObject(K));
 
   // Only one call to needsToReserveAllocationSpace should have been made.
   EXPECT_EQ(MM->NeedsToReserveAllocationSpaceCount, 1)
@@ -281,8 +284,7 @@ TEST_F(RTDyldObjectLinkingLayerExecutionTest, TestNotifyLoadedSignature) {
   RTDyldObjectLinkingLayer ObjLayer(
       ES, [](VModuleKey) { return nullptr; },
       [](VModuleKey) { return std::make_shared<NullResolver>(); },
-      [](RTDyldObjectLinkingLayer::ObjHandleT,
-         const RTDyldObjectLinkingLayer::ObjectPtr &obj,
+      [](VModuleKey, const RTDyldObjectLinkingLayer::ObjectPtr &obj,
          const RuntimeDyld::LoadedObjectInfo &info) {});
 }