Skip to content

Commit 961f31d

Browse files
committed
[TargetMachine] Don't imply dso_local on global variable declarations in Reloc::Static model
clang/lib/CodeGen/CodeGenModule sets dso_local on applicable global variables, we don't need to duplicate the work in TargetMachine:shouldAssumeDSOLocal. (Actually the long-term goal (started by r324535) is to remove as much additional implied dso_local in TargetMachine:shouldAssumeDSOLocal as possible.) By not implying dso_local, we will respect dso_local/dso_preemptable specifiers set by the frontend. This allows the proposed -fno-direct-access-external-data option to work with -fno-pic and prevent copy relocations. This patch should be NFC in terms of the Clang behavior because the case we don't set dso_local is a case Clang sets dso_local. However, some tests don't set dso_local on some `external global` and expose some differences. Most tests have been fixed to be more robust in previous commits.
1 parent fd32639 commit 961f31d

13 files changed

+1160
-1085
lines changed

llvm/lib/Target/TargetMachine.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,18 @@ bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
190190
if (Arch == Triple::ppc || TT.isPPC64())
191191
return false;
192192

193-
// Check if we can use copy relocations.
194-
if (!(GV && GV->isThreadLocal()) && RM == Reloc::Static)
195-
return true;
193+
// dso_local is traditionally implied for Reloc::Static. Eventually we shall
194+
// drop the if block entirely and respect dso_local/dso_preemptable
195+
// specifiers set by the frontend.
196+
if (RM == Reloc::Static) {
197+
// We currently respect dso_local/dso_preemptable specifiers for
198+
// variables.
199+
if (!GV || F)
200+
return true;
201+
// TODO Remove the special case for x86-32 and wasm.
202+
if ((Arch == Triple::x86 || TT.isWasm()) && !GV->isThreadLocal())
203+
return true;
204+
}
196205
} else if (TT.isOSBinFormatELF()) {
197206
// If dso_local allows AsmPrinter::getSymbolPreferLocal to use a local
198207
// alias, set the flag. We cannot set dso_local for other global values,

llvm/test/CodeGen/AArch64/extern-weak.ll

+4-6
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ define i32* @bar() {
3939

4040
ret i32* %addr
4141

42-
; In the large model, the usual relocations are absolute and can
43-
; materialise 0.
44-
; CHECK-LARGE: movz [[ADDR:x[0-9]+]], #:abs_g0_nc:arr_var
45-
; CHECK-LARGE: movk [[ADDR]], #:abs_g1_nc:arr_var
46-
; CHECK-LARGE: movk [[ADDR]], #:abs_g2_nc:arr_var
47-
; CHECK-LARGE: movk [[ADDR]], #:abs_g3:arr_var
42+
; Note, In the large model, if dso_local, the relocations are absolute and can materialise 0.
43+
; CHECK-LARGE: adrp x[[ADDR:[0-9]+]], :got:arr_var
44+
; CHECK-LARGE-NEXT: ldr x[[ADDR]], [x[[ADDR]], :got_lo12:arr_var]
45+
; CHECK-LARGE-NEXT: add x0, x[[ADDR]], #20
4846

4947
; CHECK-TINY: ldr [[BASE:x[0-9]+]], :got:arr_var
5048
; CHECK-TINY: add x0, [[BASE]], #20

llvm/test/CodeGen/AArch64/tiny_model.ll

+13-12
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
define void @foo1() {
1616
; CHECK-LABEL: foo1:
1717
; CHECK: // %bb.0: // %entry
18-
; CHECK-NEXT: adr x8, src
18+
; CHECK-NEXT: ldr x8, :got:src
1919
; CHECK-NEXT: ldrb w8, [x8]
20-
; CHECK-NEXT: adr x9, dst
20+
; CHECK-NEXT: ldr x9, :got:dst
2121
; CHECK-NEXT: strb w8, [x9]
2222
; CHECK-NEXT: ret
2323
;
2424
; CHECK-GLOBISEL-LABEL: foo1:
2525
; CHECK-GLOBISEL: // %bb.0: // %entry
26-
; CHECK-GLOBISEL-NEXT: adr x8, src
26+
; CHECK-GLOBISEL-NEXT: ldr x8, :got:src
2727
; CHECK-GLOBISEL-NEXT: ldrb w8, [x8]
28-
; CHECK-GLOBISEL-NEXT: adr x9, dst
28+
; CHECK-GLOBISEL-NEXT: ldr x9, :got:dst
2929
; CHECK-GLOBISEL-NEXT: strb w8, [x9]
3030
; CHECK-GLOBISEL-NEXT: ret
3131
;
@@ -53,15 +53,15 @@ entry:
5353
define void @foo2() {
5454
; CHECK-LABEL: foo2:
5555
; CHECK: // %bb.0: // %entry
56-
; CHECK-NEXT: adr x8, ptr
57-
; CHECK-NEXT: adr x9, dst
56+
; CHECK-NEXT: ldr x8, :got:ptr
57+
; CHECK-NEXT: ldr x9, :got:dst
5858
; CHECK-NEXT: str x9, [x8]
5959
; CHECK-NEXT: ret
6060
;
6161
; CHECK-GLOBISEL-LABEL: foo2:
6262
; CHECK-GLOBISEL: // %bb.0: // %entry
63-
; CHECK-GLOBISEL-NEXT: adr x8, ptr
64-
; CHECK-GLOBISEL-NEXT: adr x9, dst
63+
; CHECK-GLOBISEL-NEXT: ldr x8, :got:ptr
64+
; CHECK-GLOBISEL-NEXT: ldr x9, :got:dst
6565
; CHECK-GLOBISEL-NEXT: str x9, [x8]
6666
; CHECK-GLOBISEL-NEXT: ret
6767
;
@@ -88,16 +88,17 @@ define void @foo3() {
8888
;
8989
; CHECK-LABEL: foo3:
9090
; CHECK: // %bb.0: // %entry
91-
; CHECK-NEXT: adr x8, src
91+
; CHECK-NEXT: ldr x8, :got:src
92+
; CHECK-NEXT: ldr x9, :got:ptr
9293
; CHECK-NEXT: ldrb w8, [x8]
93-
; CHECK-NEXT: ldr x9, ptr
94+
; CHECK-NEXT: ldr x9, [x9]
9495
; CHECK-NEXT: strb w8, [x9]
9596
; CHECK-NEXT: ret
9697
;
9798
; CHECK-GLOBISEL-LABEL: foo3:
9899
; CHECK-GLOBISEL: // %bb.0: // %entry
99-
; CHECK-GLOBISEL-NEXT: adr x8, src
100-
; CHECK-GLOBISEL-NEXT: adr x9, ptr
100+
; CHECK-GLOBISEL-NEXT: ldr x8, :got:src
101+
; CHECK-GLOBISEL-NEXT: ldr x9, :got:ptr
101102
; CHECK-GLOBISEL-NEXT: ldrb w8, [x8]
102103
; CHECK-GLOBISEL-NEXT: ldr x9, [x9]
103104
; CHECK-GLOBISEL-NEXT: strb w8, [x9]

0 commit comments

Comments
 (0)