Skip to content

Recompiler: bound a direct DXIL compute lane's loops by its dispatch's share - #3669

Open
Prime1997 wants to merge 2 commits into
xcl-dev:mainfrom
Prime1997:contrib/up-dxil-loop-share
Open

Prime1997 wants to merge 2 commits into
xcl-dev:mainfrom
Prime1997:contrib/up-dxil-loop-share

Conversation

@Prime1997

Copy link
Copy Markdown
Collaborator

Requested by Prime

In the direct DXIL translator, a compute lane's loops were bounded per loop entry by the dispatch's share (1024 to 65 536 passes a thread), but the lane's total was still 65 536 passes. Nested loops (each entry of an inner loop starts a fresh count), loops one after another, and every loop of the compact form (programs of 20 000 instructions and more, which count no entry) could spend all of them. On an RTX 3060 Ti 8 GB a retail loop in a 262 144-thread dispatch took 0.47 ms a pass, so 65 536 passes would be about 31 s, far past the 2 s Windows waits before it removes the device. A compute lane's whole loop budget now starts at four entries of its dispatch's share, at most 65 536: large dispatches get 4096 passes (about 1.9 s at that rate in the worst case), small and indirect dispatches keep 65 536 as before. The lane then goes to the program's end, as it already did at 0. A second commit fixes invalid DXIL in the compact form's compute epilogue: on main it shows in a multi-wave group of a compact compute program with S_BARRIER, whose safe barrier exit reads a register after the entry gate's path joins. We found it with the new GPU test in our builds, where the epilogue reads one more register.

Related: #2793 gives each loop region its own budget in the HLSL path and in the direct translator's plain and compact forms; it touches the same budget code in gpu/dxil/kernel.rs, compact.rs and lower.rs, so whichever lands second needs a rebase and a look at how the two caps combine. #1976 adds XCL_SHADER_LOOP_LIMIT, which also makes the direct translator's loop limit configurable, like XCL_DXIL_LOOP_GUARD here.

What is in it (two commits)

  • crates/xcl/src/gpu/dxil/lower.rs: LOOP_SHARE_ENTRIES (4), loop_guard() (XCL_DXIL_LOOP_GUARD), loop_share_wanted() (XCL_DXIL_LOOP_SHARE), and Init::budget, the lane budget's first value when it is not the constant; a unit test of the guard's parsing.
  • crates/xcl/src/gpu/dxil/kernel.rs: dispatch_loop_cap (the dispatch's share, split out of loop_passes) and share_budget (four shares, at most the guard), made once in the entry block for both forms; loop_guard_tag for the cache identity, with a unit test. The compact form's epilogue now goes into an end block that no SSA edge enters and loads the registers there, so a read in it no longer becomes a phi of a load the entry gate's path never passes ("Instruction does not dominate all uses").
  • crates/xcl/src/gpu/dxil/compact.rs: loop heads count down the budget only when the guard is on; comments on where reads load.
  • crates/xcl/src/gpu/recomp/pso.rs, crates/xcl/src/shaders.rs: non-default switch values get their own translated-cache identity.
  • crates/xcl/src/gpu/dxil/core_tests.rs: direct_compute_lane_budget_follows_the_dispatch_share (the budget in the disassembly, both forms) and gpu_direct_compute_lane_budget_follows_the_dispatch (passes run on the GPU with and without a share).
  • README.md: the loop budget paragraph.

Tested

  • RTX 3060 Ti 8 GB, Windows 11, release build: the change has been in our test builds since 30 Nov (no measured before/after on its own).
  • 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.
  • The guard does not report that it fired, so a loop it ends early in a working program would show only as wrong output.

Switches

  • XCL_DXIL_LOOP_SHARE (default on): =0 leaves a compute lane the guard's passes in all, as before.
  • XCL_DXIL_LOOP_GUARD (default 65 536): the passes a lane may run in all, also the most one loop entry gets; =0 emits no loop guard at all.

AI use

  • Claude Code wrote the change and this description.

🤖 Generated with Claude Code

Prime1997 and others added 2 commits December 2, 2026 05:22
…s share

A loop that never ends on the GPU is a TDR. The direct DXIL translator
already guards every loop it emits: each guest loop head (every target
of a retreating CFG edge, so every cycle, also in the compact form's
dispatcher) counts down the lane's budget (VAR_BUDGET, 65 536) and, in
the plain form of a compute program, each loop entry's own count (the
dispatch's share, IO_LOOP_BUDGET, 1024..65 536 a lane); the loops the
lowering makes itself are bounded too (CAS loops 4096 rounds, the split
form's spin exchange and the safe barrier 2^20 polls). At 0 the lane
goes to the program's end; the budget is the same in every lane of a
console wave and in both halves of a split one (they pass the same
heads, the share is the dispatch's), so the wave leaves as a whole, and
a multi-wave group's safe barrier stops counting it there
(safe_barrier_exit), so no wave waits at a barrier for it. Going to the
end rather than past the loop also never enters another loop.

What was missing is a bound on the lane's whole loop work that follows
the dispatch: nested loops (an inner loop's count starts afresh at each
entry), loops one after another, and every loop of the compact form
(large programs, which has no per-entry count) could still spend the
lane's 65 536 passes - 31 s at the 0.47 ms a pass measured for a retail
loop in a 262 144-thread dispatch on an RTX 3060 Ti. A compute lane's
budget now starts at LOOP_SHARE_ENTRIES (4) loop entries of its share,
at most the lane's guard: 65 536 for small dispatches and indirect ones
(share unknown) as before, 4096 for large ones (~1.9 s at that rate in
the worst case, a single runaway loop of the plain form still ends after
one share). Four entries keep what working programs run (the tests'
three full entries of a loop under a share, as the HLSL path).

Switches (read once; non-default values get their own translated-cache
identity):
- XCL_DXIL_LOOP_GUARD=<n>: the passes a lane may run in all (default
  65 536, also the most one loop entry gets); 0 emits no loop guard.
- XCL_DXIL_LOOP_SHARE=0: the lane's budget does not follow the dispatch.

The guard does not report that it fired: the translator has no feedback
buffer for it, and adding one is more plumbing than this safety net
should carry. The HLSL path and graphics stages keep their budgets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The compact form put the epilogue's register loads in the latch, but a
compute program's entry gate also branches to exit, so a read there
became a phi whose gate operand was a load the gate path never passes
(invalid DXIL, "Instruction does not dominate all uses"). On main the
epilogue read that meets this is the safe barrier's exit
(safe_barrier_exit reads VAR_BAR_ALIVE) in a multi-wave group of a
compact compute program with S_BARRIER. We found it in our builds with
gpu_direct_compute_lane_budget_follows_the_dispatch, whose epilogue
reads one more register there. Release builds do not validate, so
compute shaders of the compact form could reach the driver invalid.

The compact form keeps the registers in local variables, which hold
their values on every way into exit. The epilogue now goes into an end
block that no SSA edge enters and loads them there, so no read becomes
such a phi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Prime1997 Prime1997 self-assigned this Dec 2, 2026
@github-actions github-actions Bot added area:docs-ci Documentation, scripts, workflows, repo governance area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation area:tests Unit, integration, e2e tests, or testtitle gpu:nvidia Code is specific to NVIDIA / wave32 GPUs (scope tag; see needs:nvidia-test) labels Dec 2, 2026
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

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

  • Working loops in large dispatches are now cut, with no sign that it happened. Direct DXIL is the default for compute (gpu/dxil/kernel.rs ~46). For any dispatch of 262 144 threads or more, such as a full-screen 1920x1080 pass, the share is floored at 1024, so share_budget (gpu/dxil/kernel.rs ~1321) gives the lane 4 x 1024 = 4096 loop-head passes for all of its loops together. On main that lane had 1024 passes per loop entry and 65 536 in all. DISPATCH_LOOP_PASSES_MIN (gpu/recomp/mod.rs ~896) exists so that "a working program's loop is not cut short because many threads run it". With this PR, an outer loop of 64 around an inner loop of 64 (4160 passes), or five loops of 1000 one after another, ends early. The lane jumps to the program's end and skips its remaining stores, and nothing is logged. The only basis for the value 4 is the PR's own test. No retail title was measured.
  • The 1.9 s bound only holds at the size that was measured. The 0.47 ms a pass comes from a 262 144-thread dispatch. Because of the 1024 floor, a 2M-thread dispatch also gets 4096 passes. Under main's throughput model (DISPATCH_LOOP_PASSES), each pass of that dispatch takes about 8x as long, so it is still far past the 2 s Windows allows. The PR shortens what working programs can run without holding large dispatches under the limit.
  • The second commit fixes a separate bug in main and has no test that fails on main. The epilogue end block (gpu/dxil/kernel.rs ~630) fixes invalid DXIL for compact compute programs that have S_BARRIER in a multi-wave group. None of the new tests contains S_BARRIER, and no existing test validates that case. The commit message also says the failure was found "in our builds", not on this branch. This fix belongs in its own PR, with a test that validates such a program, for example 64 threads on wave32.
  • Interaction with D3D12.X: cap a small compute dispatch's loop entries at 4096 passes #3667. D3D12.X: cap a small compute dispatch's loop entries at 4096 passes #3667 caps IO_LOOP_BUDGET at 4096 passes per lane for small dispatches. With both PRs merged, small dispatches get 4096 passes per entry and at most 16 384 in all. That contradicts this PR's "small and indirect dispatches keep 65 536 as before". Both PRs change the default cap for the same TDR problem, so they should be measured together, or folded into one change. There are also textual conflicts with Recompiler: bound spin waits of programs without a loop at 2^12 polls #3668 (gpu/recomp/pso.rs: both add an identity tag at the same lines), Recompiler: keep the raw-load shader flag guard in the DCE after block merging #3664 (shaders.rs) and Recompiler: eliminate redundant split-wave64 compute barriers and share exchange helpers #3655.
  • Nothing was run. The PR description check refuses the PR because Regression run has no xcl-smoke block from main or from head e13653d. Tested only lists cargo check for the gnu target; the suite was not run. Both new GPU tests skip on the no-GPU CI, so neither the new budget nor the DXIL fix has run on a GPU.
  • XCL_DXIL_LOOP_GUARD also changes direct vertex and pixel shaders. Init::first (gpu/dxil/lower.rs ~167) and the guard != 0 head check (gpu/dxil/kernel.rs ~549) apply to every stage, so =0 removes the loop guard from graphics too. The README puts the switch in the compute paragraph, and the commit says graphics stages keep their budgets.

Fine: by reading, both new tests would fail without the first commit (4091 vs 10 000; no share-budget select in the disassembly). The guard / 4 compare prevents the multiply from overflowing. Non-default switches get their own cache identity, and gpu/dxil sources are hashed into the direct identity, so old cached programs are not reused.

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: e13653d
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2913 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 #3669 (comment) and its findings stand. Most serious: on by default, lanes of large dispatches get 4096 loop passes in all (main: 65,536), so working loops are silently cut.
performance: 60 s smoke of a retail title, main 1c8b551 vs this merge (e13653d): 1652 -> 1442 frames shown (87 %; last presents per 2 s: 60 60 60), peak commit 18228 -> 15620 MB, 0 device removals, 0 crashes. This run is lower than most today: the PC was in use at the time (a browser and Discord running), and main's own runs on this machine today ranged from 1370 to 1652 frames, so the difference isn't clearly from this PR.
assessment: merges, builds and passes the whole suite on current main, and the smoke run is clean; the blocking point is in the review: on by default, lanes of large dispatches get 4096 loop passes in all (main: 65,536), so working loops are silently cut
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.

@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 e13653d7: 2 finding(s) (1 high, 1 medium), most severe first.

  1. High crates/xcl/src/gpu/dxil/lower.rs:54: LOOP_SHARE_ENTRIES = 4 caps a lane's total loop-head passes across all loops, nested ones included, at 4 x share, so in large dispatches working nested loops are silently cut off and the lane jumps to the program's end, skipping its late.... (inline)
  2. Medium crates/xcl/src/gpu/dxil/lower.rs:58: Four shares were picked to fit under the TDR limit, but at the PR's own measured rate (4096 x 0.47 ms = 1.93 s) the margin under Windows' 2 s timeout is about 3%, so a slightly heavier loop body or other queued GPU work still removes the.... (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.

/// 3060 Ti in a 262 144-thread dispatch (`recomp::DISPATCH_LOOP_PASSES`), 31 s for 65 536 and
/// 1.9 s for these 4 × 1024. Four entries keep what a working program runs (the tests'
/// three full entries of a loop under a share, `counted_loops(1024, 3)`, as the HLSL path).
pub const LOOP_SHARE_ENTRIES: u32 = 4;

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: LOOP_SHARE_ENTRIES = 4 caps a lane's total loop-head passes across all loops, nested ones included, at 4 x share, so in large dispatches working nested loops are silently cut off and the lane jumps to the program's end, skipping its later stores. A 1920x1080 compute pass with 8x8 groups (32 400 groups) gets share = 2^28/32400/64, clamped to 1024, so budget = 4096. A legitimate shader with an outer loop over 32 lights and an inner 128-step march spends 32*129 = 4128 head passes, exceeds 4096, leaves to end partway through the last light, and never writes its output. The PR notes the guard doesn't report when it fires, so this shows up only as corrupted output. Counting per loop entry (as the HLSL path's xcl_cap does), or applying the share only to loops without a per-entry count, would leave working programs alone.


/// `XCL_DXIL_LOOP_GUARD=<n>`: the loop passes a lane of a direct DXIL program may run in all
/// (`VAR_BUDGET`'s start, default `LOOP_BUDGET`), and the most a compute loop's entry gets;
/// `0` emits no loop guard at all (a loop that never ends then hangs the GPU). Read once; a

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.

Medium: Four shares were picked to fit under the TDR limit, but at the PR's own measured rate (4096 x 0.47 ms = 1.93 s) the margin under Windows' 2 s timeout is about 3%, so a slightly heavier loop body or other queued GPU work still removes the device. A runaway compact-form loop (which has no per-entry count) in a 262 144-thread dispatch whose body costs 0.5 ms a pass instead of 0.47 ms runs for 2.05 s, past the timeout, which is the failure the PR sets out to prevent. A larger margin (two shares, or a bound derived from a time target) would make the fix hold.

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

Labels

area:docs-ci Documentation, scripts, workflows, repo governance area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation area:tests Unit, integration, e2e tests, or testtitle 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.

3 participants