diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 4fa002a51f..46b0bc6bf0 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -29,7 +29,7 @@ struct State { /// untranslated pipeline – created with it – since SetGraphicsRootSignature binds nothing rs_gfx: usize, pso: usize, - /// Retained decision for `pso`; live classification is added by a later stage. + /// Retained nonblocking decision for `pso`. resolution: Option, allocator: usize, /// unified memory of this recording (io, argument tables) @@ -87,12 +87,13 @@ fn native_backing(list: *mut c_void, allocator: usize) -> usize { /// recording's memory is the previous native backing's until that backing is reset, and /// pipeline and root signature state reset per D3D12 `CreateCommandList` / `Reset` semantics. pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) { + let resolution = super::pso_async::begin_pipeline_resolution(initial_pso); let backing = native_backing(list, allocator); let (old, blocks, old_resolution) = with(list, |s| { let old = std::mem::replace(&mut s.allocator, backing); s.arena = Default::default(); s.pso = initial_pso; - let old_resolution = s.resolution.take(); + let old_resolution = std::mem::replace(&mut s.resolution, resolution); s.layout = None; s.args.clear(); s.layout_gfx = None; @@ -108,9 +109,10 @@ pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) { /// `ClearState(pso)`: resets pipeline and root-argument state back to creation defaults /// with `pso` bound, keeping the current recording's allocator and UMA arena intact. pub(super) fn clear_state(list: *mut c_void, pso: usize) { + let resolution = super::pso_async::begin_pipeline_resolution(pso); let old_resolution = with(list, |s| { s.pso = pso; - let old_resolution = s.resolution.take(); + let old_resolution = std::mem::replace(&mut s.resolution, resolution); s.layout = None; s.args.clear(); s.layout_gfx = None; @@ -997,9 +999,10 @@ pub(super) unsafe extern "system" fn set_pipeline_state(this: *mut c_void, pso: } fn set_tracked_pipeline_state(this: *mut c_void, pso: usize) { + let resolution = super::pso_async::begin_pipeline_resolution(pso); let old_resolution = with(this, |s| { s.pso = pso; - s.resolution.take() + std::mem::replace(&mut s.resolution, resolution) }); drop(old_resolution); } @@ -1579,6 +1582,68 @@ void main(uint t : SV_GroupIndex) { }) } + #[test] + fn pipeline_changes_retain_the_pending_they_classified() { + let list = 0x7100_4800usize as *mut c_void; + let psos = [0x7100_4810usize, 0x7100_4820, 0x7100_4830]; + for pso in psos { + pso_async::submit_lazy(pso, false, Box::new(|| { + panic!("bind-time classification ran pipeline work") + }), 0); + } + let identity = |pso| with(list, |s| { + assert_eq!(s.pso, pso); + s.resolution.as_ref().expect("registered pipeline was not classified").identity_for_test() + }); + + set_tracked_pipeline_state(list, psos[0]); + assert_eq!(identity(psos[0]), pso_async::pending_identity_for_test(psos[0]).unwrap()); + clear_state(list, psos[1]); + assert_eq!(identity(psos[1]), pso_async::pending_identity_for_test(psos[1]).unwrap()); + reset(list, 0, psos[2]); + assert_eq!(identity(psos[2]), pso_async::pending_identity_for_test(psos[2]).unwrap()); + + set_tracked_pipeline_state(list, 0x7100_4840); + with(list, |s| assert!(s.resolution.is_none(), "unregistered pipeline got a decision")); + clear_state(list, 0); + with(list, |s| assert!(s.resolution.is_none(), "null pipeline got a decision")); + + forget(list); + for pso in psos { + assert_eq!(pso_async::released(pso), 0); + } + } + + #[test] + fn pipeline_bind_retains_pending_across_real_address_reuse() { + let old_list = 0x7100_4850usize as *mut c_void; + let new_list = 0x7100_4860usize as *mut c_void; + let pso = 0x7100_4870usize; + pso_async::submit_lazy(pso, false, Box::new(|| { + panic!("old retained pipeline work ran") + }), 0); + set_tracked_pipeline_state(old_list, pso); + let old_identity = with(old_list, |s| { + s.resolution.as_ref().unwrap().identity_for_test() + }); + assert_eq!(Some(old_identity), pso_async::pending_identity_for_test(pso)); + + assert_eq!(pso_async::released(pso), 0); + assert!(pso_async::pending_identity_for_test(pso).is_none()); + pso_async::submit_lazy(pso, false, Box::new(|| { + panic!("replacement pipeline work ran") + }), 0); + let replacement_identity = pso_async::pending_identity_for_test(pso).unwrap(); + assert_ne!(replacement_identity.1, old_identity.1); + set_tracked_pipeline_state(new_list, pso); + + assert_eq!(with(old_list, |s| s.resolution.as_ref().unwrap().identity_for_test()), old_identity); + assert_eq!(with(new_list, |s| s.resolution.as_ref().unwrap().identity_for_test()), replacement_identity); + forget(old_list); + forget(new_list); + assert_eq!(pso_async::released(pso), 0); + } + #[test] fn pipeline_resolution_replacements_drop_outside_state_lock() { let cases: [fn(*mut c_void); 4] = [ diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 99dcb4e9c3..293a15e8c3 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -4228,6 +4228,45 @@ mod used_tests { assert!(Arc::ptr_eq(&PENDING.remove(original).unwrap(), &pending)); } + #[test] + fn bind_snapshot_defers_evicted_pipeline_without_requesting_restoration() { + let vtable = [0usize, rebuild_fake_add_ref as usize, rebuild_fake_release as usize]; + let mut native = RebuildFakePso { vtable: vtable.as_ptr(), refs: AtomicUsize::new(1) }; + let raw = &mut native as *mut RebuildFakePso as usize; + let calls = Arc::new(AtomicUsize::new(0)); + let build_calls = calls.clone(); + let recipe = Recipe::new(move || { + build_calls.fetch_add(1, Ordering::Relaxed); + raw + }); + let build = Arc::new(Build::new_keyed(recipe.initial_job(), false, None, Some(recipe.clone()))); + let initial = build.job.lock().unwrap().take().unwrap(); + complete(&build, initial()); + let pin = recipe.pin_with_residency(raw).0.unwrap(); + let coordinator = pipeline_restoration_coordinator(); + let evicted = coordinator.install_eviction(&build, pin.ledger_identity()).unwrap().commit(); + drop(pin); + assert_eq!(try_evict_current(&build), EvictResult::Evicted { generation: 1 }); + let original = 0x7E57_D012usize; + let pending = structured_pending(build.clone(), false); + PENDING.insert(original, pending.clone()); + + let resolution = begin_pipeline_resolution(original).expect("registered evicted pipeline"); + assert!(matches!(&resolution.0, StructuredResolve::Deferred { pending: retained, .. } + if Arc::ptr_eq(retained, &pending))); + assert_eq!(calls.load(Ordering::Relaxed), 1, "bind snapshot ran the rebuild recipe"); + assert!(build.queued.lock().unwrap().is_none(), "bind snapshot queued build work"); + assert!(coordinator.receipts.records(1).is_empty(), "bind snapshot reserved a receipt"); + assert_eq!(coordinator.evicted.state(&evicted), Some(EvictedBuildState::Evicted)); + + assert!(Arc::ptr_eq(&PENDING.remove(original).unwrap(), &pending)); + assert!(build.clear_eviction_token(&evicted)); + assert!(coordinator.evicted.finish(&evicted, EvictedBuildState::Poisoned)); + assert!(coordinator.evicted.retire(&evicted)); + drop(resolution); drop(pending); drop(build); drop(recipe); + assert_eq!(native.refs.load(Ordering::Relaxed), 0); + } + #[test] fn restoration_coordinator_reuses_its_bounded_eviction_capacity() { let coordinator = RestorationCoordinator::<2, PIPELINE_RESOLUTION_RECEIPTS>::new(510).unwrap(); @@ -6711,6 +6750,11 @@ fn entry(pso: usize) -> Option> { PENDING.get(pso) } +#[cfg(test)] +pub(super) fn pending_identity_for_test(pso: usize) -> Option<(usize, usize)> { + entry(pso).map(|pending| (pso, Arc::as_ptr(&pending) as usize)) +} + impl Pending { /// What a bind of the stand-in `pso` gives the driver while the pipeline is not built. fn stand(&self, pso: usize) -> usize { @@ -6922,9 +6966,8 @@ pub(super) fn resolve_and_pin_for_bind(pso: usize) -> (usize, Option { @@ -6939,6 +6982,12 @@ enum StructuredResolve { fallback: usize, bound_unbuilt: bool, }, + /// Exact identity retained, but no restoration was requested by this snapshot. + Deferred { + original: usize, + pending: Arc, + fallback: usize, + }, Restoring { original: usize, pending: Arc, @@ -6953,13 +7002,16 @@ enum StructuredResolve { }, } +#[allow(dead_code)] // activated with the later restoration-request stage const PIPELINE_RESTORATION_EVICTIONS: usize = 1024; const PIPELINE_RESOLUTION_RECEIPTS: usize = 1024; +#[allow(dead_code)] // activated with the later restoration-request stage type PipelineRestorationCoordinator = RestorationCoordinator; -/// One dormant coordinator identity for the later live classification stage. Merely obtaining it -/// neither starts workers nor enables eviction; no production caller classifies through it yet. +/// One dormant coordinator identity for a later restoration-request stage. Merely obtaining it +/// neither starts workers nor enables eviction. +#[allow(dead_code)] // activated with the later restoration-request stage fn pipeline_restoration_coordinator() -> &'static PipelineRestorationCoordinator { static COORDINATOR: OnceLock = OnceLock::new(); COORDINATOR.get_or_init(|| { @@ -6967,14 +7019,14 @@ fn pipeline_restoration_coordinator() -> &'static PipelineRestorationCoordinator }) } -/// Opaque retained resolver decision carried by command-list state. Live callers do not make -/// these yet; the later classification stage supplies them without exposing coordinator types. -#[allow(dead_code)] +/// Opaque retained resolver decision carried by command-list state. +#[allow(dead_code)] // consumed by a later emission stage; retaining it pins exact identity now #[derive(Clone)] pub(super) struct PipelineResolution(StructuredResolve); /// Wrap one nonblocking decision without exposing coordinator or receipt types to command-list /// state. This does not start workers and never runs a queued driver build on the caller. +#[allow(dead_code)] // activated with the later restoration-request stage fn begin_pipeline_resolution_on( original: usize, pending: &Arc, @@ -6984,17 +7036,11 @@ fn begin_pipeline_resolution_on( PipelineResolution(classify_structured_on(original, pending, coordinator, q)) } -/// Dormant live-queue seam for the later bind integration. There are intentionally no production -/// callers yet; worker startup, state installation, and emission waits remain separate stages. -#[allow(dead_code)] -fn begin_pipeline_resolution(original: usize) -> Option { +/// Classify a bind without waiting or running driver work. Worker startup, consumption of the +/// decision, and pressure policy remain separate stages. +pub(super) fn begin_pipeline_resolution(original: usize) -> Option { let pending = entry(original)?; - Some(begin_pipeline_resolution_on( - original, - &pending, - pipeline_restoration_coordinator(), - queue(), - )) + Some(PipelineResolution(classify_retained(original, &pending))) } #[cfg(test)] @@ -7041,6 +7087,7 @@ impl PipelineResolution { pub(super) fn identity_for_test(&self) -> (usize, usize) { match &self.0 { StructuredResolve::Initial { original, pending, .. } + | StructuredResolve::Deferred { original, pending, .. } | StructuredResolve::Restoring { original, pending, .. } | StructuredResolve::Terminal { original, pending, .. } => { (*original, Arc::as_ptr(pending) as usize) @@ -7080,7 +7127,8 @@ impl StructuredResolve { Self::Initial { fallback, bound_unbuilt, .. } => StructuredResolvedPipeline { raw: *fallback, pin: None, bound_unbuilt: *bound_unbuilt, }, - Self::Restoring { fallback, .. } | Self::Terminal { fallback, .. } => { + Self::Deferred { fallback, .. } | Self::Restoring { fallback, .. } + | Self::Terminal { fallback, .. } => { StructuredResolvedPipeline { raw: *fallback, pin: None, bound_unbuilt: true } } } @@ -7093,7 +7141,7 @@ impl StructuredResolve { Self::Ready { raw, pin, .. } => Ok(Some(StructuredResolvedPipeline { raw: *raw, pin: pin.clone(), bound_unbuilt: false, })), - Self::Initial { .. } => Ok(None), + Self::Initial { .. } | Self::Deferred { .. } => Ok(None), Self::Restoring { ticket, .. } => ticket.wait_and_take_pin() .map(|pin| Some(StructuredResolvedPipeline { raw: pin.raw(), pin: Some(pin), bound_unbuilt: false })) .map_err(StructuredResolveFailure::from), @@ -7116,6 +7164,34 @@ impl From for StructuredResolveFailure { } } +/// Retain one point-in-time bind decision. This takes no coordinator receipt and submits no work; +/// an absent exact generation stays deferred for a later restoration-request stage. +fn classify_retained(original: usize, pending: &Arc) -> StructuredResolve { + let done = pending.build.done.lock().unwrap(); + let raw = pending.build.real.load(Ordering::Acquire); + let fallback = pending.stand(original); + if !*done { + return StructuredResolve::Initial { + original, pending: pending.clone(), fallback, bound_unbuilt: true, + }; + } + let Some(recipe) = pending.build.recipe.as_ref() else { + return StructuredResolve::Initial { + original, pending: pending.clone(), fallback, bound_unbuilt: false, + }; + }; + let (pin, residency) = recipe.pin_with_residency(raw); + match (pin, residency) { + (Some(pin), Residency::Resident | Residency::Rebuilding) => { + StructuredResolve::Ready { original, raw, pin: Some(pin) } + } + (_, Residency::Failed) => StructuredResolve::Initial { + original, pending: pending.clone(), fallback, bound_unbuilt: false, + }, + _ => StructuredResolve::Deferred { original, pending: pending.clone(), fallback }, + } +} + /// Classify one retained Pending without running driver work or waiting. The caller supplies the /// private coordinator and queue so this foundation neither creates a live policy singleton nor /// starts workers. No Build, recipe, registry, coordinator, or queue lock overlaps submission.