From 600b38fda784a5502b8944be31ec68b680009140 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 12:06:13 -0800 Subject: [PATCH] Runtime: a piece's kernel copies empty before the states that hold them release The copies the pieces hold with no count live in the same kept objects as the counted states they are of, and are emptied, and dropped, before those states: every read of a copy (the rate update at retire) goes through that list, so a read after the release finds an empty list and sets no rate, which the pieces tests that assert a run sets its kernel's rate would report. Verified: cargo fmt only. --- crates/metal2vulkan-runtime/src/kept.rs | 14 ++++++++------ crates/metal2vulkan-runtime/src/piece.rs | 7 +++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/kept.rs b/crates/metal2vulkan-runtime/src/kept.rs index 9c83df254..045f8c907 100644 --- a/crates/metal2vulkan-runtime/src/kept.rs +++ b/crates/metal2vulkan-runtime/src/kept.rs @@ -230,13 +230,15 @@ impl DerefMut for Objects { /// The lists of each kind a recording keeps. #[derive(Default)] pub(crate) struct ByKind { + /// Its pieces' kernels (`crate::piece::Kernels`), each the content of a state `pipelines` + /// keeps, held with no count: emptied before `pipelines` is, as it is dropped first, so no + /// copy outlives its state. + pub(crate) kernels: Kernels, pub(crate) buffers: Kept, pub(crate) textures: Kept, pub(crate) samplers: Kept, pub(crate) pipelines: Kept, pub(crate) render_pipelines: Kept, - /// Its pieces' kernels, of the states `pipelines` keeps (`crate::piece::Kernels`). - pub(crate) kernels: Kernels, } impl ByKind { @@ -262,15 +264,15 @@ impl ByKind { /// Releases every object, keeping each list's capacity, within [`KEPT_BOUND`], for the next /// recording. pub(crate) fn clear(&mut self) { + match self.kernels.capacity() > KEPT_BOUND { + true => self.kernels = Kernels::new(), + false => self.kernels.clear(), + } self.buffers.clear(); self.textures.clear(); self.samplers.clear(); self.pipelines.clear(); self.render_pipelines.clear(); - match self.kernels.capacity() > KEPT_BOUND { - true => self.kernels = Kernels::new(), - false => self.kernels.clear(), - } } } diff --git a/crates/metal2vulkan-runtime/src/piece.rs b/crates/metal2vulkan-runtime/src/piece.rs index 7008be965..e25e76b75 100644 --- a/crates/metal2vulkan-runtime/src/piece.rs +++ b/crates/metal2vulkan-runtime/src/piece.rs @@ -140,8 +140,11 @@ impl Work { if self.kernels.is_empty() { self.kernels = list.len()..list.len(); } - // SAFETY: the command buffer keeps the state `kernel` is of until it retires, - // after the list's last use (`crate::command::retire`). + // SAFETY: `kernel` is the content of the state the encoder bound, which the + // command buffer's kept objects hold, counted (`Building::lend_pipeline`), in + // `pipelines`; `list` is the same objects' `kernels`, emptied before `pipelines` + // releases anything, and dropped first (`ByKind`). The copy is read only through + // `list` (`Work::ran`), so never after its state is released. let compiled = unsafe { kernel.uncounted() }; list.push(Kernel { compiled, groups }); self.kernels.end = list.len();