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

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 25 01:15:43 PDT 2021


phosek created this revision.
phosek added reviewers: rnk, MaskRay.
Herald added a subscriber: hiraditya.
phosek requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When a nodeduplicate COMDAT group contains a weak symbol, choose
a non-weak symbol (or one of the weak ones) rather than reporting
an error. This addresses issue PR51394.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108689

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


Index: llvm/test/Linker/comdat4.ll
===================================================================
--- llvm/test/Linker/comdat4.ll
+++ llvm/test/Linker/comdat4.ll
@@ -1,4 +1,7 @@
 ; RUN: not llvm-link %s %p/Inputs/comdat3.ll -S -o - 2>&1 | FileCheck %s
+; RUN: llvm-link %s %p/Inputs/comdat6.ll -S -o - 2>&1
+; RUN: llvm-link %p/Inputs/comdat6.ll %s -S -o - 2>&1
+; RUN: llvm-link %p/Inputs/comdat6.ll %p/Inputs/comdat6.ll -S -o - 2>&1
 
 $foo = comdat nodeduplicate
 @foo = global i64 43, comdat($foo)
Index: llvm/test/Linker/Inputs/comdat6.ll
===================================================================
--- /dev/null
+++ llvm/test/Linker/Inputs/comdat6.ll
@@ -0,0 +1,2 @@
+$foo = comdat nodeduplicate
+ at foo = weak 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.368573.patch
Type: text/x-patch
Size: 1904 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210825/38b1faa4/attachment.bin>


More information about the llvm-commits mailing list