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

Open
Prime1997 wants to merge 2 commits from upstream-pr/3669 into main
Prime1997 commented 2026-12-02 05:54:24 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Prime1997 on 2026-12-02T05:54:24Z.


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 1c8b5514): 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

<!-- greyfinch-sync:ivall/xcl:issue:5600749349 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3669). Originally posted by [Prime1997](https://github.com/Prime1997) on 2026-12-02T05:54:24Z. --- <!-- claude-projects-attribution: {"github_login":"Prime1997"} --> _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 1c8b5514): `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](https://claude.com/claude-code)
Foniass commented 2026-12-02 09:12:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T09:12:56Z.


Review (head e13653d7, 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 #3667. #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 #3668 (gpu/recomp/pso.rs: both add an identity tag at the same lines), #3664 (shaders.rs) and #3655.
  • Nothing was run. The PR description check refuses the PR because Regression run has no xcl-smoke block from main or from head e13653d7. 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.

<!-- greyfinch-sync:ivall/xcl:comment:7340551173 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3669#issuecomment-7340551173). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-02T09:12:56Z. --- Review (head e13653d7, 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 #3667.** #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 #3668 (`gpu/recomp/pso.rs`: both add an identity tag at the same lines), #3664 (`shaders.rs`) and #3655. - **Nothing was run.** The PR description check refuses the PR because **Regression run** has no `xcl-smoke` block from main or from head e13653d7. **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 commented 2026-12-02 11:39:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T11:39:18Z.


Test report (not a merge-check: PR description red)
base: 1c8b551475
head: e13653d721
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 https://github.com/xcl-dev/xcl/pull/3669#issuecomment-7340551173 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 1c8b5514 vs this merge (e13653d7): 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.

<!-- greyfinch-sync:ivall/xcl:comment:7344220920 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3669#issuecomment-7344220920). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-02T11:39:18Z. --- Test report (not a merge-check: `PR description` red) base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: e13653d7219c029b673dc025b359703f9f34e10e 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 https://github.com/xcl-dev/xcl/pull/3669#issuecomment-7340551173 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 1c8b5514 vs this merge (e13653d7): 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 commented 2026-12-02 20:47:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T20:47:03Z.

Code review location: crates/xcl/src/gpu/dxil/lower.rs; line 54

@@ -36,10 +36,43 @@ pub const VAR_XLW: u32 = VAR_BUDGET + 6;
 /// `tw0 + 1` of the last image store / atomic through the k-th T# of `Lw::img_writes` (16
 /// slots; the recompiler's `xcl_iw{srsrc}`)
 pub const VAR_IMG_WRITE: u32 = VAR_BUDGET + 8;
-/// loop iterations a lane may run (the recompiler's `xcl_budget`). The plain form of a compute
-/// program also counts each entry of a loop (`VAR_LOOP_PASSES`, `kernel::loop_passes`); the
-/// compact form (`compact`) and the per-lane form (`recomp::lower_dxil`) keep this budget alone.
+/// loop iterations a lane may run (the recompiler's `xcl_budget`; `loop_guard` changes it). The
+/// plain form of a compute program also counts each entry of a loop (`VAR_LOOP_PASSES`,
+/// `kernel::loop_passes`); the compact form (`compact`) and the per-lane form
+/// (`recomp::lower_dxil`) keep this budget alone. A compute program's lane has at most
+/// `LOOP_SHARE_ENTRIES` entries' worth of its dispatch's share in all (`loop_share_wanted`).
 pub const LOOP_BUDGET: u32 = 1 << 16;
+/// How many loop entries' worth of the dispatch's share (`kernel::dispatch_loop_cap`, between
+/// `DISPATCH_LOOP_PASSES_MIN` and the lane's budget) a compute lane may run in all loops
+/// together: the per-entry count alone lets nested loops (each entry of an inner loop starts
+/// afresh), loops after one another and every loop of the compact form (which has no per-entry
+/// count) run the lane's whole 65 536 passes, which on a large dispatch is far past the 2 s
+/// Windows waits before it removes the device: a retail loop measured 0.47 ms a pass on an RTX
+/// 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;

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.

<!-- greyfinch-sync:ivall/xcl:review-comment:4775811889 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3669#discussion_r4775811889). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T20:47:03Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower.rs`; line 54 ~~~~diff @@ -36,10 +36,43 @@ pub const VAR_XLW: u32 = VAR_BUDGET + 6; /// `tw0 + 1` of the last image store / atomic through the k-th T# of `Lw::img_writes` (16 /// slots; the recompiler's `xcl_iw{srsrc}`) pub const VAR_IMG_WRITE: u32 = VAR_BUDGET + 8; -/// loop iterations a lane may run (the recompiler's `xcl_budget`). The plain form of a compute -/// program also counts each entry of a loop (`VAR_LOOP_PASSES`, `kernel::loop_passes`); the -/// compact form (`compact`) and the per-lane form (`recomp::lower_dxil`) keep this budget alone. +/// loop iterations a lane may run (the recompiler's `xcl_budget`; `loop_guard` changes it). The +/// plain form of a compute program also counts each entry of a loop (`VAR_LOOP_PASSES`, +/// `kernel::loop_passes`); the compact form (`compact`) and the per-lane form +/// (`recomp::lower_dxil`) keep this budget alone. A compute program's lane has at most +/// `LOOP_SHARE_ENTRIES` entries' worth of its dispatch's share in all (`loop_share_wanted`). pub const LOOP_BUDGET: u32 = 1 << 16; +/// How many loop entries' worth of the dispatch's share (`kernel::dispatch_loop_cap`, between +/// `DISPATCH_LOOP_PASSES_MIN` and the lane's budget) a compute lane may run in all loops +/// together: the per-entry count alone lets nested loops (each entry of an inner loop starts +/// afresh), loops after one another and every loop of the compact form (which has no per-entry +/// count) run the lane's whole 65 536 passes, which on a large dispatch is far past the 2 s +/// Windows waits before it removes the device: a retail loop measured 0.47 ms a pass on an RTX +/// 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; ~~~~ --- **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.
bmjask commented 2026-12-02 20:47:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T20:47:03Z.

Code review location: crates/xcl/src/gpu/dxil/lower.rs; line 58

@@ -36,10 +36,43 @@ pub const VAR_XLW: u32 = VAR_BUDGET + 6;
 /// `tw0 + 1` of the last image store / atomic through the k-th T# of `Lw::img_writes` (16
 /// slots; the recompiler's `xcl_iw{srsrc}`)
 pub const VAR_IMG_WRITE: u32 = VAR_BUDGET + 8;
-/// loop iterations a lane may run (the recompiler's `xcl_budget`). The plain form of a compute
-/// program also counts each entry of a loop (`VAR_LOOP_PASSES`, `kernel::loop_passes`); the
-/// compact form (`compact`) and the per-lane form (`recomp::lower_dxil`) keep this budget alone.
+/// loop iterations a lane may run (the recompiler's `xcl_budget`; `loop_guard` changes it). The
+/// plain form of a compute program also counts each entry of a loop (`VAR_LOOP_PASSES`,
+/// `kernel::loop_passes`); the compact form (`compact`) and the per-lane form
+/// (`recomp::lower_dxil`) keep this budget alone. A compute program's lane has at most
+/// `LOOP_SHARE_ENTRIES` entries' worth of its dispatch's share in all (`loop_share_wanted`).
 pub const LOOP_BUDGET: u32 = 1 << 16;
+/// How many loop entries' worth of the dispatch's share (`kernel::dispatch_loop_cap`, between
+/// `DISPATCH_LOOP_PASSES_MIN` and the lane's budget) a compute lane may run in all loops
+/// together: the per-entry count alone lets nested loops (each entry of an inner loop starts
+/// afresh), loops after one another and every loop of the compact form (which has no per-entry
+/// count) run the lane's whole 65 536 passes, which on a large dispatch is far past the 2 s
+/// Windows waits before it removes the device: a retail loop measured 0.47 ms a pass on an RTX
+/// 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;
+
+/// `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

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.

<!-- greyfinch-sync:ivall/xcl:review-comment:4775811902 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3669#discussion_r4775811902). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T20:47:03Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower.rs`; line 58 ~~~~diff @@ -36,10 +36,43 @@ pub const VAR_XLW: u32 = VAR_BUDGET + 6; /// `tw0 + 1` of the last image store / atomic through the k-th T# of `Lw::img_writes` (16 /// slots; the recompiler's `xcl_iw{srsrc}`) pub const VAR_IMG_WRITE: u32 = VAR_BUDGET + 8; -/// loop iterations a lane may run (the recompiler's `xcl_budget`). The plain form of a compute -/// program also counts each entry of a loop (`VAR_LOOP_PASSES`, `kernel::loop_passes`); the -/// compact form (`compact`) and the per-lane form (`recomp::lower_dxil`) keep this budget alone. +/// loop iterations a lane may run (the recompiler's `xcl_budget`; `loop_guard` changes it). The +/// plain form of a compute program also counts each entry of a loop (`VAR_LOOP_PASSES`, +/// `kernel::loop_passes`); the compact form (`compact`) and the per-lane form +/// (`recomp::lower_dxil`) keep this budget alone. A compute program's lane has at most +/// `LOOP_SHARE_ENTRIES` entries' worth of its dispatch's share in all (`loop_share_wanted`). pub const LOOP_BUDGET: u32 = 1 << 16; +/// How many loop entries' worth of the dispatch's share (`kernel::dispatch_loop_cap`, between +/// `DISPATCH_LOOP_PASSES_MIN` and the lane's budget) a compute lane may run in all loops +/// together: the per-entry count alone lets nested loops (each entry of an inner loop starts +/// afresh), loops after one another and every loop of the compact form (which has no per-entry +/// count) run the lane's whole 65 536 passes, which on a large dispatch is far past the 2 s +/// Windows waits before it removes the device: a retail loop measured 0.47 ms a pass on an RTX +/// 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; + +/// `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 ~~~~ --- **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.
bmjask commented 2026-12-02 20:47:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T20:47:03Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ivall/xcl:review:6158714044 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3669#pullrequestreview-6158714044). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T20:47:03Z. **Review:** COMMENTED --- 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._
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3669:upstream-pr/3669'
git switch 'upstream-pr/3669'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/3669'
git switch 'upstream-pr/3669'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3669'
git switch 'upstream-pr/3669'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3669'
git switch 'main'
git merge --squash 'upstream-pr/3669'
git switch 'main'
git merge --ff-only 'upstream-pr/3669'
git switch 'main'
git merge 'upstream-pr/3669'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3669
No description provided.