fix(shader): wave-wide exec and vcc branch conditions #608

Merged
adriadam10 merged 1 commit from upstream-pr/608 into main 2026-10-05 05:13:08 +00:00
adriadam10 commented 2026-10-05 02:58:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T02:58:12Z.


What

Bug: s_cbranch_execz, s_cbranch_execnz, s_cbranch_vccz and s_cbranch_vccnz test 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_execnz with 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 requests GroupNonUniformBallot for these branches: without it, a shader whose only cross-lane op is the branch fails spirv-val ("OpGroupNonUniformBallot requires one of these capabilities: GroupNonUniformBallot"; checked with s_mov_b32 exec_lo, 0x1f + s_cbranch_execz).

Test agc_driver_scalar_program_flow also 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_trap is 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's EmitBranchCondition it fails (program flow wave32: lane 0 execnz_partial is 0x51c9e72a, expected 0x51c9bd70); restricting the fix to EXEC only fails on vccnz_partial, ignoring the ballot's high word fails on execnz_upper_half (wave64), treating zero kinds as "any" fails on execz_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 except agc_driver_pixel_helper_lanes, which fails the same way on main on RADV.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4739625703 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/608). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T02:58:12Z. --- ### What **Bug:** `s_cbranch_execz`, `s_cbranch_execnz`, `s_cbranch_vccz` and `s_cbranch_vccnz` test 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_execnz` with 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 requests `GroupNonUniformBallot` for these branches: without it, a shader whose only cross-lane op is the branch fails `spirv-val` ("OpGroupNonUniformBallot requires one of these capabilities: GroupNonUniformBallot"; checked with `s_mov_b32 exec_lo, 0x1f` + `s_cbranch_execz`). Test `agc_driver_scalar_program_flow` also 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_trap` is 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's `EmitBranchCondition` it fails (`program flow wave32: lane 0 execnz_partial is 0x51c9e72a, expected 0x51c9bd70`); restricting the fix to EXEC only fails on `vccnz_partial`, ignoring the ballot's high word fails on `execnz_upper_half` (wave64), treating zero kinds as "any" fails on `execz_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 except `agc_driver_pixel_helper_lanes`, which fails the same way on main on RADV. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-05 03:09:22 +00:00 (Migrated from github.com)

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: the s_and_saveexec + s_cbranch_execz/execnz/vccz/vccnz cases with scalar code on the skipped side, the four s_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. Requesting GroupNonUniformBallot for 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:

  • This conflicts with my #586 in SpirvAnalysis.cpp: both add a block just before return requirements;. Keeping both is the whole fix, and if yours goes in first I'll rebase mine.
  • PPSA21564 has s_cbranch_execz in 3,971 of the 5,688 shaders I have dumped, s_cbranch_vccz in 4,315 and s_cbranch_vccnz in 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987418172 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/608#issuecomment-5987418172). Originally posted by [oneandonlydean](https://github.com/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`: the `s_and_saveexec` + `s_cbranch_execz/execnz/vccz/vccnz` cases with scalar code on the skipped side, the four `s_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. Requesting `GroupNonUniformBallot` for 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: - This conflicts with my #586 in `SpirvAnalysis.cpp`: both add a block just before `return requirements;`. Keeping both is the whole fix, and if yours goes in first I'll rebase mine. - PPSA21564 has `s_cbranch_execz` in 3,971 of the 5,688 shaders I have dumped, `s_cbranch_vccz` in 4,315 and `s_cbranch_vccnz` in 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.
github-actions[bot] commented 2026-10-05 03:10:26 +00:00 (Migrated from github.com)

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#586 core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp L196
#612 core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp L196
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987428160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/608#issuecomment-5987428160). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T03:10:26Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #586 | `core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp` [L196](https://github.com/boykopovar/AnyPS5/blob/f809199b0a88ce834977b3eba6a9ccfca390c59d/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp#L196) | | | | #612 | `core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp` [L196](https://github.com/boykopovar/AnyPS5/blob/f809199b0a88ce834977b3eba6a9ccfca390c59d/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp#L196) | | |
oneandonlydean commented 2026-10-05 05:04:12 +00:00 (Migrated from github.com)

Imported 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5988443734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/608#issuecomment-5988443734). Originally posted by [oneandonlydean](https://github.com/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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!608
No description provided.