Skip to content

Commit 3f8ef57

Browse files
committed
MachineSink: Fix sinking VGPR def out of a divergent loop
This fixes sinking a VGPR def out of a loop past the reconvergence point at the SI_END_CF. There was a prior fix which introduced blockPrologueInterferes (D121277) to fix the same basic problem for the post RA sink. This also had the special case isIgnorableUse case which was incorrect, because in some contexts the exec use is not ignorable. I'm thinking about a new way to represent this which will avoid needing hasIgnorableUse and isBasicBlockPrologue, which would function more like the exception handling. Fixes: SWDEV-407790 https://reviews.llvm.org/D155343
1 parent d5ab379 commit 3f8ef57

4 files changed

+14
-7
lines changed

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ static bool blockPrologueInterferes(const MachineBasicBlock *BB,
288288
if (!Reg)
289289
continue;
290290
if (MO.isUse()) {
291-
if (Reg.isPhysical() &&
292-
(TII->isIgnorableUse(MO) || (MRI && MRI->isConstantPhysReg(Reg))))
291+
if (Reg.isPhysical() && MRI && MRI->isConstantPhysReg(Reg))
293292
continue;
294293
if (PI->modifiesRegister(Reg, TRI))
295294
return true;
@@ -1007,16 +1006,24 @@ MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
10071006
if (MBB == SuccToSinkTo)
10081007
return nullptr;
10091008

1009+
if (!SuccToSinkTo)
1010+
return nullptr;
1011+
10101012
// It's not safe to sink instructions to EH landing pad. Control flow into
10111013
// landing pad is implicitly defined.
1012-
if (SuccToSinkTo && SuccToSinkTo->isEHPad())
1014+
if (SuccToSinkTo->isEHPad())
10131015
return nullptr;
10141016

10151017
// It ought to be okay to sink instructions into an INLINEASM_BR target, but
10161018
// only if we make sure that MI occurs _before_ an INLINEASM_BR instruction in
10171019
// the source block (which this code does not yet do). So for now, forbid
10181020
// doing so.
1019-
if (SuccToSinkTo && SuccToSinkTo->isInlineAsmBrIndirectTarget())
1021+
if (SuccToSinkTo->isInlineAsmBrIndirectTarget())
1022+
return nullptr;
1023+
1024+
MachineBasicBlock::const_iterator InsertPos =
1025+
SuccToSinkTo->SkipPHIsAndLabels(SuccToSinkTo->begin());
1026+
if (blockPrologueInterferes(SuccToSinkTo, InsertPos, MI, TRI, TII, MRI))
10201027
return nullptr;
10211028

10221029
return SuccToSinkTo;

llvm/test/CodeGen/AMDGPU/machine-sink-loop-var-out-of-divergent-loop-swdev407790.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ define void @machinesink_loop_variable_out_of_divergent_loop(i32 %arg, i1 %cmp49
2121
; CHECK-NEXT: .LBB0_1: ; %Flow
2222
; CHECK-NEXT: ; in Loop: Header=BB0_3 Depth=1
2323
; CHECK-NEXT: s_or_b32 exec_lo, exec_lo, s8
24-
; CHECK-NEXT: v_add_nc_u32_e32 v4, -4, v4
2524
; CHECK-NEXT: .LBB0_2: ; %Flow1
2625
; CHECK-NEXT: ; in Loop: Header=BB0_3 Depth=1
2726
; CHECK-NEXT: s_or_b32 exec_lo, exec_lo, s7
@@ -54,6 +53,7 @@ define void @machinesink_loop_variable_out_of_divergent_loop(i32 %arg, i1 %cmp49
5453
; CHECK-NEXT: ;;#ASMEND
5554
; CHECK-NEXT: v_add_nc_u32_e32 v4, s9, v2
5655
; CHECK-NEXT: v_cmp_ge_u32_e64 s4, v4, v0
56+
; CHECK-NEXT: v_add_nc_u32_e32 v4, -4, v4
5757
; CHECK-NEXT: s_or_b32 s8, s4, s8
5858
; CHECK-NEXT: s_andn2_b32 exec_lo, exec_lo, s8
5959
; CHECK-NEXT: s_cbranch_execz .LBB0_1

llvm/test/CodeGen/AMDGPU/machine-sink-loop-var-out-of-divergent-loop-swdev407790.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ body: |
4242
; CHECK-NEXT: successors: %bb.2(0x40000000), %bb.5(0x40000000)
4343
; CHECK-NEXT: {{ $}}
4444
; CHECK-NEXT: INLINEASM &"", 1 /* sideeffect attdialect */
45+
; CHECK-NEXT: [[V_ADD_U32_e64_:%[0-9]+]]:vgpr_32 = V_ADD_U32_e64 [[COPY]], [[COPY1]], 0, implicit $exec
4546
; CHECK-NEXT: [[SI_IF_BREAK:%[0-9]+]]:sreg_32 = SI_IF_BREAK killed [[SI_IF1]], [[SI_IF]], implicit-def dead $scc
4647
; CHECK-NEXT: SI_LOOP [[SI_IF_BREAK]], %bb.2, implicit-def dead $exec, implicit-def dead $scc, implicit $exec
4748
; CHECK-NEXT: S_BRANCH %bb.5
@@ -51,7 +52,6 @@ body: |
5152
; CHECK-NEXT: {{ $}}
5253
; CHECK-NEXT: [[PHI:%[0-9]+]]:vgpr_32 = PHI [[COPY]], %bb.4
5354
; CHECK-NEXT: SI_END_CF [[SI_IF_BREAK]], implicit-def dead $exec, implicit-def dead $scc, implicit $exec
54-
; CHECK-NEXT: [[V_ADD_U32_e64_:%[0-9]+]]:vgpr_32 = V_ADD_U32_e64 [[COPY]], [[COPY1]], 0, implicit $exec
5555
; CHECK-NEXT: INLINEASM &"", 1 /* sideeffect attdialect */, implicit [[V_ADD_U32_e64_]]
5656
; CHECK-NEXT: S_BRANCH %bb.2
5757
; CHECK-NEXT: {{ $}}

llvm/test/CodeGen/AMDGPU/sink-after-control-flow.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ body: |
1717
; GFX10-NEXT: {{ $}}
1818
; GFX10-NEXT: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
1919
; GFX10-NEXT: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 8
20+
; GFX10-NEXT: [[V_LSHRREV_B32_e64_:%[0-9]+]]:vgpr_32 = V_LSHRREV_B32_e64 [[S_MOV_B32_]], [[DEF]], implicit $exec
2021
; GFX10-NEXT: [[V_BFE_U32_e64_:%[0-9]+]]:vgpr_32 = V_BFE_U32_e64 [[DEF]], 8, 5, implicit $exec
2122
; GFX10-NEXT: [[S_MOV_B32_1:%[0-9]+]]:sreg_32 = S_MOV_B32 5
2223
; GFX10-NEXT: [[V_CMP_NE_U32_e64_:%[0-9]+]]:sreg_32 = V_CMP_NE_U32_e64 [[V_BFE_U32_e64_]], killed [[S_MOV_B32_1]], implicit $exec
@@ -37,7 +38,6 @@ body: |
3738
; GFX10-NEXT: successors: %bb.3(0x40000000), %bb.4(0x40000000)
3839
; GFX10-NEXT: {{ $}}
3940
; GFX10-NEXT: $exec_lo = S_OR_B32 $exec_lo, [[S_XOR_B32_1]], implicit-def $scc
40-
; GFX10-NEXT: [[V_LSHRREV_B32_e64_:%[0-9]+]]:vgpr_32 = V_LSHRREV_B32_e64 [[S_MOV_B32_]], [[DEF]], implicit $exec
4141
; GFX10-NEXT: [[S_MOV_B32_2:%[0-9]+]]:sreg_32 = S_MOV_B32 31
4242
; GFX10-NEXT: [[V_CMP_NE_U32_e64_1:%[0-9]+]]:sreg_32 = V_CMP_NE_U32_e64 [[V_BFE_U32_e64_]], killed [[S_MOV_B32_2]], implicit $exec
4343
; GFX10-NEXT: [[S_XOR_B32_2:%[0-9]+]]:sreg_32 = S_XOR_B32 [[V_CMP_NE_U32_e64_1]], -1, implicit-def $scc

0 commit comments

Comments
 (0)