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
32 changes: 31 additions & 1 deletion crates/xcl/src/gpu/rdna2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,28 @@ fn scalar_int(d: &Decoded, code: u64) -> Option<u32> {
/// (`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.
pub fn resolve_far_jumps(insts: &mut [Decoded]) {
use std::collections::HashSet;

for d in insts.iter_mut() {
d.far_target = None;
}

// Collect every direct branch target before annotating far jumps, plus prospective far-jump
// targets so a far jump entering another sequence after `s_getpc_b64` also keeps it indirect.
let mut targets: HashSet<u64> = insts.iter().filter_map(branch_target).collect();
let mut candidates: Vec<(usize, u64)> = 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" {
continue;
}
if get.pc + get.len as u64 != lo.pc
|| lo.pc + lo.len as u64 != hi.pc
|| hi.pc + hi.len as u64 != set.pc
{
continue;
}
let sub = match (lo.inst.name, hi.inst.name) {
("S_ADD_U32", "S_ADDC_U32") => false,
("S_SUB_U32", "S_SUBB_U32") => true,
Expand All @@ -547,7 +564,20 @@ 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) };
targets.insert(target);
candidates.push((k, target));
}

for (k, target) in candidates {
// Any branch entering `lo`, `hi`, or `set` bypasses `s_getpc_b64`, so the jump depends on
// incoming SGPR state and must stay indirect.
if !targets.contains(&insts[k - 2].pc)
&& !targets.contains(&insts[k - 1].pc)
&& !targets.contains(&insts[k].pc)
{
insts[k].far_target = Some(target);
}
}
}

Expand Down
69 changes: 69 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5657,6 +5657,75 @@ fn far_jumps_are_direct_branches() {
}
}

/// A branch (direct or resolved far jump) entering a far-jump sequence after `s_getpc_b64`
/// bypasses its PC initialization, so the `s_setpc_b64` must stay indirect (`far_target == None`).
/// Branching to the `s_getpc_b64` entry itself still runs the full sequence and stays direct.
#[test]
fn far_jumps_stay_indirect_when_branched_into() {
let decode_and_resolve = |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
};

// Layout: [0] S_CBRANCH_SCC1 simm16 (4 B, pc=0)
// [1] S_GETPC_B64 s[4:5] (4 B, pc=4)
// [2] S_ADD/SUB_U32 s4,s4,lit (8 B, pc=8)
// [3] S_ADDC/SUBB_U32 s5,s5,0 (4 B, pc=16)
// [4] S_SETPC_B64 s[4:5] (4 B, pc=20)
// [5] S_NOP 0 (4 B, pc=24)
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 { 4u32 } else { 16u32 };
let expected_target = if sub { 4u64 } else { 24u64 };

// Branch target PC = 4 + simm16 * 4:
// simm16 = 0 -> pc=4 (S_GETPC_B64, valid entry -> stays direct)
// simm16 = 1 -> pc=8 (lo add/sub, interior -> must stay indirect)
// simm16 = 3 -> pc=16 (hi addc/subb, interior -> must stay indirect)
// simm16 = 4 -> pc=20 (S_SETPC_B64, interior -> must stay indirect)
for (simm16, expect_direct) in [(0u64, true), (1, false), (3, false), (4, false)] {
let code = prog(&[
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", simm16)], 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),
("S_NOP", "ENC_SOPP", &[("SIMM16", 0)], None),
]);
let insts = decode_and_resolve(&code);
let want = expect_direct.then_some(expected_target);
assert_eq!(insts[4].far_target, want, "sub={sub} simm16={simm16}");
}
}

// Far-to-far edge: sequence A (pc=0..20) resolves to pc=24, which is the `lo` instruction
// inside sequence B (pc=20..40). Sequence A remains direct; sequence B must stay indirect.
// A: [0] GETPC (pc=0, base=4), [1] ADD +20 (pc=4, 8B), [2] ADDC 0 (pc=12), [3] SETPC (pc=16) -> target 24
// B: [4] GETPC (pc=20, base=24), [5] ADD +16 (pc=24, 8B), [6] ADDC 0 (pc=32), [7] SETPC (pc=36) -> target 40
// [8] S_NOP (pc=40)
let far_to_far = prog(&[
("S_GETPC_B64", "ENC_SOP1", &[("SDST", 4)], None),
("S_ADD_U32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 255)], Some(20)),
("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(16)),
("S_ADDC_U32", "ENC_SOP2", &[("SDST", 7), ("SSRC0", 7), ("SSRC1", 128)], None),
("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 6)], None),
("S_NOP", "ENC_SOPP", &[("SIMM16", 0)], None),
]);
let insts = decode_and_resolve(&far_to_far);
assert_eq!(insts[3].far_target, Some(24), "first far jump A stays direct");
assert_eq!(insts[7].far_target, None, "second far jump B targeted inside its interior stays indirect");
}

/// 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.
Expand Down
Loading