Skip to content

Commit 07dc190

Browse files
committed
Linker: Avoid scheduling the link of a global value twice due to an alias
3d4f3a0 (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. Differential Revision: https://reviews.llvm.org/D101419 Radar-Id: rdar://75752728
1 parent d217d8b commit 07dc190

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

llvm/lib/Linker/IRMover.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,16 @@ Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {
603603
return New;
604604
}
605605

606-
// When linking a global for an indirect symbol, it will always be linked.
607-
// However we need to check if it was not already scheduled to satisfy a
608-
// reference from a regular global value initializer. We know if it has been
609-
// schedule if the "New" GlobalValue that is mapped here for the indirect
610-
// symbol is the same as the one already mapped. If there is an entry in the
606+
// If the global is being linked for an indirect symbol, it may have already
607+
// been scheduled to satisfy a regular symbol. Similarly, a global being linked
608+
// for a regular symbol may have already been scheduled for an indirect
609+
// symbol. Check for these cases by looking in the other value map and
610+
// confirming the same value has been scheduled. If there is an entry in the
611611
// ValueMap but the value is different, it means that the value already had a
612612
// definition in the destination module (linkonce for instance), but we need a
613-
// new definition for the indirect symbol ("New" will be different.
614-
if (ForIndirectSymbol && ValueMap.lookup(SGV) == New)
613+
// new definition for the indirect symbol ("New" will be different).
614+
if ((ForIndirectSymbol && ValueMap.lookup(SGV) == New) ||
615+
(!ForIndirectSymbol && IndirectSymbolValueMap.lookup(SGV) == New))
615616
return New;
616617

617618
if (ForIndirectSymbol || shouldLink(New, *SGV))

llvm/test/Linker/alias-4.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; RUN: llvm-link %s -S -o - | FileCheck %s
2+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
3+
target triple = "x86_64-apple-macosx10.9"
4+
5+
; CHECK-DAG: @gv0 = constant i64* @alias
6+
; CHECK-DAG: @gv1 = constant i64 ptrtoint (i64* @gv1 to i64)
7+
; CHECK-DAG: @alias = alias i64, i64* @gv1
8+
9+
@gv0 = constant i64* @alias
10+
@gv1 = constant i64 ptrtoint (i64* @gv1 to i64)
11+
12+
@alias = alias i64, i64* @gv1

0 commit comments

Comments
 (0)