Shaders: an atomic subtraction on group memory is a compare-exchange loop on NVIDIA #149

Open
notquiteog wants to merge 1 commit from upstream-pr/149 into main
notquiteog commented 2026-11-27 22:46:43 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by notquiteog on 2026-11-27T22:46:43Z.


Problem

On NVIDIA cards, one of the title's compute pipelines is never made: weatherParticle's CS_BeginUpdate (compute 7f61193d2c0e5f22). The driver refuses it with E_OUTOFMEMORY every run, so its dispatches are dropped. We saw this on an RTX 5060 Ti; issue 1034 of fxceless's victory-fork reports the same on an RTX 5080.

It is not memory, and it is not the stack: 2, 64, 256 and 1024 MiB of stack all fail the same way. The program has an atomic subtraction on group-shared memory. The translator lowers the console's ds_sub_u32 to atomicrmw sub, and ds_add_u32 of a value the program negated to atomicrmw add of 0 - x. NVIDIA's compiler rejects both.

We bisected the emitted module by changing one thing at a time:

  • Refused: an add of 0 - x, of x * -1 or of ~x + 1, and atomicrmw sub of x. The WaveSize attribute, the LDS size, and whether the result is read made no difference.
  • Made: an add of x, of a constant, of 5 - x, of -x + 5 or of c ? -x : 0, and a compare-exchange loop.

Change

  • The rewrite: Module::atomic_subtracts turns every atomicrmw sub, and every atomicrmw add of a value written as a negation, into a compare-exchange loop with the same result. The loop reads the dword, exchanges it for itself less the value until no other thread wrote in between, and returns the old value. The block is split at the atomic, and phis that named it now name the new tail block. It runs in Shader::finish, after this tree's module clean-up passes (optimise), so it sees what they leave.
  • When it applies: only when the host adapter is an NVIDIA card (vendor 0x10DE). The vendor is read from the default DXGI adapter, the one D3D12CreateDevice(None) takes. Other hosts get exactly the code they get today. VICTORY_ATOMIC_SUB_LOOP=1 or =0 overrides this on any host.
  • Shader cache: the setting is part of the cache key (build).

The same change is open on Marvisak/VIctory as pull request 3619. Upstream pull request 3142, which turns ds_sub_u32 into an add of the negated value, does not cover this: NVIDIA refuses that form too.

Tested

  • cargo build --release.
  • cargo clippy -p victory-shader -p victory-gpu -p victory --all-targets -- -D warnings, with and without --all-features. A workspace-wide clippy stops at the victory-game example shaderpack_states, on main too.
  • New unit test bitcode::tests::atomic_subtractions_become_loops. It checks that two of four atomics become loops, the block split and phi renaming, and the three spellings of a negation.
  • cargo test -p victory-shader --lib emit::bitcode:: (17 passed) and cargo test -p victory-gpu --lib shader_cache pass.
  • In our fork, the same pass also has a GPU test. 64 threads take their index off one LDS dword, as ds_add_u32 of the negated index and as ds_sub_u32, with the old value checked. It passes on the RTX 5060 Ti and on WARP. In play, the pipeline is made on the 5060 Ti. That test needs ds helpers this tree's valu_tests doesn't have, so it is not included here.

Measured

Before: CS_BeginUpdate is refused on every run, so 1 pipeline is missing. After: the pipeline is made. The extra loop only costs time when threads contend on the same dword, and only in that one program.

