Skip to content

D3D12.X: cap a small compute dispatch's loop entries at 4096 passes - #3667

Open
Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-loop-passes-max
Open

Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-loop-passes-max

Conversation

@Prime1997

Copy link
Copy Markdown
Collaborator

Requested by Prime

A compute dispatch of only a few groups got the largest loop budget there is: recomp::dispatch_loop_budget shares 2^28 passes among a dispatch's threads, so one group of 64 threads got 65 536 passes a loop entry. That share model fits a dispatch that fills the GPU. A small dispatch runs its groups side by side, so a loop that never ends costs the full 65 536 passes of latency whatever the thread count. On an RTX 3060 Ti, in a game title, a split wave64 compute program took 50.4 ms for one 1x1x1 group, twice a frame, every frame. This change caps each lane's loop entry at 4096 passes (four times DISPATCH_LOOP_PASSES_MIN) for compute dispatches with known group counts. Large dispatches already get less than that and are unchanged. No translated code changes, so the shader cache stays valid.

Related: #3652 (open) lowers the split-wave64 exchange and barrier waits of the direct DXIL translator from 2^20 polls (an exit bit for a departed half, then 2^12 and 2^16 polls). That is the other bounded wait that can hold a small dispatch this long. The two do not touch the same code.

What is in it

  • crates/xcl/src/hle/d3d12x/bind.rs: loop_passes_max() reads XCL_LOOP_PASSES_MAX. capped_loop_budget() computes the IO_LOOP_BUDGET value, min(share, max x threads per group), with a saturating product so extreme values fall back to the share. loop_budget_for() applies it to a compute pipeline and logs the first 64 programs whose loops get fewer passes. The direct bind path writes it for compute dispatches with known group counts. Unit test small_dispatches_get_at_most_the_max_loop_passes.
  • crates/xcl/src/hle/d3d12x/console_list.rs: translated console command lists take the same capped budget.

Tested

  • RTX 3060 Ti 8 GB, Windows 11, release build: the change has been in our test builds since 1 Dec. Not yet shown: whether that program's 50 ms drops to ~3 ms, or whether its time is a bounded wait or real work.
  • This branch (on main 1c8b551): cargo check -p xcl --lib --tests --target x86_64-pc-windows-gnu passes. The test suite was not run on this exact branch.

Regression run

  • Not run yet: testers welcome.

Not tested

  • The suite and scripts/smoke.ps1 on this branch; AMD.
  • A program whose loop really needs more than 4096 passes an entry in a small dispatch. XCL_LOOP_PASSES_MAX=0 restores the old budget if one shows up.

Switches

  • XCL_LOOP_PASSES_MAX=<n>: default 4096, the most passes one loop entry of a lane gets in a compute dispatch with known group counts. =0 keeps the dispatch's share alone, as before (up to 65 536). Indirect dispatches are not affected.

AI use

  • Claude Code wrote the change and this description.

🤖 Generated with Claude Code

A game title on an RTX 3060 Ti: a wave64 compute program split in halves
took 50.4 ms for ONE 1x1x1 group, 2 calls a frame, the same every call.
A fixed latency per group, not work that scales with groups.

recomp::dispatch_loop_budget shares 2^28 loop passes among a dispatch's
threads (a throughput model, calibrated on a 262 144-thread dispatch). A
dispatch of one group gets clamp(2^28 / threads, 1024, 65536) = 65 536
passes a loop entry, the most there is. Its groups run side by side, so a
loop that never ends costs passes x one pass's latency whatever the thread
count: 65 536 passes at ~0.77 us (a split-form pass with a few exchanges)
is 50 ms. Both translators take the cap from IO_LOOP_BUDGET, written here.

XCL_LOOP_PASSES_MAX=<n> (default 4096, four times DISPATCH_LOOP_PASSES_MIN,
which the largest dispatches already get; 0 = the share alone, as before)
writes min(share, n x threads) so each lane's loop entry gets at most n
passes. Large dispatches keep their smaller share (80x45 groups of 64:
1165 an entry, unchanged). Indirect dispatches (IO_LOOP_BUDGET 0) keep
65 536. No translated code changes: the shader cache stays warm. The
thread product and n x threads saturate, so extreme values fall back to
the dispatch's share instead of overflowing.

Log: one line naming the switch, and the first 64 programs whose loops
get fewer passes ("XCL_LOOP_PASSES_MAX: cs ... a loop entry gets 4096
passes, not 65536").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review (head a9670ad, diff read, not a merge-check; checks red):

  • A default cap that can end working loops in many dispatches, not only ones of a few groups. capped_loop_budget (hle/d3d12x/bind.rs ~5128) lowers the per-entry passes whenever 2^28 / (groups x threads) > 4096. That is every compute dispatch under 65 536 threads, and those dispatches drop from up to 65 536 passes to 4 096. Small dispatches are where one lane does serial work: single-group reductions and scans, or a 1x1x1 loop over a list. For example, 64 threads striding a 512K-element buffer need 8 192 passes per entry. When the cap is hit, the direct DXIL path ends the lane's program (gpu/dxil/kernel.rs ~520-523) and HLSL breaks out of the loop, so the output is silently wrong. The description itself says it was not shown that the 50 ms is a runaway loop rather than real work. Making this the default needs that evidence first, or the cap should be opt-in.
  • It may not reach the program it is meant for. The per-entry count exists only when there is no compact plan (gpu/dxil/kernel.rs ~492, plan.is_none()). Split-wave64 compute programs with atomics, and large programs, get the compact form automatically (gpu/dxil/compact.rs ~261-263). There they keep the lane's 65 536 passes, and this change does nothing. If the 50 ms is the split halves' exchange/barrier spin (2^20 polls), it is the subject of Recompiler: signal Partner::Spin exit bit (0x4000_0000) in @xcl.spin.wait #3652 / Recompiler: bound spin waits of programs without a loop at 2^12 polls #3668, not this PR. The PR has no before/after number.
  • The tests do not cover the change. small_dispatches_get_at_most_the_max_loop_passes (bind.rs ~7191) checks only the arithmetic of the new helper. Neither call site that writes IO_LOOP_BUDGET (bind.rs ~5380, console_list.rs ~1059) is tested. No GPU test shows a small dispatch's loop ending at 4 096 passes, or a long legitimate loop surviving.
  • The log and the hot path. loop_budget_for (bind.rs ~5143-5160) runs for every compute dispatch with known group counts. For every dispatch under 65 536 threads it locks PSO_SHADERS and SEEN, and keeps doing so after 64 programs have been named. Programs without a loop also use up the 64 names, although they never read IO_LOOP_BUDGET. As a result, the log does not identify the programs whose loops were actually cut.
  • Combined with Recompiler: bound a direct DXIL compute lane's loops by its dispatch's share #3669. Recompiler: bound a direct DXIL compute lane's loops by its dispatch's share #3669 makes the direct DXIL lane total four times the per-entry cap derived from IO_LOOP_BUDGET. With both PRs merged, a small dispatch's lane goes from 65 536 passes in all to 16 384, and that includes the compact form. Neither description mentions this.
  • README and PR checks. README.md ~395-401 still says 1024 to 65 536 passes per thread, and the new default-on XCL_LOOP_PASSES_MAX is not documented there. PR checks is red because this runtime change has no xcl-smoke blocks (main and head) under Regression run. Tested has only a cargo check with the gnu target and no suite counts, and AI use does not name the model.

Fine: indirect dispatches (no group counts) and XCL_LOOP_PASSES_MAX=0 keep main's share. The thread count used is the console's numthreads (pso_threads_exact), which is what both translators divide by. The saturating products handle extreme values. No translated code changes.

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

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Test report (not a merge-check: PR description red)
base: 1c8b551
head: a9670ad
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read by one reviewer at this same head; the review is at #3667 (comment) and its findings stand. Most serious: a default 4096-pass cap on every dispatch under 65,536 threads can silently cut legitimate loops.
performance: 60 s smoke of a retail title, main 1c8b551 vs this merge (a9670ad): 1652 -> 1687 frames shown (102 %; last presents per 2 s: 60 60 60), peak commit 18228 -> 17407 MB, 0 device removals, 0 crashes.
assessment: merges, builds and passes the whole suite on current main, and the smoke run is clean; the blocking point is in the review: a default 4096-pass cap on every dispatch under 65,536 threads can silently cut legitimate loops
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@tiagonleal

Copy link
Copy Markdown
Contributor

On the open question in the description (a bounded wait or real work): on our RTX 5070 Ti the 1x1x1 split program is CS_DefragHeap_ProcessFreeRequests, and it is neither. Its outer loop count is s_ff1_i32_b32 of a constant-buffer dword, so a 0 there gives 0xFFFFFFFF passes, and the loop runs until XCL's pass budget ends it (45 ms per dispatch here).

The 0 comes from the title's GPU heaps being outside the coherent window: with the Series S window at 512 MB, its three heaps (1162 + 1277 + 258 MB) do not fit, become plain memory, and translated shaders read zeros from them (throny's follow-up on #3568). With XCL_COHERENT_MB=3408 the heaps fit and the program drops out of the XCL_GPU_TIMES top 60 (under 2.45 ms per frame). Numbers are in my comment on #3568.

So the cap here shortens that case, and the program needs only a few passes once its data is reachable. With real data its block walk is a data-dependent loop, so it may be worth checking that 4096 passes still cover it.
AI use: Claude Code (Claude Opus 5.5, with a Claude Fable 5.2 analysis of the shader and logs).

  • Tiago

@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.

Review of a9670ad4: 1 finding(s) (1 high), most severe first.

  1. High crates/xcl/src/hle/d3d12x/bind.rs:5093: The new default of 4096 silently cuts short real loops in every small direct compute dispatch, which used to get up to 65 536 passes per loop entry, while the same program keeps 65 536 when dispatched indirectly. (inline)

These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.


/// Default of `XCL_LOOP_PASSES_MAX`: four times `recomp::DISPATCH_LOOP_PASSES_MIN`, the passes a
/// loop entry of the largest dispatches already gets.
const LOOP_PASSES_MAX_DEFAULT: u32 = 4096;

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.

High: The new default of 4096 silently cuts short real loops in every small direct compute dispatch, which used to get up to 65 536 passes per loop entry, while the same program keeps 65 536 when dispatched indirectly. A 1x1x1 dispatch of a [64,1,1] or [1,1,1] program with a legitimate serial loop (a single-group scan or compaction over more than 4096 items, a long list walk, a spin on a flag another group sets) now leaves the loop after 4096 passes and runs to the end with partial results: wrong or corrupt output with only one info line per program. The PR says the 50 ms may instead come from the exchange waits #3652 bounds, so the cap may not even fix the motivating case. Keeping the default at the old budget and limiting the cap to the split-wave64 form (or to the identified program) would avoid changing correct programs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:nvidia Code is specific to NVIDIA / wave32 GPUs (scope tag; see needs:nvidia-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants