Skip to content

Commit 7117dea

Browse files
authored
AsmPrinter: Remove ELF's special lowerRelativeReference for unnamed_addr function; use lowerDSOLocalEquivalent in more cases
https://reviews.llvm.org/D17938 introduced lowerRelativeReference to give ConstantExpr sub (A-B) special semantics in ELF: when `A` is an `unnamed_addr` function, create a PLT-generating relocation. This was intended for C++ relative vtables, but C++ relative vtable ended up using DSOLocalEquivalent (lowerDSOLocalEquivalent). This special treatment of `unnamed_addr` seems unusual. Let's remove it. Only COFF needs an overload to generate a @IMGREL32 relocation specifier (llvm/test/MC/COFF/cross-section-relative.ll). Pull Request: #134781
1 parent 6d2b767 commit 7117dea

File tree

10 files changed

+41
-42
lines changed

10 files changed

+41
-42
lines changed

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
125125
lowerSymbolDifference(const MCSymbol *LHS, const MCSymbol *RHS,
126126
int64_t Addend,
127127
std::optional<int64_t> PCRelativeOffset) const;
128-
const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
129-
const GlobalValue *RHS, int64_t Addend,
130-
std::optional<int64_t> PCRelativeOffset,
131-
const TargetMachine &TM) const override;
132128

133129
const MCExpr *lowerDSOLocalEquivalent(const MCSymbol *LHS,
134130
const MCSymbol *RHS, int64_t Addend,

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3496,10 +3496,12 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV,
34963496
LHSGV, RHSGV, Addend, PCRelativeOffset, TM);
34973497

34983498
// (ELF-specific) If the generic symbol difference does not apply, and
3499-
// LHS is a dso_local_equivalent of a dso_preemptable function,
3500-
// reference the PLT entry instead.
3501-
if (DSOEquiv && TM.getTargetTriple().isOSBinFormatELF() &&
3502-
!(LHSGV->isDSOLocal() || LHSGV->isImplicitDSOLocal()))
3499+
// LHS is a dso_local_equivalent of a function, reference the PLT entry
3500+
// instead. Note: A default visibility symbol is by default preemptible
3501+
// during linking, and should not be referenced with PC-relative
3502+
// relocations. Therefore, use a PLT relocation even if the function is
3503+
// dso_local.
3504+
if (DSOEquiv && TM.getTargetTriple().isOSBinFormatELF())
35033505
Res = getObjFileLowering().lowerDSOLocalEquivalent(
35043506
LHSSym, RHSSym, Addend, PCRelativeOffset, TM);
35053507

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,24 +1233,6 @@ const MCExpr *TargetLoweringObjectFileELF::lowerSymbolDifference(
12331233
return Res;
12341234
}
12351235

