Bound max_buffer_length by three quarters of the device's largest device-local heap, as Metal does

The RTX 5080 Laptop GPU answered max_buffer_length 2^40 (its maxBufferSize; maxMemoryAllocationSize
is u64::MAX), a length no allocation of its 16 GiB can meet. Metal answers three quarters of
recommendedMaxWorkingSetSize, measured with MTLCopyAllDevices on the cluster's Macs:
- AMD Radeon Pro 5300M (4 GiB, macOS 26.7.1): maxBufferLength 3,208,642,560 of
  recommendedMaxWorkingSetSize 4,278,190,080, exactly three quarters;
- Apple M3 Max (64 GiB, macOS 27.0.1): 41,747,087,360 of 55,662,788,608, three quarters rounded
  down to 16 KiB.
(Intel UHD Graphics 630 on the same MacBook answers 2 GiB of a 1.5 GiB working set: outside the
rule, and no venue of ours.)

What: the runtime reads the size of the device's largest DEVICE_LOCAL heap as it queries the
physical device (DeviceIdentity::device_local_heap) and answers the compile layer's
max_buffer_length bound by working_set_buffer_bound(heap), three quarters of it to 16 KiB; the
runtime holds no working set size, so the heap stands for it. On MoltenVK the M3 Max's heap is
64 GiB (vulkaninfo), whose bound (48 GiB) lies above the compile layer's 41,747,087,360, Metal's
own, which stays. Buffers past the new length are refused nil as before, as Metal refuses them.
The compile layer's MetalDeviceProperties::max_buffer_length (what a shader can address) is
unchanged.

Tests: a runtime unit test of the rule on both Metal measurements and on heaps above, below and
at the limit; the lane's capability check takes the bound and prints both values.

Verified: compile and the 5080 and M3 runs follow in the lane proof run.
(cherry picked from commit 11f1133bd6f80f1917a4f93bb37b2c1402946a40)
This commit is contained in:
Anees Iqbal committed 2026-11-05 05:10:58 -08:00
1 parent ad905b8425
commit 8a0ff9110f
5 files changed
+91 -7

No files matched your search

