Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:test/scalar-program-flow
Oct 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:test/scalar-program-flow

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

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

s_cbranch_execz/execnz/vccz/vccnz test the whole wave mask on hardware,
but the single-lane path branched on each lane's own bit, so inactive
lanes took the other side and diverged in scalar state. Branch on a
wave ballot instead, and execute the untested SOPP ops (cdbg branches,
s_clause, s_inst_prefetch, s_setprio, s_waitcnt_depctr, s_ttracedata)
against values measured on hardware.

Co-Authored-By: Claude <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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 fix(recompiler): size per-invocation LDS arrays to the addresses they reach #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

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

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

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Gated on PPSA21564 three times plus a warm-up, since most of its shaders take these branches: no visual or FPS change.

@oneandonlydean
oneandonlydean merged commit 50e7712 into boykopovar:main Oct 5, 2026
3 checks passed
@adriadam10
adriadam10 deleted the test/scalar-program-flow branch October 5, 2026 06:47
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 5, 2026
Conflicts resolved:
- libSceAudioPropagation/Export.cpp: upstream's boykopovar#482 + boykopovar#489 version taken; it is
  the same no-propagation model as astrobot's stand-in, with SourceRender and the
  handle-only UnregisterMaterial already in it.
- State.cpp (boykopovar#453): astrobot's resident depth model kept; it already skips the
  tests of an absent plane (boykopovar#453's test is on astrobot since e087a4f).
- Graphics.cpp, State.hpp, ShaderResources.cpp: astrobot's DepthTarget model kept
  (exportIndex, no DepthSurface.hpp), upstream's ThreadOwned include added.
- BdaResources.cpp: astrobot's form with boykopovar#390's message.
- VulkanDevice.cpp, Context.hpp, PacketExecution.cpp: both sides kept
  (calibrated timestamps, host query reset, GDS, memory budget; pipeline stats).
- Spirv: astrobot's split-wave rules first, then boykopovar#608's wave-mask ballot and
  boykopovar#587's host subgroup lane; astrobot's function LDS sizing and boykopovar#573's LDS lock
  both kept; conditionalDepth kept in the atomic update loops; upstream's
  buffer atomics (boykopovar#321 boykopovar#332 boykopovar#594) and 64-bit LDS atomics taken.
- TechnicalDebt.md: both sides, duplicate lines dropped.


Build fixes folded in: the upstream boykopovar#453 test block duplicated next to
astrobot's adapted copy removed; a lost closing brace after the host
query reset block restored; lookupLog() is a function since boykopovar#590; the
duplicate MUTEX_TYPE_ADAPTIVE constant (astrobot's 93b2d0e equals boykopovar#156)
removed; the ops boykopovar#596 drops removed from the masked-select list and the
BDA test message updated (the gate-batch-2 integ commits 559e4fab6 and
2ec968fb5).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants