Skip to content

Commit fb84912

Browse files
committed
Mark BTI-clearing instructions as scheduling region boundaries
Following #68313 this patch extends the idea to M-profile PACBTI. The Machine Scheduler can reorder instructions within a scheduling region depending on the scheduling policy set. If a BTI-clearing instruction happens to partake in one such region, it might be moved around, therefore ending up where it shouldn't. The solution is to mark all BTI-clearing instructions as scheduling region boundaries. This essentially means that they must not be part of any scheduling region, and as consequence never get moved: - PAC - PACBTI - BTI - SG - CALL_BTI (pseudo-instruction for setjmp + bti) Note that PAC isn't BTI-clearing, but it's replaced by PACBTI late in the compilation pipeline. As far as I know, currently it isn't possible to organically obtain code that's susceptible to the bug: - Instructions that write to SP are region boundaries. PAC seems to always be followed by the pushing of r12 to the stack, so essentially PAC is always by itself in a scheduling region. - CALL_BTI is expanded into a machine instruction bundle. Bundles are unpacked only after the last machine scheduler run. Thus setjmp and BTI can be separated only if someone deliberately runs the scheduler once more. - The BTI insertion pass is run late in the pipeline, only after the last machine scheduling has run. So once again it can be reordered only if someone deliberately runs the scheduler again. Nevertheless, one can reasonably argue that we should prevent the bug in spite of the compiler not being able to produce the required conditions for it. If things change, the compiler will be robust against this issue. The tests written for this are contrived: bogus MIR instructions have been added adjacent to the BTI-clearing instructions in order to have them inside non-trivial scheduling regions.
1 parent 42b28c6 commit fb84912

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
20882088
if (!MI.isCall() && MI.definesRegister(ARM::SP))
20892089
return true;
20902090

2091-
return false;
2091+
return TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF);
20922092
}
20932093

20942094
bool ARMBaseInstrInfo::

llvm/lib/Target/ARM/Thumb2InstrInfo.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ MachineInstr *Thumb2InstrInfo::commuteInstructionImpl(MachineInstr &MI,
286286
return ARMBaseInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
287287
}
288288

