diff --git a/crates/xcl/src/gpu/recomp/heap_window_tests.rs b/crates/xcl/src/gpu/recomp/heap_window_tests.rs index 9eaf7e8b45..1ae5c75a4e 100644 --- a/crates/xcl/src/gpu/recomp/heap_window_tests.rs +++ b/crates/xcl/src/gpu/recomp/heap_window_tests.rs @@ -170,3 +170,253 @@ void main(uint l : SV_GroupThreadID) { } } } + +fn gpu_kernel_ready() -> bool { + if super::runner::adapter().is_err() { + super::runner::skip_test("no GPU"); + return false; + } + if super::runner::wave64_programs_supported() != Ok(true) { + super::runner::skip_test("no wave64"); + return false; + } + if super::dxc::location().is_err() { + super::runner::skip_test("no DXC"); + return false; + } + true +} + +fn atomic_int64_on_heap_supported() -> bool { + let Some((dev, _)) = device() else { + return false; + }; + let mut o11 = D3D12_FEATURE_DATA_D3D12_OPTIONS11::default(); + unsafe { dev.CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS11, &mut o11 as *mut _ as _, std::mem::size_of_val(&o11) as u32).is_ok() && o11.AtomicInt64OnDescriptorHeapResourceSupported.as_bool() } +} + +fn compile_variant(code: &[u8], direct: bool, heap: bool, mem_windows: u32) -> Vec { + let mut opt = super::runner::options(super::Mode::Kernel); + opt.mem_windows = mem_windows; + opt.heap_windows = heap; + if direct { + let mw = if heap && mem_windows > 1 { mem_windows | super::HEAP_WINDOWS } else { mem_windows }; + let ko = crate::gpu::dxil::kernel::KernelOptions { wave64: opt.wave64, split: opt.split_wave64, mem_windows: mw, sampler_base: 0 }; + crate::gpu::dxil::kernel::translate(code, ko).unwrap_or_else(|e| panic!("direct heap={heap}: {e}")) + } else { + let t = super::translate(code, &opt).unwrap_or_else(|e| panic!("hlsl heap={heap}: {e}")); + super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("dxc heap={heap}: {e}\n{}", t.hlsl)) + } +} + +/// Divergent per-lane window indexing (`NonUniformResourceIndex`): in a single wave, lanes hit +/// window 0 (`l % 3 == 0`), window 1 (`l % 3 == 1`), and out-of-UMA (`l % 3 == 2`, aliasing w0 +/// under `& 3`) simultaneously on loads, stores, and atomics. HLSL and direct DXIL must match +/// the switch form bit-for-bit on both registers and memory. +#[test] +fn gpu_heap_windows_divergent_lanes_match_switch_form() { + if !gpu_kernel_ready() { + return; + } + let e = |n: &str, enc: &str, f: &[(&str, u64)]| crate::gpu::rdna2::encode(n, enc, f, None).unwrap(); + let mut code = Vec::new(); + code.extend(e("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 4), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 0)])); + code.extend(e("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 2), ("DATA", 5), ("SADDR", 0x7D), ("OFFSET", 0)])); + code.extend(e("GLOBAL_ATOMIC_ADD", "ENC_FLAT_GLBL", &[("VDST", 8), ("ADDR", 6), ("DATA", 9), ("SADDR", 0x7D), ("OFFSET", 0), ("GLC", 1)])); + code.extend(e("S_ENDPGM", "ENC_SOPP", &[])); + + 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 base_hi = (w0 >> 32) as u32; + let word = |a: u64| unsafe { &mut *(a as *mut u32) }; + + const CANARY: u32 = 0xCAFE_BABE; + let mut st = super::runner::KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let hi = match l % 3 { + 0 => base_hi, + 1 => base_hi + 1, + _ => base_hi + 4, // OOB, shares low bits with w0 + }; + r[0] = (0x1000 + 4 * l) as u32; + r[1] = hi; + r[2] = (0x2000 + 4 * l) as u32; + r[3] = hi; + r[4] = 0x7777_7777; + r[5] = 0xA000_0000 | l as u32; + r[6] = 0x3000; + r[7] = hi; + r[8] = 0x7777_7777; + r[9] = 1; + } + + let mut baseline: Option> = None; + for (direct, heap) in [(false, false), (false, true), (true, false), (true, true)] { + for l in 0..64u64 { + *word(w0 + 0x1000 + 4 * l) = 0x1000 + l as u32; + *word(w1 + 0x1000 + 4 * l) = 0x2000 + l as u32; + *word(w0 + 0x2000 + 4 * l) = CANARY; + *word(w1 + 0x2000 + 4 * l) = CANARY; + } + *word(w0 + 0x3000) = 0; + *word(w1 + 0x3000) = 0; + + let dxil = compile_variant(&code, direct, heap, 2); + let res = super::runner::run_in_arena(&dxil, &st, &arena).unwrap_or_else(|err| panic!("direct={direct} heap={heap}: {err}")); + let loads: Vec = (0..64).map(|l| res.vgpr[l][4]).collect(); + for l in 0..64usize { + let want_load = match l % 3 { + 0 => 0x1000 + l as u32, + 1 => 0x2000 + l as u32, + _ => 0, + }; + assert_eq!(loads[l], want_load, "direct={direct} heap={heap} lane {l}: divergent load"); + let w0_store = *word(w0 + 0x2000 + 4 * l as u64); + let w1_store = *word(w1 + 0x2000 + 4 * l as u64); + match l % 3 { + 0 => { + assert_eq!(w0_store, 0xA000_0000 | l as u32, "direct={direct} heap={heap} lane {l}: w0 store"); + assert_eq!(w1_store, CANARY, "direct={direct} heap={heap} lane {l}: w1 untouched"); + } + 1 => { + assert_eq!(w1_store, 0xA000_0000 | l as u32, "direct={direct} heap={heap} lane {l}: w1 store"); + assert_eq!(w0_store, CANARY, "direct={direct} heap={heap} lane {l}: w0 untouched"); + } + _ => { + assert_eq!(w0_store, CANARY, "direct={direct} heap={heap} lane {l}: OOB store must not clobber w0"); + assert_eq!(w1_store, CANARY, "direct={direct} heap={heap} lane {l}: OOB store must not clobber w1"); + assert_eq!(res.vgpr[l][8], 0, "direct={direct} heap={heap} lane {l}: OOB atomic returns 0"); + } + } + } + assert_eq!(*word(w0 + 0x3000), 22, "direct={direct} heap={heap}: w0 atomic count (lanes 0,3,..,63)"); + assert_eq!(*word(w1 + 0x3000), 21, "direct={direct} heap={heap}: w1 atomic count (lanes 1,4,..,61)"); + match &baseline { + Some(b) => assert_eq!(&loads, b, "direct={direct} heap={heap}: must match switch baseline"), + None => baseline = Some(loads), + } + } +} + +/// 64-bit atomics (`GLOBAL_ATOMIC_ADD_X2`, `GLOBAL_ATOMIC_CMPSWAP_X2`) on heap windows: +/// in-bounds window-1 operations carry across the 32-bit boundary and swap cleanly, while +/// out-of-UMA 64-bit atomics return 0 and leave live window canaries untouched. +#[test] +fn gpu_heap_windows_64bit_atomics_match_switch_form() { + if !gpu_kernel_ready() { + return; + } + if !atomic_int64_on_heap_supported() { + super::runner::skip_test("no 64-bit heap atomics"); + return; + } + let e = |n: &str, enc: &str, f: &[(&str, u64)]| crate::gpu::rdna2::encode(n, enc, f, None).unwrap(); + let mut code = Vec::new(); + // 1. In-bounds w1 + 0x1000: 64-bit atomic add (+1 per lane) across 32-bit carry boundary + code.extend(e("GLOBAL_ATOMIC_ADD_X2", "ENC_FLAT_GLBL", &[("VDST", 10), ("ADDR", 0), ("DATA", 2), ("SADDR", 0x7D), ("OFFSET", 0), ("GLC", 1)])); + // 2. In-bounds w1 + 0x1010: 64-bit cmpswap + code.extend(e("GLOBAL_ATOMIC_CMPSWAP_X2", "ENC_FLAT_GLBL", &[("VDST", 12), ("ADDR", 4), ("DATA", 20), ("SADDR", 0x7D), ("OFFSET", 0), ("GLC", 1)])); + // 3. Out-of-UMA (base_hi + 5, aliases w1 + 0x1020 under & 3): 64-bit atomic add dropped + code.extend(e("GLOBAL_ATOMIC_ADD_X2", "ENC_FLAT_GLBL", &[("VDST", 14), ("ADDR", 6), ("DATA", 2), ("SADDR", 0x7D), ("OFFSET", 0), ("GLC", 1)])); + // 4. Out-of-UMA (base_hi + 4, aliases w0 + 0x1020 under & 3): 64-bit cmpswap dropped + code.extend(e("GLOBAL_ATOMIC_CMPSWAP_X2", "ENC_FLAT_GLBL", &[("VDST", 16), ("ADDR", 8), ("DATA", 20), ("SADDR", 0x7D), ("OFFSET", 0), ("GLC", 1)])); + code.extend(e("S_ENDPGM", "ENC_SOPP", &[])); + + 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 base_hi = (w0 >> 32) as u32; + let qword = |a: u64| unsafe { &mut *(a as *mut u64) }; + + const INIT_CAS: u64 = 0x1122_3344_5566_7788; + const NEW_CAS: u64 = 0x99AA_BBCC_DDEE_FF00; + const CANARY64: u64 = 0xDEAD_BEEF_CAFE_BABE; + + let mut st = super::runner::KernelState::default(); + for r in st.vgpr.iter_mut() { + r[0] = 0x1000; + r[1] = base_hi + 1; + r[2] = 1; + r[3] = 0; + r[4] = 0x1010; + r[5] = base_hi + 1; + r[6] = 0x1020; + r[7] = base_hi + 5; + r[8] = 0x1020; + r[9] = base_hi + 4; + r[14] = 0x7777_7777; + r[15] = 0x7777_7777; + r[16] = 0x7777_7777; + r[17] = 0x7777_7777; + r[20] = NEW_CAS as u32; + r[21] = (NEW_CAS >> 32) as u32; + r[22] = INIT_CAS as u32; + r[23] = (INIT_CAS >> 32) as u32; + } + + for (direct, heap) in [(false, false), (false, true), (true, false), (true, true)] { + *qword(w1 + 0x1000) = 0x0000_0000_FFFF_FFF0; + *qword(w1 + 0x1010) = INIT_CAS; + *qword(w1 + 0x1020) = CANARY64; + *qword(w0 + 0x1020) = CANARY64; + + let dxil = compile_variant(&code, direct, heap, 2); + let res = super::runner::run_in_arena(&dxil, &st, &arena).unwrap_or_else(|err| panic!("direct={direct} heap={heap}: {err}")); + + assert_eq!(*qword(w1 + 0x1000), 0x0000_0001_0000_0030, "direct={direct} heap={heap}: 64-bit add carry in w1"); + assert_eq!(*qword(w1 + 0x1010), NEW_CAS, "direct={direct} heap={heap}: 64-bit cmpswap in w1"); + let cas_winners = res.vgpr.iter().filter(|v| (v[12] as u64 | ((v[13] as u64) << 32)) == INIT_CAS).count(); + assert_eq!(cas_winners, 1, "direct={direct} heap={heap}: exactly one lane wins 64-bit cmpswap"); + assert_eq!(*qword(w1 + 0x1020), CANARY64, "direct={direct} heap={heap}: OOB 64-bit add must not touch w1"); + assert_eq!(*qword(w0 + 0x1020), CANARY64, "direct={direct} heap={heap}: OOB 64-bit cmpswap must not touch w0"); + assert_eq!((res.vgpr[0][14], res.vgpr[0][15]), (0, 0), "direct={direct} heap={heap}: OOB 64-bit add returns 0"); + assert_eq!((res.vgpr[0][16], res.vgpr[0][17]), (0, 0), "direct={direct} heap={heap}: OOB 64-bit cmpswap returns 0"); + } +} + +/// Multi-window `HEAP_WINDOWS` translation and DXIL validation across graphics stages (VS, PS, MS) +/// in both direct DXIL and HLSL+DXC (for 4 and 16 windows). +#[test] +fn heap_windows_graphics_stages_validate() { + use crate::gpu::dxbc::SigElement; + use crate::gpu::dxil::kernel::{translate_mesh, translate_pixel, translate_vertex, KernelOptions}; + use crate::gpu::recomp::{Mode, MsInterface, Options, PsInterface, VsInterface, HEAP_WINDOWS}; + + let sig = |name: &str, sv: u32, reg: u32| SigElement { name: name.into(), index: 0, system_value: sv, component_type: 3, register: reg, mask: 0xF, rw_mask: 0xF, stream: 0 }; + let e = |n: &str, enc: &str, f: &[(&str, u64)]| crate::gpu::rdna2::encode(n, enc, f, None).unwrap(); + + // Minimal shader that performs a global memory load (exercising heap-window accessors) and exports. + let mut vs_ps_code = Vec::new(); + vs_ps_code.extend(e("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 2), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 0)])); + vs_ps_code.extend(e("EXP", "ENC_EXP", &[("TGT", 12), ("EN", 0xF), ("VSRC0", 2), ("VSRC1", 2), ("VSRC2", 2), ("VSRC3", 2), ("DONE", 1)])); + vs_ps_code.extend(e("S_ENDPGM", "ENC_SOPP", &[])); + + let mut ps_code = Vec::new(); + ps_code.extend(e("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 2), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 0)])); + ps_code.extend(e("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 2), ("VSRC1", 2), ("VSRC2", 2), ("VSRC3", 2), ("DONE", 1), ("VM", 1)])); + ps_code.extend(e("S_ENDPGM", "ENC_SOPP", &[])); + + let vs = VsInterface { inputs: vec![], outputs: vec![sig("SV_Position", 1, 0)], attr_base: 4, params: None }; + let ps = PsInterface { inputs: vec![sig("SV_Position", 1, 0)], outputs: vec![sig("SV_Target", 64, 0)], input_ena: 0, attrs: None }; + let ms = MsInterface { outputs: vec![sig("SV_Position", 1, 0)], threads: 64, max_vertices: 3, max_primitives: 1 }; + + let has_dxc = super::dxc::location().is_ok(); + for windows in [4u32, crate::gpu::uma::MAX_HEAP_WINDOWS as u32] { + let ko = KernelOptions { wave64: true, split: false, mem_windows: windows | HEAP_WINDOWS, sampler_base: 0 }; + translate_vertex(&vs_ps_code, ko, &vs).unwrap_or_else(|err| panic!("direct VS {windows}w: {err}")); + translate_pixel(&ps_code, ko, &ps).unwrap_or_else(|err| panic!("direct PS {windows}w: {err}")); + translate_mesh(&vs_ps_code, ko, &ms).unwrap_or_else(|err| panic!("direct MS {windows}w: {err}")); + + for (label, code, mode, target) in [ + ("VS", &vs_ps_code, Mode::Vertex(vs.clone()), "vs_6_6"), + ("PS", &ps_code, Mode::Pixel(ps.clone()), "ps_6_6"), + ("MS", &vs_ps_code, Mode::Mesh(ms.clone()), "cs_6_6"), + ] { + let opt = Options { mode, mem_windows: windows, heap_windows: true, ..Default::default() }; + let t = super::translate(code, &opt).unwrap_or_else(|err| panic!("hlsl {label} {windows}w: {err}")); + assert!(t.hlsl.contains("ResourceDescriptorHeap["), "hlsl {label} {windows}w must use heap windows"); + if has_dxc { + super::dxc::compile(&t.hlsl, "main", target, &[]).unwrap_or_else(|err| panic!("dxc {label} {windows}w: {err}\n{}", t.hlsl)); + } + } + } +}