Skip to content

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

Open
csmith105 wants to merge 1 commit into
ProjectLeonida:mainfrom
csmith105:codex/lds-mskor-atomic
Open

csmith105 wants to merge 1 commit into
ProjectLeonida:mainfrom
csmith105:codex/lds-mskor-atomic

Conversation

@csmith105

Copy link
Copy Markdown

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

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Review of head a509b37 (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.

demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
… page with ProjectLeonida#522's ds_mskor row

The keep-both resolution of ProjectLeonida#522's conflict also restored main's copies of the ds_{inc,dec,rsub}, ds_wrxchg2 and ds_mskor rows that ProjectLeonida#413 had edited (test_design_contract_rows, test_docs_design_rows). The page is now the one before the merge with ProjectLeonida#522's ds_mskor row in place, which keeps ProjectLeonida#413's GPU-fixture note, marked as measured on the earlier two-atomic emission.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants