rdna2spirv: use subgroup ballot in quad_any for non-PS stages to avoid partial-quad UB (OLD-9833) #1133

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

@⁠ambroseautomation on GitHub · 2026-11-24 19:03 UTC · original
ambroseautomation:carry/old-9833-quad-any-ballot → main · +183 −17 · 2 files · 1 commits · open

Claimed in the coordination issue as: OLD-9833 (#14)

What and why

Carries forward old-pr/9833 (draft #101, original commit author Lewandox preserved; indexed in #17), rebased cleanly onto current main.

Fixes partial-quad undefined behavior in Gen.quad_any (tools/rdna2spirv.py):

  • For fragment shaders (ps), keeps OpGroupNonUniformQuadSwap (2x2 fragment quads always have 4 invocations).
  • For compute, vertex, geometry, and mesh shaders (cs, vs, gs, ms), replaces OpGroupNonUniformQuadSwap / OpGroupNonUniformShuffle with OpGroupNonUniformBallot + OpGroupNonUniformBallotBitExtract across the quad's four lanes (combined with bitwise OR to avoid short-circuiting in MSL), so absent invocations in partial quads (workgroups or draws whose size is not a multiple of 4) read as 0 instead of triggering undefined behavior.
  • Adds tests/semantic/test_quad_any_partial_quad.py verifying compute partial quads, vertex partial quads, and fragment quad-swap preservation.

Tested

  • python -m unittest discover -s tests/semantic -p test_quad_any_partial_quad.py -v: 3 tests, OK.
  • python -I scripts/run_semantic_tests.py tests/semantic: 471 files, 6,171 tests, 0 files failed.
  • python -m compileall -q make_game.py tools tests: exit 0.
  • python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_quad_any_partial_quad.py: exit 0 (no game data).

GPUs tested: n/a (CPU SPIR-V interpreter semantic suite on Windows 11 Pro 64-bit, Python 3.12.10)

AI-assisted: yes

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. (n/a — partial-quad UB fix for existing quad_any lowering)
**@⁠ambroseautomation** on GitHub · 2026-11-24 19:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1133) `ambroseautomation:carry/old-9833-quad-any-ballot` → `main` · +183 −17 · 2 files · 1 commits · open Claimed in the coordination issue as: OLD-9833 (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Carries forward `old-pr/9833` (draft #101, original commit author `Lewandox` preserved; indexed in #17), rebased cleanly onto current `main`. Fixes partial-quad undefined behavior in `Gen.quad_any` (`tools/rdna2spirv.py`): - For fragment shaders (`ps`), keeps `OpGroupNonUniformQuadSwap` (2x2 fragment quads always have 4 invocations). - For compute, vertex, geometry, and mesh shaders (`cs`, `vs`, `gs`, `ms`), replaces `OpGroupNonUniformQuadSwap` / `OpGroupNonUniformShuffle` with `OpGroupNonUniformBallot` + `OpGroupNonUniformBallotBitExtract` across the quad's four lanes (combined with bitwise OR to avoid short-circuiting in MSL), so absent invocations in partial quads (workgroups or draws whose size is not a multiple of 4) read as 0 instead of triggering undefined behavior. - Adds `tests/semantic/test_quad_any_partial_quad.py` verifying compute partial quads, vertex partial quads, and fragment quad-swap preservation. ### Tested - `python -m unittest discover -s tests/semantic -p test_quad_any_partial_quad.py -v`: 3 tests, OK. - `python -I scripts/run_semantic_tests.py tests/semantic`: 471 files, 6,171 tests, 0 files failed. - `python -m compileall -q make_game.py tools tests`: exit 0. - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_quad_any_partial_quad.py`: exit 0 (no game data). GPUs tested: n/a (CPU SPIR-V interpreter semantic suite on Windows 11 Pro 64-bit, Python 3.12.10) AI-assisted: yes ### 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. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — partial-quad UB fix for existing `quad_any` lowering)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:39:46 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-24 19:10 UTC · original · reviewed

Independent RTX 3090 validation on exact head ed4a77cb4b. The focused test_quad_any_partial_quad.py suite passes 3/3. I also generated a temporary synthetic compute module with workgroup size 3 from the PR's WQM test logic, assembled it, and verified it with spirv-val for Vulkan 1.2. On Bazzite 45 / NVIDIA 620.73.04 / Vulkan 1.4.357 (subgroup 32), vk_compute_test ran it on the RTX 3090; all 64 output words matched: launched lanes 0-2 reported WQM=1 and SCC=1, and the absent fourth lane's output remained untouched. This directly exercises the partial non-fragment quad path on NVIDIA. It is one synthetic case and does not claim native wave64 or AMD validation.

**@⁠KernelSmoker** on GitHub · 2026-11-24 19:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1133#pullrequestreview-6045652791) · **reviewed** Independent RTX 3090 validation on exact head ed4a77cb4b4b11ae04095c0953a62b26a1a1c22e. The focused test_quad_any_partial_quad.py suite passes 3/3. I also generated a temporary synthetic compute module with workgroup size 3 from the PR's WQM test logic, assembled it, and verified it with spirv-val for Vulkan 1.2. On Bazzite 45 / NVIDIA 620.73.04 / Vulkan 1.4.357 (subgroup 32), vk_compute_test ran it on the RTX 3090; all 64 output words matched: launched lanes 0-2 reported WQM=1 and SCC=1, and the absent fourth lane's output remained untouched. This directly exercises the partial non-fragment quad path on NVIDIA. It is one synthetic case and does not claim native wave64 or AMD validation.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 19:15 UTC · original · approved these changes

Reviewed and verified. Resolves SPIR-V undefined behavior in Gen.quad_any for non-fragment stages (CS, VS, GS, MS) where invocations may not be multiples of 4. Lowers via OpGroupNonUniformBallot and OpGroupNonUniformBallotBitExtract so absent invocations evaluate to 0 without invalid quad swaps. Tested in tests/semantic/test_quad_any_partial_quad.py (3/3 PASS) and verified on RTX 3090. 100% clean-room.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 19:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1133#pullrequestreview-6045723816) · **approved these changes** Reviewed and verified. Resolves SPIR-V undefined behavior in `Gen.quad_any` for non-fragment stages (CS, VS, GS, MS) where invocations may not be multiples of 4. Lowers via `OpGroupNonUniformBallot` and `OpGroupNonUniformBallotBitExtract` so absent invocations evaluate to 0 without invalid quad swaps. Tested in `tests/semantic/test_quad_any_partial_quad.py` (3/3 PASS) and verified on RTX 3090. 100% clean-room.
Author
Owner

@⁠madhyn on GitHub · 2026-11-24 19:24 UTC · original

Verified ed4a77cb4 on Arch Linux (Python 3.14):

  • python3 -m unittest tests/semantic/test_quad_any_partial_quad.py: 3 tests passed (0.158 s)

AI-assisted: yes

**@⁠madhyn** on GitHub · 2026-11-24 19:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1133#issuecomment-7142056779) Verified `ed4a77cb4` on Arch Linux (Python 3.14): - `python3 -m unittest tests/semantic/test_quad_any_partial_quad.py`: **3 tests passed** (0.158 s) AI-assisted: yes
Author
Owner

@⁠Deedlyy on GitHub · 2026-11-24 20:14 UTC · original

Recommendation: [MERGE]

Reasoning: Carries #101: quad_any uses a subgroup ballot for non-PS stages instead of quad swap/shuffle, removing partial-quad undefined behaviour in compute/vertex/geometry/mesh shaders (fragment keeps quad swap). Translator change with a 154-line test. Verified by madhyn and KernelSmoker (RTX 3090 synthetic module). CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main. Merge once the queued checks are green.

Impact level: 4/10

**@⁠Deedlyy** on GitHub · 2026-11-24 20:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1133#issuecomment-7143244607) **Recommendation:** [MERGE] **Reasoning:** Carries #101: `quad_any` uses a subgroup ballot for non-PS stages instead of quad swap/shuffle, removing partial-quad undefined behaviour in compute/vertex/geometry/mesh shaders (fragment keeps quad swap). Translator change with a 154-line test. Verified by madhyn and KernelSmoker (RTX 3090 synthetic module). CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with `main`. Merge once the queued checks are green. **Impact level:** 4/10
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 18:30 UTC · original · approved these changes

Approving. Merged into current main (b9320a1) with no conflicts. The new test_quad_any_partial_quad.py passes, and the full translator suite gives 491 files / 6,295 tests with only test_int32_to_f64 failing, the same baseline failure as main here (Linux, Python 3.13).

Switching compute and the non-fragment stages from quad swaps or shuffles to OpGroupNonUniformBallot + BallotBitExtract is right. A swap or shuffle from an absent invocation is undefined, but the ballot leaves absent lanes at 0, so a partial quad ORs in only the lanes that exist and full quads give the same result as before. Pixel shaders keep the quad swaps, since a 2x2 quad always has four invocations there.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 18:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1133#pullrequestreview-6062911780) · **approved these changes** Approving. Merged into current main (b9320a1) with no conflicts. The new `test_quad_any_partial_quad.py` passes, and the full translator suite gives 491 files / 6,295 tests with only `test_int32_to_f64` failing, the same baseline failure as main here (Linux, Python 3.13). Switching compute and the non-fragment stages from quad swaps or shuffles to `OpGroupNonUniformBallot` + `BallotBitExtract` is right. A swap or shuffle from an absent invocation is undefined, but the ballot leaves absent lanes at 0, so a partial quad ORs in only the lanes that exist and full quads give the same result as before. Pixel shaders keep the quad swaps, since a 2x2 quad always has four invocations there. AI-assisted (Claude).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1133/head:github-bot/gh-1133'
git switch 'github-bot/gh-1133'

Merge

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