Index: llvm/include/llvm/IR/Module.h =================================================================== --- llvm/include/llvm/IR/Module.h +++ llvm/include/llvm/IR/Module.h @@ -895,6 +895,9 @@ SmallVectorImpl &Vec, bool CompilerUsed); +/// Generate a unique name based on BaseName and UniqueSuffix. +std::string getUniqueName(StringRef BaseName, StringRef UniqueSuffix); + /// An raw_ostream inserter for modules. inline raw_ostream &operator<<(raw_ostream &O, const Module &M) { M.print(O, nullptr); Index: llvm/include/llvm/IR/ModuleSummaryIndex.h =================================================================== --- llvm/include/llvm/IR/ModuleSummaryIndex.h +++ llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -1398,11 +1398,9 @@ /// Convenience method for creating a promoted global name /// for the given value name of a local, and its original module's ID. static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) { - SmallString<256> NewName(Name); - NewName += ".llvm."; - NewName += utostr((uint64_t(ModHash[0]) << 32) | - ModHash[1]); // Take the first 64 bits - return std::string(NewName.str()); + return getUniqueName(Name, + utostr((uint64_t(ModHash[0]) << 32) + | ModHash[1])); // Take the first 64 bits } /// Helper to obtain the unpromoted name for a global value (or the original Index: llvm/lib/IR/Module.cpp =================================================================== --- llvm/lib/IR/Module.cpp +++ llvm/lib/IR/Module.cpp @@ -673,6 +673,16 @@ return GV; } +std::string llvm::getUniqueName(StringRef BaseName, StringRef UniqueSuffix) { + // If the suffix comes from getUniqueModuleId() it may have a $ at the front. + // Remove that as it doesn't demangle nicely. + UniqueSuffix.consume_front("$"); + + // Use a period to separate the suffix, since by convention Itanium demanglers + // will ignore it which means the symbol name will still demangle nicely. + return (BaseName + ".llvm." + UniqueSuffix).str(); +} + void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) { if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) { std::unique_ptr ProfileSummary( Index: llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp =================================================================== --- llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp +++ llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp @@ -55,7 +55,7 @@ } } - std::string NewName = (Name + ModuleId).str(); + std::string NewName = getUniqueName(Name, ModuleId); if (const auto *C = ExportGV.getComdat()) if (C->getName() == Name) Index: llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp =================================================================== --- llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp +++ llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp @@ -1153,7 +1153,7 @@ // to make it visible to thin LTO objects. We can only get here during the // ThinLTO export phase. if (TheFn->hasLocalLinkage()) { - std::string NewName = (TheFn->getName() + "$merged").str(); + std::string NewName = getUniqueName(TheFn->getName(), "merged"); // Since we are renaming the function, any comdats with the same name must // also be renamed. This is required when targeting COFF, as the comdat name Index: llvm/test/ThinLTO/X86/devirt2.ll =================================================================== --- llvm/test/ThinLTO/X86/devirt2.ll +++ llvm/test/ThinLTO/X86/devirt2.ll @@ -190,12 +190,12 @@ ; RUN: llvm-nm %t5.1 | FileCheck %s --check-prefix=NM-HYBRID1 ; RUN: llvm-nm %t5.2 | FileCheck %s --check-prefix=NM-HYBRID2 -; NM-HYBRID1-DAG: U _ZN1A1nEi$ -; NM-HYBRID1-DAG: U _ZN1E1mEi$ +; NM-HYBRID1-DAG: U _ZN1A1nEi.llvm.{{[0-9a-f]*}} +; NM-HYBRID1-DAG: U _ZN1E1mEi.llvm.{{[0-9a-f]*}} ; NM-HYBRID1-DAG: U _ZN1D1mEi -; NM-HYBRID2-DAG: T _ZN1A1nEi$ -; NM-HYBRID2-DAG: T _ZN1E1mEi$ +; NM-HYBRID2-DAG: T _ZN1A1nEi.llvm.{{[0-9a-f]*}} +; NM-HYBRID2-DAG: T _ZN1E1mEi.llvm.{{[0-9a-f]*}} ; NM-HYBRID2-DAG: W _ZN1D1mEi ; NM-HYBRID2-DAG: T _ZN1B1fEi ; NM-HYBRID2-DAG: T _ZN1C1fEi Index: llvm/test/Transforms/ThinLTOBitcodeWriter/split-internal1.ll =================================================================== --- llvm/test/Transforms/ThinLTOBitcodeWriter/split-internal1.ll +++ llvm/test/Transforms/ThinLTOBitcodeWriter/split-internal1.ll @@ -12,14 +12,14 @@ ; BCA0: