From d85fb1186250445e6afee4f3276ffd0e72a74bb6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 18 Nov 2026 11:21:55 -0800 Subject: [PATCH] rt tests: busy work waits on a timeline semaphore the host signals, not on a host-set event The held-group tests' busy command buffers waited with vkCmdWaitEvents for a VkEvent the host set after its checks, while those command buffers were pending. Vulkan forbids that: VUID-vkSetEvent-event-09543 (an event must not be waited on by a command buffer in the pending state when it is set), and the queue forward-progress section requires host-signalled events to be signalled before the command buffer waits on them. The validation layer reported it on lavapipe (VUID-vkCmdWaitEvents-srcStageMask-01158), and a driver that times out such a stall may lose the device. The busy submission now waits for a timeline semaphore to reach 1, which the host signals with vkSignalSemaphore after the last check: a wait before its signal, which Vulkan defines for timeline semaphores. rt's events never submit a wait whose signal is not submitted (the submit thread holds such a segment), so a test-only seam, CommandBuffer::wait_semaphore_for_test, adds a raw timeline wait to the current segment's submission (Segment::test_waits, staged with the segment's other waits). Both exist only under the device test cfgs. The busy work is still submitted and in flight and completes only after the host's signal, so what the tests count is unchanged: rt's submissions, the group held behind three in flight, and completion order (a later submission's signal covers every command before it in submission order). Also a new test: two command buffers made and enqueued in order on one thread, the second encoded and committed there, the first encoded and committed on a second thread afterwards. The second waits behind the first, they complete in enqueue order, and each has its own status, handlers and GPU times. Verified: cargo fmt only so far; the check job runs clippy, the command device tests on lavapipe with the validation layer, and the 3090 runs. --- crates/metal2vulkan-rt/src/command.rs | 24 ++++ crates/metal2vulkan-rt/src/submit.rs | 9 ++ crates/metal2vulkan-rt/src/tests/command.rs | 144 +++++++++++++++----- 3 files changed, 143 insertions(+), 34 deletions(-) diff --git a/crates/metal2vulkan-rt/src/command.rs b/crates/metal2vulkan-rt/src/command.rs index 1b7f790e8..ee2708b71 100644 --- a/crates/metal2vulkan-rt/src/command.rs +++ b/crates/metal2vulkan-rt/src/command.rs @@ -240,6 +240,10 @@ pub(crate) struct Segment { /// changed ranges (`crate::capture`). Each copy-in goes ahead of the segment's commands, after /// its waits, and is kept until the command buffer retires. pub(crate) copy_ins: Vec, + /// For the device tests: timeline semaphore waits its submission makes whether or not any + /// signal was submitted ([`CommandBuffer::wait_semaphore_for_test`]). + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) test_waits: Vec<(vk::Semaphore, u64)>, } pub(crate) struct Handlers { @@ -645,6 +649,26 @@ impl CommandBuffer { Ok(()) }) } + + /// For the device tests: the current segment's submission waits for `semaphore`, a timeline + /// semaphore of the device, to reach `value`, which the test signals from the host + /// (`vkSignalSemaphore`) once the submission is made: a wait before its signal, which Vulkan + /// defines for timeline semaphores. The submit thread does not hold the segment for it, so it + /// is submitted and in flight, and completes only after the host's signal. Called before the + /// segment records any command. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn wait_semaphore_for_test(&self, semaphore: vk::Semaphore, value: u64) { + self.building(|building| { + let segment = building.last(); + assert!( + segment.command.is_none(), + "a wait ahead of the segment's commands" + ); + segment.test_waits.push((semaphore, value)); + Ok(()) + }) + .expect("a command buffer being recorded"); + } } impl Building { diff --git a/crates/metal2vulkan-rt/src/submit.rs b/crates/metal2vulkan-rt/src/submit.rs index 76fac6d2b..3f87b2dcc 100644 --- a/crates/metal2vulkan-rt/src/submit.rs +++ b/crates/metal2vulkan-rt/src/submit.rs @@ -1629,6 +1629,15 @@ fn stage(segment: &Segment, watch: &mut Vec>) -> Staged { .stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS), ); } + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + for &(semaphore, value) in &segment.test_waits { + staged.waits.push( + vk::SemaphoreSubmitInfo::default() + .semaphore(semaphore) + .value(value) + .stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS), + ); + } for (event, value) in &segment.signals { let mut state = lock(&event.state); if *value <= state.submitted.max(state.floor) { diff --git a/crates/metal2vulkan-rt/src/tests/command.rs b/crates/metal2vulkan-rt/src/tests/command.rs index fb76afd3a..c710873a3 100644 --- a/crates/metal2vulkan-rt/src/tests/command.rs +++ b/crates/metal2vulkan-rt/src/tests/command.rs @@ -266,6 +266,79 @@ pub(crate) mod on_device { }); } + /// A command buffer made and enqueued beforehand, encoded on another thread and committed after + /// the one enqueued behind it: two command buffers made and enqueued in order on one thread, + /// the second encoded and committed there, then the first encoded and committed on a second + /// thread. The second waits behind the first until the first is committed, they complete in + /// enqueue order, and each has its own status, handlers and GPU times. + #[test] + fn every_device_runs_enqueued_command_buffers_in_enqueue_order_across_threads() { + each_device("command_enqueue_threads", |name, _, device| { + let mut arena = Arena::new(&device); + let (a, b) = (arena.buffer(), arena.buffer()); + let queue = queue(&device); + let log = Log::default(); + let first = queue.command_buffer().expect("a command buffer"); + let second = queue.command_buffer().expect("a command buffer"); + first.enqueue().expect("an enqueue"); + second.enqueue().expect("an enqueue"); + log.handlers("first", &first); + log.handlers("second", &second); + fill(&second, &b, 2); + second.commit().expect("a commit"); + thread::sleep(Duration::from_millis(50)); + assert_eq!( + (first.status(), second.status()), + ( + CommandBufferStatus::Enqueued, + CommandBufferStatus::Committed + ), + "{name}: the second waits behind the first" + ); + assert!(log.lines().is_empty(), "{name}: {:?}", log.lines()); + let encoder = { + let (first, a) = (first.clone(), a.clone()); + thread::spawn(move || { + fill(&first, &a, 1); + first.commit().expect("a commit"); + }) + }; + encoder.join().expect("the encoding thread"); + waits_return( + name, + "enqueued across threads", + vec![first.clone(), second.clone()], + ); + for command_buffer in [&first, &second] { + assert_eq!( + (command_buffer.status(), command_buffer.error()), + (CommandBufferStatus::Completed, None), + "{name}" + ); + } + assert_eq!((word(&a), word(&b)), (1, 2), "{name}"); + let lines = log.lines(); + let at = |line: &str| { + (lines.iter().position(|logged| logged == line)) + .unwrap_or_else(|| panic!("{name}: no {line:?} in {lines:?}")) + }; + assert_eq!(lines.len(), 4, "{name}: {lines:?}"); + assert!(at("first scheduled Scheduled") < at("first completed Completed None")); + assert!(at("second scheduled Scheduled") < at("second completed Completed None")); + assert!( + at("first completed Completed None") < at("second completed Completed None"), + "{name}: completions in enqueue order: {lines:?}" + ); + let times = [&first, &second].map(|cb| (cb.gpu_start_time(), cb.gpu_end_time())); + println!("{name}: GPU start and end of the first and the second {times:?}"); + for (start, end) in times { + assert!(0 < start && start <= end, "{name}: {times:?}"); + } + assert!(times[0].1 <= times[1].1, "{name}: {times:?}"); + drop((first, second, a, b, arena, queue, device)); + }); + } + /// The physical device a device was opened on, for a second device. fn physical_of(instance: &Instance, device: &Device) -> crate::PhysicalDevice { let raw = device.raw().physical_device; @@ -936,29 +1009,33 @@ pub(crate) mod on_device { .expect("a buffer") } - /// Busy work a queue keeps in flight until the test lets it end: each command buffer waits on - /// the GPU for a `VkEvent` that only the host sets ([`Busy::open`]), then fills a block. It is - /// submitted and does not complete before the test's checks are made, however fast the device - /// or however loaded the host. A wait for an event the host sets after the wait was submitted - /// is valid outside a render pass, with the host stage as its first scope (`vkCmdWaitEvents`). - /// Dropped, it opens the event and waits for each one, so a failed check does not leave the - /// device's work waiting forever. + /// Busy work a queue keeps in flight until the test lets it end: each command buffer's + /// submission waits for a timeline semaphore to reach 1, which only the host signals + /// ([`Busy::open`]), then fills a block. It is submitted and does not complete before the + /// test's checks are made, however fast the device or however loaded the host. A wait + /// submitted before its signal is defined for a timeline semaphore, and the host signals it + /// with `vkSignalSemaphore` (an event the host sets while a command buffer waiting on it is + /// pending is not: `VUID-vkSetEvent-event-09543`). Dropped, it opens the work and waits for + /// each one, so a failed check does not leave the device's work waiting forever. struct Busy { device: Device, - event: vk::Event, + semaphore: vk::Semaphore, work: Buffer, running: Vec, } impl Busy { fn new(device: &Device) -> Busy { - let info = vk::EventCreateInfo::default(); - // SAFETY: the device is live; the event is destroyed in `drop`. - let event = - unsafe { device.core().device.create_event(&info, None) }.expect("an event"); + let mut timeline = vk::SemaphoreTypeCreateInfo::default() + .semaphore_type(vk::SemaphoreType::TIMELINE) + .initial_value(0); + let info = vk::SemaphoreCreateInfo::default().push_next(&mut timeline); + // SAFETY: the device is live; the semaphore is destroyed in `drop`. + let semaphore = unsafe { device.core().device.create_semaphore(&info, None) } + .expect("a timeline semaphore"); Busy { device: device.clone(), - event, + semaphore, work: private(device), running: Vec::new(), } @@ -975,25 +1052,14 @@ pub(crate) mod on_device { let command_buffer = queue.command_buffer().expect("a command buffer"); let (target, offset) = self.work.device_range(); let size = self.work.length(); - let event = self.event; + command_buffer.wait_semaphore_for_test(self.semaphore, 1); command_buffer .record_for_test(&[&self.work], |device, command| { - // SAFETY: the command buffer is recording outside any render pass; only the - // host sets the event; the range is the buffer's, in whole words. - unsafe { - device.cmd_wait_events( - command, - &[event], - vk::PipelineStageFlags::HOST, - vk::PipelineStageFlags::TRANSFER, - &[], - &[], - &[], - ); - device.cmd_fill_buffer(command, target, offset, size, 1); - } + // SAFETY: the command buffer is recording outside any render pass; the range + // is the buffer's, in whole words. + unsafe { device.cmd_fill_buffer(command, target, offset, size, 1) }; }) - .expect("a wait and a fill"); + .expect("a fill"); command_buffer.commit().expect("a commit"); within(name, || device.counters().metrics().submissions > before); self.running.push(command_buffer.clone()); @@ -1005,10 +1071,20 @@ pub(crate) mod on_device { &self.running } - /// Lets the busy work end: the host sets the event. A second call changes nothing. + /// Lets the busy work end: the host signals the semaphore to 1. A second call changes + /// nothing. fn open(&self) { - // SAFETY: the event is the device's, and only this thread sets it. - unsafe { self.device.core().device.set_event(self.event) }.expect("an event set"); + let device = &self.device.core().device; + // SAFETY: the semaphore is the device's timeline semaphore. + let value = unsafe { device.get_semaphore_counter_value(self.semaphore) } + .expect("the semaphore's value"); + if value == 0 { + let signal = vk::SemaphoreSignalInfo::default() + .semaphore(self.semaphore) + .value(1); + // SAFETY: only this thread signals it, and 1 is above its value. + unsafe { device.signal_semaphore(&signal) }.expect("a host signal"); + } } } @@ -1018,8 +1094,8 @@ pub(crate) mod on_device { for command_buffer in &self.running { command_buffer.wait_until_completed(); } - // SAFETY: every command buffer that waits on it completed. - unsafe { self.device.core().device.destroy_event(self.event, None) }; + // SAFETY: every submission that waits on it completed. + unsafe { (self.device.core().device).destroy_semaphore(self.semaphore, None) }; } }