Skip to content
Closed
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
11 changes: 11 additions & 0 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>) -> 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(),
Expand Down Expand Up @@ -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() {
Expand Down
45 changes: 45 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>)> = 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<u32> = 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);
}
}
}
Loading