Shaders: lower ds_rsub_u32 and ds_rsub_rtn_u32 in pure 32-bit DXIL #2116

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

@⁠geiyiliyabumo on GitHub · 2026-11-25 05:19 UTC · original
geiyiliyabumo:feat/shader-ds-rsub → main · +128 −1 · 2 files · 1 commits · open

Summary

  • Lowers GFX10 LDS atomic reverse-subtract instructions ds_rsub_u32 (0x002) and ds_rsub_rtn_u32 (0x022) in crates/victory-shader/src/emit/lower.rs (fn ds) via an EXEC-guarded pure 32-bit cmpxchg retry loop (new = data0.wrapping_sub(old)) without enabling Int64 / WIDE_OPS.
  • Preserves LDS and vdst on inactive EXEC lanes and returns the exchanged old value for ds_rsub_rtn_u32.
  • Adds synthetic DXIL-validation and GPU execution tests in crates/victory-shader/src/emit/semantics_tests.rs covering both opcodes, nonzero byte offsets, wraparound, zero/u32::MAX, EXEC masking, 64-lane concurrent contention, and verifying Int64 is not set.

Verification

  • cargo xtask policy: passed
  • cargo clippy -p victory-shader --tests -- -D warnings: passed
  • cargo test -p victory-shader --lib lds_rsub -- --test-threads=1 with Microsoft dxil.dll: 2/2 passed on AMD Radeon RX 9060 XT (RDNA4) and Microsoft Basic Render Driver (WARP)
  • Zero game files or game-derived data used (synthetic tests only).
**@⁠geiyiliyabumo** on GitHub · 2026-11-25 05:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2116) `geiyiliyabumo:feat/shader-ds-rsub` → `main` · +128 −1 · 2 files · 1 commits · open ## Summary - Lowers GFX10 LDS atomic reverse-subtract instructions `ds_rsub_u32` (`0x002`) and `ds_rsub_rtn_u32` (`0x022`) in `crates/victory-shader/src/emit/lower.rs` (`fn ds`) via an `EXEC`-guarded pure 32-bit `cmpxchg` retry loop (`new = data0.wrapping_sub(old)`) without enabling `Int64` / `WIDE_OPS`. - Preserves LDS and `vdst` on inactive `EXEC` lanes and returns the exchanged `old` value for `ds_rsub_rtn_u32`. - Adds synthetic DXIL-validation and GPU execution tests in `crates/victory-shader/src/emit/semantics_tests.rs` covering both opcodes, nonzero byte offsets, wraparound, zero/`u32::MAX`, `EXEC` masking, 64-lane concurrent contention, and verifying `Int64` is not set. ## Verification - `cargo xtask policy`: passed - `cargo clippy -p victory-shader --tests -- -D warnings`: passed - `cargo test -p victory-shader --lib lds_rsub -- --test-threads=1` with Microsoft `dxil.dll`: 2/2 passed on **AMD Radeon RX 9060 XT (RDNA4)** and **Microsoft Basic Render Driver (WARP)** - Zero game files or game-derived data used (synthetic tests only).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:55:18 +00:00
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 05:26 UTC · original

Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 6d5e03d. victory-shader: 246 passed, 3 failed, 3 ignored. The new lds_rsub_respects_exec_and_returns_the_old_dword and lds_rsub_serializes_contending_lanes pass. The 3 failures are #1744's NVIDIA failures on main. Same as main on this hardware.

The lowering uses a compare-exchange retry, not an atomicrmw sub of a register. That matters on newer NVIDIA cards: xcl-dev/xcl#1919 found RTX 50 drivers (617 to 621) refusing compute pipelines with a groupshared atomic subtract of a register (E_OUTOFMEMORY), so this form avoids that.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 05:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7153773377) Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at `6d5e03d`. `victory-shader`: 246 passed, 3 failed, 3 ignored. The new `lds_rsub_respects_exec_and_returns_the_old_dword` and `lds_rsub_serializes_contending_lanes` pass. The 3 failures are #1744's NVIDIA failures on `main`. Same as `main` on this hardware. The lowering uses a compare-exchange retry, not an `atomicrmw sub` of a register. That matters on newer NVIDIA cards: xcl-dev/xcl#1919 found RTX 50 drivers (617 to 621) refusing compute pipelines with a groupshared atomic subtract of a register (`E_OUTOFMEMORY`), so this form avoids that. AI-assisted (Claude Code).
Author
Owner

@⁠fiestorra on GitHub · 2026-11-25 06:23 UTC · original

Checked the operand order, which is the whole risk here: it is src − old, so the reversal is right

ds_rsub is the one instruction in this family where getting the operands the wrong way round produces no error, no refusal and no crash — just a value that is wrong for every lane, forever. So that is what I checked first:

let new  = self.s.m.bin(retry, Bin::Sub, v0, expected);   // v0 = src, expected = the observed old
let old  = self.s.m.cmpxchg(retry, p, expected, new);

new = src − old, and _rtn returns the exchanged old. That is the reverse subtract the name says. A plain ds_add written as old − src would compile and run too, and nothing in the corpus would notice, so it is worth saying out loud that this one is right.

The loop is the standard shape (start expected at zero, cmpxchg, on failure retry with the value the exchange returned) and on success old == expected, so the returned value is the pre-exchange dword. Tests on this branch: 249 passed, 3 ignored, and the two that matter by name:

emit::semantics_tests::lds_rsub_serializes_contending_lanes                ok
emit::semantics_tests::lds_rsub_respects_exec_and_returns_the_old_dword    ok

