Skip to content
Merged
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
12 changes: 7 additions & 5 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,14 @@ impl Lw {
r
}

/// One unaligned dword from byte address `a` (one Load2 across memory windows).
fn ld_raw_u32_guarded(&mut self, guard: Option<Value>, a: Value) -> Value {
/// One unaligned dword from byte address `a` (one Load2 across memory windows), optionally
/// checking embedded shader `.rodata` for both loaded dwords.
fn ld_raw_u32_guarded(&mut self, guard: Option<Value>, a: Value, rodata: bool) -> Value {
let b = self.andc(a, !3);
let s = self.andc(a, 3);
let s = self.shlc(s, 3);
let w = self.with_mem_raw_loads(guard, &[(b, 0x3)]);
let w = if rodata && !self.rodata.is_empty() { self.rodata_loads(b, w) } else { w };
let k32 = self.c(32);
let inv = self.sub(k32, s);
let inv = self.andc(inv, 31);
Expand Down Expand Up @@ -1299,7 +1301,7 @@ impl Lw {
let nv = self.c(n);
return self.guarded(c, &mut |lw| lw.st_bytes(a, [data, z, z, z], nv));
}
let r0 = self.ld_raw_u32_guarded(Some(act), a);
let r0 = self.ld_raw_u32_guarded(Some(act), a, false);
let z = self.c(0);
let raw = self.sel(ok, r0, z);
let nv = self.d16_merge(v, raw, n, signed, half);
Expand All @@ -1316,7 +1318,7 @@ impl Lw {
let nv = self.c(n);
return self.guarded(c, &mut |lw| lw.st_bytes(a, [v, z, z, z], nv));
}
let r0 = self.ld_raw_u32_guarded(Some(act), a);
let r0 = self.ld_raw_u32_guarded(Some(act), a, false);
let bits = 8 * n;
let val = if signed {
let x = self.shlc(r0, 32 - bits);
Expand Down Expand Up @@ -2016,7 +2018,7 @@ impl Lw {
}
let vd = Self::fv(d, "VDST");
let v = self.vreg(vd);
let r0 = self.ld_raw_u32_guarded(Some(act), a);
let r0 = self.ld_raw_u32_guarded(Some(act), a, true);
let nv = self.d16_merge(v, r0, n, signed, half);
self.write32(&Dst::V(vd), nv);
return Ok(());
Expand Down
6 changes: 5 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4433,7 +4433,8 @@ impl Em {
self.line(&format!("if ({act}) st_bytes({t}_a, uint4({}, 0u, 0u, 0u), {n}u);", d16_data(&v, half)));
} else {
let v = self.vreg(f("VDST"));
self.line(&format!("if ({act}) {v} = {};", d16_merge(&v, &format!("ld_raw({t}_a).x"), n, signed, half)));
let raw = if self.opt.rodata.iter().any(|b| !b.1.is_empty()) { format!("xcl_gload_u32({t}_a)") } else { format!("ld_raw({t}_a).x") };
self.line(&format!("if ({act}) {v} = {};", d16_merge(&v, &raw, n, signed, half)));
}
return Ok(());
}
Expand Down Expand Up @@ -5975,6 +5976,9 @@ fn rodata_loader(rodata: &[(u32, Vec<u32>)], windows: bool) -> String {
let inside = format!("{w0}({})", inside.join(" || "));
let _ = writeln!(s, "uint2 xcl_gload2(uint a) {{ [branch] if ({inside}) {{ return uint2(xcl_gload(a), xcl_gload(a + 4u)); }} return mem.Load2(a); }}");
let _ = writeln!(s, "uint4 xcl_gload4(uint a) {{ [branch] if ({inside}) {{ return uint4(xcl_gload(a), xcl_gload(a + 4u), xcl_gload(a + 8u), xcl_gload(a + 12u)); }} return mem.Load4(a); }}");
// (each dword looked up on its own, as `rodata_loads`: a load from 1 - 3 bytes before a block
// takes its upper bytes from the block)
let _ = writeln!(s, "uint xcl_gload_u32(uint a) {{ uint b = a & ~3u; uint s = (a & 3u) * 8u; uint2 w = uint2(xcl_gload(b), xcl_gload(b + 4u)); return select(s == 0u, w.x, (w.x >> s) | (w.y << ((32u - s) & 31u))); }}");
s
}

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

/// Sub-dword `global_load_*` instructions (`ubyte`, `sbyte`, `ushort`, `sshort`, `short_d16`,
/// `short_d16_hi`, including unaligned 16-bit loads crossing a 4-byte boundary) read shader
/// `.rodata` in both HLSL (`xcl_gload_u32`) and Direct DXIL (`rodata_loads`) while keeping
/// non-code memory addresses on regular memory.
#[test]
fn subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil() {
use crate::gpu::dxil::{kernel, validate};

let body = |table: u32| {
prog(&[
("S_GETPC_B64", "ENC_SOP1", &[("SDST", 0)], None), // s[0:1] = 4
("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 255)], Some(table.wrapping_sub(4))),
("S_ADDC_U32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 1), ("SSRC1", 128)], None),
// s[2:3] = s[0:1] - 8: 4 bytes before the rodata block
("S_SUB_U32", "ENC_SOP2", &[("SDST", 2), ("SSRC0", 0), ("SSRC1", 136)], None),
("S_SUBB_U32", "ENC_SOP2", &[("SDST", 3), ("SSRC0", 1), ("SSRC1", 128)], None),
// sub-dword loads from rodata at byte offset v10 (crossing a 4-byte boundary at v10=3,7,11)
("GLOBAL_LOAD_UBYTE", "ENC_FLAT_GLBL", &[("VDST", 2), ("ADDR", 10), ("SADDR", 0), ("OFFSET", 0)], None),
("GLOBAL_LOAD_SBYTE", "ENC_FLAT_GLBL", &[("VDST", 3), ("ADDR", 10), ("SADDR", 0), ("OFFSET", 0)], None),
("GLOBAL_LOAD_USHORT", "ENC_FLAT_GLBL", &[("VDST", 4), ("ADDR", 10), ("SADDR", 0), ("OFFSET", 0)], None),
("GLOBAL_LOAD_SSHORT", "ENC_FLAT_GLBL", &[("VDST", 5), ("ADDR", 10), ("SADDR", 0), ("OFFSET", 0)], None),
("GLOBAL_LOAD_SHORT_D16", "ENC_FLAT_GLBL", &[("VDST", 6), ("ADDR", 10), ("SADDR", 0), ("OFFSET", 0)], None),
("GLOBAL_LOAD_SHORT_D16_HI", "ENC_FLAT_GLBL", &[("VDST", 7), ("ADDR", 10), ("SADDR", 0), ("OFFSET", 0)], None),
// non-rodata memory load at v[12:13]
("GLOBAL_LOAD_USHORT", "ENC_FLAT_GLBL", &[("VDST", 8), ("ADDR", 12), ("SADDR", 0x7D), ("OFFSET", 0)], None),
// 16-bit loads across the block's edges: v11 = 3 (1 byte before it), 23 (its last byte)
("GLOBAL_LOAD_USHORT", "ENC_FLAT_GLBL", &[("VDST", 14), ("ADDR", 11), ("SADDR", 2), ("OFFSET", 0)], None),
])
};
let block = body(0).len() as u32 + 4;
let code = body(block + 4);
let table: [u32; 4] = [0x83_12_F0_05, 0x7F_A4_91_23, 0xC2_55_00_EE, 0x11_22_33_44];
// the memory words on either side of the block
const BEFORE: u32 = 0xA1B2_C3D4;
const AFTER: u32 = 0xBAD0_0005;
let bytes: Vec<u8> = table.iter().flat_map(|w| w.to_le_bytes()).collect();
let mut data = vec![table.len() as u32];
data.extend_from_slice(&table);
let rodata = vec![(block, data)];

// Without rodata, HLSL must keep `ld_raw` and omit `xcl_gload_u32`.
let no_ro = translate(&code, &Options::default()).unwrap();
assert!(no_ro.complete(), "{:?}", no_ro.unsupported_why);
assert!(!no_ro.hlsl.contains("xcl_gload_u32"), "unexpected xcl_gload_u32 without rodata");
// With rodata, HLSL must emit `xcl_gload_u32` and Direct DXIL must translate (and validate when DXC is present).
let with_ro = translate(&code, &Options { rodata: rodata.clone(), ..Options::default() }).unwrap();
assert!(with_ro.complete(), "{:?}", with_ro.unsupported_why);
assert!(with_ro.hlsl.contains("xcl_gload_u32("), "expected xcl_gload_u32 in HLSL:\n{}", with_ro.hlsl);
for mw in [1u32, 2] {
let k = kernel::translate_entry_rodata(
&code,
kernel::KernelOptions { wave64: true, split: false, mem_windows: mw, sampler_base: 0 },
kernel::Entry::Kernel,
&rodata,
)
.expect("direct DXIL translate sub-dword rodata");
if super::dxc::location().is_ok() {
validate::validate(&k.dxil).expect("direct DXIL validate sub-dword rodata");
}
}

if !gpu_ready() {
return;
}

let check = |r: &runner::KernelResult, what: &str| {
for l in 0..64usize {
let off = l % 15; // exercises all alignments 0..3, including boundary crossings 3, 7, 11
let b0 = bytes[off];
let u16v = u16::from_le_bytes([bytes[off], bytes[off + 1]]);
let v = &r.vgpr[l];
assert_eq!(v[2], b0 as u32, "{what} lane {l} off {off}: ubyte");
assert_eq!(v[3], (b0 as i8 as i32) as u32, "{what} lane {l} off {off}: sbyte");
assert_eq!(v[4], u16v as u32, "{what} lane {l} off {off}: ushort");
assert_eq!(v[5], (u16v as i16 as i32) as u32, "{what} lane {l} off {off}: sshort");
assert_eq!(v[6], 0xCAFE_0000 | u16v as u32, "{what} lane {l} off {off}: short_d16");
assert_eq!(v[7], ((u16v as u32) << 16) | 0xBEEF, "{what} lane {l} off {off}: short_d16_hi");
assert_eq!(v[8], 0x5000 + l as u32, "{what} lane {l}: non-rodata memory ushort");
// memory's top byte and the block's first (its length, 4); the block's last byte and memory's low one
let edge = if l % 2 == 0 { (BEFORE >> 24) | 0x0400 } else { (table[3] >> 24) | (AFTER & 0xFF) << 8 };
assert_eq!(v[14], edge, "{what} lane {l}: ushort across the block's {} edge", if l % 2 == 0 { "start" } else { "end" });
}
};

// Single window: memory has no copy of the rodata table.
let mut st = KernelState::default();
let mut mem = vec![0u32; 1024];
for (l, r) in st.vgpr.iter_mut().enumerate() {
r[6] = 0xCAFE_1111;
r[7] = 0x2222_BEEF;
r[10] = (l % 15) as u32;
r[11] = if l % 2 == 0 { 3 } else { 23 };
r[12] = 0x800 + 4 * l as u32;
mem[0x200 + l] = 0xDEAD_0000 | (0x5000 + l as u32);
}
mem[block as usize / 4 - 1] = BEFORE;
mem[block as usize / 4 + 5] = AFTER;
let th = translate(&code, &Options { rodata: rodata.clone(), ..runner::options(Mode::Kernel) }).unwrap();
let dxil_hlsl = super::dxc::compile(&th.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", th.hlsl)).unwrap();
check(&runner::run(&dxil_hlsl, &st, &mem).unwrap(), "HLSL 1-window");

let split = runner::wave64_supported() != Ok(true);
let k1 = kernel::translate_entry_rodata(&code, kernel::KernelOptions { wave64: true, split, mem_windows: 1, sampler_base: 0 }, kernel::Entry::Kernel, &rodata).unwrap();
check(&runner::run(&k1.dxil, &st, &mem).unwrap(), "Direct DXIL 1-window");

// Multi-window: window 0 holds decoy bytes at the rodata offset; non-rodata memory lives in window 1.
let arena = crate::gpu::uma::Uma::with_windows(2, 1 << 20).unwrap();
let (w0, w1) = (arena.window(0).0 as u64, arena.window(1).0 as u64);
let word = |a: u64| unsafe { &mut *(a as *mut u32) };
for k in 0..8u64 {
*word(w0 + block as u64 + 4 * k) = 0xBAD0_0000 + k as u32;
}
*word(w0 + block as u64 - 4) = BEFORE;
for (l, r) in st.vgpr.iter_mut().enumerate() {
*word(w1 + 0x800 + 4 * l as u64) = 0xDEAD_0000 | (0x5000 + l as u32);
r[12] = (w1 + 0x800 + 4 * l as u64) as u32;
r[13] = (w1 >> 32) as u32;
}
for heap in [false, true] {
let th2 = translate(&code, &on_gpu(Options { mem_windows: 2, heap_windows: heap, rodata: rodata.clone(), ..Default::default() })).unwrap();
let dxil2 = super::dxc::compile(&th2.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", th2.hlsl)).unwrap();
check(&runner::run_in_arena(&dxil2, &st, &arena).unwrap(), if heap { "HLSL heap windows" } else { "HLSL 2-window" });
}
let k2 = kernel::translate_entry_rodata(&code, kernel::KernelOptions { wave64: true, split, mem_windows: 2, sampler_base: 0 }, kernel::Entry::Kernel, &rodata).unwrap();
check(&runner::run_in_arena(&k2.dxil, &st, &arena).unwrap(), "Direct DXIL 2-window");
}

/// Experiment: does OpenExistingHeapFromAddress accept a reservation with only part committed?
#[test]
#[ignore]
Expand Down
Loading