[llvm] r279952 - [Orc] Simplify LogicalDylib and move it back inside CompileOnDemandLayer. Also

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 28 17:54:30 PDT 2016


Author: lhames
Date: Sun Aug 28 19:54:29 2016
New Revision: 279952

URL: http://llvm.org/viewvc/llvm-project?rev=279952&view=rev
Log:
[Orc] Simplify LogicalDylib and move it back inside CompileOnDemandLayer. Also
switch to using one indirect stub manager per logical dylib rather than one per
input module.

LogicalDylib is a helper class used by the CompileOnDemandLayer to manage
symbol resolution between modules during lazy compilation. In particular, it
ensures that internal symbols resolve correctly even in the case where multiple
input modules contain the same internal symbol name (which must to be promoted
to external hidden linkage so that functions in any given module can be split
out by lazy compilation). LogicalDylib's resolution scheme (before this commit)
required one stub-manager per input module. This made recompilation of functions
(by adding a module containing a new definition) difficult, as the stub manager
for any given symbol was bound to the module that supplied the original
definition. By using one stubs manager for the whole logical dylib symbols can
be more easily replaced, although support for doing this is not included in this
patch (it will be implemented in a follow up).


Removed:
    llvm/trunk/include/llvm/ExecutionEngine/Orc/LogicalDylib.h
    llvm/trunk/unittests/ExecutionEngine/Orc/LogicalDylibTest.cpp
Modified:
    llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
    llvm/trunk/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h
    llvm/trunk/tools/lli/OrcLazyJIT.h
    llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h?rev=279952&r1=279951&r2=279952&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h Sun Aug 28 19:54:29 2016
@@ -17,7 +17,6 @@
 
 #include "IndirectionUtils.h"
 #include "LambdaResolver.h"
-#include "LogicalDylib.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Transforms/Utils/Cloning.h"
@@ -88,38 +87,32 @@ private:
     return llvm::make_unique<RO>(std::move(ResourcePtr));
   }
 
