Skip to content

Recompiler: enforce LOOP_BUDGET at loop headers in per-lane lowering - #2769

Open
Saksukella wants to merge 2 commits into
xcl-dev:amd-devfrom
Saksukella:fix/per-lane-loop-budget
Open

Saksukella wants to merge 2 commits into
xcl-dev:amd-devfrom
Saksukella:fix/per-lane-loop-budget

Conversation

@Saksukella

@Saksukella Saksukella commented Nov 26, 2026 •

Copy link
Copy Markdown

What is in it

  • crates/xcl/src/gpu/recomp/ssa.rs:
    • Add Var::Budget to track remaining loop-header iterations (LOOP_BUDGET = 1 << 16) per lane in the per-lane SSA lowering backend.
  • crates/xcl/src/gpu/recomp/lower_dxil.rs:
    • Map Var::Budget to i32 in DxilIr::ty() and add the Var::Budget arm in DxilIr::escape().
  • crates/xcl/src/gpu/recomp/lower.rs:
    • Detect loop headers from reverse-postorder (rpo_pos[b] >= rpo_pos[s]), split each loop header into a budget-check entry block and continuation block (budget_cont), initialize Var::Budget to crate::gpu::dxil::lower::LOOP_BUDGET at block 0, and branch to exit when the counter reaches 0 (otherwise decrement and continue).
    • Brings XCL_PER_LANE=1 into parity with the HLSL backend (recomp/emit.rs xcl_budget = 1u << 16) and the per-instruction Direct DXIL backend (gpu/dxil/kernel.rs VAR_BUDGET), preventing runaway compute shaders (e.g. zero-stride or wrapping loop bounds) from hanging the GPU until Windows TDR (0x887a0006).
    • Add CPU unit test infinite_loop_exits_at_loop_budget and nested_loops_share_loop_budget_across_headers verifying an unconditional back-edge loop exits cleanly after exactly LOOP_BUDGET iterations.
  • Target branch: amd-dev.

Tested

  • Hardware: AMD Radeon RX 9070 XT (driver 32.0.32029.2010), 24-thread x86_64 CPU, 32 GB RAM, Windows; rustc 1.100.0 (0ef1bc386 2026-11-09); debug (tests) and release (xcl.exe).
  • cargo test -p xcl --lib gpu::recomp::lower::tests: 37 passed, 0 failed (0.43s), including infinite_loop_exits_at_loop_budget.

Regression run

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: AMD Radeon RX 9070 XT
title: b8db18c2
seconds: 60
exit: 0x0
frames shown: 1624 (PresentX 1624)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19700 MB
xcl-smoke v1
commit: 4dcc796b9d82220a3655494250bbdd4e00fcc560
gpu: AMD Radeon RX 9070 XT
title: b8db18c2
seconds: 60
exit: 0x0
frames shown: 1609 (PresentX 1609)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15113 MB

Not tested

  • NVIDIA and Intel GPU adapters (tested on AMD Radeon RX 9070 XT only).

AI use

  • Antigravity (Google Gemini): assisted with XCL_GPU_CULPRIT / DRED TDR diagnosis, DXBC disassembly comparison across backends, writing the Var::Budget per-lane lowering patch and unit test, and running local tests and smoke verification. All diffs and measurements were verified locally on hardware.
xcl-smoke v1
commit: 9a6d83c1276485fdd256c3a694feb2d09504b4dc
gpu: AMD Radeon RX 9070 XT
title: b8db18c2
seconds: 60
exit: 0x0
frames shown: 1650 (PresentX 1650)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15548 MB

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Independent amd-win test report (claim: #1607 (comment))

Tested: head 4dcc796b9d82220a3655494250bbdd4e00fcc560 (unchanged since claim; no other reports) vs. base b5c0275f (merge-base on amd-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_DIR separate, -j 4):

  • cargo test -p xcl --lib -j 4 -- --test-threads=4 --skip placed_title_textures_share_vram_and_preserve_pixels
  • each failing test again alone with --test-threads=1, on head and on base
  • cargo test -p xcl --lib -j 4 -- --test-threads=1 recomp::

Results

  • Head, full lib: 1533 passed, 11 failed, 27 ignored, 1 filtered (the skipped test).
  • New test gpu::recomp::lower::tests::infinite_loop_exits_at_loop_budget: passes.
  • Head 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_pixels alone on head: passes.
  • Base, full lib: the parallel run aborted with STATUS_STACK_BUFFER_OVERRUN (known amd-dev behaviour, here in a different test than on head), so I have no clean full-run count for base. Instead I ran each of the 11 head failures alone on both commits.

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_BUDGET cap 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 or scripts/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.

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

}

#[test]
fn infinite_loop_exits_at_loop_budget() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Var::Budget is initialized at entry and then consumed at every detected loop header, matching the shared xcl_budget in 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 in emit.rs and dxil/kernel.rs, but did not run tests or the game.

@Saksukella

Copy link
Copy Markdown
Author

Added nested_loops_share_loop_budget_across_headers in 9a6d83c1 right beside infinite_loop_exits_at_loop_budget: an infinite outer loop wrapping a K = 3 inner loop asserts that both headers decrement the same entry-initialized Var::Budget (s[0] + s[2] == LOOP_BUDGET and per-lane v[0][1] == expected_inner) without reinitializing at the inner header. Verified with gpu::recomp::lower::tests (38 passed; 0 failed) and a fresh 60s RX 9070 XT smoke (1,650 frames, 60 60 60, 0 crashes, 0 device removals, 15,548 MB peak commit).

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…xcl-dev#2818 (loop budget, wave32 mask kill, SDWA SDST, u32x2 routing)
@vibryxcx

Copy link
Copy Markdown

Tested on the current amd-dev and reviewed against it. The tests are fine, but the parity in the description no longer holds: since 00fe5ae the 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 9a6d83c1 sits on b5c0275f. It merges onto amd-dev bf7c82ee with no conflict, so I tested that merge against bf7c82ee, cargo test -p xcl --lib -- gpu::recomp:: --test-threads=1:

  • bf7c82ee: 430 passed, 2 failed, 20 ignored.
  • merge: 432 passed, 2 failed, 20 ignored. The only difference is the two new tests (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).
  • SKIPPED: f16_programs_on_the_gpu only (4 lines, gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426), on both.

Review

  1. 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:

    This PR's per-lane lowering spends 1 per loop-header visit from a single Var::Budget for the whole program. Under XCL_PER_LANE=1, then:

    This may be deliberate. On RDNA2, per-region budgets in compute are what crash the AMD shader compiler on a retail title (amd-dev f84a949: AMD driver crashes (amdxc64.dll+0xd2a411) compiling the title's native DXIL compute shader ca7beffb8d3a8f28 (RX 6750 XT) #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.

  2. Exit from a loop header. An exhausted lane branches from the header straight to exit and skips the rest of the program, stores included. That matches what the HLSL and DXIL paths do. exit_stale now checks staleness only on the ends tails, 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.

  3. Scope at runtime. All of this is under XCL_PER_LANE=1, which is off by default (pso.rs keys those entries as xcl-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=1 on a retail title (this card loses the device on bf7c82ee within 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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants