diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043f..8d0b7293c1 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -5582,7 +5582,7 @@ fn windowed(h: &str, windows: u32, heap: Option<&str>) -> String { /// `default` (reads return 0, stores/atomics are dropped and zero their `out` parameter) so /// out-of-UMA pointers can never wrap onto live memory windows. /// `heap`: Some(storage class) – window k is taken from the descriptor heap -/// (`Options::heap_windows`) and the same rule is one comparison before the access. +/// (`Options::heap_windows`), and an out-of-range `xcl_w` takes the null view at `MEM_HEAP_NULL`. fn mem_accessors(windows: u32, heap: Option<&str>) -> String { let mut sigs: Vec<(&str, String, &str, &str, &str)> = vec![ ("uint", "Load".into(), "uint a", "a", "return 0u;"), @@ -5610,14 +5610,16 @@ fn mem_accessors(windows: u32, heap: Option<&str>) -> String { let mut s = String::new(); if let Some(coherent) = heap { // one access: window k is descriptor MEM_HEAP_BASE + k; an `xcl_w` outside the windows - // does what the switch's `default` does - let _ = writeln!(s, "#define XCL_MW {coherent}RWByteAddressBuffer m = ResourceDescriptorHeap[NonUniformResourceIndex({}u + xcl_w)]", super::MEM_HEAP_BASE); - let _ = writeln!(s, "uint4 xcl_loadx4(uint4 a) {{ [branch] if (xcl_w >= {windows}u) {{ return uint4(0u, 0u, 0u, 0u); }} XCL_MW; return uint4(m.Load(a.x), m.Load(a.y), m.Load(a.z), m.Load(a.w)); }}"); - for (ret, name, params, args, def) in sigs { + // takes the null view at MEM_HEAP_NULL, which does what the switch's `default` does. No + // branch: a guard gives each access two paths, DXC's GVN can join their handles into a + // `phi`, and NVIDIA refuses such programs at once with E_OUTOFMEMORY (#756). + let null = super::MEM_HEAP_NULL - super::MEM_HEAP_BASE; + let index = if windows >= null { format!("min(xcl_w, {null}u)") } else { format!("select(xcl_w < {windows}u, xcl_w, {null}u)") }; + let _ = writeln!(s, "#define XCL_MW {coherent}RWByteAddressBuffer m = ResourceDescriptorHeap[NonUniformResourceIndex({}u + {index})]", super::MEM_HEAP_BASE); + let _ = writeln!(s, "uint4 xcl_loadx4(uint4 a) {{ XCL_MW; return uint4(m.Load(a.x), m.Load(a.y), m.Load(a.z), m.Load(a.w)); }}"); + for (ret, name, params, args, _) in sigs { let r = if ret == "void" { "" } else { "return " }; - // (the switch's `break` leaves the accessor) - let outside = def.replace("break;", "return;"); - let _ = writeln!(s, "{ret} xcl_m{name}({params}) {{ [branch] if (xcl_w >= {windows}u) {{ {outside} }} XCL_MW; {r}m.{name}({args}); }}"); + let _ = writeln!(s, "{ret} xcl_m{name}({params}) {{ XCL_MW; {r}m.{name}({args}); }}"); } return s; } diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index d1e7d8bc4e..3da939d160 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -79,6 +79,10 @@ mod heap_window_tests; /// (`console_heap::null_views`). pub const MEM_HEAP_BASE: u32 = 4; +/// The null raw UAV after the windows' views: an `xcl_w` outside the windows takes it (reads 0, +/// stores and atomics dropped, atomics return 0: the switch's `default`). +pub const MEM_HEAP_NULL: u32 = MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32; + /// Flag in a memory-windows count (`pso::mem_windows`, the translation caches' keys): /// `Options::heap_windows`. `translate` takes it out of `mem_windows`. pub const HEAP_WINDOWS: u32 = 0x100; @@ -519,8 +523,8 @@ pub struct Options { /// every access from the high address bits pub mem_windows: u32, /// with `mem_windows` > 1: reach window k through descriptor `MEM_HEAP_BASE + k` of the - /// shader-visible heap (a raw UAV of the window, `ResourceDescriptorHeap`) – one access per - /// site instead of a `switch` over the root UAVs `mem`, `mem1`.., which inlines every + /// shader-visible heap (a raw UAV of the window, `ResourceDescriptorHeap`; `MEM_HEAP_NULL` + /// outside the windows) – one access per site, with no branch, instead of a `switch` over the root UAVs `mem`, `mem1`.., which inlines every /// access once per window (HANDOFF: twice the DXC + driver time with 4 windows) pub heap_windows: bool, /// structured control flow when the program allows it (false, or `XCL_RECOMP_NO_STRUCTURED`: diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index b6dd120ff1..31c2e172e4 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -105,7 +105,7 @@ pub fn in_switch_form(f: impl FnOnce() -> R) -> R { } /// Whether translated shaders reach the memory windows through the shared descriptor heap -/// (`Options::heap_windows`; descriptors `MEM_HEAP_BASE`.. made by `console_heap::set_window_views`) +/// (`Options::heap_windows`; descriptors `MEM_HEAP_BASE`..=`MEM_HEAP_NULL` made by `console_heap::set_window_views`) /// instead of a `switch` over root UAVs. Opt-in: on with `XCL_HEAP_WINDOWS=1`, unless the views /// could not be made (no shared heap: then the switch, as before). Off by default: two NVIDIA /// setups failed with it (the driver's compiler ending the process; a device removal when the diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs index ed683350f7..5c20dcfd5d 100644 --- a/crates/xcl/src/gpu/recomp/runner.rs +++ b/crates/xcl/src/gpu/recomp/runner.rs @@ -1528,7 +1528,7 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result Result, Some(&raw_window_uav(size as u64)), h); } + let mut h = dh.GetCPUDescriptorHandleForHeapStart(); + h.ptr += super::MEM_HEAP_NULL as usize * inc; + dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(0)), h); list.SetDescriptorHeaps(&[Some(dh.clone())]); list.SetComputeRootSignature(&gpu.rs); list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress()); @@ -1692,7 +1695,7 @@ fn dispatch_with( let dh: ID3D12DescriptorHeap = dev .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, - NumDescriptors: super::MEM_HEAP_BASE + win_count as u32, + NumDescriptors: super::MEM_HEAP_NULL + 1, Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, NodeMask: 0, }) @@ -1703,6 +1706,9 @@ fn dispatch_with( h.ptr += (super::MEM_HEAP_BASE as usize + k) * inc; dev.CreateUnorderedAccessView(&mem_buf, None::<&ID3D12Resource>, Some(&raw_window_uav(mem_bytes)), h); } + let mut h = dh.GetCPUDescriptorHandleForHeapStart(); + h.ptr += super::MEM_HEAP_NULL as usize * inc; + dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(0)), h); list.SetDescriptorHeaps(&[Some(dh.clone()), Some(bound._heaps[1].clone())]); list.SetComputeRootSignature(rs); list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress()); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e648..2a56083c22 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -3808,23 +3808,32 @@ fn gpu_mesh_mode_reads_memory_windows() { /// accesses in window 0. #[test] fn gpu_memory_windows_above_4_gib() { - memory_windows_above_4_gib(false); + memory_windows_above_4_gib(2, false); } /// The same program with the windows taken from the descriptor heap (`Options::heap_windows`): /// same results, and no `switch` over windows in the HLSL. #[test] fn gpu_memory_windows_from_the_heap() { - memory_windows_above_4_gib(true); + memory_windows_above_4_gib(2, true); } -fn memory_windows_above_4_gib(heap: bool) { +// Adapted from XCL PR #822, e9705bb (GPL-2.0-only): +// https://github.com/xcl-dev/xcl/pull/822. Also check 8 windows and explicit Direct DXIL. +#[test] +fn gpu_memory_windows_beyond_the_root_uavs() { + for windows in [8, 16] { + memory_windows_above_4_gib(windows, true); + } +} + +fn memory_windows_above_4_gib(windows: usize, heap: bool) { if !gpu_ready() { return; } - 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); - assert_eq!(w1 - w0, 1 << 32); + let arena = crate::gpu::uma::Uma::with_windows(windows, 1 << 20).unwrap(); + let (w0, w1) = (arena.window(0).0 as u64, arena.window(windows - 1).0 as u64); + assert_eq!(w1 - w0, ((windows - 1) as u64) << 32); let word = |a: u64| unsafe { &mut *(a as *mut u32) }; *word(w1 + 0x1000) = 0xC0FFEE; *word(w1 + 0x1004) = 0x1234; @@ -3849,12 +3858,19 @@ fn memory_windows_above_4_gib(heap: bool) { let p = prog(&refs); let one = translate(&p, &Options::default()).unwrap(); assert!(!one.hlsl.contains("mem1") && !one.hlsl.contains("xcl_w =") && !one.hlsl.contains("xcl_m"), "one window: the single-window HLSL"); - let t = translate(&p, &on_gpu(Options { mem_windows: 2, heap_windows: heap, ..Default::default() })).unwrap(); + let t = translate(&p, &on_gpu(Options { mem_windows: windows as u32, heap_windows: heap, ..Default::default() })).unwrap(); assert!(t.hlsl.contains("RWByteAddressBuffer mem1 : register(u3)")); assert_eq!(t.hlsl.contains("switch (xcl_w)"), !heap, "heap windows: no switch over windows"); - assert_eq!(t.hlsl.contains("ResourceDescriptorHeap[NonUniformResourceIndex(4u + xcl_w)]"), heap); let hlsl_dxil = || super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", t.hlsl)).unwrap(); - let dxil = direct_or(&p, &on_gpu(Options { mem_windows: 2, heap_windows: heap, ..Default::default() }), hlsl_dxil()); + let dxil = if windows > 4 && runner::direct_dxil() { + // direct_or intentionally falls back above four windows: do not count that as DXIL coverage. + crate::gpu::dxil::kernel::translate(&p, crate::gpu::dxil::kernel::KernelOptions { + wave64: true, split: runner::split_wave64(), + mem_windows: windows as u32 | super::HEAP_WINDOWS, sampler_base: 0, + }).expect("expanded direct DXIL") + } else { + direct_or(&p, &on_gpu(Options { mem_windows: windows as u32, heap_windows: heap, ..Default::default() }), hlsl_dxil()) + }; let hi = |a: u64| (a >> 32) as u32; let mut st = KernelState::default(); st.sgpr[2..4].copy_from_slice(&[(w1 + 0x1000) as u32, hi(w1)]); @@ -3883,6 +3899,13 @@ fn memory_windows_above_4_gib(heap: bool) { } assert_eq!(*word(w1 + 0x6000), 64, "global_atomic_add in window 1"); assert_eq!(*word(w0 + 0x6000), 0, "nothing in window 0 at the same low address"); + if heap { + let null = super::MEM_HEAP_NULL - super::MEM_HEAP_BASE; + let index = if windows as u32 >= null { format!("min(xcl_w, {null}u)") } else { format!("select(xcl_w < {windows}u, xcl_w, {null}u)") }; + assert!(t.hlsl.contains(&format!("ResourceDescriptorHeap[NonUniformResourceIndex({}u + {index})]", super::MEM_HEAP_BASE))); + assert!(!t.hlsl.contains("if (xcl_w"), "heap windows: no branch around an access"); + } + println!("{windows} windows, target {}, direct={}: scalar/buffer/global loads, stores and atomic output correct", windows - 1, runner::direct_dxil()); } /// Out-of-UMA addresses must NEVER alias onto live UMA windows via `& 3`: @@ -3895,24 +3918,31 @@ fn memory_windows_above_4_gib(heap: bool) { /// on `global_load`/`global_store` updates the target window accurately. #[test] fn gpu_memory_windows_reject_out_of_uma_aliasing() { - memory_windows_reject_out_of_uma_aliasing(false); + memory_windows_reject_out_of_uma_aliasing(false, 2); } /// The same with the windows taken from the descriptor heap (`Options::heap_windows`): an /// address outside them reads 0 and drops its stores and atomics there too. #[test] fn gpu_heap_windows_reject_out_of_uma_aliasing() { - memory_windows_reject_out_of_uma_aliasing(true); + memory_windows_reject_out_of_uma_aliasing(true, 2); +} + +/// Cover partial and complete expanded layouts; the full layout uses the min-index path. +#[test] +fn gpu_heap_windows_reject_out_of_uma_aliasing_all_slots() { + for windows in [4, 8, 16] { memory_windows_reject_out_of_uma_aliasing(true, windows); } } -fn memory_windows_reject_out_of_uma_aliasing(heap: bool) { +fn memory_windows_reject_out_of_uma_aliasing(heap: bool, windows: u32) { if !gpu_ready() { return; } - let arena = crate::gpu::uma::Uma::with_windows(2, 1 << 20).unwrap(); + let arena = crate::gpu::uma::Uma::with_windows(windows as usize, 1 << 20).unwrap(); let (w0, w1) = (arena.window(0).0 as u64, arena.window(1).0 as u64); assert_eq!(w1 - w0, 1 << 32); let base_hi = (w0 >> 32) as u32; + let outside = windows.max(4); assert!(base_hi >= 4); let word = |a: u64| unsafe { &mut *(a as *mut u32) }; @@ -3933,24 +3963,24 @@ fn memory_windows_reject_out_of_uma_aliasing(heap: bool) { (n, "ENC_FLAT_GLBL", f.iter().map(|&(a, b)| (unsafe { std::mem::transmute::<&str, &'static str>(a) }, b)).collect(), None) }; let list = vec![ - // 1. Out-of-UMA s_load from hi = base_hi + 4 (would alias w0 under `& 3`) -> must read 0 + // 1. Out-of-UMA s_load from hi = base_hi + outside (would alias w0 under `& 3`) -> must read 0 ("S_LOAD_DWORD", "ENC_SMEM", vec![("SDATA", 30), ("SBASE", 1), ("OFFSET", 0)], None), // s[2:3] // 2. 32-bit hi == 0 s_load at offset 0x4000 -> MUST read w0 + 0x4000 ("S_LOAD_DWORD", "ENC_SMEM", vec![("SDATA", 31), ("SBASE", 2), ("OFFSET", 0)], None), // s[4:5] - // 3. Out-of-UMA buffer_store with V# hi = base_hi + 5 and stride = 4 -> must NOT touch w1 + 0x1000 + // 3. Out-of-UMA buffer_store with V# hi = base_hi + outside + 1 and stride = 4 -> must NOT touch w1 + 0x1000 ("BUFFER_STORE_DWORD", "ENC_MUBUF", vec![("VDATA", 1), ("VADDR", 0), ("SRSRC", 2), ("SOFFSET", 128), ("IDXEN", 1)], None), // s[8:11] - // 4. Out-of-UMA buffer_load with V# hi = base_hi + 5 and stride = 4 -> must read 0 (v2) + // 4. Out-of-UMA buffer_load with V# hi = base_hi + outside + 1 and stride = 4 -> must read 0 (v2) ("BUFFER_LOAD_DWORD", "ENC_MUBUF", vec![("VDATA", 2), ("VADDR", 0), ("SRSRC", 2), ("SOFFSET", 128), ("IDXEN", 1)], None), // 5. Valid strided V# buffer_load from w1 + 0x5000 (d1 has non-zero stride 4 in bits 29:16) -> must read 0x5566_7788 (v3) ("BUFFER_LOAD_DWORD", "ENC_MUBUF", vec![("VDATA", 3), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None), // s[12:15] - // 6. Out-of-UMA global_store with hi = base_hi + 4 (would alias w0 + 0x1000) -> dropped + // 6. Out-of-UMA global_store with hi = base_hi + outside (would alias w0 + 0x1000) -> dropped glbl("GLOBAL_STORE_DWORD", &[("ADDR", 4), ("DATA", 1), ("SADDR", 0x7D), ("OFFSET", 0)]), // 7. Out-of-UMA global_store with hi = 0 (null/32-bit pointer at 0x2000) -> dropped, must NOT touch w0 + 0x2000 glbl("GLOBAL_STORE_DWORD", &[("ADDR", 6), ("DATA", 1), ("SADDR", 0x7D), ("OFFSET", 0)]), - // 8. Out-of-UMA global_atomic_add (hi = base_hi + 5) & global_atomic_inc CAS loop (hi = 0xFFFF_FFFF) -> dropped & terminates cleanly + // 8. Out-of-UMA global_atomic_add (hi = base_hi + outside + 1) & global_atomic_inc CAS loop (hi = 0xFFFF_FFFF) -> dropped & terminates cleanly glbl("GLOBAL_ATOMIC_ADD", &[("VDST", 10), ("ADDR", 8), ("DATA", 1), ("SADDR", 0x7D), ("OFFSET", 0), ("GLC", 1)]), glbl("GLOBAL_ATOMIC_INC", &[("VDST", 11), ("ADDR", 12), ("DATA", 1), ("SADDR", 0x7D), ("OFFSET", 0), ("GLC", 1)]), - // 9. Out-of-UMA buffer_atomic_add with V# hi = base_hi + 4 (would alias w0 + 0x3000) -> dropped + // 9. Out-of-UMA buffer_atomic_add with V# hi = base_hi + outside (would alias w0 + 0x3000) -> dropped ("BUFFER_ATOMIC_ADD", "ENC_MUBUF", vec![("VDATA", 14), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("IDXEN", 1), ("GLC", 1)], None), // s[16:19] // 10. 64-bit carry on global_load: s[20:21] = (w0 + 0xFFFF_FFF0) + v16 (0x20) -> w1 + 0x10 -> must read 0x99AA_BBCC (v17) glbl("GLOBAL_LOAD_DWORD", &[("VDST", 17), ("ADDR", 16), ("SADDR", 20), ("OFFSET", 0)]), @@ -3959,17 +3989,23 @@ fn memory_windows_reject_out_of_uma_aliasing(heap: bool) { ]; let refs: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); let p = prog(&refs); - let t = translate(&p, &on_gpu(Options { mem_windows: 2, heap_windows: heap, ..Default::default() })).unwrap(); + let t = translate(&p, &on_gpu(Options { mem_windows: windows, heap_windows: heap, ..Default::default() })).unwrap(); assert_eq!(t.hlsl.contains("switch (xcl_w)"), !heap); let hlsl_dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", t.hlsl)).unwrap(); - let dxil = direct_or(&p, &on_gpu(Options { mem_windows: 2, heap_windows: heap, ..Default::default() }), hlsl_dxil); + let dxil = if windows > 4 && runner::direct_dxil() { + crate::gpu::dxil::kernel::translate(&p, crate::gpu::dxil::kernel::KernelOptions { + wave64: true, split: runner::split_wave64(), mem_windows: windows | super::HEAP_WINDOWS, sampler_base: 0, + }).expect("expanded direct DXIL") + } else { + direct_or(&p, &on_gpu(Options { mem_windows: windows, heap_windows: heap, ..Default::default() }), hlsl_dxil) + }; let mut st = KernelState::default(); - st.sgpr[2..4].copy_from_slice(&[0x1000, base_hi + 4]); // s[2:3]: out-of-UMA hi = base_hi + 4 + st.sgpr[2..4].copy_from_slice(&[0x1000, base_hi + outside]); // s[2:3]: out-of-UMA hi = base_hi + outside st.sgpr[4..6].copy_from_slice(&[0x4000, 0]); // s[4:5]: hi == 0 32-bit descriptor read - st.sgpr[8..12].copy_from_slice(&[0x1000, ((base_hi + 5) & 0xFFFF) | (4 << 16), 64, 0]); // out-of-UMA V# (aliases w1 under & 3) + st.sgpr[8..12].copy_from_slice(&[0x1000, ((base_hi + outside + 1) & 0xFFFF) | (4 << 16), 64, 0]); // out-of-UMA V# (aliases w1 under & 3) st.sgpr[12..16].copy_from_slice(&[0x5000, ((base_hi + 1) & 0xFFFF) | (4 << 16), 64, 0]); // valid strided V# in w1 - st.sgpr[16..20].copy_from_slice(&[0x3000, ((base_hi + 4) & 0xFFFF) | (4 << 16), 64, 0]); // out-of-UMA atomic V# (aliases w0 under & 3) + st.sgpr[16..20].copy_from_slice(&[0x3000, ((base_hi + outside) & 0xFFFF) | (4 << 16), 64, 0]); // out-of-UMA atomic V# (aliases w0 under & 3) st.sgpr[20..22].copy_from_slice(&[0xFFFF_FFF0, base_hi]); // carry base st.sgpr[22..24].copy_from_slice(&[0, base_hi]); // borrow base (0 - 16 borrows into base_hi - 1) for r in st.vgpr.iter_mut() { @@ -3980,11 +4016,11 @@ fn memory_windows_reject_out_of_uma_aliasing(heap: bool) { r[11] = 0x7777_7777; r[14] = 0xBAD0_DEAD; r[4] = 0x1000; - r[5] = base_hi + 4; // out-of-UMA global_store (aliases w0 under & 3) + r[5] = base_hi + outside; // out-of-UMA global_store (aliases w0 under & 3) r[6] = 0x2000; r[7] = 0; // hi == 0 global_store (null pointer -> must be dropped) r[8] = 0x2000; - r[9] = base_hi + 5; // out-of-UMA global_atomic_add (aliases w1 under & 3) + r[9] = base_hi + outside + 1; // out-of-UMA global_atomic_add (aliases w1 under & 3) r[12] = 0x3000; r[13] = 0xFFFF_FFFF; // out-of-UMA global_atomic_inc CAS loop r[16] = 0x20; // carry offset: 0xFFFF_FFF0 + 0x20 = 0x1_0000_0010 -> w1 + 0x10 @@ -4008,6 +4044,7 @@ fn memory_windows_reject_out_of_uma_aliasing(heap: bool) { assert_eq!(*word(w1 + 0x2000), CANARY1, "out-of-UMA global_atomic_add must not clobber w1"); assert_eq!(*word(w0 + 0x3000), CANARY0, "out-of-UMA buffer_atomic_add must not clobber w0"); assert_eq!(*word(w1 + 0x3000), CANARY1, "out-of-UMA global_atomic_inc must not clobber w1"); + println!("INVALID windows={windows} heap={heap} adapter={} checks passed", runner::adapter().unwrap()); } /// The start-up check of the coherent UMA windows (`real::check_uma_import`): the GPU sees what diff --git a/crates/xcl/src/gpu/uma.rs b/crates/xcl/src/gpu/uma.rs index 7592d044b0..fffb5e4690 100644 --- a/crates/xcl/src/gpu/uma.rs +++ b/crates/xcl/src/gpu/uma.rs @@ -1157,7 +1157,10 @@ mod tests { // HLSL path accepts 8/16 with heap_windows, rejects without. assert!(recomp::translate(&code, &Options { mem_windows: w, heap_windows: false, ..Default::default() }).is_err()); let t = recomp::translate(&code, &Options { mem_windows: w, heap_windows: true, ..Default::default() }).unwrap(); - assert!(t.hlsl.contains(&format!("xcl_w >= {w}u")), "HLSL must range-check against {w} windows"); + // no guard branch: an index outside the windows takes the null view at MEM_HEAP_NULL + let null = recomp::MEM_HEAP_NULL - recomp::MEM_HEAP_BASE; + let want = if w >= null { format!("min(xcl_w, {null}u)") } else { format!("select(xcl_w < {w}u, xcl_w, {null}u)") }; + assert!(t.hlsl.contains(&want) && !t.hlsl.contains("xcl_w >= "), "HLSL must clamp to {w} windows: {want}"); assert!(!t.hlsl.contains("mem4"), "root signature ABI stays at 4 root UAVs"); // Direct DXIL path validates cleanly with 8/16 | HEAP_WINDOWS. translate(&code, KernelOptions { wave64: true, split: false, mem_windows: w | HEAP_WINDOWS, sampler_base: 0 }).unwrap(); diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 14d4f3695a..248a198856 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -167,8 +167,9 @@ pub unsafe fn shared_heap(dev: &ID3D12Device) -> bool { inc: dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV), heap, cap, - // the memory windows' raw UAVs (`set_window_views`) come first - next: crate::gpu::recomp::MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32, + // the memory windows' raw UAVs and the null view after them (`set_window_views`) + // come first + next: crate::gpu::recomp::MEM_HEAP_NULL + 1, free: Vec::new(), }); } @@ -212,10 +213,11 @@ pub fn window_views_failed() -> bool { /// shared heap, made now if need be (`Options::heap_windows`: shaders take window k from there). /// Active windows (`bytes > 0`) in wider layouts (8 / 16) must all have a valid buffer; unused /// trailing slots (`(0, 0)`) safely alias window 0's buffer. -pub unsafe fn set_window_views(dev: &ID3D12Device, windows: &[(usize, usize)]) -> bool { +/// `MEM_HEAP_NULL` after the windows is a null view; heap views require 64-bit atomics. +pub unsafe fn set_window_views(dev: &ID3D12Device, windows: &[(usize, usize)], atomics64: bool) -> bool { let count = windows.len().max(crate::gpu::uma::MAX_WINDOWS); let complete = count <= crate::gpu::uma::MAX_WINDOWS || windows.iter().all(|&(raw, bytes)| raw != 0 || bytes == 0); - let ok = count <= crate::gpu::uma::MAX_HEAP_WINDOWS && complete && windows.first().is_some_and(|w| w.0 != 0) && shared_heap(dev) && { + let ok = atomics64 && count <= crate::gpu::uma::MAX_HEAP_WINDOWS && complete && windows.first().is_some_and(|w| w.0 != 0) && shared_heap(dev) && { let (device, cpu, inc) = { let g = GLOBAL.lock().unwrap(); let s = g.as_ref().unwrap(); @@ -229,11 +231,13 @@ pub unsafe fn set_window_views(dev: &ID3D12Device, windows: &[(usize, usize)]) - let at = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: (cpu + (crate::gpu::recomp::MEM_HEAP_BASE as u64 + k as u64) * inc as u64) as usize }; device.CreateUnorderedAccessView(r, None::<&ID3D12Resource>, Some(&crate::gpu::recomp::runner::raw_window_uav(bytes as u64)), at); } + let at = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: (cpu + crate::gpu::recomp::MEM_HEAP_NULL as u64 * inc as u64) as usize }; + device.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&crate::gpu::recomp::runner::raw_window_uav(0)), at); true }; if !ok { WINDOW_VIEWS_FAILED.store(true, std::sync::atomic::Ordering::Relaxed); - crate::xlog_warn!("memory windows: could not bind {count} window(s) in the shared descriptor heap"); + crate::xlog_warn!("memory windows: could not bind {count} window(s) (64-bit atomics on heap views: {atomics64}) in the shared descriptor heap"); } ok } @@ -654,21 +658,21 @@ mod tests { dev: dev.clone(), heap: ID3D12DescriptorHeap::from_raw(&mut heap as *mut _ as *mut c_void), cpu: 0x1000, gpu: 0x2000, inc: 32, cap: 1024, - next: crate::gpu::recomp::MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32, free: Vec::new(), + next: crate::gpu::recomp::MEM_HEAP_NULL + 1, free: Vec::new(), }); let window = (&mut resource as *mut _ as usize, 0x10000); - assert!(set_window_views(&dev, &[window])); - assert_eq!(CALLS.swap(0, Ordering::Relaxed), crate::gpu::uma::MAX_WINDOWS as u32); + assert!(set_window_views(&dev, &[window], true)); + assert_eq!(CALLS.swap(0, Ordering::Relaxed), crate::gpu::uma::MAX_WINDOWS as u32 + 1); let mut windows = [window; crate::gpu::uma::MAX_HEAP_WINDOWS]; - assert!(set_window_views(&dev, &windows)); - assert_eq!(CALLS.swap(0, Ordering::Relaxed), windows.len() as u32); + assert!(set_window_views(&dev, &windows, true)); + assert_eq!(CALLS.swap(0, Ordering::Relaxed), windows.len() as u32 + 1); // Unused trailing window `(0, 0)` safely aliases window 0; an active window that failed // to open `(0, bytes > 0)` is rejected. windows[7] = (0, 0); - assert!(set_window_views(&dev, &windows)); - assert_eq!(CALLS.swap(0, Ordering::Relaxed), windows.len() as u32); + assert!(set_window_views(&dev, &windows, true)); + assert_eq!(CALLS.swap(0, Ordering::Relaxed), windows.len() as u32 + 1); windows[7] = (0, 0x10000); - assert!(!set_window_views(&dev, &windows)); + assert!(!set_window_views(&dev, &windows, true)); assert_eq!(CALLS.load(Ordering::Relaxed), 0); assert!(UNLOCKED.load(Ordering::Relaxed), "device callback ran while GLOBAL was locked"); *GLOBAL.lock().unwrap() = None; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 9241a3a18d..88b52ea3b1 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -370,7 +370,9 @@ unsafe fn open_uma(device: &ID3D12Device) { // shaders take the windows from the shared descriptor heap (`pso::heap_windows`) if n > 1 && crate::gpu::recomp::pso::heap_windows() { let views: Vec<(usize, usize)> = bufs.iter().enumerate().map(|(k, b)| (b.0, u.window(k).1)).collect(); - if super::console_heap::set_window_views(device, &views) { + let mut o11 = D3D12_FEATURE_DATA_D3D12_OPTIONS11::default(); + let atomics64 = device.CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS11, &mut o11 as *mut _ as *mut c_void, std::mem::size_of_val(&o11) as u32).is_ok() && o11.AtomicInt64OnDescriptorHeapResourceSupported.as_bool(); + if super::console_heap::set_window_views(device, &views, atomics64) { crate::xlog_info!("D3D12.X: memory windows from the shared descriptor heap (no per-access switch)"); } }