Index: llvm/lib/LTO/LTO.cpp =================================================================== --- llvm/lib/LTO/LTO.cpp +++ llvm/lib/LTO/LTO.cpp @@ -455,20 +455,29 @@ 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 (GV->hasAvailableExternallyLinkage()) { + // We can link available_externally symbols even if they are + // non-prevailing. + GlobalValue *CombinedGV = + RegularLTO.CombinedModule->getNamedValue(GV->getName()); + if (!CombinedGV || CombinedGV->isDeclaration()) + Keep.push_back(GV); } } // 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,9 +870,6 @@ if (DGV && !DGV->isDeclarationForLinker()) return false; - if (SGV.hasAvailableExternallyLinkage()) - return true; - if (SGV.isDeclaration() || DoneLinkingBodies) return false; Index: llvm/lib/Linker/LinkModules.cpp =================================================================== --- llvm/lib/Linker/LinkModules.cpp +++ llvm/lib/Linker/LinkModules.cpp @@ -425,7 +425,8 @@ return; // Add these to the internalize list - if (!GV.hasLinkOnceLinkage() && !shouldLinkOnlyNeeded()) + if (!GV.hasLinkOnceLinkage() && !GV.hasAvailableExternallyLinkage() && + !shouldLinkOnlyNeeded()) return; if (shouldInternalizeLinkedSymbols()) Index: llvm/test/LTO/Resolution/X86/Inputs/link-odr-availextern-ae.ll =================================================================== --- /dev/null +++ llvm/test/LTO/Resolution/X86/Inputs/link-odr-availextern-ae.ll @@ -0,0 +1,6 @@ +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define available_externally i32 @f() { + ret i32 2 +} Index: llvm/test/LTO/Resolution/X86/link-odr-availextern.ll =================================================================== --- /dev/null +++ llvm/test/LTO/Resolution/X86/link-odr-availextern.ll @@ -0,0 +1,25 @@ +; Tests for correct behavior for non-prevailing resolutions in cases involving +; *_odr and available_externally linkages. + +; RUN: llvm-as %s -o %t1 +; RUN: llvm-as %S/Inputs/link-odr-availextern-ae.ll -o %t2ae + +; RUN: llvm-lto2 -o %t3 %t1 %t2ae -r %t1,f,p -r %t2ae,f, -save-temps +; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=PREVAILING %s + +; RUN: llvm-lto2 -o %t3 %t2ae %t1 -r %t1,f,p -r %t2ae,f, -save-temps +; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=PREVAILING %s + +; RUN: llvm-lto2 -o %t3 %t2ae -r %t2ae,f, -save-temps +; RUN: llvm-dis < %t3.0.0.preopt.bc -o - | FileCheck --check-prefix=NONPREVAILING %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; PREVAILING: define weak_odr i32 @f() +; PREVAILING-NEXT: ret i32 1 +; NONPREVAILING: define available_externally i32 @f() +; NONPREVAILING-NEXT: ret i32 2 +define linkonce_odr i32 @f() { + ret i32 1 +} Index: llvm/test/Linker/available_externally_a.ll =================================================================== --- llvm/test/Linker/available_externally_a.ll +++ llvm/test/Linker/available_externally_a.ll @@ -1,5 +1,7 @@ ; RUN: llvm-link %s %p/available_externally_b.ll -S -o - | FileCheck %s +; RUN: llvm-link %s -S -o - | FileCheck --check-prefix=AE-ONLY %s @foo = available_externally unnamed_addr constant i32 0 ; CHECK: @foo = hidden unnamed_addr constant i32 0 +; AE-ONLY-NOT: @foo