-  struct LogicalModuleResources {
-    std::unique_ptr<ResourceOwner<Module>> SourceModule;
-    std::set<const Function*> StubsToClone;
-    std::unique_ptr<IndirectStubsMgrT> StubsMgr;
+  class StaticGlobalRenamer {
+  public:
+    StaticGlobalRenamer() {}
 
-    LogicalModuleResources() = default;
+    StaticGlobalRenamer(StaticGlobalRenamer &&Other)
+      : NextId(Other.NextId) {}
 
-    // Explicit move constructor to make MSVC happy.
-    LogicalModuleResources(LogicalModuleResources &&Other)
-        : SourceModule(std::move(Other.SourceModule)),
-          StubsToClone(std::move(Other.StubsToClone)),
-          StubsMgr(std::move(Other.StubsMgr)) {}
-
-    // Explicit move assignment to make MSVC happy.
-    LogicalModuleResources& operator=(LogicalModuleResources &&Other) {
-      SourceModule = std::move(Other.SourceModule);
-      StubsToClone = std::move(Other.StubsToClone);
-      StubsMgr = std::move(Other.StubsMgr);
+    StaticGlobalRenamer& operator=(StaticGlobalRenamer &&Other) {
+      NextId = Other.NextId;
       return *this;
     }
 
-    JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
-      if (Name.endswith("$stub_ptr") && !ExportedSymbolsOnly) {
-        assert(!ExportedSymbolsOnly && "Stubs are never exported");
-        return StubsMgr->findPointer(Name.drop_back(9));
-      }
-      return StubsMgr->findStub(Name, ExportedSymbolsOnly);
+    void rename(Module &M) {
+      for (auto &F : M)
+        if (F.hasLocalLinkage())
+          F.setName("$static." + Twine(NextId++));
+      for (auto &G : M.globals())
+        if (G.hasLocalLinkage())
+          G.setName("$static." + Twine(NextId++));
     }
 
+  private:
+    unsigned NextId = 0;
   };
 
-  struct LogicalDylibResources {
+  struct LogicalDylib {
     typedef std::function<JITSymbol(const std::string&)> SymbolResolverFtor;
 
     typedef std::function<typename BaseLayerT::ModuleSetHandleT(
@@ -128,32 +121,84 @@ private:
                             std::unique_ptr<JITSymbolResolver>)>
       ModuleAdderFtor;
 
-    LogicalDylibResources() = default;
+    struct SourceModuleEntry {
+      std::unique_ptr<ResourceOwner<Module>> SourceMod;
+      std::set<Function*> StubsToClone;
+
+      SourceModuleEntry() = default;
+      SourceModuleEntry(SourceModuleEntry &&Other)
+          : SourceMod(std::move(Other.SourceMod)),
+            StubsToClone(std::move(Other.StubsToClone)) {}
+      SourceModuleEntry& operator=(SourceModuleEntry &&Other) {
+        SourceMod = std::move(Other.SourceMod);
+        StubsToClone = std::move(Other.StubsToClone);
+        return *this;
+      }
+    };
+
+    typedef std::vector<SourceModuleEntry> SourceModulesList;
+    typedef typename SourceModulesList::size_type SourceModuleHandle;
+
+    LogicalDylib() = default;
 
     // Explicit move constructor to make MSVC happy.
-    LogicalDylibResources(LogicalDylibResources &&Other)
+    LogicalDylib(LogicalDylib &&Other)
       : ExternalSymbolResolver(std::move(Other.ExternalSymbolResolver)),
         MemMgr(std::move(Other.MemMgr)),
-        ModuleAdder(std::move(Other.ModuleAdder)) {}
+        StubsMgr(std::move(Other.StubsMgr)),
+        StaticRenamer(std::move(Other.StaticRenamer)),
+        ModuleAdder(std::move(Other.ModuleAdder)),
+        SourceModules(std::move(Other.SourceModules)),
+        BaseLayerHandles(std::move(Other.BaseLayerHandles)) {}
 
     // Explicit move assignment operator to make MSVC happy.
-    LogicalDylibResources& operator=(LogicalDylibResources &&Other) {
+    LogicalDylib& operator=(LogicalDylib &&Other) {
       ExternalSymbolResolver = std::move(Other.ExternalSymbolResolver);
       MemMgr = std::move(Other.MemMgr);
+      StubsMgr = std::move(Other.StubsMgr);
+      StaticRenamer = std::move(Other.StaticRenamer);
       ModuleAdder = std::move(Other.ModuleAdder);
+      SourceModules = std::move(Other.SourceModules);
+      BaseLayerHandles = std::move(Other.BaseLayerHandles);
       return *this;
     }
 
+    SourceModuleHandle
+    addSourceModule(std::unique_ptr<ResourceOwner<Module>> M) {
+      SourceModuleHandle H = SourceModules.size();
+      SourceModules.push_back(SourceModuleEntry());
+      SourceModules.back().SourceMod = std::move(M);
+      return H;
+    }
+
+    Module& getSourceModule(SourceModuleHandle H) {
+      return SourceModules[H].SourceMod->getResource();
+    }
+
+    std::set<Function*>& getStubsToClone(SourceModuleHandle H) {
+      return SourceModules[H].StubsToClone;
+    }
+
+    JITSymbol findSymbol(BaseLayerT &BaseLayer, const std::string &Name,
+                         bool ExportedSymbolsOnly) {
+      if (auto Sym = StubsMgr->findStub(Name, ExportedSymbolsOnly))
+        return Sym;
+      for (auto BLH : BaseLayerHandles)
+        if (auto Sym = BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
+          return Sym;
+      return nullptr;
+    }
+
     std::unique_ptr<JITSymbolResolver> ExternalSymbolResolver;
     std::unique_ptr<ResourceOwner<RuntimeDyld::MemoryManager>> MemMgr;
+    std::unique_ptr<IndirectStubsMgrT> StubsMgr;
+    StaticGlobalRenamer StaticRenamer;
     ModuleAdderFtor ModuleAdder;
+    SourceModulesList SourceModules;
+    std::vector<BaseLayerModuleSetHandleT> BaseLayerHandles;
   };
 
-  typedef LogicalDylib<BaseLayerT, LogicalModuleResources,
-                       LogicalDylibResources> CODLogicalDylib;
-
-  typedef typename CODLogicalDylib::LogicalModuleHandle LogicalModuleHandle;
-  typedef std::list<CODLogicalDylib> LogicalDylibList;
+  typedef std::list<LogicalDylib> LogicalDylibList;
 
 public:
 
@@ -184,16 +229,15 @@ public:
                                 MemoryManagerPtrT MemMgr,
                                 SymbolResolverPtrT Resolver) {
 
-    LogicalDylibs.push_back(CODLogicalDylib(BaseLayer));
-    auto &LDResources = LogicalDylibs.back().getDylibResources();
-
-    LDResources.ExternalSymbolResolver = std::move(Resolver);
+    LogicalDylibs.push_back(LogicalDylib());
+    auto &LD = LogicalDylibs.back();
+    LD.ExternalSymbolResolver = std::move(Resolver);
+    LD.StubsMgr = CreateIndirectStubsManager();
 
     auto &MemMgrRef = *MemMgr;
-    LDResources.MemMgr =
-      wrapOwnership<RuntimeDyld::MemoryManager>(std::move(MemMgr));
+    LD.MemMgr = wrapOwnership<RuntimeDyld::MemoryManager>(std::move(MemMgr));
 
-    LDResources.ModuleAdder =
+    LD.ModuleAdder =
       [&MemMgrRef](BaseLayerT &B, std::unique_ptr<Module> M,
                    std::unique_ptr<JITSymbolResolver> R) {
         std::vector<std::unique_ptr<Module>> Ms;
@@ -222,9 +266,12 @@ public:
   /// @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 Symbol = findSymbolIn(LDI, Name, ExportedSymbolsOnly))
-        return Symbol;
+         LDI != LDE; ++LDI) {
+      if (auto Sym = LDI->StubsMgr->findStub(Name, ExportedSymbolsOnly))
+        return Sym;
+      if (auto Sym = findSymbolIn(LDI, Name, ExportedSymbolsOnly))
+        return Sym;
+    }
     return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
   }
 
@@ -232,7 +279,7 @@ public:
   ///        below this one.
   JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
                          bool ExportedSymbolsOnly) {
-    return H->findSymbol(Name, ExportedSymbolsOnly);
+    return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly);
   }
 
   /// @brief Update the stub for the given function to point at FnBodyAddr.
@@ -264,25 +311,24 @@ public:
 private:
 
   template <typename ModulePtrT>
-  void addLogicalModule(CODLogicalDylib &LD, ModulePtrT SrcMPtr) {
+  void addLogicalModule(LogicalDylib &LD, ModulePtrT SrcMPtr) {
+
+    // Rename all static functions / globals to $static.X :
+    // This will unique the names across all modules in the logical dylib,
+    // simplifying symbol lookup.
+    LD.StaticRenamer.rename(*SrcMPtr);
 
     // Bump the linkage and rename any anonymous/privote members in SrcM to
     // ensure that everything will resolve properly after we partition SrcM.
     makeAllSymbolsExternallyAccessible(*SrcMPtr);
 
     // Create a logical module handle for SrcM within the logical dylib.
-    auto LMH = LD.createLogicalModule();
-    auto &LMResources =  LD.getLogicalModuleResources(LMH);
-
-    LMResources.SourceModule = wrapOwnership<Module>(std::move(SrcMPtr));
-
-    Module &SrcM = LMResources.SourceModule->getResource();
+    Module &SrcM = *SrcMPtr;
+    auto LMId = LD.addSourceModule(wrapOwnership<Module>(std::move(SrcMPtr)));
 
     // Create stub functions.
     const DataLayout &DL = SrcM.getDataLayout();
     {
-      LMResources.StubsMgr = CreateIndirectStubsManager();
-
       typename IndirectStubsMgrT::StubInitsMap StubInits;
       for (auto &F : SrcM) {
         // Skip declarations.
@@ -292,12 +338,12 @@ private:
         // Skip weak functions for which we already have definitions.
         auto MangledName = mangle(F.getName(), DL);
         if (F.hasWeakLinkage() || F.hasLinkOnceLinkage())
-          if (auto Sym = LD.findSymbol(MangledName, false))
+          if (auto Sym = LD.findSymbol(BaseLayer, MangledName, false))
             continue;
 
         // Record all functions defined by this module.
         if (CloneStubsIntoPartitions)
-          LMResources.StubsToClone.insert(&F);
+          LD.getStubsToClone(LMId).insert(&F);
 
         // Create a callback, associate it with the stub for the function,
         // and set the compile action to compile the partition containing the
@@ -306,12 +352,12 @@ private:
         StubInits[MangledName] =
           std::make_pair(CCInfo.getAddress(),
                          JITSymbolFlags::fromGlobalValue(F));
-        CCInfo.setCompileAction([this, &LD, LMH, &F]() {
-          return this->extractAndCompile(LD, LMH, F);
+        CCInfo.setCompileAction([this, &LD, LMId, &F]() {
+          return this->extractAndCompile(LD, LMId, F);
         });
       }
 
-      auto EC = LMResources.StubsMgr->createStubs(StubInits);
+      auto EC = LD.StubsMgr->createStubs(StubInits);
       (void)EC;
       // FIXME: This should be propagated back to the user. Stub creation may
       //        fail for remote JITs.
@@ -346,7 +392,7 @@ private:
     // Initializers may refer to functions declared (but not defined) in this
     // module. Build a materializer to clone decls on demand.
     auto Materializer = createLambdaMaterializer(
-      [this, &GVsM, &LMResources](Value *V) -> Value* {
+      [this, &LD, &GVsM](Value *V) -> Value* {
         if (auto *F = dyn_cast<Function>(V)) {
           // Decls in the original module just get cloned.
           if (F->isDeclaration())
@@ -357,7 +403,7 @@ private:
           // instead.
           const DataLayout &DL = GVsM->getDataLayout();
           std::string FName = mangle(F->getName(), DL);
-          auto StubSym = LMResources.StubsMgr->findStub(FName, false);
+          auto StubSym = LD.StubsMgr->findStub(FName, false);
           unsigned PtrBitWidth = DL.getPointerTypeSizeInBits(F->getType());
           ConstantInt *StubAddr =
             ConstantInt::get(GVsM->getContext(),
@@ -389,20 +435,20 @@ private:
 
     // Build a resolver for the globals module and add it to the base layer.
     auto GVsResolver = createLambdaResolver(
-        [&LD, LMH](const std::string &Name) {
-          if (auto Sym = LD.findSymbol(Name, false))
+        [this, &LD, LMId](const std::string &Name) {
+          if (auto Sym = LD.StubsMgr->findStub(Name, false))
+            return Sym;
+          if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
             return Sym;
-          auto &LDResolver = LD.getDylibResources().ExternalSymbolResolver;
-          return LDResolver->findSymbolInLogicalDylib(Name);
+          return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
         },
         [&LD](const std::string &Name) {
-          auto &LDResolver = LD.getDylibResources().ExternalSymbolResolver;
-          return LDResolver->findSymbol(Name);
+          return LD.ExternalSymbolResolver->findSymbol(Name);
         });
 
-    auto GVsH = LD.getDylibResources().ModuleAdder(BaseLayer, std::move(GVsM),
-                                                   std::move(GVsResolver));
-    LD.addToLogicalModule(LMH, GVsH);
+    auto GVsH = LD.ModuleAdder(BaseLayer, std::move(GVsM),
+                               std::move(GVsResolver));
+    LD.BaseLayerHandles.push_back(GVsH);
   }
 
   static std::string mangle(StringRef Name, const DataLayout &DL) {
@@ -414,11 +460,11 @@ private:
     return MangledName;
   }
 
-  JITTargetAddress extractAndCompile(CODLogicalDylib &LD,
-                                     LogicalModuleHandle LMH,
-                                     Function &F) {
-    auto &LMResources = LD.getLogicalModuleResources(LMH);
-    Module &SrcM = LMResources.SourceModule->getResource();
+  JITTargetAddress
+  extractAndCompile(LogicalDylib &LD,
+                    typename LogicalDylib::SourceModuleHandle LMId,
+                    Function &F) {
+    Module &SrcM = LD.getSourceModule(LMId);
 
     // If F is a declaration we must already have compiled it.
     if (F.isDeclaration())
@@ -428,7 +474,7 @@ private:
     std::string CalledFnName = mangle(F.getName(), SrcM.getDataLayout());
 
     auto Part = Partition(F);
-    auto PartH = emitPartition(LD, LMH, Part);
+    auto PartH = emitPartition(LD, LMId, Part);
 
     JITTargetAddress CalledAddr = 0;
     for (auto *SubF : Part) {
@@ -444,7 +490,7 @@ private:
         CalledAddr = FnBodyAddr;
 
       // Update the function body pointer for the stub.
-      if (auto EC = LMResources.StubsMgr->updatePointer(FnName, FnBodyAddr))
+      if (auto EC = LD.StubsMgr->updatePointer(FnName, FnBodyAddr))
         return 0;
     }
 
@@ -452,11 +498,11 @@ private:
   }
 
   template <typename PartitionT>
-  BaseLayerModuleSetHandleT emitPartition(CODLogicalDylib &LD,
-                                          LogicalModuleHandle LMH,
-                                          const PartitionT &Part) {
-    auto &LMResources = LD.getLogicalModuleResources(LMH);
-    Module &SrcM = LMResources.SourceModule->getResource();
+  BaseLayerModuleSetHandleT
+  emitPartition(LogicalDylib &LD,
+                typename LogicalDylib::SourceModuleHandle LMId,
+                const PartitionT &Part) {
+    Module &SrcM = LD.getSourceModule(LMId);
 
     // Create the module.
     std::string NewName = SrcM.getName();
@@ -469,14 +515,14 @@ private:
     M->setDataLayout(SrcM.getDataLayout());
     ValueToValueMapTy VMap;
 
-    auto Materializer = createLambdaMaterializer([this, &LMResources, &M,
+    auto Materializer = createLambdaMaterializer([this, &LD, &LMId, &M,
                                                   &VMap](Value *V) -> Value * {
       if (auto *GV = dyn_cast<GlobalVariable>(V))
         return cloneGlobalVariableDecl(*M, *GV);
 
       if (auto *F = dyn_cast<Function>(V)) {
         // Check whether we want to clone an available_externally definition.
-        if (!LMResources.StubsToClone.count(F))
+        if (!LD.getStubsToClone(LMId).count(F))
           return cloneFunctionDecl(*M, *F);
 
         // Ok - we want an inlinable stub. For that to work we need a decl
@@ -516,19 +562,16 @@ private:
 
     // Create memory manager and symbol resolver.
     auto Resolver = createLambdaResolver(
-        [this, &LD, LMH](const std::string &Name) {
-          if (auto Sym = LD.findSymbolInternally(LMH, Name))
+        [this, &LD, LMId](const std::string &Name) {
+          if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
             return Sym;
-          auto &LDResolver = LD.getDylibResources().ExternalSymbolResolver;
-          return LDResolver->findSymbolInLogicalDylib(Name);
+          return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
         },
         [this, &LD](const std::string &Name) {
-          auto &LDResolver = LD.getDylibResources().ExternalSymbolResolver;
-          return LDResolver->findSymbol(Name);
+          return LD.ExternalSymbolResolver->findSymbol(Name);
         });
 
-    return LD.getDylibResources().ModuleAdder(BaseLayer, std::move(M),
-                                              std::move(Resolver));
+    return LD.ModuleAdder(BaseLayer, std::move(M), std::move(Resolver));
   }
 
   BaseLayerT &BaseLayer;

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h?rev=279952&r1=279951&r2=279952&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h Sun Aug 28 19:54:29 2016
@@ -43,12 +43,12 @@ public:
   ///   This class provides a read-only view of the element with any casts on
   /// the function stripped away.
   struct Element {
-    Element(unsigned Priority, const Function *Func, const Value *Data)
+    Element(unsigned Priority, Function *Func, Value *Data)
       : Priority(Priority), Func(Func), Data(Data) {}
 
     unsigned Priority;
-    const Function *Func;
-    const Value *Data;
+    Function *Func;
+    Value *Data;
   };
 
   /// @brief Construct an iterator instance. If End is true then this iterator

Removed: llvm/trunk/include/llvm/ExecutionEngine/Orc/LogicalDylib.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/LogicalDylib.h?rev=279951&view=auto
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/LogicalDylib.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/LogicalDylib.h (removed)
@@ -1,146 +0,0 @@
-//===--- LogicalDylib.h - Simulates dylib-style symbol lookup ---*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// Simulates symbol resolution inside a dylib.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
-#define LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
-
-#include "llvm/ExecutionEngine/JITSymbol.h"
-#include <string>
-#include <vector>
-
-namespace llvm {
-namespace orc {
-
-template <typename BaseLayerT,
-          typename LogicalModuleResources,
-          typename LogicalDylibResources>
-class LogicalDylib {
-public:
-  typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
-private:
-
-  typedef std::vector<BaseLayerModuleSetHandleT> BaseLayerHandleList;
-
-  struct LogicalModule {
-    // Make this move-only to ensure they don't get duplicated across moves of
-    // LogicalDylib or anything like that.
-    LogicalModule(LogicalModule &&RHS)
-        : Resources(std::move(RHS.Resources)),
-          BaseLayerHandles(std::move(RHS.BaseLayerHandles)) {}
-    LogicalModule() = default;
-    LogicalModuleResources Resources;
-    BaseLayerHandleList BaseLayerHandles;
-  };
-  typedef std::vector<LogicalModule> LogicalModuleList;
-
-public:
-
-  typedef typename BaseLayerHandleList::iterator BaseLayerHandleIterator;
-  typedef typename LogicalModuleList::size_type LogicalModuleHandle;
-
-  LogicalDylib(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
-
-  ~LogicalDylib() {
-    for (auto &LM : LogicalModules)
-      for (auto BLH : LM.BaseLayerHandles)
-        BaseLayer.removeModuleSet(BLH);
-  }
-
-  // If possible, remove this and ~LogicalDylib once the work in the dtor is
-  // moved to members (eg: self-unregistering base layer handles).
-  LogicalDylib(LogicalDylib &&RHS)
-      : BaseLayer(std::move(RHS.BaseLayer)),
-        LogicalModules(std::move(RHS.LogicalModules)),
-        DylibResources(std::move(RHS.DylibResources)) {}
-
-  LogicalModuleHandle createLogicalModule() {
-    LogicalModules.push_back(LogicalModule());
-    return LogicalModules.size() - 1;
-  }
-
-  void addToLogicalModule(LogicalModuleHandle LMH,
-                          BaseLayerModuleSetHandleT BaseLayerHandle) {
-    LogicalModules[LMH].BaseLayerHandles.push_back(BaseLayerHandle);
-  }
-
-  LogicalModuleResources& getLogicalModuleResources(LogicalModuleHandle LMH) {
-    return LogicalModules[LMH].Resources;
-  }
-
-  BaseLayerHandleIterator moduleHandlesBegin(LogicalModuleHandle LMH) {
-    return LogicalModules[LMH].BaseLayerHandles.begin();
-  }
-
-  BaseLayerHandleIterator moduleHandlesEnd(LogicalModuleHandle LMH) {
-    return LogicalModules[LMH].BaseLayerHandles.end();
-  }
-
-  JITSymbol findSymbolInLogicalModule(LogicalModuleHandle LMH,
-                                      const std::string &Name,
-                                      bool ExportedSymbolsOnly) {
-
-    if (auto StubSym =
-          LogicalModules[LMH].Resources.findSymbol(Name, ExportedSymbolsOnly))
-      return StubSym;
-
-    for (auto BLH : LogicalModules[LMH].BaseLayerHandles)
-      if (auto Symbol = BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
-        return Symbol;
-    return nullptr;
-  }
-
-  JITSymbol findSymbolInternally(LogicalModuleHandle LMH,
-                                 const std::string &Name) {
-    if (auto Symbol = findSymbolInLogicalModule(LMH, Name, false))
-      return Symbol;
-
-    for (typename LogicalModuleList::size_type I = 0, E = LogicalModules.size();
-         I != E; ++I) {
-      if (I != LMH)
-        if (auto Symbol = findSymbolInLogicalModule(I, Name, false))
-          return Symbol;
-    }
-
-    return nullptr;
-  }
-
-  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
-    for (typename LogicalModuleList::size_type I = 0, E = LogicalModules.size();
-         I != E; ++I)
-      if (auto Sym = findSymbolInLogicalModule(I, Name, ExportedSymbolsOnly))
-        return Sym;
-    return nullptr;
-  }
-
-  LogicalDylibResources& getDylibResources() { return DylibResources; }
-
-  LogicalModuleResources*
-  getLogicalModuleResourcesForSymbol(const std::string &Name,
-                                     bool ExportedSymbolsOnly) {
-    for (typename LogicalModuleList::size_type I = 0, E = LogicalModules.size();
-         I != E; ++I)
-      if (auto Sym = LogicalModules[I].Resources.findSymbol(Name, ExportedSymbolsOnly))
-        return &LogicalModules[I].Resources;
-    return nullptr;
-  }
-
-protected:
-  BaseLayerT BaseLayer;
-  LogicalModuleList LogicalModules;
-  LogicalDylibResources DylibResources;
-};
-
-} // End namespace orc.
-} // End namespace llvm.
-
-#endif // LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H

Modified: llvm/trunk/tools/lli/OrcLazyJIT.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/OrcLazyJIT.h?rev=279952&r1=279951&r2=279952&view=diff
==============================================================================
--- llvm/trunk/tools/lli/OrcLazyJIT.h (original)
+++ llvm/trunk/tools/lli/OrcLazyJIT.h Sun Aug 28 19:54:29 2016
@@ -68,14 +68,28 @@ public:
       if (M->getDataLayout().isDefault())
         M->setDataLayout(DL);
 
-    // Record the static constructors and destructors. We have to do this before
-    // we hand over ownership of the module to the JIT.
+    // Rename, bump linkage and record static constructors and destructors.
+    // We have to do this before we hand over ownership of the module to the
+    // JIT.
     std::vector<std::string> CtorNames, DtorNames;
-    for (auto &M : Ms) {
-      for (auto Ctor : orc::getConstructors(*M))
-        CtorNames.push_back(mangle(Ctor.Func->getName()));
-      for (auto Dtor : orc::getDestructors(*M))
-        DtorNames.push_back(mangle(Dtor.Func->getName()));
+    {
+      unsigned CtorId = 0, DtorId = 0;
+      for (auto &M : Ms) {
+        for (auto Ctor : orc::getConstructors(*M)) {
+          std::string NewCtorName = ("$static_ctor." + Twine(CtorId++)).str();
+          Ctor.Func->setName(NewCtorName);
+          Ctor.Func->setLinkage(GlobalValue::ExternalLinkage);
+          Ctor.Func->setVisibility(GlobalValue::HiddenVisibility);
+          CtorNames.push_back(mangle(NewCtorName));
+        }
+        for (auto Dtor : orc::getDestructors(*M)) {
+          std::string NewDtorName = ("$static_dtor." + Twine(DtorId++)).str();
+          Dtor.Func->setLinkage(GlobalValue::ExternalLinkage);
+          Dtor.Func->setVisibility(GlobalValue::HiddenVisibility);
+          DtorNames.push_back(mangle(Dtor.Func->getName()));
+          Dtor.Func->setName(NewDtorName);
+        }
+      }
     }
 
     // Symbol resolution order:

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt?rev=279952&r1=279951&r2=279952&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt Sun Aug 28 19:54:29 2016
@@ -14,7 +14,6 @@ add_llvm_unittest(OrcJITTests
   IndirectionUtilsTest.cpp
   GlobalMappingLayerTest.cpp
   LazyEmittingLayerTest.cpp
-  LogicalDylibTest.cpp
   ObjectLinkingLayerTest.cpp
   ObjectTransformLayerTest.cpp
   OrcCAPITest.cpp

Removed: llvm/trunk/unittests/ExecutionEngine/Orc/LogicalDylibTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/LogicalDylibTest.cpp?rev=279951&view=auto
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/LogicalDylibTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/LogicalDylibTest.cpp (removed)
@@ -1,76 +0,0 @@
-//===----- CompileOnDemandLayerTest.cpp - Unit tests for the COD layer ----===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "OrcTestCommon.h"
-#include "llvm/ExecutionEngine/Orc/LogicalDylib.h"
-#include "gtest/gtest.h"
-
-using namespace llvm;
-using namespace llvm::orc;
-
-namespace {
-
-
-TEST(LogicalDylibTest, getLogicalModuleResourcesForSymbol) {
-
-  std::map<int, std::set<std::string>> ModuleSymbols;
-
-  ModuleSymbols[0] = std::set<std::string>({ "foo", "dummy" });
-  ModuleSymbols[1] = std::set<std::string>({ "bar" });
-  ModuleSymbols[2] = std::set<std::string>({ "baz", "dummy" });
-
-  auto MockBaseLayer = createMockBaseLayer<int>(
-      DoNothingAndReturn<int>(0),
-      DoNothingAndReturn<void>(),
-      [&](const std::string &Name, bool) {
-        for (auto &S : ModuleSymbols)
-          if (S.second.count(Name))
-            return JITSymbol(1, JITSymbolFlags::Exported);
-        return JITSymbol(nullptr);
-      },
-      [&](int H, const std::string &Name, bool) {
-        if (ModuleSymbols[H].count(Name))
-          return JITSymbol(1, JITSymbolFlags::Exported);
-        return JITSymbol(nullptr);
-      });
-
-  struct LDResources { };
-  struct LMResources {
-  public:
-    int ID;
-    std::set<std::string> *Symbols;
-
-    LMResources() : ID(0), Symbols(nullptr) {}
-    LMResources(int ID, std::set<std::string> &Symbols)
-        : ID(ID), Symbols(&Symbols) {}
-
-    JITSymbol findSymbol(const std::string &Name, bool) {
-      assert(Symbols);
-      if (Symbols->count(Name))
-        return JITSymbol(ID, JITSymbolFlags::Exported);
-      return JITSymbol(nullptr);
-    }
-  };
-
-  LogicalDylib<decltype(MockBaseLayer), LMResources, LDResources>
-    LD(MockBaseLayer);
-
-  // Add logical module resources for each of our dummy modules.
-  for (int I = 0; I < 3; ++I) {
-    auto H = LD.createLogicalModule();
-    LD.addToLogicalModule(H, I);
-    LD.getLogicalModuleResources(H) = LMResources(I, ModuleSymbols[I]);
-  }
-
-  {
-    auto LMR = LD.getLogicalModuleResourcesForSymbol("bar", true);
-    EXPECT_TRUE(LMR->ID == 1) << "getLogicalModuleResourcesForSymbol failed";
-  }
-}
-}




More information about the llvm-commits mailing list