Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 122 additions & 9 deletions crates/xcl/src/gpu/recomp/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1261,22 +1262,46 @@ fn lower_with<I: Ir>(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<u64> = sum.guarded.iter().copied().collect();
let mut budget_cont: Vec<Option<usize>> = vec![None; n];
let mut splits: Vec<Vec<(std::ops::Range<usize>, 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<usize> = (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<usize> = (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<usize> = (0..count).collect();
let mut use_owner: Vec<usize> = (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;
Expand All @@ -1297,7 +1322,10 @@ fn lower_with<I: Ir>(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];
Expand All @@ -1314,7 +1342,12 @@ fn lower_with<I: Ir>(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);
Expand Down Expand Up @@ -1366,6 +1399,8 @@ fn lower_with<I: Ir>(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);
Expand Down Expand Up @@ -1514,6 +1549,21 @@ fn lower_with<I: Ir>(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 {
Expand Down Expand Up @@ -1605,6 +1655,16 @@ fn lower_with<I: Ir>(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<I>, 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() {
Expand All @@ -1613,7 +1673,7 @@ fn lower_with<I: Ir>(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));
Expand All @@ -1627,7 +1687,7 @@ fn lower_with<I: Ir>(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
Expand All @@ -1644,7 +1704,7 @@ fn lower_with<I: Ir>(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();
Expand Down Expand Up @@ -1807,7 +1867,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::S(_) | Var::M(_) | Var::Budget => {}
}
}
if name.starts_with("V_") && !sw.is_empty() {
Expand Down Expand Up @@ -4579,4 +4639,57 @@ pub(crate) mod tests {
}
check_mem(true);
}

#[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.

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);
}
}
3 changes: 2 additions & 1 deletion crates/xcl/src/gpu/recomp/lower_dxil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/gpu/recomp/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading