From 765e1e53416551e6597b95bd03a96d1b7136ae61 Mon Sep 17 00:00:00 2001 From: AliceWood <36452359+AliceWood@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:58:50 +0300 Subject: [PATCH] gpu: isolate watch test resets from TLAS result reuse --- crates/xtp-gpu/src/rt/mod.rs | 2 - crates/xtp-gpu/src/rt/tests.rs | 88 +++++++++++++--------------------- crates/xtp-gpu/src/shadow.rs | 4 +- crates/xtp-gpu/src/watch.rs | 10 +++- 4 files changed, 42 insertions(+), 62 deletions(-) diff --git a/crates/xtp-gpu/src/rt/mod.rs b/crates/xtp-gpu/src/rt/mod.rs index 7608a7a..46ff295 100644 --- a/crates/xtp-gpu/src/rt/mod.rs +++ b/crates/xtp-gpu/src/rt/mod.rs @@ -642,8 +642,6 @@ impl Registry { /// of one, finds nothing there (its instances are inactive, its rays miss) /// instead of a structure in released memory. pub(crate) fn release_buffer(owner: Option, start: u64, end: u64) { - #[cfg(test)] - tests::released(start, end); let (previous, slots) = registry().retire(start, end, Some(owner)); drop(previous); for slot in slots { diff --git a/crates/xtp-gpu/src/rt/tests.rs b/crates/xtp-gpu/src/rt/tests.rs index d7d29de..2a11187 100644 --- a/crates/xtp-gpu/src/rt/tests.rs +++ b/crates/xtp-gpu/src/rt/tests.rs @@ -921,71 +921,47 @@ fn rt_spares_serve_later_generations_once_free() { assert!(table.spares.is_empty()); } -/// The ranges of title memory released so far ([`release_buffer`]). -static RELEASED: Mutex> = Mutex::new(Vec::new()); - -/// Notes a release of title memory `[start, end)`. -pub(super) fn released(start: u64, end: u64) { - RELEASED - .lock() - .unwrap_or_else(|e| e.into_inner()) - .push((start, end)); -} - -/// The releases of title memory so far whose range holds `at`. Another -/// test's report of a remap of all title memory (`watch`'s tests report -/// one over the whole address space) retires every structure and spare of -/// the registry, whichever test made them. -fn releases_over(at: u64) -> usize { - (RELEASED.lock().unwrap_or_else(|e| e.into_inner()).iter()) - .filter(|&&(start, end)| (start..end).contains(&at)) - .count() -} - /// A TLAS rebuilt every frame at one title address alternates between two /// results instead of allocating one a frame: the last generation stays in /// its slot until the next is submitted, the one before is free again. -/// The reused result traces as the last build. A try that a release of -/// the TLAS's memory cut into, which retired its generations, runs again. +/// The reused result traces as the last build. #[test] fn rt_tlas_rebuilds_reuse_a_result_nothing_holds() { let test = "rt_tlas_rebuilds_reuse_a_result_nothing_holds"; let Some((gpu, rays)) = dxr(test) else { return; }; - let (first, rebuilt, traced) = (0..5) - .find_map(|_| { - let world = World::new(); - let tlas = world.tlas.address(); - let releases = releases_over(tlas); - let generation = || registry().structures.get(&tlas).map(|s| s.result.gpu()); - let first = generation()?; - let rebuilt = (0..4) - .map(|_| { - let list = TitleList::new(); - // SAFETY: the title list and the instance buffer are live. - unsafe { - assert!(Builder.build(list.this(), &tlas_desc(tlas, &world._inputs[2]))) - }; - list.run(); - generation() - }) - .collect::>>()?; - let traced = rays.then(|| { - let slot = Descriptor::new(tlas); - let list = TitleList::new(); - let out = unwritten(); - trace(gpu.host(), &list, Scene::Descriptor(slot.handle()), &out); - list.run(); - out.words() - }); - (releases_over(tlas) == releases).then_some((first, rebuilt, traced)) - }) - .expect("five tries, each cut into by a release of all title memory"); - assert_ne!(rebuilt[0], first, "the first generation is still published"); - assert_eq!(rebuilt, [rebuilt[0], first, rebuilt[0], first]); - if let Some(traced) = traced { - assert_eq!(traced, hit()); + let world = World::new(); + let tlas = world.tlas.address(); + // A new allocation can get the same GPU address. A weak reference + // identifies the result without keeping it in use. + let generation = || Arc::downgrade(®istry().structures[&tlas].result); + let rebuild = || { + let list = TitleList::new(); + // SAFETY: the title list and the instance buffer are live. + unsafe { assert!(Builder.build(list.this(), &tlas_desc(tlas, &world._inputs[2]))) }; + list.run(); + generation() + }; + let first = generation(); + let second = rebuild(); + assert!( + !first.ptr_eq(&second), + "the first generation is still published" + ); + + // A watch test can reset its guards while the first result is a spare. + drop(crate::watch::tests::serial()); + for expected in [&first, &second, &first] { + assert!(rebuild().ptr_eq(expected), "reuse the retired result"); + } + if rays { + let slot = Descriptor::new(tlas); + let list = TitleList::new(); + let out = unwritten(); + trace(gpu.host(), &list, Scene::Descriptor(slot.handle()), &out); + list.run(); + assert_eq!(out.words(), hit()); } } diff --git a/crates/xtp-gpu/src/shadow.rs b/crates/xtp-gpu/src/shadow.rs index 7a7f86a..f18ecd7 100644 --- a/crates/xtp-gpu/src/shadow.rs +++ b/crates/xtp-gpu/src/shadow.rs @@ -4985,8 +4985,8 @@ mod tests { } /// Held by the tests that count on what reads learned of pages - /// ([`Pages`]): the guard's tests report all title memory changed - /// (`crate::watch::tests::serial`), which forgets it. + /// ([`Pages`]): the guard tests reset the same page-read cache in + /// [`crate::watch::tests::serial`]. fn learned() -> std::sync::MutexGuard<'static, ()> { crate::watch::tests::serial() } diff --git a/crates/xtp-gpu/src/watch.rs b/crates/xtp-gpu/src/watch.rs index 01a698f..a9a84ca 100644 --- a/crates/xtp-gpu/src/watch.rs +++ b/crates/xtp-gpu/src/watch.rs @@ -1229,7 +1229,10 @@ pub fn title_memory_remapped(at: usize, len: usize) { // guards' lock, which reads may wait for). drop(crate::shadow::title_changing(at, len)); crate::resource::memory_remapped(at, len); - let range = blocks(at, len); + forget_guards(blocks(at, len)); +} + +fn forget_guards(range: Range) { let mut lock = settled(range.clone()); let g = &mut *lock; g.owners.forget(range.clone()); @@ -1412,7 +1415,10 @@ pub(crate) mod tests { pub(crate) fn serial() -> std::sync::MutexGuard<'static, ()> { static SERIAL: Mutex<()> = Mutex::new(()); let serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - title_memory_remapped(0, usize::MAX); + // Reset the guards and the page-read cache these tests share. A + // title remap would also evict live GPU resources in parallel tests. + drop(crate::shadow::title_changing(0, usize::MAX)); + forget_guards(blocks(0, usize::MAX)); same().clear(); serial }