Recompiler: enforce LOOP_BUDGET at loop headers in per-lane lowering #2759
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2769"
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?
Imported from ivall/xcl. Originally posted by Saksukella on 2026-11-26T11:33:33Z.
Enforce
LOOP_BUDGETat loop headers in the per-lane SSA lowering backend (XCL_PER_LANE=1), charging weightedloop_costper header visit and keeping per-region budgets for kernel programs while keeping a single program-wideVar::Budgetper lane for compute and graphics shaders. Exhausted lanes branch straight from the loop header toexit, matching direct-DXIL semantics and preventing runaway shaders from triggering Windows TDR (0x887a0006).What is in it
crates/xcl/src/gpu/recomp/ssa.rs: addVar::Budget,Var::BudgetOwner, andVar::BudgetTotal.crates/xcl/src/gpu/recomp/lower_dxil.rs: mapVar::Budget,Var::BudgetOwner, andVar::BudgetTotaltoi32inDxilIr::ty()and handle them inDxilIr::escape().crates/xcl/src/gpu/recomp/lower.rs: detect loop headers from reverse-postorder, split each loop header into a budget-check entry and continuation block, deduct weightedloop_cost(1per128bytes of innermost loop code, clamped to1..=256), apply per-region budgets (LOOP_BUDGET * 2per region,LOOP_BUDGET * 4total) for kernel programs only (keeping compute/graphics on a single program-wide budget to avoid the AMD compiler crash in #2874), and add regression tests (infinite_loop_exits_at_loop_budget,nested_loops_share_loop_budget_across_headers,long_loop_spends_its_code_size_per_header_visit,kernel_loop_after_a_full_spin_keeps_its_own_budget).Tested
32.0.32029.2010), AMD Ryzen 9 7900X, 32 GB RAM, Windows 11 Pro (build26200),rustc 1.100.0 (0ef1bc386 2026-11-09); debug (tests) and release (xcl.exe).cargo test --lib -p xcl recomp::lower -- --test-threads=4: 64 passed, 0 failed, 0 ignored (2.49s), includinginfinite_loop_exits_at_loop_budget,nested_loops_share_loop_budget_across_headers,long_loop_spends_its_code_size_per_header_visit, andkernel_loop_after_a_full_spin_keeps_its_own_budget.SKIPPEDGPU sub-cases on this adapter:f16_programs_on_the_gpu(V_MUL_F16/V_PK_MUL_F16refused by per-lane form) andf32_programs_on_the_gpu(V_CVT_F16_F32refused by per-lane form).cargo check --workspace: passed.Regression run
Not tested
cargo test --workspacerun (targetedrecomp::lowersuite +cargo check --workspace+ 60sTitleID 7952987Asmoke run were executed).AI use
Imported from ivall/xcl. Originally posted by oni-swr on 2026-11-26T11:47:42Z.
Independent amd-win test report (claim: https://github.com/xcl-dev/xcl/issues/1607#issuecomment-7190975352)
Tested: head
4dcc796b9d82220a3655494250bbdd4e00fcc560(unchanged since claim; no other reports) vs. baseb5c0275f(merge-base onamd-dev).Hardware: RX 7800 XT, Ryzen 7 5700X3D, Windows 11 Pro 10.0.26200, debug build, stable-x86_64-pc-windows-msvc. Not release, not a retail title.
Commands (
CARGO_TARGET_DIRseparate,-j 4):cargo test -p xcl --lib -j 4 -- --test-threads=4 --skip placed_title_textures_share_vram_and_preserve_pixels--test-threads=1, on head and on basecargo test -p xcl --lib -j 4 -- --test-threads=1 recomp::Results
gpu::recomp::lower::tests::infinite_loop_exits_at_loop_budget: passes.recomp::alone: 416 passed, 2 failed, 20 ignored (failures:f32_programs_on_the_gpu,a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier).placed_title_textures_share_vram_and_preserve_pixelsalone on head: passes.Attribution (alone,
--test-threads=1): the same 10 tests fail on base and on head:fuzz_validate_and_gpu_1(#2115),dispatch_chain_native_wave64_builds_whole,a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier,f32_programs_on_the_gpu,a_dispatch_with_a_pipeline_still_building_waits_at_the_submit,a_put_off_dispatch_runs_at_the_submit_when_its_build_outlasts_the_wait_budget,bitset_runs_emit_exact_contiguous_page_ranges,giant_driver_compiles_have_their_own_lane,title_bytes_are_used_in_place_when_all_are_readable,physical_home_without_sparse_windows_uses_coherent_window. The 11th,two_pass_binds_set_the_shared_view_heap, fails only in the parallel run and passes alone on both. So no failure I found is caused by this PR. I did not investigate the root causes of the pre-existing ones.SKIPPED: 4 lines, all
f16_programs_on_the_gpu(f16 arithmetic and packed, wave32/wave64, per-lane form refused: V_MUL_F16 / V_PK_MUL_F16, #1426). These are not coverage.Diff review: read the 3-file diff; no concrete defect found. The
LOOP_BUDGETcap is exercised only by the CPU-interpreter test, so the GPU (DXIL) path of the new loop-header block is covered only by the existing per-lane GPU tests, which do not include a loop that hits the budget. I did not run a retail title orscripts/smoke.ps1.AI use: Claude Code (Claude Sonnet 5.5), coordinated by Claude Code (Claude Opus 5.5). Results are from real runs on the hardware above.
Imported from ivall/xcl. Originally posted by gut0to on 2026-11-26T15:45:29Z.
Review: COMMENTED
AI-assisted review (OpenAI GPT-6). I checked the SSA budget flow against the other lowering backends; I did not run tests or the game.
Imported from ivall/xcl. Originally posted by gut0to on 2026-11-26T15:45:29Z.
Code review location:
crates/xcl/src/gpu/recomp/lower.rs; line 4644Var::Budgetis initialized at entry and then consumed at every detected loop header, matching the sharedxcl_budgetin the other backends. This regression covers only one self-loop, so it does not pin SSA propagation through nested headers. Could you add a nested-loop case that shows the inner loop consumes the same per-lane budget and the total work remains bounded, rather than accidentally reinitializing at an inner header? AI-assisted review (OpenAI GPT-6); I checked the new lowering against the shared-budget paths inemit.rsanddxil/kernel.rs, but did not run tests or the game.Imported from ivall/xcl. Originally posted by Saksukella on 2026-11-26T18:06:44Z.
Added
nested_loops_share_loop_budget_across_headersin9a6d83c1right besideinfinite_loop_exits_at_loop_budget: an infinite outer loop wrapping aK = 3inner loop asserts that both headers decrement the same entry-initializedVar::Budget(s[0] + s[2] == LOOP_BUDGETand per-lanev[0][1] == expected_inner) without reinitializing at the inner header. Verified withgpu::recomp::lower::tests(38 passed; 0 failed) and a fresh 60s RX 9070 XT smoke (1,650frames,60 60 60,0crashes,0device removals,15,548 MBpeak commit).Imported from ivall/xcl. Originally posted by vibryxcx on 2026-11-27T02:21:36Z.
Tested on the current
amd-devand reviewed against it. The tests are fine, but the parity in the description no longer holds: since00fe5aethe other backends spend the budget by loop cost and by region in compute, and this per-lane version spends 1 per header from one program-wide budget.Tested: RX 6800 (RDNA2, driver 32.0.21045.5002), Windows 11 Pro 26200, Rust 1.100.0, debug. This head
9a6d83c1sits onb5c0275f. It merges ontoamd-devbf7c82eewith no conflict, so I tested that merge againstbf7c82ee,cargo test -p xcl --lib -- gpu::recomp:: --test-threads=1:bf7c82ee: 430 passed, 2 failed, 20 ignored.infinite_loop_exits_at_loop_budget,nested_loops_share_loop_budget_across_headers), both ok. The 2 failures are the same on both (a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier,f32_programs_on_the_gpu).f16_programs_on_the_gpuonly (4 lines, #1426), on both.Review
Parity with the other backends on
amd-dev.00fe5ae("bound long shader loops and keep mesh budgets simple") changed the HLSL path (recomp/mod.rs) and direct DXIL (dxil/kernel.rs,dxil/compact.rs) in two ways:loop_cost(an 80-instruction loop runs half the iterations,gpu_an_endless_long_loop_stops_after_the_budget_by_its_cost);budget_regions, from #2793 / #2865).This PR's per-lane lowering spends 1 per loop-header visit from a single
Var::Budgetfor the whole program. UnderXCL_PER_LANE=1, then:gpu_a_loop_after_a_spin_to_the_budget_still_runs); a per-lane counterpart of that test would show it.This may be deliberate. On RDNA2, per-region budgets in compute are what crash the AMD shader compiler on a retail title (#2874), so one budget is the safer shape for the driver. If so, saying it in the description (or porting only
loop_cost) would make the difference intended rather than accidental.Exit from a loop header. An exhausted lane branches from the header straight to
exitand skips the rest of the program, stores included. That matches what the HLSL and DXIL paths do.exit_stalenow checks staleness only on theendstails, not on the header-to-exit edges. So a register that is defined only after a loop header and read at exit will not be reported stale on the budget path. That seems acceptable for a runaway lane, but nothing tests it.Scope at runtime. All of this is under
XCL_PER_LANE=1, which is off by default (pso.rskeys those entries asxcl-per-lane-…). A default title run does not reach it, so I did not run the title for this PR.Not tested:
XCL_PER_LANE=1on a retail title (this card loses the device onbf7c82eewithin minutes anyway, #2874); this head on its own base; NVIDIA.AI use: Claude Code (Claude Opus 5.5) read the diff and
00fe5ae, ran the tests on base and merge, and wrote this comment.Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-28T01:44:01Z.
Following the budget-parity review, I tested a small correction on #2769 head
9a6d83c1merged into currentamd-dev7c795f5e.The current base matters here: compute and graphics deliberately keep one budget because the per-region shape hit the AMD compiler (#2874); kernel programs still use per-region budgets. The candidate retains that distinction. It charges each loop header using the existing
loop_costrule, and gives kernel programs only the existing per-region owner and total counters.Two targeted regressions exercise behavior the PR's current tests do not cover:
cargo test --offline --locked -j 4 -p xcl --lib gpu::recomp::lower::tests -- --test-threads=1with the candidate: 41 passed, 0 failed, 0 ignored, including the new GPU check; four wave64 skip notices on this RTX 3080 Ti. Windows 11 build 26300, driver 621.35, Rust 1.99.1, DXC 1.8.2502.11. The full workspace suite, AMD hardware and a title run were not tested on this uncommitted candidate. This is a fix suggestion for the existing PR, not a merge-readiness claim.Candidate diff, including the focused regressions
AI use: Codex (GPT-6.2 Sol) traced the current lowering paths, wrote and ran the local regressions and candidate, and drafted this reply.
Imported from ivall/xcl. Originally posted by throny on 2026-11-28T17:24:37Z.
merge-check claim
head:
9a6d83c127Imported from ivall/xcl. Originally posted by throny on 2026-11-28T17:46:40Z.
merge-check
base:
7c795f5e(amd-dev)head:
9a6d83c127merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 on amd-dev
7c795f5e+ this PR -> 1806 passed, 16 failed, 48 ignored (21 SKIPPED lines) on an RTX 4080. The base (see #2605, which changes nothing) has 1802 passed, 17 failed in the run beside it; all 16 failures here are among the base's 17, so nothing new fails on this card (the one that differs, the_mirror_sync_copies_only_the_pages_the_title_wrote, is not in this PR's area).review: the mechanism is sound and better than before (the per-lane form had no cap at all, so a runaway loop hung the GPU), but the parity points from the thread are unanswered and the description claims a parity that current amd-dev does not have. (1) Every loop-header visit costs 1; the direct-DXIL path weighs a visit by the loop's cost, so a long loop can run up to 256 times more iterations here before the cap, and that cap exists because of driver timeouts. (2) There is one counter per lane for the whole program, set once at entry; a lane that passes 65,536 header visits in total leaves through the exit, and in compute everything after the loop, stores included, is skipped without a log line (the direct path does the same; the HLSL path breaks out of the loop and runs on). Not tested by anyone: a lane leaving early before a barrier in divergent control flow, and ballots once a lane has left.
performance: one compare and one subtraction per loop-header visit. It changes per-lane sources only: only per-lane cache entries are re-translated.
verdict: not yet: answer the two parity reviews (loop cost; what the description says), then I re-check.
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).
Imported from ivall/xcl. Originally posted by Saksukella on 2026-11-28T23:54:07Z.
Rebased cleanly onto
main(227d2cb388) and retargeted this PR's base branch tomain.Summary of updates addressing review feedback:
loop_costdeduction: Each loop-header visit now deductsloop_cost(1per128bytes of innermost loop code, clamped to1..=256) and checksCmp::Ultagainstcost, so long loops cannot run up to 256x more iterations before hittingLOOP_BUDGET.BudgetOwner/BudgetTotal): Kernel programs track per-region budgets (LOOP_BUDGET * 2per region,LOOP_BUDGET * 4total). Compute and graphics shaders intentionally stay on a single program-wideVar::Budgetper lane (avoiding the AMD compiler crash in #2874).exit, matching direct-DXIL semantics.cargo test --lib -p xcl recomp::lower -- --test-threads=4: 64 passed, 0 failed (includinglong_loop_spends_its_code_size_per_header_visitandkernel_loop_after_a_full_spin_keeps_its_own_budget).cargo check --workspace: passed.smoke.ps1(TitleID 7952987A, heada68bf237e5d0):1687frames shown (main227d2cb388:1674),0device removals,0crashes,18139 MBpeak commit.Imported from ivall/xcl. Originally posted by throny on 2026-11-29T00:30:03Z.
merge-check claim
head:
a68bf237e5Re-check of the head rebased onto main.
Imported from ivall/xcl. Originally posted by throny on 2026-11-29T00:46:58Z.
merge-check
base:
fa204595head:
a68bf237e5merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2612 passed, 0 failed, 49 ignored (27 SKIPPED lines). Base alone at
fa204595: 2608 passed, 0 failed, 49 ignored.review: re-check of the head rewritten onto main, and first a correction on my side. My check of 2026-11-28 asked for parity with the direct-DXIL path's loop-cost weighting. That was true of amd-dev at
7c795f5e, the PR's target that day (the weighting came with00fe5aec). The PR now targets main, and neither main (fa204595) nor today's amd-dev (8942a5b9, rewritten since) has any loop-cost weighting: the direct-DXIL, compact and HLSL paths all charge 1 per loop-header visit. So the weighting and the per-region budgets this head adds to answer that point make the per-lane form stricter than every other path on main: a loop of 32 KiB or more would end after 256 header visits where the others allow 65,536, which can cut a legitimate long loop short. The comment "matching the other lowering backends" is therefore not true on main. Sorry for the detour; the earlier 1-per-visit form is the one that matches main. Still open from the thread: exit_stale ignores the header-to-exit edges; a lane leaving before a barrier in divergent flow and ballots after a lane has left are untested; a lane over budget still skips the rest silently (as the direct path does), without a log line. Process: the smoke run does not reach this code without XCL_PER_LANE=1, and AI use names no tool or model.performance: one compare and one subtraction per loop-header visit. Per-lane sources only: only per-lane cache entries are re-translated.
verdict: not yet: on main, go back to 1 per header visit (or show why per-lane needs the weighting when no other path has it), and name the model under AI use.
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).
Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-30T02:39:57Z.
NVIDIA and AMD full-suite run of head
a68bf237, on its own base227d2cband merged with currentmain53e85fb1, to go with the reports above by throny, since none of them covers AMD. Nothing here fails because of this PR. On the Radeon the lib test binary stops partway on this base and onmainalike (#3488), so the AMD runs are partial. The bare-head runs come first.Tested
cargo build --workspace, thenXCL_REQUIRE_GPU=mesh cargo test --workspace --no-fail-fast -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::recomp::lower_dxil::tests::mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu --skip gpu::recomp::lower_dxil::tests::bridged_dpp_agrees_with_hlsl_on_the_gpu --skip hle::d3d12x::bind::tests::two_pass_binds_set_the_shared_view_heap --skip gpu::dxil::fuzz_tests: 2586 passed, 0 failed, 47 ignored. SKIPPED (no wave64):a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu,a_per_lane_2d_or_3d_group_runs_on_the_gpu,coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu,cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu,dispatch_chain_native_wave64_builds_whole,gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware,the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.XCL_ADAPTER=radeon),cargo test --workspace --no-fail-fast -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 --skip two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs: 138 passed, 0 failed, 20 ignored, incomplete: thexcllib test binary stopped itself partway (exit code0x80004005, aftersparse upload preparation still failed after bounded retries), so most of its tests did not run. That stop comes from #2959 onmainand is not this PR's: #3488.--skipleaves out tests that fail on main53e85fb1too, not because of this PR. On NVIDIA:heap_window_out_of_memory_falls_back_to_the_switch_form(#1651), the fourgpu::recomp::lower_dxil::tests::*_programs_on_the_gputests,const_and_null_mask_operands_agree_with_hlsl_on_the_gpuandmask_ff0_and_flbit_agree_with_hlsl_on_the_gpu(follow-up on #1651) fail becauserunner::skip_testturns their skips into failures underXCL_REQUIRE_GPU=mesh,bridged_dpp_agrees_with_hlsl_on_the_gpu(no wave64 on this card) andtwo_pass_binds_set_the_shared_view_heap(heap windows are opt-in,XCL_HEAP_WINDOWS=1) fail the same way;gpu::dxil::fuzz_testsstays skipped as in earlier runs (seeds 70, 545, 1019 and 1507 failedCreateComputePipelineStatewithE_OUTOFMEMORYon this card; they passed on this main's baseline). On AMD:fuzz_validate_and_gpu_1fails on seed 540 (res[833]0x7 vs CPU 0x6).two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobsfails on main too, since #2959 (#3441). The AMD run does not setXCL_REQUIRE_GPU.main53e85fb1(git merge --no-ff, clean, local only), same commands and skips. NVIDIA: 2782 passed, 1 failed, 47 ignored; the one failure isconcurrent_shell_miss_reuses_winner_and_releases_loser_pipeline, which fails onmainitself since #2369 (already reported there: https://github.com/xcl-dev/xcl/pull/2369#issuecomment-7264263758). AMD: 138 passed, 0 failed, 20 ignored, incomplete: the same #3488 stop, and before itgpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form(new onmainwith #3263) fails the same way on baremain53e85fb1.main53e85fb1on both cards, same commands without skips, for comparison: NVIDIA fails the skipped tests plus the #2369 test; the Radeon stops at the same place.Not tested
AI use
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:55:13Z.
Triage (#1607) – head
a68bf23No other PR adds a loop budget to the per-lane SSA lowering (
recomp/lower.rs). #2793 adds per-region budgets to the plain, compact and kernel DXIL backends but does not touch this file. This PR defines its own private copies ofLOOP_BUDGETS_PER_REGION,LOOP_BUDGETS_IN_ALL,budget_regionsandbudget_region, which #2793 puts inrecomp/mod.rs; if both land, one copy should go. The comment saysloop_cost(1 unit per 128 bytes of the innermost loop) matches the other backends, but on main the plain and compact backends spend 1 per header visit and nothing on main definesloop_cost. So a long loop gets fewer iterations underXCL_PER_LANE=1than under the default backend. Either drop the cost or change the comment. The branch conflicts with main.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.