Shaders: use atomicCompareExchange CAS loop for buffer_store_byte and buffer_store_short #2208
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2208
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2208"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@RVZO6 on GitHub · 2026-11-25 07:59 UTC · original
RVZO6:rvzo/buffer-subdword-cas→main· +620 −7 · 4 files · 1 commits · openSub-dword raw buffer stores (
buffer_store_byteandbuffer_store_short) incrates/victory-shader/src/emit/lower.rscurrently lower to two separatedx.op.atomicBinOp.i32calls: anANDto clear the target byte or halfword followed by anORto 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 as0xFFor0xFFFF).Claimed on #1633 (
https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155420630).The change
crates/victory-shader/src/emit/lower.rs:AND+ORsequence with a lock-freedx.op.atomicCompareExchange.i32(opcode79) CAS loop insidebegin_guarded(guard):rawBufferLoad.f32(noTYPED_UAV_LOADSrequired).casblock (header = exiting latch, preserving clean structured CFG) updating only the target byte/halfword bits untilprev == old.mark_view_writeandend_guarded.dxil.dllvalidation testsubdword_buffer_stores_emit_cas_loop_and_validatecovering byte/short, unguarded/guarded, andview_markson/off.crates/victory-shader/src/emit/bitcode.rs:#[cfg(test)]IR/CFG inspection helperscall_countandcas_loop_count.crates/victory-shader/src/emit/stride_tests.rs:subdword_buffer_stores_pack_distinct_lanes_in_one_dwordandsubdword_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.mdentry replaces the racyAND+ORlowering ofbuffer_store_byte/buffer_store_shortwith a CAS loop.Tested
windows-latestMSVC + Windows WARP SM 6.6 CI run45570823602(Clippyclean,Clippy, diagnostics buildclean,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 ... okemit::stride_tests::subdword_cas_shaders_lower_and_validate ... okemit::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)
@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
main940d32f(clean merge) and ranvictory translate --validate --listonshaders.rpf(1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result withmainprogram by program (25,889 programs). Validated: 23,918 onmain, 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 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 toemit/lower/subdword_tests.rs, solower.rsstays under the 512 KiB policy limit.In
crates/victory-shader/src/emit/lower.rs(sub-dwordbuffer_store_byte/buffer_store_shortpath of the MUBUF lowering), the separateANDandORatomicBinOps are replaced with a compare-exchange loop. A firstrawBufferLoadgives the guess, then a self-looping block computes(old & keep) | x, runsatomicCompareExchange(opcode 79) and exits when the result matches. The loop sits betweenbegin_guarded/end_guarded, andmark_view_writefollows 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_markson/off), plus a 64-lane same-slot race on WARP.CI: passing (
check). Recommending a look, not approving.Generated by Claude Code
@RVZO6 on GitHub · 2026-11-25 17:20 UTC · original
Rebased onto current
main(13f91ca, headed06257; resolved test-helper adjacency inbitcode.rsand end-of-file test append instride_tests.rs).Verified green on
windows-latestMSVC + WARP SM 6.6 CI (Run 45662428628) and Wine +dxil.dll 1.9.2609.5.@RVZO6 on GitHub · 2026-11-26 00:18 UTC · original
Rebased onto current
main(63bcca4, head7bad309). Moved the unit test intocrates/victory-shader/src/emit/lower/subdword_tests.rssolower.rsstays below the 512 KiB asset-policy limit. Verified green onwindows-latestMSVC + WARP SM 6.6 CI (clippy, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45726165860.@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, withdxil.dllvalidator):cargo test -p victory-shader --lib subdword— 4 passed, 0 failed on both RTX 5090 hardware andVICTORY_D3D12_WARP=1(includingsubdword_buffer_stores_emit_cas_loop_and_validate)cargo clippy -p victory-shader --all-targets— 0 warningscargo xtask policy— ok (183 files ok)Clean
atomicCompareExchangeCAS-loop lowering forbuffer_store_byteandbuffer_store_short, verified end-to-end on RTX 5090 + WARP. Looks great.AI-assisted (Gemini 4 Neon).
@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.
@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=0andVICTORY_WARP=1) — 4 passed, 0 failed (includingsubdword_buffer_stores_emit_cas_loop_and_validate,subdword_cas_shaders_lower_and_validate,subdword_buffer_stores_do_not_tear_concurrent_same_slot_writes, andsubdword_buffer_stores_pack_distinct_lanes_in_one_dword)cargo test --locked -p victory-shader --lib emit::lower::tests::(VICTORY_WARP=0andVICTORY_WARP=1) — 33 passed, 0 failedcargo 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 failedcargo clippy --locked -p victory-shader --all-targets -- -D warnings(default and--all-features) — 0 warningscargo xtask policy— ok (192 files ok);emit/lower.rssize is 522,135 B (< 524,288 B)Review notes
crates/victory-shader/src/emit/lower.rs:9618-9665):begin_guarded/end_guardedkeeps inactive (EXEC=0) lanes out of the loop, concurrent active lanes targeting different fields of the same aligned dword converge without tearing via theoldphi feedback,dx.op.atomicCompareExchange.i32(op79, raw UAV handle) andmark_view_writeare emitted cleanly, andbitcode.rsliveness/bitcode generation is sound.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 like0, the loop has no retry bound).shortcutcomment atlower.rs:9623, an unalignedbuffer_store_shortatoff & 3 == 3truncates its upper byte (0xFFFF << 24), matching the prior single-dword assumption.rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower/subdword_tests.rsflags the long import on line 2, extra blank line on line 15, and comment/assert wrapping.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.