rdna2spirv: quad_perm DPP in a pixel shader reads its quad through quad operations (SHADER-PS-DPP-QUAD-OPS) #2416

Closed
ninjahawk wants to merge 2 commits from upstream-pr/2416 into main
ninjahawk commented 2026-11-27 02:05:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ninjahawk on 2026-11-27T02:05:58Z.


Claimed in the coordination issue as: SHADER-PS-DPP-QUAD-OPS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7207494420), from Snooozs's note in the same issue (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7198062450)

What and why

Gen.dpp_insn lowered every quad_perm to OpGroupNonUniformShuffle, and with fi:0 it read the source lane's EXEC bit through OpGroupNonUniformBallot. Without MaximallyReconvergesKHR, Vulkan ("Helper Invocations") lets an implementation treat helper invocations as inactive for every group operation other than quad and derivative ones. So in a pixel shader, a lane whose quad neighbour is a helper (a pixel at a triangle edge) could read an undefined value, or miss the neighbour's EXEC bit. The s_wqm path already uses OpGroupNonUniformQuadSwap for this reason.

In fragment shaders (not under --wave64-emulate, which already uses the workgroup exchange), quad_perm now reads its quad through quad operations:

  • [1,0,3,2], [2,3,0,1] and [3,2,1,0] become one OpGroupNonUniformQuadSwap (direction 0, 1, 2);
  • any other selector becomes one OpGroupNonUniformQuadBroadcast per lane it reads (constant index), then a select on the lane's selector. A broadcast like [2,2,2,2] is a single op;
  • [0,1,2,3] reads its own lane, with no group op;
  • with fi:0, the source lane's EXEC bit goes through the same quad read instead of a ballot.

Each quad result is stored eagerly (Gen.eager), as the shuffle was, so SPIRV-Cross cannot inline it into a conditional (SHADER-529). Vertex, geometry, mesh and compute shaders keep the shuffle. Quad operations are only guaranteed in fragment and compute shaders, and compute has no helpers.

docs/design.md's DPP row describes the new rule and says how it was checked.

Tests:

  • tests/semantic/spirv_wave_interp.py gets a helpers= option, the strict reading of the spec. Helper lanes take part in quad ops. A shuffle or broadcast from one is undefined, a ballot does not see it, any other group op they reach raises UnsupportedConstruct, and their outputs are discarded.
  • New tests/semantic/test_dpp_quad_ops_ps.py, all hand-written programs:
    • It covers 12 quad_perm selectors: the three swaps, the four broadcasts, the pair and half broadcasts [1,1,3,3], [2,3,2,3], [0,0,2,2], [0,1,0,1], and [3,1,2,0] (all four lanes, no pattern).
    • Each runs with fi/bound_ctrl 1/1, 0/1 and 0/0, with lane 5 on or off in EXEC, and is checked lane by lane against the RDNA2 rule.
    • The same programs run with lanes 2, 5 and 12 as helpers.
    • It also checks that there is no shuffle or ballot, that SPIRV-Cross cannot inline the quad ops into a conditional (conditional_subgroup_ops from test_dpp_eager_subgroup.py), and that a vertex shader keeps the shuffle.
  • tests/test_translate.py::test_dpp_controls_translate_and_validate builds pixel shaders. For its three quad_perm cases it now expects the quad ops: QuadSwap for the swap, no group op for the identity.

Tested

Windows 11, CPython 3.12.13, CPU only, no game files, on main d1dff7949. Tools: llvm-mc 21.1.8 (the CI pin; conda-forge llvm-tools) and SPIRV-Tools 2026.3 (conda-forge).

  • python -I -m unittest discover -s tests/semantic -p test_dpp_quad_ops_ps.py: 7 tests OK. With the old tools/rdna2spirv.py, 59 subtests fail. The helper case, for example, reads QuadSwap/Shuffle from invocation 2, which is not running as an undefined output.
  • LLVM_MC=<llvm-mc 21.1.8> python -I -m pytest tests/test_translate.py: 720 passed. The three quad_perm cases of test_dpp_controls_translate_and_validate fail on the old translator with the new assertion.
  • All 78 programs of the new test file, the identity included, pass spirv-as/spirv-val --target-env vulkan1.2.
  • python -I scripts/run_semantic_tests.py tests/semantic -j 8, with SPIRV-Tools on PATH and LEONIDA_REQUIRE_SPIRV_TOOLS=1: 501 files, 6,381 tests, 0 files failed (435 s). Without SPIRV-Tools: the same counts, 0 failed (810 s).
  • The change only affects pixel shaders that use quad_perm. It replaces one subgroup op with quad ops of the same capability family (GroupNonUniformQuad, which the s_wqm path already declares).

