diff --git a/crates/metal2vulkan-runtime/src/address.rs b/crates/metal2vulkan-runtime/src/address.rs index ee2b404a7..4814ae874 100644 --- a/crates/metal2vulkan-runtime/src/address.rs +++ b/crates/metal2vulkan-runtime/src/address.rs @@ -42,9 +42,11 @@ //! buffer in its slots is gone. Which slot a buffer takes, and when a pool is needed or goes, is //! the caller's to model, as are Metal's own allocations in its pools. //! - **Outside the space**: the runtime's own buffers (the index buffers it generates, staging for -//! fills, uploads and copies, scratch, tile memory, shadows) take ranges of a second space above -//! it, `[2^47, 2^48)`, so a program's buffer addresses never depend on when the runtime's own -//! work completes, as Metal's own allocations never move a program's. +//! fills, uploads and copies, scratch, tile memory, shadows, and the buffers of the device's +//! in-place check, `in_place_check.rs`, which runs on a thread of its own while the program makes +//! its resources) take ranges of a second space above it, `[2^47, 2^48)`, so a program's buffer +//! addresses never depend on whether or when the runtime's own work runs or completes, as +//! Metal's own allocations never move a program's, and no pin of the program's meets them. //! [`AddressPlacement::Outside`], a runtime extension, puts a caller's helper buffer there too. A //! pin never reaches it. //! - **Indirect command buffers' records**: an indirect command buffer's ID is the one Metal's pool diff --git a/crates/metal2vulkan-runtime/src/buffer.rs b/crates/metal2vulkan-runtime/src/buffer.rs index cbd9dba8c..43a035643 100644 --- a/crates/metal2vulkan-runtime/src/buffer.rs +++ b/crates/metal2vulkan-runtime/src/buffer.rs @@ -1913,10 +1913,22 @@ impl Device { /// answers nil, its validation stopping it), `Memoryless` storage /// (`Invalid`). pub fn new_buffer(&self, length: u64, options: ResourceOptions) -> Result { + self.new_buffer_placed(length, options, AddressPlacement::Any) + } + + /// [`Device::new_buffer`] at `placement` in the address space: made, refused, announced and + /// kept among the program's resources as that is, at `AddressPlacement::Outside` for a buffer + /// whose address Metal shows no program, so that no program buffer's address depends on it + /// (`address.rs`). + pub(super) fn new_buffer_placed( + &self, + length: u64, + options: ResourceOptions, + placement: AddressPlacement, + ) -> Result { self.keep_room(); let options = self.own_memory_options("new_buffer", options)?; - let (buffer, class) = - self.buffer_of_length("new_buffer", length, options, AddressPlacement::Any)?; + let (buffer, class) = self.buffer_of_length("new_buffer", length, options, placement)?; self.degrade_system_memory(&buffer, class, || { format!("a `Shared` buffer of {length} bytes") }); diff --git a/crates/metal2vulkan-runtime/src/in_place_check.rs b/crates/metal2vulkan-runtime/src/in_place_check.rs index deeb2854f..3253d3ed1 100644 --- a/crates/metal2vulkan-runtime/src/in_place_check.rs +++ b/crates/metal2vulkan-runtime/src/in_place_check.rs @@ -15,6 +15,11 @@ //! must then read the CPU's write where the CPU wrote it, and the pattern elsewhere. Each class //! runs every shape of [`SHAPES`] (texels of 1, 2, 4, 8 and 16 bytes, rows apart or one after //! another, and 3840x2160 at 4 bytes) [`ROUNDS`] times. +//! - **Its buffers' addresses** lie outside the Metal address space (`AddressPlacement::Outside`, +//! `address.rs`), each made as the product makes its class's otherwise: the check runs while +//! the program makes its resources, so a range of the space it took would refuse a pin of the +//! program's there, or move a range the runtime picks for it. Its pages, their import, the +//! image and the move do not depend on the address, so neither does a verdict. //! - **Its cost** is the pattern's fill and compare of each window, at the speed of a copy and a //! comparison of memory (a 3840x2160 surface's 33 MB among them), and each image's making and //! first move: the writes themselves run only while the move does. @@ -42,7 +47,9 @@ use crate::exit_guard; use crate::host_memory::HostMemory; use crate::in_place::{encode, mark_checking, record_name, PagesClass, Verdict}; use crate::values::ResourceOptions; -use crate::{Buffer, BufferContents, BufferDescriptor, Device, TextureDescriptor}; +use crate::{ + AddressPlacement, Buffer, BufferContents, BufferDescriptor, Device, TextureDescriptor, +}; use metal2vulkan::device::HostPageTexturePolicy; use metal2vulkan::metal::{PixelFormat, TextureUsage}; use std::ptr::NonNull; @@ -285,7 +292,9 @@ impl ClassPages { match class { PagesClass::Pointer => pointer_pages(device, length), PagesClass::Fresh => { - let buffer = device.new_buffer(length, ResourceOptions::shared()).ok()?; + let buffer = device + .new_buffer_placed(length, ResourceOptions::shared(), AddressPlacement::Outside) + .ok()?; if !buffer.in_fresh_pages() { return None; } @@ -330,7 +339,8 @@ fn no_copy_kept( deallocator: None, keep_alive: Some(pages), }; - let descriptor = BufferDescriptor::new(length, ResourceOptions::shared(), contents); + let descriptor = BufferDescriptor::new(length, ResourceOptions::shared(), contents) + .with_placement(AddressPlacement::Outside); device.new_buffer_with_descriptor(descriptor).ok() } diff --git a/crates/metal2vulkan-runtime/src/tests/in_place_check.rs b/crates/metal2vulkan-runtime/src/tests/in_place_check.rs new file mode 100644 index 000000000..f3c18e18e --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/in_place_check.rs @@ -0,0 +1,98 @@ +//! The device's check of the first move in place (`in_place_check.rs`) takes no range of the Metal +//! address space: its buffers, one per class of pages, lie outside it (`address.rs`, "Outside the +//! space"), so no pin or pick of the program's depends on whether or when the check runs, and the +//! space's state changes no verdict. +//! +//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, +//! `m2v_cpu_icd`), on the kind of device the cfg selects. Each device's shader cache is a fresh +//! directory, so no record holds its verdicts and its check runs. + +use super::blit_holds::{device, Scratch}; +use crate::address::{PAGE, SPACE_BASE, SPACE_END}; +use crate::{ + AddressPlacement, Buffer, BufferContents, BufferDescriptor, Device, Error, GpuAddress, + ResourceOptions, +}; +use std::time::{Duration, Instant}; + +/// The longest a check may take. +const WAIT: Duration = Duration::from_secs(120); + +/// The low part of the space the pins cover: its first GiB. +const LOW: u64 = 1 << 30; + +/// A pin every this many bytes of [`LOW`], of [`PINNED`] bytes: every second one held through the +/// check leaves no free run of the space a check's buffer of 40 MiB fits in. +const STRIDE: u64 = 8 << 20; +const PINNED: u64 = 16 << 10; + +/// A buffer of [`PINNED`] bytes pinned at `at`. +fn pin(device: &Device, at: u64) -> Result { + let descriptor = BufferDescriptor::new(PINNED, ResourceOptions::shared(), BufferContents::Zero) + .with_placement(AddressPlacement::At(GpuAddress(at))); + device.new_buffer_with_descriptor(descriptor) +} + +/// The pin addresses of [`LOW`] at even multiples of [`STRIDE`], or at odd ones. +fn pins(odd: bool) -> impl Iterator { + (0..LOW / STRIDE) + .filter(move |k| k % 2 == u64::from(odd)) + .map(|k| SPACE_BASE + k * STRIDE) +} + +/// A device whose check runs with the Metal space held but for runs shorter than a check's +/// buffer (the space past its first GiB by one pool, a pin every 16 MiB of that GiB) answers +/// every class as a device whose check ran with the space empty does, and each pin between the +/// held ones, made and released in turn until the check settles, is made. A check whose buffer +/// took a range of the space would find none: that class would be unchecked, or a pin refused +/// (`PlacementConflict::Held`). +#[test] +fn the_in_place_check_takes_no_range_of_the_metal_space() { + let verdicts = |device: &Device| -> Vec { + device.in_place_check_report().into_iter().take(3).collect() + }; + let reference = { + let scratch = Scratch::new("in-place-check-reference"); + let device = device(&scratch.0); + assert!( + device.settle_in_place_checks(WAIT), + "the reference check answered" + ); + verdicts(&device) + }; + let scratch = Scratch::new("in-place-check-placement"); + let device = device(&scratch.0); + let rest = SPACE_BASE + LOW; + // The pool's span is its pages and one guard page, to the space's end. + let pool = device + .reserve_address_pool( + AddressPlacement::At(GpuAddress(rest)), + SPACE_END - rest - PAGE, + ) + .unwrap(); + let held: Vec = pins(false) + .map(|at| pin(&device, at).unwrap_or_else(|error| panic!("the pin at {at:#x}: {error}"))) + .collect(); + let started = Instant::now(); + let mut rounds = 0; + while !device.settle_in_place_checks(Duration::ZERO) { + assert!(started.elapsed() < WAIT, "the check answered"); + let made: Vec = pins(true) + .map(|at| { + pin(&device, at).unwrap_or_else(|error| { + panic!("the pin at {at:#x} while the check ran: {error}") + }) + }) + .collect(); + drop(made); + rounds += 1; + } + let answered = verdicts(&device); + eprintln!( + "IN-PLACE-CHECK-PLACEMENT on {}: {answered:?} with the space held, {rounds} rounds of \ + pins while it ran; {reference:?} with it empty", + device.name() + ); + assert_eq!(answered, reference, "the verdicts with the space held"); + drop((held, pool)); +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 4f47e74da..a19ead14c 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -153,6 +153,8 @@ mod host_step_neutrals; mod image_barriers; mod image_plan; mod in_place; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod in_place_check; mod inbox; mod indirect_command_buffer; #[cfg(any(m2v_gpu, m2v_cpu_icd))]