Skip to content

Commit b452ca5

Browse files
committed
[AMDGPU] SIWholeQuadMode: avoid execz effects in exact regions
Exact mode regions within WQM may have EXEC=0 in divergent control flow. This occurs if a branch is only taken by helper lanes and an instruction requiring WQM disabling is encountered. The current code extends the exact region as far as possible; however, this can result in it including instructions with unwanted side effects at EXEC=0. In particular readfirstlane combined with scalar loads can produce invalid memory accesses in this circumstance. Workaround this by shrinking exact regions to only the instructions requiring WQM disabling when unwanted side effects are present. Eventually we should branch over these regions when EXEC=0, but this requires visibility of CFG/divergence information not currently available.
1 parent 79996cd commit b452ca5

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

llvm/lib/Target/AMDGPU/SIWholeQuadMode.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,26 @@ void SIWholeQuadMode::processBlock(MachineBasicBlock &MBB, bool IsEntry) {
13641364
llvm_unreachable("Unknown state");
13651365
break;
13661366
}
1367+
char StartState = State & StateStrict ? NonStrictState : State;
1368+
bool WQMToExact =
1369+
StartState == StateWQM && (Needs & StateExact) && !(Needs & StateWQM);
1370+
bool ExactToWQM = StartState == StateExact && (Needs & StateWQM) &&
1371+
!(Needs & StateExact);
1372+
bool PreferLast = Needs == StateWQM;
1373+
// Exact regions in divergent control flow may run at EXEC=0, so try to
1374+
// exclude instructions with unexpected effects from them.
1375+
// FIXME: ideally we would branch over these when EXEC=0,
1376+
// but this requires updating implicit values, live intervals and CFG.
1377+
if (WQMToExact || ExactToWQM) {
1378+
for (MachineBasicBlock::iterator I = First; I != II; ++I) {
1379+
if (TII->hasUnwantedEffectsWhenEXECEmpty(*I)) {
1380+
PreferLast = WQMToExact;
1381+
break;
1382+
}
1383+
}
1384+
}
13671385
MachineBasicBlock::iterator Before =
1368-
prepareInsertion(MBB, First, II, Needs == StateWQM, SaveSCC);
1386+
prepareInsertion(MBB, First, II, PreferLast, SaveSCC);
13691387

13701388
if (State & StateStrict) {
13711389
assert(State == StateStrictWWM || State == StateStrictWQM);
@@ -1385,18 +1403,16 @@ void SIWholeQuadMode::processBlock(MachineBasicBlock &MBB, bool IsEntry) {
13851403

13861404
toStrictMode(MBB, Before, SavedNonStrictReg, Needs);
13871405
State = Needs;
1388-
13891406
} else {
1390-
if (State == StateWQM && (Needs & StateExact) && !(Needs & StateWQM)) {
1407+
if (WQMToExact) {
13911408
if (!WQMFromExec && (OutNeeds & StateWQM)) {
13921409
assert(!SavedWQMReg);
13931410
SavedWQMReg = MRI->createVirtualRegister(BoolRC);
13941411
}
13951412

13961413
toExact(MBB, Before, SavedWQMReg);
13971414
State = StateExact;
1398-
} else if (State == StateExact && (Needs & StateWQM) &&
1399-
!(Needs & StateExact)) {
1415+
} else if (ExactToWQM) {
14001416
assert(WQMFromExec == (SavedWQMReg == 0));
14011417

14021418
toWQM(MBB, Before, SavedWQMReg);

llvm/test/CodeGen/AMDGPU/wqm.ll

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,13 +3551,13 @@ define amdgpu_ps float @short_exact_regions(<8 x i32> inreg %rsrc, <4 x i32> inr
35513551
; GFX9-W64-NEXT: s_and_saveexec_b64 s[14:15], vcc
35523552
; GFX9-W64-NEXT: s_cbranch_execz .LBB59_2
35533553
; GFX9-W64-NEXT: ; %bb.1: ; %if
3554-
; GFX9-W64-NEXT: s_and_saveexec_b64 s[16:17], s[12:13]
35553554
; GFX9-W64-NEXT: global_load_dword v0, v[1:2], off
35563555
; GFX9-W64-NEXT: s_waitcnt vmcnt(0)
3557-
; GFX9-W64-NEXT: v_readfirstlane_b32 s18, v0
3558-
; GFX9-W64-NEXT: s_buffer_load_dword s18, s[8:11], s18 offset:0x0
3556+
; GFX9-W64-NEXT: v_readfirstlane_b32 s16, v0
3557+
; GFX9-W64-NEXT: s_buffer_load_dword s16, s[8:11], s16 offset:0x0
35593558
; GFX9-W64-NEXT: s_waitcnt lgkmcnt(0)
3560-
; GFX9-W64-NEXT: v_mov_b32_e32 v0, s18
3559+
; GFX9-W64-NEXT: v_mov_b32_e32 v0, s16
3560+
; GFX9-W64-NEXT: s_and_saveexec_b64 s[16:17], s[12:13]
35613561
; GFX9-W64-NEXT: buffer_store_dwordx4 v[3:6], v0, s[0:3], 0 idxen
35623562
; GFX9-W64-NEXT: s_mov_b64 exec, s[16:17]
35633563
; GFX9-W64-NEXT: .LBB59_2: ; %endif
@@ -3581,13 +3581,13 @@ define amdgpu_ps float @short_exact_regions(<8 x i32> inreg %rsrc, <4 x i32> inr
35813581
; GFX10-W32-NEXT: v_cmpx_gt_u32_e32 16, v0
35823582
; GFX10-W32-NEXT: s_cbranch_execz .LBB59_2
35833583
; GFX10-W32-NEXT: ; %bb.1: ; %if
3584-
; GFX10-W32-NEXT: s_and_saveexec_b32 s14, s12
35853584
; GFX10-W32-NEXT: global_load_dword v0, v[1:2], off
35863585
; GFX10-W32-NEXT: s_waitcnt vmcnt(0)
3587-
; GFX10-W32-NEXT: v_readfirstlane_b32 s15, v0
3588-
; GFX10-W32-NEXT: s_buffer_load_dword s15, s[8:11], s15 offset:0x0
3586+
; GFX10-W32-NEXT: v_readfirstlane_b32 s14, v0
3587+
; GFX10-W32-NEXT: s_buffer_load_dword s14, s[8:11], s14 offset:0x0
35893588
; GFX10-W32-NEXT: s_waitcnt lgkmcnt(0)
3590-
; GFX10-W32-NEXT: v_mov_b32_e32 v0, s15
3589+
; GFX10-W32-NEXT: v_mov_b32_e32 v0, s14
3590+
; GFX10-W32-NEXT: s_and_saveexec_b32 s14, s12
35913591
; GFX10-W32-NEXT: buffer_store_dwordx4 v[3:6], v0, s[0:3], 0 idxen
35923592
; GFX10-W32-NEXT: s_mov_b32 exec_lo, s14
35933593
; GFX10-W32-NEXT: .LBB59_2: ; %endif
@@ -3633,38 +3633,37 @@ define amdgpu_ps float @short_exact_regions_2(<8 x i32> inreg %rsrc, <4 x i32> i
36333633
; GFX9-W64-NEXT: s_mov_b64 s[12:13], exec
36343634
; GFX9-W64-NEXT: s_wqm_b64 exec, exec
36353635
; GFX9-W64-NEXT: image_sample v[3:4], v0, s[0:7], s[8:11] dmask:0x3
3636-
; GFX9-W64-NEXT: s_and_b64 exec, exec, s[12:13]
3637-
; GFX9-W64-NEXT: global_load_dword v0, v[1:2], off
3638-
; GFX9-W64-NEXT: s_waitcnt vmcnt(1)
3639-
; GFX9-W64-NEXT: image_sample v5, v3, s[0:7], s[8:11] dmask:0x4
3640-
; GFX9-W64-NEXT: ; kill: killed $sgpr0_sgpr1_sgpr2_sgpr3_sgpr4_sgpr5_sgpr6 killed $sgpr7
3641-
; GFX9-W64-NEXT: ; kill: killed $vgpr3
3636+
; GFX9-W64-NEXT: global_load_dword v5, v[1:2], off
3637+
; GFX9-W64-NEXT: ; kill: killed $vgpr0
36423638
; GFX9-W64-NEXT: ; kill: killed $vgpr1 killed $vgpr2
36433639
; GFX9-W64-NEXT: s_waitcnt vmcnt(1)
3644-
; GFX9-W64-NEXT: v_readfirstlane_b32 s0, v0
3640+
; GFX9-W64-NEXT: image_sample v0, v3, s[0:7], s[8:11] dmask:0x4
3641+
; GFX9-W64-NEXT: s_waitcnt vmcnt(1)
3642+
; GFX9-W64-NEXT: v_readfirstlane_b32 s0, v5
36453643
; GFX9-W64-NEXT: s_buffer_load_dword s0, s[8:11], s0 offset:0x0
36463644
; GFX9-W64-NEXT: s_waitcnt vmcnt(0)
3647-
; GFX9-W64-NEXT: v_add_f32_e32 v0, v4, v5
3645+
; GFX9-W64-NEXT: v_add_f32_e32 v0, v4, v0
36483646
; GFX9-W64-NEXT: s_waitcnt lgkmcnt(0)
36493647
; GFX9-W64-NEXT: v_add_f32_e32 v0, s0, v0
3648+
; GFX9-W64-NEXT: s_and_b64 exec, exec, s[12:13]
36503649
; GFX9-W64-NEXT: ; return to shader part epilog
36513650
;
36523651
; GFX10-W32-LABEL: short_exact_regions_2:
36533652
; GFX10-W32: ; %bb.0: ; %main_body
36543653
; GFX10-W32-NEXT: s_mov_b32 s12, exec_lo
36553654
; GFX10-W32-NEXT: s_wqm_b32 exec_lo, exec_lo
36563655
; GFX10-W32-NEXT: image_sample v[3:4], v0, s[0:7], s[8:11] dmask:0x3 dim:SQ_RSRC_IMG_1D
3657-
; GFX10-W32-NEXT: s_and_b32 exec_lo, exec_lo, s12
36583656
; GFX10-W32-NEXT: global_load_dword v0, v[1:2], off
36593657
; GFX10-W32-NEXT: s_waitcnt vmcnt(1)
36603658
; GFX10-W32-NEXT: image_sample v1, v3, s[0:7], s[8:11] dmask:0x4 dim:SQ_RSRC_IMG_1D
36613659
; GFX10-W32-NEXT: s_waitcnt vmcnt(1)
36623660
; GFX10-W32-NEXT: v_readfirstlane_b32 s0, v0
3661+
; GFX10-W32-NEXT: s_buffer_load_dword s0, s[8:11], s0 offset:0x0
36633662
; GFX10-W32-NEXT: s_waitcnt vmcnt(0)
36643663
; GFX10-W32-NEXT: v_add_f32_e32 v0, v4, v1
3665-
; GFX10-W32-NEXT: s_buffer_load_dword s0, s[8:11], s0 offset:0x0
36663664
; GFX10-W32-NEXT: s_waitcnt lgkmcnt(0)
36673665
; GFX10-W32-NEXT: v_add_f32_e32 v0, s0, v0
3666+
; GFX10-W32-NEXT: s_and_b32 exec_lo, exec_lo, s12
36683667
; GFX10-W32-NEXT: ; return to shader part epilog
36693668
main_body:
36703669
%tex1 = call <4 x float> @llvm.amdgcn.image.sample.1d.v4f32.f32(i32 15, float %c, <8 x i32> %rsrc, <4 x i32> %sampler, i1 false, i32 0, i32 0) #0

0 commit comments

Comments
 (0)