From fbe8ecc617b13ee51350ed2ec37e57d418c67f56 Mon Sep 17 00:00:00 2001 From: local Date: Wed, 25 Nov 2026 04:53:33 -0600 Subject: [PATCH] Stores by address are bounded: title buffers take the builder's only where it was given them to write A directory entry says whether stores by address reach its page (translator and host). Faults take at most 128 pages a frame and 8,192 in all; every slot of the copies' run holds a null view until its copy is there; a page is looked at once a frame. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/draw.rs | 12 +- crates/victory-gpu/src/memory.rs | 126 +++++++++++++++----- crates/victory-shader/src/emit/intersect.rs | 2 +- crates/victory-shader/src/emit/lower.rs | 34 ++++-- crates/victory-shader/src/emit/mod.rs | 2 +- crates/victory-shader/src/emit/shader.rs | 6 +- 6 files changed, 136 insertions(+), 46 deletions(-) diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index b075c97..7efd5ac 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -1061,6 +1061,16 @@ impl State { h => crate::descriptor::heap(h as This).host(), }?; let (mut addresses, mut constants) = (Vec::new(), Vec::new()); + // (What it may write: its root views of unordered access.) + let written: Vec = t + .used + .iter() + .filter(|u| u.kind == ResourceType::Uav && u.root == RootType::Descriptor) + .filter_map(|u| match self.roots_ref(compute).get(usize::from(u.slot)) { + Some(&Root::View(address)) => Some(address), + _ => None, + }) + .collect(); for root in self.roots_ref(compute) { match root { // (Of a program that only walks structures, the structures' @@ -1075,7 +1085,7 @@ impl State { addresses.push(self.view_address(compute, u.slot, u.dword)); } } - crate::memory::ready(list, heap, &addresses, &constants, use_) + crate::memory::ready(list, heap, &addresses, &written, &constants, use_) } /// The bound heap holding `handle`. diff --git a/crates/victory-gpu/src/memory.rs b/crates/victory-gpu/src/memory.rs index 1e4a952..a1168c5 100644 --- a/crates/victory-gpu/src/memory.rs +++ b/crates/victory-gpu/src/memory.rs @@ -32,7 +32,7 @@ //! stage, so before one of its dispatches the host makes ready what it can //! see the dispatch name: //! -//! - 1 MiB from each root view outside buffers, and where title memory has a +//! - 256 KiB from each root view outside buffers, and where title memory has a //! packed ("dehydrated") structure there, all of it: the title ships its //! structures packed, and the builder's rehydration stages unpack each in //! place once it is streamed in; @@ -53,6 +53,11 @@ //! card holds them unpacked (run light-3: a quarter of the structures //! unpacked a second time, into garbage all over their neighbours). //! +//! **Stores are bounded**: a structure the builder misreads makes it store +//! anywhere. Pages of title buffers take its stores only once one of its +//! dispatches was given that buffer as an unordered access view (its scratch +//! memory, its results); what else a store reaches is an arena's. +//! //! shortcut: a page is kept for the rest of the run. //! shortcut: pages a list does not name are looked at again in turn, over //! [`SWEEP`] frames. @@ -60,7 +65,7 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; -use victory_shader::emit::{MEMORY_FAULTS, MEMORY_PAGES}; +use victory_shader::emit::{MEMORY_FAULTS, MEMORY_PAGES, MEMORY_READ_ONLY}; use windows::Win32::Graphics::Direct3D12::*; use crate::descriptor::host::HostHeap; @@ -84,14 +89,17 @@ const PACKED: u64 = 64 << 20; /// A packed structure not named for so many frames is another one. const AGAIN: u64 = 8; /// From a root view; from an address in constants. -const VIEW: u64 = 1 << 20; +const VIEW: u64 = 256 << 10; const BUILT: u64 = 4 << 20; /// The biggest title buffer whose builder results come back. const BACK: u64 = 256 << 10; -/// Pages taken from the faults of one frame, at most. -const FAULTS: usize = 2048; +/// Pages taken from the faults, of one frame and in all, at most: a ray in a +/// structure that is not right goes anywhere, and 2,048 a frame took all the +/// pages there are in the first seconds (run light-4). +const FAULTS: usize = 128; +const FAULTS_ALL: usize = 8192; /// Frames over which every page is looked at once. -const SWEEP: usize = 32; +const SWEEP: usize = 128; /// What a draw or dispatch does with title memory by address. #[derive(Clone, Copy, PartialEq)] @@ -109,6 +117,8 @@ struct Held { copy: usize, /// The write-watch asking after which it was last taken (0: never). taken: u64, + /// The frame it was last looked at in. + looked: u64, } struct Memory { @@ -131,6 +141,10 @@ struct Memory { windows: usize, /// Packed structures the builder came to: the frame it last did. packed: HashMap, + /// Copies of title buffers the builder may store in. + open: std::collections::HashSet, + /// Pages taken from faults. + faulted: usize, /// Per host view heap (by address): the first slot of its run, and how /// many copies have their view there. heaps: HashMap, @@ -199,6 +213,8 @@ impl Memory { sweep: 0, windows: 0, packed: HashMap::new(), + open: Default::default(), + faulted: 0, heaps: HashMap::new(), frame: u64::MAX, refused: Default::default(), @@ -221,7 +237,7 @@ impl Memory { return None; } // SAFETY: an entry of the directory. - (unsafe { self.entry(page).read() } as usize).checked_sub(1) + ((unsafe { self.entry(page).read() } & !MEMORY_READ_ONLY) as usize).checked_sub(1) } /// Whether `page` can be given a place: in the directory, not turned down @@ -293,12 +309,15 @@ impl Memory { let entry = self.entry(at / PAGE); // SAFETY: an entry of the directory. if unsafe { entry.read() } == 0 { - fill(entry, n, at - first); + close(entry, n, at - first); } at += PAGE; } } - fill(self.entry(page), n, start - shadow.span().0); + // SAFETY: as above. + if unsafe { self.entry(page).read() } == 0 { + close(self.entry(page), n, start - shadow.span().0); + } return true; } // Small title buffers in it: what the builder stores in them comes @@ -336,12 +355,34 @@ impl Memory { }; let offset = self.used * PAGE; self.used += 1; - self.pages.insert(page, Held { at: arena + offset as usize, copy, taken: 0 }); + self.pages.insert(page, Held { at: arena + offset as usize, copy, taken: 0, looked: u64::MAX }); self.order.push(page); fill(self.entry(page), copy, offset); true } + /// A dispatch of the builder was given `address` to write: if it is in a + /// title buffer's copy, stores by address reach that from here on. + fn opens(&mut self, address: u64) { + let Some(n) = self.map(address / PAGE).then(|| self.copy_of(address / PAGE)).flatten() else { + return; + }; + if self.pages.contains_key(&(address / PAGE)) || !self.open.insert(n) { + return; + } + let (first, len) = self.copies[n].span(); + tracing::info!( + start = format_args!("{first:#x}"), + len, + "title memory by address: the structure builder may store in a title buffer" + ); + for page in first.div_ceil(PAGE)..(first + len) / PAGE { + if self.copy_of(page) == Some(n) { + fill(self.entry(page), n, page * PAGE - first); + } + } + } + /// [`Memory::map`] of the pages of `[address, address + len)`, up to the /// first that is not the title's, and what the title wrote there since /// they were taken goes into the arenas. @@ -357,7 +398,9 @@ impl Memory { /// Takes the pages `[first, end)` that are in arenas from title memory /// where it wrote them. fn bring(&mut self, first: u64, end: u64) { - if first >= end { + // Once a frame: the builder names the same pages list after list. + let frame = self.frame; + if !(first..end).any(|page| self.pages.get(&page).is_some_and(|h| h.looked != frame)) { return; } let started = std::time::Instant::now(); @@ -366,6 +409,10 @@ impl Memory { let Some(held) = self.pages.get_mut(&page) else { continue; }; + if held.looked == frame { + continue; + } + held.looked = frame; self.stats.looked += 1; let wrote = answer .as_ref() @@ -396,18 +443,19 @@ impl Memory { /// If title memory has a packed structure at `address` that the builder /// comes to anew: all of it has its pages, and goes up as it is there. - fn unpacks(&mut self, address: u64) { + /// Whether there is a packed structure. + fn unpacks(&mut self, address: u64) -> bool { if address & 0xFF != 0 || !self.possible(address / PAGE) { - return; + return false; } let mut header = [0u8; 8]; crate::host::copy_mapped(header.as_mut_ptr(), address as *const u8, 8, false); let Some(len) = packed_len(u64::from_le_bytes(header)) else { - return; + return false; }; let last = self.packed.insert(address, self.frame); if last.is_some_and(|f| f + AGAIN >= self.frame) { - return; + return true; } self.ready(address, len); let mut title = [0u8; PIECE]; @@ -426,6 +474,7 @@ impl Memory { at += n as u64; } self.stats.packed += 1; + true } /// The pages shaders found empty since the last frame. @@ -449,8 +498,9 @@ impl Memory { pages.sort_unstable(); let mut made = 0; for &page in &pages { - if made < FAULTS && self.copy_of(page).is_none() && self.map(page) { + if made < FAULTS && self.faulted < FAULTS_ALL && self.copy_of(page).is_none() && self.map(page) { made += 1; + self.faulted += 1; self.bring(page, page + 1); } } @@ -546,6 +596,11 @@ impl Memory { } } +/// [`fill`] of a page stores by address do not reach. +fn close(entry: *mut u32, n: usize, offset: u64) { + fill(entry, n | MEMORY_READ_ONLY as usize, offset); +} + /// The offset first: a shader that sees the number sees it too. fn fill(entry: *mut u32, n: usize, offset: u64) { // SAFETY: an entry of the directory, under the mutex. @@ -585,11 +640,19 @@ fn merge(title: &[u8; PIECE], ours: &mut [u8], never: bool) -> usize { } /// Before a draw whose shader reads title memory by address: what its -/// root arguments name is made ready (`addresses`: its root views; -/// `constants`: its root constants, see the module), every numbered copy +/// root arguments name is made ready (`addresses`: its root views, of them +/// `written` those it writes; `constants`: its root constants, see the +/// module), every numbered copy /// has its view in `heap`'s run and is brought up to date when `list` is /// submitted. None if the host cannot hold it; the draw is then dropped. -pub(crate) fn ready(list: &HostList, heap: &HostHeap, addresses: &[u64], constants: &[u32], use_: Use) -> Option { +pub(crate) fn ready( + list: &HostList, + heap: &HostHeap, + addresses: &[u64], + written: &[u64], + constants: &[u32], + use_: Use, +) -> Option { let mut guard = MEMORY.lock().unwrap_or_else(|e| e.into_inner()); if guard.is_none() { *guard = Memory::new(); @@ -639,20 +702,20 @@ pub(crate) fn ready(list: &HostList, heap: &HostHeap, addresses: &[u64], constan } for &at in addresses.iter().filter(|&&a| a >= 1 << 32) { // (Several structures to a page.) - if builds && list.first_use(usize::MAX - (3 << 33) - (at >> 8) as usize) { - m.unpacks(at); + let packed = builds && list.first_use(usize::MAX - (3 << 33) - (at >> 8) as usize) && m.unpacks(at); + if builds && written.contains(&at) && named(4, at) { + if let Some((start, len)) = crate::shadow::in_buffer(at, 1) + && len <= BACK + && use_ == (Use::Builds { back: true }) + { + m.small(list, start, len); + } + m.opens(at); } - if !named(2, at) { + if packed || !named(2, at) { continue; } match crate::shadow::in_buffer(at, 1) { - Some((start, len)) if builds && len <= BACK => { - if use_ == (Use::Builds { back: true }) { - m.small(list, start, len); - } - m.ready(at, 1); - m.named_in_upload(at); - } Some(_) => { m.ready(at, 1); m.named_in_upload(at); @@ -666,7 +729,10 @@ pub(crate) fn ready(list: &HostList, heap: &HostHeap, addresses: &[u64], constan None => { // Held for the heap's life: not given back with a list's slots. let base = heap.dynamic(COPIES + 1)?; - heap.null_raw_uav_at(base); + // Every slot of the run: a number shows no view of another kind. + for n in 0..=COPIES { + heap.null_raw_uav_at(base + n); + } (base, 0) } }; diff --git a/crates/victory-shader/src/emit/intersect.rs b/crates/victory-shader/src/emit/intersect.rs index e1043f3..b5335cb 100644 --- a/crates/victory-shader/src/emit/intersect.rs +++ b/crates/victory-shader/src/emit/intersect.rs @@ -244,7 +244,7 @@ impl<'a> Lower<'a> { let at = self.s.m.select(c, inside, at, outside); return (handle, at); } - self.memory(low, high, guard) + self.memory(low, high, guard, false) } /// `count` dwords of the node from its byte `base` + `offset` (`base` diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 319b20e..38fab3b 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -1783,7 +1783,7 @@ impl<'a> Lower<'a> { if let Some(bias) = self.root_bias(a.param) { off = self.s.m.bin(c, Bin::Add, off, bias); } - match self.root_memory(&src, off, guard) { + match self.root_memory(&src, off, guard, false) { Some((h, byte)) => { off = byte; Some(h) @@ -3013,8 +3013,10 @@ impl<'a> Lower<'a> { /// (and drops writes) through the null view at the first slot, and its /// number is left for the host, for lanes that run (`guard`). Goes on in /// another block. - fn memory(&mut self, lo: V, hi: V, guard: Option) -> (V, V) { - use super::shader::{MEMORY_FAULTS, MEMORY_PAGES, SLOT_FAULTS, SLOT_MEMORY, USER_MEMORY_BASE}; + /// With `store`, a page that is not to be written ([`super::shader::MEMORY_READ_ONLY`]) + /// is the null view. + fn memory(&mut self, lo: V, hi: V, guard: Option, store: bool) -> (V, V) { + use super::shader::{MEMORY_FAULTS, MEMORY_PAGES, MEMORY_READ_ONLY, SLOT_FAULTS, SLOT_MEMORY, USER_MEMORY_BASE}; let c = self.cur; let (i32t, res) = (self.s.t.i32, self.s.t.res_ret); let k = |l: &mut Self, v: u32| l.s.m.i32(v); @@ -3043,7 +3045,9 @@ impl<'a> Lower<'a> { &[directory, at, iu, both, align], ); let (copy, start) = (self.s.m.extract(c, entry, 0), self.s.m.extract(c, entry, 1)); - let (copy, start) = (self.as_int(copy), self.as_int(start)); + let (number, start) = (self.as_int(copy), self.as_int(start)); + let mask = k(self, !MEMORY_READ_ONLY); + let copy = self.s.m.bin(c, Bin::And, number, mask); // A page without a copy: its number, at a place by the number. In a // branch: the notes lie in the PC's memory, and a store there with // every node of every ray would be most of what a trace does. @@ -3073,6 +3077,12 @@ impl<'a> Lower<'a> { ); self.end_guarded(noting, &[]); let c = self.cur; + let copy = if store { + let closed = self.s.m.cmp(c, pred::SLT, number, zero); + self.s.m.select(c, closed, zero, copy) + } else { + copy + }; let base = self.host_word(RootType::Other(USER_MEMORY_BASE), 0, 0); let slot = self.s.m.bin(c, Bin::Add, base, copy); // The builder's threads read what others of the dispatch wrote (a @@ -3112,7 +3122,7 @@ impl<'a> Lower<'a> { /// another block. /// shortcut: an access of several dwords is taken not to cross the end /// of a host copy (the builder's records lie on their size). - fn root_memory(&mut self, src: &Source, off: V, guard: Option) -> Option<(V, V)> { + fn root_memory(&mut self, src: &Source, off: V, guard: Option, store: bool) -> Option<(V, V)> { let (lo, hi) = self.root_address(src)?; let c = self.cur; let i32t = self.s.t.i32; @@ -3123,7 +3133,7 @@ impl<'a> Lower<'a> { let sign = self.s.m.bin(c, Bin::AShr, off, k31); let hi = self.s.m.bin(c, Bin::Add, hi, carry); let hi = self.s.m.bin(c, Bin::Add, hi, sign); - let (h, byte) = self.memory(sum, hi, guard); + let (h, byte) = self.memory(sum, hi, guard, store); self.root_copy = Some(h); Some((h, byte)) } @@ -3173,7 +3183,7 @@ impl<'a> Lower<'a> { let Some(Operand::Vector { reg, .. }) = *dst else { return Err("a global load result"); }; - let (h, byte) = self.memory(lo, hi, guard); + let (h, byte) = self.memory(lo, hi, guard, false); let c = self.cur; let mask = self.s.m.i8((1u8 << n) - 1); let r = self.s.op( @@ -3195,7 +3205,7 @@ impl<'a> Lower<'a> { let n = count(rest)?; let d = data.as_ref().ok_or("global store data")?; let vals = self.vgprs(inst, d, n)?; - let (h, byte) = self.memory(lo, hi, guard); + let (h, byte) = self.memory(lo, hi, guard, true); let mut args = vec![h, byte, iu]; args.extend(&vals); args.resize(3 + 4, iu); @@ -3222,7 +3232,7 @@ impl<'a> Lower<'a> { Some(x) => self.i32_of(x)?, None => self.s.m.i32(0), }; - let (h, byte) = self.memory(lo, hi, guard); + let (h, byte) = self.memory(lo, hi, guard, true); let c = self.cur; // DXIL's atomic operations: add 0, and 1, or 2, xor 3, signed // minimum 4 and maximum 5, unsigned 6 and 7, exchange 8. @@ -3357,7 +3367,7 @@ impl<'a> Lower<'a> { for first in (0..count).step_by(4) { let n = (count - first).min(4); let (lo, hi) = self.address_plus(lo, hi, offset + 4 * i32::from(first)); - let (h, byte) = self.memory(lo, hi, None); + let (h, byte) = self.memory(lo, hi, None, false); let c = self.cur; let mask = self.s.m.i8((1u8 << n) - 1); let r = self.s.op( @@ -6004,7 +6014,7 @@ impl<'a> Lower<'a> { return Ok(()); } if matches!(kind, Some(u) if u.kind == ResourceType::Srv) { - let h = match self.root_memory(src, addr, None) { + let h = match self.root_memory(src, addr, None, false) { Some((h, byte)) => { addr = byte; h @@ -11279,7 +11289,7 @@ impl<'a> Lower<'a> { off = self.s.m.bin(c, Bin::Add, element, off); index = None; } - if let Some((_, byte)) = self.root_memory(src, off, guard) { + if let Some((_, byte)) = self.root_memory(src, off, guard, store || atomic.is_some()) { off = byte; } } diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index e23971f..2f06eac 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -117,7 +117,7 @@ pub use sampler::StaticSampler; pub use lower::{host_settings, set_host_max_wave}; pub use rect::rect_list_gs; pub use shader::{ - MEMORY_FAULTS, MEMORY_PAGES, ROOT_CBV_SPACE, SAMPLER_SPACE, SLOT_FAULTS, SLOT_MEMORY, SLOT_STRIDES, + MEMORY_FAULTS, MEMORY_PAGES, MEMORY_READ_ONLY, ROOT_CBV_SPACE, SAMPLER_SPACE, SLOT_FAULTS, SLOT_MEMORY, SLOT_STRIDES, SLOT_STRIDES_COUNT, USER_DATA_SPACE, USER_MEMORY_BASE, USER_ROOT_ADDRESS, USER_VIEW_ADDRESS, USER_VIEW_BIAS, raw_views, set_raw_views, set_view_heap_limit, view_heap_limit, }; diff --git a/crates/victory-shader/src/emit/shader.rs b/crates/victory-shader/src/emit/shader.rs index 7ae8baf..d2605dc 100644 --- a/crates/victory-shader/src/emit/shader.rs +++ b/crates/victory-shader/src/emit/shader.rs @@ -37,7 +37,8 @@ pub const SLOT_STRIDES_COUNT: u32 = 1 << 21; /// - the root SRV `t` in [`ROOT_CBV_SPACE`] is a directory of /// [`MEMORY_PAGES`] entries of two dwords, one per 64 KiB of title /// address space: the number of the host copy holding that page (0: none -/// yet) and the page's byte offset in that copy; +/// yet; with [`MEMORY_READ_ONLY`]: stores there are dropped) and the page's +/// byte offset in that copy; /// - copy `n` is the raw buffer view at view-heap slot `base + n`, `base` /// the user-data dword of kind [`USER_MEMORY_BASE`]; slot `base` itself /// reads zeros and drops writes; @@ -49,6 +50,9 @@ pub const SLOT_MEMORY: u8 = 254; pub const SLOT_FAULTS: u8 = 253; /// Entries of the [`SLOT_MEMORY`] directory: title addresses below 128 GiB. pub const MEMORY_PAGES: u32 = 1 << 21; +/// In a directory entry's first dword: shaders do not store there by address +/// (a title buffer the structure builder has not been given to write). +pub const MEMORY_READ_ONLY: u32 = 1 << 31; /// Dwords of the [`SLOT_FAULTS`] buffer. pub const MEMORY_FAULTS: u32 = 4096; /// `RootType::Other` code of a user-data dword the host computes: the view