diff --git a/llvm/test/tools/gold/X86/Inputs/thinlto_linkonceresolution.ll b/llvm/test/tools/gold/X86/Inputs/thinlto_linkonceresolution.ll new file mode 100644 index 000000000000..2f7153268aca --- /dev/null +++ b/llvm/test/tools/gold/X86/Inputs/thinlto_linkonceresolution.ll @@ -0,0 +1,4 @@ +target triple = "x86_64-unknown-linux-gnu" +define linkonce_odr hidden void @f() { + ret void +} diff --git a/llvm/test/tools/gold/X86/thinlto_linkonceresolution.ll b/llvm/test/tools/gold/X86/thinlto_linkonceresolution.ll new file mode 100644 index 000000000000..044087b98090 --- /dev/null +++ b/llvm/test/tools/gold/X86/thinlto_linkonceresolution.ll @@ -0,0 +1,32 @@ +; RUN: llvm-as -function-summary %s -o %t.o +; RUN: llvm-as -function-summary %p/Inputs/thinlto_linkonceresolution.ll -o %t2.o + +; Ensure the plugin ensures that for ThinLTO the prevailing copy of a +; linkonce symbol is changed to weak to ensure it is not eliminated. +; Note that gold picks the first copy of f() as the prevailing one, +; so listing %t2.o first is sufficient to ensure that this copy is +; preempted. Also, set the import-instr-limit to 0 to prevent f() from +; being imported from %t2.o which hides the problem. +; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \ +; RUN: --plugin-opt=thinlto \ +; RUN: --plugin-opt=-import-instr-limit=0 \ +; RUN: --plugin-opt=save-temps \ +; RUN: -o %t3.o %t2.o %t.o +; RUN: llvm-nm %t3.o | FileCheck %s +; RUN: llvm-dis %t.o.opt.bc -o - | FileCheck --check-prefix=OPT %s +; RUN: llvm-dis %t2.o.opt.bc -o - | FileCheck --check-prefix=OPT2 %s + +; Ensure that f() is defined in resulting object file, and also +; confirm the weak linkage directly in the saved opt bitcode files. +; CHECK-NOT: U f +; OPT: declare extern_weak hidden void @f() +; OPT2: define weak_odr hidden void @f() + +target triple = "x86_64-unknown-linux-gnu" +define i32 @g() { + call void @f() + ret i32 0 +} +define linkonce_odr hidden void @f() { + ret void +} diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp index 83640bac6c1f..ae5c41a173b5 100644 --- a/llvm/tools/gold/gold-plugin.cpp +++ b/llvm/tools/gold/gold-plugin.cpp @@ -709,6 +709,16 @@ getModuleForFile(LLVMContext &Context, claimed_file &F, const void *View, if (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP && !Res.IsLinkonceOdr) Resolution = LDPR_PREVAILING_DEF; + // In ThinLTO mode change all prevailing resolutions to LDPR_PREVAILING_DEF. + // For ThinLTO the IR files are compiled through the backend independently, + // so we need to ensure that any prevailing linkonce copy will be emitted + // into the object file by making it weak. Additionally, we can skip the + // IRONLY handling for internalization, which isn't performed in ThinLTO + // mode currently anyway. + if (options::thinlto && (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP || + Resolution == LDPR_PREVAILING_DEF_IRONLY)) + Resolution = LDPR_PREVAILING_DEF; + GV->setUnnamedAddr(Res.UnnamedAddr); GV->setVisibility(Res.Visibility);