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
12 changes: 12 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,3 +1468,15 @@ fn raw_loads_in_root_windows_respect_exec_and_bounds() {
}
}
}

/// With heap windows a program selects every window descriptor of the shared heap, as the
/// HLSL accessors do (`recomp::heap_window_tests::heap_windows_select_every_heap_slot`): the
/// slots past the unified block's windows hold null views.
#[test]
fn heap_windows_select_every_heap_slot() {
use super::lower_mem::heap_window_bound;
let slots = crate::gpu::uma::MAX_HEAP_WINDOWS as u32;
for mems in [2usize, 3, 4, 8, 16] {
assert_eq!(heap_window_bound(mems), slots, "{mems} unified windows");
}
}
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct KernelOptions {
/// identity (`recomp::pso`): bump it when `pso::direct_compute` / `direct_graphics` change the
/// containers. An edit of `gpu/dxil` needs no bump, `XCL_DIRECT_DXIL_SOURCE_ID` is its hash
/// (the `gpu/dxil` sources are not in build.rs's translator source hash).
pub const TRANSLATOR_VERSION: u32 = 12;
pub const TRANSLATOR_VERSION: u32 = 13;

/// Console compute shaders (and the kernel tests) go through this translator, with the HLSL
/// path as the fallback for what it cannot translate yet: the default, and `XCL_DIRECT_DXIL=1`.
Expand Down
12 changes: 10 additions & 2 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ pub struct FmtComp1 {
pub dflt: Value,
}

/// Windows a program with heap windows (`Lw::heap_mem`) may select: every window descriptor
/// of the shared heap (`MEM_HEAP_BASE`.., `uma::MAX_HEAP_WINDOWS`), the unified block's and the
/// null views or mapped memory after them (`console_heap::set_window_views`) – the same bound as
/// the HLSL path's accessors (`recomp::emit::mem_accessors`).
pub(crate) fn heap_window_bound(mems: usize) -> u32 {
(mems as u32).max(crate::gpu::uma::MAX_HEAP_WINDOWS as u32)
}

impl Lw {
/// Runs `f` on the active memory window (`mem`, `mem1`.. at u2.., or descriptor heap
/// when `self.heap_mem` is set via `XCL_HEAP_WINDOWS`), returning `out_tys.len()` values
Expand All @@ -173,7 +181,7 @@ impl Lw {
// Descriptor-heap windows (`#1693`, `XCL_HEAP_WINDOWS=1`): single guarded heap lookup
// combined with `guard`, stacking cleanly with vector loads/stores & FormatLoad opt.
if let Some(coherent) = self.heap_mem {
let windows = self.mems.len() as u32;
let windows = heap_window_bound(self.mems.len());
let w = self.rd(super::lower::VAR_WIN);
let inside = self.cmpc(Pred::ULt, w, windows);
let cond = match guard {
Expand Down Expand Up @@ -413,7 +421,7 @@ impl Lw {
return vec![z; n];
}
let w = self.rd(super::lower::VAR_WIN);
let inside = self.cmpc(Pred::ULt, w, self.mems.len() as u32);
let inside = self.cmpc(Pred::ULt, w, heap_window_bound(self.mems.len()));
let ok = match guard {
Some(g) if self.s.m.const_u64(g) != Some(1) => self.and(g, inside),
_ => inside,
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6076,6 +6076,9 @@ fn method_switch(windows: u32, w: &str, ret: &str, name: &str, args: &str, def:
fn mem_accessors(windows: u32, heap: Option<&str>) -> String {
let mut s = String::new();
if let Some(coherent) = heap {
// every window descriptor of the shared heap, not only the unified block's: the slots
// past it hold null views (`console_heap::set_window_views`) or memory XCL maps there
let windows = windows.max(crate::gpu::uma::MAX_HEAP_WINDOWS as u32);
// Redirect ordinary out-of-window loads/stores to null raw UAVs without a divergent
// branch. Atomics keep the guard because their out parameter must be set to zero.
let buf = |nul: u32| format!(
Expand Down
27 changes: 27 additions & 0 deletions crates/xcl/src/gpu/recomp/heap_window_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,30 @@ fn heap_windows_graphics_stages_validate() {
}
}
}

/// With heap windows, the HLSL accessors let a program select every window descriptor of the
/// shared heap (`uma::MAX_HEAP_WINDOWS`), whatever its own window count: the slots past the
/// unified block's windows hold null views (`console_heap::set_window_views`), so an address
/// there reads 0 as before. Without heap windows the bound stays the program's own count.
/// (The direct path's bound: `dxil::core_tests::heap_windows_select_every_heap_slot`.)
#[test]
fn heap_windows_select_every_heap_slot() {
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("S_ENDPGM", "ENC_SOPP", &[]));
let slots = crate::gpu::uma::MAX_HEAP_WINDOWS as u32;
for windows in [2u32, 4, 8, slots] {
let t = super::translate(&code, &super::Options { mem_windows: windows, heap_windows: true, ..Default::default() }).unwrap();
// loads and stores select a null view past the bound, atomics branch on it
assert!(t.hlsl.contains(&format!("bool xcl_o = xcl_w >= {slots}u;")), "{windows} windows:\n{}", t.hlsl);
assert!(t.hlsl.contains(&format!("[branch] if (xcl_w >= {slots}u)")), "{windows} windows:\n{}", t.hlsl);
if windows < slots {
assert!(!t.hlsl.contains(&format!("xcl_w >= {windows}u")), "{windows} windows:\n{}", t.hlsl);
}
}
// the root-UAV form: a switch over the program's own 2 windows, nothing past them
let t = super::translate(&code, &super::Options { mem_windows: 2, heap_windows: false, ..Default::default() }).unwrap();
assert!(!t.hlsl.contains("ResourceDescriptorHeap[") && t.hlsl.contains("case 1u: return mem1."), "{}", t.hlsl);
assert!(!t.hlsl.contains("case 2u:"), "{}", t.hlsl);
}
21 changes: 15 additions & 6 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1579,8 +1579,9 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result<Kern
list.ResourceBarrier(&[transition(b, filled, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)]);
}
// the windows as raw UAVs at MEM_HEAP_BASE.. of a shader-visible heap
// (`Options::heap_windows`; programs with the root-UAV switch ignore it)
let win_count = arena.windows.len().clamp(crate::gpu::uma::MAX_WINDOWS, crate::gpu::uma::MAX_HEAP_WINDOWS);
// (`Options::heap_windows`; programs with the root-UAV switch ignore it), null views in
// the slots past the arena's windows, as `console_heap::set_window_views`
let win_count = crate::gpu::uma::MAX_HEAP_WINDOWS;
let dh: ID3D12DescriptorHeap = dev
.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
Expand All @@ -1591,10 +1592,12 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result<Kern
.map_err(e)?;
let inc = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize;
for k in 0..win_count {
let (b, size) = bufs.get(k).map_or((&mem_buf, arena.windows[0].1), |b| (b, arena.windows[k].1));
let mut h = dh.GetCPUDescriptorHandleForHeapStart();
h.ptr += (super::MEM_HEAP_BASE as usize + k) * inc;
dev.CreateUnorderedAccessView(b, None::<&ID3D12Resource>, Some(&raw_window_uav(size as u64)), h);
match (bufs.get(k), arena.windows.get(k)) {
(Some(b), Some(w)) => dev.CreateUnorderedAccessView(b, None::<&ID3D12Resource>, Some(&raw_window_uav(w.1 as u64)), h),
_ => dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(crate::gpu::uma::GRAIN as u64)), h),
}
}
for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] {
let mut h = dh.GetCPUDescriptorHandleForHeapStart();
Expand Down Expand Up @@ -1853,7 +1856,9 @@ fn dispatch_with(
let bound = bind_resources(dev, &list, &[], &[])?;
// A PSO built by the backend's hook uses the runtime's window count: with heap windows
// (`Options::heap_windows`) its accesses go to ResourceDescriptorHeap[MEM_HEAP_BASE + w],
// so every window slot is a raw view of `mem` (no textures here to share the heap with).
// so the root form's windows (MAX_WINDOWS, all `mem` below) are raw views of `mem` and the
// slots past them null views, as `console_heap::set_window_views` (no textures here to
// share the heap with).
let win_count = crate::gpu::uma::MAX_HEAP_WINDOWS;
let dh: ID3D12DescriptorHeap = dev
.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
Expand All @@ -1867,7 +1872,11 @@ fn dispatch_with(
for k in 0..win_count {
let mut h = dh.GetCPUDescriptorHandleForHeapStart();
h.ptr += (super::MEM_HEAP_BASE as usize + k) * inc;
dev.CreateUnorderedAccessView(&mem_buf, None::<&ID3D12Resource>, Some(&raw_window_uav(mem_bytes)), h);
if k < crate::gpu::uma::MAX_WINDOWS {
dev.CreateUnorderedAccessView(&mem_buf, None::<&ID3D12Resource>, Some(&raw_window_uav(mem_bytes)), h);
} else {
dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(crate::gpu::uma::GRAIN as u64)), h);
}
}
for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] {
let mut h = dh.GetCPUDescriptorHandleForHeapStart();
Expand Down
4 changes: 3 additions & 1 deletion crates/xcl/src/gpu/uma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,9 @@ 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");
// heap-window programs select every heap window slot (null views past the layout's)
let slots = MAX_HEAP_WINDOWS as u32;
assert!(t.hlsl.contains(&format!("xcl_w >= {slots}u")), "HLSL must range-check against the {slots} heap window slots ({w} windows)");
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();
Expand Down
25 changes: 17 additions & 8 deletions crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,15 @@ pub fn window_views_failed() -> bool {
/// Active windows (`bytes > 0`) in wider layouts (8 / 16) must all have a valid buffer; a slot
/// without one (`(0, 0)`: a window with no memory) gets a null raw UAV – shaders read 0 there and
/// their stores go nowhere. (It once aliased window 0: what was at such an address read window
/// 0's bytes, and GPU writes there overwrote its descriptor heaps.)
/// 0's bytes, and GPU writes there overwrote its descriptor heaps.) The slots past the layout's
/// windows, up to `MAX_HEAP_WINDOWS`, get null views too: shaders with heap windows may select
/// any of the 16 (`recomp::emit::mem_accessors`, `dxil::lower_mem::heap_window_bound`), and a
/// null view reads 0 and drops writes, as an address outside the windows did before.
pub unsafe fn set_window_views(dev: &ID3D12Device, windows: &[(usize, usize)]) -> 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 layout = windows.len().max(crate::gpu::uma::MAX_WINDOWS);
let complete = layout <= crate::gpu::uma::MAX_WINDOWS || windows.iter().all(|&(raw, bytes)| raw != 0 || bytes == 0);
let count = crate::gpu::uma::MAX_HEAP_WINDOWS;
let ok = layout <= 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();
Expand All @@ -626,7 +630,7 @@ pub unsafe fn set_window_views(dev: &ID3D12Device, windows: &[(usize, usize)]) -
};
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 {} window(s) in the shared descriptor heap", windows.len());
}
ok
}
Expand Down Expand Up @@ -1877,10 +1881,15 @@ mod tests {
next: crate::gpu::recomp::MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32, free: Vec::new(), retired: VecDeque::new(), nulls: None, leases: PublishLeases::default(),
});
let window = (&mut resource as *mut _ as usize, 0x10000);
// the root-UAV slots past the windows there are: null views, not window 0 again
// the layout's windows, then null views up to MAX_HEAP_WINDOWS (not window 0 again)
let heap_slots = crate::gpu::uma::MAX_HEAP_WINDOWS as u32;
let all = (1u32 << heap_slots) - 1;
assert!(set_window_views(&dev, &[window]));
assert_eq!(CALLS.swap(0, Ordering::Relaxed), crate::gpu::uma::MAX_WINDOWS as u32);
assert_eq!(NULLS.swap(0, Ordering::Relaxed), 0b1110);
assert_eq!(CALLS.swap(0, Ordering::Relaxed), heap_slots);
assert_eq!(NULLS.swap(0, Ordering::Relaxed), all & !0b1);
assert!(set_window_views(&dev, &[window; 8]));
assert_eq!(CALLS.swap(0, Ordering::Relaxed), heap_slots);
assert_eq!(NULLS.swap(0, Ordering::Relaxed), all & !0xff);
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);
Expand Down
Loading