From c553ebbfcb6d50f4a09d01e8be269a70f8f55df1 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 23 Nov 2026 10:53:22 -0800 Subject: [PATCH] rt: a device with no heap placement of links looks at none, and a command buffer keeps no alias state until it touches one The instruction bench (job m3-max-71736, lavapipe burst_2000, 2,000 one-dispatch command buffers a run, A acda405b30 against B 867e059aaf alternated A B B A A B) read own_per_run 50,646,700 against 51,286,684, +1.26%, about 320 instructions a commit: each dispatch walked its slots for placements, and Building and Segment carried the marks and the part inline. Now the heap ids count the placements with links (HeapIds::any_linked), and a dispatch, a pass's begin, a draw and a blit look at their resources' placements only while one has links; Building's marks and a segment's part are boxed, made at the first touch. --- crates/metal2vulkan-runtime/src/alias.rs | 17 ++++-- crates/metal2vulkan-runtime/src/blit.rs | 56 +++++++++++-------- crates/metal2vulkan-runtime/src/command.rs | 33 +++++++---- crates/metal2vulkan-runtime/src/encoder.rs | 4 +- crates/metal2vulkan-runtime/src/hazard.rs | 18 ++++++ .../src/render_encoder.rs | 13 +++-- crates/metal2vulkan-runtime/src/submit.rs | 4 +- 7 files changed, 99 insertions(+), 46 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/alias.rs b/crates/metal2vulkan-runtime/src/alias.rs index 62d3cda09..b8948c1a2 100644 --- a/crates/metal2vulkan-runtime/src/alias.rs +++ b/crates/metal2vulkan-runtime/src/alias.rs @@ -124,7 +124,9 @@ impl Placement { place: Arc::downgrade(&place), stale: Arc::clone(&place.stale), }); - other.linked.store(true, Relaxed); + if !other.linked.swap(true, Relaxed) { + heap.linked(true); + } links.push(Link { place: Arc::downgrade(other), stale: Arc::clone(&other.stale), @@ -133,6 +135,7 @@ impl Placement { if !links.is_empty() { place.stale.store(moved.is_some(), Relaxed); place.linked.store(true, Relaxed); + heap.linked(true); *lock(&place.links) = links; } places.push(Arc::downgrade(&place)); @@ -169,10 +172,16 @@ impl Drop for Placement { .filter_map(|link| link.place.upgrade()) .collect() }; + if *self.linked.get_mut() { + self.heap.linked(false); + } for other in &met { let mut links = lock(&other.links); links.retain(|link| !std::ptr::eq(link.place.as_ptr(), me)); - other.linked.store(!links.is_empty(), Relaxed); + let linked = !links.is_empty(); + if other.linked.swap(linked, Relaxed) && !linked { + self.heap.linked(false); + } } } } @@ -295,10 +304,6 @@ pub(crate) struct Aliased { } impl Aliased { - pub(crate) fn is_empty(&self) -> bool { - self.checks.is_empty() && self.writes.is_empty() - } - /// For the device tests: its checks. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(crate) fn checks(&self) -> usize { diff --git a/crates/metal2vulkan-runtime/src/blit.rs b/crates/metal2vulkan-runtime/src/blit.rs index 1d3a07f6e..e58cfcd97 100644 --- a/crates/metal2vulkan-runtime/src/blit.rs +++ b/crates/metal2vulkan-runtime/src/blit.rs @@ -222,13 +222,15 @@ impl BlitCommandEncoder { building.wrote_texture_managed(destination); // A heap texture it writes that an alias's write left stale moves from undefined first // (`crate::alias`). - building.places( - [ - (source.object().placement(), Access::Read), - (destination.object().placement(), Access::Write), - ] - .into_iter(), - )?; + if building.aliased() { + building.places( + [ + (source.object().placement(), Access::Read), + (destination.object().placement(), Access::Write), + ] + .into_iter(), + )?; + } let overs = [ (Reached::Texture(source.object()), Access::Read), (Reached::Texture(destination.object()), Access::ReadWrite), @@ -458,13 +460,15 @@ impl BlitCommandEncoder { building.managed_backs(backs)?; building.wrote_managed(destination); // A write of a heap buffer leaves the textures over its bytes stale (`crate::alias`). - building.places( - [ - (source.object().placement(), Access::Read), - (destination.object().placement(), Access::Write), - ] - .into_iter(), - )?; + if building.aliased() { + building.places( + [ + (source.object().placement(), Access::Read), + (destination.object().placement(), Access::Write), + ] + .into_iter(), + )?; + } let overs = [ (Reached::Buffer(source.span()), Access::Read), (Reached::Buffer(destination.span()), Access::Write), @@ -570,13 +574,15 @@ impl BlitCommandEncoder { true => (Access::Write, Access::Read), false => (Access::Read, Access::Write), }; - building.places( - [ - (texture.object().placement(), into), - (buffer.object().placement(), out), - ] - .into_iter(), - )?; + if building.aliased() { + building.places( + [ + (texture.object().placement(), into), + (buffer.object().placement(), out), + ] + .into_iter(), + )?; + } let overs = match into_image { true => [ (Reached::Texture(texture.object()), Access::ReadWrite), @@ -704,7 +710,9 @@ impl BlitCommandEncoder { building.managed_backs(backs)?; building.wrote_managed(buffer); // A write of a heap buffer leaves the textures over its bytes stale (`crate::alias`). - building.places([(buffer.object().placement(), Access::Write)].into_iter())?; + if building.aliased() { + building.places([(buffer.object().placement(), Access::Write)].into_iter())?; + } let overs = [(Reached::Buffer(buffer.span()), Access::Write)]; building.overs_before(overs, false)?; let command = building.ordered_command()?; @@ -858,7 +866,9 @@ impl BlitCommandEncoder { building.wrote_texture_managed(texture); // Its levels past the first are written: a heap texture an alias's write left stale // moves from undefined first (`crate::alias`). - building.places([(texture.object().placement(), Access::ReadWrite)].into_iter())?; + if building.aliased() { + building.places([(texture.object().placement(), Access::ReadWrite)].into_iter())?; + } let overs = [(Reached::Texture(texture.object()), Access::ReadWrite)]; building.overs_before(overs, false)?; let command = building.ordered_command()?; diff --git a/crates/metal2vulkan-runtime/src/command.rs b/crates/metal2vulkan-runtime/src/command.rs index 7baeab145..e66d37a34 100644 --- a/crates/metal2vulkan-runtime/src/command.rs +++ b/crates/metal2vulkan-runtime/src/command.rs @@ -233,8 +233,9 @@ pub(crate) struct Building { /// The drawables it presents, each with the consumer's semaphore its end signals /// ([`CommandBuffer::present_drawable`]). presents: Vec<(Texture, vk::Semaphore)>, - /// What its commands did to the heap placements with links they touched (`crate::alias`). - marks: Marks, + /// What its commands did to the heap placements with links they touched (`crate::alias`), + /// from the first such touch. + marks: Option>, } /// The push constant bytes a command buffer holds, from offset 0, as its pushes left them. @@ -343,9 +344,10 @@ pub(crate) struct Segment { /// Windows): its submission acquires them from `VK_QUEUE_FAMILY_EXTERNAL` at its start and /// releases them to it at its end (`crate::submit`). pub(crate) planes: Vec>, - /// The checks at its head and its writes of heap placements with links (`crate::alias`): - /// staged, a command buffer of moves leads it where the checks find one owed. - pub(crate) aliased: Aliased, + /// The checks at its head and its writes of heap placements with links (`crate::alias`), + /// where it has any: staged, a command buffer of moves leads it where the checks find one + /// owed. + pub(crate) aliased: Option>, } pub(crate) struct Handlers { @@ -432,7 +434,7 @@ impl CommandBuffer { vertex: Vec::new(), tally: Tally::default(), presents: Vec::new(), - marks: Marks::default(), + marks: None, }), ended: None, times: Times::default(), @@ -903,7 +905,7 @@ impl CommandBuffer { let state = self.state(); let building = (state.building.as_ref()).expect("a command buffer not yet committed"); (building.gpu.segments.iter()) - .map(|segment| segment.aliased.checks()) + .map(|segment| segment.aliased.as_ref().map_or(0, |part| part.checks())) .collect() } @@ -1144,12 +1146,23 @@ impl Building { self.split()?; } let mut segment = self.gpu.segments.len() - 1; - if !rendering && self.last().command.is_some() && self.marks.splits(uses, segment) { + let marks = self.marks.get_or_insert_with(Box::default); + if !rendering && self.gpu.segments[segment].command.is_some() && marks.splits(uses, segment) + { self.split()?; segment += 1; } - let part = &mut self.gpu.segments[segment].aliased; - Ok(self.marks.used(uses, segment, part)) + let part = self.gpu.segments[segment] + .aliased + .get_or_insert_with(Box::default); + let marks = self.marks.get_or_insert_with(Box::default); + Ok(marks.used(uses, segment, part)) + } + + /// Whether a heap placement of the device has links (`crate::alias`): where none has, a + /// command's uses look at no placement. + pub(crate) fn aliased(&self) -> bool { + self.device.core().heap_ids.any_linked() } /// [`aliases`](Self::aliases) and [`record_moves`](Self::record_moves) for a command outside a diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 15d08df89..f6f4bc869 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -1224,7 +1224,9 @@ impl ComputeCommandEncoder { let building = recording(&mut self.building); // Heap textures it writes that an alias's write left stale move from undefined first // (`crate::alias`). - building.places(accesses.places(interface))?; + if building.aliased() { + building.places(accesses.places(interface))?; + } // Textures over a buffer in an image of their own take their copies first // (`crate::over`): looked for where one is bound or declared, the kernel reaches // memory outside its resources, or such an image is ahead of its buffer, and among diff --git a/crates/metal2vulkan-runtime/src/hazard.rs b/crates/metal2vulkan-runtime/src/hazard.rs index b26513d74..2625150f6 100644 --- a/crates/metal2vulkan-runtime/src/hazard.rs +++ b/crates/metal2vulkan-runtime/src/hazard.rs @@ -10,6 +10,7 @@ use crate::device::Core; use crate::memory::BlockId; use metal2vulkan::metal::BindingAccess; use std::collections::HashMap; +use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; use std::sync::{Arc, Mutex, Weak}; /// How a dispatch uses a resource. @@ -195,6 +196,15 @@ impl HeapId { pub(crate) fn places(&self) -> &Mutex>> { &self.places } + + /// A placement of the heap gained its first link (`gained`) or lost its last. + pub(crate) fn linked(&self, gained: bool) { + let linked = &self.core.heap_ids.linked; + match gained { + true => linked.fetch_add(1, Relaxed), + false => linked.fetch_sub(1, Relaxed), + }; + } } impl Drop for HeapId { @@ -210,6 +220,9 @@ impl Drop for HeapId { #[derive(Default)] pub(crate) struct HeapIds { state: Mutex, + /// The device's heap placements with links (`crate::alias`): while none has, a command's + /// uses look at no placement. + linked: AtomicUsize, } #[derive(Default)] @@ -222,6 +235,11 @@ struct HeapIdState { const HEAP_IDS_PRUNE: usize = 64; impl HeapIds { + /// Whether a heap placement of the device has links. + pub(crate) fn any_linked(&self) -> bool { + self.linked.load(Relaxed) != 0 + } + /// The id of the heap at `offset` in `block`, a new one where no resource of it lives. pub(crate) fn of(&self, core: &Arc, block: BlockId, offset: u64) -> Arc { let mut state = crate::device::lock(&self.state); diff --git a/crates/metal2vulkan-runtime/src/render_encoder.rs b/crates/metal2vulkan-runtime/src/render_encoder.rs index 67f5b302d..bacc5a71e 100644 --- a/crates/metal2vulkan-runtime/src/render_encoder.rs +++ b/crates/metal2vulkan-runtime/src/render_encoder.rs @@ -846,10 +846,12 @@ fn begin( }); // Heap textures it renders into that an alias's write left stale move from undefined first // (`crate::alias`). - let places = (pass.targets().map(|t| &t.texture)) - .chain(resolves().map(|r| &r.texture)) - .map(|texture| (TextureObject::object(texture).placement(), Access::Write)); - building.places(places)?; + if building.aliased() { + let places = (pass.targets().map(|t| &t.texture)) + .chain(resolves().map(|r| &r.texture)) + .map(|texture| (TextureObject::object(texture).placement(), Access::Write)); + building.places(places)?; + } building.overs_before(overs(), false)?; let opens = building.opens_command_buffer(); let command = building.ordered_command()?; @@ -3180,6 +3182,9 @@ impl RenderCommandEncoder { /// rendering's end and its new beginning. Refused as another command that ends the rendering /// is while a visibility result's query counts the draws. fn draw_aliases(&mut self) -> Result<(), Error> { + if !self.core.heap_ids.any_linked() { + return Ok(()); + } let Some(state) = &self.pipeline else { return Ok(()); }; diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index f30bce162..b0177f287 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -1920,8 +1920,8 @@ impl WorkState { image.keep_plane(&mut group.planes); } // The moves its checks find owed lead it, in its submit info (`crate::alias`). - if !segment.aliased.is_empty() { - match alias::stage(core, &mut pending.gpu.recorder, &segment.aliased) { + if let Some(part) = &segment.aliased { + match alias::stage(core, &mut pending.gpu.recorder, part) { Ok(Some(lead)) => group.segments.push(Staged { command: Some(vk::CommandBufferSubmitInfo::default().command_buffer(lead)), ..Staged::default()