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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ next steps:
| **Ray tracing** | ✅ | BLAS / TLAS built on the GPU by the game's own XboxBvhBuilder; RDNA2 BVH node format; BVH intersection instructions emulated in shaders | GPU traversal = brute force reference; the game's builder output checked on the GPU |
| **Upscaling** | ✅ | the game's AMD FSR 4 console DLL (`amd_fidelityfx_upscaler_xbox.dll`): context, the 29 pipelines in the console driver's own container (FSPX) translated, a frame's 20 passes recorded and run on the GPU | end-to-end with the real DLL: a 1280×720 → 1920×1080 frame on an RX 9070, the input's gradient in the output |

The shader recompiler preserves branches when several loop exits share a destination or a
branch jumps directly into an else block ([#701](https://github.com/ivall/xcl/issues/701)).

### Media and tools

| Feature | State | What it does | Verified by |
Expand Down
22 changes: 13 additions & 9 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ struct Ctx<'a> {
starts: &'a BTreeSet<u64>,
leaders: &'a [u64],
loops: &'a [(u64, u64)],
/// every branch: (address of the branch, target)
/// every branch in instruction order: (address of the branch, target)
edges: &'a [(u64, u64)],
/// `structurable_to` results: (lo, hi, exit) → targets leaving the range
memo: std::cell::RefCell<std::collections::HashMap<MemoKey, Option<BTreeSet<u64>>>>,
Expand Down Expand Up @@ -741,11 +741,14 @@ 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 {
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(),
_ => false,
// A nested conditional to t can be emitted as a plain if, without raising a flag.
let first = c.edges.partition_point(|&(src, _)| src < end);
if c.edges[first..].iter().take_while(|&&(src, _)| src < t).any(|&(_, dst)| (t..u).contains(&dst)) {
return false;
}
let none = BTreeSet::new();
structurable_to(c, end, t, Some(u), nest + 1, cont, &none).is_some()
&& structurable_to(c, t, u, None, nest + 1, cont, &none).is_some()
}

/// Can [lo, hi) be emitted as structured code (no `pc` guards)? Linear blocks, forward
Expand Down Expand Up @@ -845,10 +848,6 @@ fn structurable_uncached(c: &Ctx, lo: u64, hi: u64, exit: Option<u64>, nest: usi
Item::If { end, t, u, .. } => {
let mut r = structurable_to(c, end, t, u, nest + 1, cont, &none)?;
if let Some(u) = u {
if r.contains(&t) {
why(t, &format!("branch from the if to the start of the else {t:#x}"));
return None;
}
r.extend(structurable_to(c, t, u, None, nest + 1, cont, &none)?);
}
r
Expand Down Expand Up @@ -1002,6 +1001,11 @@ fn emit_structured(
}
}
};
// The open guard tested its flags before this item could set one of them again.
if guard.as_ref().is_some_and(|g| !g.is_disjoint(&raised)) {
body.push_str(&format!("{pad}}}\n"));
guard = None;
}
pending.extend(raised);
}
if guard.is_some() {
Expand Down
87 changes: 87 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,93 @@ fn hlsl_is_generated_for_straight_line_and_branches() {
let _ = Mode::Kernel;
}

// Synthetic programs from #701: each add records a visit in its destination SGPR.
fn two_breaks_program() -> Vec<u8> {
prog(&[
("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 129)], None), // 00: loop
("S_BITCMP1_B32", "ENC_SOPC", &[("SSRC0", 10), ("SSRC1", 0)], None),
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 9)], None), // 08: -> out
("S_ADD_U32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 1), ("SSRC1", 129)], None),
("S_BITCMP1_B32", "ENC_SOPC", &[("SSRC0", 11), ("SSRC1", 0)], None),
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 6)], None), // 14: -> out
("S_ADD_U32", "ENC_SOP2", &[("SDST", 2), ("SSRC0", 2), ("SSRC1", 129)], None),
("S_BITCMP1_B32", "ENC_SOPC", &[("SSRC0", 12), ("SSRC1", 0)], None),
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 1)], None), // 20: -> latch
("S_ADD_U32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 129)], None),
("S_CMP_LT_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 131)], None), // 28: latch
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFF4)], None), // 2c: -> loop
("S_ADD_U32", "ENC_SOP2", &[("SDST", 3), ("SSRC0", 3), ("SSRC1", 129)], None), // 30: out
])
}

