Skip to content

Commit a842430

Browse files
authored
[AArch64] Add check that prologue insertion doesn't clobber live regs. (#71826)
This patch extends AArch64FrameLowering::emitProglogue to check if the inserted prologue clobbers live registers. It updates `llvm/test/CodeGen/AArch64/framelayout-scavengingslot.mir` with an extra load to make x9 live before the store, preserving the original test. It uses the original `llvm/test/CodeGen/AArch64/framelayout-scavengingslot.mir` as `llvm/test/CodeGen/AArch64/emit-prologue-clobber-verification.mir`, because there x9 is marked as live on entry, but used as scratch reg as it is not callee saved. The new assertion catches a mis-compile in `store-swift-async-context-clobber-live-reg.ll` on https://github.com/apple/llvm-project/tree/next
1 parent 369c0eb commit a842430

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,18 @@ static void emitDefineCFAWithFP(MachineFunction &MF, MachineBasicBlock &MBB,
14231423
.setMIFlags(MachineInstr::FrameSetup);
14241424
}
14251425

1426+
/// Collect live registers from the end of \p MI's parent up to (including) \p
1427+
/// MI in \p LiveRegs.
1428+
static void getLivePhysRegsUpTo(MachineInstr &MI, const TargetRegisterInfo &TRI,
1429+
LivePhysRegs &LiveRegs) {
1430+
1431+
MachineBasicBlock &MBB = *MI.getParent();
1432+
LiveRegs.addLiveOuts(MBB);
1433+
for (const MachineInstr &MI :
1434+
reverse(make_range(MI.getIterator(), MBB.instr_end())))
1435+
LiveRegs.stepBackward(MI);
1436+
}
1437+
14261438
void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14271439
MachineBasicBlock &MBB) const {
14281440
MachineBasicBlock::iterator MBBI = MBB.begin();
@@ -1431,6 +1443,8 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14311443
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
14321444
const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
14331445
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1446+
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1447+
14341448
MachineModuleInfo &MMI = MF.getMMI();
14351449
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
14361450
bool EmitCFI = AFI->needsDwarfUnwindInfo(MF);
@@ -1440,6 +1454,39 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14401454
bool HasWinCFI = false;
14411455
auto Cleanup = make_scope_exit([&]() { MF.setHasWinCFI(HasWinCFI); });
14421456

1457+
MachineBasicBlock::iterator End = MBB.end();
1458+
#ifndef NDEBUG
1459+
// Collect live register from the end of MBB up to the start of the existing
1460+
// frame setup instructions.
1461+
MachineBasicBlock::iterator NonFrameStart = MBB.begin();
1462+
while (NonFrameStart != End &&
1463+
NonFrameStart->getFlag(MachineInstr::FrameSetup))
1464+
++NonFrameStart;
1465+
1466+
LivePhysRegs LiveRegs(*TRI);
1467+
if (NonFrameStart != MBB.end()) {
1468+
getLivePhysRegsUpTo(*NonFrameStart, *TRI, LiveRegs);
1469+
// Ignore registers used for stack management for now.
1470+
LiveRegs.removeReg(AArch64::SP);
1471+
LiveRegs.removeReg(AArch64::X19);
1472+
LiveRegs.removeReg(AArch64::FP);
1473+
LiveRegs.removeReg(AArch64::LR);
1474+
}
1475+
1476+
auto VerifyClobberOnExit = make_scope_exit([&]() {
1477+
if (NonFrameStart == MBB.end())
1478+
return;
1479+
// Check if any of the newly instructions clobber any of the live registers.
1480+
for (MachineInstr &MI :
1481+
make_range(MBB.instr_begin(), NonFrameStart->getIterator())) {
1482+
for (auto &Op : MI.operands())
1483+
if (Op.isReg() && Op.isDef())
1484+
assert(!LiveRegs.contains(Op.getReg()) &&
1485+
"live register clobbered by inserted prologue instructions");
1486+
}
1487+
});
1488+
#endif
1489+
14431490
bool IsFunclet = MBB.isEHFuncletEntry();
14441491

14451492
// At this point, we're going to decide whether or not the function uses a
@@ -1608,7 +1655,6 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
16081655
// Move past the saves of the callee-saved registers, fixing up the offsets
16091656
// and pre-inc if we decided to combine the callee-save and local stack
16101657
// pointer bump above.
1611-
MachineBasicBlock::iterator End = MBB.end();
16121658
while (MBBI != End && MBBI->getFlag(MachineInstr::FrameSetup) &&
16131659
!IsSVECalleeSave(MBBI)) {
16141660
if (CombineSPBump)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# RUN: not --crash llc -mtriple=aarch64-none-linux-gnu -run-pass=prologepilog %s -o -
2+
#
3+
# REQUIRES: asserts
4+
#
5+
---
6+
# x9 is marked as live on function entry, but it will be used as scratch
7+
# register for prologue computations at the beginning of the prologue.
8+
# Use this to check we catch that the prologue clobbers $x9.
9+
name: x9_clobbered_on_fn_entry
10+
tracksRegLiveness: true
11+
frameInfo:
12+
isFrameAddressTaken: true
13+
stack:
14+
- { id: 0, size: 16, alignment: 16 }
15+
- { id: 1, size: 32768, alignment: 32 }
16+
body: |
17+
bb.0:
18+
liveins: $x0, $x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17, $x18, $x19, $x20, $x21, $x22, $x23, $x24, $x25, $x26, $x27, $x28, $lr
19+
STRXui $x0, %stack.0, 0
20+
B %bb.1
21+
bb.1:
22+
liveins: $x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17, $x18, $x19, $x20, $x21, $x22, $x23, $x24, $x25, $x26, $x27, $x28, $lr
23+
RET_ReallyLR implicit $lr
24+
...

llvm/test/CodeGen/AArch64/framelayout-scavengingslot.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ stack:
1919
body: |
2020
bb.0:
2121
liveins: $x0, $x8
22+
$x9 = LDRXui $x0, 0 :: (load (s64))
2223
STRXui $x0, %stack.0, 0
2324
B %bb.1
2425
bb.1:

llvm/test/CodeGen/AArch64/framelayout-sve-calleesaves-fix.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
; CHECK-NEXT: ret
3131
...
3232
name: fix_restorepoint_p4
33+
tracksRegLiveness: true
3334
stack:
3435
- { id: 0, stack-id: scalable-vector, size: 16, alignment: 16 }
3536
body: |
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc -o - -mtriple=arm64e-apple-macosx %s | FileCheck %s
3+
4+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
5+
6+
define swifttailcc void @test_async_with_jumptable(ptr %src, ptr swiftasync %as) #0 {
7+
; CHECK-LABEL: test_async_with_jumptable:
8+
; CHECK: ; %bb.0: ; %entry
9+
; CHECK-NEXT: orr x29, x29, #0x1000000000000000
10+
; CHECK-NEXT: str x19, [sp, #-32]! ; 8-byte Folded Spill
11+
; CHECK-NEXT: stp x29, x30, [sp, #16] ; 16-byte Folded Spill
12+
; CHECK-NEXT: add x16, sp, #8
13+
; CHECK-NEXT: movk x16, #49946, lsl #48
14+
; CHECK-NEXT: mov x17, x22
15+
; CHECK-NEXT: pacdb x17, x16
16+
; CHECK-NEXT: str x17, [sp, #8]
17+
; CHECK-NEXT: add x29, sp, #16
18+
; CHECK-NEXT: .cfi_def_cfa w29, 16
19+
; CHECK-NEXT: .cfi_offset w30, -8
20+
; CHECK-NEXT: .cfi_offset w29, -16
21+
; CHECK-NEXT: .cfi_offset w19, -32
22+
; CHECK-NEXT: mov x20, x22
23+
; CHECK-NEXT: ldr x8, [x0]
24+
; CHECK-NEXT: cmp x8, #2
25+
; CHECK-NEXT: csel x9, xzr, x0, eq
26+
; CHECK-NEXT: cmp x8, #0
27+
; CHECK-NEXT: csel x10, x22, xzr, eq
28+
; CHECK-NEXT: cmp x8, #1
29+
; CHECK-NEXT: csel x19, x9, x10, gt
30+
; CHECK-NEXT: mov x22, x0
31+
; CHECK-NEXT: bl _foo
32+
; CHECK-NEXT: mov x2, x0
33+
; CHECK-NEXT: mov x0, x19
34+
; CHECK-NEXT: mov x1, x20
35+
; CHECK-NEXT: ldp x29, x30, [sp, #16] ; 16-byte Folded Reload
36+
; CHECK-NEXT: ldr x19, [sp], #32 ; 8-byte Folded Reload
37+
; CHECK-NEXT: and x29, x29, #0xefffffffffffffff
38+
; CHECK-NEXT: br x2
39+
entry:
40+
%l = load i64, ptr %src, align 8
41+
switch i64 %l, label %dead [
42+
i64 0, label %exit
43+
i64 1, label %then.1
44+
i64 2, label %then.2
45+
i64 3, label %then.3
46+
]
47+
48+
then.1:
49+
br label %exit
50+
51+
then.2:
52+
br label %exit
53+
54+
then.3:
55+
br label %exit
56+
57+
dead: ; preds = %entryresume.5
58+
unreachable
59+
60+
exit:
61+
%p = phi ptr [ %src, %then.3 ], [ null, %then.2 ], [ %as, %entry ], [ null, %then.1 ]
62+
%r = call i64 @foo()
63+
%fn = inttoptr i64 %r to ptr
64+
musttail call swifttailcc void %fn(ptr swiftasync %src, ptr %p, ptr %as)
65+
ret void
66+
}
67+
68+
declare i64 @foo()
69+
70+
attributes #0 = { "frame-pointer"="non-leaf" }

0 commit comments

Comments
 (0)