diff --git a/README.md b/README.md index 2a957e95e..7354ea26b 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 34ac919bb..77e091dd8 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -530,7 +530,7 @@ struct Ctx<'a> { starts: &'a BTreeSet, 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>>>, @@ -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) -> 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 @@ -845,10 +848,6 @@ fn structurable_uncached(c: &Ctx, lo: u64, hi: u64, exit: Option, 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 @@ -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() { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index a4f50929b..a87a84151 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -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 { + 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 { + 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(&[