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

Open
github-bot wants to merge 1 commit from github-bot/gh-2416 into main AGit
Owner

@⁠ninjahawk on GitHub · 2026-11-27 02:05 UTC · original
ninjahawk:leon/ps-dpp-quad-ops → main · +214 −10 · 5 files · 1 commits · open

Claimed in the coordination issue as: SHADER-PS-DPP-QUAD-OPS (#1608), from Snooozs's finding (#1608)

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 selectors: the 10 the finding lists, plus [3,3,3,3] and [3,1,2,0].
    • 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).
  • Translated and spirv-val-valid counts over the corpus: not measured, because no game files were used. 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. I have not seen a driver give wrong output for the old lowering. This follows the spec's allowance, and on a driver that runs helpers in shuffles both lowerings give the same values.

#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.
**@⁠ninjahawk** on GitHub · 2026-11-27 02:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2416) `ninjahawk:leon/ps-dpp-quad-ops` → `main` · +214 −10 · 5 files · 1 commits · open Claimed in the coordination issue as: SHADER-PS-DPP-QUAD-OPS (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608), from Snooozs's finding (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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 selectors: the 10 the finding lists, plus `[3,3,3,3]` and `[3,1,2,0]`. - 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). - Translated and `spirv-val`-valid counts over the corpus: not measured, because no game files were used. 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. I have not seen a driver give wrong output for the old lowering. This follows the spec's allowance, and on a driver that runs helpers in shuffles both lowerings give the same values. #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.
Without MaximallyReconvergesKHR, Vulkan lets helper invocations count as
inactive for every group operation other than quad and derivative ones.
quad_perm was lowered to OpGroupNonUniformShuffle (and, with fi:0, the
source lane's EXEC bit to a ballot), so a pixel next to a triangle edge
could read an undefined value from its helper neighbour.

In fragment shaders (not under --wave64-emulate), the three swaps become
OpGroupNonUniformQuadSwap and every other selector one
OpGroupNonUniformQuadBroadcast per lane read plus a select on the lane's
selector; with fi:0 the EXEC bit goes through the same quad read. Each
result is stored eagerly, as the shuffle was (SHADER-529). Other stages
keep the shuffle.

tests/semantic/spirv_wave_interp.py gains a helpers= option that takes
the strict reading of the spec, and tests/semantic/test_dpp_quad_ops_ps.py
checks values for 12 selectors x fi/bound_ctrl x EXEC with and without
helper lanes. The test_translate.py DPP assertion now expects the quad
ops for its pixel-shader quad_perm cases.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 02:08:52 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-27 02:19 UTC · original

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.

**@⁠ecox982745** on GitHub · 2026-11-27 02:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2416#issuecomment-7208020889) 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.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 04:43 UTC · original · approved these changes

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** on GitHub · 2026-11-27 04:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2416#pullrequestreview-6083583907) · **approved these changes** 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).
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 05:35 UTC · original · approved these changes

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.

**@⁠tiguan26** on GitHub · 2026-11-27 05:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2416#pullrequestreview-6083914492) · **approved these changes** 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2416/head:github-bot/gh-2416'
git switch 'github-bot/gh-2416'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2416'
git switch 'github-bot/gh-2416'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2416'
git switch 'github-bot/gh-2416'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2416'
git switch 'main'
git merge --squash 'github-bot/gh-2416'
git switch 'main'
git merge --ff-only 'github-bot/gh-2416'
git switch 'main'
git merge 'github-bot/gh-2416'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.