Shaders: an atomic subtraction on group memory is a compare-exchange loop on NVIDIA #3619
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3619"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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(compute7f61193d2c0e5f22). The driver refuses it withE_OUTOFMEMORYevery 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_u32toatomicrmw sub, andds_add_u32of a value the program negated toatomicrmw addof0 - x. NVIDIA's compiler rejects both.We bisected the emitted module by changing one thing at a time:
0 - x, ofx * -1or of~x + 1, andatomicrmw subofx. The WaveSize attribute, the LDS size, and whether the result is read made no difference.x, of a constant, of5 - x, of-x + 5or ofc ? -x : 0, and a compare-exchange loop.Change
Module::atomic_subtractsturns everyatomicrmw sub, and everyatomicrmw addof 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 inShader::finish, before the shader flags are read from the module.0x10DE). The vendor is read from the default DXGI adapter, the oneD3D12CreateDevice(None)takes. Other hosts get exactly the code they get today.VICTORY_ATOMIC_SUB_LOOP=1or=0overrides this on any host.translate --pipelinestranslates as the runtime does for its adapter.This is separate from pull request 3142, which turns
ds_sub_u32into 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).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::andcargo test -p victory-gpu --lib shader_cachepass.ds_add_u32of the negated index and asds_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 needsdshelpers this tree'svalu_testsdoesn't have, so it is not included here.Measured
Before:
CS_BeginUpdateis 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
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):
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-91at47bdeca) turns the loops on for every0x10DEadapter, so Ampere and Ada cards would get them as well.cs_6_6measurement (32 threads, 200,000 iterations), 32 lanes on one slot took 318.9 ms with the loop, against 7.5 ms for a plainInterlockedAdd. 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, wherec ? -x : 0was 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 subon Ampere directly. I have an RTX 3070 and can build both of the forms your bisection refused, withVICTORY_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).
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'sprobe/checkhelpers insemantics_tests.rs. Each lane writesato its own LDS dword, does the atomic withb, 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.ds_add_rtn_u32(control)ds_sub_rtn_u32v_sub_nc_u32 v13, 0, b+ds_add_rtn_u32 [own], v13main), 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.ds_add_rtn_u32next to them runs fine. With the loops on, WARP is correct. This is the system WARP (d3d10warp.dll10.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_settingsturns 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, asmaindoes 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_u32also 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-sharedatomicrmw subitself, andatomicrmw addof 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 noInterlockedSub, so DXC never emitsatomicrmw subon 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.mainrefusesds_rsub_rtn_u32andds_dec_rtn_u32before 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).
Imported from Marvisak/VIctory. Originally posted by Aniseto on 2026-11-28T08:07:26Z.
Ran the group-memory atomic subtraction tests at
47bdecaon 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.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 theCS_BeginUpdatepipeline 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
7f61193d2c0e5f22pipeline, 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.
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.rpfof 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 withvictory warm.With this commit (ported by hand onto a downstream fork, trustno1-coder/VIctory-NVIDIA, whose
shader_cache.rsandemit/shader.rsdiffer from this repo):warm --theseon it:made ... 1), and the log saysatomic subtractions on group memory are compare-exchange loops.translate --validate --list, switch on): onlyweatherParticle:CS_BeginUpdatechanges output, and 25,570 programs validate as before.pipeline refusedline, no device removal.Not compared: the weather particles side by side.
🤖 Generated with Claude Code
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.