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

Closed
ambroseautomation wants to merge 1 commit from upstream-pr/1133 into main
ambroseautomation commented 2026-11-24 19:03:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ambroseautomation on 2026-11-24T19:03:29Z.


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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5478508139 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1133). Originally posted by [ambroseautomation](https://github.com/ambroseautomation) on 2026-11-24T19:03:29Z. --- 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 commented 2026-11-24 19:10:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KernelSmoker on 2026-11-24T19:10:40Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6045652791 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1133#pullrequestreview-6045652791). Originally posted by [KernelSmoker](https://github.com/KernelSmoker) on 2026-11-24T19:10:40Z. **Review:** COMMENTED --- 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 commented 2026-11-24 19:15:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T19:15:06Z.

Review: APPROVED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6045723816 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1133#pullrequestreview-6045723816). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T19:15:06Z. **Review:** APPROVED --- 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 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by madhyn on 2026-11-24T19:24:42Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7142056779 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1133#issuecomment-7142056779). Originally posted by [madhyn](https://github.com/madhyn) on 2026-11-24T19:24:42Z. --- 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 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Deedlyy on 2026-11-24T20:14:45Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7143244607 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1133#issuecomment-7143244607). Originally posted by [Deedlyy](https://github.com/Deedlyy) on 2026-11-24T20:14:45Z. --- **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 commented 2026-11-25 18:30:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-25T18:30:25Z.

Review: APPROVED


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

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062911780 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1133#pullrequestreview-6062911780). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-25T18:30:25Z. **Review:** APPROVED --- 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).

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!1133
No description provided.