diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 34ac919bb..98bcc4874 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -692,7 +692,9 @@ fn item_at(c: &Ctx, i: usize, lo: u64, hi: u64, exit: Option, nest: usize, // a jump into the loop that starts right after: the rotated form (the jump itself is // the loop entry, emitted as nothing) Term::Jump(u) if rotated_loop(c, start, end, u, lo, hi).is_some() => Item::Block { start, end, raise: None }, - Term::Cond(t) if nest < MAX_NEST && t >= end && t <= hi && (t == hi || c.leaders.binary_search(&t).is_ok()) && !inside(start, t) => { + // (in the if branch of an if / else, `hi` is the start of the else branch: a jump to it is no + // skip to the end of the branch – the branch ends with the jump over the else – but a jump into the else) + Term::Cond(t) if nest < MAX_NEST && t >= end && t <= hi && (t == hi || c.leaders.binary_search(&t).is_ok()) && !inside(start, t) && !(t == hi && exit.is_some()) => { // if / else only when both branches are structured on their own, the if branch // does not jump into the else branch and no earlier jump lands in it; otherwise a // plain if (the else branch then runs under the skip flag of the jump over it) @@ -1002,6 +1004,14 @@ fn emit_structured( } } }; + // a jump to a target that is already pending leaves the pending set (and so the open + // guard) as it is, but what follows must not run once the jump is taken: close the + // guard, the next item opens one that tests the flags again + if guard.is_some() && raised.iter().any(|t| pending.contains(t)) { + body.push_str(&format!("{pad}}} +")); + guard = None; + } pending.extend(raised); } if guard.is_some() { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 596a74198..28f9b999d 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7187,3 +7187,89 @@ fn fspx_containers_translate_and_compile() { println!("FSPX: {ok} / {n} translated and compiled"); assert_eq!(ok, n); } + +/// Run a scalar-only program with the given options (wave64 where the GPU has it) and return +/// the scalar registers 0..16 after it. +fn run_scalar_program(code: &[u8], structured: bool, inputs: &[(usize, u32)]) -> Vec { + let wave64 = runner::wave64_supported() == Ok(true); + let t = translate(code, &Options { wave64, structured, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)); + let mem: Vec = code.chunks(4).map(|c| u32::from_le_bytes(c.try_into().unwrap())).collect(); + let mut st = KernelState::default(); + for &(r, v) in inputs { + st.sgpr[r] = v; + } + runner::run(&dxil, &st, &mem).unwrap().sgpr[..16].to_vec() +} + +/// `if (s10 != 0) { s0 = 1; if (s11 != 0) { s1 = 1; goto end } else-start }`: a conditional jump +/// from the if branch to the first block of the else branch must run the else branch. +#[test] +fn gpu_a_jump_from_the_if_branch_to_the_else_branch_runs_it() { + if !gpu_ready() { + return; + } + let one = |r: u64| ("S_MOV_B32", "ENC_SOP1", vec![("SDST", r), ("SSRC0", 129)], None::); + let list: Vec<(&str, &str, Vec<(&str, u64)>, Option)> = vec![ + ("S_CMP_EQ_U32", "ENC_SOPC", vec![("SSRC0", 10), ("SSRC1", 128)], None), // 0 + ("S_CBRANCH_SCC1", "ENC_SOPP", vec![("SIMM16", 5)], None), // 4: → else (28) + one(0), // 8: if branch + ("S_CMP_EQ_U32", "ENC_SOPC", vec![("SSRC0", 11), ("SSRC1", 128)], None), // 12 + ("S_CBRANCH_SCC1", "ENC_SOPP", vec![("SIMM16", 2)], None), // 16: → else (28) + one(1), // 20 + ("S_BRANCH", "ENC_SOPP", vec![("SIMM16", 1)], None), // 24: → end (32) + one(2), // 28: else + one(3), // 32: end + ]; + let borrowed: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(n, e, f, l)| (*n, *e, f.as_slice(), *l)).collect(); + let code = prog(&borrowed); + // (s10, s11) → s0..s3 + let cases = [((0, 0), [0, 0, 1, 1]), ((0, 1), [0, 0, 1, 1]), ((1, 0), [1, 0, 1, 1]), ((1, 1), [1, 1, 0, 1])]; + for structured in [true, false] { + for ((a, b), want) in cases { + let s = run_scalar_program(&code, structured, &[(10, a), (11, b)]); + assert_eq!(&s[..4], &want, "structured: {structured}, s10 = {a}, s11 = {b}"); + } + } +} + +/// A loop with two breaks to the same block, and an `if` before the latch: once the second break +/// is taken, the code after it (the `if`) must not run. The first form opened one guard for the +/// flag of the first break and left it open when the second one raised the same flag. +#[test] +fn gpu_a_second_jump_to_a_pending_target_skips_what_follows() { + if !gpu_ready() { + return; + } + type Op = (&'static str, &'static str, Vec<(&'static str, u64)>, Option); + let list: Vec = vec![ + ("S_CMP_EQ_U32", "ENC_SOPC", vec![("SSRC0", 10), ("SSRC1", 128)], None), // 0: loop head + ("S_CBRANCH_SCC1", "ENC_SOPP", vec![("SIMM16", 8)], None), // 4: break → 40 + ("S_CMP_EQ_U32", "ENC_SOPC", vec![("SSRC0", 11), ("SSRC1", 128)], None), // 8 + ("S_CBRANCH_SCC1", "ENC_SOPP", vec![("SIMM16", 6)], None), // 12: break → 40 + ("S_CMP_EQ_U32", "ENC_SOPC", vec![("SSRC0", 12), ("SSRC1", 128)], None), // 16 + ("S_CBRANCH_SCC1", "ENC_SOPP", vec![("SIMM16", 1)], None), // 20: → 28 + ("S_MOV_B32", "ENC_SOP1", vec![("SDST", 0), ("SSRC0", 129)], None), // 24: s0 = 1 + ("S_ADD_U32", "ENC_SOP2", vec![("SDST", 13), ("SSRC0", 13), ("SSRC1", 129)], None), // 28: latch + ("S_CMP_LG_U32", "ENC_SOPC", vec![("SSRC0", 13), ("SSRC1", 130)], None), // 32 + ("S_CBRANCH_SCC1", "ENC_SOPP", vec![("SIMM16", 0xFFF6)], None), // 36: → 0 + ("S_MOV_B32", "ENC_SOP1", vec![("SDST", 1), ("SSRC0", 129)], None), // 40: after the loop + ]; + let borrowed: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(n, e, f, l)| (*n, *e, f.as_slice(), *l)).collect(); + let code = prog(&borrowed); + // (s10, s11, s12) → (s0, s1, s13): s10 / s11 = 0 break, s12 = 0 skips s0 = 1; s1 = 1 is the code after the loop, reached either way + let cases = [ + ((1, 1, 1), (1, 1, 2)), // no break: two iterations, s0 = 1 + ((1, 0, 1), (0, 1, 0)), // the second break, first iteration: nothing of the rest runs + ((0, 1, 1), (0, 1, 0)), // the first break + ((0, 0, 1), (0, 1, 0)), // both + ((1, 1, 0), (0, 1, 2)), // no break, the if skipped + ]; + for structured in [true, false] { + for ((a, b, c), (s0, s1, s13)) in cases { + let s = run_scalar_program(&code, structured, &[(10, a), (11, b), (12, c)]); + assert_eq!((s[0], s[1], s[13]), (s0, s1, s13), "structured: {structured}, s10 = {a}, s11 = {b}, s12 = {c}"); + } + } +}