diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index 54df58357a..c462be09f2 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -19,6 +19,7 @@ use std::collections::HashSet; use super::lane_masks::{self, Edge, Summary}; use super::ssa::{Sink, Ssa, Var}; use super::{KERNEL_SCC_SLOT, KERNEL_SGPRS, KERNEL_VGPRS}; +use crate::gpu::dxil::lower::LOOP_BUDGET; use crate::gpu::rdna2::{self, Decoded, Variant}; mod wave; @@ -1261,22 +1262,46 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s // then for every coalesced guarded run (only the lanes with their EXEC bit enter): the run's // block and the one the rest of its block continues in. `tail[b]`: the IR block the // program's block b ends in. - let (exit, sstore, vstore, skip) = (n + 1, n + 2, n + 3, n + 4); + let mut rpo_pos = vec![usize::MAX; n]; + for (pos, &b) in sum.order.iter().enumerate() { + rpo_pos[b] = pos; + } + let mut loop_header = vec![false; n]; + for &b in &sum.order { + for &(s, _) in &sum.blocks[b].succ { + if rpo_pos[s] <= rpo_pos[b] { + loop_header[s] = true; + } + } + } let guarded: HashSet = sum.guarded.iter().copied().collect(); + let mut budget_cont: Vec> = vec![None; n]; let mut splits: Vec, usize, usize)>> = vec![Vec::new(); n]; // (run, body, cont) - let mut count = n + 5; + let mut count = n + 1; for (b, block) in sum.blocks.iter().enumerate() { + if loop_header[b] { + budget_cont[b] = Some(count); + count += 1; + } for run in coalesced_runs(insts, &block.insts, &guarded) { splits[b].push((run, count, count + 1)); count += 2; } } - let tail: Vec = (0..n).map(|b| splits[b].last().map_or(blk(b), |s| s.2)).collect(); + let (exit, sstore, vstore, skip) = (count, count + 1, count + 2, count + 3); + count += 4; + let body_start = |b: usize| budget_cont[b].unwrap_or(blk(b)); + let tail: Vec = (0..n).map(|b| splits[b].last().map_or(body_start(b), |s| s.2)).collect(); let mut partial = vec![false; count]; let mut def_owner: Vec = (0..count).collect(); let mut use_owner: Vec = (0..count).collect(); for (b, p) in partial_blocks(&sum).into_iter().enumerate() { partial[blk(b)] = p; + if let Some(bc) = budget_cont[b] { + partial[bc] = p; + def_owner[bc] = blk(b); + use_owner[bc] = blk(b); + } for &(_, s, c) in &splits[b] { partial[s] = true; partial[c] = p; @@ -1297,7 +1322,10 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s preds[blk(s)].push(tail[b]); } } - let mut prev = blk(b); + if let Some(bc) = budget_cont[b] { + preds[bc] = vec![blk(b)]; + } + let mut prev = body_start(b); for &(_, s, c) in &splits[b] { preds[s] = vec![prev]; preds[c] = vec![prev, s]; @@ -1314,7 +1342,12 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s } else if matches!(stage, Stage::Compute(_)) { preds[skip] = vec![0]; } - preds[exit] = kernel.then_some(skip).into_iter().chain(ends.iter().map(|&b| tail[b])).collect(); + preds[exit] = kernel + .then_some(skip) + .into_iter() + .chain(ends.iter().map(|&b| tail[b])) + .chain(sum.order.iter().copied().filter(|&b| loop_header[b]).map(blk)) + .collect(); for i in 0..count { let id = ir.new_block(); debug_assert_eq!(id, i); @@ -1366,6 +1399,8 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s // prologue: the SGPRs from `io` (kernel: the whole register file; compute: the user data) l.ir.at(0); l.ssa.seal(&mut l.ir, 0); + let budget = l.ir.int(LOOP_BUDGET); + l.ssa.write(0, Var::Budget, budget); let lane = l.lane(); for &r in &sregs { let a = l.ir.int(r as u32); @@ -1514,6 +1549,21 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s let last = block.insts.end - 1; 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); + 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); + l.ssa.write(bc, Var::Budget, next); + cur = bc; + } let mut split = splits[b].iter().peekable(); let mut i = block.insts.start; while i < body_end { @@ -1605,6 +1655,16 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s return l.finish(stale); }; // kernel exit: every lane is here; masks the runner reads as numbers are the wave's ballots + let exit_stale = |l: &mut Lower, var: Var, exit_v: I::V| -> bool { + if ends.is_empty() { + l.stale(exit, exit_v) + } else { + ends.iter().any(|&b| { + let v = l.read(tail[b], var); + l.stale(tail[b], v) + }) + } + }; let mut sval: Vec<(u8, I::V)> = Vec::new(); let mut i = 0; while i < sregs.len() { @@ -1613,7 +1673,7 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s let s = l.read(exit, Var::S(r)); if l.ssa.resolve_value(s) != l.num_marker && !l.reaches(s, l.num_marker) { l.num_uses.push(s); - if l.stale(exit, s) { + if exit_stale(&mut l, Var::S(r), s) { stale.push(r); } sval.push((r, s)); @@ -1627,7 +1687,7 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s } // read directly: a stale bit here is reported, not refused (only the ABI's store sees it) let m = l.read(exit, Var::M(r)); - if r != EXEC && l.stale(exit, m) { + if r != EXEC && exit_stale(&mut l, Var::M(r), m) { stale.push(r); if l.wave64 { stale.push(r + 1); // the upper half comes from the same mask @@ -1644,7 +1704,7 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s } } let scc = l.read(exit, Var::Scc); - if l.stale(exit, scc) { + if exit_stale(&mut l, Var::Scc, scc) { stale.push(KERNEL_SCC_SLOT as u8); } let vval: Vec<(u16, I::V)> = vregs.iter().map(|&v| (v, l.read(exit, Var::V(v)))).collect(); @@ -1807,7 +1867,7 @@ impl Lower<'_, I> { let s = self.ir.icmp(Cmp::Ne, v, z); self.write_scc(b, s); } - Var::S(_) | Var::M(_) => {} + Var::S(_) | Var::M(_) | Var::Budget => {} } } if name.starts_with("V_") && !sw.is_empty() { @@ -4579,4 +4639,57 @@ pub(crate) mod tests { } check_mem(true); } + + #[test] + fn infinite_loop_exits_at_loop_budget() { + let lines: &[I] = &[ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 128)]), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 129)]), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 129), ("VSRC1", 1)]), + ("S_BRANCH", "ENC_SOPP", &[("SIMM16", 0xFFFD)]), + ]; + let (mut s, mut v) = state(32); + s[0] = 0; + s[126] = 1; // lane 0 active so the CPU interpreter finishes well within its step limit + v[0][1] = 0; + let (s, v) = run(lines, false, &s, &v); + assert_eq!(s[0], LOOP_BUDGET); + assert_eq!(v[0][1], LOOP_BUDGET); + } + + #[test] + fn nested_loops_share_loop_budget_across_headers() { + // Outer infinite loop (header at inst 2, counted in s0) wrapping an inner loop that + // runs K = 3 times per outer iteration (header at inst 4, counted in s1 per outer + // trip and accumulated in s2 / v1 across all outer trips). Both headers decrement + // the same shared Var::Budget initialized at entry, so total header trips (s0 + s2) + // equal LOOP_BUDGET rather than LOOP_BUDGET * 3 or hanging on reinitialization. + let lines: &[I] = &[ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 128)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 128)]), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 129)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 1), ("SSRC0", 128)]), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 1), ("SSRC1", 129)]), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 2), ("SSRC0", 2), ("SSRC1", 129)]), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 129), ("VSRC1", 1)]), + ("S_CMP_LT_U32", "ENC_SOPC", &[("SSRC0", 1), ("SSRC1", 131)]), + ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFFB)]), + ("S_BRANCH", "ENC_SOPP", &[("SIMM16", 0xFFF8)]), + ]; + let (mut s, mut v) = state(32); + s[0] = 0; + s[1] = 0; + s[2] = 0; + s[126] = 1; // lane 0 active so the CPU interpreter finishes well within its step limit + v[0][1] = 0; + let (s, v) = run(lines, false, &s, &v); + let full_outer = LOOP_BUDGET / 4; + let rem = LOOP_BUDGET % 4; + let expected_outer = full_outer + u32::from(rem > 0); + let expected_inner = full_outer * 3 + rem.saturating_sub(1); + assert_eq!(s[0], expected_outer); + assert_eq!(s[2], expected_inner); + assert_eq!(s[0] + s[2], LOOP_BUDGET); + assert_eq!(v[0][1], expected_inner); + } } diff --git a/crates/xcl/src/gpu/recomp/lower_dxil.rs b/crates/xcl/src/gpu/recomp/lower_dxil.rs index cec9257c8a..5865c59e1f 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(_) => self.lw.s.m.i32(), + Var::S(_) | Var::V(_) | Var::Budget => self.lw.s.m.i32(), } } @@ -267,6 +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"), }; 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 0a7fd4698d..940b1553d9 100644 --- a/crates/xcl/src/gpu/recomp/ssa.rs +++ b/crates/xcl/src/gpu/recomp/ssa.rs @@ -24,6 +24,8 @@ pub enum Var { M(u8), V(u16), Scc, + /// remaining loop-header iterations for this lane (`i32`) + Budget, } /// What the construction needs from the module builder.