Skip to content

Commit

Permalink
[AArch64][GlobalISel] Don't localize TLS G_GLOBAL_VALUEs on Darwin.
Browse files Browse the repository at this point in the history
On Darwin these need to be selected into a function call for the TLS
address lookup. As a result, they can't be moved below a physreg write,
which happens in call sequences. In the long term, we should have some
mechanism in the localizer to prevent localizing into target-specific
atomic instruction sequences.

rdar://60056248

Differential Revision: https://reviews.llvm.org/D76652
  • Loading branch information
aemerson committed Mar 24, 2020
1 parent 5699d08 commit 472d282
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
13 changes: 13 additions & 0 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Expand Up @@ -13926,3 +13926,16 @@ void AArch64TargetLowering::finalizeLowering(MachineFunction &MF) const {
bool AArch64TargetLowering::needsFixedCatchObjects() const {
return false;
}

bool AArch64TargetLowering::shouldLocalize(
const MachineInstr &MI, const TargetTransformInfo *TTI) const {
if (MI.getOpcode() == TargetOpcode::G_GLOBAL_VALUE) {
// On Darwin, TLS global vars get selected into function calls, which
// we don't want localized, as they can get moved into the middle of a
// another call sequence.
const GlobalValue &GV = *MI.getOperand(1).getGlobal();
if (GV.isThreadLocal() && Subtarget->isTargetMachO())
return false;
}
return TargetLoweringBase::shouldLocalize(MI, TTI);
}
3 changes: 3 additions & 0 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.h
Expand Up @@ -859,6 +859,9 @@ class AArch64TargetLowering : public TargetLowering {
bool shouldNormalizeToSelectSequence(LLVMContext &, EVT) const override;

void finalizeLowering(MachineFunction &MF) const override;

bool shouldLocalize(const MachineInstr &MI,
const TargetTransformInfo *TTI) const override;
};

namespace AArch64 {
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/CodeGen/AArch64/GlobalISel/localizer-arm64-tti.ll
Expand Up @@ -59,3 +59,46 @@ if.end:
ret i32 0
}

@tls_gv = common thread_local global i32 0, align 4

; This test checks that we don't try to localize TLS variables on Darwin.
; If the user happens to be inside a call sequence, we could end up rematerializing
; below a physreg write, clobbering it (TLS accesses on Darwin need a function call).
; For now, we check we don't localize at all. We could in theory make sure that
; we don't localize into the middle of a call sequence instead.
define i32 @darwin_tls() {
; CHECK-LABEL: name: darwin_tls
; CHECK: bb.1.entry:
; CHECK: successors: %bb.2(0x40000000), %bb.3(0x40000000)
; CHECK: [[GV:%[0-9]+]]:gpr(p0) = G_GLOBAL_VALUE @tls_gv
; CHECK: [[GV1:%[0-9]+]]:gpr(p0) = G_GLOBAL_VALUE @var2
; CHECK: [[C:%[0-9]+]]:gpr(s32) = G_CONSTANT i32 0
; CHECK: [[GV2:%[0-9]+]]:gpr(p0) = G_GLOBAL_VALUE @var1
; CHECK: [[LOAD:%[0-9]+]]:gpr(s32) = G_LOAD [[GV2]](p0) :: (dereferenceable load 4 from @var1)
; CHECK: [[C1:%[0-9]+]]:gpr(s32) = G_CONSTANT i32 1
; CHECK: [[ICMP:%[0-9]+]]:gpr(s32) = G_ICMP intpred(ne), [[LOAD]](s32), [[C1]]
; CHECK: [[TRUNC:%[0-9]+]]:gpr(s1) = G_TRUNC [[ICMP]](s32)
; CHECK: G_BRCOND [[TRUNC]](s1), %bb.3
; CHECK: bb.2.if.then:
; CHECK: successors: %bb.3(0x80000000)
; CHECK: [[LOAD1:%[0-9]+]]:gpr(s32) = G_LOAD [[GV]](p0) :: (dereferenceable load 4 from @tls_gv)
; CHECK: [[GV3:%[0-9]+]]:gpr(p0) = G_GLOBAL_VALUE @var2
; CHECK: G_STORE [[LOAD1]](s32), [[GV3]](p0) :: (store 4 into @var2)
; CHECK: bb.3.if.end:
; CHECK: [[C2:%[0-9]+]]:gpr(s32) = G_CONSTANT i32 0
; CHECK: $w0 = COPY [[C2]](s32)
; CHECK: RET_ReallyLR implicit $w0
entry:
%0 = load i32, i32* @var1, align 4
%cmp = icmp eq i32 %0, 1
br i1 %cmp, label %if.then, label %if.end

if.then:
%tls = load i32, i32* @tls_gv, align 4
store i32 %tls, i32* @var2, align 4
br label %if.end

if.end:
ret i32 0
}

0 comments on commit 472d282

Please sign in to comment.