From a527eb364b7c53b4155ebc5b207dffab54e3602b Mon Sep 17 00:00:00 2001 From: Youssef Rahmouni Date: Tue, 24 Nov 2026 09:59:29 +0100 Subject: [PATCH] Recompiler: keep far jumps indirect when entered after s_getpc_b64 (#863) --- crates/xcl/src/gpu/rdna2.rs | 22 +++++++++-- crates/xcl/src/gpu/recomp/tests.rs | 62 ++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/rdna2.rs b/crates/xcl/src/gpu/rdna2.rs index 9c60d3d382..58bed89eb5 100644 --- a/crates/xcl/src/gpu/rdna2.rs +++ b/crates/xcl/src/gpu/rdna2.rs @@ -5,7 +5,7 @@ //! encoding identification, literal constants, SDWA/DPP extension dwords and MIMG NSA. //! It is the first stage of the shader recompiler (console microcode → PC shaders). -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::fmt::Write as _; use std::sync::OnceLock; @@ -516,8 +516,13 @@ fn scalar_int(d: &Decoded, code: u64) -> Option { /// (`s_sub_u32` / `s_subb_u32` for a negative offset): a jump to the address after the /// `s_getpc_b64` plus the 64-bit constant hi:lo. Each such `s_setpc_b64` gets that target /// (`far_target`), so it is an unconditional branch like `s_branch` to everything that -/// follows the control flow. Any other `s_setpc_b64` stays an indirect jump. +/// follows the control flow. Any other `s_setpc_b64` – or one whose `add` / `addc` / +/// `setpc` is entered by a branch that bypasses `s_getpc_b64` – stays an indirect jump. pub fn resolve_far_jumps(insts: &mut [Decoded]) { + for d in insts.iter_mut() { + d.far_target = None; + } + let mut candidates = Vec::new(); for k in 3..insts.len() { let [get, lo, hi, set] = &insts[k - 3..=k] else { continue }; if get.inst.name != "S_GETPC_B64" || set.inst.name != "S_SETPC_B64" { @@ -547,7 +552,18 @@ pub fn resolve_far_jumps(insts: &mut [Decoded]) { let (Some(l), Some(h)) = (half(lo, r), half(hi, r + 1)) else { continue }; let offset = (h as u64) << 32 | l as u64; let base = get.pc + get.len as u64; - insts[k].far_target = Some(if sub { base.wrapping_sub(offset) } else { base.wrapping_add(offset) }); + let target = if sub { base.wrapping_sub(offset) } else { base.wrapping_add(offset) }; + candidates.push((k, target)); + } + if candidates.is_empty() { + return; + } + let mut targets: HashSet = insts.iter().filter_map(branch_target).collect(); + targets.extend(candidates.iter().map(|&(_, target)| target)); + for (k, target) in candidates { + if insts[k - 2..=k].iter().all(|d| !targets.contains(&d.pc)) { + insts[k].far_target = Some(target); + } } } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 4e032b68d1..f0f7fff43f 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -5657,6 +5657,68 @@ fn far_jumps_are_direct_branches() { } } +/// A far-jump sequence entered after `s_getpc_b64` by a direct branch or another far jump +/// depends on the incoming SGPR value and must stay an indirect `s_setpc_b64`. +#[test] +fn far_jumps_stay_indirect_when_entered_after_getpc() { + let decode_all = |code: &[u8]| { + let mut v = Vec::new(); + let mut off = 0; + while off + 4 <= code.len() { + let d = crate::gpu::rdna2::decode(&code[off..], off as u64).unwrap(); + off += d.len; + v.push(d); + } + crate::gpu::rdna2::resolve_far_jumps(&mut v); + v + }; + + // Direct branch at 0x00 into get (0x04), lo (0x08), hi (0x10), or set (0x14). + for sub in [false, true] { + let (lo, hi) = if sub { ("S_SUB_U32", "S_SUBB_U32") } else { ("S_ADD_U32", "S_ADDC_U32") }; + let offset = if sub { 4 } else { 0x14 }; + let expected = if sub { 0x04 } else { 0x1C }; + for (simm, enters_after_getpc) in [(0u64, false), (1, true), (3, true), (4, true)] { + let code = prog(&[ + ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", simm)], None), + ("S_GETPC_B64", "ENC_SOP1", &[("SDST", 4)], None), + (lo, "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 255)], Some(offset)), + (hi, "ENC_SOP2", &[("SDST", 5), ("SSRC0", 5), ("SSRC1", 128)], None), + ("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 4)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 135)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 137)], None), + ]); + let insts = decode_all(&code); + let want = if enters_after_getpc { None } else { Some(expected) }; + assert_eq!(insts[4].far_target, want, "sub={sub} simm={simm}"); + if enters_after_getpc { + let t = super::translate(&code, &Options::default()).unwrap(); + assert!(!t.complete(), "sub={sub} simm={simm} must keep s_setpc_b64 indirect"); + } + } + } + + // Far-to-far edge: first far jump (0x00, base 0x04) targets the second sequence's + // get (0x14), lo (0x18), hi (0x20), or set (0x24). + for (first_off, enters_after_getpc) in [(0x10u32, false), (0x14, true), (0x1C, true), (0x20, true)] { + let code = prog(&[ + ("S_GETPC_B64", "ENC_SOP1", &[("SDST", 4)], None), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 255)], Some(first_off)), + ("S_ADDC_U32", "ENC_SOP2", &[("SDST", 5), ("SSRC0", 5), ("SSRC1", 128)], None), + ("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 4)], None), + ("S_GETPC_B64", "ENC_SOP1", &[("SDST", 6)], None), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 6), ("SSRC0", 6), ("SSRC1", 255)], Some(0x10)), + ("S_ADDC_U32", "ENC_SOP2", &[("SDST", 7), ("SSRC0", 7), ("SSRC1", 128)], None), + ("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 6)], None), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 137)], None), + ]); + let insts = decode_all(&code); + assert_eq!(insts[3].far_target, Some(0x04 + first_off as u64)); + let want = if enters_after_getpc { None } else { Some(0x28) }; + assert_eq!(insts[7].far_target, want, "first_off={first_off:#x}"); + } +} + /// Mesh mode for a GPU with 32-lane waves only: the console's two wave64s per group as four /// hardware wave32s exchanging masks and lane reads (counter-paired: two console waves), /// programs with cross-half operations included; native wave64 and wave32 programs unchanged.