@@ -107,10 +107,16 @@ pub struct DeviceProperties {
pub drm_render: Option<(i64, i64)>,
/// The Vulkan version the device runs at: the lower of the instance's and the device's.
pub api_version: ApiVersion,
/// `maxBufferLength`: the longest buffer a shader can bind whole, the compile layer's answer:
/// `maxStorageBufferRange` where buffers are reached through descriptors only, else the lower
/// of `maxBufferSize` and `maxMemoryAllocationSize` (the device's allocation sizes, which
/// follow its machine's memory on integrated GPUs, as Metal's answer does).
/// `maxBufferLength`: the longest buffer a shader can bind whole, the compile layer's answer
/// (`maxStorageBufferRange` where buffers are reached through descriptors only, else the lower
/// of `maxBufferSize` and `maxMemoryAllocationSize`), at most three quarters of the device's
/// largest device-local heap, rounded down to 16 KiB ([`working_set_buffer_bound`]). Metal
/// answers three quarters of its `recommendedMaxWorkingSetSize`: 3,208,642,560 of 4,278,190,080
/// on an AMD Radeon Pro 5300M (4 GiB, macOS 26.7.1), and 41,747,087,360 of 55,662,788,608, to
/// 16 KiB, on an Apple M3 Max (64 GiB, macOS 27.0.1); the runtime holds no working set size and
/// takes the heap for it, so a discrete GPU's answer follows its memory, as Metal's does, where
/// its Vulkan limits alone allow far more (`maxBufferSize` 1 TiB on NVIDIA's 615.71.09). On
/// MoltenVK the bound is above the compile layer's answer, Metal's own.
pub max_buffer_length: u64,
/// `maxThreadsPerThreadgroup`: per axis, `maxComputeWorkGroupSize` capped by
/// `maxComputeWorkGroupInvocations` and by an Apple GPU's 1024.
@@ -197,6 +203,16 @@ pub(crate) struct DeviceIdentity {
/// The sample counts every color and depth image can be sampled at:
/// `sampledImageColorSampleCounts & sampledImageDepthSampleCounts`.
pub sampled_sample_counts: SampleCountFlags,
/// The size of the device's largest `DEVICE_LOCAL` memory heap, where it was read
/// (`VkPhysicalDeviceMemoryProperties`): what bounds [`DeviceProperties::max_buffer_length`].
pub device_local_heap: Option<u64>,
}
/// The longest buffer Metal's rule allows a device whose working set is `working_set` bytes:
/// three quarters of it, rounded down to 16 KiB ([`DeviceProperties::max_buffer_length`]).
pub(crate) fn working_set_buffer_bound(working_set: u64) -> u64 {
const PAGE: u64 = 16 << 10;
working_set / 4 * 3 / PAGE * PAGE
}
/// The class of Mac a device presents, whose GPU decides answers Metal gives without a property
@@ -264,6 +280,11 @@ impl DeviceProperties {
) -> DeviceProperties {
let lossless_compression = presents_lossless_compression(&identity, mac_class);
let metal = compile.metal_properties();
let max_buffer_length = identity
.device_local_heap
.map_or(metal.max_buffer_length, |heap| {
metal.max_buffer_length.min(working_set_buffer_bound(heap))
});
let [width, height, depth] = metal.max_threads_per_threadgroup.map(u64::from);
let texture_sample_counts = metal.raster_sample_counts & identity.sampled_sample_counts;
let max_vertex_amplification_count = if description.has(VkFeature::Multiview) {
@@ -282,7 +303,7 @@ impl DeviceProperties {
pci_bus: identity.pci_bus,
drm_render: identity.drm_render,
api_version: identity.api_version,
max_buffer_length: metal.max_buffer_length,
max_buffer_length,
max_threads_per_threadgroup: Size::new(width, height, depth),
max_threadgroup_memory_length: u64::from(metal.max_threadgroup_memory_length),
argument_buffers_support: metal.argument_buffers_tier,
@@ -152,6 +152,15 @@ pub(super) unsafe fn query(
let core = properties.properties;
// SAFETY: the caller's contract.
let (pci_bus, drm_render) = unsafe { bus_addresses(instance, physical) };
// SAFETY: the caller's contract.
let memory = unsafe { instance.get_physical_device_memory_properties(physical) };
let heaps =
&memory.memory_heaps[..(memory.memory_heap_count as usize).min(vk::MAX_MEMORY_HEAPS)];
let device_local_heap = heaps
.iter()
.filter(|heap| heap.flags.contains(vk::MemoryHeapFlags::DEVICE_LOCAL))
.map(|heap| heap.size)
.max();
let identity = DeviceIdentity {
name: core
.device_name_as_c_str()
@@ -173,6 +182,7 @@ pub(super) unsafe fn query(
& core.limits.sampled_image_depth_sample_counts)
.as_raw(),
),
device_local_heap,
};
(identity, v11.max_memory_allocation_size)
}
@@ -5,7 +5,7 @@ use std::path::PathBuf;
use crate::capabilities::{
amplification_count_supported, rate_map_layers_supported, sample_count_supported,
DeviceIdentity, RATE_MAP_LAYERS,
working_set_buffer_bound, DeviceIdentity, RATE_MAP_LAYERS,
};
use crate::{
BufferTextureProblem, DeviceKind, DeviceProperties, Error, Invalid, MacClass, Problem, Query,
@@ -81,6 +81,7 @@ fn identity(stem: &str) -> DeviceIdentity {
gpu_core_count: None,
sampled_sample_counts: profile_counts(stem, "sampledImageColorSampleCounts")
& profile_counts(stem, "sampledImageDepthSampleCounts"),
device_local_heap: None,
}
}
@@ -176,6 +177,44 @@ fn lossless_compression_is_the_presented_mac_class() {
}
}
/// Metal's rule for `maxBufferLength`, three quarters of the working set to 16 KiB, gives what
/// Metal answered: an AMD Radeon Pro 5300M (4 GiB, macOS 26.7.1) of `recommendedMaxWorkingSetSize`
/// 4,278,190,080 and an Apple M3 Max (64 GiB, macOS 27.0.1) of 55,662,788,608. A device's answer
/// is the compile layer's bound by its largest device-local heap's (a 16 GiB heap's 12 GiB, a heap
/// of the limit's size three quarters of it), and the compile layer's where the bound lies above
/// it or no heap was read.
#[test]
fn max_buffer_length_is_three_quarters_of_the_device_local_heap_at_most() {
assert_eq!(working_set_buffer_bound(4_278_190_080), 3_208_642_560);
assert_eq!(working_set_buffer_bound(55_662_788_608), 41_747_087_360);
let (stem, desc) = &committed_descriptions()[0];
let policy = DevicePolicy::UNTRUSTED;
let enabled = desc
.clone()
.with_enabled(&desc.recommended_enables(&policy))
.unwrap_or_else(|e| panic!("{stem}: {e}"));
let compile = Device::new(enabled.clone(), policy).unwrap_or_else(|e| panic!("{stem}: {e}"));
let limit = compile.metal_properties().max_buffer_length;
for heap in [
None,
Some(16 << 30),
Some(64 << 30),
Some(limit),
Some(u64::MAX),
] {
let identity = DeviceIdentity {
device_local_heap: heap,
..identity(stem)
};
let p = DeviceProperties::new(identity, enabled.clone(), &compile, None);
let want = heap.map_or(limit, |heap| {
limit.min(heap / 4 * 3 / (16 << 10) * (16 << 10))
});
assert_eq!(p.max_buffer_length, want, "{stem}: heap {heap:?}");
assert!(p.max_buffer_length <= limit, "{stem}: heap {heap:?}");
}
}
/// Every answer equals its source on every committed profile: the Vulkan limit, feature or
/// format the device was created with, or the compile layer's answer for it, an Apple GPU's at
/// most.
@@ -39,6 +39,7 @@ fn identity(
max_viewports: 16,
gpu_core_count: None,
sampled_sample_counts: SampleCountFlags::empty(),
device_local_heap: None,
}
}
@@ -125,7 +125,20 @@ fn a_runtime_device_answers_its_queries_and_the_apple_only_calls() {
assert_eq!(p.vendor_id, d.vendor_id);
assert_eq!(p.device_id, d.device_id);
assert_eq!(p.api_version, d.api_version);
assert_eq!(p.max_buffer_length, metal.max_buffer_length);
// The compile layer's answer, at most three quarters of the device's largest device-local
// heap, to 16 KiB (`DeviceProperties::max_buffer_length`).
println!(
"CAPABILITIES {}: max_buffer_length {} of the compile layer's {}",
p.name, p.max_buffer_length, metal.max_buffer_length
);
assert!(
p.max_buffer_length == metal.max_buffer_length
|| (p.max_buffer_length < metal.max_buffer_length
&& p.max_buffer_length.is_multiple_of(16 << 10)),
"max_buffer_length {} against the compile layer's {}",
p.max_buffer_length,
metal.max_buffer_length
);
let [width, height, depth] = metal.max_threads_per_threadgroup.map(u64::from);
assert_eq!(
p.max_threads_per_threadgroup,