Not GPU-verified by me (CPU only). Hardware checks by reviewers in the comments: RADV 26.0.8 on gfx906 (wave64) shows wrong edge pixels on main (8,276 of 1,118,208, all from helper-neighbour reads) and none with this PR; an RTX 4090 matches the RDNA reference with both lowerings, as expected for a driver that runs helpers in shuffles.

#2362 also changes Gen.dpp_insn, in the src0 modifier handling. It conflicts with main already, so whichever lands second needs a small rebase.

GPUs tested: n/a (CPU-only tests)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5519936382 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2416). Originally posted by [ninjahawk](https://github.com/ninjahawk) on 2026-11-27T02:05:58Z. --- Claimed in the coordination issue as: SHADER-PS-DPP-QUAD-OPS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7207494420), from Snooozs's note in the same issue (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7198062450) ### What and why `Gen.dpp_insn` lowered every `quad_perm` to `OpGroupNonUniformShuffle`, and with `fi:0` it read the source lane's EXEC bit through `OpGroupNonUniformBallot`. Without `MaximallyReconvergesKHR`, Vulkan ("Helper Invocations") lets an implementation treat helper invocations as inactive for every group operation other than quad and derivative ones. So in a pixel shader, a lane whose quad neighbour is a helper (a pixel at a triangle edge) could read an undefined value, or miss the neighbour's EXEC bit. The `s_wqm` path already uses `OpGroupNonUniformQuadSwap` for this reason. In fragment shaders (not under `--wave64-emulate`, which already uses the workgroup exchange), `quad_perm` now reads its quad through quad operations: - `[1,0,3,2]`, `[2,3,0,1]` and `[3,2,1,0]` become one `OpGroupNonUniformQuadSwap` (direction 0, 1, 2); - any other selector becomes one `OpGroupNonUniformQuadBroadcast` per lane it reads (constant index), then a select on the lane's selector. A broadcast like `[2,2,2,2]` is a single op; - `[0,1,2,3]` reads its own lane, with no group op; - with `fi:0`, the source lane's EXEC bit goes through the same quad read instead of a ballot. Each quad result is stored eagerly (`Gen.eager`), as the shuffle was, so SPIRV-Cross cannot inline it into a conditional (SHADER-529). Vertex, geometry, mesh and compute shaders keep the shuffle. Quad operations are only guaranteed in fragment and compute shaders, and compute has no helpers. `docs/design.md`'s DPP row describes the new rule and says how it was checked. Tests: - `tests/semantic/spirv_wave_interp.py` gets a `helpers=` option, the strict reading of the spec. Helper lanes take part in quad ops. A shuffle or broadcast from one is undefined, a ballot does not see it, any other group op they reach raises `UnsupportedConstruct`, and their outputs are discarded. - New `tests/semantic/test_dpp_quad_ops_ps.py`, all hand-written programs: - It covers 12 `quad_perm` selectors: the three swaps, the four broadcasts, the pair and half broadcasts `[1,1,3,3]`, `[2,3,2,3]`, `[0,0,2,2]`, `[0,1,0,1]`, and `[3,1,2,0]` (all four lanes, no pattern). - Each runs with `fi`/`bound_ctrl` 1/1, 0/1 and 0/0, with lane 5 on or off in EXEC, and is checked lane by lane against the RDNA2 rule. - The same programs run with lanes 2, 5 and 12 as helpers. - It also checks that there is no shuffle or ballot, that SPIRV-Cross cannot inline the quad ops into a conditional (`conditional_subgroup_ops` from `test_dpp_eager_subgroup.py`), and that a vertex shader keeps the shuffle. - `tests/test_translate.py::test_dpp_controls_translate_and_validate` builds pixel shaders. For its three `quad_perm` cases it now expects the quad ops: `QuadSwap` for the swap, no group op for the identity. ### Tested Windows 11, CPython 3.12.13, CPU only, no game files, on `main` `d1dff7949`. Tools: llvm-mc 21.1.8 (the CI pin; conda-forge `llvm-tools`) and SPIRV-Tools 2026.3 (conda-forge). - `python -I -m unittest discover -s tests/semantic -p test_dpp_quad_ops_ps.py`: 7 tests OK. With the old `tools/rdna2spirv.py`, 59 subtests fail. The helper case, for example, reads `QuadSwap`/`Shuffle from invocation 2, which is not running` as an undefined output. - `LLVM_MC=<llvm-mc 21.1.8> python -I -m pytest tests/test_translate.py`: 720 passed. The three `quad_perm` cases of `test_dpp_controls_translate_and_validate` fail on the old translator with the new assertion. - All 78 programs of the new test file, the identity included, pass `spirv-as`/`spirv-val --target-env vulkan1.2`. - `python -I scripts/run_semantic_tests.py tests/semantic -j 8`, with SPIRV-Tools on PATH and `LEONIDA_REQUIRE_SPIRV_TOOLS=1`: 501 files, 6,381 tests, 0 files failed (435 s). Without SPIRV-Tools: the same counts, 0 failed (810 s). - The change only affects pixel shaders that use `quad_perm`. It replaces one subgroup op with quad ops of the same capability family (`GroupNonUniformQuad`, which the `s_wqm` path already declares). Not GPU-verified by me (CPU only). Hardware checks by reviewers in the comments: RADV 26.0.8 on gfx906 (wave64) shows wrong edge pixels on `main` (8,276 of 1,118,208, all from helper-neighbour reads) and none with this PR; an RTX 4090 matches the RDNA reference with both lowerings, as expected for a driver that runs helpers in shuffles. #2362 also changes `Gen.dpp_insn`, in the src0 modifier handling. It conflicts with `main` already, so whichever lands second needs a small rebase. GPUs tested: n/a (CPU-only tests) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
ecox982745 commented 2026-11-27 02:19:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-27T02:19:18Z.


Independently checked edd67b24 on a Windows RTX 4090 (NVIDIA 616.64, subgroup size 32). All 60 synthetic triangle draws matched the RDNA reference across 120,960 covered pixel results. The 60 readbacks were also byte-identical to the same draws on the base commit d1dff794. This driver therefore provides a passing hardware check for the new lowering, but it did not reproduce an edge-pixel error on the old one.

For reproduction, I used the 12 selectors in test_dpp_quad_ops_ps.py with five (fi, bound_ctrl, disabled lane) settings: (1,1,none), (0,1,none), (0,0,5), (0,1,5), (1,1,5). After restoring EXEC, the shader exported the DPP result and lane ID as floats. A 64×64 right triangle with clip-space corners (-1,-1), (1,-1), (-1,1) covered 2,016 pixels per draw. A separate GLSL draw broadcast each quad lane’s gl_HelperInvocation flag and identified the selected source as a helper on 1,416 reads across the matrix. All output modules passed spirv-val --target-env vulkan1.2; Vulkan validation reported no errors. These were synthetic shaders only.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7208020889 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2416#issuecomment-7208020889). Originally posted by [ecox982745](https://github.com/ecox982745) on 2026-11-27T02:19:18Z. --- Independently checked `edd67b24` on a Windows RTX 4090 (NVIDIA 616.64, subgroup size 32). All 60 synthetic triangle draws matched the RDNA reference across 120,960 covered pixel results. The 60 readbacks were also byte-identical to the same draws on the base commit `d1dff794`. This driver therefore provides a passing hardware check for the new lowering, but it did not reproduce an edge-pixel error on the old one. For reproduction, I used the 12 selectors in `test_dpp_quad_ops_ps.py` with five `(fi, bound_ctrl, disabled lane)` settings: (1,1,none), (0,1,none), (0,0,5), (0,1,5), (1,1,5). After restoring EXEC, the shader exported the DPP result and lane ID as floats. A 64×64 right triangle with clip-space corners (-1,-1), (1,-1), (-1,1) covered 2,016 pixels per draw. A separate GLSL draw broadcast each quad lane’s `gl_HelperInvocation` flag and identified the selected source as a helper on 1,416 reads across the matrix. All output modules passed `spirv-val --target-env vulkan1.2`; Vulkan validation reported no errors. These were synthetic shaders only.
tiguan26 commented 2026-11-27 04:43:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:43:44Z.

Review: APPROVED


Approving edd67b24f.

The three swaps map onto OpGroupNonUniformQuadSwap directions correctly ([1,0,3,2] horizontal 0, [2,3,0,1] vertical 1, [3,2,1,0] diagonal 2), and the other selectors take one QuadBroadcast per distinct lane read with constant indices and a select on the lane's selector, so a helper neighbour is read through a quad operation the spec defines for helpers. With fi:0 the source's EXEC bit goes through the same quad read, so it is the neighbour's bit, not this lane's. uses_quad adds GroupNonUniformQuad, the eager stores keep SHADER-529's every-lane execution, and stages other than pixel shaders and --wave64-emulate keep the shuffle.

Merged onto main d1dff7949 together with #2397, #2355, #2357, #2360 and #2366 (no conflicts), on Linux with Python 3.13: test_dpp_quad_ops_ps.py 7 passed (132 subtests); tests/test_translate.py passes except main's own ps_sdwa_f16_const.asm failure (an llvm-mc version difference here); the full translator suite has only the 8 failures main has here. AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083583907 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2416#pullrequestreview-6083583907). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T04:43:44Z. **Review:** APPROVED --- Approving edd67b24f. The three swaps map onto `OpGroupNonUniformQuadSwap` directions correctly ([1,0,3,2] horizontal 0, [2,3,0,1] vertical 1, [3,2,1,0] diagonal 2), and the other selectors take one `QuadBroadcast` per distinct lane read with constant indices and a select on the lane's selector, so a helper neighbour is read through a quad operation the spec defines for helpers. With `fi:0` the source's EXEC bit goes through the same quad read, so it is the neighbour's bit, not this lane's. `uses_quad` adds `GroupNonUniformQuad`, the eager stores keep SHADER-529's every-lane execution, and stages other than pixel shaders and `--wave64-emulate` keep the shuffle. Merged onto `main` `d1dff7949` together with #2397, #2355, #2357, #2360 and #2366 (no conflicts), on Linux with Python 3.13: `test_dpp_quad_ops_ps.py` 7 passed (132 subtests); `tests/test_translate.py` passes except `main`'s own `ps_sdwa_f16_const.asm` failure (an llvm-mc version difference here); the full translator suite has only the 8 failures `main` has here. AI-assisted (Claude Code, Claude Opus 5.5).
tiguan26 commented 2026-11-27 05:35:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T05:35:57Z.

Review: APPROVED


Approving. Checked on edd67b24f merged onto current main (d1dff7949).

Translator semantic suite: 501 files, the only failures are main's known test_fspx_dot4_op_sel_disasm and test_int32_to_f64. tests/test_translate.py: 719 passed, the one failure is main's ps_sdwa_f16_const.asm. As a negative check I ran the new test_dpp_quad_ops_ps against main's rdna2spirv.py: 36 failures and 23 errors, so the strict-helper interpreter does catch the shuffle/ballot reading a helper neighbour; with the PR it passes.

Read the diff. The three QUAD_SWAPS directions match the spec's OpGroupNonUniformQuadSwap (0 horizontal 1,0,3,2; 1 vertical 2,3,0,1; 2 diagonal 3,2,1,0). Every other selector does one QuadBroadcast per distinct lane read (constant Index, as required) and a select on this lane's perm, and the results go through eager, so the SHADER-529 rule holds. The source lane's EXEC bit for fi:0 also goes through the quad read rather than a ballot, which is the part a helper would otherwise break. The emulate path keeps its exchange, and compute and other stages keep the shuffle.

One note, not a blocker: there is no GPU fixture with quad_perm in a pixel shader, so nothing on a real device draws the triangle-edge case. The interpreter's strict reading is the stronger check anyway, but a small ps_ fixture drawn across an edge would show it on hardware, if anyone has a GPU that actually drops helpers from shuffles.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083914492 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2416#pullrequestreview-6083914492). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T05:35:57Z. **Review:** APPROVED --- Approving. Checked on edd67b24f merged onto current main (d1dff7949). Translator semantic suite: 501 files, the only failures are main's known `test_fspx_dot4_op_sel_disasm` and `test_int32_to_f64`. `tests/test_translate.py`: 719 passed, the one failure is main's `ps_sdwa_f16_const.asm`. As a negative check I ran the new `test_dpp_quad_ops_ps` against main's `rdna2spirv.py`: 36 failures and 23 errors, so the strict-helper interpreter does catch the shuffle/ballot reading a helper neighbour; with the PR it passes. Read the diff. The three `QUAD_SWAPS` directions match the spec's OpGroupNonUniformQuadSwap (0 horizontal 1,0,3,2; 1 vertical 2,3,0,1; 2 diagonal 3,2,1,0). Every other selector does one QuadBroadcast per distinct lane read (constant Index, as required) and a select on this lane's `perm`, and the results go through `eager`, so the SHADER-529 rule holds. The source lane's EXEC bit for fi:0 also goes through the quad read rather than a ballot, which is the part a helper would otherwise break. The emulate path keeps its exchange, and compute and other stages keep the shuffle. One note, not a blocker: there is no GPU fixture with quad_perm in a pixel shader, so nothing on a real device draws the triangle-edge case. The interpreter's strict reading is the stronger check anyway, but a small `ps_` fixture drawn across an edge would show it on hardware, if anyone has a GPU that actually drops helpers from shuffles.
zmasi commented 2026-11-27 11:11:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-27T11:11:30Z.


Confirmed a visible difference at triangle edges on fixed-wave64 gfx906, RADV 26.0.8, using hand-written shaders. Same 273 draws on each translator: 13 quad selectors × 7 EXEC/fi/bound settings × 3 triangles.

Translator Wrong pixels out of 1,118,208 Draws with any wrong pixel
Parent d1dff794 and current main 63185f4a3 (tested separately) 8,276 each 231/273 each
PR edd67b24 0 0/273

Every wrong value on main read a helper neighbour; none came from the 419,027 live-source reads. The PR matched all 9,037 helper-source reads and left every outside pixel at its clear sentinel. A separate HelperInvocation shader confirmed the edge coverage; a deliberately wrong oracle failed all 273 draws. Repeating the PR readbacks gave identical bytes. No validation errors or synchronization hazards were reported.

This exercises native fragment quad operations on this driver, not wave32 hardware or the emulation path.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7216786441 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2416#issuecomment-7216786441). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-27T11:11:30Z. --- Confirmed a visible difference at triangle edges on fixed-wave64 gfx906, RADV 26.0.8, using hand-written shaders. Same 273 draws on each translator: 13 quad selectors × 7 EXEC/fi/bound settings × 3 triangles. | Translator | Wrong pixels out of 1,118,208 | Draws with any wrong pixel | |---|---:|---:| | Parent `d1dff794` and current main `63185f4a3` (tested separately) | 8,276 each | 231/273 each | | PR `edd67b24` | 0 | 0/273 | Every wrong value on main read a helper neighbour; none came from the 419,027 live-source reads. The PR matched all 9,037 helper-source reads and left every outside pixel at its clear sentinel. A separate `HelperInvocation` shader confirmed the edge coverage; a deliberately wrong oracle failed all 273 draws. Repeating the PR readbacks gave identical bytes. No validation errors or synchronization hazards were reported. This exercises native fragment quad operations on this driver, not wave32 hardware or the emulation path. AI-assisted.
ninjahawk commented 2026-11-27 20:46:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ninjahawk on 2026-11-27T20:46:41Z.


Thanks for the hardware runs and the reviews. The gfx906 numbers show the edge-pixel case on main that I could not show on CPU, so I added them to the description, together with the 4090 result (no difference there, as expected when a driver keeps helpers in shuffles).

Pushed 1ef70658c: test-only, it restores the parametrize(...) spacing that the first commit lost in tests/test_translate.py and renames one comment in the new test. No translator change. test_dpp_quad_ops_ps.py and the DPP/quad cases of tests/test_translate.py pass again (51 passed, 132 subtests).

A small ps_ GPU fixture drawn across a triangle edge is a good follow-up; I'll leave it out of this PR to keep it to the translator change.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7227830382 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2416#issuecomment-7227830382). Originally posted by [ninjahawk](https://github.com/ninjahawk) on 2026-11-27T20:46:41Z. --- Thanks for the hardware runs and the reviews. The gfx906 numbers show the edge-pixel case on `main` that I could not show on CPU, so I added them to the description, together with the 4090 result (no difference there, as expected when a driver keeps helpers in shuffles). Pushed 1ef70658c: test-only, it restores the `parametrize(...)` spacing that the first commit lost in `tests/test_translate.py` and renames one comment in the new test. No translator change. `test_dpp_quad_ops_ps.py` and the DPP/quad cases of `tests/test_translate.py` pass again (51 passed, 132 subtests). A small `ps_` GPU fixture drawn across a triangle edge is a good follow-up; I'll leave it out of this PR to keep it to the translator change.

Pull request closed

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
ProjectLeonida/mandy!2416
No description provided.