Shaders: use atomicCompareExchange CAS loop for buffer_store_byte and buffer_store_short #2206

Open
RVZO6 wants to merge 1 commit from upstream-pr/2208 into main
RVZO6 commented 2026-11-25 07:59:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T07:59:24Z.


Sub-dword raw buffer stores (buffer_store_byte and buffer_store_short) in crates/victory-shader/src/emit/lower.rs currently lower to two separate dx.op.atomicBinOp.i32 calls: an AND to clear the target byte or halfword followed by an OR to write the new bits. When two lanes or threads store to the same byte or halfword concurrently, their clears and sets can interleave (AND, AND, OR, OR), producing a bitwise-OR tear (such as 0xFF or 0xFFFF).

Claimed on #1633 (https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155420630).

The change

  • crates/victory-shader/src/emit/lower.rs:
    • Replace the two-op AND + OR sequence with a lock-free dx.op.atomicCompareExchange.i32 (opcode 79) CAS loop inside begin_guarded(guard):
      • Load the initial dword via rawBufferLoad.f32 (no TYPED_UAV_LOADS required).
      • Loop with a single self-branching cas block (header = exiting latch, preserving clean structured CFG) updating only the target byte/halfword bits until prev == old.
      • Advance to the exit block before mark_view_write and end_guarded.
    • Add zero-GPU DXIL IR/CFG + dxil.dll validation test subdword_buffer_stores_emit_cas_loop_and_validate covering byte/short, unguarded/guarded, and view_marks on/off.
  • crates/victory-shader/src/emit/bitcode.rs:
    • Add #[cfg(test)] IR/CFG inspection helpers call_count and cas_loop_count.
  • crates/victory-shader/src/emit/stride_tests.rs:
    • Add hardware/WARP execution tests subdword_buffer_stores_pack_distinct_lanes_in_one_dword and subdword_buffer_stores_do_not_tear_concurrent_same_slot_writes (64-lane same-slot byte/halfword race).

Not a duplicate of

No open PR or CLAIMS.md entry replaces the racy AND+OR lowering of buffer_store_byte / buffer_store_short with a CAS loop.

Tested

  • Fork windows-latest MSVC + Windows WARP SM 6.6 CI run 45570823602 (Clippy clean, Clippy, diagnostics build clean, Tests: 731 passed; 0 failed; 10 ignored, victory-shader: 262 passed; 0 failed; 3 ignored, xtask: 6 passed; 0 failed):
    • subdword_buffer_stores_emit_cas_loop_and_validate ... ok
    • emit::stride_tests::subdword_cas_shaders_lower_and_validate ... ok
    • emit::stride_tests::subdword_buffer_stores_pack_distinct_lanes_in_one_dword ... ok (executed on Windows WARP SM 6.6)
    • emit::stride_tests::subdword_buffer_stores_do_not_tear_concurrent_same_slot_writes ... ok (executed on Windows WARP SM 6.6)

Not run: the title; any hardware GPU. The title was not used in these tests.

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

<!-- greyfinch-sync:Marvisak/VIctory:issue:5487444780 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-25T07:59:24Z. --- Sub-dword raw buffer stores (`buffer_store_byte` and `buffer_store_short`) in `crates/victory-shader/src/emit/lower.rs` currently lower to two separate `dx.op.atomicBinOp.i32` calls: an `AND` to clear the target byte or halfword followed by an `OR` to write the new bits. When two lanes or threads store to the same byte or halfword concurrently, their clears and sets can interleave (`AND`, `AND`, `OR`, `OR`), producing a bitwise-OR tear (such as `0xFF` or `0xFFFF`). Claimed on #1633 (`https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155420630`). ## The change - `crates/victory-shader/src/emit/lower.rs`: - Replace the two-op `AND` + `OR` sequence with a lock-free `dx.op.atomicCompareExchange.i32` (opcode `79`) CAS loop inside `begin_guarded(guard)`: - Load the initial dword via `rawBufferLoad.f32` (no `TYPED_UAV_LOADS` required). - Loop with a single self-branching `cas` block (header = exiting latch, preserving clean structured CFG) updating only the target byte/halfword bits until `prev == old`. - Advance to the exit block before `mark_view_write` and `end_guarded`. - Add zero-GPU DXIL IR/CFG + `dxil.dll` validation test `subdword_buffer_stores_emit_cas_loop_and_validate` covering byte/short, unguarded/guarded, and `view_marks` on/off. - `crates/victory-shader/src/emit/bitcode.rs`: - Add `#[cfg(test)]` IR/CFG inspection helpers `call_count` and `cas_loop_count`. - `crates/victory-shader/src/emit/stride_tests.rs`: - Add hardware/WARP execution tests `subdword_buffer_stores_pack_distinct_lanes_in_one_dword` and `subdword_buffer_stores_do_not_tear_concurrent_same_slot_writes` (64-lane same-slot byte/halfword race). ## Not a duplicate of No open PR or `CLAIMS.md` entry replaces the racy `AND`+`OR` lowering of `buffer_store_byte` / `buffer_store_short` with a CAS loop. ## Tested - Fork `windows-latest` MSVC + Windows WARP SM 6.6 CI run [`45570823602`](https://github.com/RVZO6/VIctory/actions/runs/45570823602) (`Clippy` clean, `Clippy, diagnostics build` clean, `Tests`: **`731 passed; 0 failed; 10 ignored`**, `victory-shader`: **`262 passed; 0 failed; 3 ignored`**, `xtask`: **`6 passed; 0 failed`**): - `subdword_buffer_stores_emit_cas_loop_and_validate ... ok` - `emit::stride_tests::subdword_cas_shaders_lower_and_validate ... ok` - `emit::stride_tests::subdword_buffer_stores_pack_distinct_lanes_in_one_dword ... ok` (executed on Windows WARP SM 6.6) - `emit::stride_tests::subdword_buffer_stores_do_not_tear_concurrent_same_slot_writes ... ok` (executed on Windows WARP SM 6.6) **Not run:** the title; any hardware GPU. The title was not used in these tests. AI-assisted: T3 Code (Antigravity, gemini-4-neon)
DenRakEiw commented 2026-11-25 08:03:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T08:03:33Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib subdword — 4 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1 (including subdword_buffer_stores_emit_cas_loop_and_validate)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean atomicCompareExchange CAS-loop lowering for buffer_store_byte and buffer_store_short, verified end-to-end on RTX 5090 + WARP. Looks great.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6053841320 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208#pullrequestreview-6053841320). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T08:03:33Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with `dxil.dll` validator): - `cargo test -p victory-shader --lib subdword` — **4 passed, 0 failed** on both RTX 5090 hardware and `VICTORY_D3D12_WARP=1` (including `subdword_buffer_stores_emit_cas_loop_and_validate`) - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean `atomicCompareExchange` CAS-loop lowering for `buffer_store_byte` and `buffer_store_short`, verified end-to-end on RTX 5090 + WARP. Looks great. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 08:17:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T08:17:45Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

145 passed / 0 failed. buffer_store atomics via atomicCompareExchange CAS loop — CAS-loop lowering for buffer atomics.

<!-- greyfinch-sync:Marvisak/VIctory:review:6054007025 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208#pullrequestreview-6054007025). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T08:17:45Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 145 passed / 0 failed. buffer_store atomics via atomicCompareExchange CAS loop — CAS-loop lowering for buffer atomics.
sinan-cmyk commented 2026-11-25 11:05:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T10:47:34Z.


Offline measurement on a retail dump

Result: no count change against main.

I merged head 06240520 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7160179607 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7160179607). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T10:47:34Z. --- ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 06240520 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 14:07:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T13:29:57Z.


Automated triage for the maintainers.

Merge candidate. Rebased onto current main (63bcca4), one commit, merges cleanly. The unit test moved to emit/lower/subdword_tests.rs, so lower.rs stays under the 512 KiB policy limit.

In crates/victory-shader/src/emit/lower.rs (sub-dword buffer_store_byte/buffer_store_short path of the MUBUF lowering), the separate AND and OR atomicBinOps are replaced with a compare-exchange loop. A first rawBufferLoad gives the guess, then a self-looping block computes (old & keep) | x, runs atomicCompareExchange (opcode 79) and exits when the result matches. The loop sits between begin_guarded/end_guarded, and mark_view_write follows on the exit block. On the hardware a byte or short store never tears its neighbours or itself; the old clear-then-set could leave an OR of two concurrent values. This lowering matches the hardware: every iteration lets at least one contending lane succeed, so the loop makes progress. Tests cover IR shape and validation (guarded/unguarded, view_marks on/off), plus a 64-lane same-slot race on WARP.

CI: passing (check). Recommending a look, not approving.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163864060 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7163864060). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T13:29:57Z. --- <!-- victory-triage v1 head=7bad309bbfd9794bc7614be5f8ee499b2de02dcd verdict=merge-candidate at=2026-11-26T01:24:47Z --> Automated triage for the maintainers. **Merge candidate.** Rebased onto current `main` (`63bcca4`), one commit, merges cleanly. The unit test moved to `emit/lower/subdword_tests.rs`, so `lower.rs` stays under the 512 KiB policy limit. In `crates/victory-shader/src/emit/lower.rs` (sub-dword `buffer_store_byte`/`buffer_store_short` path of the MUBUF lowering), the separate `AND` and `OR` `atomicBinOp`s are replaced with a compare-exchange loop. A first `rawBufferLoad` gives the guess, then a self-looping block computes `(old & keep) | x`, runs `atomicCompareExchange` (opcode 79) and exits when the result matches. The loop sits between `begin_guarded`/`end_guarded`, and `mark_view_write` follows on the exit block. On the hardware a byte or short store never tears its neighbours or itself; the old clear-then-set could leave an OR of two concurrent values. This lowering matches the hardware: every iteration lets at least one contending lane succeed, so the loop makes progress. Tests cover IR shape and validation (guarded/unguarded, `view_marks` on/off), plus a 64-lane same-slot race on WARP. CI: passing (`check`). Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
RVZO6 commented 2026-11-25 18:02:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T17:20:08Z.


Rebased onto current main (13f91ca, head ed06257; resolved test-helper adjacency in bitcode.rs and end-of-file test append in stride_tests.rs).

Verified green on windows-latest MSVC + WARP SM 6.6 CI (Run 45662428628) and Wine + dxil.dll 1.9.2609.5.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169354436 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7169354436). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-25T17:20:08Z. --- Rebased onto current `main` (`13f91ca`, head `ed06257`; resolved test-helper adjacency in `bitcode.rs` and end-of-file test append in `stride_tests.rs`). Verified green on `windows-latest` MSVC + WARP SM 6.6 CI ([Run 45662428628](https://github.com/RVZO6/VIctory/actions/runs/45662428628)) and Wine + `dxil.dll 1.9.2609.5`.
RVZO6 commented 2026-11-26 01:03:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T00:18:36Z.


Rebased onto current main (63bcca4, head 7bad309). Moved the unit test into crates/victory-shader/src/emit/lower/subdword_tests.rs so lower.rs stays below the 512 KiB asset-policy limit. Verified green on windows-latest MSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45726165860.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178621682 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7178621682). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-26T00:18:36Z. --- Rebased onto current `main` (`63bcca4`, head `7bad309`). Moved the unit test into `crates/victory-shader/src/emit/lower/subdword_tests.rs` so `lower.rs` stays below the 512 KiB asset-policy limit. Verified green on `windows-latest` MSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45726165860.
fuzeofficlal commented 2026-11-26 19:19:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:19:52Z.

Review: APPROVED


Verified locally merged onto origin/main (63bcca43, Windows 11 x64, Rust 1.98.1 MSVC, RTX 5090 & WARP + dxil.dll):

  • cargo test --locked -p victory-shader --lib subdword (VICTORY_WARP=0 and VICTORY_WARP=1) — 4 passed, 0 failed (including subdword_buffer_stores_emit_cas_loop_and_validate, subdword_cas_shaders_lower_and_validate, subdword_buffer_stores_do_not_tear_concurrent_same_slot_writes, and subdword_buffer_stores_pack_distinct_lanes_in_one_dword)
  • cargo test --locked -p victory-shader --lib emit::lower::tests:: (VICTORY_WARP=0 and VICTORY_WARP=1) — 33 passed, 0 failed
  • cargo test --locked -p victory-shader --lib emit::stride_tests:: — 11 passed, 0 failed on WARP (VICTORY_WARP=1); 9 passed, 0 failed on RTX 5090 (--skip a_heap_buffer_read_)
  • cargo test --locked -p victory-shader --lib constant_color_validates — 1 passed, 0 failed
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features) — 0 warnings
  • cargo xtask policy — ok (192 files ok); emit/lower.rs size is 522,135 B (< 524,288 B)

Review notes

  • Verified the CAS loop (crates/victory-shader/src/emit/lower.rs:9618-9665): begin_guarded/end_guarded keeps inactive (EXEC=0) lanes out of the loop, concurrent active lanes targeting different fields of the same aligned dword converge without tearing via the old phi feedback, dx.op.atomicCompareExchange.i32 (op 79, raw UAV handle) and mark_view_write are emitted cleanly, and bitcode.rs liveness/bitcode generation is sound.
  • Non-blocking spec notes:
    • Loop termination relies solely on prev == old; under DXIL/SM 5.1 rules, out-of-bounds or pixel-helper atomic returns are undefined (if a driver ever returned varying values on OOB/helper lanes rather than a constant like 0, the loop has no retry bound).
    • As noted by the shortcut comment at lower.rs:9623, an unaligned buffer_store_short at off & 3 == 3 truncates its upper byte (0xFFFF << 24), matching the prior single-dword assumption.
  • Minor formatting nit: rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower/subdword_tests.rs flags the long import on line 2, extra blank line on line 15, and comment/assert wrapping.
<!-- greyfinch-sync:Marvisak/VIctory:review:6079454222 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2208#pullrequestreview-6079454222). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T19:19:52Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`63bcca43`, Windows 11 x64, Rust 1.98.1 MSVC, RTX 5090 & WARP + `dxil.dll`): - `cargo test --locked -p victory-shader --lib subdword` (`VICTORY_WARP=0` and `VICTORY_WARP=1`) — **4 passed, 0 failed** (including `subdword_buffer_stores_emit_cas_loop_and_validate`, `subdword_cas_shaders_lower_and_validate`, `subdword_buffer_stores_do_not_tear_concurrent_same_slot_writes`, and `subdword_buffer_stores_pack_distinct_lanes_in_one_dword`) - `cargo test --locked -p victory-shader --lib emit::lower::tests::` (`VICTORY_WARP=0` and `VICTORY_WARP=1`) — **33 passed, 0 failed** - `cargo test --locked -p victory-shader --lib emit::stride_tests::` — **11 passed, 0 failed** on WARP (`VICTORY_WARP=1`); **9 passed, 0 failed** on RTX 5090 (`--skip a_heap_buffer_read_`) - `cargo test --locked -p victory-shader --lib constant_color_validates` — **1 passed, 0 failed** - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`) — 0 warnings - `cargo xtask policy` — ok (`192 files ok`); `emit/lower.rs` size is **522,135 B** (< 524,288 B) ### Review notes - Verified the CAS loop (`crates/victory-shader/src/emit/lower.rs:9618-9665`): `begin_guarded`/`end_guarded` keeps inactive (`EXEC=0`) lanes out of the loop, concurrent active lanes targeting different fields of the same aligned dword converge without tearing via the `old` phi feedback, `dx.op.atomicCompareExchange.i32` (op `79`, raw UAV handle) and `mark_view_write` are emitted cleanly, and `bitcode.rs` liveness/bitcode generation is sound. - **Non-blocking spec notes**: - Loop termination relies solely on `prev == old`; under DXIL/SM 5.1 rules, out-of-bounds or pixel-helper atomic returns are undefined (if a driver ever returned varying values on OOB/helper lanes rather than a constant like `0`, the loop has no retry bound). - As noted by the `shortcut` comment at `lower.rs:9623`, an unaligned `buffer_store_short` at `off & 3 == 3` truncates its upper byte (`0xFFFF << 24`), matching the prior single-dword assumption. - Minor formatting nit: `rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower/subdword_tests.rs` flags the long import on line 2, extra blank line on line 15, and comment/assert wrapping.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2208:upstream-pr/2208'
git switch 'upstream-pr/2208'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2208'
git switch 'upstream-pr/2208'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2208'
git switch 'upstream-pr/2208'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2208'
git switch 'main'
git merge --squash 'upstream-pr/2208'
git switch 'main'
git merge --ff-only 'upstream-pr/2208'
git switch 'main'
git merge 'upstream-pr/2208'
git push origin 'main'
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
Marvisak/VIctory!2206
No description provided.