From 09d6a0a9f3ca14abbc77d2a88af9173f36e67d3c Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 06:43:54 -0800 Subject: [PATCH] Submission checks: record their command buffers through the driver's own commands, counted among none of the device's compressed_blits::extended_range_blits_write_metal_s_encoding_under_the_canonical_policy failed on lavapipe (um790, ebf00511f0) with 3 vkCmdCopyBuffer between its counts around a blit, which it reads as zeros written. lvpfix's probe (m3-max-3592) printed for that blit 3 copies, 0 zero fills and 0 fresh-page buffers, only while the submission checks had not answered, and none once they had. WorkCounts::commands_recorded counts every command recorded on the device, whatever the command buffer and thread, and the follows check (Linux) records exactly three copies of its own, one a submission, through the device's routed functions: those were the copies, not a zero fill. The checks now record through the driver's own entry points (recorder::unrouted, the inverse of recorder::through: the command the device's slot stored, or the device's command where it took no slot), so their begin, end, copy and barrier count among none of the device's commands_recorded or copy sites, as their submissions count among none of its submissions. Test: submit_check_device's recall test holds a device that ran the checks to the cmd_copy_buffer count of the one that recalled them (the follows check's three made it differ on Linux before). Not yet built. (cherry picked from commit 35435ee82de9c26b2c031284db21e8e8a13a870b) --- crates/metal2vulkan-runtime/src/device/mod.rs | 5 ++ .../metal2vulkan-runtime/src/recorder/mod.rs | 20 +++++ .../src/recorder/thunks.rs | 10 +++ .../metal2vulkan-runtime/src/submit_check.rs | 78 ++++++++++++++++--- .../src/tests/submit_check_device.rs | 10 ++- 5 files changed, 110 insertions(+), 13 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index d61523c2b..38e4216d4 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -675,6 +675,11 @@ impl DeviceShared { .map_or_else(Default::default, Slot::counts) } + /// The recorder's slot the device's commands go through, where it took one (`recorder`). + pub(crate) fn recording(&self) -> Option<&Slot> { + self.recording.as_ref() + } + /// `vkAllocateCommandBuffers` of `info`: the loader serves it and reads the device's loader /// word first ([`DeviceWord::restored`]). pub(super) fn allocate_command_buffers( diff --git a/crates/metal2vulkan-runtime/src/recorder/mod.rs b/crates/metal2vulkan-runtime/src/recorder/mod.rs index 7f8583b3d..6634ed068 100644 --- a/crates/metal2vulkan-runtime/src/recorder/mod.rs +++ b/crates/metal2vulkan-runtime/src/recorder/mod.rs @@ -165,6 +165,26 @@ pub(crate) unsafe fn through(slot: Option<&Slot>, command: Command, rea } } +/// `routed`, a device's command `command` as its routed functions hold it, as the device's driver +/// gave it, where the device has `slot`: recorded through it, a command counts for no slot. Where +/// the device has none, its functions are its driver's, and `routed` is. +/// +/// # Safety +/// +/// `routed` is the command of the device `slot` is of, of `command`'s signature, `F`'s. +pub(crate) unsafe fn unrouted(slot: Option<&Slot>, command: Command, routed: F) -> F { + assert_eq!( + std::mem::size_of::(), + std::mem::size_of::<*const c_void>() + ); + let real = slot.map_or(std::ptr::null(), |slot| thunks::real(slot.index, command)); + if real.is_null() { + return routed; + } + // SAFETY: the driver's entry point of `command`, of `F`'s signature (the caller's). + unsafe { std::mem::transmute_copy::<*const c_void, F>(&real) } +} + /// The extension commands of `VK_EXT_mesh_shader` a draw records, routed (`render.rs`). #[derive(Clone, Copy)] pub(crate) struct MeshDraws { diff --git a/crates/metal2vulkan-runtime/src/recorder/thunks.rs b/crates/metal2vulkan-runtime/src/recorder/thunks.rs index ed54c91b4..21a603ca7 100644 --- a/crates/metal2vulkan-runtime/src/recorder/thunks.rs +++ b/crates/metal2vulkan-runtime/src/recorder/thunks.rs @@ -151,6 +151,16 @@ macro_rules! commands { } } + /// Slot `index`'s `command` as its device's driver gave it: null where none is stored. + pub(super) fn real(index: usize, command: Command) -> *const c_void { + let real = match command { + $(Command::$v0 => REAL.$f0[index].load(Ordering::Acquire),)* + $(Command::$v3 => REAL.$f3[index].load(Ordering::Acquire),)* + $(Command::$ve => REAL.$fe[index].load(Ordering::Acquire),)* + }; + real.cast_const() + } + /// Forget slot `index`'s commands. pub(super) fn clear(index: usize) { $(REAL.$f0[index].store(std::ptr::null_mut(), Ordering::Release);)* diff --git a/crates/metal2vulkan-runtime/src/submit_check.rs b/crates/metal2vulkan-runtime/src/submit_check.rs index 4b531f8bf..fe531a1f0 100644 --- a/crates/metal2vulkan-runtime/src/submit_check.rs +++ b/crates/metal2vulkan-runtime/src/submit_check.rs @@ -85,7 +85,9 @@ //! of the exit guard (`exit_guard.rs`). Its submissions are counted apart //! (`WorkCounts::submit_check_calls`), not among the device's, with the relay's mark after each //! of its uses of the queue where the device relays its queues' failures (`device/relay.rs`, -//! `By::Checks`). +//! `By::Checks`); the commands it records go through the driver's own entry points, counted +//! among none of the device's (`WorkCounts::commands_recorded`, `recorder::unrouted`). Its +//! submission calls hold the device's loader word (`loader_word.rs`). use crate::command_buffer::fences::Fences; use crate::device::functions::{Functions, Vk}; @@ -97,6 +99,7 @@ use crate::freed_memory::Parking; use crate::host_memory::Importer; use crate::in_place::DriverIdentity; use crate::loader_word::DeviceWord; +use crate::recorder::{self, Command}; use crate::release; use crate::shader_cache::store::{hex, Lookup, Store}; use crate::sharing::Sharing; @@ -710,6 +713,52 @@ struct Parts { /// Its memory types' properties, by index. type_flags: Vec, identity: DriverIdentity, + /// The commands the checks record their command buffers with. + recording: Recording, +} + +/// The commands the checks record their own command buffers with, as the device's driver gave +/// them (`recorder::unrouted`): counted among none of the device's (`WorkCounts::commands_recorded`, +/// `copy_sites`), as their submissions are among none of its submissions. +#[derive(Clone, Copy)] +struct Recording { + begin: vk::PFN_vkBeginCommandBuffer, + end: vk::PFN_vkEndCommandBuffer, + #[cfg(target_os = "linux")] + copy: vk::PFN_vkCmdCopyBuffer, + #[cfg(target_os = "linux")] + barrier: vk::PFN_vkCmdPipelineBarrier, +} + +impl Recording { + /// `shared`'s. + fn of(shared: &crate::device::DeviceShared) -> Recording { + let slot = shared.recording(); + let commands = shared.vk().fp_v1_0(); + // SAFETY: each is the device's command of its name, of its signature. + unsafe { + Recording { + begin: recorder::unrouted( + slot, + Command::BeginCommandBuffer, + commands.begin_command_buffer, + ), + end: recorder::unrouted( + slot, + Command::EndCommandBuffer, + commands.end_command_buffer, + ), + #[cfg(target_os = "linux")] + copy: recorder::unrouted(slot, Command::CmdCopyBuffer, commands.cmd_copy_buffer), + #[cfg(target_os = "linux")] + barrier: recorder::unrouted( + slot, + Command::CmdPipelineBarrier, + commands.cmd_pipeline_barrier, + ), + } + } + } } impl Parts { @@ -735,6 +784,7 @@ impl Parts { families: shared.queue_families.clone(), type_flags, identity: device.driver_identity(), + recording: Recording::of(shared), } } @@ -1283,12 +1333,13 @@ impl Objects { self.copy = self.empty.pop()?; } let begin = vk::CommandBufferBeginInfo::default(); + let recording = parts.recording; for &buffer in &self.empty { // SAFETY: a new command buffer of the pool, recorded once on this thread. unsafe { - raw.begin_command_buffer(buffer, &begin).ok()?; + (recording.begin)(buffer, &begin).result().ok()?; exit_guard::called(Call::BeginCommandBuffer); - raw.end_command_buffer(buffer).ok()?; + (recording.end)(buffer).result().ok()?; exit_guard::called(Call::EndCommandBuffer); } } @@ -1391,34 +1442,37 @@ impl Objects { fn copy(&self, parts: &Parts, import: &Import, page: u64) -> Option { { let _busy = exit_guard::enter()?; - let raw = parts.vk(); let region = vk::BufferCopy::default().dst_offset(page).size(COPIED); let barrier = vk::MemoryBarrier::default() .src_access_mask(vk::AccessFlags::TRANSFER_WRITE) .dst_access_mask(vk::AccessFlags::HOST_READ); let begin = vk::CommandBufferBeginInfo::default() .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); + let recording = parts.recording; // SAFETY: the copy command buffer is the check's, in no pending submission (each is // waited for), its pool made to reset its buffers; the buffer is live, and its two - // ranges do not overlap. + // ranges do not overlap; each array lives for its call. unsafe { - if raw.begin_command_buffer(self.copy, &begin).is_err() { + if (recording.begin)(self.copy, &begin) != vk::Result::SUCCESS { return Some(false); } exit_guard::called(Call::BeginCommandBuffer); - raw.cmd_copy_buffer(self.copy, import.buffer, import.buffer, &[region]); + (recording.copy)(self.copy, import.buffer, import.buffer, 1, ®ion); exit_guard::called(Call::CmdCopyBuffer); - raw.cmd_pipeline_barrier( + (recording.barrier)( self.copy, vk::PipelineStageFlags::TRANSFER, vk::PipelineStageFlags::HOST, vk::DependencyFlags::empty(), - &[barrier], - &[], - &[], + 1, + &barrier, + 0, + std::ptr::null(), + 0, + std::ptr::null(), ); exit_guard::called(Call::CmdPipelineBarrier); - if raw.end_command_buffer(self.copy).is_err() { + if (recording.end)(self.copy) != vk::Result::SUCCESS { return Some(false); } exit_guard::called(Call::EndCommandBuffer); diff --git a/crates/metal2vulkan-runtime/src/tests/submit_check_device.rs b/crates/metal2vulkan-runtime/src/tests/submit_check_device.rs index b8d768d59..3b640469d 100644 --- a/crates/metal2vulkan-runtime/src/tests/submit_check_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/submit_check_device.rs @@ -5,7 +5,9 @@ //! - **They answer.** A device made over a fresh shader cache runs its checks once //! (`WorkCounts::submit_checks_run`), each verdict an answer (never pending once settled; the //! follows check answered on Linux where the device imports host pointers), their calls counted -//! apart (`WorkCounts::submit_check_calls`); a later device over the same cache recalls them +//! apart (`WorkCounts::submit_check_calls`) and their commands among none of the device's +//! (`commands_recorded`: a device that ran them recorded the copies one that recalled them did, +//! the follows check's three among neither); a later device over the same cache recalls them //! (`submit_checks_recalled`), runs none, and holds the verdicts of the last device that ran //! them: the first's, or, where load split its tries and nothing was recorded, those of the //! next device that ran them. @@ -91,6 +93,7 @@ fn the_checks_answer_once_and_a_second_device_recalls_them() { ); } let counts = first.work_counts(); + let mut copies = counts.commands_recorded.cmd_copy_buffer; assert_eq!(counts.submit_checks_run, 1); assert_eq!(counts.submit_checks_recalled, 0); assert!( @@ -118,11 +121,16 @@ fn the_checks_answer_once_and_a_second_device_recalls_them() { assert_eq!(counts.submit_checks_run, 0); assert_eq!(counts.submit_check_calls, 0); assert_eq!(verdicts(&second), ran); + assert_eq!( + counts.commands_recorded.cmd_copy_buffer, copies, + "the device that ran the checks recorded other copies than one that recalled them" + ); break; } if counts.submit_checks_run == 1 { println!("{} again: {}", second.name(), second.submit_check_report()); ran = verdicts(&second); + copies = counts.commands_recorded.cmd_copy_buffer; } if start.elapsed() >= WAIT { let name = record_name(&second.driver_identity());