289+
bool Thumb2InstrInfo::isSchedulingBoundary(const MachineInstr &MI,
290+
const MachineBasicBlock *MBB,
291+
const MachineFunction &MF) const {
292+
// BTI clearing instructions shall not take part in scheduling regions as
293+
// they must stay in their intended place. Although PAC isn't BTI clearing,
294+
// it can be transformed into PACBTI after the pre-RA Machine Scheduling
295+
// has taken place, so its movement must also be restricted.
296+
switch (MI.getOpcode()) {
297+
case ARM::t2BTI:
298+
case ARM::t2PAC:
299+
case ARM::t2PACBTI:
300+
case ARM::t2CALL_BTI:
301+
case ARM::t2SG:
302+
return true;
303+
default:
304+
break;
305+
}
306+
return ARMBaseInstrInfo::isSchedulingBoundary(MI, MBB, MF);
307+
}
308+
289309
void llvm::emitT2RegPlusImmediate(MachineBasicBlock &MBB,
290310
MachineBasicBlock::iterator &MBBI,
291311
const DebugLoc &dl, Register DestReg,

llvm/lib/Target/ARM/Thumb2InstrInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class Thumb2InstrInfo : public ARMBaseInstrInfo {
6868
unsigned OpIdx1,
6969
unsigned OpIdx2) const override;
7070

71+
bool isSchedulingBoundary(const MachineInstr &MI,
72+
const MachineBasicBlock *MBB,
73+
const MachineFunction &MF) const override;
74+
7175
private:
7276
void expandLoadStackGuard(MachineBasicBlock::iterator MI) const override;
7377
};
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# RUN: llc -o - -run-pass=machine-scheduler -misched=shuffle %s | FileCheck %s
2+
# RUN: llc -o - -run-pass=postmisched %s | FileCheck %s
3+
4+
--- |
5+
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
6+
target triple = "thumbv8.1m.main-arm-none-eabi"
7+
8+
define i32 @foo_bti(i32 %a) #1 {
9+
entry:
10+
%add = add nsw i32 %a, 1
11+
ret i32 %add
12+
}
13+
14+
define i32 @foo_pacbti(i32 %a) #1 {
15+
entry:
16+
%add = add nsw i32 %a, 1
17+
ret i32 %add
18+
}
19+
20+
define i32 @foo_setjmp() #0 {
21+
entry:
22+
%buf = alloca [20 x i64], align 8
23+
%call = call i32 @setjmp(ptr noundef nonnull %buf) #4
24+
%tobool.not = icmp eq i32 %call, 0
25+
br i1 %tobool.not, label %if.else, label %if.then
26+
27+
if.then: ; preds = %entry
28+
call void @longjmp(ptr noundef nonnull %buf, i32 noundef 1) #5
29+
unreachable
30+
31+
if.else: ; preds = %entry
32+
ret i32 0
33+
}
34+
35+
declare i32 @setjmp(ptr noundef) #2
36+
declare void @longjmp(ptr noundef, i32 noundef) #3
37+
38+
attributes #0 = { "frame-pointer"="all" "target-cpu"="cortex-m55" "target-features"="+armv8.1-m.main" }
39+
attributes #1 = { "frame-pointer"="all" "target-cpu"="cortex-m55" "target-features"="+armv8.1-m.main" }
40+
attributes #2 = { nounwind returns_twice "frame-pointer"="all" "target-cpu"="cortex-m55" "target-features"="+armv8.1-m.main" }
41+
attributes #3 = { noreturn nounwind "frame-pointer"="all" "target-cpu"="cortex-m55" "target-features"="+armv8.1-m.main" }
42+
attributes #4 = { nounwind returns_twice }
43+
attributes #5 = { noreturn nounwind }
44+
45+
...
46+
---
47+
name: foo_bti
48+
tracksRegLiveness: true
49+
body: |
50+
bb.0.entry:
51+
liveins: $r0
52+
53+
t2BTI
54+
renamable $r0, dead $cpsr = nsw tADDi8 killed renamable $r0, 1, 14 /* CC::al */, $noreg
55+
tBX_RET 14 /* CC::al */, $noreg, implicit killed $r0
56+
57+
...
58+
59+
# CHECK-LABEL: name: foo_bti
60+
# CHECK: body:
61+
# CHECK-NEXT: bb.0.entry:
62+
# CHECK-NEXT: liveins: $r0
63+
# CHECK-NEXT: {{^ +$}}
64+
# CHECK-NEXT: t2BTI
65+
66+
---
67+
name: foo_pacbti
68+
tracksRegLiveness: true
69+
body: |
70+
bb.0.entry:
71+
liveins: $r0, $lr, $r12
72+
73+
frame-setup t2PAC implicit-def $r12, implicit $lr, implicit $sp
74+
renamable $r2 = nsw t2ADDri $r0, 3, 14 /* CC::al */, $noreg, $noreg
75+
$sp = frame-setup t2STMDB_UPD $sp, 14 /* CC::al */, $noreg, killed $r7, killed $lr
76+
$r7 = frame-setup tMOVr killed $sp, 14 /* CC::al */, $noreg
77+
early-clobber $sp = frame-setup t2STR_PRE killed $r12, $sp, -4, 14 /* CC::al */, $noreg
78+
renamable $r0 = nsw t2ADDri killed renamable $r0, 1, 14 /* CC::al */, $noreg, $noreg
79+
$r12, $sp = frame-destroy t2LDR_POST $sp, 4, 14 /* CC::al */, $noreg
80+
$sp = frame-destroy t2LDMIA_UPD $sp, 14 /* CC::al */, $noreg, def $r7, def $lr
81+
t2AUT implicit $r12, implicit $lr, implicit $sp
82+
tBX_RET 14 /* CC::al */, $noreg, implicit $r0
83+
84+
...
85+
86+
# CHECK-LABEL: name: foo_pacbti
87+
# CHECK: body:
88+
# CHECK-NEXT: bb.0.entry:
89+
# CHECK-NEXT: liveins: $r0, $lr, $r12
90+
# CHECK-NEXT: {{^ +$}}
91+
# CHECK-NEXT: frame-setup t2PAC implicit-def $r12, implicit $lr, implicit $sp
92+
93+
---
94+
name: foo_setjmp
95+
tracksRegLiveness: true
96+
body: |
97+
bb.0.entry:
98+
successors: %bb.1
99+
liveins: $lr
100+
101+
frame-setup tPUSH 14 /* CC::al */, $noreg, $r7, killed $lr, implicit-def $sp, implicit $sp
102+
$r7 = frame-setup tMOVr $sp, 14 /* CC::al */, $noreg
103+
$sp = frame-setup tSUBspi $sp, 40, 14 /* CC::al */, $noreg
104+
renamable $r0 = tMOVr $sp, 14 /* CC::al */, $noreg
105+
tBL 14 /* CC::al */, $noreg, @setjmp, csr_aapcs, implicit-def dead $lr, implicit $sp, implicit killed $r0, implicit-def $sp, implicit-def $r0
106+
t2BTI
107+
renamable $r2 = nsw t2ADDri $r0, 3, 14 /* CC::al */, $noreg, $noreg
108+
tCMPi8 killed renamable $r0, 0, 14 /* CC::al */, $noreg, implicit-def $cpsr
109+
t2IT 0, 2, implicit-def $itstate
110+
renamable $r0 = tMOVi8 $noreg, 0, 0 /* CC::eq */, $cpsr, implicit $itstate
111+
$sp = frame-destroy tADDspi $sp, 40, 0 /* CC::eq */, $cpsr, implicit $itstate
112+
frame-destroy tPOP_RET 0 /* CC::eq */, killed $cpsr, def $r7, def $pc, implicit killed $r0, implicit $sp, implicit killed $itstate
113+
114+
bb.1.if.then:
115+
renamable $r0 = tMOVr $sp, 14 /* CC::al */, $noreg
116+
renamable $r1, dead $cpsr = tMOVi8 1, 14 /* CC::al */, $noreg
117+
tBL 14 /* CC::al */, $noreg, @longjmp, csr_aapcs, implicit-def dead $lr, implicit $sp, implicit killed $r0, implicit killed $r1, implicit-def $sp
118+
119+
...
120+
121+
# CHECK-LABEL: name: foo_setjmp
122+
# CHECK: body:
123+
# CHECK: tBL 14 /* CC::al */, $noreg, @setjmp, csr_aapcs, implicit-def dead $lr, implicit $sp, implicit killed $r0, implicit-def $sp, implicit-def $r0
124+
# CHECK-NEXT: t2BTI

0 commit comments

Comments
 (0)