[PATCH] D95126: [llvm-link] Fix for an assertion when linking global with appending linkage

Sergey Dmitriev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 21 04:00:27 PST 2021


sdmitriev created this revision.
sdmitriev added reviewers: tra, tejohnson, jdoerfert.
Herald added a subscriber: hiraditya.
sdmitriev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch fixes llvm-link assertion when linking external variable
declaration with a definition with appending linkage.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95126

Files:
  llvm/lib/Linker/IRMover.cpp
  llvm/test/Linker/Inputs/appending-global.ll
  llvm/test/Linker/appending-global-proto.ll


Index: llvm/test/Linker/appending-global-proto.ll
===================================================================
--- /dev/null
+++ llvm/test/Linker/appending-global-proto.ll
@@ -0,0 +1,11 @@
+; RUN: llvm-link %s %p/Inputs/appending-global.ll -S -o - | FileCheck %s
+; RUN: llvm-link %p/Inputs/appending-global.ll %s -S -o - | FileCheck %s
+
+; Checks that we can link global variable with appending linkage with the
+; existing external declaration.
+
+; CHECK-DAG: @var = appending global [1 x i8*] undef
+; CHECK-DAG: @use = global [1 x i8*] [i8* bitcast ([1 x i8*]* @var to i8*)]
+
+ at var = external global i8*
+ at use = global [1 x i8*] [i8* bitcast (i8** @var to i8*)]
Index: llvm/test/Linker/Inputs/appending-global.ll
===================================================================
--- /dev/null
+++ llvm/test/Linker/Inputs/appending-global.ll
@@ -0,0 +1 @@
+ at var = appending global [1 x i8* ] undef
Index: llvm/lib/Linker/IRMover.cpp
===================================================================
--- llvm/lib/Linker/IRMover.cpp
+++ llvm/lib/Linker/IRMover.cpp
@@ -868,7 +868,7 @@
   }
 
   uint64_t DstNumElements = 0;
-  if (DstGV) {
+  if (DstGV && !DstGV->isDeclaration()) {
     ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());
     DstNumElements = DstTy->getNumElements();
 
@@ -928,9 +928,9 @@
 
   Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
 
-  Mapper.scheduleMapAppendingVariable(*NG,
-                                      DstGV ? DstGV->getInitializer() : nullptr,
-                                      IsOldStructor, SrcElements);
+  Mapper.scheduleMapAppendingVariable(
+      *NG, DstGV && !DstGV->isDeclaration() ? DstGV->getInitializer() : nullptr,
+      IsOldStructor, SrcElements);
 
   // Replace any uses of the two global variables with uses of the new
   // global.
@@ -983,7 +983,6 @@
     DGV = nullptr;
 
   // Handle the ultra special appending linkage case first.
-  assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
   if (SGV->hasAppendingLinkage())
     return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
                                  cast<GlobalVariable>(SGV));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95126.318146.patch
Type: text/x-patch
Size: 2201 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210121/655758d9/attachment.bin>


More information about the llvm-commits mailing list