From c5e98be51d8727b6fbfbe5ae0d6563f28e94c4b7 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 14:39:33 -0800 Subject: [PATCH] Runtime: a queue whose work all completed takes turn 0 at its next commit, seen or not What: WorkState::push reads the progress value the completion thread has seen complete (Done::completed), passed in by enqueue and commit: a queue with nothing queued and nothing in flight past it takes turn 0, as one that never submitted. Why: the submit thread forgets a queue's completed submissions only as it next looks (settle), and a completion with nothing held does not wake it. So a queue whose last submission had completed kept its old turn at its next commit, and a later commit of another, idle queue ranked before it (rank) and went first, the first's fill before its wait passed. Metal submits a command buffer committed to a queue with nothing ahead of it at its commit (the runtime README's M3 Max rows). Verified: pending. --- crates/metal2vulkan-runtime/src/command.rs | 4 ++-- crates/metal2vulkan-runtime/src/submit.rs | 26 ++++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/command.rs b/crates/metal2vulkan-runtime/src/command.rs index 1d0ee87d5..c585d339b 100644 --- a/crates/metal2vulkan-runtime/src/command.rs +++ b/crates/metal2vulkan-runtime/src/command.rs @@ -667,7 +667,7 @@ impl CommandBuffer { let mut state = self.state(); state.open()?; if state.status == CommandBufferStatus::NotEnqueued { - work.enqueue(&self.inner); + work.enqueue(&self.inner, core.done.completed()); state.status = CommandBufferStatus::Enqueued; self.inner.changed.notify_all(); } @@ -756,7 +756,7 @@ impl CommandBuffer { .end_all(&core, vec![(Arc::clone(&self.inner), pending, Some(error))]); } else { // One ended while enqueued retires behind its queue's work held or in flight. - work.commit(&self.inner, pending, ended); + work.commit(&self.inner, pending, ended, core.done.completed()); drop(work); core.work.wake(); } diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index 53e3271c6..10996766f 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -1561,13 +1561,18 @@ impl WorkState { &mut self.fifos[index] } - /// Takes `cb`'s place at its queue's end. A queue with nothing queued or in flight takes turn - /// 0, as one that never submitted. - fn push(&mut self, cb: &Arc) -> &mut Entry { + /// Takes `cb`'s place at its queue's end. A queue with nothing queued, and nothing in flight + /// past `completed`, the progress value the completion thread has seen complete, takes turn + /// 0, as one that never submitted: the submit thread forgets a queue's completed submissions + /// only as it next looks ([`settle`](Self::settle)), which a completion with nothing held does + /// not wake it to do, so its list alone would keep a queue whose work all completed out of + /// turn 0, and a later commit of another queue would rank before it ([`rank`](Self::rank)). + fn push(&mut self, cb: &Arc, completed: u64) -> &mut Entry { self.next_place += 1; let place = self.next_place; let fifo = self.fifo(&cb.queue); - if fifo.entries.is_empty() && fifo.group.is_none() && fifo.in_flight.is_empty() { + let done = (fifo.in_flight.back()).is_none_or(|&value| value <= completed); + if fifo.entries.is_empty() && fifo.group.is_none() && done { fifo.turn = 0; } let entries = &mut fifo.entries; @@ -1581,26 +1586,29 @@ impl WorkState { entries.back_mut().expect("an entry just pushed") } - /// Takes `cb`'s place in its queue's FIFO. - pub(crate) fn enqueue(&mut self, cb: &Arc) { - self.push(cb); + /// Takes `cb`'s place in its queue's FIFO, `completed` being the progress value seen + /// complete ([`push`](Self::push)). + pub(crate) fn enqueue(&mut self, cb: &Arc, completed: u64) { + self.push(cb, completed); } /// Hands `cb` to the submit thread: at its place where it was enqueued, else at its queue's /// end. With `error` (it ended while enqueued, and its place went), at its queue's end, to - /// retire with that error behind the work held or in flight before it. + /// retire with that error behind the work held or in flight before it. `completed` is the + /// progress value seen complete ([`push`](Self::push)). pub(crate) fn commit( &mut self, cb: &Arc, pending: Pending, error: Option, + completed: u64, ) { self.dirty = true; let enqueued = (self.fifo(&cb.queue).entries.iter_mut()) .find(|entry| Arc::ptr_eq(&entry.cb, cb) && entry.pending.is_none()); let entry = match enqueued { Some(entry) => entry, - None => self.push(cb), + None => self.push(cb, completed), }; entry.pending = Some(pending); entry.error = error;