From 7cc30e485e6ea846f5fdf2e5cd028d0abbce57f6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 16 Nov 2026 03:28:45 -0800 Subject: [PATCH] Tests: a no-copy buffer's import goes at the completion past a merge on the host RUNTIME 2440 removed the program's buffer from the host step's drains (SpliceDrain, MergeDrain), whose segment the command buffer keeps until it goes. The test that showed the hold, a_no_copy_buffer_a_committed_dispatch_writes_a_texture_over_keeps_its_import_until_completion, no longer reaches a merge on the host since RUNTIME 2438/2439: its rows fill whole lines, so no line holds a byte beside its texels, and the merge goes by its kernel alone. The new leg TextureMergedOnHost keeps the path: the same dispatch into a texture over the no-copy buffer, its rows a texel short of their pitch (511 RGBA8 texels in 2048 bytes), on a device whose every texture over host pages is a shadow (HostPageTexturePolicy::Shadow, under which the check gives no answer), so both releases (at the commit, and once scheduled) take the merge on the host. Each asserts that a host step ran (WorkCounts::shadow_host_steps grew) and that the deallocator runs once the command buffer completed, the CommandBuffer still held. Where the device's lines hold no more than a texel, the leg prints and returns. texture_write and over_buffer take the width. The texture write legs' check goes row by row: each row's texels hold the dispatch's, and the bytes past them to the pitch still hold the pages' fill (the merge keeps them); for the 512-texel rows of TextureWrite that is every word, as before. Test only; no product change. Verified (lavapipe, m2v_cpu_icd, msi m3-max-36042): - CONTROL_LVP=100 with 7713853865's shadow.rs, shadow_merge.rs and shadow_splice.rs (the drains holding `_buffer`): "host steps 1", then "the deallocator never ran" at the Committed release. - HOLD_LVP=0 at RUNTIME 2440: the new test (host steps 1 at each release) and TextureWrite, 2 of 2. - HOLDS_LVP=0: every *holds* test, 134 of 134. - clippy of metal2vulkan-runtime under m2v_cpu_icd and m2v_gpu: clean. --- .../src/tests/encoder_holds.rs | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/tests/encoder_holds.rs b/crates/metal2vulkan-runtime/src/tests/encoder_holds.rs index 3fa1a6144..d51467ec3 100644 --- a/crates/metal2vulkan-runtime/src/tests/encoder_holds.rs +++ b/crates/metal2vulkan-runtime/src/tests/encoder_holds.rs @@ -2,8 +2,9 @@ //! its command buffer completed: `blit_holds.rs`'s drop-after-commit legs for the other encoder //! kinds. Each leg encodes a command that reads or writes a no-copy buffer over host pages, by one //! way a command reaches a buffer (a binding, an index or indirect buffer, an attachment or a -//! texture over the buffer, an address held in memory that `use_resource` or `use_heap` makes -//! resident, an indirect command buffer's command, a sparse texture's indirect mapping update), +//! texture over the buffer, one whose write back the shadow merges on the host, an address held +//! in memory that `use_resource` or `use_heap` makes resident, an indirect command buffer's +//! command, a sparse texture's indirect mapping update), //! before busy work is committed, commits it after that work, and releases the no-copy buffer //! and every other object the command uses: once at the commit and once more where the command //! buffer was scheduled. While the command buffer has not completed, the buffer's deallocator @@ -17,7 +18,7 @@ //! `engine/tob_renders.ll` and `engine/ob2_loaded_pointer.ll` (`llvm-as`). use super::blit_holds::{ - busy, device, over_host, until, Host, Scratch, BYTES, FILLED, SOURCE, WAIT, + busy, device, over_host, shadow_device, until, Host, Scratch, BYTES, FILLED, SOURCE, WAIT, }; use crate::error::RuntimeGap; use crate::{ @@ -87,6 +88,13 @@ enum Leg { IndirectDispatch, /// A dispatch that writes a texture over it. TextureWrite, + /// The same, the texture's rows a texel short of their pitch, so that a line of the device's + /// memory holds each row's last texels and bytes beside them, and every texture over host + /// pages a shadow (`HostPageTexturePolicy::Shadow`, under which the check gives no answer): + /// its write back merges on the host (`shadow_splice.rs`, **Merges**), in a host step whose + /// drain holds none of the program's objects, so the import goes at the completion as for + /// every other leg, though the command buffer is kept. + TextureMergedOnHost, /// A dispatch that reads and writes it through an address held in memory, made resident with /// `use_resource`. UsedResource, @@ -230,11 +238,11 @@ pub(super) fn pass(target: &Texture) -> RenderPassDescriptor { pass } -/// An `RGBA8Unorm` texture over the no-copy buffer's bytes of `usage`; `None`, printed, where the -/// device makes none. -fn over_buffer(no_copy: &Buffer, usage: TextureUsage) -> Option { +/// An `RGBA8Unorm` texture over the no-copy buffer's bytes of `usage`, `width` texels a row of +/// [`ROW`] bytes; `None`, printed, where the device makes none. +fn over_buffer(no_copy: &Buffer, usage: TextureUsage, width: u64) -> Option { let mut descriptor = - TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, OVER_WIDTH, OVER_HEIGHT, false); + TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, width, OVER_HEIGHT, false); descriptor.usage = usage; descriptor.resource_options = ResourceOptions::default(); match no_copy.new_texture(&descriptor, 0, ROW) { @@ -276,7 +284,17 @@ fn encode( Leg::KernelReads | Leg::KernelWrites | Leg::IndirectDispatch => { Some(dispatch(leg, device, command_buffer, host, no_copy)) } - Leg::TextureWrite => texture_write(device, command_buffer, host, no_copy), + Leg::TextureWrite => texture_write(device, command_buffer, host, no_copy, OVER_WIDTH), + Leg::TextureMergedOnHost => { + // A row's last texel and the bytes beside it share a line only where a line holds + // more than a texel. + let line = device.shared().in_place.line(); + if line <= 4 { + eprintln!("ENCODER-HOLDS: lines of {line} bytes hold no byte beside a texel"); + return None; + } + texture_write(device, command_buffer, host, no_copy, OVER_WIDTH - 1) + } Leg::UsedResource => Some(used_resource(device, command_buffer, host, no_copy)), Leg::UsedHeap => Some(used_heap(device, queue, command_buffer, host, no_copy)), Leg::IndirectCommands => Some(indirect_commands(device, command_buffer, host, no_copy)), @@ -456,7 +474,7 @@ fn attachment( host: &Host, no_copy: &Buffer, ) -> Option { - let target = over_buffer(no_copy, TextureUsage::RENDER_TARGET)?; + let target = over_buffer(no_copy, TextureUsage::RENDER_TARGET, OVER_WIDTH)?; let state = render_state(device, "engine/tob_renders.ll", "tob_rect", "tob_solid"); host.fill(SOURCE); let mut encoder = command_buffer @@ -567,27 +585,28 @@ fn dispatch( } /// The texture write leg: `invert_texels` from a texture of zeros into a texture over the no-copy -/// buffer, which takes `(1, 1, 1, 0)`. +/// buffer, `width` texels a row, which takes `(1, 1, 1, 0)`. fn texture_write( device: &Device, command_buffer: &CommandBuffer, host: &Host, no_copy: &Buffer, + width: u64, ) -> Option { - let destination = over_buffer(no_copy, TextureUsage::SHADER_WRITE)?; + let destination = over_buffer(no_copy, TextureUsage::SHADER_WRITE, width)?; let library = library(device, "examples/runtime_examples.ll"); let function = library.new_function_with_name("invert_texels").unwrap(); let state = device .new_compute_pipeline_state_with_function(&function, &PipelineOptions::default()) .unwrap(); let mut descriptor = - TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, OVER_WIDTH, OVER_HEIGHT, false); + TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, width, OVER_HEIGHT, false); descriptor.usage = TextureUsage::SHADER_READ; descriptor.resource_options = ResourceOptions::shared(); let source = device.new_texture(&descriptor).unwrap(); source .replace_region( - Region::new_2d(0, 0, OVER_WIDTH, OVER_HEIGHT), + Region::new_2d(0, 0, width, OVER_HEIGHT), 0, 0, &vec![0; BYTES], @@ -601,7 +620,7 @@ fn texture_write( encoder.set_texture(Some(&source), 0).unwrap(); encoder.set_texture(Some(&destination), 1).unwrap(); encoder - .dispatch_threads(Size::new(OVER_WIDTH, OVER_HEIGHT, 1), Size::new(8, 8, 1)) + .dispatch_threads(Size::new(width, OVER_HEIGHT, 1), Size::new(8, 8, 1)) .unwrap(); encoder.end_encoding().unwrap(); Some(Encoded { @@ -611,11 +630,21 @@ fn texture_write( watch("the texture over the buffer", destination.held()), ], released: vec![Box::new(state), Box::new(source), Box::new(destination)], - check: Box::new(|host| { - assert!( - host.holds(u32::from_le_bytes([255, 255, 255, 0])), - "the dispatch's texels are not in the pages under the texture" - ); + // Each row's texels hold the dispatch's, and the bytes past them to the pitch the pages' + // own, which the dispatch did not write. + check: Box::new(move |host| { + let texel = u32::from_le_bytes([255, 255, 255, 0]); + for (at, row) in host.words().chunks((ROW / 4) as usize).enumerate() { + let (texels, padding) = row.split_at(width as usize); + assert!( + texels.iter().all(|&word| word == texel), + "row {at}: the dispatch's texels are not in the pages under the texture" + ); + assert!( + padding.iter().all(|&word| word == SOURCE), + "row {at}: the bytes past its texels changed" + ); + } }), }) } @@ -873,12 +902,16 @@ fn sparse_mapping( fn run(leg: Leg) { let scratch = Scratch::new(&format!("encoder-{leg:?}")); let host = Host::new(); - let device = device(&scratch.0); + let device = match leg { + Leg::TextureMergedOnHost => shadow_device(&scratch.0), + _ => device(&scratch.0), + }; let queue = device.new_command_queue().unwrap(); let target = device .new_buffer(FILLED, ResourceOptions::private()) .unwrap(); for release in [Release::Committed, Release::Scheduled] { + let host_steps = device.work_counts().shadow_host_steps; let runs = Arc::new(AtomicUsize::new(0)); let Some(no_copy) = over_host(&device, &host, &runs) else { return; @@ -927,6 +960,11 @@ fn run(leg: Leg) { command_buffer.wait_until_completed_for(WAIT).unwrap(); assert_eq!(command_buffer.error(), None); busy.wait_until_completed_for(WAIT).unwrap(); + if leg == Leg::TextureMergedOnHost { + let steps = device.work_counts().shadow_host_steps - host_steps; + eprintln!("ENCODER-HOLDS {leg:?} released {release:?}: host steps {steps}"); + assert!(steps > 0, "{leg:?}: no merge on the host ran"); + } until("the deallocator never ran", || { runs.load(Ordering::SeqCst) == 1 }); @@ -985,6 +1023,11 @@ fn a_no_copy_buffer_a_committed_dispatch_writes_a_texture_over_keeps_its_import_ run(Leg::TextureWrite); } +#[test] +fn a_no_copy_buffer_a_committed_dispatch_merged_on_the_host_keeps_its_import_until_completion() { + run(Leg::TextureMergedOnHost); +} + #[test] fn a_no_copy_buffer_a_committed_dispatch_reaches_by_address_keeps_its_import_until_completion() { run(Leg::UsedResource);