From 1b4a1402fc299a65559170464c1e1bd618face5d Mon Sep 17 00:00:00 2001 From: dadnotdaddy <328468659+dadnotdaddy@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:01:32 -0600 Subject: [PATCH] Recompiler: the first structured form stops at a taken jump to a pending target and runs an else it jumps into (#701) A jump whose target already has a pending skip flag left the open if (!(xcl_skip_...)) guard in place, so the items after it still ran: any raised flag now closes the guard, and the next item re-tests it. A jump from the if branch to the start of the else branch is not raised from that level, so else_ok accepted the if / else and the else never ran: such an if is now a plain if, the else running under the skip flag of the jump over it. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/mod.rs | 11 ++++++++ crates/xcl/src/gpu/recomp/tests.rs | 45 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 34ac919bb..1baee03d4 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -741,6 +741,11 @@ fn rotated_loop(c: &Ctx, start: u64, end: u64, u: u64, lo: u64, hi: u64) -> Opti /// `if (!c) { [end, t) } else { [t, u) }` holds: both branches structured, the if branch does /// not jump into the else branch. fn else_ok(c: &Ctx, end: u64, t: u64, u: u64, nest: usize, cont: Option) -> bool { + // a jump to t from inside the if branch is not raised (it ends that level: a plain if + // there), but as an if / else it would skip the else branch + if c.edges.iter().any(|&(src, dst)| dst == t && src >= end && src < t) { + return false; + } let none = BTreeSet::new(); match structurable_to(c, end, t, Some(u), nest + 1, cont, &none) { Some(r) if !r.iter().any(|&x| x >= t && x < u) => structurable_to(c, t, u, None, nest + 1, cont, &none).is_some(), @@ -1002,6 +1007,12 @@ fn emit_structured( } } }; + // a flag set here is tested again before the next item, even one already pending + // (the open guard was tested before it was set) + if !raised.is_empty() && guard.take().is_some() { + body.push_str(&format!("{pad}}} +")); + } 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 a4f50929b..d083dd584 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7250,3 +7250,48 @@ fn fspx_containers_translate_and_compile() { println!("FSPX: {ok} / {n} translated and compiled"); assert_eq!(ok, n); } + +/// #701: the first structured form (`emit_structured`, also inside the `pc` chain's guards). +/// (1) A loop with two exits to the same block: the second exit's flag is already pending, +/// so the open `if (!(xcl_skip_…))` must be re-tested after it. (2) A jump from the if branch +/// to the start of the else branch: the else branch runs. s11 = 2: the second test is taken. +#[test] +fn first_structured_form_follows_jumps_to_pending_targets_and_into_the_else() { + if !gpu_ready() { + return; + } + // s[r] += 1; scc = bit s0 of s[r] + let a = |r: u64| ("S_ADD_U32", "ENC_SOP2", vec![("SDST", r), ("SSRC0", r), ("SSRC1", 129)]); + let b = |r: u64| ("S_BITCMP1_B32", "ENC_SOPC", vec![("SSRC0", r), ("SSRC1", 0)]); + let br = |n: &'static str, k: u64| (n, "ENC_SOPP", vec![("SIMM16", k)]); + let two_breaks = vec![ + a(0), b(10), br("S_CBRANCH_SCC1", 9), // → out + a(1), b(11), br("S_CBRANCH_SCC1", 6), // → out + a(2), b(12), br("S_CBRANCH_SCC1", 1), // → latch + a(4), + ("S_CMP_LT_U32", "ENC_SOPC", vec![("SSRC0", 0), ("SSRC1", 131)]), // latch + br("S_CBRANCH_SCC1", 0xFFF4), // → loop + a(3), // out + ]; + let into_else = vec![ + a(0), b(10), br("S_CBRANCH_SCC1", 5), // → else + a(1), b(11), br("S_CBRANCH_SCC1", 2), // → else + a(2), br("S_BRANCH", 1), // → join + a(3), // else + a(4), // join + ]; + for (name, list, want) in [("two breaks", two_breaks, [1, 1, 0, 1, 0]), ("jump into else", into_else, [1, 1, 0, 1, 1])] { + let l: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(n, e, f)| (*n, *e, &f[..], None)).collect(); + let code = prog(&l); + for structured in [true, false] { + let t = translate(&code, &Options { wave64: false, structured, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap(); + let mem: Vec = code.chunks(4).map(|c| u32::from_le_bytes(c.try_into().unwrap())).collect(); + let mut st = KernelState::default(); + st.sgpr[11] = 2; + let r = runner::run(&dxil, &st, &mem).unwrap(); + assert_eq!(r.sgpr[..5], want, "{name}, structured {structured}\n{}", t.hlsl); + } + } +}