From 59481e60bdc6fddcc354c24ba5d17f6079a9a8c7 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 13:04:03 -0800 Subject: [PATCH] Runtime: a cut command buffer whose next piece waits for an event holds no other queue What: block_shared counts a cut command buffer at its queue's head as a holder only while its next segment is ready (its waits met). A command buffer of another queue that shares with it is held while that piece could go, and goes where the piece waits for an event not yet met. The README's queue section states the one rule this and the stale-picture fix keep: "Across queues, work goes to the device in any order, except that a command buffer never goes ahead of an earlier-committed one it shares a tracked resource or a fence with, and an event wait holds only its own queue." Its two sentences on the hold and Metrics::shared_holds say the rest. Why: the hold was a cycle. A queue held behind a cut command buffer it shares a buffer with is skipped by next_ready (submit.rs:1629) and wants nothing (:1741), and the cut command buffer's piece after encodeWaitForEvent is not ready until that event's value is reached. Where the value comes from the held queue, from a command buffer behind it, or from the host after a wait on it, nothing moves again: no refusal, no loss, the command buffer never completes. Metal's wait stops only its own queue, and runs the other queue's command buffer beside it. An update of a fence held so is not waited for in Metal either (crate::fence, probe cross_queue_fence_held_update.m). Verified: - The test every_device_runs_the_signaller_of_a_cut_command_buffers_wait_though_it_shares (queue A: two pieces, then a wait for E, then a fill; queue B: a copy from A's buffer, then the signal of E) fails without the fix on lavapipe: m3-max-68633 at f268d3ca07, 10.187 s, pieces.rs:304, the signaller not complete in 10 s; both completed once the host set E. - With the fix, every piece test passes on lavapipe, that one in 0.073 s: m3-max-68643, 12 of 12, at 4e12dfd4f6, whose code this commit carries (it adds README lines only). - Compile check (fmt, signatures, clippy plain, m2v_gpu and m2v_cpu_icd, workspace Rustdoc): m3-max-68645 at 4e12dfd4f6. Where the runtime relies on commit order across queues, and how it holds under the pieces and the device's room: 1. Sharers of a tracked resource across queues, with no event or fence between them (a fill committed first, a read committed after it on another queue): the room's rank could send the later one first. The open hole of the stale first-use picture; fixed in step() beside updater_held, the queue holding the earlier sharer going through to it first, with its own forcing test (lane surfpic). 2. The shared hold against an event wait: this commit, and its test above. 3. Waits that submit nothing: stage() marks an event's value submitted as the signal is staged (submit.rs:2308), and ready() reads that mark (:2282). The signalling segment's group goes in the same turn of pass() that staged it (step() returns Goes, :2055-2056 and the end match; pass() closes and submits it before stepping again), and one thread makes every submission (run() :2355; submit() called only at :2450 and :2460). So a waiter made ready by the mark is closed, and submitted, after its signaller. Held by structure. 4. Uploads numbered for the next commit (take_uploads, :1463): an upload goes in the first submission whose command buffers' last was committed at or after its number. Held by contract: the next close takes it. A cut command buffer's later pieces take none, so a later commit's upload may run before them; its calls came after that command buffer's commit. 5. Captures: a Managed buffer's copy-in waits at its capturing commit's number, and the first submission of a command buffer committed at or after it takes it (close(), :2147). A cut command buffer committed before the capture takes none in its later pieces; Metal leaves the range undefined for command buffers committed before the didModifyRange (crate::capture). 6. Releases: a block an unsent upload holds, released, has the uploads flushed (:1124), and a resource released in flight is freed once its submissions complete (command.rs:1823, core.pool.ended() at retire). A cut command buffer keeps what it holds until it retires after its last piece, so a release frees no sooner than before the pieces. 7. Fences across queues: a waiter goes after each held command buffer of another queue that updates its fence (updater_held, :1829), and after every ready piece of a cut one that does (the shared hold, covering fences: command.rs:338). Where the cut one's next piece waits for an event, the waiter now goes before the update. Metal does not wait for an update in a command buffer it holds at an event wait before its work (probe cross_queue_fence_held_update.m, an M3 Max, jobs m3-max-52186 and m3-max-52188). An update after a wait in the middle of a command buffer whose earlier work runs is not measured: whether Metal's waiter waits for it is unaudited. 9. Readers: Gpu::shares compares the tracked ids alone (command.rs:334-339), so two readers of one resource share it, and a read is held behind a cut command buffer that only reads it: an over-hold, with no write to order. No read and write tracking is built for it. 8. The in-flight list: the device's, in submission order (pushed :2161, popped as each value completes, :1861). A queue's own next command buffer waits for every piece of its cut one (every_device_runs_a_queues_next_command_buffer_after_every_piece), so each queue completes in its commit order. --- crates/metal2vulkan-runtime/README.md | 11 +++++++++-- crates/metal2vulkan-runtime/src/counters.rs | 2 +- crates/metal2vulkan-runtime/src/submit.rs | 11 +++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 154d3d08f..823433f7a 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -1902,7 +1902,13 @@ made, with no lock, and never rewritten for layouts. (`Metrics::pieces_passed`), unless it holds a buffer or texture the cut one holds (by the ids the hazards track them by, a heap's resources by the heap's), waits on a fence the cut one updates, or either reaches memory outside its resources: then it goes after the last piece - (`Metrics::shared_holds`). The cut command buffer is scheduled as its first piece is submitted, + (`Metrics::shared_holds`), held only while the cut one's next piece is ready; where that piece + waits for an event not yet met, it goes, as Metal's wait stops only its own queue, and the + signal may come from the queue it holds. Across queues, work goes to the device in any order, + except that a command buffer never goes ahead of an earlier-committed one it shares a tracked + resource or a fence with, and an event wait holds only its own queue. Two readers of one + resource share it here, so a read waits behind a cut command buffer that only reads what it + reads, though no write orders them: an over-hold. The cut command buffer is scheduled as its first piece is submitted, where none of its later pieces waits for an event, and its GPU times run from its first piece's start to its last piece's end, the runs of what went between its pieces included, as on the UHD 630 under macOS 26.7.1 a running command buffer's end moved by the run of one that started @@ -2152,7 +2158,8 @@ while the driver's exit handler waited on a lock, with every runtime thread alre dispatch whose own expected run passes 4 ms, a render pass and the encoders after it, the dispatches of a kernel whose rate is not yet measured, and the commands of a piece that writes a Managed buffer or while copies over a texture are owed. It waits behind every piece of a cut - command buffer that holds one of its buffers or textures, a read of a read included. Metal + command buffer that holds one of its buffers or textures, a read of a read included, except + while that command buffer's next piece waits for an event. Metal starts it beside the running command buffer: on a Radeon Pro 5300M another queue's command buffer started 0.21 to 0.40 ms after its commit beside a running one (job m3-max-68137), and on an Intel UHD 630 under macOS 26.7.1 one of a queue at a lower priority number started about diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index f639be4f3..0f51e18d8 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -2646,7 +2646,7 @@ pub struct Metrics { /// Command buffers held behind every piece of a cut command buffer of another queue, /// committed before them, as they hold a buffer or texture it holds (by the ids the hazards /// track them by), wait on a fence it updates, or either reaches memory outside its - /// resources. + /// resources: held while its next piece is ready, not while that piece waits for an event. pub shared_holds: u64, /// Elements of set 1's sampler arrays written (`vkUpdateDescriptorSets`): a sampler state's or /// a constexpr sampler's placement and its going write only the elements whose sampler diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index 7a5e6a524..cf623749c 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -1762,14 +1762,17 @@ impl WorkState { } /// Marks each FIFO whose head, committed after a cut command buffer of another queue, the head - /// of its own with pieces not yet staged, shares a resource with it (`Gpu::shares`): held until that - /// command buffer's every piece is staged, counted once a head - /// (`Metrics::shared_holds`). + /// of its own with pieces not yet staged, shares a resource with it (`Gpu::shares`): held while + /// that command buffer's next piece is ready, until its every piece is staged, counted once a + /// head (`Metrics::shared_holds`). One whose next piece waits for an event not yet met holds + /// no other queue, as Metal's wait stops only its own queue: the signal it waits for may come + /// from the queue it would hold. fn block_shared(&mut self, counters: &Counters) { let cuts: Vec<(usize, u64)> = (self.fifos.iter().enumerate()) .filter_map(|(index, fifo)| { let pending = fifo.head()?; - pending.gpu.cut.then_some((index, pending.seq)) + let next = pending.gpu.segments.get(pending.next); + (pending.gpu.cut && next.is_some_and(ready)).then_some((index, pending.seq)) }) .collect(); for index in 0..self.fifos.len() {