replay: the rt backend reads a Private buffer back through a blit into a Shared buffer of the play's own

The new runtime's player backend refused every read of a Private buffer
("a Private buffer": it has no contents). The gate's per-command-buffer
compare (`--dump-command-buffers`, `compare-dumps`) therefore read none of
them on rt: its play pair over glbench-780m-r2's command buffers
58254..58416 on the 3090 (m3-max-52237, development b8ea291aa5) left 41
dumps unread on rt, among them every buffer the probe gate had found
differing, and the compare said DIFFER for that alone. The probe had read
them through a blit of its own that never landed.

NewRuntime::read now copies a Private buffer's bytes with one blit into a
Shared block the play takes for the read and gives back, on a queue of the
play's own made at the first such read, and waits for the copy. The
player reads only after the work that writes the buffer completed. A
length that is not a multiple of 4 stays the copy's own refusal. The
stream's checks never named a Private buffer (a refused read ends a play),
so only the dumps change.

Test: rt_replay::a_private_buffer_reads_back_through_a_copy: a blit fills
a 16-byte Private buffer with 0x5a; the backend's read answers those
bytes. Before this change the read was refused.

Verified by the check named when this lands.
This commit is contained in:
Anees Iqbal committed 2026-11-19 07:02:13 -08:00
1 parent 2f4c9fc9dc
commit 0e9889dfaa
3 files changed
+79 -13

No files matched your search

