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

Open
geiyiliyabumo wants to merge 1 commit from upstream-pr/2116 into main
geiyiliyabumo commented 2026-11-25 05:19:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-25T05:19:01Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6597775830 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-25T05:19:01Z. --- ## 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).
DenRakEiw commented 2026-11-25 06:02:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T05:26:28Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib rsub — 2 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1 (lds_rsub_respects_exec_and_returns_the_old_dword and lds_rsub_serializes_contending_lanes)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean pure-32-bit DXIL lowering of ds_rsub_u32 and ds_rsub_rtn_u32 verified under EXEC masking and lane contention on hardware and WARP. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6052281042 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116#pullrequestreview-6052281042). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T05:26:28Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with `dxil.dll` validator): - `cargo test -p victory-shader --lib rsub` — **2 passed, 0 failed** on both RTX 5090 hardware and `VICTORY_D3D12_WARP=1` (`lds_rsub_respects_exec_and_returns_the_old_dword` and `lds_rsub_serializes_contending_lanes`) - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean pure-32-bit DXIL lowering of `ds_rsub_u32` and `ds_rsub_rtn_u32` verified under EXEC masking and lane contention on hardware and WARP. Looks good. AI-assisted (Gemini 4 Neon).
upipibi commented 2026-11-25 06:02:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T05:26:26Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7153773377 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7153773377). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-25T05:26:26Z. --- 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).
catminou4 commented 2026-11-25 07:02:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T06:33:48Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. ds_rsub_u32 and ds_rsub_rtn_u32 lowered in pure 32-bit DXIL — reverse-subtract atomics.

<!-- greyfinch-sync:Marvisak/VIctory:review:6052899873 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116#pullrequestreview-6052899873). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T06:33:48Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 144 passed / 0 failed / 2 ignored. ds_rsub_u32 and ds_rsub_rtn_u32 lowered in pure 32-bit DXIL — reverse-subtract atomics.
fiestorra commented 2026-11-25 07:03:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fiestorra on 2026-11-25T06:23:17Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154738367 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7154738367). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-11-25T06:23:17Z. --- **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.
sinan-cmyk commented 2026-11-25 09:06:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T08:38:35Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7157290809 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7157290809). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T08:38:35Z. --- ### 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).
steven-seagull27 commented 2026-11-25 10:07:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by steven-seagull27 on 2026-11-25T09:18:10Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158147138 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7158147138). Originally posted by [steven-seagull27](https://github.com/steven-seagull27) on 2026-11-25T09:18:10Z. --- 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://github.com/Marvisak/VIctory/pull/2116#issuecomment-7153773377)
Marvisak commented 2026-11-25 11:05:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T10:44:24Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7160107429 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2116#issuecomment-7160107429). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T10:44:24Z. --- <!-- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2116:upstream-pr/2116'
git switch 'upstream-pr/2116'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2116'
git switch 'upstream-pr/2116'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2116'
git switch 'upstream-pr/2116'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2116'
git switch 'main'
git merge --squash 'upstream-pr/2116'
git switch 'main'
git merge --ff-only 'upstream-pr/2116'
git switch 'main'
git merge 'upstream-pr/2116'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2114
No description provided.