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

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

@⁠HumanlyDev on GitHub · 2026-11-24 19:43 UTC · original
HumanlyDev:shader/ds-integer-atomics → main · +695 −23 · 6 files · 1 commits · open

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

**@⁠HumanlyDev** on GitHub · 2026-11-24 19:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/1606) `HumanlyDev:shader/ds-integer-atomics` → `main` · +695 −23 · 6 files · 1 commits · open ## 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:38:09 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:32 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1606#issuecomment-7149654769) <!-- victory-triage v1 head=cc94388e2e2bf239938a91cd93ec358c6896340c verdict=needs-rebase at=2026-11-25T01:32:00Z --> 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](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/mod.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1606'
git switch 'github-bot/gh-1606'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1606'
git switch 'github-bot/gh-1606'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1606'
git switch 'main'
git merge --squash 'github-bot/gh-1606'
git switch 'main'
git merge --ff-only 'github-bot/gh-1606'
git switch 'main'
git merge 'github-bot/gh-1606'
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!1606
No description provided.