Skip to content

Commit db96363

Browse files
Shoreshenarsenm
andauthored
[AMDGPU] Avoid put implicit_def into bundle that break reg's liveness (#142563)
Cause: 1. `implicit_def` inside bundle does not count for define of reg in machineinst verifier 2. Including `implicit_def` will cause relative reg not define, result in `Bad machine code: Using an undefined physical register` in the machineinst verifier Fixes #139102 --------- Co-authored-by: Matt Arsenault <[email protected]>
1 parent d40d04f commit db96363

33 files changed

+186
-131
lines changed

llvm/lib/CodeGen/MachineInstrBundle.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI,
9494
return DebugLoc();
9595
}
9696

97+
/// Check if target reg is contained in given lists, which are:
98+
/// LocalDefsV as given list for virtual regs
99+
/// LocalDefsP as given list for physical regs, in BitVector[RegUnit] form
100+
static bool containsReg(SmallSetVector<Register, 32> LocalDefsV,
101+
const BitVector &LocalDefsP, Register Reg,
102+
const TargetRegisterInfo *TRI) {
103+
if (Reg.isPhysical()) {
104+
for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
105+
if (!LocalDefsP[Unit])
106+
return false;
107+
108+
return true;
109+
}
110+
return LocalDefsV.contains(Reg);
111+
}
112+
97113
/// finalizeBundle - Finalize a machine instruction bundle which includes
98114
/// a sequence of instructions starting from FirstMI to LastMI (exclusive).
99115
/// This routine adds a BUNDLE instruction to represent the bundle, it adds
@@ -115,6 +131,7 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
115131
Bundle.prepend(MIB);
116132

117133
SmallSetVector<Register, 32> LocalDefs;
134+
BitVector LocalDefsP(TRI->getNumRegUnits());
118135
SmallSet<Register, 8> DeadDefSet;
119136
SmallSet<Register, 16> KilledDefSet;
120137
SmallSetVector<Register, 8> ExternUses;
@@ -130,7 +147,7 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
130147
if (!Reg)
131148
continue;
132149

133-
if (LocalDefs.contains(Reg)) {
150+
if (containsReg(LocalDefs, LocalDefsP, Reg, TRI)) {
134151
MO.setIsInternalRead();
135152
if (MO.isKill()) {
136153
// Internal def is now killed.
@@ -165,8 +182,10 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
165182
}
166183
}
167184

168-
if (!MO.isDead() && Reg.isPhysical())
169-
LocalDefs.insert_range(TRI->subregs(Reg));
185+
if (!MO.isDead() && Reg.isPhysical()) {
186+
for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
187+
LocalDefsP.set(Unit);
188+
}
170189
}
171190

172191
// Set FrameSetup/FrameDestroy for the bundle. If any of the instructions

llvm/test/CodeGen/AArch64/blr-bti-preserves-operands.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# The arguments to the call must become implicit arguments, because the branch
99
# only expects to get 1 explicit operand which is the branch target.
1010

11-
# CHECK: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit $sp, implicit $x0, implicit $w1 {
11+
# CHECK: BUNDLE implicit-def $lr, implicit-def $sp, implicit $sp, implicit $x0, implicit $w1 {
1212
# CHECK: BL @_setjmp, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $x0, implicit $w1, implicit-def dead $lr, implicit $sp, implicit-def $sp
1313
# CHECK: HINT 36
1414
# CHECK: }

llvm/test/CodeGen/AArch64/expand-blr-rvmarker-pseudo.mir

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
# CHECK: bb.0:
4444
# CHECK-NEXT: liveins:
4545
# CHECK-NEXT: {{ $}}
46-
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit-def dead $x0, implicit-def $fp, implicit-def $w29, implicit-def $w29_hi, implicit $x0, implicit $sp, implicit $xzr, implicit $fp {
46+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $sp, implicit-def dead $x0, implicit-def $fp, implicit $x0, implicit $sp, implicit $xzr, implicit $fp {
4747
# CHECK-NEXT: BLR $x0, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
4848
# CHECK-NEXT: ORRXrs $xzr, $fp, 0
4949
# CHECK-NEXT: BL @attachedcall, implicit-def $lr, implicit internal $sp
@@ -66,7 +66,7 @@ body: |
6666
# CHECK: bb.0:
6767
# CHECK-NEXT: liveins:
6868
# CHECK-NEXT: {{ $}}
69-
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit-def dead $x0, implicit-def $fp, implicit-def $w29, implicit-def $w29_hi, implicit $sp, implicit $x0, implicit $xzr, implicit $fp {
69+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $sp, implicit-def dead $x0, implicit-def $fp, implicit $sp, implicit $x0, implicit $xzr, implicit $fp {
7070
# CHECK-NEXT: BL @foo, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $x0, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
7171
# CHECK-NEXT: $fp = ORRXrs $xzr, $fp, 0
7272
# CHECK-NEXT: BL @attachedcall, implicit-def $lr, implicit internal $sp
@@ -86,7 +86,7 @@ body: |
8686
# CHECK: bb.0:
8787
# CHECK-NEXT: liveins:
8888
# CHECK-NEXT: {{ $}}
89-
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit-def $x0, implicit-def $w0, implicit-def $w0_hi, implicit-def $fp, implicit-def $w29, implicit-def $w29_hi, implicit $sp, implicit $x0, implicit $x1, implicit $x2, implicit $xzr, implicit $fp {
89+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $sp, implicit-def $x0, implicit-def $fp, implicit $sp, implicit $x0, implicit $x1, implicit $x2, implicit $xzr, implicit $fp {
9090
# CHECK-NEXT: BL @foo, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $x0, implicit $x1, implicit $x2, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def $x0
9191
# CHECK-NEXT: $fp = ORRXrs $xzr, $fp, 0
9292
# CHECK-NEXT: BL @attachedcall, implicit-def $lr, implicit internal $sp
@@ -106,7 +106,7 @@ body: |
106106
# CHECK: bb.0:
107107
# CHECK-NEXT: liveins:
108108
# CHECK-NEXT: {{ $}}
109-
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit-def dead $x0, implicit-def $fp, implicit-def $w29, implicit-def $w29_hi, implicit $sp, implicit $w0, implicit $w1, implicit $xzr, implicit $fp {
109+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $sp, implicit-def dead $x0, implicit-def $fp, implicit $sp, implicit $w0, implicit $w1, implicit $xzr, implicit $fp {
110110
# CHECK-NEXT: BL @foo, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $w0, implicit $w1, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
111111
# CHECK-NEXT: $fp = ORRXrs $xzr, $fp, 0
112112
# CHECK-NEXT: BL @attachedcall, implicit-def $lr, implicit internal $sp
@@ -127,7 +127,7 @@ body: |
127127
# CHECK: bb.0:
128128
# CHECK-NEXT: liveins:
129129
# CHECK-NEXT: {{ $}}
130-
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit-def dead $x0, implicit-def $fp, implicit-def $w29, implicit-def $w29_hi, implicit $x8, implicit $sp, implicit $w0, implicit $w1, implicit $xzr, implicit $fp {
130+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $sp, implicit-def dead $x0, implicit-def $fp, implicit $x8, implicit $sp, implicit $w0, implicit $w1, implicit $xzr, implicit $fp {
131131
# CHECK-NEXT: BLR $x8, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $w0, implicit $w1, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
132132
# CHECK-NEXT: $fp = ORRXrs $xzr, $fp, 0
133133
# CHECK-NEXT: BL @attachedcall, implicit-def $lr, implicit internal $sp
@@ -149,7 +149,7 @@ body: |
149149
# CHECK: bb.0:
150150
# CHECK-NEXT: liveins:
151151
# CHECK-NEXT: {{ $}}
152-
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit-def dead $x0, implicit-def $fp, implicit-def $w29, implicit-def $w29_hi, implicit $sp, implicit undef $x0, implicit $xzr, implicit $fp {
152+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $sp, implicit-def dead $x0, implicit-def $fp, implicit $sp, implicit undef $x0, implicit $xzr, implicit $fp {
153153
# CHECK-NEXT: BL @foo, csr_darwin_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit undef $x0, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
154154
# CHECK-NEXT: $fp = ORRXrs $xzr, $fp, 0
155155
# CHECK-NEXT: BL @objc_retainAutoreleasedReturnValue, implicit-def $lr, implicit internal $sp
@@ -170,7 +170,7 @@ body: |
170170
# CHECK: bb.0:
171171
# CHECK-NEXT: liveins:
172172
# CHECK-NEXT: {{ $}}
173-
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $w30_hi, implicit-def $sp, implicit-def $wsp, implicit-def $wsp_hi, implicit-def dead $x0, implicit $x0, implicit $sp {
173+
# CHECK-NEXT: BUNDLE implicit-def $lr, implicit-def $sp, implicit-def dead $x0, implicit $x0, implicit $sp {
174174
# CHECK-NEXT: BLR $x0, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def dead $x0
175175
# CHECK-NEXT: BL @attachedcall, implicit-def $lr, implicit internal $sp
176176
# CHECK-NEXT: }

llvm/test/CodeGen/AArch64/sve-intrinsics-int-arith-merging.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ body: |
2828
; CHECK-LABEL: name: bic_i16_zero
2929
; CHECK: liveins: $p0, $z0
3030
; CHECK-NEXT: {{ $}}
31-
; CHECK-NEXT: BUNDLE implicit-def $z0, implicit-def $q0, implicit-def $d0, implicit-def $s0, implicit-def $h0, implicit-def $b0, implicit-def $b0_hi, implicit-def $h0_hi, implicit-def $s0_hi, implicit-def $d0_hi, implicit-def $q0_hi, implicit killed $p0, implicit $z0 {
31+
; CHECK-NEXT: BUNDLE implicit-def $z0, implicit killed $p0, implicit $z0 {
3232
; CHECK-NEXT: $z0 = MOVPRFX_ZPzZ_H $p0, $z0
3333
; CHECK-NEXT: $z0 = LSL_ZPmI_H killed renamable $p0, internal $z0, 0
3434
; CHECK-NEXT: $z0 = BIC_ZPmZ_H killed renamable $p0, internal killed $z0, internal killed renamable $z0

llvm/test/CodeGen/AArch64/sve-intrinsics-int-binaryComm-merging.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ body: |
2828
; CHECK-LABEL: name: fmul_float_zero
2929
; CHECK: liveins: $p0, $z0
3030
; CHECK-NEXT: {{ $}}
31-
; CHECK-NEXT: BUNDLE implicit-def $z0, implicit-def $q0, implicit-def $d0, implicit-def $s0, implicit-def $h0, implicit-def $b0, implicit-def $b0_hi, implicit-def $h0_hi, implicit-def $s0_hi, implicit-def $d0_hi, implicit-def $q0_hi, implicit $p0, implicit $z0 {
31+
; CHECK-NEXT: BUNDLE implicit-def $z0, implicit $p0, implicit $z0 {
3232
; CHECK-NEXT: $z0 = MOVPRFX_ZPzZ_S $p0, $z0
3333
; CHECK-NEXT: $z0 = LSL_ZPmI_S renamable $p0, internal $z0, 0
3434
; CHECK-NEXT: $z0 = FMUL_ZPmZ_S renamable $p0, internal killed $z0, internal killed renamable $z0

llvm/test/CodeGen/AArch64/sve-intrinsics-int-binaryCommWithRev-merging.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ body: |
3030
; CHECK-LABEL: name: fsub_s_zero
3131
; CHECK: liveins: $p0, $z0
3232
; CHECK-NEXT: {{ $}}
33-
; CHECK-NEXT: BUNDLE implicit-def $z0, implicit-def $q0, implicit-def $d0, implicit-def $s0, implicit-def $h0, implicit-def $b0, implicit-def $b0_hi, implicit-def $h0_hi, implicit-def $s0_hi, implicit-def $d0_hi, implicit-def $q0_hi, implicit $p0, implicit $z0 {
33+
; CHECK-NEXT: BUNDLE implicit-def $z0, implicit $p0, implicit $z0 {
3434
; CHECK-NEXT: $z0 = MOVPRFX_ZPzZ_S $p0, $z0
3535
; CHECK-NEXT: $z0 = LSL_ZPmI_S renamable $p0, internal $z0, 0
3636
; CHECK-NEXT: $z0 = FSUBR_ZPmZ_S renamable $p0, internal killed $z0, internal killed renamable $z0
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -run-pass=si-insert-hard-clauses -verify-machineinstrs %s -o - | FileCheck -check-prefix=GCN-CLAUSE %s
3+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -run-pass=si-post-ra-bundler -verify-machineinstrs %s -o - | FileCheck -check-prefix=GCN-BUNDLE %s
4+
5+
---
6+
name: clause_implicit_def
7+
tracksRegLiveness: true
8+
body: |
9+
bb.0:
10+
liveins: $vgpr0, $sgpr32
11+
12+
; GCN-CLAUSE-LABEL: name: clause_implicit_def
13+
; GCN-CLAUSE: liveins: $vgpr0, $sgpr32
14+
; GCN-CLAUSE-NEXT: {{ $}}
15+
; GCN-CLAUSE-NEXT: BUNDLE implicit-def $vgpr2, implicit-def $vgpr3, implicit $vgpr0, implicit $sgpr32, implicit $exec, implicit $flat_scr {
16+
; GCN-CLAUSE-NEXT: S_CLAUSE 1
17+
; GCN-CLAUSE-NEXT: SCRATCH_STORE_DWORD_SADDR $vgpr0, $sgpr32, 8, 0, implicit $exec, implicit $flat_scr
18+
; GCN-CLAUSE-NEXT: $vgpr2 = IMPLICIT_DEF
19+
; GCN-CLAUSE-NEXT: $vgpr3 = IMPLICIT_DEF
20+
; GCN-CLAUSE-NEXT: SCRATCH_STORE_DWORDX2_SADDR internal $vgpr2_vgpr3, $sgpr32, 16, 0, implicit $exec, implicit $flat_scr
21+
; GCN-CLAUSE-NEXT: }
22+
;
23+
; GCN-BUNDLE-LABEL: name: clause_implicit_def
24+
; GCN-BUNDLE: liveins: $vgpr0, $sgpr32
25+
; GCN-BUNDLE-NEXT: {{ $}}
26+
; GCN-BUNDLE-NEXT: BUNDLE implicit-def $vgpr2, implicit-def $vgpr3, implicit $vgpr0, implicit $sgpr32, implicit $exec, implicit $flat_scr {
27+
; GCN-BUNDLE-NEXT: SCRATCH_STORE_DWORD_SADDR $vgpr0, $sgpr32, 8, 0, implicit $exec, implicit $flat_scr
28+
; GCN-BUNDLE-NEXT: $vgpr2 = IMPLICIT_DEF
29+
; GCN-BUNDLE-NEXT: $vgpr3 = IMPLICIT_DEF
30+
; GCN-BUNDLE-NEXT: SCRATCH_STORE_DWORDX2_SADDR internal $vgpr2_vgpr3, $sgpr32, 16, 0, implicit $exec, implicit $flat_scr
31+
; GCN-BUNDLE-NEXT: }
32+
SCRATCH_STORE_DWORD_SADDR $vgpr0, $sgpr32, 8, 0, implicit $exec, implicit $flat_scr
33+
$vgpr2 = IMPLICIT_DEF
34+
$vgpr3 = IMPLICIT_DEF
35+
SCRATCH_STORE_DWORDX2_SADDR $vgpr2_vgpr3, $sgpr32, 16, 0, implicit $exec, implicit $flat_scr
36+
...

llvm/test/CodeGen/AMDGPU/finalizebundle.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ body: |
99
; CHECK-LABEL: name: test_overlap
1010
; CHECK: liveins: $vgpr0_vgpr1
1111
; CHECK-NEXT: {{ $}}
12-
; CHECK-NEXT: BUNDLE implicit-def $vgpr2_vgpr3, implicit-def $vgpr2, implicit-def $vgpr2_lo16, implicit-def $vgpr2_hi16, implicit-def $vgpr3, implicit-def $vgpr3_lo16, implicit-def $vgpr3_hi16, implicit-def $vgpr3_vgpr4, implicit-def $vgpr4, implicit-def $vgpr4_lo16, implicit-def $vgpr4_hi16, implicit $vgpr0_vgpr1, implicit $exec, implicit $vgpr1_vgpr2 {
12+
; CHECK-NEXT: BUNDLE implicit-def $vgpr2_vgpr3, implicit-def $vgpr3_vgpr4, implicit $vgpr0_vgpr1, implicit $exec, implicit $vgpr1_vgpr2 {
1313
; CHECK-NEXT: $vgpr2_vgpr3 = V_LSHLREV_B64_pseudo_e32 1, $vgpr0_vgpr1, implicit $exec
1414
; CHECK-NEXT: $vgpr3_vgpr4 = V_LSHLREV_B64_pseudo_e32 1, $vgpr1_vgpr2, implicit $exec
1515
; CHECK-NEXT: }

0 commit comments

Comments
 (0)