diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 34ac919bb..c1c2fdec5 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) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 596a74198..487d9e8be 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7187,3 +7187,49 @@ 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}"); + } + } +}