Skip to content
Draft
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
41 changes: 33 additions & 8 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5832,15 +5832,28 @@ 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 {
// one access: window k is descriptor MEM_HEAP_BASE + k; an `xcl_w` outside the windows
// does what the switch's `default` does
// 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!(
"{{ bool xcl_o = xcl_w >= {windows}u; {coherent}RWByteAddressBuffer m = ResourceDescriptorHeap[NonUniformResourceIndex(select(xcl_o, {nul}u, {}u + xcl_w))]; ",
super::MEM_HEAP_BASE
);
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)); }}");
let _ = writeln!(s, "uint4 xcl_loadx4(uint4 a) {}return uint4(m.Load(a.x), m.Load(a.y), m.Load(a.z), m.Load(a.w)); }}", buf(super::MEM_NULL_LOAD));
for (ret, name, params, args, def) in mem_methods() {
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}); }}");
if name.starts_with("Load") || name.starts_with("Store") {
let nul = if name.starts_with("Store") { super::MEM_NULL_STORE } else { super::MEM_NULL_LOAD };
let call = if name.starts_with("Store") {
format!("m.{name}(a, {});", &args[3..])
} else {
format!("return m.{name}(a);")
};
let _ = writeln!(s, "{ret} xcl_m{name}({params}) {}{}}}", buf(nul), call);
} else {
let r = if ret == "void" { "" } else { "return " };
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}); }}");
}
}
return s;
}
Expand Down Expand Up @@ -6719,7 +6732,19 @@ uint fclass64(double f) {

#[cfg(test)]
mod tests {
use super::{PRELUDE, PRELUDE_MEM};
use super::{mem_accessors, PRELUDE, PRELUDE_MEM};

#[test]
fn heap_window_loads_and_stores_use_null_descriptors_without_branches() {
let h = mem_accessors(4, Some("globallycoherent "));
let load = format!("select(xcl_o, {}u,", super::super::MEM_NULL_LOAD);
let store = format!("select(xcl_o, {}u,", super::super::MEM_NULL_STORE);
assert!(h.contains(&load), "load accessor did not select the null-load UAV:\n{h}");
assert!(h.contains(&store), "store accessor did not select the null-store UAV:\n{h}");
assert!(h.contains("xcl_mInterlockedAdd(uint a, uint v, out uint o) { [branch] if"), "atomic guard was removed:\n{h}");
assert!(!h.contains("xcl_mLoad(uint a) { [branch]"), "ordinary load kept a divergent guard:\n{h}");
assert!(!h.contains("xcl_mStore(uint a, uint v) { [branch]"), "ordinary store kept a divergent guard:\n{h}");
}

#[test]
fn format_helpers_select_uint4_components_without_runtime_indexing() {
Expand Down
5 changes: 5 additions & 0 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ mod link_tests;
/// (`console_heap::null_views`).
pub const MEM_HEAP_BASE: u32 = 4;

/// Null raw-UAV descriptors just after all window slots. Heap-window loads outside the active
/// layout read zero from `MEM_NULL_LOAD`; stores are redirected to `MEM_NULL_STORE` and dropped.
pub const MEM_NULL_LOAD: u32 = MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32;
pub const MEM_NULL_STORE: u32 = MEM_NULL_LOAD + 1;

/// 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;
Expand Down
14 changes: 12 additions & 2 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result<Kern
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_BASE + win_count as u32).max(super::MEM_NULL_STORE + 1),
Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
NodeMask: 0,
})
Expand All @@ -1544,6 +1544,11 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result<Kern
h.ptr += (super::MEM_HEAP_BASE as usize + k) * inc;
dev.CreateUnorderedAccessView(b, None::<&ID3D12Resource>, Some(&raw_window_uav(size as u64)), h);
}
for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] {
let mut h = dh.GetCPUDescriptorHandleForHeapStart();
h.ptr += idx as usize * inc;
dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(4)), h);
}
list.SetDescriptorHeaps(&[Some(dh.clone())]);
list.SetComputeRootSignature(&gpu.rs);
list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress());
Expand Down Expand Up @@ -1783,7 +1788,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_BASE + win_count as u32).max(super::MEM_NULL_STORE + 1),
Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
NodeMask: 0,
})
Expand All @@ -1794,6 +1799,11 @@ 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);
}
for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] {
let mut h = dh.GetCPUDescriptorHandleForHeapStart();
h.ptr += idx as usize * inc;
dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(4)), h);
}
list.SetDescriptorHeaps(&[Some(dh.clone()), Some(bound._heaps[1].clone())]);
list.SetComputeRootSignature(rs);
list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress());
Expand Down
23 changes: 19 additions & 4 deletions crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,18 @@ pub unsafe fn shared_heap(dev: &ID3D12Device) -> bool {
}
};
crate::xlog_info!("console descriptor heaps: one shader-visible PC heap of {cap} view descriptors for all of them");
null_views(dev, heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
let inc = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
null_views(dev, heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, inc);
null_window_views(dev, heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, inc);
*g = Some(Global {
dev: dev.clone(),
cpu: heap.GetCPUDescriptorHandleForHeapStart().ptr as u64,
gpu: heap.GetGPUDescriptorHandleForHeapStart().ptr,
inc: dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV),
inc,
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 and their two out-of-range null UAVs come first
next: crate::gpu::recomp::MEM_NULL_STORE + 1,
free: Vec::new(),
});
}
Expand Down Expand Up @@ -210,6 +212,19 @@ unsafe fn null_views(dev: &ID3D12Device, cpu: u64, inc: u32) {
}
}

/// Null raw UAVs used by heap-window accessors for out-of-layout loads and stores.
unsafe fn null_window_views(dev: &ID3D12Device, cpu: u64, inc: u32) {
for idx in [crate::gpu::recomp::MEM_NULL_LOAD, crate::gpu::recomp::MEM_NULL_STORE] {
let at = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: (cpu + idx as u64 * inc as u64) as usize };
dev.CreateUnorderedAccessView(
None::<&ID3D12Resource>,
None::<&ID3D12Resource>,
Some(&crate::gpu::recomp::runner::raw_window_uav(4)),
at,
);
}
}

static WINDOW_VIEWS_FAILED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Whether the memory windows could not be put in the shared heap (`set_window_views`):
Expand Down
Loading