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<Option<ComputeCommandEncoder>>). The
walk it reports runs from the compute encoder's core through its segments into their work and
round it: EncoderCore, Vec<Segment>, Segment, Deferred, Arc<LoopCheck>, LoopCheck, Vec<Piece>,
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)
This commit is contained in:
Anees Iqbal committed 2026-11-05 02:08:25 -08:00
1 parent 7e9e6a1485
commit 2e04276d2b
2 files changed
+30 -32

No files matched your search

@@ -227,7 +227,7 @@ impl WeakCommandBuffer {
pub(super) struct CommandBufferShared {
device: Device,
queue: CommandQueue,
state: StateLock,
state: Mutex<State>,
/// 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<State>`
/// whose `Send` and `Sync` are written out (below).
struct StateLock(Mutex<State>);
impl std::ops::Deref for StateLock {
type Target = Mutex<State>;
fn deref(&self) -> &Mutex<State> {
&self.0
}
}
// SAFETY: a `StateLock` is a `Mutex<State>`, 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<T: Send>() {}
sent::<State>();
};
/// 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(),
@@ -54,6 +54,33 @@ pub(super) enum Deferred {
Loop(Arc<LoopCheck>),
}
// 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: Send + Sync>(_: &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 {