SHADER-LDS-MSKOR-ATOMIC: preserve masked-OR atomicity under contention #522

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

@⁠csmith105 on GitHub · 2026-11-24 06:53 UTC · original
csmith105:codex/lds-mskor-atomic → main · +82 −11 · 3 files · 1 commits · open

Claimed as SHADER-LDS-MSKOR-ATOMIC.

Depends on: #413 (documentation-row overlap only; no code dependency). Whichever merges second should retain that PR's fixture attribution and this corrected atomicity contract. Draft #173 adds a separate single-active-lane fixture and is not a dependency.

What and why

The operation is specified in the AMD RDNA2 ISA guide, section 12.13. Two lanes updating the same LDS word with ds_mskor[_rtn]_b32 can currently produce a value no atomic execution permits. From zero, lane 0 clears bit 0 and sets bit 1, while lane 1 clears bit 1 and sets bit 0. A complete masked-OR operation must leave 1 or 2. The translator's separate atomic AND and OR leave 3; both lanes return zero.

Replace the split operations with a Workgroup-scope atomic load/compare-exchange loop. A failed exchange supplies the next old word, and the loop recomputes the entire update. The returning form exposes the old word of its successful exchange. Existing EXEC and LDS-bound guards remain around the operation; address/mask/data are captured before a potentially aliasing destination is written.

The regression checks both valid serial orders, both instruction forms and offsets 0/12. Additional coverage checks destination aliasing and inactive lanes. Update the existing contract row with measured translator results and the limitation that host atomic ordering does not promise the lane order measured on native RDNA2.

Tested

Native RTX 3080, one hand-written 32-thread wave with two active update lanes; fields below are [old0, final0, old1, final1]:

Translation Output Valid atomic result
Main / split AND+OR [0,3,0,3] No
CAS loop [0,1,2,1] Yes

Both modules translated, assembled and passed spirv-val for Vulkan 1.2. Both native dispatches completed with GPU-assisted validation enabled and zero validation errors; only the old translation violated the result oracle. Used SPIRV-Tools 2026.4, Khronos validation layers 1.4.357.0, LEONIDA_VK_VALIDATION=1, VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1 and subgroup size 32.

  • New contention regression fails on main in all four form/offset cases; all seven focused tests pass after the fix.
  • Fifteen shared-memory bounds tests pass.
  • Six generated modules pass SPIR-V validation: both forms with unbounded, bounded in-range and bounded out-of-range access. Independent review executed all six through the CPU interpreter and confirmed results.
  • Full semantic suite: 465 files, 6,108 tests, zero failed files (397 seconds, eight jobs).
  • Compile, CLI-help, documentation-row, changed-file data-policy and whitespace checks pass.

No retail/game data was used. These measurements establish synthetic shader translation behavior, not a retail-game rendering result.

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 610.88, Windows 11 build 26300.

AI-assisted: yes (Codex); independently reviewed by a second agent.

Checklist

  • Based on current main; code and tests merge cleanly. Documentation overlap with #413 noted above.
  • One topic.
  • No game data, keys or decryption.
  • Existing translator contract row updated with confirmation and limits.
**@⁠csmith105** on GitHub · 2026-11-24 06:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/522) `csmith105:codex/lds-mskor-atomic` → `main` · +82 −11 · 3 files · 1 commits · open Claimed as [SHADER-LDS-MSKOR-ATOMIC](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14). Depends on: #413 (documentation-row overlap only; no code dependency). Whichever merges second should retain that PR's fixture attribution and this corrected atomicity contract. Draft #173 adds a separate single-active-lane fixture and is not a dependency. ### What and why The operation is specified in the [AMD RDNA2 ISA guide, section 12.13](https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content). Two lanes updating the same LDS word with `ds_mskor[_rtn]_b32` can currently produce a value no atomic execution permits. From zero, lane 0 clears bit 0 and sets bit 1, while lane 1 clears bit 1 and sets bit 0. A complete masked-OR operation must leave 1 or 2. The translator's separate atomic AND and OR leave 3; both lanes return zero. Replace the split operations with a Workgroup-scope atomic load/compare-exchange loop. A failed exchange supplies the next old word, and the loop recomputes the entire update. The returning form exposes the old word of its successful exchange. Existing EXEC and LDS-bound guards remain around the operation; address/mask/data are captured before a potentially aliasing destination is written. The regression checks both valid serial orders, both instruction forms and offsets 0/12. Additional coverage checks destination aliasing and inactive lanes. Update the existing contract row with measured translator results and the limitation that host atomic ordering does not promise the lane order measured on native RDNA2. ### Tested Native RTX 3080, one hand-written 32-thread wave with two active update lanes; fields below are `[old0, final0, old1, final1]`: | Translation | Output | Valid atomic result | |---|---|---| | Main / split AND+OR | `[0,3,0,3]` | No | | CAS loop | `[0,1,2,1]` | Yes | Both modules translated, assembled and passed `spirv-val` for Vulkan 1.2. Both native dispatches completed with GPU-assisted validation enabled and zero validation errors; only the old translation violated the result oracle. Used SPIRV-Tools 2026.4, Khronos validation layers 1.4.357.0, `LEONIDA_VK_VALIDATION=1`, `VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1` and subgroup size 32. - New contention regression fails on main in all four form/offset cases; all seven focused tests pass after the fix. - Fifteen shared-memory bounds tests pass. - Six generated modules pass SPIR-V validation: both forms with unbounded, bounded in-range and bounded out-of-range access. Independent review executed all six through the CPU interpreter and confirmed results. - Full semantic suite: 465 files, 6,108 tests, zero failed files (397 seconds, eight jobs). - Compile, CLI-help, documentation-row, changed-file data-policy and whitespace checks pass. No retail/game data was used. These measurements establish synthetic shader translation behavior, not a retail-game rendering result. GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 610.88, Windows 11 build 26300. AI-assisted: yes (Codex); independently reviewed by a second agent. ### Checklist - [x] Based on current `main`; code and tests merge cleanly. Documentation overlap with #413 noted above. - [x] One topic. - [x] No game data, keys or decryption. - [x] Existing translator contract row updated with confirmation and limits.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:05:52 +00:00
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-24 07:55 UTC · original

Review of head a509b37575 (AI-assisted, CPU only).

Environment: Linux (WSL2), Python venv, no GPU, no spirv-val/spirv-as installed.

What ran:

  • python -m unittest discover -s tests/semantic -p test_lds_mskor.py: 7 tests OK.
  • Same file with tools/rdna2spirv.py reverted to origin/main: test_contending_updates_are_atomic fails in all 4 subtests (returning/non-returning, offset 0/12). So the new race test catches the split AND/OR lowering in the CPU interpreter.
  • python -I scripts/run_semantic_tests.py tests/semantic: 6108 tests, 2 files failed (test_sopc_validation, test_unary_f16_validation). origin/main fails the same 2 files (6107 tests), so no new failures.

Reading of the emitted SPIR-V (not run through spirv-val here): atomic load, then a loop whose header computes (cur & ~DATA) | DATA2 and does OpAtomicCompareExchange against cur; OpLoopMerge comes right before the conditional branch, and the continue block only branches back with got as the new cur. With two lanes on one dword, the loser's exchange fails, it retries from the winner's word, and the final value is one of the two serial orders; the returned cur is the old word of the successful exchange. That matches the ISA formula and the docs row.

docs/design.md row: says how it was confirmed (CPU test plus a GPU run of the two-lane case on an RTX 3080, with the old and new results). The gfx1036 32-lane lane-order measurement stays and the row says the CAS lowering does not promise that order. Fine.

Not tested: any GPU run, spirv-val on the module, wave64 emulation, the corpus slice before/after counts.

No defects found.


Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account.

**@⁠kawaii-shakal** on GitHub · 2026-11-24 07:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/522#issuecomment-7125527325) Review of head a509b37575562b91d0315c60204c2a7d45bb19ef (AI-assisted, CPU only). Environment: Linux (WSL2), Python venv, no GPU, no spirv-val/spirv-as installed. What ran: - `python -m unittest discover -s tests/semantic -p test_lds_mskor.py`: 7 tests OK. - Same file with `tools/rdna2spirv.py` reverted to origin/main: `test_contending_updates_are_atomic` fails in all 4 subtests (returning/non-returning, offset 0/12). So the new race test catches the split AND/OR lowering in the CPU interpreter. - `python -I scripts/run_semantic_tests.py tests/semantic`: 6108 tests, 2 files failed (test_sopc_validation, test_unary_f16_validation). origin/main fails the same 2 files (6107 tests), so no new failures. Reading of the emitted SPIR-V (not run through spirv-val here): atomic load, then a loop whose header computes `(cur & ~DATA) | DATA2` and does `OpAtomicCompareExchange` against `cur`; `OpLoopMerge` comes right before the conditional branch, and the continue block only branches back with `got` as the new `cur`. With two lanes on one dword, the loser's exchange fails, it retries from the winner's word, and the final value is one of the two serial orders; the returned `cur` is the old word of the successful exchange. That matches the ISA formula and the docs row. docs/design.md row: says how it was confirmed (CPU test plus a GPU run of the two-lane case on an RTX 3080, with the old and new results). The gfx1036 32-lane lane-order measurement stays and the row says the CAS lowering does not promise that order. Fine. Not tested: any GPU run, spirv-val on the module, wave64 emulation, the corpus slice before/after counts. No defects found. --- _Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account._
This pull request has changes conflicting with the target branch.
  • docs/design.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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