diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp --- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp +++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp @@ -269,16 +269,26 @@ } // Convert the PreservedSymbols map from "Name" based to "GUID" based. +static void computeGUIDPreservedSymbols(const lto::InputFile &File, + const StringSet<> &PreservedSymbols, + const Triple &TheTriple, + DenseSet &GUIDs) { + // Iterate the symbols in the input file and if the input has preserved symbol + // compute the GUID for the symbol. + for (const auto &Sym : File.symbols()) { + if (PreservedSymbols.count(Sym.getName())) + GUIDs.insert(GlobalValue::getGUID(GlobalValue::getGlobalIdentifier( + Sym.getIRName(), GlobalValue::ExternalLinkage, ""))); + } +} + static DenseSet -computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols, +computeGUIDPreservedSymbols(const lto::InputFile &File, + const StringSet<> &PreservedSymbols, const Triple &TheTriple) { DenseSet GUIDPreservedSymbols(PreservedSymbols.size()); - for (auto &Entry : PreservedSymbols) { - StringRef Name = Entry.first(); - if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_') - Name = Name.drop_front(); - GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name)); - } + computeGUIDPreservedSymbols(File, PreservedSymbols, TheTriple, + GUIDPreservedSymbols); return GUIDPreservedSymbols; } @@ -652,7 +662,7 @@ // Convert the preserved symbols set from string to GUID auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( - PreservedSymbols, Triple(TheModule.getTargetTriple())); + File, PreservedSymbols, Triple(TheModule.getTargetTriple())); // Add used symbol to the preserved symbols. addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); @@ -702,7 +712,7 @@ // Convert the preserved symbols set from string to GUID auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( - PreservedSymbols, Triple(TheModule.getTargetTriple())); + File, PreservedSymbols, Triple(TheModule.getTargetTriple())); addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); @@ -737,7 +747,7 @@ // Convert the preserved symbols set from string to GUID auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( - PreservedSymbols, Triple(TheModule.getTargetTriple())); + File, PreservedSymbols, Triple(TheModule.getTargetTriple())); addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); @@ -770,7 +780,7 @@ // Convert the preserved symbols set from string to GUID auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( - PreservedSymbols, Triple(TheModule.getTargetTriple())); + File, PreservedSymbols, Triple(TheModule.getTargetTriple())); addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); @@ -808,7 +818,7 @@ // Convert the preserved symbols set from string to GUID auto GUIDPreservedSymbols = - computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); + computeGUIDPreservedSymbols(File, PreservedSymbols, TMBuilder.TheTriple); addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); @@ -972,8 +982,10 @@ // Convert the preserved symbols set from string to GUID, this is needed for // computing the caching hash and the internalization. - auto GUIDPreservedSymbols = - computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); + DenseSet GUIDPreservedSymbols; + for (const auto &M : Modules) + computeGUIDPreservedSymbols(*M, PreservedSymbols, TMBuilder.TheTriple, + GUIDPreservedSymbols); // Add used symbol from inputs to the preserved symbols. for (const auto &M : Modules) diff --git a/llvm/test/ThinLTO/X86/internalize.ll b/llvm/test/ThinLTO/X86/internalize.ll --- a/llvm/test/ThinLTO/X86/internalize.ll +++ b/llvm/test/ThinLTO/X86/internalize.ll @@ -4,13 +4,13 @@ ; prevailing the %t1.bc copy as non-prevailing. ; RUN: llvm-lto -thinlto-action=thinlink -o %t.index.bc %t2.bc %t1.bc ; RUN: llvm-lto -thinlto-action=internalize -thinlto-index %t.index.bc %t1.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=REGULAR -; RUN: llvm-lto -thinlto-action=internalize -thinlto-index %t.index.bc %t1.bc -o - --exported-symbol=foo | llvm-dis -o - | FileCheck %s --check-prefix=INTERNALIZE +; RUN: llvm-lto -thinlto-action=internalize -thinlto-index %t.index.bc %t1.bc -o - --exported-symbol=_foo | llvm-dis -o - | FileCheck %s --check-prefix=INTERNALIZE ; Test the enable-lto-internalization option by setting it to false. ; This makes sure indices are not marked as internallinkage and therefore ; internalization does not happen. ; RUN: llvm-lto -thinlto-action=internalize -thinlto-index %t.index.bc %t1.bc \ -; RUN: -enable-lto-internalization=false --exported-symbol=foo +; RUN: -enable-lto-internalization=false --exported-symbol=_foo ; RUN: llvm-dis < %t1.bc.thinlto.internalized.bc | FileCheck %s --check-prefix=INTERNALIZE-OPTION-DISABLE ; RUN: llvm-lto2 run %t1.bc -o %t.o -save-temps \ diff --git a/llvm/test/ThinLTO/X86/mangled_symbol.ll b/llvm/test/ThinLTO/X86/mangled_symbol.ll new file mode 100644 --- /dev/null +++ b/llvm/test/ThinLTO/X86/mangled_symbol.ll @@ -0,0 +1,26 @@ +; RUN: opt -module-summary %s -o %t1.bc +; RUN: llvm-lto -thinlto-action=thinlink -o %t.index.bc %t1.bc +;; Check baseline when both of them internalized when not exported. +; RUN: llvm-lto -thinlto-action=internalize -thinlto-index %t.index.bc %t1.bc -o - --exported-symbol=_exported | llvm-dis -o - | FileCheck %s --check-prefix=INTERNALIZED +;; Check symbols are exported, including the ones with `\01` prefix. +; RUN: llvm-lto -thinlto-action=internalize -thinlto-index %t.index.bc %t1.bc -o - --exported-symbol=_exported --exported-symbol=_extern_not_mangled --exported-symbol=_extern_mangled | llvm-dis -o - | FileCheck %s --check-prefix=EXPORTED + +; INTERNALIZED: define internal void @extern_not_mangled +; INTERNALIZED: define internal void @"\01_extern_mangled" +; EXPORTED: define void @extern_not_mangled +; EXPORTED: define void @"\01_extern_mangled" + +target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.11.0" + +define void @exported() { + ret void +} + +define void @extern_not_mangled() { + ret void +} + +define void @"\01_extern_mangled"() { + ret void +} diff --git a/llvm/test/ThinLTO/X86/weak_resolution.ll b/llvm/test/ThinLTO/X86/weak_resolution.ll --- a/llvm/test/ThinLTO/X86/weak_resolution.ll +++ b/llvm/test/ThinLTO/X86/weak_resolution.ll @@ -7,10 +7,10 @@ ; non-prevailing ODR are not kept when possible, but non-ODR non-prevailing ; are not affected. ; RUN: llvm-lto -thinlto-action=promote %t.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=MOD1 -; RUN: llvm-lto -thinlto-action=internalize %t.bc -thinlto-index=%t3.bc -exported-symbol=linkoncefunc -o - | llvm-dis -o - | FileCheck %s --check-prefix=MOD1-INT +; RUN: llvm-lto -thinlto-action=internalize %t.bc -thinlto-index=%t3.bc -exported-symbol=_linkoncefunc -o - | llvm-dis -o - | FileCheck %s --check-prefix=MOD1-INT ; RUN: llvm-lto -thinlto-action=promote %t2.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=MOD2 ; When exported, we always preserve a linkonce -; RUN: llvm-lto -thinlto-action=promote %t.bc -thinlto-index=%t3.bc -o - --exported-symbol=linkonceodrfuncInSingleModule | llvm-dis -o - | FileCheck %s --check-prefix=EXPORTED +; RUN: llvm-lto -thinlto-action=promote %t.bc -thinlto-index=%t3.bc -o - --exported-symbol=_linkonceodrfuncInSingleModule | llvm-dis -o - | FileCheck %s --check-prefix=EXPORTED target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0" diff --git a/llvm/test/ThinLTO/X86/weak_resolution_single.ll b/llvm/test/ThinLTO/X86/weak_resolution_single.ll --- a/llvm/test/ThinLTO/X86/weak_resolution_single.ll +++ b/llvm/test/ThinLTO/X86/weak_resolution_single.ll @@ -1,7 +1,7 @@ ; RUN: opt -module-summary %s -o %t.bc ; RUN: llvm-lto -thinlto-action=thinlink -o %t2.bc %t.bc -; RUN: llvm-lto -thinlto-action=internalize %t.bc -thinlto-index=%t2.bc -exported-symbol=foo -o - | llvm-dis -o - | FileCheck %s +; RUN: llvm-lto -thinlto-action=internalize %t.bc -thinlto-index=%t2.bc -exported-symbol=_foo -o - | llvm-dis -o - | FileCheck %s ; CHECK: define weak_odr void @foo() target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"