Skip to content

Recompiler: a compute loop's passes shrink with the dispatch's size - #2291

Closed
Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/dispatch-loop-budget
Closed

Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/dispatch-loop-budget

Conversation

@Prime1997

@Prime1997 Prime1997 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Prime · project thread

A lane may run 65 536 loop passes before LOOP_GUARD ends its loops, so that console code looping on a count XCL gave it wrong does not keep the GPU busy. The bound is per lane, so the work it allows grows with the dispatch: WindNew:CS_AdvectFromParentStep (a542b07e605c8fd7) runs as 16 x 16 x 16 groups of 64 threads, and with a loop count that never comes that is about 31 s on an RTX 3060 Ti, where Windows removes the device after 2 s (the open dispatch of #1452; measurements in this comment). With this change every entry of a loop in a compute program has its own count of passes that shrinks with the dispatch's size, so a loop that runs away on its own ends in time. Part of #1452.

What is in it

  • crates/xcl/src/gpu/recomp/mod.rs: IO_LOOP_BUDGET (io slot 113, free in the trap-temp range), DISPATCH_LOOP_PASSES (2^28), dispatch_loop_budget(groups); the two loop emitters ask Em::loop_guard for what goes before a loop and at its top.
  • crates/xcl/src/gpu/recomp/emit.rs: Em::loop_guard. In a compute program a loop gets uint xcl_nK = xcl_cap; before it and if (xcl_nK == 0u) break; xcl_nK--; in front of the existing guard; the entry point sets xcl_cap = clamp(slot / threads per group, 1024, 65536) when the slot is not 0. The lane's budget of 65 536 for all its loops stays, so loops inside one another are not cut to one loop's share. Other stages and programs without a loop emit the HLSL they did.
  • crates/xcl/src/hle/d3d12x/bind.rs, console_list.rs: a direct dispatch writes its share (2^28 / groups) into the slot. An indirect dispatch leaves 0: 65 536 as before.
  • crates/xcl/src/gpu/recomp/tests.rs: dispatch_loop_budget_shrinks_with_the_groups, only_compute_programs_with_a_loop_read_the_dispatch_loop_budget (no GPU), gpu_compute_loop_passes_follow_the_dispatch.

Up to 4096 lanes nothing changes. At 16 x 16 x 16 groups of 64 threads one entry of a loop ends after 1024 passes.

Tested

  • hardware (GPU + driver, CPU), Rust version, debug / release: NVIDIA GeForce RTX 3060 Ti 8 GB (driver 617.14), Intel Core i5-10400, Windows 11 26200, Rust 1.100.0, debug.
  • cargo test -p xcl --lib -- gpu_compute_loop_passes_follow_the_dispatch dispatch_loop_budget_shrinks only_compute_programs_with_a_loop: 3 passed, 0 failed, no SKIPPED. The GPU test runs a loop that never ends as a wave32 and as a split wave64 program: it stops after 65 536 passes with the slot at 0, after 5000 and 1024 for shares of that size, and entered three times it runs 3 x 1024.
  • 62 compute programs of a retail title that have loops (every sixth by size of 346, and the five with the most loops, up to 95) compile with DXC with the new guard: 62 ok, 0 failed.
  • The a542 program alone on the GPU with a count that never comes, 16 x 16 x 16 groups: 485 ms when the loop is ended after 1024 passes, against about 31 s for 65 536 (0.47 ms a pass measured). That was measured with an earlier form of this change that set the lane's total to the same 1024; I have not timed this head yet.
  • cargo test --workspace -- --test-threads=4: 1157 passed, 0 failed, 46 ignored (SKIPPED GPU tests: 25 – 20 need 64-lane waves, 3 are cases the direct per-lane form refuses, heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows), pso_measurement_compiles_an_exact_synthetic_record (needs Windows Developer Mode)).

Regression run

Seeded 60 s runs without input (the same copied profile and shader cache for each, release builds from clean checkouts of the two commits, XCL_HEAP_WINDOWS=1 XCL_LAZY_PSO=1 XCL_LAZY_COMPUTE=1 XCL_PSO_PRELOAD=0 XCL_DXC_THREADS=2). The main run is from earlier the same day, not back to back.

