From 25487ae2d0e06f46e888cbe449c2d1570ef2ff89 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 9 Nov 2026 03:08:06 -0800 Subject: [PATCH] Carried buffers: NoticesFor's scope back to two words; identity reads skip the carry check The per-call costs (draw_costs --calls on the M3, job m3-max-11515, base 5edcb8d876 against f9a367d0bb) showed two costs. Calls that never touch a buffer each cost about 14 instructions more: set_fragment_texture +14 and set_fragment_sampler_states +13. On top of that came the buffer reads: set_vertex_buffer +25, and draw_primitives +62 (10k) and +185 (ws). - NoticesFor's thread-local scope grew from one Option (two words) to four words. It is two words again: the command buffer's ObjectId number and the generation's address, each 0 for none (ObjectId::of_number reads the number back). - held_by, identity, tracked, object_id, length and metal_address read the program's buffer directly. Every generation's parts keep these fields, and a heap's buffer is never carried, so they need no check of where the buffer was carried. (cherry picked from commit 0c924d09963ed5b627f7e5166d9b43f53e017835) --- crates/metal2vulkan-runtime/src/buffer.rs | 19 +++++++++----- .../metal2vulkan-runtime/src/degradation.rs | 26 ++++++++++--------- crates/metal2vulkan-runtime/src/resource.rs | 5 ++++ 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/buffer.rs b/crates/metal2vulkan-runtime/src/buffer.rs index 93bf5accf..f591e2e94 100644 --- a/crates/metal2vulkan-runtime/src/buffer.rs +++ b/crates/metal2vulkan-runtime/src/buffer.rs @@ -3756,13 +3756,16 @@ impl Buffer { /// The encoder core that holds it from an argument table's retain (`retained::HeldBy`). pub(super) fn held_by(&self) -> &HeldBy { - &self.s().held_by + // The program's buffer's, whatever generation its parts are of: an encoder core's retain + // names the buffer, not its parts. + &self.0.held_by } /// The buffer's identity for hazard tracking: a heap buffer's is its heap's, as Metal tracks /// a heap's resources as one ([`Buffer::tracked`]). pub(super) fn identity(&self) -> Identity { - match &self.s().bytes { + // Every generation's parts keep the buffer's identity (`Buffer::carry`). + match &self.0.bytes { Bytes::Own(own) => own.identity, Bytes::Heap(bytes) => bytes.bytes.identity(), } @@ -3783,7 +3786,8 @@ impl Buffer { /// The bytes `range` of the buffer as hazard tracking keys them: its identity /// ([`Buffer::identity`]), and the range in its bytes, or for a heap buffer in its heap's. pub(super) fn tracked(&self, range: Range) -> (Identity, Range) { - let shift = match &self.s().bytes { + // A heap's buffer is never carried (`Buffer::carry`). + let shift = match &self.0.bytes { Bytes::Own(_) => 0, Bytes::Heap(bytes) => bytes.offset, }; @@ -3974,7 +3978,8 @@ impl Buffer { /// The buffer's Metal address, read for the runtime's own bookkeeping (a live-table key, a /// lookup), which hands it out to no one: [`Buffer::gpu_address`] without its exposure. pub(super) fn metal_address(&self) -> GpuAddress { - self.s().address + // Every generation's parts keep it (`Buffer::carry`). + self.0.address } /// Keep a no-copy buffer's import imported for good from now on, its Metal address handed @@ -4152,7 +4157,8 @@ impl Buffer { /// `length`: the length the buffer was made with. pub fn length(&self) -> u64 { - self.s().length + // Every generation's parts keep it (`Buffer::carry`). + self.0.length } /// `contents`: the CPU address of the buffer's bytes, or `None` for `Private` storage. A @@ -4188,7 +4194,8 @@ impl Buffer { /// pointer; this is unique among the process's buffers, textures and heaps for the buffer's /// life and never reused, where a `gpu_address` is taken again once its buffer is freed. pub fn object_id(&self) -> ObjectId { - self.s().object_id + // Every generation's parts keep it (`Buffer::carry`). + self.0.object_id } /// `gpuAddress`: the buffer's address in the device's Metal address space. A heap buffer's diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index ac7033f7c..b7d2b00bf 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -2964,19 +2964,21 @@ thread_local! { /// The command buffer whose work this thread does now, and its generation ([`NoticesFor`]). static NOTICES_FOR: Cell = const { Cell::new(Scope { - command_buffer: None, - generation: None, + command_buffer: 0, + generation: 0, }) }; } -/// What a thread's [`NoticesFor`] names: the command buffer whose work it does, and that command -/// buffer's generation of its device, the address of its `DeviceShared`, which a buffer its -/// device's recovery carried answers its parts of (`Buffer::carried`, `device/carry.rs`). +/// What a thread's [`NoticesFor`] names: the command buffer whose work it does (its +/// `ObjectId`'s number), and that command buffer's generation of its device, the address of its +/// `DeviceShared`, which a buffer its device's recovery carried answers its parts of +/// (`Buffer::carried`, `device/carry.rs`); 0 for none of either. Two words, as one optional +/// `ObjectId` is, which each scope of a command buffer's work sets and puts back. #[derive(Clone, Copy)] struct Scope { - command_buffer: Option, - generation: Option, + command_buffer: u64, + generation: usize, } /// This thread doing one command buffer's work, from its making until it is dropped: every notice @@ -2995,8 +2997,8 @@ impl NoticesFor { /// The work of the command buffer of ID `command_buffer`, made on `generation`, from now. pub(crate) fn new(command_buffer: ObjectId, generation: &DeviceShared) -> NoticesFor { let scope = Scope { - command_buffer: Some(command_buffer), - generation: Some(std::ptr::from_ref(generation).addr()), + command_buffer: command_buffer.get(), + generation: std::ptr::from_ref(generation).addr(), }; NoticesFor { outer: NOTICES_FOR.with(|current| current.replace(scope)), @@ -3011,7 +3013,7 @@ impl NoticesFor { let outer = NOTICES_FOR.with(|current| { let outer = current.get(); current.set(Scope { - command_buffer: None, + command_buffer: 0, ..outer }); outer @@ -3024,13 +3026,13 @@ impl NoticesFor { /// The command buffer whose work this thread does now, if any. pub(crate) fn current() -> Option { - NOTICES_FOR.with(|current| current.get().command_buffer) + NOTICES_FOR.with(|current| ObjectId::of_number(current.get().command_buffer)) } /// The generation whose command buffer's work this thread does now, if any: the address of /// its `DeviceShared`. pub(crate) fn generation() -> Option { - NOTICES_FOR.with(|current| current.get().generation) + NOTICES_FOR.with(|current| Some(current.get().generation).filter(|&at| at != 0)) } } diff --git a/crates/metal2vulkan-runtime/src/resource.rs b/crates/metal2vulkan-runtime/src/resource.rs index ae75e247e..c8f927c0d 100644 --- a/crates/metal2vulkan-runtime/src/resource.rs +++ b/crates/metal2vulkan-runtime/src/resource.rs @@ -40,6 +40,11 @@ impl ObjectId { ObjectId(NEXT.fetch_add(1, Ordering::Relaxed)) } + /// The identity whose number ([`get`](Self::get)) is `number`; `None` for 0, no object's. + pub(super) fn of_number(number: u64) -> Option { + (number != 0).then_some(ObjectId(number)) + } + /// The number, never 0. pub fn get(self) -> u64 { self.0