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

Closed
leonida/mandy
ambroseautomation wants to merge 0 commits from carry/old-9833-quad-any-ballot into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

ambroseautomation commented 2026-11-24 19:03:29 +00:00

Claimed in the coordination issue as: OLD-9833 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7141272211)

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)
Claimed in the coordination issue as: OLD-9833 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7141272211) ### 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)
KernelSmoker reviewed 2026-11-24 19:10:40 +00:00
KernelSmoker left a comment
No description provided.
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.
abnalstlll-byte approved these changes 2026-11-24 19:15:06 +00:00
abnalstlll-byte left a comment

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.

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.
madhyn commented 2026-11-24 19:24:42 +00:00

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

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
Deedlyy commented 2026-11-24 20:14:45 +00:00

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

**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
tiguan26 approved these changes 2026-11-25 18:30:25 +00:00
tiguan26 left a comment

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

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).
Sign in to join this conversation.
No description provided.