main:

xcl-smoke v1
commit: 568e7db7729c8fa6c8f0f4b32d798faaf11462bb
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 357 (PresentX 357)
presents per 2 s (last 3): 17 18 18
device removals: 0
crashes: 0
peak commit: 15226 MB

PR:

xcl-smoke v1
commit: 4bad50562dd5e1f7c3a26602367904a695296892
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 416 (PresentX 416)
presents per 2 s (last 3): 23 23 24
device removals: 0
crashes: 0
peak commit: 15191 MB

Not tested

  • The regression run reaches the title's start screen only: it does not reach the wind dispatch. The frame counts differ by run-to-run noise; no performance claim.
  • The title itself past the dispatch that hangs. Why the count is wrong there is not known; this only keeps the device.
  • A loop that runs away with another loop inside it still has the lane's 65 536 passes.
  • The direct DXIL translator keeps its fixed budget.
  • AMD / Intel GPUs, 64-lane hardware.

AI use

  • Claude Code with Claude Fable 5.2 did the measurements, wrote the code, the tests and this description, and opened the PR at Prime's request. The runs were on Prime's machine.

🤖 Generated with Claude Code

A lane may run 65 536 loop passes before LOOP_GUARD ends its loops, so
that console code looping on a count XCL gave it wrong does not keep the
GPU busy for minutes. That bound is per lane: the work it allows grows
with the dispatch, and a large dispatch outlasts the 2 s Windows waits
before it removes the device.

