[PATCH] D108689: [Linker] Support weak symbols in nodeduplicate COMDAT group

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 27 10:50:32 PDT 2021


phosek updated this revision to Diff 369141.
phosek edited the summary of this revision.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108689/new/

https://reviews.llvm.org/D108689

Files:
  llvm/lib/Linker/LinkModules.cpp
  llvm/test/Linker/Inputs/comdat3.ll
  llvm/test/Linker/comdat4.ll


Index: llvm/test/Linker/comdat4.ll
===================================================================
--- llvm/test/Linker/comdat4.ll
+++ 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
+ at foo = weak global i64 43, comdat($foo)
Index: llvm/test/Linker/Inputs/comdat3.ll
===================================================================
--- llvm/test/Linker/Inputs/comdat3.ll
+++ /dev/null
@@ -1,2 +0,0 @@
-$foo = comdat nodeduplicate
- at foo = global i64 43, comdat($foo)
Index: llvm/lib/Linker/LinkModules.cpp
===================================================================
--- llvm/lib/Linker/LinkModules.cpp
+++ 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: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108689.369141.patch
Type: text/x-patch
Size: 2191 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210827/21957e49/attachment.bin>


More information about the llvm-commits mailing list