RDNA2 691/721: LDS atomics (sub/rsub/inc/dec/min_i32/max_i32/cmpst/wrxchg) #23

Closed
github-bot wants to merge 1 commit from gh-pr/23/head into gh-pr/23/base
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-21 17:59 UTC · original
SchmashinPrunes:lds-atomics → main · +163 −37 · 8 files · 1 commits · closed without merging

Library / area: tombox-xshader (RDNA2 shader translator) / progress map (RDNA2 instructions). Part of #17.

Exports changed (exports.txt rows): none

Source of the behaviour: RDNA2 ISA DS semantics as encoded by LLVM's AMDGPU backend for GFX10 (llvm/lib/Target/AMDGPU/DSInstructions.td, LangRef atomicrmw). The same semantics are already used and tested by the GDS path (src/hlsl/gds.rs, tests/exec_mem/gds.rs). No console files used.

What changed

The LDS (group-shared) path only lowered ds_add/min_u32/max_u32/and/or/xor. The rest of the DS atomics were lowered for GDS only, so they never counted as verified. This lowers them for LDS too:

  • Interlocked*: ds_sub_u32, ds_wrxchg_rtn_b32, ds_cmpst_b32.
  • Compare-exchange loop (same shape as the GDS path): ds_rsub_u32, ds_inc_u32, ds_dec_u32, ds_min_i32, ds_max_i32. tb_lds is uint, so the signed min/max don't rely on DXC picking a signed InterlockedMin/Max overload.
  • All of the above plus their _rtn forms: 15 mnemonics.

Semantics worth calling out:

  • ds_inc/ds_dec are uinc_wrap/udec_wrap: old u>= data ? 0 : old+1 and (old == 0 || old u> data) ? data : old-1.
  • On GFX10, ds_cmpst takes data0 = compare value, data1 = value stored on a match. LLVM: "the order of src and cmp is the opposite of BUFFER_ATOMIC_CMPSWAP"; GFX11 swaps them back as ds_cmpstore.

Tests

New tests/exec_mem/ds.rs ds_lds_atomics_ext: 106 cases, each also rerun with runtime inputs.

  • One dword per thread, read back by the neighbour thread, so the order-dependent ops have fixed results. Inputs hit the edges: old == data, old == 0, neighbouring small values, negative vs positive (signed vs unsigned), and cmpst match / no match.
  • All 64 threads on one dword for the order-independent ops (sub, signed min/max, inc/dec that never wrap), to exercise the retry loop under contention.
  • Mutation check: swapping the cmpst operands, or making ds_min_i32 compare unsigned, makes the test fail (26 failing cases).
TOMBOX_DXC_DIR=<dxc 1.9.2609>\bin\x64 cargo test -p tombox-xshader --release --test exec ds_
test result: ok. 8 passed; 0 failed
ds_lds_ext: 15 mnemonics verified, 0 failing, 106 cases, 106 rerun with runtime inputs

python tools/progress/gen.py: RDNA2 instructions 676/721 → 691/721 (rebased onto 8cd153d; the code was unchanged by the rebase and upstream has not touched tombox-xshader since the original test run). Only the instruction metric changes in LIBRARY_MAP / MILESTONES / summary / badges. Touches the same generated progress files as other open RDNA2 PRs; whichever merges later just reruns gen.py.

Full cargo test -p tombox-xshader --release (Windows 11 LTSC, WARP)

