Copy a no-copy buffer, heap or rebind whose import the caller's gate refuses or that fails, announced

A new HostMemory::with_import_gate(ImportGate) asks the caller once,
holding no lock of the runtime's, just before a no-copy buffer, a
no-copy heap or a buffer rebind imports its pages, passing an
ImportRequest with the pages the import would map. If the gate answers
false, or an import the device and the gate admitted then fails
(OutOfMemory, another Vulkan error), the object is made over a copy of
the pages instead of failing. The default stays import-first. The copy
is host-visible memory owned by the runtime; `contents` stays the
caller's pointer. Each commit copies in the bytes the CPU changed since
the last sync, and each batch's completion merges back the bytes the GPU
changed (CPU wins where both wrote), byte by byte, before events,
waiters and handlers see it. Every copy is announced
(`copied-host-pages`) and counted. Pages the process cannot read are
never copied (the first copy reads through the kernel:
process_vm_readv / mach_vm_read_overwrite). A copied heap's buffers lie
in the copy and its textures are shadows. A rebind's copy is laid out as
its import would be. A rebind over an in-place image is refused
(NotImplemented(RebindTextures)) and a heap's rebind still imports
without asking. A caller's DirtyPages log stays attached to the copy as
to an import. The in-place check's pointer class skips copied buffers.

Ports dev-perf's fff961b7e5, 630d1b0174, 33d7aa5e98 and 9b4d4190f5 onto
a smaller coherence design: every live copy is compared in full at each
commit and completion, instead of dev-perf's log-driven,
encoder-collected copies (dev-perf's copied.rs, 79301a2515 and its
fixes, sat on a page log development has replaced). Also brings
789fa3d294's unreadable-page rule, 5190983470's lane site, and
1c7d49f788's copied-buffer skip. A driver that refuses the pointer
still fails the call (dev-perf copied there too; not taken). Known gaps
in FAST-PATHS: the compare costs O(size) per commit and completion, a
byte both sides wrote keeps the CPU's value, and a shared event's value
can be seen before the merge (c5db2d1b8a not ported). RUNTIME_VERSION
1690. Runtime-sources pin recomputed.

Public API: ImportGate, ImportRequest { start, length }
(#[non_exhaustive]), HostMemory::with_import_gate,
Degradation::CopiedHostPages, Device::fail_next_host_imports (test
hook), and the WorkCounts fields host_imports_refused_by_host,
copied_host_buffers_{made,refused,degraded}, copied_copy_in_bytes,
copied_merge_back_bytes, copied_rebind_pages_made, copied_heaps_made.

Verified on the M3 Max (MoltenVK, m2v_gpu): 11 new device tests
(tests::copied_device: buffer, rebind and heap, gate refusal and failed
admitted import, unreadable pages, a heap texture round trip) and 167
tests in the touched device filters pass; the counters move as
expected ([1,1,1,0,0] for a refusal, [0,1,0,1,0] for a failed import)
and the announcement is heard; on a 64 KiB copy 4079 bytes copied in and
4080 merged back for a 4 KiB CPU write and a 4 KiB fill, checked
exactly. Sabotage: ignoring the gate and removing the merge fails the 3
buffer tests; ignoring the rebind gate fails both rebind tests (no heap
sabotage). 249 host tests pass. Workspace clippy plain and
--cfg m2v_gpu, and rustdoc, clean; clippy re-run clean on the landing
merge. Not run on Linux.
This commit is contained in:
Anees Iqbal committed 2026-10-28 23:36:31 +02:00
1 parent 8c09b7c2c0
commit 0a1851164d
28 files changed
+1736 -42

No files matched your search

+16
View File
@@ -2491,6 +2491,22 @@ bound.
writes. Cost: the GPU's writes through it reach the pages, and the CPU then reads them there,
where after Metal's GPU writes into pages the process maps read-only the CPU reads them as they
were (an M3 Max under macOS 27.0.1, private and shared memory alike).
- `metal2vulkan-degrade-copied-host-pages` (precision). `CopiedHostPages`: A no-copy buffer, a
no-copy heap or a buffer's rebind was made over a copy of the caller's host pages in memory of
the device's own, which no import maps (a heap's textures shadows of its bytes):
the caller's gate (`HostMemory::with_import_gate`) refused the pages' import, or the import the
device and the gate admitted failed (out of memory, or another Vulkan error, such as a driver out
of the mappings the host kernel allows). Pages the process cannot read are never copied: the
buffer is refused, as their import is. A copy whose own allocation fails refuses the call
(`OutOfMemory`). A heap's rebind and memory whose GPU writes Metal's private option bit 24 drops
are never copied. Counted: `WorkCounts::copied_host_buffers_made`, of them
`copied_host_buffers_refused` and `copied_host_buffers_degraded` (and `copied_heaps_made`,
`copied_rebind_pages_made`). Cost: the GPU uses a copy of
the bytes, into which each commit writes every byte the CPU changed in the pages since, and from
which each batch's completion writes back every byte the GPU changed, where Metal's GPU reads and
writes the pages themselves: a CPU write the GPU reads mid-command-buffer, or a GPU write the CPU
reads before the batch completes (behind a shared event's value among them), is not seen, and
each commit and completion compares every byte of every copy.
- `metal2vulkan-degrade-grouped-completion` (precision). `GroupedCompletion`: Batches of command
buffers committed on one queue that the scheduler found ready together, or held while two or more
earlier submissions of their queue ran, went to the driver in one `vkQueueSubmit`, only the last