diff --git a/llvm/lib/Linker/LinkModules.cpp b/llvm/lib/Linker/LinkModules.cpp --- a/llvm/lib/Linker/LinkModules.cpp +++ b/llvm/lib/Linker/LinkModules.cpp @@ -177,9 +177,25 @@ // Go with Dst. LinkFromSrc = false; break; - case Comdat::SelectionKind::NoDeduplicate: - return emitError("Linking COMDATs named '" + ComdatName + - "': nodeduplicate has been violated!"); + case Comdat::SelectionKind::NoDeduplicate: { + const GlobalVariable *DstGV; + const GlobalVariable *SrcGV; + if (getComdatLeader(DstM, ComdatName, DstGV) || + getComdatLeader(*SrcM, ComdatName, SrcGV)) + return true; + + if (SrcGV->isWeakForLinker()) { + // Go with Dst. + LinkFromSrc = false; + } else if (DstGV->isWeakForLinker()) { + // Go with Src. + LinkFromSrc = true; + } else { + return emitError("Linking COMDATs named '" + ComdatName + + "': nodeduplicate has been violated!"); + } + break; + } case Comdat::SelectionKind::ExactMatch: case Comdat::SelectionKind::Largest: case Comdat::SelectionKind::SameSize: { diff --git a/llvm/test/Linker/Inputs/comdat3.ll b/llvm/test/Linker/Inputs/comdat3.ll deleted file mode 100644 --- a/llvm/test/Linker/Inputs/comdat3.ll +++ /dev/null @@ -1,2 +0,0 @@ -$foo = comdat nodeduplicate -@foo = global i64 43, comdat($foo) diff --git a/llvm/test/Linker/comdat4.ll b/llvm/test/Linker/comdat4.ll --- a/llvm/test/Linker/comdat4.ll +++ b/llvm/test/Linker/comdat4.ll @@ -1,5 +1,14 @@ -; RUN: not llvm-link %s %p/Inputs/comdat3.ll -S -o - 2>&1 | FileCheck %s +; RUN: split-file %s %t.dir +; RUN: not llvm-link %t.dir/global.ll %t.dir/global.ll -S -o - 2>&1 | FileCheck %s +; RUN: llvm-link %t.dir/global.ll %t.dir/weak.ll -S -o - 2>&1 +; RUN: llvm-link %t.dir/weak.ll %t.dir/global.ll -S -o - 2>&1 +; RUN: llvm-link %t.dir/weak.ll %t.dir/weak.ll -S -o - 2>&1 +;--- global.ll $foo = comdat nodeduplicate @foo = global i64 43, comdat($foo) ; CHECK: Linking COMDATs named 'foo': nodeduplicate has been violated! + +;--- weak.ll +$foo = comdat nodeduplicate +@foo = weak global i64 43, comdat($foo)