[PATCH] D101419: Linker: Avoid scheduling the link of a global value twice due to an alias

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 27 18:14:00 PDT 2021


dexonsmith created this revision.
dexonsmith added reviewers: mehdi_amini, steven_wu.
Herald added a subscriber: hiraditya.
dexonsmith requested review of this revision.
Herald added a project: LLVM.

3d4f3a0da90bd1a3 (https://reviews.llvm.org/D20586) avoided rescheduling
a global value that was materialized first through a regular value, and
then again through an alias. This commit catches the dual, avoiding
rescheduling when the global value is first materialized through an
alias.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101419

Files:
  llvm/lib/Linker/IRMover.cpp
  llvm/test/Linker/alias-4.ll


Index: llvm/test/Linker/alias-4.ll
===================================================================
--- /dev/null
+++ llvm/test/Linker/alias-4.ll
@@ -0,0 +1,12 @@
+; RUN: llvm-link %s -S -o - | FileCheck %s
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.9"
+
+; CHECK-DAG: @gv0 = constant i64* @alias
+; CHECK-DAG: @gv1 = constant i64 ptrtoint (i64* @gv1 to i64)
+; CHECK-DAG: @alias = alias i64, i64* @gv1
+
+ at gv0 = constant i64* @alias
+ at gv1 = constant i64 ptrtoint (i64* @gv1 to i64)
+
+ at alias = alias i64, i64* @gv1
Index: llvm/lib/Linker/IRMover.cpp
===================================================================
--- llvm/lib/Linker/IRMover.cpp
+++ llvm/lib/Linker/IRMover.cpp
@@ -610,15 +610,16 @@
       return New;
   }
 
-  // When linking a global for an indirect symbol, it will always be linked.
-  // However we need to check if it was not already scheduled to satisfy a
-  // reference from a regular global value initializer. We know if it has been
-  // schedule if the "New" GlobalValue that is mapped here for the indirect
-  // symbol is the same as the one already mapped. If there is an entry in the
+  // If the global is being linked for an indirect symbol, it may have already
+  // been scheduled to satisfy a regular symbol. Similarly, a global being linked
+  // for a regular symbol may have already been scheduled for an indirect
+  // symbol. Check for these cases by looking in the other value map and
+  // confirming the same value has been scheduled.  If there is an entry in the
   // ValueMap but the value is different, it means that the value already had a
   // definition in the destination module (linkonce for instance), but we need a
-  // new definition for the indirect symbol ("New" will be different.
-  if (ForIndirectSymbol && ValueMap.lookup(SGV) == New)
+  // new definition for the indirect symbol ("New" will be different).
+  if ((ForIndirectSymbol && ValueMap.lookup(SGV) == New) ||
+      (!ForIndirectSymbol && IndirectSymbolValueMap.lookup(SGV) == New))
     return New;
 
   if (ForIndirectSymbol || shouldLink(New, *SGV))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101419.341046.patch
Type: text/x-patch
Size: 2144 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210428/b098bcd5/attachment.bin>


More information about the llvm-commits mailing list