From 2e04276d2bfaede9ad69931b60f85cb265693868 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 5 Nov 2026 01:01:29 -0800 Subject: [PATCH] End the auto-trait walk of a segment's work at Deferred, in place of the command buffer's state The command buffer's StateLock (65d9d241fe) did not bring tests/consumer_auto_traits.rs back under rustc's default recursion limit (M3 job m3-max-712: clippy plain and --cfg m2v_gpu still fail, recursion_depth_exceeding_limit on Send of a Mutex>). The walk it reports runs from the compute encoder's core through its segments into their work and round it: EncoderCore, Vec, Segment, Deferred, Arc, LoopCheck, Vec, Piece, Deferred, and on. The state is one way into that work among several, so StateLock goes. Deferred's Send and Sync are now written out: each variant holds one Arc of its work, which is Send and Sync for every work that is both, and a constant proves that of each variant's work in this crate (at its limit of 256), its match naming every variant, so a variant added fails it until its work is proved too. A consumer's walk of any segment's work, from an encoder, a command buffer's recordings or a loop check's pieces, ends there. Verified: nothing yet; the M3 clippy (workspace, plain and --cfg m2v_gpu), rustdoc and the test crate's margin at lower recursion limits follow. (cherry picked from commit 56620f6a71edac26850357a386e4cec98bb0173d) --- .../src/command_buffer/mod.rs | 35 ++----------------- crates/metal2vulkan-runtime/src/deferred.rs | 27 ++++++++++++++ 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index dd954d539..9cbc9c6b4 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -227,7 +227,7 @@ impl WeakCommandBuffer { pub(super) struct CommandBufferShared { device: Device, queue: CommandQueue, - state: StateLock, + state: Mutex, /// Signaled when the command buffer's status changes (`wait_until_scheduled` waits on it). changed: Condvar, /// What `wait_until_completed` waits for: the command buffer finished, and the runtime's @@ -661,35 +661,6 @@ fn probe_answers(work: &Deferred, rerecord: Option<&StepInputs>) -> [bool; 5] { ] } -/// A command buffer's [`State`] behind its lock (`CommandBufferShared::state`): a `Mutex` -/// whose `Send` and `Sync` are written out (below). -struct StateLock(Mutex); - -impl std::ops::Deref for StateLock { - type Target = Mutex; - - fn deref(&self) -> &Mutex { - &self.0 - } -} - -// SAFETY: a `StateLock` is a `Mutex`, which is `Send` and `Sync` for every `State: Send`; -// the constant below makes the compiler prove `State: Send` from its fields, so these impls claim -// nothing it does not check. Written out, they end the compiler's walk of a command buffer's -// fields at its state where a program's crate proves its own objects' `Send` and `Sync`: the walk -// from an encoder through its command buffer's state, down its recordings, runs past rustc's -// default recursion limit (`recursion_depth_exceeding_limit`), which a program's crate keeps -// (`tests/consumer_auto_traits.rs`). -unsafe impl Send for StateLock {} -// SAFETY: as for `Send` above. -unsafe impl Sync for StateLock {} - -// The compiler's proof of what `StateLock`'s `Send` and `Sync` impls rest on. -const _: () = { - const fn sent() {} - sent::(); -}; - /// A command buffer's mutable state. pub(super) struct State { status: CommandBufferStatus, @@ -2114,7 +2085,7 @@ impl CommandBuffer { CommandBuffer(Arc::new(CommandBufferShared { device: queue.device().clone(), queue: queue.clone(), - state: StateLock(Mutex::new(State { + state: Mutex::new(State { status: CommandBufferStatus::NotEnqueued, error: None, encoder_open: false, @@ -2212,7 +2183,7 @@ impl CommandBuffer { replaying_predication: None, predicated: false, trail_mark: 0, - })), + }), changed: Condvar::new(), gate: Arc::default(), relayed: Arc::default(), diff --git a/crates/metal2vulkan-runtime/src/deferred.rs b/crates/metal2vulkan-runtime/src/deferred.rs index 57efbc67b..e45af5cc0 100644 --- a/crates/metal2vulkan-runtime/src/deferred.rs +++ b/crates/metal2vulkan-runtime/src/deferred.rs @@ -54,6 +54,33 @@ pub(super) enum Deferred { Loop(Arc), } +// SAFETY: a `Deferred` holds one `Arc` of its variant's work, which is `Send` and `Sync` for every +// work that is both; the constant below makes the compiler prove that of each variant's work, its +// match naming every variant (one added fails it until its work is proved too), so these impls +// claim nothing it does not check. Written out, they end the compiler's walk of a segment's work +// where a program's crate proves its own objects' `Send` and `Sync`: the walk from an encoder's +// segments down the work's types runs past rustc's default recursion limit +// (`recursion_depth_exceeding_limit`), which a program's crate keeps +// (`tests/consumer_auto_traits.rs`). +unsafe impl Send for Deferred {} +// SAFETY: as for `Send` above. +unsafe impl Sync for Deferred {} + +// The compiler's proof of what `Deferred`'s `Send` and `Sync` impls rest on: each variant's work. +const _: fn(&Deferred) = |work| { + fn shared(_: &T) {} + match work { + Deferred::Execute(work) => shared(work), + Deferred::Dispatch(work) => shared(work), + Deferred::SparseScrub(work) => shared(work), + Deferred::GatedCopyIn(work) => shared(work), + Deferred::RunMerge(work) => shared(work), + Deferred::RenderPass(work) => shared(work), + Deferred::Pending(work) => shared(work), + Deferred::Loop(work) => shared(work), + } +}; + /// What `commit` left a segment for its host step. #[derive(Clone)] pub(super) enum StepInputs {