Skip to content

Commit 5ad320a

Browse files
authored
[ARM][Thumb2] Mark BTI-clearing instructions as scheduling region boundaries (#79173)
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 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 run 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 3871eab commit 5ad320a

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed

llvm/lib/Target/ARM/Thumb2InstrInfo.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ 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::t2SG:
301+
return true;
302+
default:
303+
break;
304+
}
305+
return ARMBaseInstrInfo::isSchedulingBoundary(MI, MBB, MF);
306+
}
307+
289308
void llvm::emitT2RegPlusImmediate(MachineBasicBlock &MBB,
290309
MachineBasicBlock::iterator &MBBI,
291310
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: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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() #0 {
9+
entry:
10+
ret i32 0
11+
}
12+
13+
define i32 @foo_pac() #0 {
14+
entry:
15+
ret i32 0
16+
}
17+
18+
define i32 @foo_pacbti() #0 {
19+
entry:
20+
ret i32 0
21+
}
22+
23+
define i32 @foo_setjmp() #0 {
24+
entry:
25+
ret i32 0
26+
if.then:
27+
ret i32 0
28+
}
29+
30+
define i32 @foo_sg() #0 {
31+
entry:
32+
ret i32 0
33+
}
34+
35+
declare i32 @setjmp(ptr noundef) #1
36+
declare void @longjmp(ptr noundef, i32 noundef) #2
37+
38+
attributes #0 = { "frame-pointer"="all" "target-cpu"="cortex-m55" "target-features"="+armv8.1-m.main" }
39+
attributes #1 = { nounwind returns_twice "frame-pointer"="all" "target-cpu"="cortex-m55" "target-features"="+armv8.1-m.main" }
40+
attributes #2 = { noreturn nounwind "frame-pointer"="all" "target-cpu"="cortex-m55" "target-features"="+armv8.1-m.main" }
41+
42+
...
43+
---
44+
name: foo_bti
45+
tracksRegLiveness: true
46+
body: |
47+
bb.0.entry:
48+
liveins: $r0
49+
50+
t2BTI
51+
renamable $r0, dead $cpsr = nsw tADDi8 killed renamable $r0, 1, 14 /* CC::al */, $noreg
52+
tBX_RET 14 /* CC::al */, $noreg, implicit killed $r0
53+
54+
...
55+
56+
# CHECK-LABEL: name: foo_bti
57+
# CHECK: body:
58+
# CHECK-NEXT: bb.0.entry:
59+
# CHECK-NEXT: liveins: $r0
60+
# CHECK-NEXT: {{^ +$}}
61+
# CHECK-NEXT: t2BTI
62+
63+
---
64+
name: foo_pac
65+
tracksRegLiveness: true
66+
body: |
67+
bb.0.entry:
68+
liveins: $r0, $lr, $r12
69+
70+
frame-setup t2PAC implicit-def $r12, implicit $lr, implicit $sp
71+
renamable $r2 = nsw t2ADDri $r0, 3, 14 /* CC::al */, $noreg, $noreg
72+
$sp = frame-setup t2STMDB_UPD $sp, 14 /* CC::al */, $noreg, killed $r7, killed $lr
73+
$r7 = frame-setup tMOVr killed $sp, 14 /* CC::al */, $noreg
74+
early-clobber $sp = frame-setup t2STR_PRE killed $r12, $sp, -4, 14 /* CC::al */, $noreg
75+
$r12, $sp = frame-destroy t2LDR_POST $sp, 4, 14 /* CC::al */, $noreg
76+
$sp = frame-destroy t2LDMIA_UPD $sp, 14 /* CC::al */, $noreg, def $r7, def $lr
77+
t2AUT implicit $r12, implicit $lr, implicit $sp
78+
tBX_RET 14 /* CC::al */, $noreg, implicit $r0
79+
80+
...
81+
82+
# CHECK-LABEL: name: foo_pac
83+
# CHECK: body:
84+
# CHECK-NEXT: bb.0.entry:
85+
# CHECK-NEXT: liveins: $r0, $lr, $r12
86+
# CHECK-NEXT: {{^ +$}}
87+
# CHECK-NEXT: frame-setup t2PAC implicit-def $r12, implicit $lr, implicit $sp
88+
89+
---
90+
name: foo_pacbti
91+
tracksRegLiveness: true
92+
body: |
93+
bb.0.entry:
94+
liveins: $r0, $lr, $r12
95+
96+
frame-setup t2PACBTI implicit-def $r12, implicit $lr, implicit $sp
97+
renamable $r2 = nsw t2ADDri $r0, 3, 14 /* CC::al */, $noreg, $noreg
98+
$sp = frame-setup t2STMDB_UPD $sp, 14 /* CC::al */, $noreg, killed $r7, killed $lr
99+
$r7 = frame-setup tMOVr killed $sp, 14 /* CC::al */, $noreg
100+
early-clobber $sp = frame-setup t2STR_PRE killed $r12, $sp, -4, 14 /* CC::al */, $noreg
101+
$r12, $sp = frame-destroy t2LDR_POST $sp, 4, 14 /* CC::al */, $noreg
102+
$sp = frame-destroy t2LDMIA_UPD $sp, 14 /* CC::al */, $noreg, def $r7, def $lr
103+
t2AUT implicit $r12, implicit $lr, implicit $sp
104+
tBX_RET 14 /* CC::al */, $noreg, implicit $r0
105+
106+
...
107+
108+
# CHECK-LABEL: name: foo_pacbti
109+
# CHECK: body:
110+
# CHECK-NEXT: bb.0.entry:
111+
# CHECK-NEXT: liveins: $r0, $lr, $r12
112+
# CHECK-NEXT: {{^ +$}}
113+
# CHECK-NEXT: frame-setup t2PACBTI implicit-def $r12, implicit $lr, implicit $sp
114+
115+
---
116+
name: foo_setjmp
117+
tracksRegLiveness: true
118+
body: |
119+
bb.0.entry:
120+
successors: %bb.1
121+
liveins: $lr
122+
123+
frame-setup tPUSH 14 /* CC::al */, $noreg, $r7, killed $lr, implicit-def $sp, implicit $sp
124+
$r7 = frame-setup tMOVr $sp, 14 /* CC::al */, $noreg
125+
$sp = frame-setup tSUBspi $sp, 40, 14 /* CC::al */, $noreg
126+
renamable $r0 = tMOVr $sp, 14 /* CC::al */, $noreg
127+
tBL 14 /* CC::al */, $noreg, @setjmp, csr_aapcs, implicit-def dead $lr, implicit $sp, implicit killed $r0, implicit-def $sp, implicit-def $r0
128+
t2BTI
129+
renamable $r2 = nsw t2ADDri $r0, 3, 14 /* CC::al */, $noreg, $noreg
130+
tCMPi8 killed renamable $r0, 0, 14 /* CC::al */, $noreg, implicit-def $cpsr
131+
t2IT 0, 2, implicit-def $itstate
132+
renamable $r0 = tMOVi8 $noreg, 0, 0 /* CC::eq */, $cpsr, implicit $itstate
133+
$sp = frame-destroy tADDspi $sp, 40, 0 /* CC::eq */, $cpsr, implicit $itstate
134+
frame-destroy tPOP_RET 0 /* CC::eq */, killed $cpsr, def $r7, def $pc, implicit killed $r0, implicit $sp, implicit killed $itstate
135+
136+
bb.1.if.then:
137+
renamable $r0 = tMOVr $sp, 14 /* CC::al */, $noreg
138+
renamable $r1, dead $cpsr = tMOVi8 1, 14 /* CC::al */, $noreg
139+
tBL 14 /* CC::al */, $noreg, @longjmp, csr_aapcs, implicit-def dead $lr, implicit $sp, implicit killed $r0, implicit killed $r1, implicit-def $sp
140+
141+
...
142+
143+
# CHECK-LABEL: name: foo_setjmp
144+
# CHECK: body:
145+
# CHECK: tBL 14 /* CC::al */, $noreg, @setjmp, csr_aapcs, implicit-def dead $lr, implicit $sp, implicit killed $r0, implicit-def $sp, implicit-def $r0
146+
# CHECK-NEXT: t2BTI
147+
148+
---
149+
name: foo_sg
150+
tracksRegLiveness: true
151+
body: |
152+
bb.0.entry:
153+
liveins: $r0
154+
155+
t2SG 14 /* CC::al */, $noreg
156+
renamable $r0, dead $cpsr = nsw tADDi8 killed renamable $r0, 1, 14 /* CC::al */, $noreg
157+
tBX_RET 14 /* CC::al */, $noreg, implicit killed $r0
158+
159+
...
160+
161+
# CHECK-LABEL: name: foo_sg
162+
# CHECK: body:
163+
# CHECK-NEXT: bb.0.entry:
164+
# CHECK-NEXT: liveins: $r0
165+
# CHECK-NEXT: {{^ +$}}
166+
# CHECK-NEXT: t2SG

0 commit comments

Comments
 (0)