Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -46,6 +50,9 @@ struct Build {
job: Mutex<Option<Job>>,
/// 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,
Expand Down Expand Up @@ -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(),
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -305,6 +339,7 @@ fn complete(build: &Arc<Build>, real: usize) {
build.real.store(real, Ordering::Release);
}
*done = true;
build.finish_queued();
build.cv.notify_all();
orphan
};
Expand All @@ -316,6 +351,7 @@ fn complete(build: &Arc<Build>, 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);

Expand Down Expand Up @@ -450,6 +486,7 @@ fn drop_unstarted_job(build: &Arc<Build>) {
{
let mut done = build.done.lock().unwrap();
*done = true;
build.finish_queued();
build.cv.notify_all();
}
drop(job);
Expand Down Expand Up @@ -612,6 +649,9 @@ fn schedule(build: &Arc<Build>, priority: Priority, preload: bool, preload_rank:
priority
};
*queued = submit_job(queue(), build, priority, preload, preload_rank);
if queued.is_some() {
build.note_queued();
}
}
}

Expand Down Expand Up @@ -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(|| {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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))
}
Loading