✨ Half the bytes this device gathers sit behind a shader-declared bound nothing applies, so the floor stops being asked the wrong question

Component: `crates/reims-vgpu`, x86/Vulkan pathway. New arm
`REIMS_VGPU_EXTENT_NARROW`, **default off**. No default behavior changes.

# The number

`note_extent_headroom` counts, per bind that takes the gather rail, the span it
will move and how much of it a declared object size would remove. Two driven
macos-13 sustained-animation boots, summed over each boot:

                           boot 1      boot 2
bext_capped_span_bytes     274.1 GB    272.9 GB
bext_would_save_bytes      274.0 GB    272.9 GB    99.97 % of the capped
bext_uncapped_span_bytes   287.3 GB    283.7 GB
gather_bytes               236.8 GB    234.9 GB

**49 % of every byte this rail moves sits behind a cap that would remove 99.97 %
of it.** The gather is ~55 % of all GPU time this device spends — established by
four `REIMS_VGPU_GUEST_IMPORT=off` boots at 6.0-6.9 GPU us/draw against eight
import-on boots at 13.7-16.0 — so this is the largest identified cut to the
largest cost in the device.

# Why it was unreachable

`ZERO_COPY_BUFFER_MIN_BYTES` is 16 KiB and it was compared against the
**narrowed** span. Essentially every declared object is 512 bytes or less (a
boot reads 1.07 M `Object` verdicts at 64 bytes or under and 1.03 M between 65
and 512, against 79 above 64 KiB), so narrowing always dropped the bind under
the floor — which would move it off the gather rail and out of the
held-resolution registry. `load_buffer_content` therefore dropped every
sub-floor cap before the rail saw it, and both narrowing counters read zero
across 2.57 million binds.

The floor is the right gate asked of the wrong length. Whether a bind is worth
the zero-copy rail is a question about the window the guest bound; how much of
that window one shader reads is a different question. So `full` now answers the
first and the narrowed span answers the second. On the default arm the two are
the same number — the caller still hands the function only caps that clear the
floor — so that line is a no-op there.

# Why this is not the attempt that was already refused

`load_buffer_content`'s doc records applying the cap to the rail *decision*: it
saved 2.2 GB a run, halved `zc_buffer_held` by pushing binds onto CPU reads, and
bought no time. This arm cannot do that. Every bind stays on the rail it was on
with the same registry entry; only the extent walked and copied moves. And that
2.2 GB was ranked on `draw_us/draw`, a CPU wall clock, before this device could
time the GPU at all — the gather is a PCIe copy and no CPU counter was going to
see it.

# Why it is off

Reading fewer bytes than a shader touches is wrong pixels with no error
anywhere, and the whole safety argument is the translator's `Object` verdict
meaning what it says. `robustBufferAccess` bounds-clamps an over-read rather
than leaving it undefined, so the failure mode is visibly wrong rather than
unsound — still wrong. It stays off until an A/B has both the `gpu_span` reading
and a screenshot.

# Verification

1895 + 82 tests pass, 6 ignored (wire fixtures, absent on a non-Apple checkout).
The cap selection is now a named function both arms go through, so the test
asserts the rule rather than restating the filter — the previous test
re-implemented the closure and could only agree with itself. Two tests: the
default arm drops every cap in the population that actually exists, and the
narrowing arm keeps exactly those while changing nothing about caps already
above the floor.

Clippy `-D warnings` clean on all three arms.

# Not verified

**The arm has not been booted.** No screenshot, no `us/draw`, no correctness
evidence of any kind for `=on` — that is the next run, and the default is
unchanged until it lands. macos-13, x86/Vulkan, discrete host only.
This commit is contained in:
Anees Iqbal committed 2026-08-11 20:02:16 +03:00
1 parent 8c7fc027b5
commit 0005766b2a
3 files changed
+165 -17

No files matched your search