Port from notquiteog/VIctory, commit e4022a6e84 (the translator part; our tree's other host plumbing is left out).

🤖 Generated with Claude Code

<!-- greyfinch-sync:DexNom/VIctory:issue:5534073079 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/149). Originally posted by [notquiteog](https://github.com/notquiteog) on 2026-11-27T22:46:43Z. --- ### Problem On NVIDIA cards, one of the title's compute pipelines is never made: weatherParticle's `CS_BeginUpdate` (compute `7f61193d2c0e5f22`). The driver refuses it with `E_OUTOFMEMORY` every run, so its dispatches are dropped. We saw this on an RTX 5060 Ti; issue 1034 of fxceless's victory-fork reports the same on an RTX 5080. It is not memory, and it is not the stack: 2, 64, 256 and 1024 MiB of stack all fail the same way. The program has an atomic subtraction on group-shared memory. The translator lowers the console's `ds_sub_u32` to `atomicrmw sub`, and `ds_add_u32` of a value the program negated to `atomicrmw add` of `0 - x`. NVIDIA's compiler rejects both. We bisected the emitted module by changing one thing at a time: - **Refused:** an add of `0 - x`, of `x * -1` or of `~x + 1`, and `atomicrmw sub` of `x`. The WaveSize attribute, the LDS size, and whether the result is read made no difference. - **Made:** an add of `x`, of a constant, of `5 - x`, of `-x + 5` or of `c ? -x : 0`, and a compare-exchange loop. ### Change - **The rewrite:** `Module::atomic_subtracts` turns every `atomicrmw sub`, and every `atomicrmw add` of a value written as a negation, into a compare-exchange loop with the same result. The loop reads the dword, exchanges it for itself less the value until no other thread wrote in between, and returns the old value. The block is split at the atomic, and phis that named it now name the new tail block. It runs in `Shader::finish`, after this tree's module clean-up passes (`optimise`), so it sees what they leave. - **When it applies:** only when the host adapter is an NVIDIA card (vendor `0x10DE`). The vendor is read from the default DXGI adapter, the one `D3D12CreateDevice(None)` takes. Other hosts get exactly the code they get today. `VICTORY_ATOMIC_SUB_LOOP=1` or `=0` overrides this on any host. - **Shader cache:** the setting is part of the cache key (`build`). The same change is open on Marvisak/VIctory as pull request 3619. Upstream pull request 3142, which turns `ds_sub_u32` into an add of the negated value, does not cover this: NVIDIA refuses that form too. ### Tested - `cargo build --release`. - `cargo clippy -p victory-shader -p victory-gpu -p victory --all-targets -- -D warnings`, with and without `--all-features`. A workspace-wide clippy stops at the `victory-game` example `shaderpack_states`, on `main` too. - New unit test `bitcode::tests::atomic_subtractions_become_loops`. It checks that two of four atomics become loops, the block split and phi renaming, and the three spellings of a negation. - `cargo test -p victory-shader --lib emit::bitcode::` (17 passed) and `cargo test -p victory-gpu --lib shader_cache` pass. - In our fork, the same pass also has a GPU test. 64 threads take their index off one LDS dword, as `ds_add_u32` of the negated index and as `ds_sub_u32`, with the old value checked. It passes on the RTX 5060 Ti and on WARP. In play, the pipeline is made on the 5060 Ti. That test needs `ds` helpers this tree's `valu_tests` doesn't have, so it is not included here. ### Measured Before: `CS_BeginUpdate` is refused on every run, so 1 pipeline is missing. After: the pipeline is made. The extra loop only costs time when threads contend on the same dword, and only in that one program. Port from notquiteog/VIctory, commit e4022a6e84 (the translator part; our tree's other host plumbing is left out). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
retrope13 commented 2026-11-28 03:05:53 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by retrope13 on 2026-11-28T03:05:53Z.


Audited and hardware-tested head b743a613 on Windows 11 with an NVIDIA RTX 4060 Laptop GPU (Ada Lovelace) and WARP — the rewrite pass looks solid and all suites pass.

Environment

  • OS / CPU / RAM: Windows 11 26H2 (26300.9818), Intel Core i5-14500HX, 24 GB RAM
  • GPU (DXGI default adapter [0]): NVIDIA GeForce RTX 4060 Laptop GPU (0x10DE, 7,956 MiB VRAM, driver 32.0.16.2140) + WARP
  • Toolchain: rustc 1.98.1 (48a229cea 2026-09-01)

Tested

  • cargo clippy --locked -p victory-shader -p victory-gpu -p victory --all-targets -- -D warnings — clean
  • cargo clippy --locked -p victory-shader -p victory-gpu -p victory --all-targets --all-features -- -D warnings — clean
  • cargo test --locked -p victory-shader --lib emit::bitcode:: — 17 passed, 0 failed, 1 ignored (including atomic_subtractions_become_loops)
  • cargo test --locked -p victory-gpu --lib shader_cache — 2 passed, 0 failed
  • cargo test --locked -p victory-gpu -- --test-threads=1 — 219 passed, 0 failed, 4 ignored
  • cargo test --locked -p victory-shader --lib — 240 passed, 0 failed, 4 ignored
  • 64-thread hardware probe (temporary local test in valu_tests.rs, reverted after run):
    • Lowered 6 synthetic 64-thread compute shaders (ds_sub_u32, ds_add_u32 of 0 - tid, ds_sub_rtn_u32 summing all 64 returned old values into LDS[4], ds_add_rtn_u32 of 0 - v2 summing all 64 old values, plus dynamic LDS pointer variants of ds_sub_u32 and ds_add_u32 of 0 - tid).
    • With set_atomic_sub_loops(true): confirmed emitted DXIL changed on all 6 cases, passed DXIL validation, compiled Ok on RTX 4060 and WARP, and read back exact results on both (7984 for 10000 - sum(0..64) and 637984 for sum(9937..=10000) of returned old values).
    • Side observation on Ada Lovelace (32.0.16.2140): with set_atomic_sub_loops(false), those 6 minimal synthetic shaders did compile Ok on the RTX 4060 (so E_OUTOFMEMORY did not reproduce on these minimal shaders on Ada/562+/592+ driver, though enabling the CAS loop for all 0x10DE adapters caused zero regressions here).

Code Review Notes

  • Verified atomic_subtracts interacts cleanly with Module::write's RPO block ordering, live() phi liveness, self-loop phi rewriting, and empty-latch folding.
  • Two minor observations:
    1. victory warm and victory run both go through victory_gpu::shader_cache::host_shader (calling host_settings()), while victory translate --pipelines (apps/victory/src/translate.rs::run) invokes victory_shader::emit directly without calling host_settings() — so VICTORY_ATOMIC_SUB_LOOP and NVIDIA auto-detection don't apply in victory translate --pipelines yet.
    2. This tree (4ad86927) already has ds(), vop2(), S_BARRIER, DS_WRITE_B32, DS_READ_B32, and build_cs(&body, 64, 512) in crates/victory-shader/src/emit/valu_tests.rs (from the ds_min_max_f32 tests), so the 64-thread GPU test from your fork could actually be included directly here if you'd like.

Not Tested

  • Retail CS_BeginUpdate (7f61193d2c0e5f22) or live title runs.
  • Blackwell GPUs (RTX 50-series) or AMD/Intel hardware adapters.

AI Disclosure

  • Antigravity (Gemini 4) assisted with auditing the IR pass, writing the temporary 64-thread GPU probe, and summarizing these results; all builds, lints, and GPU tests were executed locally on my machine.
<!-- greyfinch-sync:DexNom/VIctory:comment:7232792143 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/149#issuecomment-7232792143). Originally posted by [retrope13](https://github.com/retrope13) on 2026-11-28T03:05:53Z. --- Audited and hardware-tested head `b743a613` on Windows 11 with an NVIDIA RTX 4060 Laptop GPU (Ada Lovelace) and WARP — the rewrite pass looks solid and all suites pass. ### Environment - **OS / CPU / RAM:** Windows 11 26H2 (`26300.9818`), Intel Core i5-14500HX, 24 GB RAM - **GPU (DXGI default adapter `[0]`):** NVIDIA GeForce RTX 4060 Laptop GPU (`0x10DE`, 7,956 MiB VRAM, driver `32.0.16.2140`) + WARP - **Toolchain:** `rustc 1.98.1 (48a229cea 2026-09-01)` ### Tested - `cargo clippy --locked -p victory-shader -p victory-gpu -p victory --all-targets -- -D warnings` — clean - `cargo clippy --locked -p victory-shader -p victory-gpu -p victory --all-targets --all-features -- -D warnings` — clean - `cargo test --locked -p victory-shader --lib emit::bitcode::` — **17 passed, 0 failed, 1 ignored** (including `atomic_subtractions_become_loops`) - `cargo test --locked -p victory-gpu --lib shader_cache` — **2 passed, 0 failed** - `cargo test --locked -p victory-gpu -- --test-threads=1` — **219 passed, 0 failed, 4 ignored** - `cargo test --locked -p victory-shader --lib` — **240 passed, 0 failed, 4 ignored** - **64-thread hardware probe (temporary local test in `valu_tests.rs`, reverted after run):** - Lowered 6 synthetic 64-thread compute shaders (`ds_sub_u32`, `ds_add_u32` of `0 - tid`, `ds_sub_rtn_u32` summing all 64 returned old values into `LDS[4]`, `ds_add_rtn_u32` of `0 - v2` summing all 64 old values, plus dynamic LDS pointer variants of `ds_sub_u32` and `ds_add_u32` of `0 - tid`). - With `set_atomic_sub_loops(true)`: confirmed emitted DXIL changed on all 6 cases, passed DXIL validation, compiled `Ok` on RTX 4060 and WARP, and read back exact results on both (`7984` for `10000 - sum(0..64)` and `637984` for `sum(9937..=10000)` of returned old values). - Side observation on Ada Lovelace (`32.0.16.2140`): with `set_atomic_sub_loops(false)`, those 6 minimal synthetic shaders did compile `Ok` on the RTX 4060 (so `E_OUTOFMEMORY` did not reproduce on these minimal shaders on Ada/562+/592+ driver, though enabling the CAS loop for all `0x10DE` adapters caused zero regressions here). ### Code Review Notes - Verified `atomic_subtracts` interacts cleanly with `Module::write`'s RPO block ordering, `live()` phi liveness, self-loop phi rewriting, and empty-latch folding. - Two minor observations: 1. `victory warm` and `victory run` both go through `victory_gpu::shader_cache::host_shader` (calling `host_settings()`), while `victory translate --pipelines` (`apps/victory/src/translate.rs::run`) invokes `victory_shader::emit` directly without calling `host_settings()` — so `VICTORY_ATOMIC_SUB_LOOP` and NVIDIA auto-detection don't apply in `victory translate --pipelines` yet. 2. This tree (`4ad86927`) already has `ds()`, `vop2()`, `S_BARRIER`, `DS_WRITE_B32`, `DS_READ_B32`, and `build_cs(&body, 64, 512)` in `crates/victory-shader/src/emit/valu_tests.rs` (from the `ds_min_max_f32` tests), so the 64-thread GPU test from your fork could actually be included directly here if you'd like. ### Not Tested - Retail `CS_BeginUpdate` (`7f61193d2c0e5f22`) or live title runs. - Blackwell GPUs (RTX 50-series) or AMD/Intel hardware adapters. ### AI Disclosure - Antigravity (Gemini 4) assisted with auditing the IR pass, writing the temporary 64-thread GPU probe, and summarizing these results; all builds, lints, and GPU tests were executed locally on my machine.
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/149:upstream-pr/149'
git switch 'upstream-pr/149'

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/149'
git switch 'upstream-pr/149'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/149'
git switch 'upstream-pr/149'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/149'
git switch 'main'
git merge --squash 'upstream-pr/149'
git switch 'main'
git merge --ff-only 'upstream-pr/149'
git switch 'main'
git merge 'upstream-pr/149'
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
DexNom/VIctory!149
No description provided.