The cases I would add, because they are where an unsigned subtract under test coverage goes quietly wrong: src = 0 with old = 0 (result zero), and src < old (a wrap to a large value). Both are one line each in the same test, and both are the kind of result a reviewer looks at for two seconds and remembers.

The same two things I raised on #2113 apply here, and I would rather repeat them once than have them lost between threads:

  • the retry loop is unbounded. Under contention that is normal practice and it terminates, but the failure mode when it does not is a TDR and a removed device, and the log will not say which shader was spinning. Either the argument ("the address is not contended across the wave") or a bounded retry with a defined fallback — but a decision written down;
  • this pull request conflicts with #2113 (in lower.rs and semantics_tests.rs); each is clean against main, so nothing is broken, but the three of them from the same week are one family and merging two of them will cost a manual resolution. One pull request with the whole family, or a stated order, would be cheaper.

No game data is involved: instruction names and test counts.

Written with an AI assistant.

**@⁠fiestorra** on GitHub · 2026-11-25 06:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7154738367) **Checked the operand order, which is the whole risk here: it is `src − old`, so the reversal is right** `ds_rsub` is the one instruction in this family where getting the operands the wrong way round produces no error, no refusal and no crash — just a value that is wrong for every lane, forever. So that is what I checked first: ```rust let new = self.s.m.bin(retry, Bin::Sub, v0, expected); // v0 = src, expected = the observed old let old = self.s.m.cmpxchg(retry, p, expected, new); ``` `new = src − old`, and `_rtn` returns the exchanged `old`. That is the reverse subtract the name says. A plain `ds_add` written as `old − src` would compile and run too, and nothing in the corpus would notice, so it is worth saying out loud that this one is right. The loop is the standard shape (start `expected` at zero, `cmpxchg`, on failure retry with the value the exchange returned) and on success `old == expected`, so the returned value is the pre-exchange dword. Tests on this branch: **249 passed, 3 ignored**, and the two that matter by name: ``` emit::semantics_tests::lds_rsub_serializes_contending_lanes ok emit::semantics_tests::lds_rsub_respects_exec_and_returns_the_old_dword ok ``` **The cases I would add**, because they are where an unsigned subtract under test coverage goes quietly wrong: `src = 0` with `old = 0` (result zero), and `src < old` (a wrap to a large value). Both are one line each in the same test, and both are the kind of result a reviewer looks at for two seconds and remembers. **The same two things I raised on #2113 apply here**, and I would rather repeat them once than have them lost between threads: - **the retry loop is unbounded.** Under contention that is normal practice and it terminates, but the failure mode when it does not is a **TDR and a removed device**, and the log will not say which shader was spinning. Either the argument ("the address is not contended across the wave") or a bounded retry with a defined fallback — but a decision written down; - **this pull request conflicts with #2113** (in `lower.rs` and `semantics_tests.rs`); each is clean against `main`, so nothing is broken, but the three of them from the same week are one family and merging two of them will cost a manual resolution. One pull request with the whole family, or a stated order, would be cheaper. No game data is involved: instruction names and test counts. Written with an AI assistant.
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 08:38 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head 6d5e03df 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).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 08:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7157290809) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 6d5e03df 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).
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-25 09:18 UTC · original

Tested 6d5e03df on an RTX 5080 with driver 621.40, following up on the question about RTX 50-series drivers and shared-memory atomic subtraction. The compute pipeline was created successfully and both reverse-subtract tests passed.

Test RTX 5080 Built-in WARP
lds_rsub_respects_exec_and_returns_the_old_dword 2 readbacks, passed 2 readbacks, passed
lds_rsub_serializes_contending_lanes 1 readback, passed 1 readback, passed

I ran the PR's compare-exchange implementation on each adapter separately and verified the D3D12 device and completed dispatches. This covers masked execution, the returned old value, and contention on this Blackwell card. Windows 11 build 26300.10075, built-in WARP 10.0.26100.10068. Earlier driver discussion

**@⁠steven-seagull27** on GitHub · 2026-11-25 09:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7158147138) Tested `6d5e03df` on an RTX 5080 with driver 621.40, following up on the question about RTX 50-series drivers and shared-memory atomic subtraction. The compute pipeline was created successfully and both reverse-subtract tests passed. | Test | RTX 5080 | Built-in WARP | | --- | --- | --- | | `lds_rsub_respects_exec_and_returns_the_old_dword` | 2 readbacks, passed | 2 readbacks, passed | | `lds_rsub_serializes_contending_lanes` | 1 readback, passed | 1 readback, passed | I ran the PR's compare-exchange implementation on each adapter separately and verified the D3D12 device and completed dispatches. This covers masked execution, the returned old value, and contention on this Blackwell card. Windows 11 build 26300.10075, built-in WARP 10.0.26100.10068. [Earlier driver discussion](https://git.gtahub.st/github/Marvisak--VIctory/pulls/2116)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 10:44 UTC · original · edited 2026-11-25 22:20 UTC

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main: main has moved since the last triage, and the branch now conflicts in crates/victory-shader/src/emit/semantics_tests.rs. It will be reviewed again once it is rebased onto current main.

Previous verdict for this head: merge candidate.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 10:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7160107429) · edited 2026-11-25 22:20 UTC <!-- victory-triage v1 head=6d5e03df3e637207d8ab232016efc9886290ad20 verdict=needs-rebase at=2026-11-25T22:24:00Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main`: `main` has moved since the last triage, and the branch now conflicts in `crates/victory-shader/src/emit/semantics_tests.rs`. It will be reviewed again once it is rebased onto current `main`. Previous verdict for this head: merge candidate. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/semantics_tests.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/2116/head:github-bot/gh-2116'
git switch 'github-bot/gh-2116'

Merge

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