1236-
const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
1237-
const GlobalValue *LHS, const GlobalValue *RHS, int64_t Addend,
1238-
std::optional<int64_t> PCRelativeOffset, const TargetMachine &TM) const {
1239-
// We may only use a PLT-relative relocation to refer to unnamed_addr
1240-
// functions.
1241-
if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
1242-
return nullptr;
1243-
1244-
// Basic correctness checks.
1245-
if (LHS->getType()->getPointerAddressSpace() != 0 ||
1246-
RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
1247-
RHS->isThreadLocal())
1248-
return nullptr;
1249-
1250-
return lowerSymbolDifference(TM.getSymbol(LHS), TM.getSymbol(RHS), Addend,
1251-
PCRelativeOffset);
1252-
}
1253-
12541236
// Reference the PLT entry of a function, optionally with a subtrahend (`RHS`).
12551237
const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent(
12561238
const MCSymbol *LHS, const MCSymbol *RHS, int64_t Addend,

llvm/test/CodeGen/ARM/plt-relative-reloc.ll renamed to llvm/test/CodeGen/ARM/relative-reloc.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ declare void @fn1() unnamed_addr
1010
declare void @fn2() unnamed_addr
1111
declare void @fn3()
1212

13+
;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
1314
; CHECK: .long 0
14-
; CHECK-NEXT: .long fn1(prel31)-vtable-4
15-
; CHECK-NEXT: .long fn2(prel31)-vtable-4
15+
; CHECK-NEXT: .long fn1-vtable-4
16+
; CHECK-NEXT: .long fn2-vtable-4
1617
; CHECK-NEXT: .long fn3-vtable-4

llvm/test/CodeGen/RISCV/dso_local_equivalent.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ declare void @extern_func()
2626
; CHECK-NEXT: .word 0 # 0x0
2727
; CHECK-NEXT: .word %pltpcrel(f0)
2828
; CHECK-NEXT: .word %pltpcrel(f1+4)
29-
; CHECK-NEXT: .word f2-_ZTV1B-8
29+
; CHECK-NEXT: .word %pltpcrel(f2+8)
3030
; CHECK-NEXT: .word %pltpcrel(f3+12)
31-
; CHECK-NEXT: .word f4-_ZTV1B-8
31+
; CHECK-NEXT: .word %pltpcrel(f4+16)
3232
; CHECK-NEXT: .size _ZTV1B, 28
3333
declare void @f0()
3434
declare void @f1()

llvm/test/CodeGen/RISCV/plt-relative-reloc.ll renamed to llvm/test/CodeGen/RISCV/relative-reloc.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ declare void @fn2() unnamed_addr
1212
declare void @fn3()
1313
@global4 = external unnamed_addr global i8
1414

15+
;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
1516
; CHECK: vtable:
1617
; CHECK-NEXT: .word 0 # 0x0
17-
; CHECK-NEXT: .word %pltpcrel(fn1)
18-
; CHECK-NEXT: .word %pltpcrel(fn2+4)
18+
; CHECK-NEXT: .word fn1-vtable-4
19+
; CHECK-NEXT: .word fn2-vtable-4
1920
; CHECK-NEXT: .word fn3-vtable-4
2021
; CHECK-NEXT: .word global4-vtable-4
2122
; CHECK-NEXT: .size vtable, 20

llvm/test/CodeGen/X86/dso_local_equivalent.ll

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,27 @@ define void @call_no_name_extern() {
207207

208208
declare hidden void @0()
209209
declare void @1()
210+
declare void @forward_extern_func()
210211

211212
;; Note that we keep this at the very end because llc emits this after all the
212213
;; functions.
213-
; CHECK: const:
214-
; CHECK: .long forward_extern_func@PLT
215-
@const = constant i32 trunc (i64 ptrtoint (ptr dso_local_equivalent @forward_extern_func to i64) to i32)
216-
217-
declare void @forward_extern_func()
214+
; CHECK-LABEL: _ZTV1B:
215+
; CHECK: .long 0
216+
; CHECK-NEXT: .long 0
217+
; CHECK-NEXT: .long f0@PLT-_ZTV1B-8
218+
; CHECK-NEXT: .long f1@PLT-_ZTV1B-8
219+
; CHECK-NEXT: .long f2@PLT-_ZTV1B-8
220+
221+
@_ZTV1B = dso_local constant { [5 x i32] } { [5 x i32] [
222+
i32 0,
223+
i32 0,
224+
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @f0 to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [7 x i32] }, ptr @_ZTV1B, i32 0, i32 0, i32 2) to i64)) to i32),
225+
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @f1 to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [7 x i32] }, ptr @_ZTV1B, i32 0, i32 0, i32 2) to i64)) to i32),
226+
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @f2 to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [7 x i32] }, ptr @_ZTV1B, i32 0, i32 0, i32 2) to i64)) to i32)
227+
] }
228+
229+
declare void @f0()
230+
declare void @f1()
231+
define dso_local void @f2() {
232+
ret void
233+
}

llvm/test/CodeGen/X86/x86-plt-relative-reloc.ll renamed to llvm/test/CodeGen/X86/relative-reloc-32.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ declare void @fn2() unnamed_addr
1111
declare void @fn3()
1212

1313
; CHECK: .long 0
14-
; CHECK-NEXT: .long fn1@PLT-vtable-4
15-
; CHECK-NEXT: .long fn2@PLT-vtable-4
14+
; CHECK-NEXT: .long fn1-vtable-4
15+
; CHECK-NEXT: .long fn2-vtable-4
1616
; CHECK-NEXT: .long fn3-vtable-4

llvm/test/CodeGen/X86/x86-64-plt-relative-reloc.ll renamed to llvm/test/CodeGen/X86/relative-reloc-64.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ declare void @fn2() unnamed_addr
1212
declare void @fn3()
1313
@global4 = external unnamed_addr global i8
1414

15+
;; Create a PC-relative relocation that the linker might decline if the addend symbol is preemptible.
1516
; CHECK: .long 0
16-
; CHECK-NEXT: .long fn1@PLT-vtable-4
17-
; CHECK-NEXT: .long fn2@PLT-vtable-4
17+
; CHECK-NEXT: .long fn1-vtable-4
18+
; CHECK-NEXT: .long fn2-vtable-4
1819
; CHECK-NEXT: .long fn3-vtable-4
1920
; CHECK-NEXT: .long global4-vtable-4

llvm/test/CodeGen/X86/relptr-rodata.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ target triple = "x86_64-unknown-linux-gnu"
2424
; CHECK-NEXT: .globl obj
2525
; CHECK: obj:
2626
; CHECK: .long 0
27-
; CHECK: .long hidden_func-obj-4
27+
; CHECK: .long hidden_func@PLT-obj-4
2828

2929
declare hidden void @hidden_func()
3030

0 commit comments

Comments
 (0)