Shaders: an atomic subtraction on group memory is a compare-exchange loop on NVIDIA #149
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/149"
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 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(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, after this tree's module clean-up passes (optimise), so it sees what they leave.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.build).The same change is open on Marvisak/VIctory as pull request 3619. Upstream pull request 3142, which turns
ds_sub_u32into 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 thevictory-gameexampleshaderpack_states, onmaintoo.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) 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 DexNom/VIctory. Originally posted by retrope13 on 2026-11-28T03:05:53Z.
Audited and hardware-tested head
b743a613on Windows 11 with an NVIDIA RTX 4060 Laptop GPU (Ada Lovelace) and WARP — the rewrite pass looks solid and all suites pass.Environment
26300.9818), Intel Core i5-14500HX, 24 GB RAM[0]): NVIDIA GeForce RTX 4060 Laptop GPU (0x10DE, 7,956 MiB VRAM, driver32.0.16.2140) + WARPrustc 1.98.1 (48a229cea 2026-09-01)Tested
cargo clippy --locked -p victory-shader -p victory-gpu -p victory --all-targets -- -D warnings— cleancargo clippy --locked -p victory-shader -p victory-gpu -p victory --all-targets --all-features -- -D warnings— cleancargo test --locked -p victory-shader --lib emit::bitcode::— 17 passed, 0 failed, 1 ignored (includingatomic_subtractions_become_loops)cargo test --locked -p victory-gpu --lib shader_cache— 2 passed, 0 failedcargo test --locked -p victory-gpu -- --test-threads=1— 219 passed, 0 failed, 4 ignoredcargo test --locked -p victory-shader --lib— 240 passed, 0 failed, 4 ignoredvalu_tests.rs, reverted after run):ds_sub_u32,ds_add_u32of0 - tid,ds_sub_rtn_u32summing all 64 returned old values intoLDS[4],ds_add_rtn_u32of0 - v2summing all 64 old values, plus dynamic LDS pointer variants ofds_sub_u32andds_add_u32of0 - tid).set_atomic_sub_loops(true): confirmed emitted DXIL changed on all 6 cases, passed DXIL validation, compiledOkon RTX 4060 and WARP, and read back exact results on both (7984for10000 - sum(0..64)and637984forsum(9937..=10000)of returned old values).32.0.16.2140): withset_atomic_sub_loops(false), those 6 minimal synthetic shaders did compileOkon the RTX 4060 (soE_OUTOFMEMORYdid not reproduce on these minimal shaders on Ada/562+/592+ driver, though enabling the CAS loop for all0x10DEadapters caused zero regressions here).Code Review Notes
atomic_subtractsinteracts cleanly withModule::write's RPO block ordering,live()phi liveness, self-loop phi rewriting, and empty-latch folding.victory warmandvictory runboth go throughvictory_gpu::shader_cache::host_shader(callinghost_settings()), whilevictory translate --pipelines(apps/victory/src/translate.rs::run) invokesvictory_shader::emitdirectly without callinghost_settings()— soVICTORY_ATOMIC_SUB_LOOPand NVIDIA auto-detection don't apply invictory translate --pipelinesyet.4ad86927) already hasds(),vop2(),S_BARRIER,DS_WRITE_B32,DS_READ_B32, andbuild_cs(&body, 64, 512)incrates/victory-shader/src/emit/valu_tests.rs(from theds_min_max_f32tests), so the 64-thread GPU test from your fork could actually be included directly here if you'd like.Not Tested
CS_BeginUpdate(7f61193d2c0e5f22) or live title runs.AI Disclosure
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.