fix(shader): wave-wide exec and vcc branch conditions #608
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/608"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T02:58:12Z.
What
Bug:
s_cbranch_execz,s_cbranch_execnz,s_cbranch_vcczands_cbranch_vccnztest the whole wave mask on hardware (RDNA2 ISA: "if (EXEC == 0) then PC = ..."), but the single-lane SPIR-V path (wave32, and wave64 on a 64-lane subgroup) branched on each invocation's own EXEC/VCC bit. With a partial mask, the inactive lanes took the other side of the branch, ran the scalar code there and came back with different "scalar" registers, so lanes that HW leaves alone saw wrong SGPR values once EXEC was restored. Example:s_and_saveexec_b32 s14, vcc_lo+s_cbranch_execnzwith 5 of 32 lanes active: HW skips the fall-through for the whole wave, AnyPS5 ran it for the 27 inactive lanes.The fix branches on a ballot of the wave in that path (
EmitWaveBallot, so a wave32 inside a 64-lane subgroup only sees its own 32 lanes): non-zero kinds = any lane set, zero kinds = no lane clear. The two-half wave64 path already did this and is unchanged. The analysis now requestsGroupNonUniformBallotfor these branches: without it, a shader whose only cross-lane op is the branch failsspirv-val("OpGroupNonUniformBallot requires one of these capabilities: GroupNonUniformBallot"; checked withs_mov_b32 exec_lo, 0x1f+s_cbranch_execz).Test
agc_driver_scalar_program_flowalso executes the SOPP ops that no test ran yet, all matching HW with main's translation (no change there):s_cbranch_execnz,s_cbranch_cdbgsys,s_cbranch_cdbguser,s_cbranch_cdbgsys_or_user,s_cbranch_cdbgsys_and_user(not taken),s_clause,s_inst_prefetch,s_setprio,s_waitcnt_depctr,s_ttracedata.s_trapis left out on purpose: main treats it as a no-op, and what it should do without a trap handler is a separate question.Behaviour measured on HW
gfx1035 oracle, wave32 and wave64, 3 random seeds each, 0 mismatches against the model: EXEC from bit k of each lane's input (random, only lane 40 = upper wave64 half only, only lane 5), then execnz/execz/vccnz/vccz with scalar code on the skipped side. All lanes read the same SGPR result, decided by "any lane set" for the whole wave. cdbg branches are never taken. The expected values in the test are the HW results.
Tested
Linux, RADV (Mesa 26.2), local lavapipe and CI's lavapipe (Mesa 25.2.8, cold: 0 s). The test runs wave32, wave64 native and wave64 split (
ComputeTarget(32)). With main'sEmitBranchConditionit fails (program flow wave32: lane 0 execnz_partial is 0x51c9e72a, expected 0x51c9bd70); restricting the fix to EXEC only fails onvccnz_partial, ignoring the ballot's high word fails onexecnz_upper_half(wave64), treating zero kinds as "any" fails onexecz_partial. On lavapipe (8-lane subgroups, narrower than a wave) only the cdbg/hint columns are checked; the branch columns skip there, like the other cross-lane tests. Full Release ctest: all pass exceptagc_driver_pixel_helper_lanes, which fails the same way on main on RADV.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T03:09:22Z.
Thanks, this is a real bug and the fix matches the ISA. I disassembled both kernels with
llvm-mc -mcpu=gfx1013: thes_and_saveexec+s_cbranch_execz/execnz/vccz/vccnzcases with scalar code on the skipped side, the fours_cbranch_cdbg*forms, and the hint ops (s_clause,s_inst_prefetch,s_setprio,s_waitcnt_depctr,s_ttracedata).In the single-lane path the branch now goes on a ballot of the wave (through
EmitWaveBallot, so a wave32 in a 64-lane subgroup only sees its own lanes): any lane set for the non-zero kinds, no lane clear for the zero kinds, which is the same rule the two-half path already used. RequestingGroupNonUniformBallotfor those branches is needed too. The new test fails on main with the row you quote (execnz_partial is 0x51c9e72a, expected 0x51c9bd70) and passes here in all three wave modes; the full ctest passes with it merged.Two notes for the merge order rather than the code:
SpirvAnalysis.cpp: both add a block just beforereturn requirements;. Keeping both is the whole fix, and if yours goes in first I'll rebase mine.s_cbranch_execzin 3,971 of the 5,688 shaders I have dumped,s_cbranch_vcczin 4,315 ands_cbranch_vccnzin 1,940. The test runs compute shaders only, and in pixel shaders this also changes which lanes reach kills and exports, so I'll run it through my visual check of PPSA21564 a few times before it goes in.CI was still running when I looked. Ready to merge from my side once it's green and that check is done.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T03:10:26Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cppL196core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cppL196Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:04:12Z.
Gated on PPSA21564 three times plus a warm-up, since most of its shaders take these branches: no visual or FPS change.