Index: llvm/lib/LTO/LTO.cpp =================================================================== --- llvm/lib/LTO/LTO.cpp +++ llvm/lib/LTO/LTO.cpp @@ -446,6 +446,11 @@ if (GV.hasAppendingLinkage()) Keep.push_back(&GV); + DenseSet AliasedGlobals; + for (auto &GA : M.aliases()) + if (GlobalObject *GO = GA.getBaseObject()) + AliasedGlobals.insert(GO); + for (const InputFile::Symbol &Sym : make_range(InputFile::symbol_iterator(SymTab.symbols().begin(), SymTab, nullptr), @@ -455,20 +460,33 @@ SymbolResolution Res = *ResI++; addSymbolToGlobalRes(Used, Sym, Res, 0); - if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) - continue; - if (Res.Prevailing && Sym.isGV()) { + if (Sym.isGV()) { GlobalValue *GV = Sym.getGV(); - Keep.push_back(GV); - switch (GV->getLinkage()) { - default: - break; - case GlobalValue::LinkOnceAnyLinkage: - GV->setLinkage(GlobalValue::WeakAnyLinkage); - break; - case GlobalValue::LinkOnceODRLinkage: - GV->setLinkage(GlobalValue::WeakODRLinkage); - break; + if (Res.Prevailing) { + if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) + continue; + Keep.push_back(GV); + switch (GV->getLinkage()) { + default: + break; + case GlobalValue::LinkOnceAnyLinkage: + GV->setLinkage(GlobalValue::WeakAnyLinkage); + break; + case GlobalValue::LinkOnceODRLinkage: + GV->setLinkage(GlobalValue::WeakODRLinkage); + break; + } + } else if (isa(GV) && + (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage()) && + !AliasedGlobals.count(cast(GV))) { + // Either of the above two types of linkage indicates that the chosen + // prevailing symbol will have the same semantics as this copy of the + // symbol, so we can set its linkage to available_externally. This + // causes the IR mover to link the symbol if it does not already have a + // definition in the combined module. + // FIXME: Consider moving to IRMover? + GV->setLinkage(GlobalValue::AvailableExternallyLinkage); + cast(GV)->setComdat(nullptr); } } // Common resolution: collect the maximum size/alignment over all commons. Index: llvm/lib/Linker/IRMover.cpp =================================================================== --- llvm/lib/Linker/IRMover.cpp +++ llvm/lib/Linker/IRMover.cpp @@ -870,7 +870,7 @@ if (DGV && !DGV->isDeclarationForLinker()) return false; - if (SGV.hasAvailableExternallyLinkage()) + if (SGV.hasAvailableExternallyLinkage() && (!DGV || DGV->isDeclaration())) return true; if (SGV.isDeclaration() || DoneLinkingBodies) Index: llvm/test/LTO/Resolution/X86/Inputs/link-availextern.ll =================================================================== --- /dev/null +++ llvm/test/LTO/Resolution/X86/Inputs/link-availextern.ll @@ -0,0 +1,11 @@ +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define linkonce_odr i32 @f() { + ret i32 2 +} + +define void @f3() { + %x = call i32 @f() + ret void +} Index: llvm/test/LTO/Resolution/X86/link-availextern.ll =================================================================== --- /dev/null +++ llvm/test/LTO/Resolution/X86/link-availextern.ll @@ -0,0 +1,30 @@ +; RUN: llvm-as %s -o %t1 +; RUN: llvm-as %S/Inputs/link-availextern.ll -o %t2 + +; RUN: llvm-lto2 -o %t3 %t1 -r %t1,f, -r %t1,f2,p -save-temps +; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=NONPREVAILING %s + +; RUN: llvm-lto2 -o %t3 %t1 %t2 -r %t1,f, -r %t1,f2,p -r %t2,f, -r %t2,f3,p -save-temps +; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=NONPREVAILING %s + +; RUN: llvm-lto2 -o %t3 %t1 %t2 -r %t1,f, -r %t1,f2,p -r %t2,f,p -r %t2,f3,p -save-temps +; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=PREVAILING %s + +; RUN: llvm-lto2 -o %t3 %t2 %t1 -r %t1,f, -r %t1,f2,p -r %t2,f,p -r %t2,f3,p -save-temps +; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=PREVAILING %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; NONPREVAILING: define available_externally i32 @f() +; NONPREVAILING-NEXT: ret i32 1 +; PREVAILING: define weak_odr i32 @f() +; PREVAILING-NEXT: ret i32 2 +define linkonce_odr i32 @f() { + ret i32 1 +} + +define void @f2() { + %x = call i32 @f() + ret void +}