From 6232c2a006ce90723f880a813b719621ac372326 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Sun, 29 Nov 2026 05:29:39 -0500 Subject: [PATCH 1/2] recomp: read shader rodata on sub-dword global/flat loads in HLSL and Direct DXIL HLSL's xcl_gload_u32 looks up each of the two dwords on its own, as Direct DXIL's rodata_loads does: through xcl_gload2 it decided with the first dword's address only, so a 16-bit load starting 1 byte before a rodata block took its upper byte from memory in HLSL and from the block in Direct DXIL. The test adds 16-bit loads across the block's start and end edges. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/lower_mem.rs | 12 +-- crates/xcl/src/gpu/recomp/emit.rs | 6 +- crates/xcl/src/gpu/recomp/tests.rs | 127 +++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index 1196b9dab0..3d03a0abbf 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -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, 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, 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); @@ -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); @@ -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); @@ -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(()); diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 2bf6fb7293..31ae3d889c 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -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(()); } @@ -5975,6 +5976,9 @@ fn rodata_loader(rodata: &[(u32, Vec)], 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 } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 9bb31dc3ad..3869c0c7d0 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -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 = 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] From fb986aecec94a839d72cf5bcaf2d5f26fabf6edc Mon Sep 17 00:00:00 2001 From: Foniass <107039406+Foniass@users.noreply.github.com> Date: Tue, 1 Dec 2026 19:36:09 +0100 Subject: [PATCH 2/2] Tests: expect 0 beside the rodata block with several memory windows subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil expected the ushort that straddles the start and end of the rodata block to take its outer byte from window 0's decoy words (BEFORE / AFTER) on the 2-window and heap-window paths too. With several memory windows an address in the shader's code range lies in no window, and a load from no window reads 0 (emit.rs's window lookup and lower_window.rs); main's own gpu_loads_read_the_constant_data_after_the_program relies on the same rule. So the HLSL 2-window run failed ("ushort across the block's start edge", 1024 against 1185) although the loads were right. The check now takes whether the run uses several windows and expects 0 for the byte beside the block then. The 1-window paths keep BEFORE / AFTER. The test still fails with emit.rs and lower_mem.rs reset to main. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/tests.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 3869c0c7d0..04938043b2 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -4997,7 +4997,9 @@ fn subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil() { return; } - let check = |r: &runner::KernelResult, what: &str| { + // `windows`: several memory windows, where a code address is in none of them, so the + // bytes beside the block read 0 + let check = |r: &runner::KernelResult, what: &str, windows: bool| { for l in 0..64usize { let off = l % 15; // exercises all alignments 0..3, including boundary crossings 3, 7, 11 let b0 = bytes[off]; @@ -5011,7 +5013,8 @@ fn subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil() { 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 }; + let (before, after) = if windows { (0, 0) } else { (BEFORE, AFTER) }; + 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" }); } }; @@ -5031,13 +5034,13 @@ fn subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil() { 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"); + check(&runner::run(&dxil_hlsl, &st, &mem).unwrap(), "HLSL 1-window", false); 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"); + check(&runner::run(&k1.dxil, &st, &mem).unwrap(), "Direct DXIL 1-window", false); - // Multi-window: window 0 holds decoy bytes at the rodata offset; non-rodata memory lives in window 1. + // Multi-window: window 0 holds decoy bytes at and around 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) }; @@ -5053,10 +5056,10 @@ fn subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil() { 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" }); + check(&runner::run_in_arena(&dxil2, &st, &arena).unwrap(), if heap { "HLSL heap windows" } else { "HLSL 2-window" }, true); } 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"); + check(&runner::run_in_arena(&k2.dxil, &st, &arena).unwrap(), "Direct DXIL 2-window", true); } /// Experiment: does OpenExistingHeapFromAddress accept a reservation with only part committed?