RUNTIME 2269: a bit-24 no-copy buffer's blit copies read its pages in place through a read twin

Off macOS, a no-copy buffer made with Metal's read-only option bit 24 is a
copied host buffer (CopiedFor::ReadOnly) whose copy takes every write; every
commit that used it compared the whole copy against the pages (reims's
12.7.6 walk: 130,448 MiB compared, 34,462 compare threads, copy-in bytes 0).

Now such a buffer also gets a read twin: the runtime's own buffer over the
same pages, imported in place, outside the Metal address space (key bit 63),
made only under the Contained policy and only where the pages import in
place (never a copy). copyFromBuffer:toBuffer and copyFromBuffer:toTexture
read through the twin until an earlier command of the same command buffer
may have written the buffer (a write of its key, writes_unnamed, or unnoted
uses); after that they read the copy, so the command buffer reads its own
dropped write back and the next command buffer reads the pages. Every
encoder refuses a command whose own uses write a twin key: it records
nothing and fails its command buffer. The twin is placed over the bytes
before its buffer, so a twin use does not drag in the copy.

Counted: read_twins_made, read_twins_unimported, read_twins_uncontained,
read_twin_blit_reads, read_twin_reads_after_writes, read_twin_writes_refused,
copied_copy_ins_compared, copied_copy_in_bytes_compared. Docs edited in
place: the FAST-PATHS row "Off macOS, GPU writes into read-only memory",
API.md's copied-host-pages and private-resource-option rows, read_only_view.rs.

RUNTIME_VERSION 2269: blit sources of bit-24 buffers read in place.

Verified (roip, lavapipe m3-max-28537 at ad4ed6a591, 40/40 read_twin_device|copied_device):
a 29,753,344-byte (3648x2038x4) buffer's copyFromBuffer:toTexture + buffer copy
compare [0 copies, 0 bytes, 0 threads] (was [1, 29,753,344, 7]), texels and
bytes match the pages; fill + copy-out in one CB reads the fill back, pages
unchanged, next CB in place; blit fill, blit copy-in and a kernel store through
a loaded pointer at gpuAddress leave the pages; a fill of the twin is refused
(negative control: with the guard off it changes the pages); no twin when the
import is refused, over read-only pages, or under UnsafeOff. Owed after
landing: 5080, clippy x3, rustdoc. Next steps: sampled textures over the buffer,
buffer slots, resident sets.

Squashed from steelbrain/roip 4d4bef55bd..ad4ed6a591 (encoder.rs import
conflict with RUNTIME 2268's encoder_fold resolved by keeping both).
This commit is contained in:
Anees Iqbal committed 2026-11-14 01:14:50 -08:00
1 parent 3b0f5f3ca4
commit 0043991660
21 files changed
+984 -24

No files matched your search

+10 -2
View File
@@ -2926,7 +2926,8 @@ bound.
of, is made as without it, and reports its options without it, as Metal's does. A no-copy
buffer or heap with the bit drops the GPU's writes as Metal's does: on macOS its pages are
imported through a read-only view of them, announcing nothing; elsewhere it is made over a copy
of them that drops the GPU's writes (`copied-host-pages`). A buffer of the runtime's memory or a texture descriptor with the bit is
of them that drops the GPU's writes (`copied-host-pages`), a no-copy buffer's blit copies out of
it reading the pages in place where they import in place (`copied-host-pages` says how). A buffer of the runtime's memory or a texture descriptor with the bit is
refused, as Metal ends the process there; a buffer placed in a heap without the bit is made as
without it, announcing nothing. Cost: the GPU's writes reach the resource's memory, the CPU and
later command buffers, where Metal's never reach them.
@@ -3695,7 +3696,14 @@ bound.
out of the pages, the gate not asked (`copied_read_only_memory`): the copy drops the GPU's writes
as a copy of read-only pages does, though a command buffer committed before the writer's batch
completed may still read them, and a render pass's store-out into it completes where Metal's
faults its command buffer. On macOS the pages are imported through a read-only view of them. A
faults its command buffer. A blit's copy out of a no-copy buffer of such memory, into a buffer
or a texture, reads its pages in place instead, through a read twin of the buffer imported over
them (`read_twins_made`), where they import in place and the device's policy contains every
access (no twin otherwise: `read_twins_unimported`, `read_twins_uncontained`), while no earlier
command of its command buffer may have written the buffer (`read_twin_blit_reads`; after one,
`read_twin_reads_after_writes`, the copy); no command writes the twin, and one that would fails
its command buffer (`read_twin_writes_refused`). On macOS the pages are imported through a
read-only view of them. A
buffer's or heap's rebind is copied the same way,
its heap's bound textures and its images in place made again over the copy, and a buffer's
rebind to bytes at another offset in their page under a texture planned at the old one is a copy