+56 -1
View File
@@ -668,6 +668,60 @@ pub const GPU_SPANS: &str = "REIMS_VGPU_GPU_SPANS";
/// one.
pub const LAYOUT_CHURN: &str = "REIMS_VGPU_LAYOUT_CHURN";
/// **Default off.** `on` lets a declared object size narrow the guest bytes the
/// gather rail *moves*, instead of only narrowing binds that stay above the
/// zero-copy floor after narrowing — which is none of them.
///
/// # Why the rail is inert without it, and why the floor is the reason
///
/// `spirv_bind::reflected_buffer_extent` answers `Object` for **57 % of buffer
/// binds** at the current translator pin, and essentially every answer is 512
/// bytes or less. `ZERO_COPY_BUFFER_MIN_BYTES` is 16 KiB, so
/// `load_buffer_content` drops every one of those caps before the rail sees it,
/// and `try_buffer_zero_copy_resolved` then resolves the whole rest of the
/// allocation. Both narrowing counters read **zero** across a driven boot of 2.57
/// million binds.
///
/// The bytes behind that, two driven macos-13 boots, summed over the boot:
///
/// ```text
/// boot 1 boot 2
/// bext_capped_span_bytes 274.1 GB 272.9 GB
/// bext_would_save_bytes 274.0 GB 272.9 GB (99.97 % of the capped)
/// bext_uncapped_span_bytes 287.3 GB 283.7 GB
/// ```
///
/// **49 % of every byte this rail moves sits behind a cap that would remove
/// 99.97 % of it**, and the gather is ~55 % of all the GPU time this device
/// spends (see [`GUEST_IMPORT`]).
///
/// # What `on` changes, and what it deliberately does not
///
/// Only which length is compared against the floor. The floor decides whether a
/// bind is worth the zero-copy rail *at all*, and that is a question about the
/// window the guest bound — so it is asked of the full span on both arms. The
/// narrowed length then decides how much of that window is walked and copied.
///
/// That distinction is the whole point. `load_buffer_content`'s doc records an
/// earlier attempt that applied the cap to the rail *decision*: it saved 2.2 GB a
/// run, halved `zc_buffer_held` by pushing binds off the registry onto CPU reads,
/// and bought no time. This arm cannot do that — every bind stays on the rail it
/// was on, with the same registry entry, and only the extent moves.
///
/// It was also ranked on `draw_us/draw`, a CPU wall clock, before this device
/// could time the GPU. The gather is a PCIe copy; no CPU counter was going to see
/// it.
///
/// # Why it is off by default
///
/// Reading fewer bytes than a shader touches is wrong pixels with no error
/// anywhere, and the entire safety argument is the translator's `Object` verdict
/// meaning what it says. `robustBufferAccess` bounds-clamps an over-read rather
/// than leaving it undefined, so the failure mode is visibly wrong rather than
/// unsound — but visibly wrong is still wrong, and this stays off until an A/B
/// has both the `gpu_span` reading and a screenshot.
pub const EXTENT_NARROW: &str = "REIMS_VGPU_EXTENT_NARROW";
/// What one variable says, including the two ways it says nothing usable.
///
/// Four states rather than a `bool` because "unset", "explicitly on" and
@@ -741,7 +795,7 @@ pub fn switch(name: &str) -> Switch {
/// Nothing enforces that a new `pub const` above is added to this list; the rule
/// is stated and honestly unenforced. What keeps it small is that the list is
/// next to the constants, and [`report_line`] is the only consumer.
pub const ALL: [&str; 18] = [
pub const ALL: [&str; 19] = [
LAZY_WRITEBACK,
SLAB_RETAIN,
CLEAR_SEED,
@@ -765,6 +819,7 @@ pub const ALL: [&str; 18] = [
COMPUTE_GATHER,
GPU_SPANS,
LAYOUT_CHURN,
EXTENT_NARROW,
];
/// The state of every variable in [`ALL`], for the one-shot boot line.
+43 -5
View File
@@ -164,17 +164,55 @@ fn sampled_zero_copy_floor_separates_video_from_small_binds() {
#[cfg(feature = "backend-vulkan")]
#[test]
fn an_extent_cap_below_the_gather_floor_is_not_applied_to_the_gather_rail() {
let keeps = |cap: u64| Some(cap).filter(|&c| c >= ZERO_COPY_BUFFER_MIN_BYTES);
use super::vulkan::gather_cap_for;
// The population today's shaders actually declare: 60 captured AIR blobs
// ran median 64 bytes, max 512. None of these may reach the gather rail.
// The population today's shaders actually declare: 60 captured AIR blobs ran
// median 64 bytes, max 512, and a driven boot reads 1.03 million `Object`
// verdicts a boot between 65 and 512 bytes against 79 above 64 KiB. None of
// these may reach the gather rail on the default arm.
for cap in [4u64, 64, 288, 512, ZERO_COPY_BUFFER_MIN_BYTES - 1] {
assert_eq!(keeps(cap), None, "cap {cap} must not narrow the gather");
assert_eq!(
gather_cap_for(Some(cap), false),
None,
"cap {cap} must not narrow the gather by default"
);
}
// A declared object at or above the floor still narrows: the bind stays on
// the rail, keeps its registry entry, and gathers less.
for cap in [ZERO_COPY_BUFFER_MIN_BYTES, ZERO_COPY_BUFFER_MIN_BYTES + 1] {
assert_eq!(keeps(cap), Some(cap), "cap {cap} should still narrow");
assert_eq!(gather_cap_for(Some(cap), false), Some(cap), "cap {cap}");
}
// A bind with no declared object has nothing to narrow by on either arm.
assert_eq!(gather_cap_for(None, false), None);
assert_eq!(gather_cap_for(None, true), None);
}
/// `REIMS_VGPU_EXTENT_NARROW=on` keeps the caps the floor drops, which is the
/// whole of the arm.
///
/// The one that matters is the population above: 512 bytes and under is where
/// essentially every declared object sits, so an arm that kept only the caps
/// already above 16 KiB would be the default arm with extra steps and would
/// measure as no change — which is exactly how a switch gets called neutral.
#[cfg(feature = "backend-vulkan")]
#[test]
fn the_narrowing_arm_keeps_the_caps_the_floor_drops() {
use super::vulkan::gather_cap_for;
for cap in [4u64, 64, 288, 512, ZERO_COPY_BUFFER_MIN_BYTES - 1] {
assert_eq!(
gather_cap_for(Some(cap), true),
Some(cap),
"cap {cap} is the population this arm exists for"
);
}
// And it changes nothing about the caps that already cleared the floor.
for cap in [ZERO_COPY_BUFFER_MIN_BYTES, 1 << 20] {
assert_eq!(
gather_cap_for(Some(cap), true),
gather_cap_for(Some(cap), false),
"cap {cap} must not depend on the arm"
);
}
}
+66 -11
View File
@@ -2700,12 +2700,17 @@ fn try_buffer_zero_copy_resolved<M: HostMemory + HostOps>(
Some(cap) => span.min(cap),
None => span,
};
if span < ZERO_COPY_BUFFER_MIN_BYTES {
// Narrowing cannot be what put a bind here: the caller only hands this
// function a cap that already clears the floor, so a sub-floor span means
// the allocation itself is sub-floor and the cap never bound anything.
// That is why this stayed one route — a `_narrowed` sibling would have
// been a counter that cannot count.
if full < ZERO_COPY_BUFFER_MIN_BYTES {
// `full` and not the narrowed `span`, because the floor asks whether this
// *window* is worth the zero-copy rail and that is a property of what the
// guest bound, not of how much of it one shader reads. Comparing the
// narrowed span instead is what made a 64-byte declared object cost the
// bind its registry entry and fall to a CPU read.
//
// On the default arm the two are the same number: the caller only hands
// this function a cap that already clears the floor, so `span == full`
// whenever `full >= FLOOR`. The distinction only bites with
// `REIMS_VGPU_EXTENT_NARROW=on`.
crate::runtime::drain::note_store_route("zc_buffer_below_floor");
return None;
}
@@ -2897,6 +2902,47 @@ fn bound_buffer_content(
/// wall clock, before this device could time the GPU. The gather is a PCIe copy
/// and about 55 % of all GPU time here, and no CPU counter was ever going to
/// see it.
/// Whether a declared object size may narrow the bytes the gather rail moves.
///
/// **Default off** — see [`crate::env::EXTENT_NARROW`] for the bytes at stake,
/// for what separates this from the earlier attempt that applied the cap to the
/// rail *decision*, and for why an arm that reads fewer guest bytes has to earn
/// its default with a screenshot as well as a number.
/// Which cap the gather rail and the held-resolution registry key carry.
///
/// A function rather than two lines at the call site because it is the whole of
/// the difference between the two arms, and because a test can then assert the
/// rule instead of restating it — the test that used to cover this re-implemented
/// the filter and so could only ever agree with itself.
///
/// `narrow` false is the default: a cap below [`ZERO_COPY_BUFFER_MIN_BYTES`] is
/// dropped, because the floor is compared against the narrowed span and a
/// 64-byte object would move the bind off the rail and out of the registry. Every
/// object this workload declares is 512 bytes or under, so that drops all of
/// them.
///
/// `narrow` true keeps every cap, and
/// [`try_buffer_zero_copy_resolved`] then compares the floor against the *full*
/// span instead, so the bind stays on the rail it was on and only the extent
/// walked and copied moves.
pub(super) fn gather_cap_for(extent_cap: Option<u64>, narrow: bool) -> Option<u64> {
if narrow {
extent_cap
} else {
extent_cap.filter(|&cap| cap >= ZERO_COPY_BUFFER_MIN_BYTES)
}
}
fn extent_narrow_enabled() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| {
matches!(
crate::env::read(crate::env::EXTENT_NARROW).0,
crate::env::Switch::On
)
})
}
fn note_extent_headroom(extent_cap: Option<u64>, span: u64) {
let Some(cap) = extent_cap else {
crate::runtime::drain::note_store_route_n("bext_uncapped_span_bytes", span);
@@ -2917,11 +2963,20 @@ fn load_buffer_content<M: HostMemory + HostOps>(
allow_zero_copy: bool,
extent_cap: Option<u64>,
) -> Option<crate::backend::vulkan::engine::BufferContent> {
// Below the floor the cap would cost the bind its registry entry, so the
// gather rail and the registry key both ignore it there. Keyed on the same
// value the walk used, or a lookup would answer with a span it did not ask
// for.
let gather_cap = extent_cap.filter(|&cap| cap >= ZERO_COPY_BUFFER_MIN_BYTES);
// Which cap the rail and the registry key carry. Keyed on the same value the
// walk used, or a lookup would answer with a span it did not ask for.
//
// Default: below the floor the cap is dropped, because it would cost the bind
// its registry entry — the floor was compared against the *narrowed* span, so
// a 64-byte object turned a registry hit into a CPU read. Since every object
// this workload declares is 512 bytes or less, that drops all of them and the
// rail is inert.
//
// `REIMS_VGPU_EXTENT_NARROW=on` keeps the cap and moves the floor comparison
// onto the full span instead (`try_buffer_zero_copy_resolved`), so the bind
// stays on exactly the rail it was on and only the extent walked and copied
// narrows.
let gather_cap = gather_cap_for(extent_cap, extent_narrow_enabled());
// A held resolution answers before anything is resolved at all: the walk
// below produces the same runs until the guest moves the addresses, and it
// announces every such move. This is the whole point of the registry — see