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

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

@⁠RVZO6 on GitHub · 2026-11-25 07:59 UTC · original
RVZO6:rvzo/buffer-subdword-cas → main · +620 −7 · 4 files · 1 commits · open

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)

**@⁠RVZO6** on GitHub · 2026-11-25 07:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208) `RVZO6:rvzo/buffer-subdword-cas` → `main` · +620 −7 · 4 files · 1 commits · open 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)
Sub-dword raw buffer stores previously lowered to two separate
dx.op.atomicBinOp.i32 calls (AND to clear the target byte or half, then
OR to set 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) and produce a bitwise-OR tear such as 0xFF or 0xFFFF.

Replace the AND+OR sequence with a lock-free dx.op.atomicCompareExchange.i32
(opcode 79) CAS loop inside begin_guarded(guard): load the initial dword
from the raw RWByteAddressBuffer via rawBufferLoad.f32 (no TYPED_UAV_LOADS
required), loop with a phi updating only the target byte/half bits until
prev == old, then advance to the exit block for mark_view_write and
end_guarded.

Add a zero-GPU DXIL IR/CFG + dxil.dll validation test in emit::lower
covering byte/short, unguarded/guarded, and view_marks on/off, plus
hardware/WARP execution tests in emit::stride_tests covering distinct-lane
packing and concurrent same-slot byte/halfword races.

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:58:27 +00:00
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 10:47 UTC · original

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

**@⁠sinan-cmyk** on GitHub · 2026-11-25 10:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7160179607) ### 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).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 13:29 UTC · original · edited 2026-11-26 01:24 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 13:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7163864060) · edited 2026-11-26 01:24 UTC <!-- 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)_
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-25 17:20 UTC · original

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.

**@⁠RVZO6** on GitHub · 2026-11-25 17:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7169354436) 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`.
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-26 00:18 UTC · original

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.

**@⁠RVZO6** on GitHub · 2026-11-26 00:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208#issuecomment-7178621682) 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.
Author
Owner

@⁠DenRakEiw on GitHub · 2026-11-25 08:03 UTC · original · approved these changes

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

**@⁠DenRakEiw** on GitHub · 2026-11-25 08:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208#pullrequestreview-6053841320) · **approved these changes** 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).
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 08:17 UTC · original · approved these changes

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.

**@⁠catminou4** on GitHub · 2026-11-25 08:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208#pullrequestreview-6054007025) · **approved these changes** 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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 19:19 UTC · original · approved these changes

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 19:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2208#pullrequestreview-6079454222) · **approved these changes** 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 can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2208'
git switch 'github-bot/gh-2208'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2208'
git switch 'github-bot/gh-2208'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2208'
git switch 'main'
git merge --squash 'github-bot/gh-2208'
git switch 'main'
git merge --ff-only 'github-bot/gh-2208'
git switch 'main'
git merge 'github-bot/gh-2208'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2208
No description provided.