Skip to content

Commit 3611c0b

Browse files
authored
[AMDGPU] SIWholeQuadMode: avoid execz effects in exact regions (#101157)
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 65c000a commit 3611c0b

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
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 && (OutNeeds & StateWQM)) || 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: 8 additions & 8 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

0 commit comments

Comments
 (0)