No new failures. Failures that are identical on unpatched main (fef9a6e):

  • exec: vop::basic::{salu,vop1,vop2,vop3}_basic, the same 4 that #15 reports.

  • amp / fixtures / mesh: 11 tests that need analysis/shaders/... dump fixtures (#13 makes these skip).

  • mesh::mesh_refusals_are_explicit: panics at mesh.rs:247 (expect("refusal")) on unpatched main too.

  • cargo test -p tombox-xshader passes for the changed area. Remaining failures are pre-existing, listed above.

  • No game or console files and no binaries included

  • No fake success; anything not possible is left missing with a note

🤖 Generated with Claude Code

**@⁠SchmashinPrunes** on GitHub · 2026-11-21 17:59 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/23) `SchmashinPrunes:lds-atomics` → `main` · +163 −37 · 8 files · 1 commits · closed without merging **Library / area:** tombox-xshader (RDNA2 shader translator) / progress map (RDNA2 instructions). Part of #17. **Exports changed (exports.txt rows):** none **Source of the behaviour:** RDNA2 ISA DS semantics as encoded by LLVM's AMDGPU backend for GFX10 (`llvm/lib/Target/AMDGPU/DSInstructions.td`, LangRef `atomicrmw`). The same semantics are already used and tested by the GDS path (`src/hlsl/gds.rs`, `tests/exec_mem/gds.rs`). No console files used. ### What changed The LDS (group-shared) path only lowered `ds_add/min_u32/max_u32/and/or/xor`. The rest of the DS atomics were lowered for GDS only, so they never counted as verified. This lowers them for LDS too: - **Interlocked\*:** `ds_sub_u32`, `ds_wrxchg_rtn_b32`, `ds_cmpst_b32`. - **Compare-exchange loop** (same shape as the GDS path): `ds_rsub_u32`, `ds_inc_u32`, `ds_dec_u32`, `ds_min_i32`, `ds_max_i32`. `tb_lds` is `uint`, so the signed min/max don't rely on DXC picking a signed `InterlockedMin/Max` overload. - All of the above plus their `_rtn` forms: 15 mnemonics. Semantics worth calling out: - `ds_inc`/`ds_dec` are `uinc_wrap`/`udec_wrap`: `old u>= data ? 0 : old+1` and `(old == 0 || old u> data) ? data : old-1`. - On GFX10, `ds_cmpst` takes **data0 = compare value, data1 = value stored on a match**. LLVM: "the order of src and cmp is the *opposite* of BUFFER_ATOMIC_CMPSWAP"; GFX11 swaps them back as `ds_cmpstore`. ### Tests New `tests/exec_mem/ds.rs` `ds_lds_atomics_ext`: 106 cases, each also rerun with runtime inputs. - **One dword per thread**, read back by the neighbour thread, so the order-dependent ops have fixed results. Inputs hit the edges: old == data, old == 0, neighbouring small values, negative vs positive (signed vs unsigned), and cmpst match / no match. - **All 64 threads on one dword** for the order-independent ops (sub, signed min/max, inc/dec that never wrap), to exercise the retry loop under contention. - **Mutation check:** swapping the cmpst operands, or making `ds_min_i32` compare unsigned, makes the test fail (26 failing cases). ``` TOMBOX_DXC_DIR=<dxc 1.9.2609>\bin\x64 cargo test -p tombox-xshader --release --test exec ds_ test result: ok. 8 passed; 0 failed ds_lds_ext: 15 mnemonics verified, 0 failing, 106 cases, 106 rerun with runtime inputs ``` `python tools/progress/gen.py`: **RDNA2 instructions 676/721 → 691/721** (rebased onto 8cd153d; the code was unchanged by the rebase and upstream has not touched tombox-xshader since the original test run). Only the instruction metric changes in LIBRARY_MAP / MILESTONES / summary / badges. Touches the same generated progress files as other open RDNA2 PRs; whichever merges later just reruns `gen.py`. ### Full `cargo test -p tombox-xshader --release` (Windows 11 LTSC, WARP) No new failures. Failures that are identical on unpatched `main` (fef9a6e): - `exec`: `vop::basic::{salu,vop1,vop2,vop3}_basic`, the same 4 that #15 reports. - `amp` / `fixtures` / `mesh`: 11 tests that need `analysis/shaders/...` dump fixtures (#13 makes these skip). - `mesh::mesh_refusals_are_explicit`: panics at `mesh.rs:247` (`expect("refusal")`) on unpatched `main` too. - [x] `cargo test -p tombox-xshader` passes for the changed area. Remaining failures are pre-existing, listed above. - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The group-shared (LDS) path only lowered add, min_u32, max_u32, and, or and
xor; the other DS atomics were lowered for GDS only. Lower them for LDS too:
sub, wrxchg and cmpst via Interlocked*, and rsub, inc, dec and the signed
min/max via a compare-exchange loop (tb_lds is uint, so a signed Interlocked
overload is not relied on).

Semantics from the RDNA2 ISA as encoded by LLVM's AMDGPU backend for GFX10:
inc/dec are uinc_wrap/udec_wrap (unsigned compares), and DS_CMPST takes
data0 = compare value, data1 = value stored on a match.

tests/exec_mem/ds.rs ds_lds_atomics_ext: 106 cases on WARP, each also rerun
with runtime inputs. Each thread owns a dword (fixed results for the
order-dependent ops; edges: old == data, old == 0, neighbouring small
values, negative vs positive), plus all 64 threads on one dword for the
order-independent ops to exercise the retry loop under contention.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:07:32 +00:00
github-bot closed this pull request 2026-11-26 06:07:32 +00:00
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 18:30 UTC · original

FYI: #25 included LDS atomics too, written before I saw your claim in #17. I've removed them from #25 so it doesn't overlap this PR. One thing to check: #21 changes the GDS base to M0[31:16] (RDNA2 ISA §3.7/§10.5). That only touches hlsl/gds.rs and shouldn't affect your LDS path.

**@⁠QuentinDanblon** on GitHub · 2026-11-21 18:30 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/23#issuecomment-7067302724) FYI: #25 included LDS atomics too, written before I saw your claim in #17. I've removed them from #25 so it doesn't overlap this PR. One thing to check: #21 changes the GDS base to M0[31:16] (RDNA2 ISA §3.7/§10.5). That only touches `hlsl/gds.rs` and shouldn't affect your LDS path.
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-21 18:55 UTC · original

Thanks for the heads-up. I checked #21 against this branch: its hlsl.rs hunk is only the loop-order sort (around line 2230, far from the LDS atomic arm), and the gds.rs change is addressing only (M0[31:16] base plus the size check). The LDS path here doesn't read M0, which matches RDNA2 (no M0 requirement for LDS on GFX9+), so there's no interaction. A test merge of #21 with this branch is clean too.

**@⁠SchmashinPrunes** on GitHub · 2026-11-21 18:55 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/23#issuecomment-7067613985) Thanks for the heads-up. I checked #21 against this branch: its `hlsl.rs` hunk is only the loop-order sort (around line 2230, far from the LDS atomic arm), and the `gds.rs` change is addressing only (`M0[31:16]` base plus the size check). The LDS path here doesn't read M0, which matches RDNA2 (no M0 requirement for LDS on GFX9+), so there's no interaction. A test merge of #21 with this branch is clean too.
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-21 22:12 UTC · original

Closing: this landed on main in 1159c96 (the LDS atomic arm in hlsl.rs and ds_lds_atomics_ext in tests/exec_mem/ds.rs match this branch apart from comments). Thanks!

**@⁠SchmashinPrunes** on GitHub · 2026-11-21 22:12 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/23#issuecomment-7070012039) Closing: this landed on main in 1159c96 (the LDS atomic arm in `hlsl.rs` and `ds_lds_atomics_ext` in `tests/exec_mem/ds.rs` match this branch apart from comments). Thanks!

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.