From 719983b05ae54a5228548768002b22192ee52ffd Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 27 Nov 2026 06:05:03 -0800 Subject: [PATCH] Runtime tests: Shared coherence's counts are held to the flush and invalidate calls made What: the test-only command table tests::layouts::recorded (loaded at Device::new while a Recording lives) now also takes vkMapMemory, vkFlushMappedMemoryRanges and vkInvalidateMappedMemoryRanges: each call is passed to the driver and counted per device, with the bytes of the ranges it names (a VK_WHOLE_SIZE range to the end of its memory's mapping as vkMapMemory named it), in tests::layouts::coherence_calls. Every test in tests::coherence starts a Recording, and each reading of a device's counters there (Seen::metrics) first asserts that shared_flushes, shared_flushed_bytes, shared_invalidates and shared_invalidated_bytes equal those calls and bytes since the device was made, so each per-point count the tests already assert is of calls the device really made. Why: the view tests counted only the runtime's own bookkeeping (Coherence::flush and settle count after the call site, the time inside a call is two clock reads, and the view runs on memory that is coherent in fact). With both Vulkan calls removed and the counters kept (scratch 188b2d0cee) they stayed green on the RX 550 (job m3-max-76941). No product line changes. Verified: see the lane's report (the RX 550 at this commit and with the calls removed). --- .../src/tests/coherence.rs | 103 +++++++++++++++-- .../metal2vulkan-runtime/src/tests/layouts.rs | 105 +++++++++++++++++- 2 files changed, 198 insertions(+), 10 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/tests/coherence.rs b/crates/metal2vulkan-runtime/src/tests/coherence.rs index 5adbdcc12..1d4df038a 100644 --- a/crates/metal2vulkan-runtime/src/tests/coherence.rs +++ b/crates/metal2vulkan-runtime/src/tests/coherence.rs @@ -10,6 +10,13 @@ //! with its bytes, and none fails. A device whose one host-visible, host-cached buffer type also //! holds the parameter ring is refused under the view, with its refusal. //! +//! Every count is of calls the device made: each reading of its counters first holds its four +//! coherence counters (the flushes, their bytes, the invalidates, their bytes) equal to the +//! `vkFlushMappedMemoryRanges` and `vkInvalidateMappedMemoryRanges` calls the device's recorded +//! table of commands saw it make since it was made, and the bytes of the ranges they named +//! (`crate::tests::layouts::coherence_calls`), so a count with no call behind it, or a call over +//! other bytes, fails where it is read. +//! //! Every count also checks the points' identity: the flushed bytes by point (before a //! submission, at a retirement, before an event's value is seen, around the runtime's own host //! accesses) sum to the flushed bytes, and the invalidated bytes are the retirements' and the @@ -53,6 +60,7 @@ mod on_device { each_device_under_the_view, finished, on_each_device, shared_event, within, PATIENCE, }; use crate::tests::import::on_device::{free, host_memory, releasing, wait_until}; + use crate::tests::layouts::{coherence_calls, Recording}; use crate::texture::TextureObject; use crate::{ Buffer, BufferLayout, ClearColor, CommandBuffer, CommandBufferStatus, CommandQueue, @@ -99,6 +107,51 @@ mod on_device { made } + /// A device's counters, read through [`Seen::metrics`]. + struct Seen<'a> { + name: &'a str, + device: &'a Device, + } + + impl Seen<'_> { + /// The device's counters now, once its coherence counters (the flushes, their bytes, the + /// invalidates, their bytes) equal the `vkFlushMappedMemoryRanges` and + /// `vkInvalidateMappedMemoryRanges` calls the recorder saw the device make since it was + /// made and their ranges' bytes (`crate::tests::layouts::coherence_calls`); a call under + /// way is in the record before the counters, so a reading waits up to a second for them + /// to agree. A device made while no [`Recording`] lived has no record and fails. + fn metrics(&self) -> Metrics { + let handle = self.device.core().device.handle(); + let read = || { + let metrics = self.device.counters().metrics(); + let claimed = [ + metrics.shared_flushes, + metrics.shared_flushed_bytes, + metrics.shared_invalidates, + metrics.shared_invalidated_bytes, + ]; + (coherence_calls(handle), claimed, metrics) + }; + let mut seen = read(); + for _ in 0..100 { + if seen.0 == Some(seen.1) { + break; + } + std::thread::sleep(std::time::Duration::from_millis(10)); + seen = read(); + } + let (made, claimed, metrics) = seen; + assert_eq!( + made, + Some(claimed), + "{}: the Vulkan calls made and their ranges' bytes (flushes, flushed bytes, \ + invalidates, invalidated bytes), against the counters' claim", + self.name + ); + metrics + } + } + /// From `before` to `after`: the host nanoseconds inside the flushes, then inside the /// invalidates. fn nanos(before: &Metrics, after: &Metrics) -> [u64; 2] { @@ -218,7 +271,7 @@ mod on_device { fn calls_of_the_work(name: &str, device: &Device, atom: Option) { const OFFSET: u64 = 256; const LEN: u64 = 64; - let counters = device.counters(); + let counters = Seen { name, device }; settled(device); within("the zero source", || device.core().filler.get().is_some()); let made = counters.metrics(); @@ -379,6 +432,7 @@ mod on_device { /// which leaves no cached type coherent, so there every device builds the state. #[test] fn every_device_flushes_and_invalidates_only_where_its_shared_type_is_not_coherent() { + let _recording = Recording::start(); use ash::vk::MemoryPropertyFlags as F; let runtime = crate::tests::validation::instance(&[]); let (_, vk_instance) = runtime.raw(); @@ -421,6 +475,7 @@ mod on_device { /// inside the calls is counted, the flushes' apart from the invalidates'. #[test] fn a_coherence_state_counts_each_call_at_its_point_and_times_it() { + let _recording = Recording::start(); use crate::buffer::BufferObject; use crate::coherence::{Coherence, Point}; const OFFSET: u64 = 320; @@ -441,7 +496,10 @@ mod on_device { let coherence = (Coherence::of(&memory, device.memory_types(), atom)) .unwrap_or_else(|| panic!("{name}: the view leaves the Shared type not coherent")); settled(&device); - let counters = device.counters(); + let counters = Seen { + name, + device: &device, + }; let block = (device.take_block(MemoryClass::Shared, 4096)).expect("a Shared block"); let buffer = (device.new_buffer(block.id, OFFSET, LEN, Some(0))).expect("a buffer"); let range = (buffer.shared_range(coherence.atom)) @@ -511,6 +569,7 @@ mod on_device { /// refuses it under the view, with that choice's refusal, which is printed. #[test] fn every_device_opened_under_the_view_flushes_and_invalidates_the_shared_memory_work_reaches() { + let _recording = Recording::start(); each_device_under_the_view("coherence", |name, _, device, _| { let atom = (device.core().coherence.as_ref()).map(|coherence| coherence.atom); assert!(atom.is_some(), "{name}: the view built the coherence state"); @@ -526,10 +585,14 @@ mod on_device { /// its own, whose bytes the submissions copy back. The destination holds the host's texels. #[test] fn every_device_opened_under_the_view_flushes_a_texture_over_a_shared_buffer_and_its_view() { + let _recording = Recording::start(); const HEIGHT: u32 = 4; each_device_under_the_view("coherence_over", |name, _, device, _| { let atom = atom(name, &device); - let counters = device.counters(); + let counters = Seen { + name, + device: &device, + }; let format = PixelFormat::RGBA8Unorm; let align = (device.minimum_linear_texture_alignment(format)) .unwrap_or_else(|error| panic!("{name}: {error}")); @@ -641,7 +704,7 @@ mod on_device { [held, out]: [&Buffer; 2], declare: impl FnOnce(&mut ComputeCommandEncoder), ) -> [u64; 9] { - let counters = device.counters(); + let counters = Seen { name, device }; let before = counters.metrics(); let command_buffer = queue.command_buffer().expect("a command buffer"); let mut encoder = @@ -665,6 +728,7 @@ mod on_device { /// host wrote there. #[test] fn every_device_opened_under_the_view_flushes_a_buffer_declared_with_use_resource() { + let _recording = Recording::start(); let Some(library) = library("ab_pointers") else { return; }; @@ -725,6 +789,7 @@ mod on_device { /// writes. The kernel reads the words the host wrote in the heap. #[test] fn every_device_opened_under_the_view_flushes_and_settles_a_shared_heap_named_by_use_heap() { + let _recording = Recording::start(); let Some(library) = library("ab_pointers") else { return; }; @@ -773,10 +838,14 @@ mod on_device { /// hold the source's bytes. #[test] fn every_device_opened_under_the_view_flushes_a_blits_shared_source_before_its_submission() { + let _recording = Recording::start(); const LEN: u64 = 1000; each_device_under_the_view("coherence_blit_source", |name, _, device, _| { let atom = atom(name, &device); - let counters = device.counters(); + let counters = Seen { + name, + device: &device, + }; let queue = device.new_command_queue(4).expect("a queue"); let shared = (device.take_block(MemoryClass::Shared, 1)).expect("a block"); let source = (device.new_buffer(shared.id, 256, LEN, None)).expect("a buffer"); @@ -830,6 +899,7 @@ mod on_device { /// what the function its entry holds returns, the entries swapped between two dispatches. #[test] fn every_device_opened_under_the_view_flushes_a_function_tables_words_before_a_call() { + let _recording = Recording::start(); const CALLS: u32 = 12; let (Some(caller), Some(functions)) = ( library("visible_table_caller"), @@ -849,7 +919,10 @@ mod on_device { )); each_device_under_the_view("coherence_function_table", |name, _, device, _| { let atom = atom(name, &device); - let counters = device.counters(); + let counters = Seen { + name, + device: &device, + }; let state = (device .new_compute_pipeline_state(&descriptor, crate::tests::no_constexpr_samplers)) .unwrap_or_else(|error| panic!("{name}: {error}")); @@ -920,11 +993,15 @@ mod on_device { /// more. #[test] fn every_device_opened_under_the_view_settles_the_writes_in_flight_before_a_signal_is_seen() { + let _recording = Recording::start(); const OFFSET: u64 = 256; const LEN: u64 = 64; each_device_under_the_view("coherence_event", |name, _, device, _| { let atom = atom(name, &device); - let counters = device.counters(); + let counters = Seen { + name, + device: &device, + }; let queue = device.new_command_queue(4).expect("a queue"); let block = (device.take_block(MemoryClass::Shared, 1)).expect("a block"); let buffer = (device.new_buffer(block.id, OFFSET, LEN, Some(0))).expect("a buffer"); @@ -1017,6 +1094,7 @@ mod on_device { /// the import, with its refusal, which is printed. #[test] fn every_device_opened_under_the_view_flushes_an_uploads_source_and_settles_its_texture() { + let _recording = Recording::start(); const SOURCE: u64 = 4096; const AT: u64 = 64; const HEIGHT: u32 = 16; @@ -1030,7 +1108,10 @@ mod on_device { const SIZE: [u32; 2] = [90, 12]; each_device_under_the_view("coherence_upload", |name, _, device, _| { let atom = atom(name, &device); - let counters = device.counters(); + let counters = Seen { + name, + device: &device, + }; let format = PixelFormat::R8Unorm; let align = (device.minimum_linear_texture_alignment(format)) .unwrap_or_else(|error| panic!("{name}: {error}")); @@ -1162,12 +1243,16 @@ mod on_device { /// reads the GPU's bytes, not the zeros. #[test] fn every_device_opened_under_the_view_flushes_a_range_a_submission_only_writes_before_it() { + let _recording = Recording::start(); const AT: u64 = 192; const LEN: u64 = 1000; const HEIGHT: u32 = 4; each_device_under_the_view("coherence_written", |name, _, device, _| { let atom = atom(name, &device); - let counters = device.counters(); + let counters = Seen { + name, + device: &device, + }; settled(&device); let queue = device.new_command_queue(4).expect("a queue"); let private = (device.take_block(MemoryClass::Private, 1)).expect("a block"); diff --git a/crates/metal2vulkan-runtime/src/tests/layouts.rs b/crates/metal2vulkan-runtime/src/tests/layouts.rs index 17fc58300..40771d6cb 100644 --- a/crates/metal2vulkan-runtime/src/tests/layouts.rs +++ b/crates/metal2vulkan-runtime/src/tests/layouts.rs @@ -7,6 +7,11 @@ //! renderings each colour attachment's view and image as made ([`rendered`]). Its barrier commands are counted too. The core commands that name a layout and that rt does not record get no entry, so //! that a call of one panics (ash's stub for a command it did not load) and the test learns of it. //! +//! The same table counts each `vkFlushMappedMemoryRanges` and `vkInvalidateMappedMemoryRanges` +//! call and the bytes of the ranges it names, a range of `VK_WHOLE_SIZE` to the end of its +//! memory's mapping as `vkMapMemory` named it ([`coherence_calls`]): the calls themselves, which +//! the runtime's own counters of them claim (`crate::coherence`). +//! //! Each command buffer's barriers, copies to a buffer and timestamp writes are kept in the order //! recorded, for every command buffer submitted ([`submitted`]). //! @@ -131,6 +136,9 @@ struct Driver { cmd_clear_depth_stencil_image: vk::PFN_vkCmdClearDepthStencilImage, cmd_begin_rendering: vk::PFN_vkCmdBeginRendering, cmd_write_timestamp2: vk::PFN_vkCmdWriteTimestamp2, + map_memory: vk::PFN_vkMapMemory, + flush_mapped_memory_ranges: vk::PFN_vkFlushMappedMemoryRanges, + invalidate_mapped_memory_ranges: vk::PFN_vkInvalidateMappedMemoryRanges, } #[derive(Default)] @@ -140,6 +148,12 @@ struct Record { barriers: HashMap, /// Each recorded device's driver commands. drivers: HashMap, + /// Each recorded device's `vkFlushMappedMemoryRanges` and `vkInvalidateMappedMemoryRanges` + /// calls and their ranges' bytes ([`coherence_calls`]). + coherence: HashMap, + /// The end of each memory's mapping where `vkMapMemory` named its size, `None` where it named + /// `VK_WHOLE_SIZE`. + mapped: HashMap<(vk::Device, vk::DeviceMemory), Option>, /// Each command buffer's device. devices: HashMap, /// Each image view's image, type and range. @@ -221,6 +235,14 @@ pub(crate) fn forget() { record().named.clear(); } +/// `device`'s `vkFlushMappedMemoryRanges` and `vkInvalidateMappedMemoryRanges` calls since it was +/// made, and the bytes of the ranges they named: the flushes, their bytes, the invalidates, their +/// bytes. A range whose end is unknown (`VK_WHOLE_SIZE` past a mapping of `VK_WHOLE_SIZE`) counts +/// `u64::MAX` bytes. `None` for a device made while no [`Recording`] lived. +pub(crate) fn coherence_calls(device: vk::Device) -> Option<[u64; 4]> { + record().coherence.get(&device).copied() +} + /// Every copy between images `device` recorded so far, in the order recorded. pub(crate) fn image_copies(device: vk::Device) -> Vec { (record().copies.iter()) @@ -397,9 +419,25 @@ pub(crate) unsafe fn recorded(instance: &ash::Instance, device: ash::Device) -> cmd_write_timestamp2: std::mem::transmute::<*const c_void, vk::PFN_vkCmdWriteTimestamp2>( load(c"vkCmdWriteTimestamp2"), ), + map_memory: std::mem::transmute::<*const c_void, vk::PFN_vkMapMemory>(load( + c"vkMapMemory", + )), + flush_mapped_memory_ranges: std::mem::transmute::< + *const c_void, + vk::PFN_vkFlushMappedMemoryRanges, + >(load(c"vkFlushMappedMemoryRanges")), + invalidate_mapped_memory_ranges: std::mem::transmute::< + *const c_void, + vk::PFN_vkInvalidateMappedMemoryRanges, + >(load(c"vkInvalidateMappedMemoryRanges")), } }; - record().drivers.insert(handle, driver); + let mut fresh = record(); + fresh.drivers.insert(handle, driver); + // A handle made again starts again. + fresh.coherence.insert(handle, [0; 4]); + fresh.mapped.retain(|&(device, _), _| device != handle); + drop(fresh); let ours = |name: &CStr| -> Option<*const c_void> { let function: *const c_void = match name.to_bytes() { b"vkBeginCommandBuffer" => begin_command_buffer as *const c_void, @@ -418,6 +456,9 @@ pub(crate) unsafe fn recorded(instance: &ash::Instance, device: ash::Device) -> b"vkCmdClearDepthStencilImage" => cmd_clear_depth_stencil_image as *const c_void, b"vkCmdBeginRendering" => cmd_begin_rendering as *const c_void, b"vkCmdWriteTimestamp2" => cmd_write_timestamp2 as *const c_void, + b"vkMapMemory" => map_memory as *const c_void, + b"vkFlushMappedMemoryRanges" => flush_mapped_memory_ranges as *const c_void, + b"vkInvalidateMappedMemoryRanges" => invalidate_mapped_memory_ranges as *const c_void, _ => return None, }; Some(function) @@ -480,6 +521,68 @@ unsafe fn slice<'a, T>(pointer: *const T, count: u32) -> &'a [T] { } } +unsafe extern "system" fn map_memory( + device: vk::Device, + memory: vk::DeviceMemory, + offset: vk::DeviceSize, + size: vk::DeviceSize, + flags: vk::MemoryMapFlags, + data: *mut *mut c_void, +) -> vk::Result { + let driver = record().drivers[&device]; + let end = match size { + vk::WHOLE_SIZE => None, + size => Some(offset + size), + }; + record().mapped.insert((device, memory), end); + // SAFETY: the caller's arguments, passed on. + unsafe { (driver.map_memory)(device, memory, offset, size, flags, data) } +} + +/// Counts one call of `device`'s at `at` of its [`coherence_calls`] (0 a flush, 2 an +/// invalidate), and the bytes of its `ranges` one further. +fn mapped_call(device: vk::Device, at: usize, ranges: &[vk::MappedMemoryRange<'_>]) { + let mut record = record(); + let bytes = (ranges.iter()) + .map(|range| match range.size { + vk::WHOLE_SIZE => (record + .mapped + .get(&(device, range.memory)) + .copied() + .flatten()) + .map_or(u64::MAX, |end| end.saturating_sub(range.offset)), + size => size, + }) + .fold(0, u64::saturating_add); + let tally = record.coherence.entry(device).or_default(); + tally[at] += 1; + tally[at + 1] = tally[at + 1].saturating_add(bytes); +} + +unsafe extern "system" fn flush_mapped_memory_ranges( + device: vk::Device, + count: u32, + ranges: *const vk::MappedMemoryRange<'_>, +) -> vk::Result { + let driver = record().drivers[&device]; + // SAFETY: the caller's `count` ranges. + mapped_call(device, 0, unsafe { slice(ranges, count) }); + // SAFETY: the caller's arguments, passed on. + unsafe { (driver.flush_mapped_memory_ranges)(device, count, ranges) } +} + +unsafe extern "system" fn invalidate_mapped_memory_ranges( + device: vk::Device, + count: u32, + ranges: *const vk::MappedMemoryRange<'_>, +) -> vk::Result { + let driver = record().drivers[&device]; + // SAFETY: the caller's `count` ranges. + mapped_call(device, 2, unsafe { slice(ranges, count) }); + // SAFETY: the caller's arguments, passed on. + unsafe { (driver.invalidate_mapped_memory_ranges)(device, count, ranges) } +} + unsafe extern "system" fn allocate_command_buffers( device: vk::Device, info: *const vk::CommandBufferAllocateInfo<'_>,