WindNew:CS_AdvectFromParentStep (a542b07e605c8fd7, 4 x 4 x 4 threads) is
dispatched as 16 x 16 x 16 groups and loops while a counter differs from
a count in title memory. Run alone on an RTX 3060 Ti with synthetic
inputs, the translated program takes 1.0 ms with a count of 0, 6.7 ms
with 64, 25 ms with 256 – and with a count that never comes 111 ms when
the loop is ended after 256 passes and 485 ms after 1024 (0.47 ms a
pass), which is 31 s for 65 536. That is the dispatch left open when the
device was removed (xcl-dev#1452).

A compute dispatch whose group counts are known now passes its share of
2^28 loop passes for one group in a free io slot (IO_LOOP_BUDGET), and
in a compute program every entry of a loop has its own count of passes:
that share among the group's threads, between 1024 and 65 536 a lane.
Up to 4096 lanes nothing changes; at 16 x 16 x 16 groups of 64 threads
one entry of a loop ends after 1024 passes. The lane's budget of 65 536
for all its loops stays, so loops inside one another are not cut to one
loop's share: only a loop that runs away on its own is ended early. A
slot of 0 (an indirect dispatch, the test runner) leaves 65 536, and
programs without a loop, other stages and the direct translator emit
what they did.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@Prime1997 A heads-up from our #1284 (XCL_LOOP_BUDGET, mergeable, head f3eb053). It isn't a duplicate, but the two meet in the same place:

  • Recompiler: XCL_LOOP_BUDGET sets the loop iteration budget of a lane #1284 makes the per-lane xcl_budget that LOOP_GUARD counts down configurable: XCL_LOOP_BUDGET swaps the prelude's 1u << 16 for any whole number from 1. This PR adds a per-loop xcl_cap in front of that guard, defaulting to 1u << 16 and clamped to 1024..=1u << 16 (emit.rs:4699, :4873 at 4bad505). With both merged, raising XCL_LOOP_BUDGET above 65 536 would no longer let a single loop in a compute program run longer, because xcl_cap would end it first. Lowering it still works, because LOOP_GUARD fires first.
  • They also conflict as text: git merge-tree of the two heads stops in recomp/emit.rs and recomp/mod.rs.

If the upper bound of the clamp (and xcl_cap's default) took loop_budget() from #1284 instead of a literal 1u << 16, the two would compose. Whichever merges second needs that. I'm happy to rebase #1284 onto this one if it lands first.

AI-assisted (Claude Code). Read from the two diffs only; nothing run.

@Prime1997
Prime1997 marked this pull request as ready for review November 25, 2026 12:54
@Lum7nt

Lum7nt commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

AMD targeted tests on clean head 4bad50562dd5e1f7c3a26602367904a695296892: Windows 11 build 26300, Radeon RX 9070 XT (driver 32.0.32029.2010), Rust 1.100.0 MSVC, DXC/DXIL 1.9.2609.5, release profile. Set XCL_ADAPTER=9070 XT, XCL_REQUIRE_GPU=wave64. For each filter below I ran cargo test --offline --locked --release -p xcl --lib <filter> -- --nocapture --test-threads=1:

  • gpu_compute_loop_passes_follow_the_dispatch: 1 passed, 0 failed, 0 ignored.
  • dispatch_loop_budget_shrinks_with_the_groups: 1 passed, 0 failed, 0 ignored.
  • only_compute_programs_with_a_loop_read_the_dispatch_loop_budget: 1 passed, 0 failed, 0 ignored.

No SKIPPED messages. The first is the small single-group GPU fixture covering wave32 and wave64 program options and the different loop limits; the other two are CPU checks.

60 s retail smoke comparison: clean release builds; PR run first, then main. Same title, XCL_ADAPTER=9070 XT, XCL_HEAP_WINDOWS=1, XCL_PSO_PRELOAD=0; no automated input. The PR produced 254 frames versus main’s 1556 (16.3%, below the 80% comparison threshold), with zero recorded crashes or device removals. This single pair is order-sensitive and does not establish a cause or a reproducible regression; caches and profile state were not reset between runs. The last three PR present intervals recovered to 11, 59, 60.

Main:

xcl-smoke v1
commit: 1a0bbe3a2ba91ea8f5b6fc29130a053868d3c9e6
gpu: AMD Radeon RX 9070 XT
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1556 (PresentX 1557)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14676 MB

PR:

xcl-smoke v1
commit: 4bad50562dd5e1f7c3a26602367904a695296892
gpu: AMD Radeon RX 9070 XT
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 254 (PresentX 254)
presents per 2 s (last 3): 11 59 60
device removals: 0
crashes: 0
peak commit: 15323 MB

Not tested: the large wind dispatch, progression past the loading hang, full workspace, or a controlled cache/profile comparison.

Repeat after main: Same settings and no reset; PR/main/PR run order. The repeated PR run showed 1577 frames, compared with 1556 on main. This makes the first result non-reproducible in this sequence but does not identify its cause.

xcl-smoke v1
commit: 4bad50562dd5e1f7c3a26602367904a695296892
gpu: AMD Radeon RX 9070 XT
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1577 (PresentX 1577)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15041 MB

AI use: OpenAI Codex (GPT-6; exact variant unavailable) ran the tests and smoke pair and drafted this report.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

AMD RDNA3 (native wave64) test report for exact head 4bad50562dd5e1f7c3a26602367904a695296892, as pushed (not merged onto current main).

Tested

  • AMD Radeon RX 7800 XT (RDNA3, native wave64), driver 32.0.31035.1003; Windows 11 Pro 10.0.26200; Rust 1.100.0 (stable-x86_64-pc-windows-msvc); debug; DXC 1.9.0.5512 (dxcompiler.dll)
  • cargo build --workspace: ok
  • Targeted: dispatch_loop_budget_shrinks_with_the_groups, only_compute_programs_with_a_loop_read_the_dispatch_loop_budget, gpu_compute_loop_passes_follow_the_dispatch: 3 passed, 0 failed (the GPU one ran, not skipped)
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 1156 passed, 1 failed, 46 ignored (summed over all test binaries)
  • SKIPPED: 7 lines in the full run (6 `lower_dxil` f16/f32 per-lane-form cases "refused (unsupported V_MUL_F16 / V_PK_MUL_F16 / V_CVT_F16_F32 ... gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics)", plus `heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows`). The one failure is `gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1` (seed 722: res[30] = 0x1, CPU 0x7fffffff). It is not in code this PR touches, and I reproduced the same failure and seed on `main` at `1a0bbe3` with `cargo test -p xcl --lib fuzz_validate_and_gpu_1`, so it is pre-existing on RDNA3, not caused by this PR.

Not tested

AI use: Claude Code (Claude Sonnet 5.5) ran the build and tests and wrote this report; the numbers are from the runs above.

@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
@Prime1997

Copy link
Copy Markdown
Collaborator Author

@upipibi agreed, and thanks for reading both diffs. The two compose the way you describe: the cap's default and the clamp's upper bound are the prelude's literal 65 536 only because loop_budget() is not on main. If #1284 lands first I rebase and take loop_budget() in both places; if this one lands first, that is the same two-line change in your rebase.

Thanks @Lum7nt, @oni-swr and @nnonno91 for the runs on AMD (native wave64 included) and the boundary cases at 1024 / 1025 passes.

Two updates from my side:

  • Rebase. main moved under this (3f4495a, Recompiler: split large direct compute pipelines at CFG edges #2080 changed translated_dispatch), so the PR shows a conflict. The rebased commit is ready (bind_abi(…, Some([x, y, z])), then the dispatch chain as main has it; targeted tests 3 passed, workspace suite 1278 passed, 0 failed, 46 ignored on the RTX 3060 Ti). I push it together with its regression run so the description never names a head it was not run on.
  • Timing with the final form (the earlier 485 ms was measured with a first version that cut the lane's total): in the translation of a build that carries this commit, with the slot as bind_abi writes it for 16 x 16 x 16 groups, one a542 dispatch with a count that never comes takes 511 ms (271 ms for 2048 groups, 121 ms for 1024). Counts of 0 / 64 / 256 take 1.0 / 6.8 / 23 ms as before.
  • A limit I measured the hard way: the cap bounds one dispatch. Five of those 511 ms dispatches back to back in one command list were removed on this card (0x887a0006); one alone was not. So if a title issues several runaway dispatches in a row, this is not enough on its own. The same reasoning says a dispatch that main now runs in slabs (Recompiler: split large direct compute pipelines at CFG edges #2080) should keep the whole dispatch's size for the slot, which the rebase does.

AI use: Claude Code (Claude Fable 5.2) wrote this and ran the measurements on Prime's machine.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@Prime1997 Prime1997 mentioned this pull request Nov 26, 2026
@thehollowedone

Copy link
Copy Markdown
Contributor

Independent synthetic RTX 3080 Ti check of #2291 head 4bad505 and its exact parent 1a0bbe3. Windows 11 build 26300, NVIDIA 621.35, Ryzen 7 9800X3D, rustc 1.99.1, DXC 1.8.2502.11. A finite synthetic RDNA2 compute loop increments until its own target of 1,025, then stores the count. On the PR head, the GPU returned 1,025 with slot 113 = 0 and 1,024 with slot 113 = dispatch_loop_budget([16,16,16]). The same test on the parent returned 1,025 with both slot values. Command on each: cargo test --offline --locked -j 4 -p xcl --lib gpu_finite_loop_keeps_its_result_above_the_dispatch_share -- --test-threads=1 --nocapture; head 0 passed, 1 failed, parent 1 passed, 0 failed, no ignores or skips.

The fixture launches only one group and supplies the slot value that production calculates for a 16×16×16-group, 64-thread dispatch. It verifies the per-lane HLSL behavior without making a large dispatch or claiming a title result. This demonstrates a valid finite loop can silently produce a different output under the new default cap. The existing endless-loop test proves the cutoff works, but cannot establish output parity for valid counts above it. I would keep the dispatch-sized cutoff out of the default correctness path. For the reported world-load hang, #2205 can help trace the count passed to the affected dispatch; the value the GPU actually consumes and its producer ordering still need verification. A general threshold cannot distinguish stale input from a valid long loop. Current-main integration, the full suite and clean-head smoke still need rechecking.

Failing PR-head GPU regression (passes on its parent with slot 113 = 65,536)
diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs
index 3433d5f8..1df211d9 100644
--- a/crates/xcl/src/gpu/recomp/tests.rs
+++ b/crates/xcl/src/gpu/recomp/tests.rs
@@ -7306,6 +7306,37 @@ fn gpu_compute_loop_passes_follow_the_dispatch() {
     }
 }
 
+/// A finite console loop must finish at its own exit, even when dispatched in many groups.
+#[test]
+fn gpu_finite_loop_keeps_its_result_above_the_dispatch_share() {
+    if !gpu_ready() {
+        return;
+    }
+    let p = prog(&[
+        ("S_MOV_B32", "ENC_SOP1", &[("SDST", 11), ("SSRC0", 255)], Some(1025)),
+        ("S_MOV_B32", "ENC_SOP1", &[("SDST", 10), ("SSRC0", 128)], None),
+        ("S_ADD_U32", "ENC_SOP2", &[("SDST", 10), ("SSRC0", 10), ("SSRC1", 129)], None),
+        ("S_CMP_LG_U32", "ENC_SOPC", &[("SSRC0", 10), ("SSRC1", 11)], None),
+        ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFFD)], None),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 10)], None),
+        ("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 6), ("SRC0", 130), ("VSRC1", 0)], None),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 7), ("SRC0", 128)], None),
+        ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 6), ("DATA", 4), ("SADDR", 0x7D)], None),
+    ]);
+    let cs = super::CsInterface { threads: [64, 1, 1], user_sgprs: 2, tgid: [false; 3], tg_size: false, tid_components: 1 };
+    let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64: false, ..Default::default() });
+    let t = translate(&p, &opt).unwrap();
+    assert!(t.complete(), "{:?}", t.unsupported_why);
+    let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap();
+    let mut io = [0u32; 128];
+    let input = vec![0xffff_ffff; 64];
+    let uncapped = runner::dispatch(&dxil, [1, 1, 1], &io, &input).unwrap();
+    assert_eq!(uncapped[0], 1025, "control: finite loop exits by its own count");
+    io[super::IO_LOOP_BUDGET] = super::dispatch_loop_budget([16, 16, 16]);
+    let capped = runner::dispatch(&dxil, [1, 1, 1], &io, &input).unwrap();
+    assert_eq!(capped[0], 1025, "the dispatch size must not truncate a finite loop");
+}
+
 /// v_fract_f32 at the edges, as RDNA computes it (x - floor(x) kept below 1.0): ±inf gives
 /// NaN and a NaN stays NaN – a `min()` clamp returned 0.99999994 there on NVIDIA (HLSL min
 /// returns its non-NaN operand); a tiny negative value rounds to the largest float below 1.

AI use: Codex (GPT-6.2 Sol) built and ran both GPU checks and wrote this report; Codex (GPT-6.2 Astra) independently traced the loop-cap and dispatch paths.

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical review of current head 4bad5056. Blocking semantic regression: the dispatch-size cap can terminate a valid finite loop before its expected 1,025th iteration in a 16×16×16 dispatch. The independent GPU fixture passed on the parent and failed on this head when the per-dispatch budget was active. A watchdog heuristic must not change a shader's correct result based on dispatch size. Please make the reduced cap opt-in/diagnostic or prove a safe bound for the affected program, and retain the finite-loop regression. This branch also conflicts with main. No local GPU run.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 4bad505, diff read, not a merge-check; it conflicts with main, so not built or run):

  • Superseded: this commit is on main as 2536d2f (cherry-picked via Merge nvidia-dev into main (makes #2572 mergeable) #3203). Em::loop_guard, IO_LOOP_BUDGET (slot 113), dispatch_loop_budget and the three tests are on main as written here; main then added the same budget to the direct DXIL translator (d9a247b) and an off switch, XCL_DISPATCH_LOOP_BUDGET=0 (a0b82da).
  • A point for main, not this PR (reasoned, not measured): every compute loop now has a per-entry limit by default, down to the 1024 minimum for large dispatches (a 1920x1080 pass in 8x8 groups gets 128, raised to 1024). A real loop longer than that, such as a long ray-tracing traversal, would end early with wrong output and nothing logged; a once-only log line when the limit cuts a loop would make it visible.

Fine: the per-entry counter leaves nested loops the lane's full budget, and indirect dispatches keep 65,536. Recommendation: close as superseded by 2536d2f.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded: this change is already on main (2536d2f). PR's changed lines are identical to main 2536d2f 'a compute loop's passes shrink with the dispatch's size' (compared line by line, no difference)

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

@throny throny added the duplicate This issue or pull request already exists label Dec 1, 2026
@throny throny closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants