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
19 changes: 18 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub(super) struct Em {
/// split wave64: pc → SGPRs whose whole value may be read after that instruction
/// (`x64_masks`); a mask written to none of them stays in its half (no exchange)
pub x64_whole_live: BTreeMap<u64, u128>,
/// split wave64: pc of the 64-bit bitwise ops that exchange their result once, for the
/// compares they combine (`x64_masks`)
pub x64_sync: BTreeSet<u64>,
/// wave64 VS / PS on a GPU with 32-lane waves only: what the console's lanes 32..63 hold
/// where the program reads them (`upper_lanes`), used when the wave has 32 lanes
pub upper: Option<super::upper_lanes::UpperLanes>,
Expand Down Expand Up @@ -481,6 +484,7 @@ impl Em {
uses_scratch: false,
x64_words: 1,
x64_whole_live: BTreeMap::new(),
x64_sync: BTreeSet::new(),
upper: None,
cur_pc: 0,
static_ss: BTreeMap::new(),
Expand Down Expand Up @@ -826,7 +830,7 @@ impl Em {
// no exchange for a mask that is only ever read lane by lane (the half's own word is
// enough), that goes nowhere, or that is 0 in every lane anyway
let local = match *dst {
Dst::S(n) => n < 126 && self.x64_whole_live.get(&self.cur_pc).is_some_and(|live| live & (3u128 << n) == 0),
Dst::S(n) => n <= 126 && n & 1 == 0 && self.x64_whole_live.get(&self.cur_pc).is_some_and(|live| live & (3u128 << n) == 0),
Dst::Null => true,
Dst::V(_) => false,
};
Expand Down Expand Up @@ -1456,6 +1460,10 @@ impl Em {
}
};
self.line(&format!("uint {t0} = {lo}; uint {t1} = {hi};"));
if self.x64() && self.x64_sync.contains(&self.cur_pc) {
let x = self.t();
self.line(&format!("uint2 {x} = xcl_x_sync_mask({t0}, {t1}); {t0} = {x}.x; {t1} = {x}.y;"));
}
if let Some(c) = scc {
self.line(&format!("scc = (uint)({c});"));
}
Expand Down Expand Up @@ -5465,6 +5473,15 @@ uint2 xcl_x_ballot(bool b) {
uint o = xcl_x_meet() ? xcl_x_get(0u) : 0u;
return xcl_half != 0u ? uint2(o, m) : uint2(m, o);
}
// a mask each half holds only its own word of (`x64_masks`): both words, from both halves.
// With no other half, the word computed here for it is already right (its compare bits are 0,
// and a tree op such as s_nor_b64 may still set them).
uint2 xcl_x_sync_mask(uint lo, uint hi) {
uint m = select(xcl_half != 0u, hi, lo);
xcl_x_put(0u, m);
uint o = xcl_x_meet() ? xcl_x_get(0u) : select(xcl_half != 0u, lo, hi);
return xcl_half != 0u ? uint2(o, m) : uint2(m, o);
}
// v_readlane / v_readfirstlane: the same lane index in both halves; the owner half's value
uint xcl_x_rla_uniform(uint v, uint l) {
uint m = WaveReadLaneAt(v, l & 31u);
Expand Down
4 changes: 3 additions & 1 deletion crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,9 @@ pub fn translate(code: &[u8], opt: &Options) -> Result<Translation, String> {
// split wave64: masks only read lane by lane skip the halves' exchange (geometry mode
// continues from the ES into the GS through s_setpc: left as it is)
if opt.wave64 && opt.split_wave64 && matches!(opt.mode, Mode::Kernel | Mode::Compute(_) | Mode::Mesh(_)) && std::env::var_os("XCL_X64_ALL_MASKS").is_none() {
em.x64_whole_live = x64_masks::by_pc(&insts, matches!(opt.mode, Mode::Kernel));
let plan = x64_masks::analyze(&insts, matches!(opt.mode, Mode::Kernel));
em.x64_whole_live = plan.whole_live;
em.x64_sync = plan.sync;
}
// a wave64 VS / PS on a GPU that can run 32-lane waves: what lanes 32..63 hold where the
// program reads them (the driver may still pick 64 lanes: decided at run time); only a
Expand Down
219 changes: 219 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5815,6 +5815,225 @@ fn gpu_split_wave64_lane_local_masks() {
}
}

/// Bitwise 64-bit SALU ops (`s_and_b64`, `s_or_b64`, `s_not_b64`, `s_andn2_b64`, `s_mov_b64`,
/// `s_cselect_b64`) over lane-local masks stay in each 32-lane half when neither their
/// destination nor `scc` is whole-live afterward; when `scc` IS read (e.g. only upper-half
/// lanes match and `s_cselect_b32` reads `scc`), the result is exchanged so both halves
/// observe the same `scc`.
#[test]
fn gpu_split_wave64_bitwise_lane_masks() {
if !gpu_ready() {
return;
}
const V: u64 = 256;
// Part 1: 3 masks combined purely for v_cndmask_b32 -> zero ballot exchanges
let local_code = prog(&[
// s[20:21] = (v0 >= 10), s[22:23] = (v0 < 50), vcc = ((v0 & 1) == 0)
("V_CMP_GE_U32", "ENC_VOP3", &[("VDST", 20), ("SRC0", V), ("SRC1", 138)], None),
("V_CMP_LT_U32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V), ("SRC1", 255)], Some(50)),
("V_AND_B32", "ENC_VOP2", &[("VDST", 9), ("SRC0", 129), ("VSRC1", 0)], None),
("V_CMP_EQ_U32", "ENC_VOPC", &[("SRC0", 128), ("VSRC1", 9)], None),
// s[24:25] = s[20:21] & s[22:23] (10 <= l < 50)
("S_AND_B64", "ENC_SOP2", &[("SDST", 24), ("SSRC0", 20), ("SSRC1", 22)], None),
// s[26:27] = s[24:25] & ~vcc (10 <= l < 50 && l odd)
("S_ANDN2_B64", "ENC_SOP2", &[("SDST", 26), ("SSRC0", 24), ("SSRC1", 106)], None),
// s[28:29] = ~s[20:21] | s[26:27] (l < 10 || (10 <= l < 50 && l odd))
("S_NOT_B64", "ENC_SOP1", &[("SDST", 28), ("SSRC0", 20)], None),
("S_OR_B64", "ENC_SOP2", &[("SDST", 28), ("SSRC0", 28), ("SSRC1", 26)], None),
// vcc = s_mov_b64(s[28:29]), then s_cselect_b64 with uniform scc (s0 == 1)
("S_MOV_B64", "ENC_SOP1", &[("SDST", 106), ("SSRC0", 28)], None),
("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 0), ("SSRC1", 129)], None),
("S_CSELECT_B64", "ENC_SOP2", &[("SDST", 106), ("SSRC0", 106), ("SSRC1", 26)], None),
("V_CNDMASK_B32", "ENC_VOP2", &[("VDST", 1), ("SRC0", V + 2), ("VSRC1", 3)], None),
// Clear all mask SGPRs and overwrite SCC so kernel-mode readback sees uniform registers
("S_MOV_B64", "ENC_SOP1", &[("SDST", 20), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 22), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 24), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 26), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 28), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 106), ("SSRC0", 128)], None),
]);
if runner::split_wave64() {
let t = translate(&local_code, &runner::options(Mode::Kernel)).unwrap();
assert_eq!(t.hlsl.matches("= xcl_x_ballot_local(and(").count(), 3, "all 3 compares stay local");
assert_eq!(t.hlsl.matches("= xcl_x_ballot(and(").count(), 0, "no ballot exchange needed");
}
let mut st = KernelState::default();
st.sgpr[0] = 1;
for (l, r) in st.vgpr.iter_mut().enumerate() {
r[0] = l as u32;
r[2] = 100 + l as u32;
r[3] = 200 + l as u32;
}
let r = runner::run_code(&local_code, &st, &[]).unwrap();
for l in 0..64u32 {
let cond = l < 10 || ((10..50).contains(&l) && l % 2 == 1);
assert_eq!(r.vgpr[l as usize][1], if cond { 200 + l } else { 100 + l }, "lane {l}: bitwise local mask");
}

// Part 2: only upper-half lanes (40..49) match; s_and_b64's SCC is read by s_cselect_b32 ->
// its result is exchanged (once, for both compares) so half 0 and half 1 agree on SCC == 1.
let scc_code = prog(&[
("V_CMP_GE_U32", "ENC_VOP3", &[("VDST", 20), ("SRC0", V), ("SRC1", 255)], Some(40)),
("V_CMP_LT_U32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V), ("SRC1", 255)], Some(50)),
("S_AND_B64", "ENC_SOP2", &[("SDST", 24), ("SSRC0", 20), ("SSRC1", 22)], None),
("S_CSELECT_B32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 255), ("SSRC1", 139)], Some(77)),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 20), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 22), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 24), ("SSRC0", 128)], None),
]);
if runner::split_wave64() {
let t = translate(&scc_code, &runner::options(Mode::Kernel)).unwrap();
assert_eq!(t.hlsl.matches("= xcl_x_ballot(and(").count(), 0, "the compares stay local");
assert_eq!(t.hlsl.matches("= xcl_x_sync_mask(").count(), 1, "live SCC: s_and_b64 exchanges its result");
}
let r = runner::run_code(&scc_code, &st, &[]).unwrap();
assert_eq!(r.sgpr[1], 77, "both halves must see SCC == 1 from upper-half match");
}

/// Split wave64 (`x64_masks`): EXEC written by `v_cmpx` or `s_and_saveexec_b64` and read only
/// lane by lane before it is restored stays in each half (no exchange); a `v_cmpx` whose EXEC
/// `v_readfirstlane` reads is still exchanged, and both halves see the first active lane.
#[test]
fn gpu_split_wave64_predicated_exec_masks() {
if !gpu_ready() {
return;
}
const V: u64 = 256;
let code = prog(&[
// v_cmpx region: lanes 0..49 add 100 to v4, then EXEC is restored
("S_MOV_B64", "ENC_SOP1", &[("SDST", 10), ("SSRC0", 126)], None),
("V_CMPX_LT_U32", "ENC_VOPC", &[("SRC0", V), ("VSRC1", 6)], None), // v6 = 50
("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 255), ("VSRC1", 4)], Some(100)),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 10)], None),
// s_and_saveexec_b64 region: lanes 20..63 add 50 to v5, then EXEC is restored
("V_CMP_GE_U32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V), ("SRC1", 148)], None),
("S_AND_SAVEEXEC_B64", "ENC_SOP1", &[("SDST", 10), ("SSRC0", 22)], None),
("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 255), ("VSRC1", 5)], Some(50)),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 10)], None),
// v_cmpx then v_readfirstlane: only lanes 41..63 active, both halves must read lane 41
("S_MOV_B64", "ENC_SOP1", &[("SDST", 10), ("SSRC0", 126)], None),
("V_CMPX_GT_U32", "ENC_VOP3", &[("SRC0", V), ("SRC1", 255)], Some(40)),
("V_READFIRSTLANE_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", V)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 10)], None),
// the kernel runner reads every SGPR and SCC back: clear the local mask, overwrite SCC
("S_MOV_B64", "ENC_SOP1", &[("SDST", 22), ("SSRC0", 128)], None),
("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 128), ("SSRC1", 128)], None),
]);
if runner::split_wave64() {
let t = translate(&code, &runner::options(Mode::Kernel)).unwrap();
assert_eq!(t.hlsl.matches("= xcl_x_ballot_local(and(").count(), 2, "both predicated regions stay local
{}", t.hlsl);
assert_eq!(t.hlsl.matches("= xcl_x_ballot(and(").count(), 1, "the v_cmpx read by v_readfirstlane is exchanged");
}
let mut st = KernelState::default();
for (l, r) in st.vgpr.iter_mut().enumerate() {
let l = l as u32;
r[0] = l;
r[4] = 1000 + l;
r[5] = 2000 + l;
r[6] = 50;
}
let r = runner::run_code(&code, &st, &[]).unwrap();
assert_eq!(r.sgpr[2], 41, "v_readfirstlane after a v_cmpx that leaves only upper-half lanes");
for l in 0..64u32 {
let v = &r.vgpr[l as usize];
assert_eq!(v[4], 1000 + l + if l < 50 { 100 } else { 0 }, "lane {l}: v_cmpx region");
assert_eq!(v[5], 2000 + l + if l >= 20 { 50 } else { 0 }, "lane {l}: s_and_saveexec region");
}
}

/// Split wave64 (`x64_masks`): two compares combined by `s_and_b64` and read whole
/// (`s_bcnt1`, and `scc` of a match in the upper half only) stay in their halves; the
/// `s_and_b64` exchanges its result once, and both halves see the whole-wave value.
#[test]
fn gpu_split_wave64_bitwise_mask_tree_exchanges_once() {
if !gpu_ready() {
return;
}
const V: u64 = 256;
let code = prog(&[
// vcc = lanes 5..63, s[24:25] = lanes 0..41, s0 = bcnt1(s[24:25] & vcc) = 37
("V_CMP_GE_U32", "ENC_VOPC", &[("SRC0", V), ("VSRC1", 7)], None), // v7 = 5
("V_CMP_LT_U32", "ENC_VOP3", &[("VDST", 24), ("SRC0", V), ("SRC1", 255)], Some(42)),
("S_AND_B64", "ENC_SOP2", &[("SDST", 24), ("SSRC0", 24), ("SSRC1", 106)], None),
("S_BCNT1_I32_B64", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 24)], None),
// lanes 41..63 & lanes 0..49 = 41..49, all in the upper half: scc = 1 in both halves
("V_CMP_GT_U32", "ENC_VOP3", &[("VDST", 26), ("SRC0", V), ("SRC1", 255)], Some(40)),
("V_CMP_LT_U32", "ENC_VOP3", &[("VDST", 28), ("SRC0", V), ("SRC1", 255)], Some(50)),
("S_AND_B64", "ENC_SOP2", &[("SDST", 26), ("SSRC0", 26), ("SSRC1", 28)], None),
("S_CSELECT_B32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 255), ("SSRC1", 139)], Some(77)),
// the kernel runner reads every SGPR and SCC back: clear the masks, overwrite SCC
("S_MOV_B64", "ENC_SOP1", &[("SDST", 106), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 24), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 26), ("SSRC0", 128)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 28), ("SSRC0", 128)], None),
("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 128), ("SSRC1", 128)], None),
]);
if runner::split_wave64() {
let t = translate(&code, &runner::options(Mode::Kernel)).unwrap();
assert_eq!(t.hlsl.matches("= xcl_x_ballot_local(and(").count(), 4, "the four compares stay local\n{}", t.hlsl);
assert_eq!(t.hlsl.matches("= xcl_x_ballot(and(").count(), 0);
assert_eq!(t.hlsl.matches("= xcl_x_sync_mask(").count(), 2, "each s_and_b64 exchanges once");
}
let mut st = KernelState::default();
for (l, r) in st.vgpr.iter_mut().enumerate() {
r[0] = l as u32;
r[7] = 5;
}
let r = runner::run_code(&code, &st, &[]).unwrap();
assert_eq!(r.sgpr[0], 37, "bcnt1 of lanes 5..41");
assert_eq!(r.sgpr[1], 77, "scc of a match in the upper half only, in both halves");
}

/// Split wave64 (`x64_masks`): a wave64 group of 32 threads has no upper half to exchange
/// with. The absent lanes' compare bits are 0, so `s_nor_b64` of two compares sets the whole
/// upper word; the tree's exchange must keep the word the lower half computed, for
/// `s_bcnt1` and for `scc`.
#[test]
fn gpu_split_wave64_bitwise_mask_tree_in_a_partial_wave() {
if !gpu_ready() {
return;
}
use super::{dxc, CsInterface};
let p = prog(&[
// ~(lanes 0..7 | lanes 24..31) = lanes 8..23 and 32..63: s10 = 48
("V_CMP_LT_U32", "ENC_VOP3", &[("VDST", 20), ("SRC0", V), ("SRC1", 136)], None),
("V_CMP_GE_U32", "ENC_VOP3", &[("VDST", 22), ("SRC0", V), ("SRC1", 152)], None),
("S_NOR_B64", "ENC_SOP2", &[("SDST", 24), ("SSRC0", 20), ("SSRC1", 22)], None),
("S_BCNT1_I32_B64", "ENC_SOP1", &[("SDST", 10), ("SSRC0", 24)], None),
// ~(lanes 0..15 | lanes 16..31) = lanes 32..63, none present: scc = 1, s11 = 77
("V_CMP_LT_U32", "ENC_VOP3", &[("VDST", 26), ("SRC0", V), ("SRC1", 144)], None),
("V_CMP_GE_U32", "ENC_VOP3", &[("VDST", 28), ("SRC0", V), ("SRC1", 144)], None),
("S_NOR_B64", "ENC_SOP2", &[("SDST", 26), ("SSRC0", 26), ("SSRC1", 28)], None),
("S_CSELECT_B32", "ENC_SOP2", &[("SDST", 11), ("SSRC0", 255), ("SSRC1", 139)], Some(77)),
// each thread stores (s10, s11) at s0 + 8 · thread
("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 10)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", 11)], None),
("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 7), ("SRC0", 131), ("VSRC1", 0)], None),
("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 7), ("SRC0", 0), ("VSRC1", 7)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 8), ("SRC0", 128)], None),
("GLOBAL_STORE_DWORDX2", "ENC_FLAT_GLBL", &[("ADDR", 7), ("DATA", 5), ("SADDR", 0x7D)], None),
]);
let cs = CsInterface { threads: [32, 1, 1], user_sgprs: 4, tgid: [false; 3], tg_size: false, tid_components: 1 };
let t = translate(&p, &on_gpu(Options { mode: Mode::Compute(cs), wave64: true, ..Default::default() })).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
if runner::split_wave64() {
assert_eq!(t.hlsl.matches("= xcl_x_ballot_local(and(").count(), 4, "the four compares stay local\n{}", t.hlsl);
assert_eq!(t.hlsl.matches("= xcl_x_sync_mask(").count(), 2, "each s_nor_b64 exchanges once");
}
let dxil = dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}"));
let base = 0x100usize;
let mut io = vec![0u32; 128];
io[0] = base as u32;
let out = runner::dispatch(&dxil, [1, 1, 1], &io, &vec![0u32; (base + 32 * 8) / 4]).unwrap();
for tid in 0..32 {
let at = (base + tid * 8) / 4;
assert_eq!(out[at], 48, "thread {tid}: s_bcnt1 of an s_nor_b64 with no upper half");
assert_eq!(out[at + 1], 77, "thread {tid}: scc of an s_nor_b64 set in the absent half only");
}
}

/// Masks that are read whole after a write `x64_masks` must not take for an overwrite (an
/// SDWA compare into its SDST, `s_bitset1`, `s_movreld`) or through a read the operand
/// tables do not show (`s_movrels`, an SMEM register offset, an SDWA scalar src1): each is
Expand Down
Loading
Loading