[llvm] r343257 - [ORC] Coalesce all of ORC's symbol renaming / linkage-promotion utilities into

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 27 12:27:21 PDT 2018


Author: lhames
Date: Thu Sep 27 12:27:20 2018
New Revision: 343257

URL: http://llvm.org/viewvc/llvm-project?rev=343257&view=rev
Log:
[ORC] Coalesce all of ORC's symbol renaming / linkage-promotion utilities into
one SymbolLinkagePromoter utility.

SymbolLinkagePromoter renames anonymous and private symbols, and bumps all
linkages to at least global/hidden-visibility. Modules whose symbols have been
promoted by this utility can be decomposed into sub-modules without introducing
link errors. This is used by the CompileOnDemandLayer to extract single-function
modules for lazy compilation.

Modified:
    llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
    llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
    llvm/trunk/include/llvm/ExecutionEngine/Orc/LLJIT.h
    llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
    llvm/trunk/lib/ExecutionEngine/Orc/LLJIT.cpp
    llvm/trunk/tools/lli/lli.cpp

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=343257&r1=343256&r2=343257&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h Thu Sep 27 12:27:20 2018
@@ -167,25 +167,6 @@ private:
     return llvm::make_unique<RO>(std::move(ResourcePtr));
   }
 
-  class StaticGlobalRenamer {
-  public:
-    StaticGlobalRenamer() = default;
-    StaticGlobalRenamer(StaticGlobalRenamer &&) = default;
-    StaticGlobalRenamer &operator=(StaticGlobalRenamer &&) = default;
-
-    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 LogicalDylib {
     struct SourceModuleEntry {
       std::unique_ptr<Module> SourceMod;
@@ -239,7 +220,7 @@ private:
     VModuleKey K;
     std::shared_ptr<SymbolResolver> BackingResolver;
     std::unique_ptr<IndirectStubsMgrT> StubsMgr;
-    StaticGlobalRenamer StaticRenamer;
+    SymbolLinkagePromoter PromoteSymbols;
     SourceModulesList SourceModules;
     std::vector<VModuleKey> BaseLayerVModuleKeys;
   };
@@ -361,14 +342,9 @@ public:
 private:
   Error addLogicalModule(LogicalDylib &LD, std::unique_ptr<Module> 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/private members in SrcM to
-    // ensure that everything will resolve properly after we partition SrcM.
-    makeAllSymbolsExternallyAccessible(*SrcMPtr);
+    // Rename anonymous globals and promote linkage to ensure that everything
+    // will resolve properly after we partition SrcM.
+    LD.PromoteSymbols(*SrcMPtr);
 
     // Create a logical module handle for SrcM within the logical dylib.
     Module &SrcM = *SrcMPtr;

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h?rev=343257&r1=343256&r2=343257&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h Thu Sep 27 12:27:20 2018
@@ -413,12 +413,16 @@ GlobalVariable *createImplPointer(Pointe
 ///        indirect call using the given function pointer.
 void makeStub(Function &F, Value &ImplPointer);
 
-/// Raise linkage types and rename as necessary to ensure that all
-///        symbols are accessible for other modules.
-///
-///   This should be called before partitioning a module to ensure that the
-/// partitions retain access to each other's symbols.
-void makeAllSymbolsExternallyAccessible(Module &M);
+/// Promotes private symbols to global hidden, and renames to prevent clashes
+/// with other promoted symbols. The same SymbolPromoter instance should be
+/// used for all symbols to be added to a single JITDylib.
+class SymbolLinkagePromoter {
+public:
+  void operator()(Module &M);
+
+private:
+  unsigned NextId = 0;
+};
 
 /// Clone a function declaration into a new module.
 ///

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/LLJIT.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/LLJIT.h?rev=343257&r1=343256&r2=343257&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/LLJIT.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/LLJIT.h Thu Sep 27 12:27:20 2018
@@ -173,6 +173,7 @@ private:
 
   IRTransformLayer2 TransformLayer;
   CompileOnDemandLayer2 CODLayer;
+  SymbolLinkagePromoter PromoteSymbols;
 };
 
 } // End namespace orc

Modified: llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp?rev=343257&r1=343256&r2=343257&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp Thu Sep 27 12:27:20 2018
@@ -246,57 +246,23 @@ void makeStub(Function &F, Value &ImplPo
     Builder.CreateRet(Call);
 }
 
-// Utility class for renaming global values and functions during partitioning.
-class GlobalRenamer {
-public:
+void SymbolLinkagePromoter::operator()(Module &M) {
+  for (auto &GV : M.global_values()) {
 
-  static bool needsRenaming(const Value &New) {
-    return !New.hasName() || New.getName().startswith("\01L");
-  }
-
-  const std::string& getRename(const Value &Orig) {
-    // See if we have a name for this global.
-    {
-      auto I = Names.find(&Orig);
-      if (I != Names.end())
-        return I->second;
+    // Rename if necessary.
+    if (!GV.hasName())
+      GV.setName("__orc_anon." + Twine(NextId++));
+    else if (GV.getName().startswith("\01L"))
+      GV.setName("__" + GV.getName().substr(1) + "." + Twine(NextId++));
+    else if (GV.hasLocalLinkage())
+      GV.setName("__orc_lcl." + GV.getName() + "." + Twine(NextId++));
+
+    if (GV.hasLocalLinkage()) {
+      GV.setLinkage(GlobalValue::ExternalLinkage);
+      GV.setVisibility(GlobalValue::HiddenVisibility);
     }
-
-    // Nope. Create a new one.
-    // FIXME: Use a more robust uniquing scheme. (This may blow up if the user
-    //        writes a "__orc_anon[[:digit:]]* method).
-    unsigned ID = Names.size();
-    std::ostringstream NameStream;
-    NameStream << "__orc_anon" << ID++;
-    auto I = Names.insert(std::make_pair(&Orig, NameStream.str()));
-    return I.first->second;
-  }
-private:
-  DenseMap<const Value*, std::string> Names;
-};
-
-static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) {
-  if (V.hasLocalLinkage()) {
-    if (R.needsRenaming(V))
-      V.setName(R.getRename(V));
-    V.setLinkage(GlobalValue::ExternalLinkage);
-    V.setVisibility(GlobalValue::HiddenVisibility);
+    GV.setUnnamedAddr(GlobalValue::UnnamedAddr::None);
   }
-  V.setUnnamedAddr(GlobalValue::UnnamedAddr::None);
-  assert(!R.needsRenaming(V) && "Invalid global name.");
-}
-
-void makeAllSymbolsExternallyAccessible(Module &M) {
-  GlobalRenamer Renamer;
-
-  for (auto &F : M)
-    raiseVisibilityOnValue(F, Renamer);
-
-  for (auto &GV : M.globals())
-    raiseVisibilityOnValue(GV, Renamer);
-
-  for (auto &A : M.aliases())
-    raiseVisibilityOnValue(A, Renamer);
 }
 
 Function* cloneFunctionDecl(Module &Dst, const Function &F,

Modified: llvm/trunk/lib/ExecutionEngine/Orc/LLJIT.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/LLJIT.cpp?rev=343257&r1=343256&r2=343257&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Orc/LLJIT.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/Orc/LLJIT.cpp Thu Sep 27 12:27:20 2018
@@ -181,7 +181,7 @@ Error LLLazyJIT::addLazyIRModule(JITDyli
   if (auto Err = applyDataLayout(*TSM.getModule()))
     return Err;
 
-  makeAllSymbolsExternallyAccessible(*TSM.getModule());
+  PromoteSymbols(*TSM.getModule());
 
   recordCtorDtors(*TSM.getModule());
 

Modified: llvm/trunk/tools/lli/lli.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/lli.cpp?rev=343257&r1=343256&r2=343257&view=diff
==============================================================================
--- llvm/trunk/tools/lli/lli.cpp (original)
+++ llvm/trunk/tools/lli/lli.cpp Thu Sep 27 12:27:20 2018
@@ -816,7 +816,6 @@ int runOrcLazyJIT(const char *ProgName)
     if (!M)
       reportError(Err, ProgName);
 
-    orc::makeAllSymbolsExternallyAccessible(*M);
     ExitOnErr(J->addLazyIRModule(orc::ThreadSafeModule(std::move(M), TSCtx)));
   }
 




More information about the llvm-commits mailing list