Shaders: spin-wait loops stop after 4096 iterations; loop limit in release builds #452
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!452
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/452/head"
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?
@AntoninMichelq on GitHub · 2026-11-24 03:34 UTC · original
AntoninMichelq:up/spin-loop-limit→main· +732 −31 · 6 files · 6 commits · closed without mergingRequested by 4nto · project thread
Summary
Spin-wait loops now stop after 4096 iterations instead of 2^20. On an RX 7600, a loop limit of 4096 cut GPU time per world frame by a factor of 3 to 10.
Some compute loops only reread one address until another workgroup, queue or the command processor changes it (decoupled look-back, persistent-thread queues, locks). On the console the write comes within microseconds. On the host it may never come: the writer's view can be another host resource, a null view, or not yet mirrored, and the host compiler may hoist the load out of a loop that writes nothing. The loop then runs to the 2^20 limit in every lane, about a second per wave of uncached reads.
VICTORY_SHADER_LOOP_LIMITin release builds too. It was gated on thediagnosticsfeature. The shader cache already keys on it.emit/spin.rs) finds such loops in the IR. They stop atVICTORY_SHADER_SPIN_LIMIT(default 4096, never above the loop limit). Other loops keep the 2^20 limit.What changed
emit/spin.rs(new): a loop is a spin wait when it has no stores, exports, LDS writes, barriers or messages; every memory read and atomic is at an address the loop does not change; and nothing loop-carried is made by arithmetic, only lane masks, compares and what was read.emit/lower.rs:count_iterationuses the spin limit for those loop headers.emit/mod.rs:spin_limit()andDEFAULT_SPIN_LIMIT; the loop limit is read in every build.victory-gpu/src/shader_cache.rs: the cache key includes the spin limit.README.md: the new variable.Open PR #292 also reads
VICTORY_SHADER_LOOP_LIMITin release builds (the same as commit 1), and changes the loop counters to one shared budget per invocation. It has no spin-wait limit. The two touch the same counter code, so whichever lands second needs a small rebase.How it was tested
cargo test -p victory-shader(Linux) passes: 62 tests, including new ones for a spin wait on a table buffer (spin limit), a loop that writes LDS (loop limit), the spin-loop finder and the limit parser.cargo check --tests -p victory-shader -p victory-gpuandcargo check -p victoryforx86_64-pc-windows-msvc(withCARGO_FEATURE_PURE=1) pass.VICTORY_SHADER_LOOP_LIMIT=4096), GPU time per frame went from 410 to 1,384 ms to a median of 127 ms.Where 3D rendering stands on an RX 7600 (Windows 11)
These observations come from the integration stack (upstream main plus many open PRs), not from this branch alone.
🤖 Generated with Claude Code
https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
@aycow on GitHub · 2026-11-24 04:03 UTC · original
I found a fixed-address atomic case in
1fcaf13. Inemit/spin.rs, MUBUF atomics can enter the read-only spin classification. An original synthetic GFX10 loop performs a returningbuffer_atomic_addon a zeroed counter and exits when the old value reaches 32. With a general limit of 64 and a spin limit of 16, the original translation writes 16. Treating MUBUF atomics as effects in the existing store branch and excluding them from the read branch writes the CPU-reference value of 33.I reproduced both outputs on AMD Radeon Graphics, an RTX 5070 Laptop GPU, and WARP with the D3D12 debug layer and full GPU-based validation active. All three reported zero validation warnings or errors. Could you cover this case in the classifier regression suite? I’ve kept the focused correction separate from the runtime census. This test checks observable atomic side effects and does not establish the cause of title polling.
@aycow on GitHub · 2026-11-24 04:22 UTC · original
For the next #452 revision, I can rerun the synthetic atomic test on AMD, NVIDIA and WARP. I’d keep paired assertions in the classifier test: a fixed-address load/compare loop still uses the spin budget, while a returning
buffer_atomic_addloop with the same fixed address uses the general budget and produces 33 increments at limits 64/16. Any non-returning buffer atomic is also an observable write. The original fixture and the small classifier change are available if useful for your branch.@LoukaAyariPignel on GitHub · 2026-11-24 08:04 UTC · original
I merged current
maininto this branch to clear the conflict:fix-452on my fork, merge commitb533e24. The only conflict was in the imports of theemit::lowertest module (this PR'sLowered/RootParam/UsedResourceagainsthalf_shiftfrommain). I took the union of both. On Windows,cargo test -p victory-shader,clippy -D warningsandfmt --checkpass, and it still merges cleanly with today'smain.@Nixitov on GitHub · 2026-11-24 10:18 UTC · original
Data point for the spin-wait limit, measured on the machine where the world hang is worst here: the spin limit alone does not stop this title's 10-second host submissions, the general loop limit does.
Setup: Windows 11 Pro 26200.9974, RTX 5070, driver 617.14, release build, this PR merged onto a stack of the shader PRs (#298, #585, #383, #259, #302, #307, #539, #571, #569) and the GPU PRs (#493, #483, #581, #595), 110 s in the world each, same scene (first mission intro).
VICTORY_SHADER_LOOP_LIMITset per run (this PR makes it work in release builds, which is what made the measurement possible — thank you for that).The submissions that take 10 s are not spin waits: with the spin pass in, they are still 10 s, and they stop being a problem only when the general count comes down. Under 2^16 the frame rate is unchanged (2.6-4.4 fps, same draws/frame), so shortening those loops costs nothing visible here.
That matters beyond frame time: a submission over the host's ~2 s watchdog removes the device, and twice today a 10 s one took the whole desktop down with it (the machine had to be restarted). At 2^14 the longest submission is 371 ms and a 110 s world run has no removal.
Integration notes for landing it on current main (all small, on
emit/lower.rs):Lowergained fields from the other shader PRs: the merge keepsresolved_reads,pure_spill_writes,guard_slotsnext to this PR'sspinandspun, and the constructor sets all of them.Partsgainedhost_waves(from #54'sWaveRangework, which arrives with #298); two test helpers that buildParts(lower.rs'slowers/lower_with,image_tests.rs) needhost_waves: Noneto compile.cargo test -p victory-shader --lib -- spin: 5 passed.No game files; only timings and counts.
AI-assisted (DeepSeek V4.1 Flash coding agent).
@AntoninMichelq on GitHub · 2026-11-24 10:31 UTC · original
Thanks a lot for these measurements, the 2^20 / 2^16 / 2^14 comparison on the RTX 5070 makes the case clearly.
The default general loop limit is now 2^14 (16,384) in cd6a864. The doc comment on LOOP_LIMIT gives the reason (the ~2 s host watchdog, your numbers) and the trade-off: a legitimate loop of more than 16,384 iterations per lane is cut short, so its result is wrong there.
VICTORY_SHADER_LOOP_LIMIT still overrides it in every build, including release, so a higher value can check whether a wrong result comes from the limit. The spin-wait limit stays at 4096 and never above the loop limit, and the shader cache still keys on both.
Current main (
ffe310e) is merged in, no rebase (6c23321). In that main, Lower does not have resolved_reads, pure_spill_writes or guard_slots yet, and Parts has no host_waves, so those notes did not apply this time. The conflicts were in the test-only fields of Lowered and in the test helpers (main's lower_wave split and the new resource::track argument). I'll apply your notes when those PRs land.cargo test -p victory-shader passes on Linux: 102 tests (1 ignored). cargo check --tests for x86_64-pc-windows-msvc passes for victory-shader, victory-gpu and victory.
@PastiiJay on GitHub · 2026-11-24 10:41 UTC · original
Data point for
cd6a864on RDNA4 (RX 9070 XT 16 GB), driver 32.0.32029.2010, Ryzen 7 9800X3D, 32 GB, Windows 11. Diag build,VICTORY_D3D12_DRED=1, no pad connected. Local merge ofmain@ffe310e+ this PR + #431 + #67 (#568 did not merge cleanly on top, left out).ffe310eplainffe310e+ #452 (cd6a864) + #431 + #67page fault at 0x1a939c000, list 21 stopped at 4/11On this card the earlier removal looks like the 2 s TDR: with the loop limits from this PR every world frame stays under it, audio stays in sync, and the run only ends on a separate page fault. Same result on 3 earlier runs without this PR (always ~60 s). Happy to rerun another revision or limit value and post the numbers (on RDNA4 only).
@Machad3x on GitHub · 2026-11-24 13:46 UTC · original
Result on an NVIDIA RTX 3080 Ti (32-lane waves, Windows 11), fork github.com/Machad3x/VIctory branch claude/gta6-shader-translation-crashes-705550 (main + #302 #489 #294 #578 #650 #233 #709 #585 #309 #587 #452): wave64 mesh programs are NOT what hangs the GPU. With #578's mesh refusal switched off (a diagnostics-only switch on the fork, VICTORY_ALLOW_WAVE64_MESH=1): without #452 a world frame costs 4.3 s of GPU and the device is removed (even with #587); with VICTORY_SHADER_LOOP_LIMIT=64 the same run stays up (3731 draws per frame, about 90 ms of GPU per frame, no removal, no slow list); with #452 and its defaults (spin limit 4096, no manual limit) the run stays up for 5 minutes, 3900 draws per frame, 441 ms of GPU per frame. So the TDR was loops running to the 2^20 limit, as #452 measured on an RX 7600, and #578's mesh refusal looks unnecessary once #452 is in; only the frame CPU time (about 600 MB of copies per frame) remains very high. Not yet checked: whether the picture is right (the world was still mostly dark on the RX 7600 per #452).
@Yaekai on GitHub · 2026-11-24 16:12 UTC · original
Independent verification on Windows 11 + NVIDIA RTX 3070 (Ampere, Wave32, stock TDR, Rust 1.98.1) at head
3ede934:cargo test -p victory-shader --lib -- --skip valu_tests --skip waterfall_tests: 108 passed, 0 failed.cargo clippy -p victory-shader -- -D warnings: 0 warnings.1.0.2.1,--console series-s, release build onffe310e+#452+#587+#493): withoutVICTORY_SHADER_LOOP_LIMIT, world entry hitDXGI_ERROR_DEVICE_HUNG (0x887A0006)within ~10–15 s; withVICTORY_SHADER_LOOP_LIMIT=1024, the world stayed up for 19+ minutes (~2,300 draws/frame, ~120–215 ms GPU/frame) with 0 hangs until closed manually. Huge stability win on NVIDIA!@AgneyaK on GitHub · 2026-11-24 16:36 UTC · original
Test report on a 6 GB RTX 3050 Laptop, title 1.0.2.1.
3ede934merges cleanly ontomainffe310e. On top of the wave64 stack (#223, #233,#320, #439) it conflicts in
crates/victory-shader/src/emit/mod.rs, in two places, and both aremechanical. The stack adds
mod split_tests;where this PR addsmod spin;. In the test module, thestack adds
use super::Target;where this PR adds#[cfg(windows)]on thebitcode::Attrimport.Keeping both lines in each place builds.
mainhad a 28 s submission and then the+0x23cf683stop.With this PR (its defaults: 2^14 general, 4096 for spin waits), the longest submission was 0.73 s and
+0x23cf683did not come. The device was removed 4:16 in instead. With #579'sVICTORY_SKIP_WAVE64=meshon top, the run lasted 9:27. Full table in #1232.
mainalone in the title.AI use: Claude Code (Claude Opus 5.5) merged and built the trees, ran the title on my machine and drafted this note; results are from the runs described.
@465d6a0eac on GitHub · 2026-11-24 17:49 UTC · original
Thank you for this, @AntoninMichelq! Many pull requests here are still waiting to be merged, so I've ported this one to 465d6a0eac/AgentRecompVI, a VIctory fork where AI agents review and merge each other's work: https://github.com/465d6a0eac/AgentRecompVI/pull/36. You're credited as the author there. This stays the original.
Test results
cargo clippy(both variants) andcargo test --workspacepass (429 passed, 0 failed).VICTORY_SHADER_LOOP_LIMIT=4096: GPU time per frame in the world fell from 6,100–6,400 ms to 300–500 ms, so loops running to the 2^20 limit were most of it. With this pull request's defaults it was 190–310 ms.emit/mod.rsnow declares #233'ssplit_testsnext to yourspinmodule.@adnanoos on GitHub · 2026-11-24 20:30 UTC · original
RTX 5060 Laptop GPU: this PR removes a reproducible world
DEVICE_HUNGat a draw; capping every loop at 4096 also removes the slow submissionsSetup. NVIDIA GeForce RTX 5060 Laptop GPU (32-lane waves), driver 32.0.16.2124, Windows 11 Pro 26200, title 1.0.2.1, Series X mode. Base: #1520 at
2190efe(an integration branch with #994, #302, #193 and others) plus theresync_title_writesbound I posted on #994, which removes a two-minute boot stall on NVIDIA.--features diagnostics,VICTORY_D3D12_DRED=1, normal presentation, Enter every 5 s for 4 min. Each run was capped at 12 min.DXGI_ERROR_DEVICE_HUNG3m16s into the world (and 2m50s in an earlier run)VICTORY_SHADER_LOOP_LIMIT=4096(every loop)+0x5149aec+0x5149aecThe hang this PR removes. It is the same stop in both default runs: one list stops at command 197 (op 3) of 763 or 765,
draw 4x391/draw 4x575with vertexcb47fdb3b37b7880, pixelb335d5633b1ed53f, followed by draws of vertex65511934598e485c/ pixel5e69b507ec27b789. DRED reports no page fault.Margin. With this PR the longest submission was 1.57 s on this GPU, close to the ~2 s TDR; with a 4096 limit on every loop there was none over the threshold. So the remaining long submissions come from loops that are not spin waits by
spin.rs's definition and stop at 2^14 here. That may be worth knowing when choosing the general limit, or alongside #292's shared budget.Merge. It merges into #1520 with two trivial conflicts in
emit/lower.rs, both of the "keep both" kind: theLowerstruct gets bothret(from #1520) andspin/spun, and so does its initialiser.The
+0x5149aecstop after both capped runs is the title pool running dry, as in #1232, which saw the same order of events on an RTX 3050 laptop.@Marvisak on GitHub · 2026-11-25 00:18 UTC · original · edited 2026-11-25 09:16 UTC
Automated triage for the maintainers.
Merge candidate. Recommending a look, not approving.
main(940d32f). It makesVICTORY_SHADER_LOOP_LIMITwork in release builds, lowers the default general loop limit from 2^20 to 2^14 (emit/lower.rsLOOP_LIMIT), and gives loops that only reread a fixed address (emit/spin.rsspin_loops) a separate 4096 limit (spin_limit, capped at the loop limit), used incount_iteration. Both limits feed the shader cache key (shader_cache.rskey) and the pre-built pipeline list tag (pipeline/library.rstag), with tests for each.global_load_*make a loop non-spin; reads must use loop-invariant addresses; loop-carried values may only be masks, compares, copies or loaded values. The returningbuffer_atomic_addcase raised in the comments is handled in0b2e932(MUBUF atomics areAccess::Effect).loop_limit(), so whichever lands second needs a rebase.spin.rsand theAccess::Readdoc still say "(or an atomic)", which no longer matches the code.CI:
checkpassing.Generated by Claude Code
@astrosteveo on GitHub · 2026-11-25 04:41 UTC · original
I rebased this onto current
main(940d32f), in case it helps with the triage's "needs rebase":rebase/452on my fork. It has your five commits with their author kept (the old merge of main is dropped), and one commit of mine. Take whatever is useful.Conflicts. The four files triage lists are all "keep both" or the same edit against newer text. In
shader_cache.rs, main'skey()now hashes named parts, so the spin limit became a fourth argument, andthe_key_covers_translator_encoding_and_settingscovers it.One conflict git does not show. Since this branched, main's
pipeline/library.rsgot a tag for the pre-built pipeline list. It is meant to cover the translation as the shader cache keys it, but it has onlyloop_limit(). After a run with anotherVICTORY_SHADER_SPIN_LIMITthe list would still hold, though the pipelines it names are not the ones that run makes.723baccadds the spin limit to the tag, with a test that fails without it.Checked on Linux (Wine 11.22, vkd3d-proton, RTX 5080):
cargo test --workspace: 696 passed, 13 failed.maingives 688 passed and the same 13 failures there; the 8 more are this PR's tests and the one above.cargo clippy --workspace --all-targets -- -D warnings, with and without--all-features, for the MSVC and GNU Windows targets: clean.Not checked: a Windows run of the tests, and the title.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pull request closed