Runtime tests: the fenced host writer meets the gate by a handshake, not by chance

host_in_place's each_device_fences_a_host_writer_around_the_restore_alone
asserted held > 0 (the writer found the fence's gate held) on one fenced
make, which holds only where the writer's thread is scheduled inside the
span. On the Radeon 780M (pin T's round, job m3-max-84449) the writer
missed it: the make lost no word and kept its bytes, and held was 0.

The writer now tells a Handshake when it first finds the gate held,
before it waits there, and when it ends; the fenced make's Gate
(Gate::meeting) keeps the gate in after() until the writer has found it
held or ended. The writer stamps from before the make to after it
returns and needs about 130 ms to stamp every word, so it meets the gate
inside the span by structure, and lost == 0 still checks that the stamp
it then writes lands after the write-back. The other Gates (the refusing
one, in place alone, the timed makes) wait for no writer, as before.

The unfenced half keeps its four makes: its witness is a stamp between
the GPU's save and its write-back, which run in one batch with no host
call between them, so no handshake reaches it; the test's doc says so.

The runtime-sources pin is unchanged (pin-compute.py: the hash equals
the committed one). Test only, no version. Verified: jobs to follow.
This commit is contained in:
Anees Iqbal committed 2026-12-03 10:58:56 -08:00
1 parent e5b7498833
commit 004f68b48f
1 file changed
+46 -10
@@ -657,14 +657,23 @@ mod on_device {
}
}
/// What a host writer tells a fence that waits for it ([`Gate::meeting`]): that it found the
/// gate held, before it waits there, and that it ended.
#[derive(Default)]
struct Handshake {
met: AtomicBool,
ended: AtomicBool,
}
/// A host writer of the frame at `base`, `len` bytes: it stamps its words in order, one a
/// microsecond, each through `gate`, until `stop` is set or every word is stamped, and sets
/// `started` first. Answers how many it stamped and how many of those found the gate held.
/// `started` first. It tells `told` when it first finds the gate held, before it waits there,
/// and when it ends. Answers how many it stamped and how many of those found the gate held.
fn stamp(
base: usize,
len: u64,
gate: &Mutex<()>,
(started, stop): (&AtomicBool, &AtomicBool),
(started, stop, told): (&AtomicBool, &AtomicBool, &Handshake),
) -> (u64, u64) {
let begun = Instant::now();
let (mut stamped, mut held) = (0, 0);
@@ -678,6 +687,7 @@ mod on_device {
Ok(open) => open,
Err(TryLockError::WouldBlock) => {
held += 1;
told.met.store(true, Ordering::SeqCst);
gate.lock().unwrap_or_else(PoisonError::into_inner)
}
Err(TryLockError::Poisoned(open)) => open.into_inner(),
@@ -686,6 +696,7 @@ mod on_device {
unsafe { word(base, stamped).write_volatile(stamp_of(stamped)) };
stamped += 1;
}
told.ended.store(true, Ordering::SeqCst);
(stamped, held)
}
@@ -710,12 +721,14 @@ mod on_device {
}
/// A caller's fence over a host writer's `gate` ([`RestoreFence`]): `before` takes the gate,
/// or refuses with `refuse` where set, and `after` gives it back. Each call counts, and each
/// span between the two is timed.
/// or refuses with `refuse` where set, and `after` gives it back, where the fence waits for a
/// writer ([`meeting`](Self::meeting)) once that writer has found the gate held or ended. Each
/// call counts, and each span between the two is timed.
struct Gate<'g> {
gate: &'g Mutex<()>,
held: Option<MutexGuard<'g, ()>>,
refuse: Option<&'static str>,
writer: Option<&'g Handshake>,
befores: u32,
afters: u32,
opened: Option<Instant>,
@@ -728,12 +741,22 @@ mod on_device {
gate,
held: None,
refuse,
writer: None,
befores: 0,
afters: 0,
opened: None,
spans: Vec::new(),
}
}
/// The fence whose span ends only once the writer that tells `writer` has found the gate
/// held, or ended: a writer that runs through the span meets the fence inside it.
fn meeting(gate: &'g Mutex<()>, writer: &'g Handshake) -> Gate<'g> {
Gate {
writer: Some(writer),
..Gate::new(gate, None)
}
}
}
impl RestoreFence for Gate<'_> {
@@ -749,6 +772,12 @@ mod on_device {
fn after(&mut self) {
self.afters += 1;
// The handshake: the gate stays held until the writer found it so, or ended.
if let (Some(writer), Some(_)) = (self.writer, &self.held) {
while !writer.met.load(Ordering::SeqCst) && !writer.ended.load(Ordering::SeqCst) {
std::thread::yield_now();
}
}
if let Some(opened) = self.opened {
self.spans.push(opened.elapsed().as_micros());
}
@@ -762,9 +791,11 @@ mod on_device {
/// stamps the frame's words, one each 64 bytes, one a microsecond, from before the make to
/// after it returns. Made with no fence ([`Buffer::new_texture`]), the make loses writes: a
/// word stamped between its save and its write-back holds its old bytes (up to four makes, so
/// that the writer lands in the span). Made with a fence whose `before` takes the writer's
/// gate and whose `after` gives it back ([`Buffer::new_texture_fenced`]), the writer finds the
/// gate held at least once and loses none, and the fence runs once each way. Both makes
/// that the writer lands in the span: the loss needs a stamp between the GPU's save and its
/// write-back, in one batch, which no host call bounds). Made with a fence whose `before`
/// takes the writer's gate and whose `after` gives it back once the writer has found it held
/// ([`Buffer::new_texture_fenced`]), the writer finds the gate held, by that handshake, and
/// loses none, and the fence runs once each way. Both makes
/// count the same restore pair ([`OverCause::HostMemoryRestore`]), and every word not stamped
/// holds the bytes written before the make. A `before` that refuses makes no texture, counts
/// no copy and no texture, and is not followed by `after`; the in-place-alone call
@@ -803,6 +834,7 @@ mod on_device {
TextureDescriptor::new(TextureType::Type2D, PixelFormat::BGRA8Unorm, width, height);
frame.usage = TextureUsage::SHADER_READ;
let gate = Mutex::new(());
let told = Handshake::default();
// A make under the host writer, which the fence `fence` may hold: the make's answer,
// the words stamped, those that found the gate held, those lost, and whether the
@@ -812,8 +844,10 @@ mod on_device {
round = round.wrapping_add(1);
fill_pattern(base, len, round);
let (started, stop) = (AtomicBool::new(false), AtomicBool::new(false));
told.met.store(false, Ordering::SeqCst);
told.ended.store(false, Ordering::SeqCst);
let (made, (stamped, held)) = std::thread::scope(|scope| {
let writer = scope.spawn(|| stamp(base, len, &gate, (&started, &stop)));
let writer = scope.spawn(|| stamp(base, len, &gate, (&started, &stop, &told)));
while !started.load(Ordering::SeqCst) {
std::hint::spin_loop();
}
@@ -906,8 +940,10 @@ mod on_device {
panic!("{name}: four makes with no fence lost no write: the writer missed the span")
};
// With the fence, the writer waits at the gate and loses none.
let mut fence = Gate::new(&gate, None);
// With the fence, the writer waits at the gate and loses none: the fence's span ends
// only once the writer, which stamps from before the make to after it, met the gate
// held, so it meets it by structure.
let mut fence = Gate::meeting(&gate, &told);
let before = counters.metrics();
let (made, stamped, held, lost, kept) = under_writer(index, Some(&mut fence));
index += 1;