diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 03:31:06 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 03:31:06 -0500 |
| commit | 59db4b62b039cec2121409c60be670514020e90a (patch) | |
| tree | c8cfcf6f2eab6b66d6bd409b8c51fcfe2ca5d07c /runtime/test/submit_frame_test.cpp | |
| parent | e4c0708745dbff6c29678007d96a35bfdd22e288 (diff) | |
runtime: Round 8 WS3 — fix list-value gate TOCTOU, offload QueueHolds barriers, empty-batch/wait coalescing, replay floor & batched xmem slot-31
- Fix Pump::choose_release() TOCTOU on already-completed unlanded write groups/action gates + 10 ms quiet window on Reason::present
- Skip before_execute/visibility_seq on empty batches (preserving pending_wait_vis_seq_), coalesce past GPU-completed Waits (merging CPU vis_seq), cache CommandList summary, atomic unbuilt_sync_pipelines_ & BindlessSet clean fast-paths, hoist EmitScratch
- Dedicated QueueHolds barrier-drain thread with active_work_ tracking, wait_barriers_drained() ordering, cancel_after() wait, and non-blocking VulkanQueue::release_ready()
- Batch copy_page_mappings() per contiguous range & notify Event::replaced on mirror_heap decommit in xmem.cpp
- SEH-guarded aligned volatile read_value() (no noexcept under /EHsc) + 1000 ms slot-12 Vulkan fence mirror timeout
AI-assisted: Antigravity (Gemini 4 Neon).
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
| -rw-r--r-- | runtime/test/submit_frame_test.cpp | 284 |
1 files changed, 270 insertions, 14 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp index 7450724b1..c05357dd6 100644 --- a/runtime/test/submit_frame_test.cpp +++ b/runtime/test/submit_frame_test.cpp | |||
| @@ -394,7 +394,7 @@ int queue_sync_and_async() { | |||
| 394 | VulkanFence gate_other(context, 0), gate_d(context, 0); | 394 | VulkanFence gate_other(context, 0), gate_d(context, 0); |
| 395 | EXPECT(gate_other.semaphore() && gate_d.semaphore()); | 395 | EXPECT(gate_other.semaphore() && gate_d.semaphore()); |
| 396 | auto empty_other = std::make_shared<CommandList>(); | 396 | auto empty_other = std::make_shared<CommandList>(); |
| 397 | EXPECT(empty_other->close()); | 397 | EXPECT(empty_other->uav_barrier({}) && empty_other->close()); |
| 398 | std::shared_ptr<const CommandList> s_other = empty_other; | 398 | std::shared_ptr<const CommandList> s_other = empty_other; |
| 399 | 399 | ||
| 400 | std::memset(db, 0, kBufBytes); | 400 | std::memset(db, 0, kBufBytes); |
| @@ -447,7 +447,7 @@ int queue_sync_and_async() { | |||
| 447 | queue.set_async_depth(0); | 447 | queue.set_async_depth(0); |
| 448 | 448 | ||
| 449 | auto empty = std::make_shared<CommandList>(); | 449 | auto empty = std::make_shared<CommandList>(); |
| 450 | EXPECT(empty->close()); | 450 | EXPECT(empty->uav_barrier({}) && empty->close()); |
| 451 | std::shared_ptr<const CommandList> snap = empty; | 451 | std::shared_ptr<const CommandList> snap = empty; |
| 452 | 452 | ||
| 453 | // (a) Active unheld sync execute(). | 453 | // (a) Active unheld sync execute(). |
| @@ -474,6 +474,7 @@ int queue_sync_and_async() { | |||
| 474 | 474 | ||
| 475 | release_before.store(true, std::memory_order_release); | 475 | release_before.store(true, std::memory_order_release); |
| 476 | exec_t.join(); | 476 | exec_t.join(); |
| 477 | context.holds().wait_barriers_drained(UINT64_MAX); | ||
| 477 | EXPECT(res == ReplayResult::success); | 478 | EXPECT(res == ReplayResult::success); |
| 478 | EXPECT(barrier_ran.load(std::memory_order_acquire)); | 479 | EXPECT(barrier_ran.load(std::memory_order_acquire)); |
| 479 | EXPECT(barrier_saw_after.load(std::memory_order_acquire) == 1); | 480 | EXPECT(barrier_saw_after.load(std::memory_order_acquire) == 1); |
| @@ -504,6 +505,7 @@ int queue_sync_and_async() { | |||
| 504 | EXPECT(!sig_barrier_ran.load(std::memory_order_acquire)); | 505 | EXPECT(!sig_barrier_ran.load(std::memory_order_acquire)); |
| 505 | } | 506 | } |
| 506 | sig_t.join(); | 507 | sig_t.join(); |
| 508 | context.holds().wait_barriers_drained(UINT64_MAX); | ||
| 507 | EXPECT(sig_ok.load(std::memory_order_acquire)); | 509 | EXPECT(sig_ok.load(std::memory_order_acquire)); |
| 508 | EXPECT(sig_barrier_ran.load(std::memory_order_acquire)); | 510 | EXPECT(sig_barrier_ran.load(std::memory_order_acquire)); |
| 509 | EXPECT(sig_barrier_saw_signaled.load(std::memory_order_acquire)); | 511 | EXPECT(sig_barrier_saw_signaled.load(std::memory_order_acquire)); |
| @@ -578,8 +580,8 @@ int queue_sync_and_async() { | |||
| 578 | EXPECT(queue.idle()); | 580 | EXPECT(queue.idle()); |
| 579 | 581 | ||
| 580 | // Held consecutive batches coalesce across {steps[0..2]} (1), split at Signal(mid), run {steps[3]} (2), | 582 | // Held consecutive batches coalesce across {steps[0..2]} (1), split at Signal(mid), run {steps[3]} (2), |
| 581 | // split at Wait(mid), and coalesce {steps[4..5]} (3): 6 batches -> 3 before_execute_() calls! | 583 | // and either split at Wait(mid) (3) or coalesce {steps[3..5]} if mid already completed on the GPU (2). |
| 582 | EXPECT(before_calls.load() == 3); | 584 | EXPECT(before_calls.load() == 2 || before_calls.load() == 3); |
| 583 | for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i)); | 585 | for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i)); |
| 584 | EXPECT(db[0] == std::uint8_t(0xA6)); // strict FIFO through all 6 batches | 586 | EXPECT(db[0] == std::uint8_t(0xA6)); // strict FIFO through all 6 batches |
| 585 | 587 | ||
| @@ -731,7 +733,7 @@ int queue_sync_and_async() { | |||
| 731 | compute.set_async_depth(4); | 733 | compute.set_async_depth(4); |
| 732 | 734 | ||
| 733 | auto empty_list = std::make_shared<CommandList>(); | 735 | auto empty_list = std::make_shared<CommandList>(); |
| 734 | EXPECT(empty_list->close()); | 736 | EXPECT(empty_list->uav_barrier({}) && empty_list->close()); |
| 735 | std::shared_ptr<const CommandList> snap = empty_list; | 737 | std::shared_ptr<const CommandList> snap = empty_list; |
| 736 | 738 | ||
| 737 | std::atomic<bool> hold_before{true}; | 739 | std::atomic<bool> hold_before{true}; |
| @@ -796,6 +798,7 @@ int queue_sync_and_async() { | |||
| 796 | EXPECT(consumer_gate.signal(1)); | 798 | EXPECT(consumer_gate.signal(1)); |
| 797 | EXPECT(consumer_done.wait(1, 5'000'000'000ull)); | 799 | EXPECT(consumer_done.wait(1, 5'000'000'000ull)); |
| 798 | EXPECT(compute.idle()); | 800 | EXPECT(compute.idle()); |
| 801 | context.holds().wait_barriers_drained(UINT64_MAX); | ||
| 799 | EXPECT(present_ran.load(std::memory_order_acquire)); | 802 | EXPECT(present_ran.load(std::memory_order_acquire)); |
| 800 | 803 | ||
| 801 | // (b) Genuine deadlock across BOTH async queues (no producer can ever signal): any_progress() must settle | 804 | // (b) Genuine deadlock across BOTH async queues (no producer can ever signal): any_progress() must settle |
| @@ -944,10 +947,11 @@ int queue_sync_and_async() { | |||
| 944 | EXPECT(done.wait(1, 5'000'000'000ull)); | 947 | EXPECT(done.wait(1, 5'000'000'000ull)); |
| 945 | EXPECT(queue.idle()); | 948 | EXPECT(queue.idle()); |
| 946 | 949 | ||
| 947 | // 9 execute calls coalesced into exactly 5 before_execute_() calls AND 5 command-buffer vkQueueSubmits! | 950 | // 9 execute calls coalesced into 4 (when mid already GPU-completed before Wait(mid)) or 5 submits! |
| 948 | EXPECT(before_calls.load(std::memory_order_acquire) == 5); | 951 | const unsigned bc = before_calls.load(std::memory_order_acquire); |
| 949 | EXPECT(s_cb_submits.load(std::memory_order_acquire) == 5); | 952 | const unsigned sc = s_cb_submits.load(std::memory_order_acquire); |
| 950 | EXPECT(present_saw_submits.load(std::memory_order_acquire) == 4); | 953 | const unsigned ps = present_saw_submits.load(std::memory_order_acquire); |
| 954 | EXPECT((bc == 4 || bc == 5) && sc == bc && ps == bc - 1); | ||
| 951 | for (int i = 0; i < 9; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xD0 + i)); | 955 | for (int i = 0; i < 9; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xD0 + i)); |
| 952 | EXPECT(db[0] == std::uint8_t(0xD8)); // exact FIFO overwrite order preserved across inter-batch barriers | 956 | EXPECT(db[0] == std::uint8_t(0xD8)); // exact FIFO overwrite order preserved across inter-batch barriers |
| 953 | std::printf("queue async submit merging: 9 batches -> 5 submits, split cleanly on Signal/Wait/Present ok\n"); | 957 | std::printf("queue async submit merging: 9 batches -> 5 submits, split cleanly on Signal/Wait/Present ok\n"); |
| @@ -1037,7 +1041,7 @@ int queue_sync_and_async() { | |||
| 1037 | queue.set_async_depth(4); | 1041 | queue.set_async_depth(4); |
| 1038 | 1042 | ||
| 1039 | auto empty_list = std::make_shared<CommandList>(); | 1043 | auto empty_list = std::make_shared<CommandList>(); |
| 1040 | EXPECT(empty_list->close()); | 1044 | EXPECT(empty_list->uav_barrier({}) && empty_list->close()); |
| 1041 | std::shared_ptr<const CommandList> snap = empty_list; | 1045 | std::shared_ptr<const CommandList> snap = empty_list; |
| 1042 | 1046 | ||
| 1043 | std::atomic<unsigned> before_n{0}; | 1047 | std::atomic<unsigned> before_n{0}; |
| @@ -1861,7 +1865,7 @@ int queue_sync_and_async() { | |||
| 1861 | compute.set_async_depth(4); | 1865 | compute.set_async_depth(4); |
| 1862 | 1866 | ||
| 1863 | auto empty = std::make_shared<CommandList>(); | 1867 | auto empty = std::make_shared<CommandList>(); |
| 1864 | EXPECT(empty->close()); | 1868 | EXPECT(empty->uav_barrier({}) && empty->close()); |
| 1865 | std::shared_ptr<const CommandList> snap = empty; | 1869 | std::shared_ptr<const CommandList> snap = empty; |
| 1866 | 1870 | ||
| 1867 | // (a) 24 Wait-separated batches within a single present_epoch on depth=4: zero room_cv_ stalls! | 1871 | // (a) 24 Wait-separated batches within a single present_epoch on depth=4: zero room_cv_ stalls! |
| @@ -2035,7 +2039,7 @@ int queue_sync_and_async() { | |||
| 2035 | compute.set_async_depth(4); | 2039 | compute.set_async_depth(4); |
| 2036 | 2040 | ||
| 2037 | auto empty = std::make_shared<CommandList>(); | 2041 | auto empty = std::make_shared<CommandList>(); |
| 2038 | EXPECT(empty->close()); | 2042 | EXPECT(empty->uav_barrier({}) && empty->close()); |
| 2039 | std::shared_ptr<const CommandList> snap = empty; | 2043 | std::shared_ptr<const CommandList> snap = empty; |
| 2040 | 2044 | ||
| 2041 | // GPU-only queue Signal must NOT bump visibility_seq_. | 2045 | // GPU-only queue Signal must NOT bump visibility_seq_. |
| @@ -2116,11 +2120,14 @@ int queue_sync_and_async() { | |||
| 2116 | 2120 | ||
| 2117 | // (b) Unlocked QueueHolds::run_barriers(): strict FIFO order + concurrent pump_ready() unblocked + cancel_after. | 2121 | // (b) Unlocked QueueHolds::run_barriers(): strict FIFO order + concurrent pump_ready() unblocked + cancel_after. |
| 2118 | context.set_before_execute({}); | 2122 | context.set_before_execute({}); |
| 2123 | auto empty_13b = std::make_shared<CommandList>(); | ||
| 2124 | EXPECT(empty_13b->close()); | ||
| 2125 | std::shared_ptr<const CommandList> snap_13b = empty_13b; | ||
| 2119 | VulkanFence hold_b1(context, 0), hold_comp(context, 0), comp_b_done(context, 0); | 2126 | VulkanFence hold_b1(context, 0), hold_comp(context, 0), comp_b_done(context, 0); |
| 2120 | EXPECT(hold_b1.semaphore() && hold_comp.semaphore() && comp_b_done.semaphore()); | 2127 | EXPECT(hold_b1.semaphore() && hold_comp.semaphore() && comp_b_done.semaphore()); |
| 2121 | 2128 | ||
| 2122 | direct.wait(hold_b1.semaphore(), 1); | 2129 | direct.wait(hold_b1.semaphore(), 1); |
| 2123 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | 2130 | EXPECT(direct.execute({&snap_13b, 1}) == ReplayResult::success); |
| 2124 | EXPECT(direct.idle()); | 2131 | EXPECT(direct.idle()); |
| 2125 | 2132 | ||
| 2126 | std::mutex fifo_m; | 2133 | std::mutex fifo_m; |
| @@ -2141,7 +2148,7 @@ int queue_sync_and_async() { | |||
| 2141 | 2148 | ||
| 2142 | // Hold an independent compute batch behind hold_comp. | 2149 | // Hold an independent compute batch behind hold_comp. |
| 2143 | compute.wait(hold_comp.semaphore(), 1); | 2150 | compute.wait(hold_comp.semaphore(), 1); |
| 2144 | EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); | 2151 | EXPECT(compute.execute({&snap_13b, 1}) == ReplayResult::success); |
| 2145 | EXPECT(compute.signal(comp_b_done.semaphore(), 1)); | 2152 | EXPECT(compute.signal(comp_b_done.semaphore(), 1)); |
| 2146 | EXPECT(compute.idle()); | 2153 | EXPECT(compute.idle()); |
| 2147 | 2154 | ||
| @@ -2168,6 +2175,7 @@ int queue_sync_and_async() { | |||
| 2168 | release_b1.store(true, std::memory_order_release); | 2175 | release_b1.store(true, std::memory_order_release); |
| 2169 | sig_b1.join(); | 2176 | sig_b1.join(); |
| 2170 | canceller.join(); | 2177 | canceller.join(); |
| 2178 | context.holds().wait_barriers_drained(UINT64_MAX); | ||
| 2171 | EXPECT(direct.idle()); | 2179 | EXPECT(direct.idle()); |
| 2172 | EXPECT(cancel_done.load(std::memory_order_acquire)); | 2180 | EXPECT(cancel_done.load(std::memory_order_acquire)); |
| 2173 | EXPECT(!b3_ran.load(std::memory_order_acquire)); | 2181 | EXPECT(!b3_ran.load(std::memory_order_acquire)); |
| @@ -2277,6 +2285,254 @@ int queue_sync_and_async() { | |||
| 2277 | std::printf("queue r7 sync-tex: per-fence vis_seq, unlocked run_barriers FIFO, lazy title_upload wait & unlocked record ok\n"); | 2285 | std::printf("queue r7 sync-tex: per-fence vis_seq, unlocked run_barriers FIFO, lazy title_upload wait & unlocked record ok\n"); |
| 2278 | } | 2286 | } |
| 2279 | 2287 | ||
| 2288 | // 14. Round 8 WS3: | ||
| 2289 | // (a) Empty-batch before_execute_() / after_execute_() / visibility_seq_ skip + mandatory guardrail that | ||
| 2290 | // pending_wait_vis_seq_ is NEVER cleared and carries exec_waits_vis_seq() forward to the next non-empty | ||
| 2291 | // execute_now(); | ||
| 2292 | // (b) Coalescing past already-GPU-completed Waits in BOTH async_worker inbox_ and drain_held_while_executing(), | ||
| 2293 | // merging holds().signaled_vis_seq() into group_vis_seq so CPU Fence::Signal visibility edges are preserved; | ||
| 2294 | // (c) Dedicated QueueHolds barrier-drain thread: gate.signal() returns immediately without blocking on Present, | ||
| 2295 | // active_work_ / any_progress() stays true while barrier runs, and post-barrier Frame N+1 GPU commands wait | ||
| 2296 | // via wait_barriers_drained() before running. | ||
| 2297 | { | ||
| 2298 | static PFN_vkQueueSubmit s_r8_real_submit = nullptr; | ||
| 2299 | static std::atomic<unsigned> s_r8_cb_submits{0}; | ||
| 2300 | struct R8HookSubmit { | ||
| 2301 | static VKAPI_ATTR VkResult VKAPI_CALL call(VkQueue q, std::uint32_t count, const VkSubmitInfo* infos, | ||
| 2302 | VkFence fence) { | ||
| 2303 | for (std::uint32_t i = 0; i < count; ++i) | ||
| 2304 | if (infos[i].commandBufferCount > 0) s_r8_cb_submits.fetch_add(1, std::memory_order_relaxed); | ||
| 2305 | return s_r8_real_submit(q, count, infos, fence); | ||
| 2306 | } | ||
| 2307 | }; | ||
| 2308 | |||
| 2309 | VulkanContext context(d.device_proc, d.device, true); | ||
| 2310 | EXPECT(context.loaded()); | ||
| 2311 | s_r8_real_submit = context.vk.vkQueueSubmit; | ||
| 2312 | s_r8_cb_submits.store(0, std::memory_order_relaxed); | ||
| 2313 | context.vk.vkQueueSubmit = &R8HookSubmit::call; | ||
| 2314 | |||
| 2315 | VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 2316 | EXPECT(direct.ok()); | ||
| 2317 | direct.set_async_depth(4); | ||
| 2318 | |||
| 2319 | void* src_map = nullptr; | ||
| 2320 | void* dst_map = nullptr; | ||
| 2321 | VkBuffer src = d.buffer(kBufBytes, usage, &src_map); | ||
| 2322 | VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map); | ||
| 2323 | EXPECT(src && dst); | ||
| 2324 | auto* sb = static_cast<std::uint8_t*>(src_map); | ||
| 2325 | auto* db = static_cast<std::uint8_t*>(dst_map); | ||
| 2326 | std::memset(sb, 0, kBufBytes); | ||
| 2327 | std::memset(db, 0, kBufBytes); | ||
| 2328 | sb[0] = 0x81; | ||
| 2329 | sb[16] = 0x82; | ||
| 2330 | sb[32] = 0x83; | ||
| 2331 | ResourceBinding r_src{}, r_dst{}; | ||
| 2332 | r_src.buffer = src; | ||
| 2333 | r_src.size = kBufBytes; | ||
| 2334 | r_dst.buffer = dst; | ||
| 2335 | r_dst.size = kBufBytes; | ||
| 2336 | r_dst.host_readable = true; | ||
| 2337 | context.add_resource(1, r_src); | ||
| 2338 | context.add_resource(2, r_dst); | ||
| 2339 | |||
| 2340 | auto empty_list = std::make_shared<CommandList>(); | ||
| 2341 | EXPECT(empty_list->close()); | ||
| 2342 | std::shared_ptr<const CommandList> empty_snap = empty_list; | ||
| 2343 | |||
| 2344 | auto make_step = [&](int idx) -> std::shared_ptr<const CommandList> { | ||
| 2345 | auto l = std::make_shared<CommandList>(); | ||
| 2346 | if (!l->copy_buffer({2, std::uint64_t((idx + 1) * 16), 1, std::uint64_t(idx * 16), 1}) || | ||
| 2347 | !l->copy_buffer({2, 0, 1, std::uint64_t(idx * 16), 1}) || !l->close()) | ||
| 2348 | return {}; | ||
| 2349 | return l; | ||
| 2350 | }; | ||
| 2351 | std::shared_ptr<const CommandList> b0 = make_step(0), b1 = make_step(1), b2 = make_step(2); | ||
| 2352 | EXPECT(b0 && b1 && b2); | ||
| 2353 | |||
| 2354 | // (a) Empty-batch skip + pending_wait_vis_seq_ preservation across empty execute. | ||
| 2355 | std::atomic<unsigned> before_calls{0}, after_calls{0}; | ||
| 2356 | std::atomic<std::uint64_t> last_req_seq{0}; | ||
| 2357 | context.set_before_execute([&] { | ||
| 2358 | before_calls.fetch_add(1, std::memory_order_acq_rel); | ||
| 2359 | last_req_seq.store(VulkanContext::current_before_execute_seq(), std::memory_order_release); | ||
| 2360 | }); | ||
| 2361 | context.set_after_execute([&] { after_calls.fetch_add(1, std::memory_order_acq_rel); }); | ||
| 2362 | |||
| 2363 | const std::uint64_t vis0 = context.current_visibility_seq(); | ||
| 2364 | EXPECT(direct.execute({&empty_snap, 1}) == ReplayResult::success); | ||
| 2365 | EXPECT(direct.idle()); | ||
| 2366 | EXPECT(context.current_visibility_seq() == vis0); | ||
| 2367 | EXPECT(before_calls.load(std::memory_order_acquire) == 0); | ||
| 2368 | EXPECT(after_calls.load(std::memory_order_acquire) == 0); | ||
| 2369 | |||
| 2370 | // Wait on a CPU-signaled fence (vis_seq V), consume it with an EMPTY list (skips before_execute_()), then run | ||
| 2371 | // a non-empty list: the non-empty list's before_execute_() MUST still observe req_seq >= V! | ||
| 2372 | VulkanFence cpu_f(context, 0), done_a(context, 0); | ||
| 2373 | EXPECT(cpu_f.semaphore() && done_a.semaphore()); | ||
| 2374 | EXPECT(cpu_f.signal(1)); | ||
| 2375 | const std::uint64_t cpu_vis = context.holds().signaled_vis_seq(cpu_f.semaphore(), 1); | ||
| 2376 | EXPECT(cpu_vis > vis0); | ||
| 2377 | |||
| 2378 | // Also advance visibility_seq_ on a dummy batch so empty_list has vis_seq == 0 while b0 gets a later vis_seq; | ||
| 2379 | // verify specifically when b0 is enqueued BEFORE cpu_f.signal(2) (held behind cpu_f2)! | ||
| 2380 | VulkanFence cpu_f2(context, 0); | ||
| 2381 | EXPECT(cpu_f2.semaphore()); | ||
| 2382 | direct.wait(cpu_f2.semaphore(), 1); | ||
| 2383 | EXPECT(direct.execute({&empty_snap, 1}) == ReplayResult::success); // held behind cpu_f2; vis_seq == 0 | ||
| 2384 | EXPECT(direct.execute({&b0, 1}) == ReplayResult::success); // held behind empty; enqueued BEFORE signal | ||
| 2385 | EXPECT(direct.signal(done_a.semaphore(), 1)); | ||
| 2386 | EXPECT(direct.idle()); | ||
| 2387 | EXPECT(before_calls.load(std::memory_order_acquire) == 0); | ||
| 2388 | |||
| 2389 | // Signal cpu_f2 AFTER b0 was enqueued: cpu_f2's vis_seq is strictly higher than b0's enqueue vis_seq! | ||
| 2390 | EXPECT(cpu_f2.signal(1)); | ||
| 2391 | const std::uint64_t cpu_vis2 = context.holds().signaled_vis_seq(cpu_f2.semaphore(), 1); | ||
| 2392 | EXPECT(done_a.wait(1, 5'000'000'000ull)); | ||
| 2393 | EXPECT(direct.idle()); | ||
| 2394 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); | ||
| 2395 | EXPECT(after_calls.load(std::memory_order_acquire) == 1); | ||
| 2396 | EXPECT(last_req_seq.load(std::memory_order_acquire) >= cpu_vis2); | ||
| 2397 | EXPECT(db[16] == 0x81); | ||
| 2398 | |||
| 2399 | // (b1) Coalescing past already-GPU-completed Wait in async_worker inbox_ + CPU Fence::Signal vis_seq merge. | ||
| 2400 | before_calls.store(0, std::memory_order_release); | ||
| 2401 | s_r8_cb_submits.store(0, std::memory_order_release); | ||
| 2402 | last_req_seq.store(0, std::memory_order_release); | ||
| 2403 | std::memset(db, 0, kBufBytes); | ||
| 2404 | std::atomic<bool> in_lead{false}, go_lead{false}; | ||
| 2405 | context.set_before_execute([&] { | ||
| 2406 | const unsigned n = before_calls.fetch_add(1, std::memory_order_acq_rel) + 1; | ||
| 2407 | last_req_seq.store(VulkanContext::current_before_execute_seq(), std::memory_order_release); | ||
| 2408 | if (n == 1) { | ||
| 2409 | in_lead.store(true, std::memory_order_release); | ||
| 2410 | while (!go_lead.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 2411 | } | ||
| 2412 | }); | ||
| 2413 | |||
| 2414 | VulkanFence done_wait_f(context, 0), done_b1(context, 0); | ||
| 2415 | EXPECT(done_wait_f.semaphore() && done_b1.semaphore()); | ||
| 2416 | |||
| 2417 | EXPECT(direct.execute({&b0, 1}) == ReplayResult::success); // pauses in before_execute_() | ||
| 2418 | while (!in_lead.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 2419 | |||
| 2420 | // Enqueue b1, then Wait(done_wait_f, 1) (already CPU-signaled with a HIGHER vis_seq!), then b2. | ||
| 2421 | EXPECT(direct.execute({&b1, 1}) == ReplayResult::success); | ||
| 2422 | EXPECT(done_wait_f.signal(1)); // GPU counter is already 1 AND carries a fresh CPU vis_seq! | ||
| 2423 | const std::uint64_t done_wait_vis = context.holds().signaled_vis_seq(done_wait_f.semaphore(), 1); | ||
| 2424 | direct.wait(done_wait_f.semaphore(), 1); | ||
| 2425 | EXPECT(direct.execute({&b2, 1}) == ReplayResult::success); | ||
| 2426 | EXPECT(direct.signal(done_b1.semaphore(), 1)); | ||
| 2427 | |||
| 2428 | go_lead.store(true, std::memory_order_release); | ||
| 2429 | EXPECT(done_b1.wait(1, 5'000'000'000ull)); | ||
| 2430 | EXPECT(direct.idle()); | ||
| 2431 | |||
| 2432 | // {b0} (1) + coalesced {b1, completed Wait, b2} (2) = exactly 2 submits, and req_seq >= done_wait_vis! | ||
| 2433 | EXPECT(before_calls.load(std::memory_order_acquire) == 2); | ||
| 2434 | EXPECT(s_r8_cb_submits.load(std::memory_order_acquire) == 2); | ||
| 2435 | EXPECT(last_req_seq.load(std::memory_order_acquire) >= done_wait_vis); | ||
| 2436 | EXPECT(db[16] == 0x81 && db[32] == 0x82 && db[48] == 0x83 && db[0] == 0x83); | ||
| 2437 | |||
| 2438 | // (b2) Coalescing past already-GPU-completed Wait in drain_held_while_executing() + CPU vis_seq merge. | ||
| 2439 | before_calls.store(0, std::memory_order_release); | ||
| 2440 | s_r8_cb_submits.store(0, std::memory_order_release); | ||
| 2441 | last_req_seq.store(0, std::memory_order_release); | ||
| 2442 | std::memset(db, 0, kBufBytes); | ||
| 2443 | context.set_before_execute([&] { | ||
| 2444 | before_calls.fetch_add(1, std::memory_order_acq_rel); | ||
| 2445 | last_req_seq.store(VulkanContext::current_before_execute_seq(), std::memory_order_release); | ||
| 2446 | }); | ||
| 2447 | |||
| 2448 | VulkanFence hold_gate(context, 0), held_cpu_f(context, 0), done_b2(context, 0); | ||
| 2449 | EXPECT(hold_gate.semaphore() && held_cpu_f.semaphore() && done_b2.semaphore()); | ||
| 2450 | |||
| 2451 | direct.wait(hold_gate.semaphore(), 1); | ||
| 2452 | EXPECT(direct.execute({&b1, 1}) == ReplayResult::success); | ||
| 2453 | direct.wait(held_cpu_f.semaphore(), 1); // unsignaled when queued -> lands in b2.waits in held_ | ||
| 2454 | EXPECT(direct.execute({&b2, 1}) == ReplayResult::success); | ||
| 2455 | EXPECT(direct.signal(done_b2.semaphore(), 1)); | ||
| 2456 | EXPECT(direct.idle()); // both b1 and b2 are now in held_ | ||
| 2457 | EXPECT(before_calls.load(std::memory_order_acquire) == 0); | ||
| 2458 | |||
| 2459 | // Signal held_cpu_f from CPU first (now GPU-completed with a newer vis_seq), THEN release hold_gate! | ||
| 2460 | EXPECT(held_cpu_f.signal(1)); | ||
| 2461 | const std::uint64_t held_cpu_vis = context.holds().signaled_vis_seq(held_cpu_f.semaphore(), 1); | ||
| 2462 | EXPECT(hold_gate.signal(1)); | ||
| 2463 | EXPECT(done_b2.wait(1, 5'000'000'000ull)); | ||
| 2464 | EXPECT(direct.idle()); | ||
| 2465 | |||
| 2466 | // Held {b1, completed Wait(held_cpu_f), b2} coalesced into ONE submit & preserved held_cpu_vis! | ||
| 2467 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); | ||
| 2468 | EXPECT(s_r8_cb_submits.load(std::memory_order_acquire) == 1); | ||
| 2469 | EXPECT(last_req_seq.load(std::memory_order_acquire) >= held_cpu_vis); | ||
| 2470 | EXPECT(db[32] == 0x82 && db[48] == 0x83 && db[0] == 0x83); | ||
| 2471 | |||
| 2472 | // (c) Dedicated QueueHolds barrier-drain thread: releaser returns immediately while barrier blocks, | ||
| 2473 | // any_progress() stays true, post-barrier Frame N+1 strictly waits for barrier completion OUTSIDE | ||
| 2474 | // pump_mutex_, and an independent compute queue's held work still pumps and completes while direct's | ||
| 2475 | // Frame N+1 is waiting on blocked Present N! | ||
| 2476 | VulkanQueue compute_c(context, d.queue, d.family, VulkanQueue::QueueKind::compute); | ||
| 2477 | EXPECT(compute_c.ok()); | ||
| 2478 | compute_c.set_async_depth(4); | ||
| 2479 | |||
| 2480 | before_calls.store(0, std::memory_order_release); | ||
| 2481 | std::memset(db, 0, kBufBytes); | ||
| 2482 | context.set_before_execute([&] { before_calls.fetch_add(1, std::memory_order_acq_rel); }); | ||
| 2483 | |||
| 2484 | VulkanFence f_gate(context, 0), f_done_c(context, 0), comp_gate_c(context, 0), comp_done_c(context, 0); | ||
| 2485 | EXPECT(f_gate.semaphore() && f_done_c.semaphore() && comp_gate_c.semaphore() && comp_done_c.semaphore()); | ||
| 2486 | |||
| 2487 | direct.wait(f_gate.semaphore(), 1); | ||
| 2488 | EXPECT(direct.execute({&b0, 1}) == ReplayResult::success); // Frame N | ||
| 2489 | EXPECT(direct.idle()); | ||
| 2490 | |||
| 2491 | std::atomic<bool> in_present{false}, release_present{false}; | ||
| 2492 | std::atomic<unsigned> present_saw_before{0}; | ||
| 2493 | int present_owner = 0; | ||
| 2494 | context.holds().after_held(&present_owner, [&] { | ||
| 2495 | present_saw_before.store(before_calls.load(std::memory_order_acquire), std::memory_order_release); | ||
| 2496 | in_present.store(true, std::memory_order_release); | ||
| 2497 | while (!release_present.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 2498 | }); | ||
| 2499 | |||
| 2500 | EXPECT(direct.execute({&b1, 1}) == ReplayResult::success); // Frame N+1 (must wait for Present N!) | ||
| 2501 | EXPECT(direct.signal(f_done_c.semaphore(), 1)); | ||
| 2502 | EXPECT(direct.idle()); | ||
| 2503 | |||
| 2504 | // Hold independent work on compute_c behind comp_gate_c. | ||
| 2505 | compute_c.wait(comp_gate_c.semaphore(), 1); | ||
| 2506 | EXPECT(compute_c.execute({&empty_snap, 1}) == ReplayResult::success); | ||
| 2507 | EXPECT(compute_c.signal(comp_done_c.semaphore(), 1)); | ||
| 2508 | EXPECT(compute_c.idle()); | ||
| 2509 | |||
| 2510 | // Releaser signals f_gate: MUST return immediately without stalling on blocked Present N! | ||
| 2511 | EXPECT(f_gate.signal(1)); | ||
| 2512 | while (!in_present.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 2513 | |||
| 2514 | // While Present N is blocked on the barrier thread AND direct's async_worker is waiting in | ||
| 2515 | // wait_barriers_drained() for Frame N+1: | ||
| 2516 | // - any_progress() == true; | ||
| 2517 | // - Frame N+1 (b1) has NOT run yet; | ||
| 2518 | // - pump_mutex_ MUST be free so comp_gate_c.signal(1) pumps compute_c and completes comp_done_c immediately! | ||
| 2519 | std::this_thread::sleep_for(std::chrono::milliseconds(15)); | ||
| 2520 | EXPECT(queue_holds::any_progress()); | ||
| 2521 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); // only Frame N (b0) ran; b1 waits for Present N! | ||
| 2522 | EXPECT(present_saw_before.load(std::memory_order_acquire) == 1); | ||
| 2523 | EXPECT(comp_gate_c.signal(1)); | ||
| 2524 | EXPECT(comp_done_c.wait(1, 2'000'000'000ull)); | ||
| 2525 | EXPECT(compute_c.idle()); | ||
| 2526 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); // b1 STILL waits for Present N! | ||
| 2527 | |||
| 2528 | release_present.store(true, std::memory_order_release); | ||
| 2529 | EXPECT(f_done_c.wait(1, 5'000'000'000ull)); | ||
| 2530 | EXPECT(direct.idle()); | ||
| 2531 | EXPECT(before_calls.load(std::memory_order_acquire) == 2); | ||
| 2532 | EXPECT(db[16] == 0x81 && db[32] == 0x82 && db[0] == 0x82); | ||
| 2533 | std::printf("queue r8 ws3: empty-batch skip+vis carry, completed-wait coalescing, dedicated barrier thread ok\n"); | ||
| 2534 | } | ||
| 2535 | |||
| 2280 | return 0; | 2536 | return 0; |
| 2281 | } | 2537 | } |
| 2282 | 2538 | ||