GPU: what a submission that missed title memory withheld leaves the copies at once
What lists using title memory write while the frame withholds stays in the
host copies until the frame's end: through the page table, through root
views (byte for byte) and through marked views. When a list of the
submission read or wrote memory with no copy, those results are garbage
computed from zeros (BVH builder nodes: +-inf bounds, 0xffffffff, rising u16
pairs), and later submissions of the same frame read them from the copies:
in run shards20 the meshlet culling read such bytes in its per-batch meshlet
buffers (stale copies with no title-side event after their take; 95.59 % of
the batches walked equal to title memory). Now what such a submission
withheld counts as changed in every buffer copy holding it right after it
completes (`drop_withheld`, which takes the place of `drop_at_frame_end`),
so the next submission's refresh takes it from title memory again. What
submissions that missed nothing withheld keeps the frame-end retake.
Run shards21 (29210929 + the page-table part alone + branch-only
diagnostics, same listing as shards20): one list per world frame wrote
through the page table, every one missed and was retaken at once (34-19,446
pages); stale copies written through the page table are gone, 98.12 % of
13,509 batches walked equal title memory. Every sampled stale batch left
was last written through a declared, unmarked view with no retake after it
(the builder's root UAVs, which the page-table path leaves to the
byte-for-byte path), hence the root and marked views here too.
Test: memory::tests::a_submission_that_missed_leaves_the_copies_at_once
(a write through the page table beside a miss; fails before the change: the
next submission reads the write instead of title memory's bytes).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
56f75f8583
commit
ec9e750eb7
3 files changed
+146
-19
No files matched your search
@@ -1273,9 +1273,7 @@ impl Host {
|
||||
.iter()
|
||||
.map(|list| list.deferred().missed.iter().any(|check| check()))
|
||||
.collect();
|
||||
if missed.contains(&true) {
|
||||
crate::memory::note_missed();
|
||||
}
|
||||
crate::memory::note_submission(missed.contains(&true));
|
||||
let withholding = crate::memory::withholding();
|
||||
for (list, &missed) in runs.iter().zip(&missed) {
|
||||
let deferred = list.deferred();
|
||||
@@ -1314,7 +1312,7 @@ impl Host {
|
||||
} else {
|
||||
shadow.keep_writes(address, len);
|
||||
crate::memory::note_withheld(len as u64);
|
||||
crate::memory::drop_at_frame_end(address, len);
|
||||
crate::memory::drop_withheld(address, len);
|
||||
}
|
||||
}
|
||||
for list in runs {
|
||||
|
||||
@@ -169,10 +169,18 @@ static FRAMES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(
|
||||
static MISSED_IN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(MISS_NEVER);
|
||||
const MISS_NEVER: u64 = u64::MAX;
|
||||
|
||||
/// A list using title memory read or wrote memory with no copy.
|
||||
pub(crate) fn note_missed() {
|
||||
/// Whether a list using title memory in the submission being completed
|
||||
/// read or wrote memory with no copy (completions run one at a time).
|
||||
static SUBMISSION_MISSED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
|
||||
/// A submission's lists ran: whether one using title memory read or wrote
|
||||
/// memory with no copy.
|
||||
pub(crate) fn note_submission(missed: bool) {
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
MISSED_IN.store(FRAMES.load(Relaxed), Relaxed);
|
||||
SUBMISSION_MISSED.store(missed, Relaxed);
|
||||
if missed {
|
||||
MISSED_IN.store(FRAMES.load(Relaxed), Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether what lists using title memory wrote stays out of title memory
|
||||
@@ -197,10 +205,17 @@ fn withheld_in(frame: u64, missed: u64) -> bool {
|
||||
static DROPPED: Mutex<Vec<(usize, usize)>> = Mutex::new(Vec::new());
|
||||
|
||||
/// Shaders wrote `[address, address + len)` of title memory in host copies
|
||||
/// and the frame withheld it: at the frame's end the copies take title
|
||||
/// memory's bytes again ([`retake_dropped`]).
|
||||
pub(crate) fn drop_at_frame_end(address: usize, len: usize) {
|
||||
DROPPED.lock().unwrap_or_else(|e| e.into_inner()).push((address, len));
|
||||
/// and the submission withheld it: the copies take title memory's bytes
|
||||
/// again at their next refresh, from right after the submission if a list
|
||||
/// of it missed (its results would feed the frame's later submissions:
|
||||
/// garbage BVH nodes over meshlet buffers, run shards20), else from the
|
||||
/// frame's end ([`retake_dropped`]).
|
||||
pub(crate) fn drop_withheld(address: usize, len: usize) {
|
||||
if SUBMISSION_MISSED.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
crate::watch::retake(address, len);
|
||||
} else {
|
||||
DROPPED.lock().unwrap_or_else(|e| e.into_inner()).push((address, len));
|
||||
}
|
||||
}
|
||||
|
||||
/// At a frame's end: what the frame withheld leaves the host copies, so
|
||||
@@ -500,7 +515,7 @@ pub(crate) fn bind(list: &HostList, key: usize) -> Option<Map> {
|
||||
let withheld = if pages + regions > 0 || withholding() { writes.len() as u64 } else { 0 };
|
||||
if withheld > 0 {
|
||||
for &(_, at) in &writes {
|
||||
drop_at_frame_end(at as usize, 1 << 16);
|
||||
drop_withheld(at as usize, 1 << 16);
|
||||
}
|
||||
}
|
||||
let mut written = 0;
|
||||
@@ -620,7 +635,7 @@ void main(uint3 id : SV_DispatchThreadID) {
|
||||
}
|
||||
"#;
|
||||
|
||||
fn pipeline() -> (
|
||||
fn pipeline(source: &str) -> (
|
||||
windows::Win32::Graphics::Direct3D12::ID3D12RootSignature,
|
||||
windows::Win32::Graphics::Direct3D12::ID3D12PipelineState,
|
||||
) {
|
||||
@@ -633,8 +648,8 @@ void main(uint3 id : SV_DispatchThreadID) {
|
||||
// SAFETY: the source and names outlive the call; the errors are text.
|
||||
unsafe {
|
||||
let made = D3DCompile(
|
||||
SOURCE.as_ptr().cast(),
|
||||
SOURCE.len(),
|
||||
source.as_ptr().cast(),
|
||||
source.len(),
|
||||
s!("memory"),
|
||||
None,
|
||||
None,
|
||||
@@ -688,7 +703,7 @@ void main(uint3 id : SV_DispatchThreadID) {
|
||||
let (Some(host), Some(views)) = (crate::host::host(), heap()) else {
|
||||
return; // no host GPU here
|
||||
};
|
||||
let (root, pso) = pipeline();
|
||||
let (root, pso) = pipeline(SOURCE);
|
||||
let (out, at) = host.shared_buffer(256).unwrap();
|
||||
// SAFETY: a live buffer.
|
||||
let out_gpu = unsafe { out.GetGPUVirtualAddress() };
|
||||
@@ -767,4 +782,118 @@ void main(uint3 id : SV_DispatchThreadID) {
|
||||
drop(copy);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes `value` at (`lo`, `hi`) through the page table, marking its
|
||||
/// page as translated shaders do, and with `missing_hi` non-zero reads
|
||||
/// (`missing_lo`, `missing_hi`), marking it if no copy holds it.
|
||||
const WRITE: &str = r#"
|
||||
#define RS "DescriptorTable(UAV(u0, numDescriptors=unbounded)), SRV(t0, space=2), UAV(u0, space=2), RootConstants(num32BitConstants=5, b0)"
|
||||
RWByteAddressBuffer views[] : register(u0);
|
||||
ByteAddressBuffer table : register(t0, space2);
|
||||
RWByteAddressBuffer dirty : register(u0, space2);
|
||||
cbuffer c : register(b0) { uint lo; uint hi; uint value; uint missing_lo; uint missing_hi; }
|
||||
void access(uint a, uint h, bool write) {
|
||||
bool inside = h < 256;
|
||||
uint l1 = inside ? (h << 4 | a >> 28) : 0;
|
||||
uint block = inside ? table.Load(l1 * 4) : 0;
|
||||
uint entry = block << 12 | (a >> 16 & 0xFFF);
|
||||
uint2 e = table.Load2((4096 + 2 * entry) * 4);
|
||||
bool miss = e.x == table.Load(4096 * 4);
|
||||
uint n = miss && block == 0 ? l1 : entry;
|
||||
if (write || miss) {
|
||||
dirty.InterlockedOr(n >> 5 << 2, 1u << (n & 31));
|
||||
}
|
||||
if (write && !miss) {
|
||||
views[NonUniformResourceIndex(e.x + 1)].Store(a - e.y, value);
|
||||
}
|
||||
}
|
||||
[RootSignature(RS)]
|
||||
[numthreads(1, 1, 1)]
|
||||
void main() {
|
||||
access(lo, hi, true);
|
||||
if (missing_hi != 0) {
|
||||
access(missing_lo, missing_hi, false);
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
/// What a submission that missed memory wrote through the page table
|
||||
/// leaves the host copies right after it, so the next submission reads
|
||||
/// title memory's bytes (run shards20: garbage BVH nodes over meshlet
|
||||
/// buffers until the frame's end); one that missed nothing leaves its
|
||||
/// writes in the copies.
|
||||
#[test]
|
||||
fn a_submission_that_missed_leaves_the_copies_at_once() {
|
||||
use crate::watch::PAGE;
|
||||
use windows_sys::Win32::System::Memory::{MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc};
|
||||
let (Some(host), Some(views)) = (crate::host::host(), heap()) else {
|
||||
return; // no host GPU here
|
||||
};
|
||||
let ((write_root, writer), (read_root, reader)) = (pipeline(WRITE), pipeline(SOURCE));
|
||||
let (out, out_cpu) = host.shared_buffer(256).unwrap();
|
||||
// SAFETY: a live buffer.
|
||||
let out_gpu = unsafe { out.GetGPUVirtualAddress() };
|
||||
// A page of title memory below 2^40, write-watched as the pools.
|
||||
let p = (0..256u64)
|
||||
.map(|k| 0x50_0000_0000 + (k << 28))
|
||||
// SAFETY: a fresh allocation where nothing is mapped, or none.
|
||||
.map(|hint| unsafe { VirtualAlloc(hint as *const _, PAGE, MEM_RESERVE | MEM_COMMIT | crate::watch::tests::MEM_WRITE_WATCH, PAGE_READWRITE) } as usize)
|
||||
.find(|&p| p != 0)
|
||||
.expect("memory below 2^40");
|
||||
let at = p + 16;
|
||||
// SAFETY: our pool, not guarded yet.
|
||||
unsafe { (at as *mut u32).write_volatile(0x11) };
|
||||
crate::shadow::register(p, p as u64, PAGE as u64);
|
||||
let copy = crate::shadow::find(p as u64, PAGE as u64).unwrap();
|
||||
let list = host.new_list().unwrap();
|
||||
// One submission of one dispatch; the list is open again after it.
|
||||
let submit = |root: &windows::Win32::Graphics::Direct3D12::ID3D12RootSignature,
|
||||
pso: &windows::Win32::Graphics::Direct3D12::ID3D12PipelineState,
|
||||
uav: Option<u64>,
|
||||
constants: &[u32]| {
|
||||
let map = bind(&list, OURS).expect("a page table");
|
||||
// SAFETY: an open list, live objects of ours.
|
||||
unsafe {
|
||||
list.list.SetDescriptorHeaps(&[Some(views.heap.clone())]);
|
||||
list.list.SetComputeRootSignature(root);
|
||||
list.list.SetComputeRootDescriptorTable(0, views.gpu(0));
|
||||
list.list.SetComputeRootShaderResourceView(1, map.table);
|
||||
list.list.SetComputeRootUnorderedAccessView(2, uav.unwrap_or(map.dirty));
|
||||
list.list.SetComputeRoot32BitConstants(3, constants.len() as u32, constants.as_ptr().cast(), 0);
|
||||
list.list.SetPipelineState(pso);
|
||||
list.list.Dispatch(1, 1, 1);
|
||||
}
|
||||
list.close();
|
||||
host.execute(&[&list]);
|
||||
list.reset();
|
||||
};
|
||||
let write = |value: u32, miss: bool| {
|
||||
// Memory no copy holds, in a 256 MiB region with no block.
|
||||
let missing: u64 = if miss { 0xff_f000_0000 } else { 0 };
|
||||
let constants = [at as u32, (at >> 32) as u32, value, missing as u32, (missing >> 32) as u32];
|
||||
submit(&write_root, &writer, None, &constants);
|
||||
};
|
||||
let read = || -> u32 {
|
||||
submit(&read_root, &reader, Some(out_gpu), &[at as u32, (at >> 32) as u32, 0]);
|
||||
// SAFETY: the mapped output, the GPU done with it.
|
||||
unsafe { out_cpu.cast::<u32>().read_volatile() }
|
||||
};
|
||||
// SAFETY: our pool.
|
||||
let title = || unsafe { (at as *const u32).read_volatile() };
|
||||
write(0x33, true);
|
||||
// Another test's submission may have refreshed the copy already.
|
||||
let held = copy.read_host(at as u64, 1)[0];
|
||||
assert!(!copy.clean() || held == 0x11, "the written page counts as changed at once");
|
||||
assert_eq!(title(), 0x11, "nothing went back");
|
||||
assert_eq!(read(), 0x11, "the next submission reads title memory's bytes");
|
||||
// Unless another test ends a frame meanwhile (its retake is right).
|
||||
let frame = FRAMES.load(std::sync::atomic::Ordering::Relaxed);
|
||||
write(0x44, false);
|
||||
let kept = read();
|
||||
if FRAMES.load(std::sync::atomic::Ordering::Relaxed) == frame {
|
||||
assert_eq!(kept, 0x44, "a submission that missed nothing keeps its writes");
|
||||
}
|
||||
crate::shadow::unregister(p, p as u64);
|
||||
drop(copy);
|
||||
}
|
||||
}
|
||||
@@ -887,11 +887,11 @@ pub(crate) fn copy_back_marks(withheld: &[(Arc<Shadow>, usize, usize)], memory_w
|
||||
(copy, page)
|
||||
})
|
||||
};
|
||||
// Held back: dropped from the copies at the frame's end.
|
||||
// Held back: dropped from the copies (`crate::memory::drop_withheld`).
|
||||
if !held.is_empty() {
|
||||
let m = marks();
|
||||
for (_, page) in held.into_iter().filter_map(|n| page_of(&m, n)) {
|
||||
crate::memory::drop_at_frame_end(page, PAGE);
|
||||
crate::memory::drop_withheld(page, PAGE);
|
||||
}
|
||||
}
|
||||
if marked.is_empty() {
|
||||
@@ -1541,7 +1541,7 @@ pub(crate) mod tests {
|
||||
c.write_host(p as u64 + 16, &[0x22]);
|
||||
c.keep_writes(start as usize, n as usize);
|
||||
}
|
||||
crate::memory::drop_at_frame_end(p + 16, 4);
|
||||
crate::memory::drop_withheld(p + 16, 4);
|
||||
assert_eq!((byte(&outer), byte(&inner)), (0x22, 0x22), "frame N: the GPU's copies keep it");
|
||||
crate::memory::log_frame();
|
||||
for c in [&outer, &inner] {
|
||||
|
||||
Reference in new issue
Block a user