diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 05:38:11 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 05:38:11 -0500 |
| commit | 6bd17e22a15533c3724b4d4dfa9198620f61baef (patch) | |
| tree | 608d6550768f3e709d18f77db71842c7443b6b97 /runtime/test/submit_frame_test.cpp | |
| parent | 8f7826117905a4d2149199e1f734a871f10183bd (diff) | |
runtime: Round 10 WS3 — GPU-met gate vis_seq=0, retroactive GPU-to-GPU wait relay, coalescer reorder, readable fast path & decoupled GPU profile resolve
- Add VulkanFence::signal_gpu_relay() (vis_seq=0) strictly for GPU-landed list-value gates (!force && !lost && !action && !cpu_met)
- Retroactive GPU-to-GPU relay via QueueHolds::relay_wait() + VulkanQueue wait substitution (never GPU-signaling values->gate, reversing wait() before add_gate(), requiring after_gate==0 && different queue)
- Decouple gpu_profile_.resolve() onto lazy resolver thread off FenceWaitDispatcher & throttle take_open() device-loss checks
- Reorder BeforeExecuteCoalescer to wb -> mirror -> flush -> tex -> shadows -> 2nd flush -> conditional 2nd tex (killing in_feed CPU fallbacks)
- Hoist host_stop & add negative caching in TextureVqCache + Stage 2b all-16 Cell::restore[] readability bypass
AI-assisted: Antigravity (Gemini 4 Neon).
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
| -rw-r--r-- | runtime/test/submit_frame_test.cpp | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp index 7f1ba1751..0d7ba9d74 100644 --- a/runtime/test/submit_frame_test.cpp +++ b/runtime/test/submit_frame_test.cpp | |||
| @@ -2528,6 +2528,231 @@ int queue_sync_and_async() { | |||
| 2528 | std::printf("queue r8 ws3: empty-batch skip+vis carry, completed-wait coalescing, dedicated barrier thread ok\n"); | 2528 | std::printf("queue r8 ws3: empty-batch skip+vis carry, completed-wait coalescing, dedicated barrier thread ok\n"); |
| 2529 | } | 2529 | } |
| 2530 | 2530 | ||
| 2531 | // 9. Round 10 WS3: VulkanFence::signal_gpu_relay() (vis_seq = 0 vs CPU Signal vis_seq > 0) and retroactive | ||
| 2532 | // QueueHolds::relay_wait() (single-wait in held_, multi-wait in held_ & inbox_, in-transit race ordering, | ||
| 2533 | // never signaling gate_sem, balanced note_wait_added/removed & active_work_). | ||
| 2534 | { | ||
| 2535 | VulkanContext context(d.device_proc, d.device, true); | ||
| 2536 | EXPECT(context.loaded()); | ||
| 2537 | VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 2538 | VulkanQueue compute_a(context, d.queue, d.family, VulkanQueue::QueueKind::compute); | ||
| 2539 | VulkanQueue compute_b(context, d.queue, d.family, VulkanQueue::QueueKind::compute); | ||
| 2540 | EXPECT(direct.ok() && compute_a.ok() && compute_b.ok()); | ||
| 2541 | direct.set_async_depth(4); | ||
| 2542 | compute_a.set_async_depth(4); | ||
| 2543 | compute_b.set_async_depth(4); | ||
| 2544 | compute_a.set_skip_before_execute(true); | ||
| 2545 | compute_b.set_skip_before_execute(true); | ||
| 2546 | |||
| 2547 | void* src_map = nullptr; | ||
| 2548 | void* mid_map = nullptr; | ||
| 2549 | void* dst_map = nullptr; | ||
| 2550 | VkBuffer src = d.buffer(kBufBytes, usage, &src_map); | ||
| 2551 | VkBuffer mid = d.buffer(kBufBytes, usage, &mid_map); | ||
| 2552 | VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map); | ||
| 2553 | EXPECT(src && mid && dst); | ||
| 2554 | auto* sb = static_cast<std::uint8_t*>(src_map); | ||
| 2555 | auto* mb = static_cast<std::uint8_t*>(mid_map); | ||
| 2556 | auto* db = static_cast<std::uint8_t*>(dst_map); | ||
| 2557 | std::memset(sb, 0, kBufBytes); | ||
| 2558 | std::memset(mb, 0, kBufBytes); | ||
| 2559 | std::memset(db, 0, kBufBytes); | ||
| 2560 | sb[0] = 0x5A; | ||
| 2561 | sb[16] = 0x6B; | ||
| 2562 | sb[32] = 0x7C; | ||
| 2563 | |||
| 2564 | ResourceBinding r_src{}, r_mid{}, r_dst{}; | ||
| 2565 | r_src.buffer = src; | ||
| 2566 | r_src.size = kBufBytes; | ||
| 2567 | r_mid.buffer = mid; | ||
| 2568 | r_mid.size = kBufBytes; | ||
| 2569 | r_dst.buffer = dst; | ||
| 2570 | r_dst.size = kBufBytes; | ||
| 2571 | r_dst.host_readable = true; | ||
| 2572 | context.add_resource(1, r_src); | ||
| 2573 | context.add_resource(2, r_mid); | ||
| 2574 | context.add_resource(3, r_dst); | ||
| 2575 | |||
| 2576 | std::atomic<unsigned> before_calls{0}; | ||
| 2577 | std::atomic<std::uint64_t> last_req_seq{0}; | ||
| 2578 | context.set_before_execute([&] { | ||
| 2579 | before_calls.fetch_add(1, std::memory_order_acq_rel); | ||
| 2580 | last_req_seq.store(VulkanContext::current_before_execute_seq(), std::memory_order_release); | ||
| 2581 | }); | ||
| 2582 | |||
| 2583 | auto empty_list = std::make_shared<CommandList>(); | ||
| 2584 | EXPECT(empty_list->close()); | ||
| 2585 | std::shared_ptr<const CommandList> empty_snap = empty_list; | ||
| 2586 | |||
| 2587 | // (a) GPU-met signal_gpu_relay (vis_seq = 0) vs CPU-met signal (vis_seq > 0). | ||
| 2588 | { | ||
| 2589 | VulkanFence gate_gpu(context, 0), gate_cpu(context, 0), done_a(context, 0), done_b(context, 0); | ||
| 2590 | EXPECT(gate_gpu.semaphore() && gate_cpu.semaphore() && done_a.semaphore() && done_b.semaphore()); | ||
| 2591 | |||
| 2592 | direct.wait(gate_gpu.semaphore(), 1); | ||
| 2593 | EXPECT(direct.execute({&empty_snap, 1}) == ReplayResult::success); | ||
| 2594 | const std::uint64_t seq_after_enqueue = context.current_visibility_seq(); | ||
| 2595 | EXPECT(direct.signal(done_a.semaphore(), 1)); | ||
| 2596 | EXPECT(direct.idle()); | ||
| 2597 | EXPECT(before_calls.load(std::memory_order_acquire) == 0); | ||
| 2598 | |||
| 2599 | EXPECT(gate_gpu.signal_gpu_relay(1)); | ||
| 2600 | EXPECT(context.current_visibility_seq() == seq_after_enqueue); // vis_seq NOT bumped! | ||
| 2601 | EXPECT(context.holds().signaled_vis_seq(gate_gpu.semaphore(), 1) == 0); | ||
| 2602 | EXPECT(done_a.wait(1, 5'000'000'000ull)); | ||
| 2603 | EXPECT(direct.idle()); | ||
| 2604 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); | ||
| 2605 | EXPECT(last_req_seq.load(std::memory_order_acquire) == seq_after_enqueue); | ||
| 2606 | |||
| 2607 | // CPU-met signal MUST bump visibility_seq and propagate into before_execute_(). | ||
| 2608 | direct.wait(gate_cpu.semaphore(), 1); | ||
| 2609 | EXPECT(direct.execute({&empty_snap, 1}) == ReplayResult::success); | ||
| 2610 | const std::uint64_t seq_before_cpu = context.current_visibility_seq(); | ||
| 2611 | EXPECT(direct.signal(done_b.semaphore(), 1)); | ||
| 2612 | EXPECT(direct.idle()); | ||
| 2613 | |||
| 2614 | EXPECT(gate_cpu.signal(1)); | ||
| 2615 | const std::uint64_t cpu_vis = context.holds().signaled_vis_seq(gate_cpu.semaphore(), 1); | ||
| 2616 | EXPECT(cpu_vis > seq_before_cpu); | ||
| 2617 | EXPECT(done_b.wait(1, 5'000'000'000ull)); | ||
| 2618 | EXPECT(direct.idle()); | ||
| 2619 | EXPECT(last_req_seq.load(std::memory_order_acquire) >= cpu_vis); | ||
| 2620 | } | ||
| 2621 | |||
| 2622 | // (b) Retroactive single-wait relay in held_: direct waits on gate_single, compute_a writes mid[0], | ||
| 2623 | // relay_wait redirects (gate_single, 1) -> (written_a, 1) WITHOUT signaling gate_single! | ||
| 2624 | { | ||
| 2625 | std::memset(mb, 0, kBufBytes); | ||
| 2626 | std::memset(db, 0, kBufBytes); | ||
| 2627 | before_calls.store(0, std::memory_order_release); | ||
| 2628 | last_req_seq.store(0, std::memory_order_release); | ||
| 2629 | |||
| 2630 | auto write_mid0 = make_copy_list(2, 0, 1, 0, 1); // mid[0] = 0x5A | ||
| 2631 | auto read_mid0 = make_copy_list(3, 0, 2, 0, 1); // dst[0] = mid[0] | ||
| 2632 | EXPECT(write_mid0 && read_mid0); | ||
| 2633 | std::shared_ptr<const CommandList> sw = write_mid0, sr = read_mid0; | ||
| 2634 | |||
| 2635 | VulkanFence gate_single(context, 0), written_a(context, 0), done_s(context, 0); | ||
| 2636 | EXPECT(gate_single.semaphore() && written_a.semaphore() && done_s.semaphore()); | ||
| 2637 | |||
| 2638 | direct.wait(gate_single.semaphore(), 1); | ||
| 2639 | EXPECT(direct.execute({&sr, 1}) == ReplayResult::success); | ||
| 2640 | const std::uint64_t seq_after_direct = context.current_visibility_seq(); | ||
| 2641 | EXPECT(direct.signal(done_s.semaphore(), 1)); | ||
| 2642 | EXPECT(direct.idle()); // sr is held behind gate_single | ||
| 2643 | EXPECT(before_calls.load(std::memory_order_acquire) == 0); | ||
| 2644 | |||
| 2645 | EXPECT(compute_a.execute({&sw, 1}) == ReplayResult::success); | ||
| 2646 | EXPECT(compute_a.signal(written_a.semaphore(), 1)); | ||
| 2647 | |||
| 2648 | const QueueHolds<VkSemaphore>::Wait rep[] = {{written_a.semaphore(), 1, {}}}; | ||
| 2649 | EXPECT(context.holds().relay_wait(gate_single.semaphore(), 1, rep, /*consume_on_substitute=*/true)); | ||
| 2650 | EXPECT(done_s.wait(1, 5'000'000'000ull)); | ||
| 2651 | EXPECT(direct.idle() && compute_a.idle()); | ||
| 2652 | EXPECT(gate_single.completed() == 0); // gate_single was NEVER signaled! | ||
| 2653 | EXPECT(db[0] == 0x5A); // GPU ordering preserved! | ||
| 2654 | EXPECT(context.current_visibility_seq() == seq_after_direct); | ||
| 2655 | EXPECT(last_req_seq.load(std::memory_order_acquire) == seq_after_direct); | ||
| 2656 | EXPECT(context.holds().active_relay_count() == 0); | ||
| 2657 | EXPECT(!context.has_unsatisfied_waiter(gate_single.semaphore(), 1)); | ||
| 2658 | } | ||
| 2659 | |||
| 2660 | // (c) Retroactive multi-wait relay in held_ (two distinct writers, one initially blocked) AND in inbox_ | ||
| 2661 | // (expanding 1 Call::Kind::wait into 2 Calls while async_worker is busy inside before_execute_()). | ||
| 2662 | { | ||
| 2663 | std::memset(mb, 0, kBufBytes); | ||
| 2664 | std::memset(db, 0, kBufBytes); | ||
| 2665 | auto write_m1 = make_copy_list(2, 16, 1, 16, 1); // mid[16] = 0x6B | ||
| 2666 | auto write_m2 = make_copy_list(2, 32, 1, 32, 1); // mid[32] = 0x7C | ||
| 2667 | auto read_both = std::make_shared<CommandList>(); | ||
| 2668 | EXPECT(read_both->copy_buffer({3, 16, 2, 16, 1}) && read_both->copy_buffer({3, 32, 2, 32, 1}) && | ||
| 2669 | read_both->close()); | ||
| 2670 | EXPECT(write_m1 && write_m2); | ||
| 2671 | std::shared_ptr<const CommandList> sm1 = write_m1, sm2 = write_m2, srb = read_both; | ||
| 2672 | |||
| 2673 | VulkanFence gate_multi(context, 0), gate_b_hold(context, 0), written_a(context, 0), written_b(context, 0), | ||
| 2674 | done_m(context, 0); | ||
| 2675 | EXPECT(gate_multi.semaphore() && gate_b_hold.semaphore() && written_a.semaphore() && | ||
| 2676 | written_b.semaphore() && done_m.semaphore()); | ||
| 2677 | |||
| 2678 | // Hold direct in held_ behind gate_multi; compute_a submits immediately; compute_b is still held behind | ||
| 2679 | // gate_b_hold so written_b is pending when relay_wait runs! | ||
| 2680 | direct.wait(gate_multi.semaphore(), 1); | ||
| 2681 | EXPECT(direct.execute({&srb, 1}) == ReplayResult::success); | ||
| 2682 | EXPECT(direct.signal(done_m.semaphore(), 1)); | ||
| 2683 | EXPECT(direct.idle()); | ||
| 2684 | |||
| 2685 | EXPECT(compute_a.execute({&sm1, 1}) == ReplayResult::success); | ||
| 2686 | EXPECT(compute_a.signal(written_a.semaphore(), 1)); | ||
| 2687 | compute_b.wait(gate_b_hold.semaphore(), 1); | ||
| 2688 | EXPECT(compute_b.execute({&sm2, 1}) == ReplayResult::success); | ||
| 2689 | EXPECT(compute_b.signal(written_b.semaphore(), 1)); | ||
| 2690 | EXPECT(compute_b.idle()); | ||
| 2691 | |||
| 2692 | const QueueHolds<VkSemaphore>::Wait reps[] = {{written_a.semaphore(), 1, {}}, | ||
| 2693 | {written_b.semaphore(), 1, {}}}; | ||
| 2694 | EXPECT(context.holds().relay_wait(gate_multi.semaphore(), 1, reps, /*consume_on_substitute=*/true)); | ||
| 2695 | EXPECT(context.holds().active_relay_count() == 0); | ||
| 2696 | // Release compute_b -> written_b signals -> direct unblocks and reads both mid[16] and mid[32]. | ||
| 2697 | EXPECT(gate_b_hold.signal_gpu_relay(1)); | ||
| 2698 | EXPECT(done_m.wait(1, 5'000'000'000ull)); | ||
| 2699 | EXPECT(direct.idle() && compute_a.idle() && compute_b.idle()); | ||
| 2700 | EXPECT(gate_multi.completed() == 0); | ||
| 2701 | EXPECT(db[16] == 0x6B && db[32] == 0x7C); | ||
| 2702 | |||
| 2703 | // Multi-wait substitution inside inbox_ while direct's worker is blocked inside before_execute_(). | ||
| 2704 | std::memset(db, 0, kBufBytes); | ||
| 2705 | std::atomic<bool> in_lead{false}, go_lead{false}; | ||
| 2706 | before_calls.store(0, std::memory_order_release); | ||
| 2707 | context.set_before_execute([&] { | ||
| 2708 | if (before_calls.fetch_add(1, std::memory_order_acq_rel) == 0) { | ||
| 2709 | in_lead.store(true, std::memory_order_release); | ||
| 2710 | while (!go_lead.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 2711 | } | ||
| 2712 | }); | ||
| 2713 | VulkanFence gate_inbox(context, 0), done_in(context, 0); | ||
| 2714 | EXPECT(gate_inbox.semaphore() && done_in.semaphore()); | ||
| 2715 | EXPECT(direct.execute({&empty_snap, 1}) == ReplayResult::success); | ||
| 2716 | while (!in_lead.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 2717 | |||
| 2718 | direct.wait(gate_inbox.semaphore(), 1); | ||
| 2719 | EXPECT(direct.execute({&srb, 1}) == ReplayResult::success); | ||
| 2720 | EXPECT(direct.signal(done_in.semaphore(), 1)); | ||
| 2721 | EXPECT(context.holds().relay_wait(gate_inbox.semaphore(), 1, reps, /*consume_on_substitute=*/true)); | ||
| 2722 | EXPECT(context.holds().active_relay_count() == 0); | ||
| 2723 | |||
| 2724 | go_lead.store(true, std::memory_order_release); | ||
| 2725 | EXPECT(done_in.wait(1, 5'000'000'000ull)); | ||
| 2726 | EXPECT(direct.idle()); | ||
| 2727 | EXPECT(gate_inbox.completed() == 0); | ||
| 2728 | EXPECT(db[16] == 0x6B && db[32] == 0x7C); | ||
| 2729 | EXPECT(!context.holds().has_progress()); // held/work counts stayed balanced across inbox expansion! | ||
| 2730 | } | ||
| 2731 | |||
| 2732 | // (d) Race ordering (in-transit lookup_relay): relay_wait registered BEFORE wait_sync sees (gate_race, 1). | ||
| 2733 | { | ||
| 2734 | context.set_before_execute({}); | ||
| 2735 | VulkanFence gate_race(context, 0), written_r(context, 0), done_r(context, 0); | ||
| 2736 | EXPECT(gate_race.semaphore() && written_r.semaphore() && done_r.semaphore()); | ||
| 2737 | EXPECT(compute_a.signal(written_r.semaphore(), 1)); | ||
| 2738 | const QueueHolds<VkSemaphore>::Wait rep[] = {{written_r.semaphore(), 1, {}}}; | ||
| 2739 | EXPECT(context.holds().relay_wait(gate_race.semaphore(), 1, rep, /*consume_on_substitute=*/true)); | ||
| 2740 | EXPECT(context.holds().active_relay_count() == 1); // retained until in-transit wait consumes it | ||
| 2741 | |||
| 2742 | direct.wait(gate_race.semaphore(), 1); | ||
| 2743 | EXPECT(direct.execute({&empty_snap, 1}) == ReplayResult::success); | ||
| 2744 | EXPECT(direct.signal(done_r.semaphore(), 1)); | ||
| 2745 | EXPECT(done_r.wait(1, 5'000'000'000ull)); | ||
| 2746 | EXPECT(direct.idle() && compute_a.idle()); | ||
| 2747 | EXPECT(gate_race.completed() == 0); | ||
| 2748 | EXPECT(context.holds().active_relay_count() == 0); | ||
| 2749 | EXPECT(!context.has_unsatisfied_waiter(gate_race.semaphore(), 1)); | ||
| 2750 | EXPECT(!context.holds().has_progress()); | ||
| 2751 | } | ||
| 2752 | |||
| 2753 | std::printf("queue r10 ws3: signal_gpu_relay vis_seq=0, retroactive relay_wait (single/multi/inbox/race) ok\n"); | ||
| 2754 | } | ||
| 2755 | |||
| 2531 | return 0; | 2756 | return 0; |
| 2532 | } | 2757 | } |
| 2533 | 2758 | ||