diff --git a/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h b/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h index 0c048e4c251b..6c77bb5ad3f4 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h @@ -167,25 +167,6 @@ private: return llvm::make_unique(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 SourceMod; @@ -239,7 +220,7 @@ private: VModuleKey K; std::shared_ptr BackingResolver; std::unique_ptr StubsMgr; - StaticGlobalRenamer StaticRenamer; + SymbolLinkagePromoter PromoteSymbols; SourceModulesList SourceModules; std::vector BaseLayerVModuleKeys; }; @@ -361,14 +342,9 @@ public: private: Error addLogicalModule(LogicalDylib &LD, std::unique_ptr 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; diff --git a/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h index 8842dd1a5be7..77c15658f308 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h @@ -413,12 +413,16 @@ GlobalVariable *createImplPointer(PointerType &PT, Module &M, const Twine &Name, /// 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. /// diff --git a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h index 61c5cd29de73..ca66e0c2343f 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h @@ -173,6 +173,7 @@ private: IRTransformLayer2 TransformLayer; CompileOnDemandLayer2 CODLayer; + SymbolLinkagePromoter PromoteSymbols; }; } // End namespace orc diff --git a/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp index 54baed237b21..c1ece816b754 100644 --- a/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp @@ -246,57 +246,23 @@ void makeStub(Function &F, Value &ImplPointer) { 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"); - } + // 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++)); - 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; + 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; + GV.setUnnamedAddr(GlobalValue::UnnamedAddr::None); } -private: - DenseMap 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); - } - 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, diff --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp index fef190ba575f..91fa4ec2077e 100644 --- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp +++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp @@ -181,7 +181,7 @@ Error LLLazyJIT::addLazyIRModule(JITDylib &JD, ThreadSafeModule TSM) { if (auto Err = applyDataLayout(*TSM.getModule())) return Err; - makeAllSymbolsExternallyAccessible(*TSM.getModule()); + PromoteSymbols(*TSM.getModule()); recordCtorDtors(*TSM.getModule()); diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp index 4977cccabda8..c9fc11b36b44 100644 --- a/llvm/tools/lli/lli.cpp +++ b/llvm/tools/lli/lli.cpp @@ -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))); }