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

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

Imported from Marvisak/VIctory. Originally posted by notquiteog on 2026-11-27T22:33:03Z.


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, before the shader flags are read from the module.
  • 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, and translate --pipelines translates as the runtime does for its adapter.

This is separate from pull request 3142, which turns ds_sub_u32 into an add of the negated value; NVIDIA refuses that form too. The pass catches both forms, so the two PRs can land in either order.

Tested

  • cargo build --release, and both clippy runs from CI (-D warnings, with and without --all-features).
  • 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:: 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:Marvisak/VIctory:issue:5533952926 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3619). Originally posted by [notquiteog](https://github.com/notquiteog) on 2026-11-27T22:33:03Z. --- ### 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`, before the shader flags are read from the module. - **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, and `translate --pipelines` translates as the runtime does for its adapter. This is separate from pull request 3142, which turns `ds_sub_u32` into an add of the negated value; NVIDIA refuses that form too. The pass catches both forms, so the two PRs can land in either order. ### Tested - `cargo build --release`, and both clippy runs from CI (`-D warnings`, with and without `--all-features`). - 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::` 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)
upipibi commented 2026-11-27 23:01:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-27T23:01:06Z.


xcl ran into the same driver refusal earlier, and two of its measurements bear on this PR (read from their threads, not run here):

  • Which cards. The refusal follows the GPU generation. On xcl#1919, an RTX 3080 Ti on driver 617.42 (the version that refuses on the RTX 5060 in xcl#1905) built the groupshared add of 0 - v (xcl main's form at the time) and got the values right (report). Every refusal there is from RTX 50 cards (5060, 5060 Ti, 5070, 5090 failing on main in those reports), as are this PR's 5060 Ti and the RTX 5080 report. host_settings (shader_cache.rs:86-91 at 47bdeca) turns the loops on for every 0x10DE adapter, so Ampere and Ada cards would get them as well.
  • What the loop costs. xcl#1919's first head used a compare-exchange loop too, and it was replaced because of its cost under contention. In their synthetic cs_6_6 measurement (32 threads, 200,000 iterations), 32 lanes on one slot took 318.9 ms with the loop, against 7.5 ms for a plain InterlockedAdd. With 4 lanes per slot it was 30.3 vs 8.8 ms, and with a slot per lane 11.6 vs 8.8 ms. Their final form keeps the native atomic add and hides the negation from the driver: the operand is OR-ed with a wave-uniform zero (WaveActiveBitOr(0)), so the driver can't recognise the negation. That matches your bisection, where c ? -x : 0 was accepted.

If many lanes of the weather-particle program update one counter, the contended rows are the ones that apply. Neither project has tested an atomicrmw sub on Ampere directly. I have an RTX 3070 and can build both of the forms your bisection refused, with VICTORY_ATOMIC_SUB_LOOP=0, to check where the boundary falls for VIctory's own output.

AI-assisted (Claude Code, Claude Opus 5.5 read both PRs' threads and this head).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7229804328 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3619#issuecomment-7229804328). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-27T23:01:06Z. --- xcl ran into the same driver refusal earlier, and two of its measurements bear on this PR (read from their threads, not run here): - **Which cards.** The refusal follows the GPU generation. On xcl#1919, an RTX 3080 Ti on driver 617.42 (the version that refuses on the RTX 5060 in xcl#1905) built the groupshared add of `0 - v` (xcl main's form at the time) and got the values right ([report](https://github.com/xcl-dev/xcl/pull/1919#issuecomment-7154240231)). Every refusal there is from RTX 50 cards (5060, 5060 Ti, 5070, 5090 failing on main in those reports), as are this PR's 5060 Ti and the RTX 5080 report. `host_settings` (`shader_cache.rs:86-91` at `47bdeca`) turns the loops on for every `0x10DE` adapter, so Ampere and Ada cards would get them as well. - **What the loop costs.** xcl#1919's first head used a compare-exchange loop too, and it was replaced because of its cost under contention. In their synthetic `cs_6_6` measurement (32 threads, 200,000 iterations), 32 lanes on one slot took 318.9 ms with the loop, against 7.5 ms for a plain `InterlockedAdd`. With 4 lanes per slot it was 30.3 vs 8.8 ms, and with a slot per lane 11.6 vs 8.8 ms. Their final form keeps the native atomic add and hides the negation from the driver: the operand is OR-ed with a wave-uniform zero (`WaveActiveBitOr(0)`), so the driver can't recognise the negation. That matches your bisection, where `c ? -x : 0` was accepted. If many lanes of the weather-particle program update one counter, the contended rows are the ones that apply. Neither project has tested an `atomicrmw sub` on Ampere directly. I have an RTX 3070 and can build both of the forms your bisection refused, with `VICTORY_ATOMIC_SUB_LOOP=0`, to check where the boundary falls for VIctory's own output. AI-assisted (Claude Code, Claude Opus 5.5 read both PRs' threads and this head).
upipibi commented 2026-11-28 04:17:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T04:17:10Z.


The run I promised, on an RTX 3070 8 GB (Ampere), driver 616.56, Windows 11, at this head 47bdeca. It uses a local probe test, not pushed, built with this tree's probe/check helpers in semantics_tests.rs. Each lane writes a to its own LDS dword, does the atomic with b, and returns the old value, then reads the dword back. Each case covers 340 input pairs, compared with the model, run twice per host, with the same output both times.

Program RTX 3070, loops off RTX 3070, loops on WARP, loops off WARP, loops on
ds_add_rtn_u32 (control) 0 mismatches 0 0 0
ds_sub_rtn_u32 0 mismatches 0 device suspended 0
v_sub_nc_u32 v13, 0, b + ds_add_rtn_u32 [own], v13 0 mismatches 0 device suspended 0
  • Ampere doesn't refuse. With the loops off (the same translation as main), the 3070 builds both pipelines and gets every value right. That fits xcl's finding that the refusal follows the RTX 50 generation, so turning the loops on for every NVIDIA adapter costs older cards a loop they don't need. It's correct there either way.
  • WARP fails without the loops. The same two programs suspend the WARP device ("The GPU device instance has been suspended"), every time, while the plain ds_add_rtn_u32 next to them runs fine. With the loops on, WARP is correct. This is the system WARP (d3d10warp.dll 10.0.26100.9711, no app-local copy beside the test binary); app-local WARP 1.0.22, which other runs here use, was not tried.

That bears on the default. host_settings turns the loops on only when the first DXGI adapter is NVIDIA (default_adapter_vendor, host.rs:288-295). A machine whose first adapter is Microsoft Basic Render (no GPU, as on CI), AMD or Intel, running on WARP, translates without the loops and would lose the device on these programs, as main does today. Turning the loops on whenever the device is WARP would cover that.

Narrowed down since (same card and setup): the non-returning ds_sub_u32 also loses the WARP device with the loops off, while the RTX 3070 gets it right (340 inputs, 0 mismatches). So it isn't the returned value. It's the group-shared atomicrmw sub itself, and atomicrmw add of a negation, which WARP most likely folds into the same thing. A likely reason both NVIDIA's RTX 50 compiler and WARP trip on exactly these two forms: HLSL has no InterlockedSub, so DXC never emits atomicrmw sub on group-shared memory, and a driver path DXC never exercises goes untested. If that holds, the loops are the safe default on every host, with a switch to turn them off where a card is known to be fine. main refuses ds_rsub_rtn_u32 and ds_dec_rtn_u32 before lowering, so they never reach a driver.

AI-assisted (Claude Code, Claude Opus 5.5 wrote the local probe and ran it on this card).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7233886777 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3619#issuecomment-7233886777). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-28T04:17:10Z. --- The run I promised, on an RTX 3070 8 GB (Ampere), driver 616.56, Windows 11, at this head `47bdeca`. It uses a local probe test, not pushed, built with this tree's `probe`/`check` helpers in `semantics_tests.rs`. Each lane writes `a` to its own LDS dword, does the atomic with `b`, and returns the old value, then reads the dword back. Each case covers 340 input pairs, compared with the model, run twice per host, with the same output both times. | Program | RTX 3070, loops off | RTX 3070, loops on | WARP, loops off | WARP, loops on | |---|---|---|---|---| | `ds_add_rtn_u32` (control) | 0 mismatches | 0 | 0 | 0 | | `ds_sub_rtn_u32` | **0 mismatches** | 0 | **device suspended** | 0 | | `v_sub_nc_u32 v13, 0, b` + `ds_add_rtn_u32 [own], v13` | **0 mismatches** | 0 | **device suspended** | 0 | - **Ampere doesn't refuse.** With the loops off (the same translation as `main`), the 3070 builds both pipelines and gets every value right. That fits xcl's finding that the refusal follows the RTX 50 generation, so turning the loops on for every NVIDIA adapter costs older cards a loop they don't need. It's correct there either way. - **WARP fails without the loops.** The same two programs suspend the WARP device ("The GPU device instance has been suspended"), every time, while the plain `ds_add_rtn_u32` next to them runs fine. With the loops on, WARP is correct. This is the system WARP (`d3d10warp.dll` 10.0.26100.9711, no app-local copy beside the test binary); app-local WARP 1.0.22, which other runs here use, was not tried. That bears on the default. `host_settings` turns the loops on only when the first DXGI adapter is NVIDIA (`default_adapter_vendor`, `host.rs:288-295`). A machine whose first adapter is Microsoft Basic Render (no GPU, as on CI), AMD or Intel, running on WARP, translates without the loops and would lose the device on these programs, as `main` does today. Turning the loops on whenever the device is WARP would cover that. **Narrowed down since** (same card and setup): the non-returning `ds_sub_u32` also loses the WARP device with the loops off, while the RTX 3070 gets it right (340 inputs, 0 mismatches). So it isn't the returned value. It's the group-shared `atomicrmw sub` itself, and `atomicrmw add` of a negation, which WARP most likely folds into the same thing. A likely reason both NVIDIA's RTX 50 compiler and WARP trip on exactly these two forms: HLSL has no `InterlockedSub`, so DXC never emits `atomicrmw sub` on group-shared memory, and a driver path DXC never exercises goes untested. If that holds, the loops are the safe default on every host, with a switch to turn them off where a card is known to be fine. `main` refuses `ds_rsub_rtn_u32` and `ds_dec_rtn_u32` before lowering, so they never reach a driver. AI-assisted (Claude Code, Claude Opus 5.5 wrote the local probe and ran it on this card).
Aniseto commented 2026-11-28 08:07:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Aniseto on 2026-11-28T08:07:26Z.


Ran the group-memory atomic subtraction tests at 47bdeca on an NVIDIA Blackwell desktop part:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXC 1.9.2609.5 (shared env), debug build.

cargo test -p victory-shader --lib emit::bitcode:: -- --test-threads=1
13 passed; 0 failed; 1 ignored; 0 measured; 292 filtered out      (exit 0)

cargo test -p victory-gpu --lib shader_cache -- --test-threads=1
4 passed; 0 failed; 0 ignored; 0 measured; 228 filtered out      (exit 0)

All pass, including atomic_subtractions_become_loops. These are translation and cache tests; the GPU test that creates the pipeline exists only in the author's fork, so I did not verify that the CS_BeginUpdate pipeline is actually made on this adapter. The thread has runs on RTX 5060 Ti, RTX 5080 and RTX 3070, but no 5070 Ti.

Not tested: the title's compute 7f61193d2c0e5f22 pipeline, a paired smoke run, the full workspace suite, AMD and Intel GPUs, native wave64 hosts, Wine, release profile, game data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7236377944 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3619#issuecomment-7236377944). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-28T08:07:26Z. --- Ran the group-memory atomic subtraction tests at `47bdeca` on an NVIDIA Blackwell desktop part: Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, Rust 1.98.1 (pinned by `rust-toolchain.toml`), MSVC 14.44.35207, DXC 1.9.2609.5 (shared env), debug build. ``` cargo test -p victory-shader --lib emit::bitcode:: -- --test-threads=1 13 passed; 0 failed; 1 ignored; 0 measured; 292 filtered out (exit 0) cargo test -p victory-gpu --lib shader_cache -- --test-threads=1 4 passed; 0 failed; 0 ignored; 0 measured; 228 filtered out (exit 0) ``` All pass, including `atomic_subtractions_become_loops`. These are translation and cache tests; the GPU test that creates the pipeline exists only in the author's fork, so I did not verify that the `CS_BeginUpdate` pipeline is actually made on this adapter. The thread has runs on RTX 5060 Ti, RTX 5080 and RTX 3070, but no 5070 Ti. Not tested: the title's compute `7f61193d2c0e5f22` pipeline, a paired smoke run, the full workspace suite, AMD and Intel GPUs, native wave64 hosts, Wine, release profile, game data. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash Free** - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.
futurepro62 commented 2026-11-29 01:36:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-29T01:36:13Z.


Confirmed on another NVIDIA card: RTX 5070 Ti (16 GB), Windows 11 26300, shaders.rpf of 1.0.2.1.

Before: host pipeline refused error=Not enough memory resources ... (0x8007000E) shaders=[compute 7f61193d2c0e5f22] once in every run's log, and again whenever the pipeline was made ahead with victory warm.

With this commit (ported by hand onto a downstream fork, trustno1-coder/VIctory-NVIDIA, whose shader_cache.rs and emit/shader.rs differ from this repo):

  • The driver makes the pipeline (warm --these on it: made ... 1), and the log says atomic subtractions on group memory are compare-exchange loops.
  • Whole archive offline (translate --validate --list, switch on): only weatherParticle:CS_BeginUpdate changes output, and 25,570 programs validate as before.
  • About 75 minutes in game: no pipeline refused line, no device removal.

Not compared: the weather particles side by side.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7248891981 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3619#issuecomment-7248891981). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-29T01:36:13Z. --- Confirmed on another NVIDIA card: RTX 5070 Ti (16 GB), Windows 11 26300, `shaders.rpf` of 1.0.2.1. **Before:** `host pipeline refused error=Not enough memory resources ... (0x8007000E) shaders=[compute 7f61193d2c0e5f22]` once in every run's log, and again whenever the pipeline was made ahead with `victory warm`. **With this commit** (ported by hand onto a downstream fork, trustno1-coder/VIctory-NVIDIA, whose `shader_cache.rs` and `emit/shader.rs` differ from this repo): - The driver makes the pipeline (`warm --these` on it: `made ... 1`), and the log says `atomic subtractions on group memory are compare-exchange loops`. - Whole archive offline (`translate --validate --list`, switch on): only `weatherParticle:CS_BeginUpdate` changes output, and 25,570 programs validate as before. - About 75 minutes in game: no `pipeline refused` line, no device removal. Not compared: the weather particles side by side. 🤖 Generated with [Claude Code](https://claude.com/claude-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/3619:upstream-pr/3619'
git switch 'upstream-pr/3619'

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