Skip to content

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

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/buffer-subdword-cas
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/buffer-subdword-cas

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown

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)

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, 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

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 0624052 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 Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@RVZO6

RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown
Author

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
RVZO6 force-pushed the rvzo/buffer-subdword-cas branch from 0624052 to ed06257 Compare November 25, 2026 17:20
clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…rrier fence, W14-07 CAS stores, W14-08 wave64 readlane

Implements Wave 14 Lane 14C shader stability fixes:
- W14-01 (--store-page-guard, adapted from ethan00013/keylime@650550ffa, GPL-3.0): emit lazy %store_addr_valid(heap_base, va) checking the 64 KiB page-validity bitmap at heap_base+0x170000 and GPBM control header at heap_base+0x180000 (runtime/src/gpu_page_bitmap.h) on PSB stores/atomics; default off, zero bloat on read-only shaders.
- W14-05 (adapted from ethan00013/keylime@0afb8154c5, GPL-3.0): per-function tripped flag in add_loop_guards so hitting a nested inner loop cap breaks enclosing loops instead of multiplying limit*limit.
- W14-06 (adapted from Marvisak/VIctory#1799 & five-star-recomp@320612a4d7, GPL-3.0): downgrade s_barrier inside switch/dispatch form, divergent guards, or non-top-level mesh blocks to OpMemoryBarrier while preserving uniform top-level CS OpControlBarrier.
- W14-07 (adapted from Marvisak/VIctory#2208, GPL-3.0): replace OpAtomicAnd+OpAtomicOr in global_sub and buffer_sub with OpAtomicCompareExchange CAS loops.
- W14-08 (adapted from fxceless/victory-fork#354 GPL-3.0 & brcly/TuxBox#185 Apache-2.0): tighten uniform_vgprs/wave64_halves implicit-read tracking (DPP mods, vlist, block-0 back-edges, v_fmac_f64, v_dot*c_u32_*, v_cndmask_b16) and constant-lane/uniform v_readlane_b32 lowering.
- Add synthetic semantic & spirv-val regression suite tests/semantic/test_w14_shader_stability.py (zero game data).
clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…rmat CAS

- Guard k > 0 dwords of swizzled multi-dword tbuffer_store with store_addr_valid(va_k) when step is not None and --store-page-guard is enabled (tools/rdna2spirv.py).
- Upgrade 1-/2-byte tbuffer_store_format and dynamic buffer_store_format sub-dword merges from non-atomic OpAtomicAnd+OpAtomicOr to atomic_subdword_cas (Marvisak/VIctory#2208, GPL-3.0).
- Add W15-05 semantic & spirv-val tests in tests/semantic/test_w14_shader_stability.py and update stale OpAtomicAnd/Or assertions in tests/semantic/test_buffer_store_format.py and tests/test_translate.py.
… buffer_store_short

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)
@RVZO6

RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown
Author

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
RVZO6 force-pushed the rvzo/buffer-subdword-cas branch from ed06257 to 7bad309 Compare November 26, 2026 00:18
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…AS loop for buffer_store_byte and buffer_store_short

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…AS loop for buffer_store_byte and buffer_store_short

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants