Repository navigation
Recompiler: enforce LOOP_BUDGET at loop headers in per-lane lowering - #2769
Saksukella wants to merge 3 commits into
Conversation
|
Independent amd-win test report (claim: #1607 (comment)) Tested: head Commands (
Results
Attribution (alone, SKIPPED: 4 lines, all Diff review: read the 3-file diff; no concrete defect found. The 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
left a comment
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
|
Added |
…xcl-dev#2818 (loop budget, wave32 mask kill, SDWA SDST, u32x2 routing)
|
Tested on the current Tested: RX 6800 (RDNA2, driver 32.0.21045.5002), Windows 11 Pro 26200, Rust 1.100.0, debug. This head
Review
Not tested: AI use: Claude Code (Claude Opus 5.5) read the diff and |
|
Following the budget-parity review, I tested a small correction on #2769 head 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 Two targeted regressions exercise behavior the PR's current tests do not cover:
Candidate diff, including the focused regressionsdiff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs
index c462be09..d893da9b 100644
--- a/crates/xcl/src/gpu/recomp/lower.rs
+++ b/crates/xcl/src/gpu/recomp/lower.rs
@@ -1257,6 +1257,8 @@ fn lower_with<I: Ir>(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s
if n == 0 {
return Err("empty program".into());
}
+ let loops = super::loop_ranges(insts);
+ let regions = if kernel { super::budget_regions(&loops) } else { Vec::new() };
// IR blocks: 0 prologue, 1..=n the program's, n + 1 exit, n + 2 lane 0's scalar stores,
// n + 3 every lane's vector stores, n + 4 the way to the exit of lanes outside EXEC at launch,
// then for every coalesced guarded run (only the lanes with their EXEC bit enter): the run's
@@ -1401,6 +1403,12 @@ fn lower_with<I: Ir>(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s
l.ssa.seal(&mut l.ir, 0);
let budget = l.ir.int(LOOP_BUDGET);
l.ssa.write(0, Var::Budget, budget);
+ if !regions.is_empty() {
+ let owner = l.ir.int(0);
+ let total = l.ir.int(LOOP_BUDGET * super::LOOP_BUDGETS_IN_ALL);
+ l.ssa.write(0, Var::BudgetOwner, owner);
+ l.ssa.write(0, Var::BudgetTotal, total);
+ }
let lane = l.lane();
for &r in &sregs {
let a = l.ir.int(r as u32);
@@ -1550,18 +1558,38 @@ fn lower_with<I: Ir>(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s
let body_end = block_body_end(insts, &block.insts);
let mut cur = ib;
if let Some(bc) = budget_cont[b] {
- // cap a lane's loop iterations (matching emit.rs and dxil/kernel.rs LOOP_BUDGET)
- let left = l.read(cur, Var::Budget);
- let z = l.ir.int(0);
- let exhausted = l.ir.icmp(Cmp::Eq, left, z);
+ // Charge long loops for their code size, as the other backends do.
+ let pc = insts[block.insts.start].pc;
+ let cost = l.ir.int(super::loop_cost(&loops, pc));
+ let mut left = l.read(cur, Var::Budget);
+ let mut total = None;
+ let region = super::budget_region(®ions, pc);
+ if let Some(r) = region {
+ let owner = l.read(cur, Var::BudgetOwner);
+ let r_value = l.ir.int(r);
+ let changed = l.ir.icmp(Cmp::Ne, owner, r_value);
+ let fresh = l.ir.int(LOOP_BUDGET * super::LOOP_BUDGETS_PER_REGION);
+ left = l.ir.select(changed, fresh, left);
+ total = Some(l.read(cur, Var::BudgetTotal));
+ }
+ let mut exhausted = l.ir.icmp(Cmp::Ult, left, cost);
+ if let Some(t) = total {
+ let depleted = l.ir.icmp(Cmp::Ult, t, cost);
+ exhausted = l.ir.bin(Bin::Or, exhausted, depleted);
+ }
l.ir.br_if(exhausted, exit, bc);
done[cur] = true;
l.ir.at(bc);
l.ssa.seal(&mut l.ir, bc);
sealed[bc] = true;
- let one = l.ir.int(1);
- let next = l.ir.bin(Bin::Sub, left, one);
+ let next = l.ir.bin(Bin::Sub, left, cost);
l.ssa.write(bc, Var::Budget, next);
+ if let (Some(r), Some(t)) = (region, total) {
+ let owner = l.ir.int(r);
+ l.ssa.write(bc, Var::BudgetOwner, owner);
+ let remaining = l.ir.bin(Bin::Sub, t, cost);
+ l.ssa.write(bc, Var::BudgetTotal, remaining);
+ }
cur = bc;
}
let mut split = splits[b].iter().peekable();
@@ -1867,7 +1895,7 @@ impl<I: Ir> Lower<'_, I> {
let s = self.ir.icmp(Cmp::Ne, v, z);
self.write_scc(b, s);
}
- Var::S(_) | Var::M(_) | Var::Budget => {}
+ Var::S(_) | Var::M(_) | Var::Budget | Var::BudgetOwner | Var::BudgetTotal => {}
}
}
if name.starts_with("V_") && !sw.is_empty() {
@@ -4657,6 +4685,64 @@ pub(crate) mod tests {
assert_eq!(v[0][1], LOOP_BUDGET);
}
+ #[test]
+ fn long_loop_spends_its_code_size_per_header_visit() {
+ let mut lines: Vec<I> = vec![
+ ("S_MOV_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 128)]),
+ ("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 255), ("LITERAL", 1)]),
+ ];
+ for _ in 0..30 {
+ lines.push(("S_ADD_U32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 255), ("LITERAL", 1)]));
+ }
+ lines.push(("S_CMP_LG_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 128)]));
+ lines.push(("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFC0)]));
+ lines.push(ENDPGM);
+ let (mut s, mut v) = state(32);
+ s[126] = 1;
+ v[0][1] = 0;
+ let (s, _) = run(&lines, false, &s, &v);
+ assert_eq!(s[0], LOOP_BUDGET / 2, "a 256-byte loop spends two units per visit");
+ }
+
+ const SPIN_THEN_LOOP: &[I] = &[
+ ("S_MOV_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 128)]),
+ ("S_MOV_B32", "ENC_SOP1", &[("SDST", 1), ("SSRC0", 128)]),
+ ("S_MOV_B32", "ENC_SOP1", &[("SDST", 3), ("SSRC0", 255), ("LITERAL", LOOP_BUDGET as u64)]),
+ ("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 129)]),
+ ("S_CMP_LT_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 3)]),
+ ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFFD)]),
+ ("S_ADD_U32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 1), ("SSRC1", 129)]),
+ ("S_CMP_LT_U32", "ENC_SOPC", &[("SSRC0", 1), ("SSRC1", 131)]),
+ ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFFD)]),
+ ENDPGM,
+ ];
+
+ #[test]
+ fn kernel_loop_after_a_full_spin_keeps_its_own_budget() {
+ let (mut s, v) = state(32);
+ s[126] = 1;
+ let (s, _) = run(SPIN_THEN_LOOP, false, &s, &v);
+ assert_eq!(s[0], LOOP_BUDGET);
+ assert_eq!(s[1], 3, "the second loop must run after the first spends one budget");
+ }
+
+ #[test]
+ fn kernel_loop_after_a_full_spin_runs_on_the_gpu() {
+ use super::super::{lower_dxil, runner};
+ if !gpu_waves() {
+ runner::skip_test("no GPU with 32-lane waves");
+ return;
+ }
+ let (dxil, _) = lower_dxil::kernel(&decode(SPIN_THEN_LOOP), false).unwrap();
+ let (mut s, _) = state(32);
+ s[126] = 1;
+ let mut input = runner::KernelState::default();
+ input.sgpr.copy_from_slice(&s);
+ let gpu = runner::run(&dxil, &input, &[]).unwrap();
+ assert_eq!(gpu.sgpr[0], LOOP_BUDGET);
+ assert_eq!(gpu.sgpr[1], 3);
+ }
+
#[test]
fn nested_loops_share_loop_budget_across_headers() {
// Outer infinite loop (header at inst 2, counted in s0) wrapping an inner loop that
diff --git a/crates/xcl/src/gpu/recomp/lower_dxil.rs b/crates/xcl/src/gpu/recomp/lower_dxil.rs
index 5865c59e..1af0ddb6 100644
--- a/crates/xcl/src/gpu/recomp/lower_dxil.rs
+++ b/crates/xcl/src/gpu/recomp/lower_dxil.rs
@@ -100,7 +100,7 @@ impl DxilIr {
fn ty(&mut self, var: Var) -> crate::gpu::dxil::module::Ty {
match var {
Var::M(_) | Var::Scc => self.lw.s.m.i1(),
- Var::S(_) | Var::V(_) | Var::Budget => self.lw.s.m.i32(),
+ Var::S(_) | Var::V(_) | Var::Budget | Var::BudgetOwner | Var::BudgetTotal => self.lw.s.m.i32(),
}
}
@@ -267,7 +267,7 @@ impl Ir for DxilIr {
Var::V(n) => VAR_VGPR + n as u32,
Var::Scc => VAR_SCC,
Var::M(_) => unreachable!("a lane-bit view is not a register"),
- Var::Budget => unreachable!("loop budget is not a register"),
+ Var::Budget | Var::BudgetOwner | Var::BudgetTotal => unreachable!("loop budget is not a register"),
};
for &(v, x) in inputs {
self.lw.wr(var(v), x);
diff --git a/crates/xcl/src/gpu/recomp/ssa.rs b/crates/xcl/src/gpu/recomp/ssa.rs
index 940b1553..be4c94d7 100644
--- a/crates/xcl/src/gpu/recomp/ssa.rs
+++ b/crates/xcl/src/gpu/recomp/ssa.rs
@@ -26,6 +26,8 @@ pub enum Var {
Scc,
/// remaining loop-header iterations for this lane (`i32`)
Budget,
+ BudgetOwner,
+ BudgetTotal,
}
/// What the construction needs from the module builder.
AI use: Codex (GPT-6.2 Sol) traced the current lowering paths, wrote and ran the local regressions and candidate, and drafted this reply. |
|
merge-check claim |
|
merge-check |
9a6d83c to
a68bf23
Compare
|
Rebased cleanly onto Summary of updates addressing review feedback:
|
|
merge-check claim Re-check of the head rebased onto main. |
|
merge-check |
|
NVIDIA and AMD full-suite run of head Tested
Not tested
AI use
|
|
No other PR adds a loop budget to the per-lane SSA lowering ( AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human. |
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 amd-dev f84a949: AMD driver crashes (amdxc64.dll+0xd2a411) compiling the title's native DXIL compute shader ca7beffb8d3a8f28 (RX 6750 XT) #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