diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -282,10 +282,25 @@ FunctionImporter::ImportMapTy &ImportList, StringMap *ExportLists) { for (auto &VI : Summary.refs()) { - if (DefinedGVSummaries.count(VI.getGUID())) { - LLVM_DEBUG( - dbgs() << "Ref ignored! Target already in destination module.\n"); - continue; + // Find if the module already contains a suitable definition. + const auto &GVS = DefinedGVSummaries.find(VI.getGUID()); + if (GVS != DefinedGVSummaries.end()) { + if (VI.getSummaryList().size() > 1 && + GlobalValue::isInterposableLinkage(GVS->second->linkage())) { + // If the module contains a non-prevailing def of a multiply defined + // global, it still requires a prevailing one to be imported. Otherwise, + // it may result in an undefined symbol because if the prevailing copy + // is read-only it will be internalized thus become not available for + // linking. + // FIXME: Instead of checking for linkage type, use prevailing symbol + // information and ensure the prevailing copy is read-only before allow + // importing. + ; + } else { + LLVM_DEBUG( + dbgs() << "Ref ignored! Target already in destination module.\n"); + continue; + } } LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n"); diff --git a/llvm/test/ThinLTO/X86/Inputs/weak_globals_import.ll b/llvm/test/ThinLTO/X86/Inputs/weak_globals_import.ll new file mode 100644 --- /dev/null +++ b/llvm/test/ThinLTO/X86/Inputs/weak_globals_import.ll @@ -0,0 +1,11 @@ +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@G = dso_local local_unnamed_addr global i32 42, align 4 + +; Function Attrs: noinline +define dso_local i32 @_Z3foov() local_unnamed_addr #0 { + ret i32 0 +} + +attributes #0 = { noinline } diff --git a/llvm/test/ThinLTO/X86/weak_globals_import.ll b/llvm/test/ThinLTO/X86/weak_globals_import.ll new file mode 100644 --- /dev/null +++ b/llvm/test/ThinLTO/X86/weak_globals_import.ll @@ -0,0 +1,30 @@ +; RUN: opt -module-summary %s -o %t1.bc +; RUN: opt -module-summary %p/Inputs/weak_globals_import.ll -o %t2.bc + +; RUN: llvm-lto2 run -save-temps %t1.bc %t2.bc -o %t.out \ +; RUN: -r=%t1.bc,main,plx \ +; RUN: -r=%t1.bc,_Z3foov,l \ +; RUN: -r=%t1.bc,G \ +; RUN: -r=%t2.bc,_Z3foov,pl \ +; RUN: -r=%t2.bc,G,pl +; RUN: llvm-dis %t.out.1.3.import.bc -o - | FileCheck %s +; RUN: llvm-dis %t.out.2.3.import.bc -o - | FileCheck %s + +; Test that a prevailing def for a global variable got imported even if +; a destination module already contains non-prevailing one. + +; CHECK: @G = internal local_unnamed_addr global i32 42 + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@G = weak dso_local local_unnamed_addr global i32 0, align 4 + +define dso_local i32 @main() local_unnamed_addr { + %1 = load i32, i32* @G, align 4 + %2 = tail call i32 @_Z3foov() + %3 = add nsw i32 %2, %1 + ret i32 %3 +} + +declare dso_local i32 @_Z3foov() local_unnamed_addr