fn jump_into_else_program() -> Vec<u8> {
prog(&[
("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 129)], None),
("S_BITCMP1_B32", "ENC_SOPC", &[("SSRC0", 10), ("SSRC1", 0)], None),
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 5)], None), // 08: -> else
("S_ADD_U32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 1), ("SSRC1", 129)], None),
("S_BITCMP1_B32", "ENC_SOPC", &[("SSRC0", 11), ("SSRC1", 0)], None),
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 2)], None), // 14: -> else
("S_ADD_U32", "ENC_SOP2", &[("SDST", 2), ("SSRC0", 2), ("SSRC1", 129)], None),
("S_BRANCH", "ENC_SOPP", &[("SIMM16", 1)], None), // 1c: -> join
("S_ADD_U32", "ENC_SOP2", &[("SDST", 3), ("SSRC0", 3), ("SSRC1", 129)], None), // 20: else
("S_ADD_U32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 129)], None), // 24: join
])
}

#[test]
fn structured_repeated_loop_exit_rechecks_guard() {
let code = two_breaks_program();
for structured in [true, false] {
let t = translate(&code, &Options { structured, ..Default::default() }).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
let (_, after_exit) = t.hlsl.split_once("// 0014:").unwrap();
let (before_next, _) = after_exit.split_once("// 0018:").unwrap();
assert!(before_next.split_once("xcl_skip_30 = true;").is_some_and(|(_, tail)| tail.contains("if (!(xcl_skip_30)) {")),
"structured={structured}: the second exit must guard the next block\n{}", t.hlsl);
}
}

#[test]
fn structured_jump_into_else_preserves_target() {
let code = jump_into_else_program();
for structured in [true, false] {
let t = translate(&code, &Options { structured, ..Default::default() }).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
// Only the explicit jump over the else block should guard it, not HLSL's `else`.
let (_, after_jump) = t.hlsl.split_once("// 001c:").unwrap();
let (before_else, _) = after_jump.split_once("// 0020:").unwrap();
assert!(before_else.split_once("xcl_skip_24 = true;").is_some_and(|(_, tail)| tail.contains("if (!(xcl_skip_24)) {")),
"structured={structured}: the else block must run unless the jump over it was taken\n{}", t.hlsl);
}
}

#[test]
fn gpu_structured_branch_targets_match_rdna2() {
if !gpu_ready() {
return;
}
// Take the second branch (the two regressions), the first branch, or neither.
let inputs = [(0, 2), (2, 0), (0, 0)];
for (name, code, expected) in [
("two breaks", two_breaks_program(), [[1, 1, 0, 1, 0], [1, 0, 0, 1, 0], [3, 3, 3, 1, 3]]),
("jump into else", jump_into_else_program(), [[1, 1, 0, 1, 1], [1, 0, 0, 1, 1], [1, 1, 1, 0, 1]]),
] {
for structured in [true, false] {
let t = translate(&code, &Options { structured, ..runner::options(Mode::Kernel) }).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));
for ((s10, s11), want) in inputs.into_iter().zip(expected) {
let mut st = KernelState::default();
st.sgpr[10] = s10;
st.sgpr[11] = s11;
let r = runner::run(&dxil, &st, &[]).unwrap();
assert_eq!(&r.sgpr[..5], &want, "{name}, structured={structured}, s10={s10}, s11={s11}");
}
}
}
}

#[test]
fn scalar_bfe_64_handles_unsigned_signed_and_zero_width() {
let code = prog(&[
Expand Down
Loading