Skip to content

Shaders: lower remaining 32-bit integer and bitwise LDS atomics - #1606

Open
HumanlyDev wants to merge 1 commit into
Marvisak:mainfrom
HumanlyDev:shader/ds-integer-atomics
Open

HumanlyDev wants to merge 1 commit into
Marvisak:mainfrom
HumanlyDev:shader/ds-integer-atomics

Conversation

@HumanlyDev

Copy link
Copy Markdown

What

Lowers the remaining GFX10 32-bit integer and bitwise LDS atomic instructions in Lower::ds:

  • ds_rsub_u32 (0x002) / ds_rsub_rtn_u32 (0x022): atomic mem = data0 - mem.
  • ds_inc_u32 (0x003) / ds_inc_rtn_u32 (0x023): atomic mem = (mem >= data0) ? 0 : mem + 1 (unsigned comparison).
  • ds_dec_u32 (0x004) / ds_dec_rtn_u32 (0x024): atomic mem = (mem == 0 || mem > data0) ? data0 : mem - 1 (unsigned comparison).
  • ds_mskor_b32 (0x00c) / ds_mskor_rtn_b32 (0x02c): atomic mem = (mem & ~data0) | data1.
  • ds_wrap_rtn_b32 (0x034): atomic mem = (mem >= data0) ? mem - data0 : mem + data1 (unsigned comparison), along with decoding its second data operand (DATA1) in gfx10::fields.
  • ds_wrxchg2_rtn_b32 (0x02e) / ds_wrxchg2st64_rtn_b32 (0x02f): paired atomic Rmw::Xchg at (offset0, offset1) scaled by 4 or 256 bytes, returning both prior values into vdst[0..2] and preserving inactive lanes' registers under guard.

emit::bitcode::Module::cmpxchg_pair exposes both fields of {i32, i1} from LLVM 3.7 cmpxchg so the single-block CAS loop branches directly on the i1 success bit (CondBr(ok, exit, head)), avoiding ABA/no-op ambiguity when new == expected and keeping the exiting block as the loop latch.

Verification

Added emit::ds_tests and SP3 disassembly coverage in gfx10::sp3::tests:

  • ds_rsub_inc_dec_mskor_wrap_and_wrxchg2_match_rdna2: validates every emitted DXIL container and container hash with dxil.dll, then executes all 11 opcodes (both _rtn and non-_rtn forms) on both the hardware SM 6.6 D3D12 adapter and WARP across boundary, wraparound, unsigned high-bit (0x8000_0000 vs 0x7FFF_FFFF), unmasked DATA1 bits in ds_mskor, and stride-64 paired exchange cases.
  • ds_atomics_handle_wave_contention_and_divergent_exec_guards: dispatches 64 threads contending on the same LDS dword (ds_inc_rtn_u32) and executes ds_dec_rtn_u32 + ds_wrxchg2_rtn_b32 under a narrowed v_cmpx EXEC guard, verifying exact histogram/wrap counts and confirming disabled lanes neither touch LDS nor overwrite destination VGPRs.
  • ds_cas_loops_nest_inside_shader_loops: runs back-to-back CAS-loop atomics (ds_wrap_rtn_b32 and ds_mskor_rtn_b32) inside a multi-iteration guest shader loop.

Local checks (Windows 11, rustc 1.98.1):

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test --workspace --locked: all tests pass (victory-shader: 123 passed, 0 failed; xtask asset policy: 155 files ok)
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p victory-shader --locked: clean

All test inputs and machine-code sequences are synthetic; no game files were used.

AI-assisted implementation and verification (Antigravity).

Decode both data operands of ds_wrap_rtn_b32 in gfx10::fields, expose
the cmpxchg success bit in emit::bitcode, and lower ds_rsub{_rtn}_u32,
ds_inc{_rtn}_u32, ds_dec{_rtn}_u32, ds_mskor{_rtn}_b32, ds_wrap_rtn_b32,
and ds_wrxchg2{,st64}_rtn_b32 in Lower::ds.

@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 (Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib emit::ds_tests:: — 3 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok

Clean lowering of the remaining 32-bit integer and bitwise LDS atomics with cmpxchg_pair support and thorough end-to-end GPU+WARP test coverage in ds_tests.rs. Looks good.

AI-assisted (Gemini 4 Neon).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/mod.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

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

Shaders: lower remaining 32-bit integer and bitwise LDS atomics — merge conflict with main (940d32f), verified locally. Relates to the LDS atomic cluster (#571/#996/#1953/#1899). Rebase needed.

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.

4 participants