From fc60379c1f965760cd37582d1e73db487d8e609f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 9 Nov 2026 01:21:48 -0800 Subject: [PATCH] Blocks made ahead stop at the allocation margin the runtime's work ahead leaves to the objects With the runtime's buffers in exportable blocks, keep_room on the 780M kept a pool of the program's Private buffers and one of its Shared ones; on a device held to a small maxMemoryAllocationCount those two blocks and the host copies' block made ahead took every allocation left, and the zero source a new Private buffer's first clear places (first_move.rs's Zeros, a plain Shared buffer) was refused: the_ahead_thread_leaves_the_allocation_limit_to_the_objects made no buffer (probe at dccbb9d3d7: three takes to the limit of 5, then Zeros::new refused). allocations.rs already says the allocations the runtime makes ahead of any object stop while fewer than a quarter of the limit are left, so the objects have those; only host_ram.rs's import thread held to it. keep_room's own makings and memory/ready.rs's makings that no call waits for now ask allocations::room_ahead before they allocate. A call that waits for a making still has it made. Verification: the failing test, the host_ram_device module and the runtime gpucfg suite on RADV PHOENIX follow. (cherry picked from commit dd5e698bff9bad8b780e6b1b2ff68e4d12cd617e) --- crates/metal2vulkan-runtime/src/allocations.rs | 12 ++++++++++-- crates/metal2vulkan-runtime/src/memory.rs | 7 ++++++- crates/metal2vulkan-runtime/src/memory/ready.rs | 8 ++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/allocations.rs b/crates/metal2vulkan-runtime/src/allocations.rs index f2266a6ee..637fddda8 100644 --- a/crates/metal2vulkan-runtime/src/allocations.rs +++ b/crates/metal2vulkan-runtime/src/allocations.rs @@ -20,8 +20,10 @@ //! the limit reached while frees are posted waits for them, then asks again, before it is //! refused. //! - **A margin the runtime's own work leaves.** [`Allocations::room_ahead`]: the allocations the -//! runtime makes ahead of any object (`host_ram.rs`'s ahead import) stop while fewer than a -//! quarter of the limit are left, so the objects and their imports have those. Once the +//! runtime makes ahead of any object (`host_ram.rs`'s ahead import, and the pooled blocks +//! `memory.rs`'s `keep_room` and `memory/ready.rs` make before anything is placed in them) stop +//! while fewer than a quarter of the limit are left, so the objects and their imports have +//! those. Once the //! device's allocations reach that quarter, it gives back the chunks it imported ahead that no //! object took ([`Allocations::made`]), so they are gone before they could take the last. @@ -203,6 +205,12 @@ pub(crate) fn register(device: vk::Device, limit: u32) -> Registered { } } +/// Whether `device` may allocate one more ahead of any object ([`Allocations::room_ahead`]): +/// always where it is not registered. +pub(crate) fn room_ahead(device: vk::Device) -> bool { + of(device).is_none_or(|allocations| allocations.room_ahead()) +} + /// `device`'s count, where it is registered. pub(crate) fn of(device: vk::Device) -> Option> { let devices = lock(); diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index d791bca6d..d332f799a 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -3826,7 +3826,9 @@ fn make_room(memory: &Mutex, device: &ash::Device, type_index: u32) -> b /// pools keep room first, so that a device near its count of allocations /// (`maxMemoryAllocationCount`) keeps it where those buffers go before anywhere else; the pools /// of [`Holds::FdBuffers`] keep none while those buffers lie in plain blocks (until the device's -/// in-place check answered, or where it refused such blocks). +/// in-place check answered, or where it refused such blocks). No block is made here while the +/// device's allocations are within the margin of its `maxMemoryAllocationCount` the runtime's +/// work ahead leaves to the objects (`allocations.rs`'s `Allocations::room_ahead`). pub(super) fn keep_room( memory: &Mutex, device: &ash::Device, @@ -3872,6 +3874,9 @@ pub(super) fn keep_room( continue; } drop(guard); + if !crate::allocations::room_ahead(device.handle()) { + break; + } let Ok(block) = maker.make(device, pool, maker.block_bytes, addressed) else { break; }; diff --git a/crates/metal2vulkan-runtime/src/memory/ready.rs b/crates/metal2vulkan-runtime/src/memory/ready.rs index 359fe6ffb..2e8f0758b 100644 --- a/crates/metal2vulkan-runtime/src/memory/ready.rs +++ b/crates/metal2vulkan-runtime/src/memory/ready.rs @@ -47,6 +47,9 @@ //! finds the making under way makes its own block. Where no block is made ahead, the call makes //! its own (`WorkCounts::buffer_blocks_made_in_place`, `image_blocks_made_in_place`). A pool's //! blocks change only at those calls, never as a making here ends. +//! - **Within the allocation margin, none.** A making no call waits for yet makes no block while +//! the device's allocations are within the margin of its `maxMemoryAllocationCount` the +//! runtime's work ahead leaves to the objects (`allocations.rs`'s `Allocations::room_ahead`). //! - **Bounded.** One making a pool at most, on a thread of its own that ends with the block. //! While it waits to be added, its bytes count against its heap's budget of kept empty blocks //! (`Memory::kept_budget`); one whose pool stopped growing for `KEPT_EMPTY_FOR`, the pool @@ -248,6 +251,11 @@ fn ready( wanted: impl Fn() -> bool, ) -> Option { let busy = exit_guard::enter()?; + // Ahead of any placement, none within the margin of the device's allocations the runtime's + // work ahead leaves (`allocations.rs`); a call waiting for it has its allocation made. + if !wanted() && !crate::allocations::room_ahead(functions.get().handle()) { + return None; + } let block = maker.make(functions.get(), pool, bytes, addressed).ok()?; if let Some(mapped) = block.mapped { let length = usize::try_from(block.size).unwrap_or(usize::MAX);