From b11f4df179c11d349b0b4b88b8adc8827c803af2 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 00:21:17 -0800 Subject: [PATCH] D3D12.X: count queued pipeline builds without a frame scan Every present walked every title-visible stand-in and locked its shared build just to update the compiling title. Cold runs can retain tens of thousands of dormant lazy stand-ins, so this both reported work that was never queued and added O(N) render-thread contention with completion. Adapt the measured accounting idea from #1168 to the integrated coalesced Build model. Track each unique title-visible build through idle, active, and done, publish the counter before a worker can complete, and finish it exactly once on completion or last-owner orphaning. Auxiliary foreground builds remain outside the title-pipeline metric. The Windows GNU library/test cross-check passed with the existing warnings. Windows MSVC runtime, GPU, and smoke tests were not available here. Source concept: #1168 by eizispark, written with Antigravity (Google DeepMind, Gemini 4 Carbon / Claude Opus 5.5). Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/pso_async.rs | 70 ++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 0f6730ab12..2f75d7ca04 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -8,11 +8,15 @@ //! `XCL_ASYNC_PSO=0` builds every pipeline in the call, as before. use std::collections::HashMap; -use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, AtomicUsize, Ordering}; use std::sync::{mpsc, Arc, Condvar, Mutex, OnceLock, Weak}; use super::build_queue::{BuildQueue, JobHandle, Priority}; +const QUEUE_IDLE: u8 = 0; +const QUEUE_ACTIVE: u8 = 1; +const QUEUE_DONE: u8 = 2; + /// Process-lifetime ranked-preload outcomes; `started` means dequeued in the Preload band. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)] #[serde(default)] @@ -46,6 +50,9 @@ struct Build { job: Mutex>, /// the build went to the workers (a lazy one goes on its first `resolve`) sent: std::sync::atomic::AtomicBool, + /// Whether a title-visible stand-in owns this build, and its exact queued-count state. + tracked: AtomicBool, + queue_state: AtomicU8, /// the real pipeline state (one owned raw pointer shared by all `Pending`s), 0 until built /// or when building failed real: AtomicUsize, @@ -153,6 +160,8 @@ impl Build { compute_key, job: Mutex::new(Some(job)), sent: std::sync::atomic::AtomicBool::new(sent), + tracked: AtomicBool::new(false), + queue_state: AtomicU8::new(QUEUE_IDLE), real: AtomicUsize::new(0), done: Mutex::new(false), cv: Condvar::new(), @@ -164,6 +173,23 @@ impl Build { } } + fn note_queued(&self) { + if self.tracked.load(Ordering::Acquire) { + // Publish the count before ACTIVE: completion may run as soon as the queue accepts + // the job and must never subtract an entry which has not been added yet. + QUEUED_BUILDS.fetch_add(1, Ordering::Relaxed); + if !mark_queued(&self.queue_state) { + QUEUED_BUILDS.fetch_sub(1, Ordering::Relaxed); + } + } + } + + fn finish_queued(&self) { + if mark_queue_done(&self.queue_state) { + QUEUED_BUILDS.fetch_sub(1, Ordering::Relaxed); + } + } + fn promote(&self, priority: Priority) { if let Some(job) = self.queued.lock().unwrap().as_ref() { let _ = job.promote(priority); @@ -200,6 +226,14 @@ impl Build { } } +fn mark_queued(state: &AtomicU8) -> bool { + state.compare_exchange(QUEUE_IDLE, QUEUE_ACTIVE, Ordering::AcqRel, Ordering::Acquire).is_ok() +} + +fn mark_queue_done(state: &AtomicU8) -> bool { + state.swap(QUEUE_DONE, Ordering::AcqRel) == QUEUE_ACTIVE +} + impl Pending { fn note_use(&self) { if self.key != 0 && !self.used.swap(true, Ordering::Relaxed) { @@ -305,6 +339,7 @@ fn complete(build: &Arc, real: usize) { build.real.store(real, Ordering::Release); } *done = true; + build.finish_queued(); build.cv.notify_all(); orphan }; @@ -316,6 +351,7 @@ fn complete(build: &Arc, real: usize) { } static BUILT: AtomicUsize = AtomicUsize::new(0); static FAILED: AtomicUsize = AtomicUsize::new(0); +static QUEUED_BUILDS: AtomicUsize = AtomicUsize::new(0); /// binds waiting right now for a pipeline still being built (`title_held`) static HELD: AtomicUsize = AtomicUsize::new(0); @@ -450,6 +486,7 @@ fn drop_unstarted_job(build: &Arc) { { let mut done = build.done.lock().unwrap(); *done = true; + build.finish_queued(); build.cv.notify_all(); } drop(job); @@ -612,6 +649,9 @@ fn schedule(build: &Arc, priority: Priority, preload: bool, preload_rank: priority }; *queued = submit_job(queue(), build, priority, preload, preload_rank); + if queued.is_some() { + build.note_queued(); + } } } @@ -766,6 +806,7 @@ fn submit_as(standin: usize, compute: bool, job: Job, send: bool, key: u64, comp spared: Default::default(), on_use: Default::default(), }); + build.tracked.store(true, Ordering::Release); PENDING.lock().unwrap().get_or_insert_with(HashMap::new).insert(standin, p.clone()); static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| { @@ -972,6 +1013,29 @@ mod used_tests { assert!(!used::shader_noted(cs), "the other set was not used"); } + #[test] + fn queued_count_state_is_once_only_and_completion_wins_races() { + let state = AtomicU8::new(QUEUE_IDLE); + assert!(mark_queued(&state)); + assert!(!mark_queued(&state)); + assert!(mark_queue_done(&state)); + assert!(!mark_queue_done(&state)); + assert!(!mark_queued(&state)); + + let completed_first = AtomicU8::new(QUEUE_IDLE); + assert!(!mark_queue_done(&completed_first)); + assert!(!mark_queued(&completed_first)); + + let build = Build::new(Box::new(|| 0), false); + build.note_queued(); + assert_eq!(build.queue_state.load(Ordering::Relaxed), QUEUE_IDLE, "auxiliary builds are not reported as title pipelines"); + build.tracked.store(true, Ordering::Release); + build.note_queued(); + assert_eq!(build.queue_state.load(Ordering::Relaxed), QUEUE_ACTIVE); + build.finish_queued(); + assert_eq!(build.queue_state.load(Ordering::Relaxed), QUEUE_DONE); + } + #[test] fn demanded_auxiliary_build_runs_on_a_worker() { let caller = std::thread::current().id(); @@ -2062,6 +2126,7 @@ pub fn released(pso: usize) -> usize { if owners == 0 { // the worker stores its result into a Build nobody will read: mark it orphaned p.build.orphan.store(true, Ordering::Release); + p.build.finish_queued(); orphaned = true; } } @@ -2090,6 +2155,5 @@ pub fn need_by_submit(pso: usize) { /// (built in the background, failed, still building) pub fn counts() -> (usize, usize, usize) { - let pending = PENDING.lock().unwrap().as_ref().map_or(0, |m| m.values().filter(|p| !*p.build.done.lock().unwrap()).count()); - (BUILT.load(Ordering::Relaxed), FAILED.load(Ordering::Relaxed), pending) + (BUILT.load(Ordering::Relaxed), FAILED.load(Ordering::Relaxed), QUEUED_BUILDS.load(Ordering::Relaxed)) }