From 45e6784e91785a03b2132724479b1cbfb56dcf48 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 15 Nov 2026 06:58:33 -0800 Subject: [PATCH] RUNTIME 2348: ICB records the GPU never wrote are read in place, not staged On a device that places indirect command buffer records by first use (no unified memory, a staging queue), a host access of records in device-local memory was staged whenever a command buffer held them or the buffer was Private: the records it read were copied down through the staging queue and waited for, counted, and staged-indirect-records announced. That held even where the GPU never wrote the records, though then the host's buffer holds exactly what device-local memory does. The XTX conformance replay of development a88823f8f4 failed on it: blit-copy-indirect-command-buffer announced staged-indirect-records. Its range-none leg copies no commands into a fresh destination (an empty copy holds nothing and marks nothing written), then executes destination command 0. The execution's hold placed the untouched records in device-local memory, and the commit, which records the execution from the CPU's records because no GPU route met the buffer, read them through host_access while its own command buffer held them: staged. The cause dates from 5dd3668a4f (RUNTIME 2294, placement by first use); the last green XTX replay (06fd49b221, 11-11) predates it, and the case's recording did not change. Now host_access serves an access that only reads (Reach::Reads) records the GPU never wrote (State::written false) in place: not staged, not counted, nothing announced. A write of such records is still staged, for its copy back alone: it no longer copies the runs down first. Why the two sides cannot differ while written is false: - placement (place_on_device) fills the device-local storage with zeros and writes encode_header's header, which is what the host's storage holds as the buffer is made (write_header: CommandBuffer::initialize's header and reset records, flags cleared; the reset record is all zero, metal2vulkan's indirect_commands/tests/records.rs reset_clears_action_and_bindings_to_zero). A Shared buffer the CPU touches before any GPU use is placed on the host for good; a Private one is placed on the device at its first host access, before that access writes. - every CPU write of the records goes through records_access with Reach::Writes or Reach::Overwrites (reset_command, batch, the CPU reset), and while the records lie in device-local memory that access is staged with back = true, whose drop copies the written runs (records and written flags) back into device-local memory: home.rs line 222, `copy_runs(self.icb.generation(), records, host, &self.runs, false)`. - every GPU route that writes the records holds them with writes = true, which sets written (home.rs line 320, `state.written |= writes`): a blit's copy and reset, and a compute or render encoder's use_resource with WRITE. The move path already relied on the same fact (a move of a buffer the GPU never wrote copies no bytes). The conformance case needs no declaration: the notice was the runtime staging where it need not. Also: the runtime-lane test private_indirect_records_follow_the_memory_model expects two staged accesses (its two setters), its getter now reading in place; the docs of home.rs, Degradation::StagedIndirectRecords (and its cost), WorkCounts::icb_records_staged_accesses and docs/API.md's catalogue entry say which accesses stage. No new Degradation kind, no new counter. Verification: in the job reports that follow. Evidence so far (lane jobs): msi m3-max-32246 at cfcdb29780: the conformance binary and the m2v_gpu archives built; the lavapipe replay (conformance replay --cpu) matches known-gaps.tsv (LVP_REPLAY=0). gem10 m3-max-32248: clippy -D warnings plain, m2v_gpu and m2v_cpu_icd on runtime + validation = 0. The XTX replay and the new device tests (with the control at 76a78ec60a) follow in the lane's um790 job. Lane commits: 76a78ec60a (tests), cfcdb29780 (fix). --- .../metal2vulkan-runtime/src/degradation.rs | 13 +++-- .../src/indirect_command_buffer/home.rs | 20 +++++-- .../src/tests/icb_records_home.rs | 58 ++++++++++++++++++- .../metal2vulkan-runtime/src/work_counts.rs | 7 ++- .../src/runtime_lane/blits.rs | 13 +++-- crates/metal2vulkan/src/cache/mod.rs | 2 +- docs/API.md | 21 ++++--- 7 files changed, 104 insertions(+), 30 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 36128cc0b..477f4c6ef 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -747,13 +747,16 @@ degradations! { /// where the records lie in device-local memory and must stay there: a `Private` /// buffer's, whose records never leave it, or a `Shared` one's that the GPU used first /// while a command buffer that used them is not complete or dropped - /// (`indirect_command_buffer/home.rs`). A `Shared` buffer's records move into host memory - /// at its first host access once no command buffer holds them, and stay there. To avoid - /// it, touch a `Shared` buffer from the CPU before the GPU uses it. + /// (`indirect_command_buffer/home.rs`). An access that only reads records the GPU never + /// wrote is not one: it reads host memory in place, which holds what device-local memory + /// does. A `Shared` buffer's records move into host memory at its first host access once + /// no command buffer holds them, and stay there. To avoid it, touch a `Shared` buffer from + /// the CPU before the GPU uses it. StagedIndirectRecords = "staged-indirect-records", Performance, "a round trip through the device's staging queue for each such access: the records it \ - reads are copied into host memory first, and those it writes copied back after, \ - each copy waited for, where Metal's CPU reads and writes the records in place"; + reads are copied into host memory first where the GPU may have written them, and \ + those it writes copied back after, each copy waited for, where Metal's CPU reads and \ + writes the records in place"; /// A command buffer's GPU times on a device that calibrates its timestamps against no /// host clock the runtime reads (no `VK_KHR_calibrated_timestamps` or /// `VK_EXT_calibrated_timestamps` with the device's time domain and a monotonic host diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs index 0c484b05f..87c02a680 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs @@ -31,10 +31,14 @@ //! until the indirect command buffer goes. //! - **Staged.** Any other host access of records in device-local memory (a `Private` buffer's, //! which never move, or a `Shared` one's while a command buffer holds them) copies the records -//! it reads into the host's buffer first and those it writes back after, each on the staging -//! queue and waited for, under the home's lock, so a move, a hold or another staged access -//! waits for it; counted (`WorkCounts::icb_records_staged_accesses`) and announced once per -//! device (`Degradation::StagedIndirectRecords`). The host never reads or writes device memory. +//! it reads into the host's buffer first, where the GPU may have written them, and those it +//! writes back after, each on the staging queue and waited for, under the home's lock, so a +//! move, a hold or another staged access waits for it; counted +//! (`WorkCounts::icb_records_staged_accesses`) and announced once per device +//! (`Degradation::StagedIndirectRecords`). Where the GPU wrote nothing, the host's buffer holds +//! what the device-local one does, so an access that only reads them (a getter, or a commit that +//! records an execution from them) reads it in place, staged not at all. The host never reads +//! or writes device memory. //! - **A recovery** (`carry.rs`) loses records in device-local memory with the lost `VkDevice`: //! the host's buffer is written again as a new buffer's, and the records stay there. @@ -404,7 +408,13 @@ impl IndirectCommandBuffer { counts.icb_records_moved_to_host(moved); return Ok(HostAccess { _staged: None }); } - if reach != Reach::Overwrites { + // Where the GPU wrote nothing, the host's buffer holds what the device-local one does, as + // a move's: a read reads it in place, staged not at all, and a write is staged for its + // copy back alone. + if !state.written && reach == Reach::Reads { + return Ok(HostAccess { _staged: None }); + } + if state.written && reach != Reach::Overwrites { copy_runs(self.generation(), records, &self.0.storage, runs, true)?; state.init_seen = true; } diff --git a/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs b/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs index cc6702ebf..1b50b6ff0 100644 --- a/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs +++ b/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs @@ -3,7 +3,8 @@ //! device-local memory, held by each command buffer that uses them; a `Shared` one's move into the //! host's memory at the first host access no command buffer holds them for, with their bytes where //! the GPU wrote them and none where it did not; a `Private` one's never move; any other host -//! access is staged, announced once per device. Elsewhere every buffer's records lie in the host's +//! access is staged, announced once per device, but a read of records the GPU never wrote, which +//! reads the host's buffer in place. Elsewhere every buffer's records lie in the host's //! memory: nothing moves and nothing is staged. On every device the commands draw what was //! encoded, whoever encoded them, and the getters answer it; a GPU reset before an encode leaves //! nothing of what an earlier encode set (`lower::access::encode`'s `default_record`). @@ -546,6 +547,61 @@ fn a_held_buffer_s_host_accesses_are_staged_until_its_holder_completes() { assert_eq!(ours.staged_notices(), usize::from(ours.placing())); } +/// A buffer of either storage mode the GPU never wrote, executed first: the execution places its +/// records in device-local memory where the device places records, held by its command buffer; +/// a getter while it holds them, and the commit's read of them (it records the execution from the +/// CPU's records), read the host's buffer in place, which holds what device-local memory does: +/// nothing moves, nothing is staged and nothing is announced. The pass draws nothing, every +/// command reset. Without the in-place read, each read is staged where the device places records. +#[test] +fn records_the_gpu_never_wrote_are_read_in_place() { + let ours = Ours::new("icb-home-unwritten"); + for options in [ResourceOptions::shared(), ResourceOptions::private()] { + let icb = ours.icb(options); + let before = moves(&ours.device.work_counts()); + let holder = ours.execute(&icb); + let placed = (ours.placing(), u32::from(ours.placing())); + assert_eq!(icb.records_on_device(), placed, "{options:?}"); + let command = icb.indirect_render_command_at(0).unwrap(); + assert!(command.draw().unwrap().is_none(), "{options:?}"); + run(&holder); + assert_eq!(ours.drawn(), Vec::::new(), "{options:?}"); + assert_eq!(icb.records_on_device(), (ours.placing(), 0), "{options:?}"); + assert_eq!(ours.moved_since(before), [0, 0, 0], "{options:?}"); + } + assert_eq!(ours.staged_notices(), 0); +} + +/// The CPU's writes of a `Private` buffer's records the GPU never wrote, which lie in device-local +/// memory where the device places records, reach the GPU's reads of them there: each setter is +/// staged, its record and written flag copied back, announced once; a blit copies every command +/// into a `Shared` buffer, whose execution draws every command. +#[test] +fn cpu_writes_of_unwritten_private_records_reach_the_gpu() { + let ours = Ours::new("icb-home-unwritten-writes"); + let source = ours.icb(ResourceOptions::private()); + let before = moves(&ours.device.work_counts()); + ours.encode_on_cpu(&source); + let [moved, bytes, staged] = ours.moved_since(before); + assert_eq!((moved, bytes), (0, 0)); + match ours.placing() { + true => assert!(staged > 0, "no setter staged"), + false => assert_eq!(staged, 0), + } + assert_eq!(source.records_on_device(), (ours.placing(), 0)); + let destination = ours.icb(ResourceOptions::shared()); + let queue = ours.device.new_command_queue().unwrap(); + let command_buffer = queue.command_buffer().unwrap(); + let mut blit = command_buffer.blit_command_encoder().unwrap(); + blit.copy_indirect_command_buffer(&source, 0..u64::from(COMMANDS), &destination, 0) + .unwrap(); + blit.end_encoding().unwrap(); + run(&command_buffer); + run(&ours.execute(&destination)); + assert_eq!(ours.drawn(), every_command()); + assert_eq!(ours.staged_notices(), usize::from(ours.placing())); +} + /// A kernel writes a `Shared` buffer through an argument buffer, a pass executes it, a blit resets /// its commands, a pass executes it again and draws nothing (the control: the check of what is /// drawn sees an empty pass), the kernel writes it again and a pass draws every command: the diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 4ddc4e87f..55f86808a 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -7291,9 +7291,10 @@ pub struct WorkCounts { pub icb_record_bytes_moved: u64, /// Host accesses of indirect command buffers' records in device-local memory that went through /// the staging queue (`indirect_command_buffer/home.rs`): a `Private` buffer's, and a `Shared` - /// one's while a command buffer that used its records was not complete or dropped. Each copies - /// the records it reads into host memory, and the records it writes back, waiting for the - /// copies (`Degradation::StagedIndirectRecords`). + /// one's while a command buffer that used its records was not complete or dropped, but an + /// access that only reads records the GPU never wrote, which reads host memory in place. Each + /// copies the records it reads into host memory where the GPU may have written them, and the + /// records it writes back, waiting for the copies (`Degradation::StagedIndirectRecords`). pub icb_records_staged_accesses: u64, /// Indirect command buffers a committed command buffer made resident for writing /// (`use_resource` or `use_resources` with `ResourceUsage::WRITE`, in any of its encoders) and diff --git a/crates/metal2vulkan-validation/src/runtime_lane/blits.rs b/crates/metal2vulkan-validation/src/runtime_lane/blits.rs index 7fc61846d..01a9a62f5 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/blits.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/blits.rs @@ -444,10 +444,11 @@ pub(super) const SYSTEM_MEMORY: [Degradation; 1] = [Degradation::SharedInSystemM pub(super) const STAGED_RECORDS: [Degradation; 1] = [Degradation::StagedIndirectRecords]; /// A `Private` indirect command buffer's records lie in device-local memory on a device without -/// unified memory that reserves a staging queue, where the CPU reaches them only through that -/// queue's copies: its two setters and its getter are each staged, announced once -/// ([`STAGED_RECORDS`]); with unified memory none is, and nothing is announced. Either way the -/// getter answers what the setter wrote. +/// unified memory that reserves a staging queue, where the CPU writes them only through that +/// queue's copies: its two setters are each staged, announced once ([`STAGED_RECORDS`]), and its +/// getter reads the host's buffer in place, which holds what device-local memory does where the +/// GPU never wrote the records; with unified memory none is staged, and nothing is announced. +/// Either way the getter answers what the setter wrote. #[test] fn private_indirect_records_follow_the_memory_model() { on_device_with(|device, messages| { @@ -480,8 +481,8 @@ fn private_indirect_records_follow_the_memory_model() { } let expected: &[Degradation] = match staged { 0 => &[], - 3 => &STAGED_RECORDS, - _ => panic!("{staged} staged accesses, not 0 or 3"), + 2 => &STAGED_RECORDS, + _ => panic!("{staged} staged accesses, not 0 or 2"), }; let announced = assert_once(&messages.take_notices(), expected); assert_eq!(announced.into_keys().collect::>(), expected); diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index f7778e3d1..1a4e1b7dc 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2347; +pub const RUNTIME_VERSION: u32 = 2348; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/docs/API.md b/docs/API.md index 8e73d52c4..93b38f6ce 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2777,15 +2777,18 @@ bound. step that reads them) on a device without unified memory that reserves a staging queue, where the records lie in device-local memory and must stay there: a `Private` buffer's, which the first use places there for good, or a `Shared` one's that the GPU used first while a command buffer that - used them is not complete or dropped. A `Shared` buffer's records lie where their first use - places them: in device-local memory where the GPU's use (an execution, `useResource`, a blit's - copy, reset or optimize) comes first, where kernels write and executions read them without - crossing the link, and otherwise in host memory. They move into host memory for good at the - first host access no command buffer holds them for, copying them there where the GPU may have - written them. To avoid it, touch a `Shared` buffer from the CPU before the GPU uses it. Cost: a - round trip through the device's staging queue for each such access: the records it reads are - copied into host memory first, and those it writes copied back after, each copy waited for, - where Metal's CPU reads and writes the records in place. + used them is not complete or dropped. An access that only reads records the GPU never wrote + (a getter, or a commit that records an execution from them) is not one: it reads host memory in + place, which holds what device-local memory does. A `Shared` buffer's records lie where their + first use places them: in device-local memory where the GPU's use (an execution, `useResource`, + a blit's copy, reset or optimize) comes first, where kernels write and executions read them + without crossing the link, and otherwise in host memory. They move into host memory for good at + the first host access no command buffer holds them for, copying them there where the GPU may + have written them. To avoid it, touch a `Shared` buffer from the CPU before the GPU uses it. + Cost: a round trip through the device's staging queue for each such access: the records it + reads are copied into host memory first where the GPU may have written them, and those it + writes copied back after, each copy waited for, where Metal's CPU reads and writes the records + in place. - `metal2vulkan-degrade-uncalibrated-gpu-times` (precision). `UncalibratedGpuTimes`: A command buffer's GPU times on a device that calibrates its timestamps against no host clock the runtime reads (no `VK_KHR_calibrated_timestamps` or `VK_EXT_calibrated_timestamps` with the device's time