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,29 @@ 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())) { + // We should not skip import if the module contains an interposable def + // of a multiply defined global. + // First of all, this is required for the correctness purpose: + // if this copy is non-prevailing it's, basically, not a definition + // and will be converted to a declaration. + // On the other side, if the prevailing copy is read-only and suitable + // for import, it should be imported. That copy will be later + // internalized thus not available for linking. + // In this case, if the import is skipped, it will result in undefined + // symbol link error. + // FIXME: Consider to add a check that the suitable prevailing + // definition exists and marked read-only. + ; + } else { + LLVM_DEBUG( + dbgs() << "Ref ignored! Target already in destination module.\n"); + continue; + } } LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n"); @@ -378,6 +397,8 @@ continue; if (DefinedGVSummaries.count(VI.getGUID())) { + // FIXME: Consider not skipping import if the module contains a + // non-prevailing def with interposable linkage. LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n"); continue; } 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,33 @@ +; 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 non-prevailing def with interposable linkage doesn't prevent +; importing a suitable definition from a prevailing module. + +; Note that the tested global is read-only thus the source and +; imported copy got internalized. + +; 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