@@ -363,6 +363,9 @@ pub struct NewRuntime {
staged_bytes: u64,
/// The bytes the host's copies back to the guest's RAM copied into the play's own sinks.
copied_back_bytes: u64,
/// The queue of the play's own that reads a Private buffer back (`private_bytes`), made at
/// the first such read.
reads: Option<rt::CommandQueue>,
/// The compute pipeline states whose `gpuResourceID` the player asked, which the runtime does
/// not give: a typed gap, counted.
unanswered_pipeline_ids: u64,
@@ -752,6 +755,7 @@ impl NewRuntime {
queue_of: HashMap::new(),
staged_bytes: 0,
copied_back_bytes: 0,
reads: None,
unanswered_pipeline_ids: 0,
block_bytes: [0; 2],
creations: Vec::new(),
@@ -917,6 +921,37 @@ impl NewRuntime {
Ok(())
}
/// The bytes of `buffer`, a Private buffer, which has no `contents`: a blit copies them into
/// a Shared buffer of the play's own, on a queue of its own (`reads`), and the read waits for
/// it. The player reads only after the work that writes the buffer completed. A length that is
/// not a multiple of 4 is the copy's own refusal (`rt::Refusal::BufferCopyUnaligned`).
fn private_bytes(&mut self, buffer: &rt::Buffer) -> Result<Vec<u8>, rt::Error> {
let length = buffer.length();
if length == 0 {
return Ok(Vec::new());
}
let queue = match &self.reads {
Some(queue) => queue.clone(),
None => (self.reads.insert(self.device.new_command_queue(1)?)).clone(),
};
let block = self.device.take_block(rt::MemoryClass::Shared, length)?;
let copied = (|| {
let into = self.device.new_buffer(block.id, 0, length, None)?;
let command_buffer = queue.command_buffer()?;
let mut blit = command_buffer.blit_command_encoder()?;
blit.copy_from_buffer_to_buffer(buffer, 0, &into, 0, length)?;
blit.end_encoding();
command_buffer.commit()?;
command_buffer.wait_until_completed()?;
let contents = into.contents().expect("a Shared buffer is mapped");
// SAFETY: the mapping holds `length` bytes, and the copy into them completed.
let bytes = unsafe { std::slice::from_raw_parts(contents.as_ptr(), length as usize) };
Ok(bytes.to_vec())
})();
self.device.release_block(block)?;
copied
}
/// Commits `command_buffer`, of `queue` and the stream's `id` (0 for one of the play's own), as
/// the next commit, in flight until its completed handler runs (`InFlight::retired`).
fn commit(
@@ -2388,18 +2423,22 @@ impl Backend for NewRuntime {
(None, Some(Object::Buffer(buffer, _))) => Some(buffer),
_ => None,
};
match buffer {
Some(buffer) => {
let contents = buffer
.contents()
.ok_or_else(|| unread("a Private buffer"))?;
let length = usize::try_from(buffer.length()).map_err(|_| unread("its length"))?;
// SAFETY: the buffer's mapping holds its length, and the player waited for the
// work that writes it.
Ok(unsafe { std::slice::from_raw_parts(contents.as_ptr(), length) }.to_vec())
}
_ => Err(unread("no buffer or texture")),
}
let Some(buffer) = buffer else {
return Err(unread("no buffer or texture"));
};
let length = usize::try_from(buffer.length()).map_err(|_| unread("its length"))?;
let Some(contents) = buffer.contents() else {
let buffer = buffer.clone();
return self
.private_bytes(&buffer)
.map_err(|error| PlayError::Runtime {
selector: "m2v.check".into(),
error: error.to_string(),
});
};
// SAFETY: the buffer's mapping holds its length, and the player waited for the work that
// writes it.
Ok(unsafe { std::slice::from_raw_parts(contents.as_ptr(), length) }.to_vec())
}
fn resource_id(&mut self, object: u64) -> Result<Option<u64>, PlayError> {
@@ -27,6 +27,8 @@
//! own second copy where the device has one): a blit's fill reaches the GPU's copy and not the
//! CPU's, an announcement with no write puts the CPU's bytes back into the GPU's copy at the next
//! commit, and a synchronize brings the GPU's bytes into the CPU's copy.
//! - A Private buffer, which has no CPU mapping, reads back through a copy into a Shared buffer of
//! the play's own: a blit's fill of it reads as the fill's bytes.
//! - In a stream whose header marks its announced bytes recorded, an announcement after a
//! read-back puts the stream's bytes into the GPU's copy, not the bytes read back, which a check
//! reads until then; without the mark it puts the bytes read back.
@@ -1087,6 +1089,29 @@ fn a_two_copy_stream_s_managed_buffer_keeps_both_copies() {
}
}
#[test]
fn a_private_buffer_reads_back_through_a_copy() {
let dir = ScratchDir::new("rt-replay-private-read").unwrap();
let path = written(&dir, |w| {
w.call("MTLCreateSystemDefaultDevice", 0, 1, &[], &[])?;
w.call("newCommandQueue", 1, 2, &[], &[])?;
// #3 Private (MTLResourceStorageModePrivate, 2 << 4), 16 bytes, filled with 0x5a.
let made = [Value::Uint(16), Value::Uint(32)];
w.call("newBufferWithLength:options:", 1, 3, &made, &[])?;
w.call("commandBuffer", 2, 10, &[], &[])?;
w.call("blitCommandEncoder", 10, 11, &[], &[])?;
let range = uints(&[("location", 0), ("length", 16)]);
let fill = [Value::Object(3), range, Value::Uint(0x5a)];
w.call("fillBuffer:range:value:", 11, 0, &fill, &[])?;
w.call("endEncoding", 11, 0, &[], &[])?;
w.call("commit", 10, 0, &[], &[])?;
w.call(WAIT_COMPLETED, 10, 0, &[], &[])
});
let (_, lines, bytes) = play_then("private-read", &path, |backend| backend.read(3));
let bytes = bytes.unwrap_or_else(|error| panic!("#3 read back: {error}: {lines:?}"));
assert_eq!(bytes, vec![0x5a; 16]);
}
/// A two-copy stream's Managed buffer #3 of 4 bytes, its header's flags `flags`: the program
/// writes 5s into its CPU copy and announces them; a fill of 9s reaches the GPU's copy, and a
/// synchronize reads it back, which the program's check of #3 sees; the program then puts its 5s
+3 -1
View File
@@ -671,7 +671,9 @@ counted from 0), completes the work committed so far, reports as at the stream's
plays can move a few draws and barriers from one group or frame to another. Only the stream's
counts are compared between plays.
Its own readbacks are not counted. Its own work around a dispatch an `m2v.privateBytes` record
Its own readbacks are not counted. The new runtime's backend reads a Private buffer, which has
no CPU mapping, through a blit copy of it into a Shared buffer of the play's own, on a queue of
the play's own, and waits for the copy. Its own work around a dispatch an `m2v.privateBytes` record
names (its buffer, fence and blits and the encoders it ends and makes again) is in none of the
stream's counts. The runtime's work for it cannot be told apart from the program's (a runtime
may record the barriers a split needs at the program's next command or at the commit), so a