From 7edbf84c8fb61ba297139335d934209dda6641a0 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 17 Nov 2026 12:22:33 -0800 Subject: [PATCH] rt: a barrier between dispatches reaches dispatches only A barrier a segment records between its dispatches (at a hazard in a serial encoder, at memory_barrier, after a write through an address outside every resource, and the one an encoder that wrote owes the next dispatch) took every command as both scopes: all commands' memory writes, made visible to all commands' reads and writes. A segment records nothing else that touches memory (its other commands are the timestamps), so the barrier now takes compute shader writes as its first scope and compute shader reads and writes and indirect argument reads as its second (Reach::Dispatches): the stages and accesses a dispatch has, decided by what the segment records, with no query. The barrier at a segment's end, the upload's and the fill's keep every command. The test recorder, which records fills and copies, keeps the wide barrier for the owed one before its commands, and is not followed by a dispatch in any test. Timed as a probe on the barrier redesign (M2V_PROBE_NARROW_BARRIER, m3-max-42031 to 42036, 42040), dispatch_chain (512 serial dispatches on one buffer) steady time: 780M Phoenix 509 -> 333 us and Hawk Point 529 -> 333 us (-35%), XTX 523 -> 492/505, equal on the 2080, 3090, RX 550 and UHD 630; the 160-command-buffer row lower on both 780Ms and equal elsewhere. Slower on none. --- crates/metal2vulkan-rt/README.md | 3 +++ crates/metal2vulkan-rt/src/command.rs | 13 ++++++------ crates/metal2vulkan-rt/src/encoder.rs | 10 +++++---- crates/metal2vulkan-rt/src/record.rs | 29 +++++++++++++++++++++------ 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index a08a4e778..cd50546e0 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -234,6 +234,9 @@ that touch blocks first. only at `memory_barrier`, and begins with one where anything was read or written since the last. - An encoder that wrote anything owes one barrier at its end. The next command records it, or the segment's own end barrier pays it, so a command buffer of one writing encoder has one barrier. +- A barrier within a segment orders dispatches after dispatches: compute shader writes before it, + compute shader reads and writes and indirect argument reads after it. A segment records nothing + else that touches memory; the barrier at a segment's end takes every command and the host. - Parameters: fields in the push window, `[0, 256)` on every device, are pushed: the dispatch region and up to 28 buffer addresses, each kernel pushing only the bytes it reads. Fields past it go to a 16-byte aligned block in the parameter ring, whose address is a field in the window: a diff --git a/crates/metal2vulkan-rt/src/command.rs b/crates/metal2vulkan-rt/src/command.rs index 7b2bd4247..8fbc32562 100644 --- a/crates/metal2vulkan-rt/src/command.rs +++ b/crates/metal2vulkan-rt/src/command.rs @@ -453,7 +453,8 @@ impl CommandBuffer { } /// For the device tests: records commands into the current segment with `f`, and keeps each - /// buffer's block until the command buffer retires. + /// buffer's block until the command buffer retires. The command buffer is to dispatch nothing + /// after them: a barrier between dispatches orders only the dispatches before it. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(crate) fn record_for_test( &self, @@ -461,7 +462,7 @@ impl CommandBuffer { f: impl FnOnce(&ash::Device, vk::CommandBuffer), ) -> Result<(), Error> { self.building(|building| { - let command = building.command()?; + let command = building.command(Reach::Commands)?; f(&building.device.core().device, command); for buffer in buffers { building.keep(buffer.held_block()); @@ -562,9 +563,9 @@ impl Building { } } - /// The current segment's command buffer, with the barrier owed recorded first. Commands after - /// a signal go in a new segment. - pub(crate) fn command(&mut self) -> Result { + /// The current segment's command buffer, with the barrier owed recorded first, reaching what + /// the caller records next (`reach`). Commands after a signal go in a new segment. + pub(crate) fn command(&mut self, reach: Reach) -> Result { if !self.last().signals.is_empty() { self.split()?; } @@ -574,7 +575,7 @@ impl Building { }; if self.owed { let core = self.device.core(); - record::barrier(&core.device, command, Reach::Commands); + record::barrier(&core.device, command, reach); core.counters.count(Metric::Barriers); self.owed = false; } diff --git a/crates/metal2vulkan-rt/src/encoder.rs b/crates/metal2vulkan-rt/src/encoder.rs index 36cdfda73..ee45f1ab0 100644 --- a/crates/metal2vulkan-rt/src/encoder.rs +++ b/crates/metal2vulkan-rt/src/encoder.rs @@ -118,7 +118,9 @@ enum Launch<'a> { /// before the next dispatch. A concurrent encoder places a barrier only at /// [`memory_barrier`](Self::memory_barrier). Every resource takes part, whatever its tracking /// mode, as serial dispatch is serial order in Metal. An encoder that wrote anything owes one -/// barrier at its end, which the next command, or the segment's own end, records. +/// barrier at its end, which the next command, or the segment's own end, records. A barrier +/// between dispatches orders the dispatches after it after the dispatches before it, and reaches +/// nothing else, as nothing else in the command buffer touches memory. /// /// Parameters: a dispatch's parameter block (bound addresses plus offsets, `set_bytes` addresses /// and grid values) is pushed as push constants up to 128 bytes; a longer one is written into the @@ -760,7 +762,7 @@ impl Building { interface: &crate::pipeline::Interface, accesses: &Accesses<'_>, ) -> Result { - let command = self.command()?; + let command = self.command(Reach::Dispatches)?; let core = Arc::clone(self.device.core()); let hazards = self.hazards(); if serial { @@ -770,7 +772,7 @@ impl Building { || (interface.reads_unbound && hazards.wrote()) || (interface.writes_unbound && hazards.touched()); if conflict { - record::barrier(&core.device, command, Reach::Commands); + record::barrier(&core.device, command, Reach::Dispatches); core.counters.count(Metric::Barriers); hazards.clear(); } @@ -811,7 +813,7 @@ impl Building { } let push = ¶meters.push[..]; debug_assert!(push.len() <= PUSH_BYTES as usize); - let command = self.command()?; + let command = self.command(Reach::Dispatches)?; // SAFETY: the command buffer is recording outside any render pass; the pipeline and the // layout are the device's, and the push range of the layout spans every stage's first 128 // bytes. diff --git a/crates/metal2vulkan-rt/src/record.rs b/crates/metal2vulkan-rt/src/record.rs index 9380181f1..7896d056b 100644 --- a/crates/metal2vulkan-rt/src/record.rs +++ b/crates/metal2vulkan-rt/src/record.rs @@ -46,26 +46,43 @@ impl Recorder { } } -/// What a barrier makes earlier writes visible to. +/// What a barrier orders, and what it makes earlier writes visible to. #[derive(Clone, Copy)] pub(crate) enum Reach { + /// Within a segment: the dispatches after it after the dispatches before it, their shader + /// reads and writes and their indirect argument reads seeing the earlier shader writes. A + /// segment records nothing else that reads or writes memory, so it orders the segment's + /// commands as [`Reach::Commands`] would. + Dispatches, /// Every later command, in this submission and every later one on the queue. Commands, /// Every later command, and the host once it has waited for the work. Host, } -/// Records one global memory barrier: every write earlier in submission order is made visible -/// to what `reach` names, and every later command waits for every earlier one. It is the only +/// Records one global memory barrier: the writes earlier in submission order are made visible +/// to what `reach` names, and the commands it names wait for the earlier ones. It is the only /// barrier the runtime records for buffers: none names a buffer or a range. The caller counts it /// where it is part of the queues' work. pub(crate) fn barrier(device: &ash::Device, buffer: vk::CommandBuffer, reach: Reach) { - let (stages, accesses) = match reach { + let (source, written, stages, accesses) = match reach { + Reach::Dispatches => ( + vk::PipelineStageFlags2::COMPUTE_SHADER, + vk::AccessFlags2::SHADER_WRITE, + vk::PipelineStageFlags2::COMPUTE_SHADER | vk::PipelineStageFlags2::DRAW_INDIRECT, + vk::AccessFlags2::SHADER_READ + | vk::AccessFlags2::SHADER_WRITE + | vk::AccessFlags2::INDIRECT_COMMAND_READ, + ), Reach::Commands => ( + vk::PipelineStageFlags2::ALL_COMMANDS, + vk::AccessFlags2::MEMORY_WRITE, vk::PipelineStageFlags2::ALL_COMMANDS, vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE, ), Reach::Host => ( + vk::PipelineStageFlags2::ALL_COMMANDS, + vk::AccessFlags2::MEMORY_WRITE, vk::PipelineStageFlags2::ALL_COMMANDS | vk::PipelineStageFlags2::HOST, vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE @@ -74,8 +91,8 @@ pub(crate) fn barrier(device: &ash::Device, buffer: vk::CommandBuffer, reach: Re ), }; let barrier = [vk::MemoryBarrier2::default() - .src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) - .src_access_mask(vk::AccessFlags2::MEMORY_WRITE) + .src_stage_mask(source) + .src_access_mask(written) .dst_stage_mask(stages) .dst_access_mask(accesses)]; let dependency = vk::DependencyInfo::default().memory_barriers(&barrier);