From c01fe0c8628f9ab301606dab62c0e89a6f79c546 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 27 Nov 2026 01:08:02 -0500 Subject: [PATCH] borealis: prune retired exact priors and prefer fragment barriers On queues using exact historical ordinals (M5 and HAL200), last_absolute and tail ordinals remain nonzero indefinitely after the referenced submission has retired on the GPU. For example, a single earlier HK_SYNC_GFX_MEM render leaves a stale prior_ta[VDM] ordinal on thousands of subsequent renders, preventing DRM_ASAHI_RENDER_FRAGMENT_BARRIERS and forcing M5 renders onto the staged render path. - Record a 64-entry ring on hk_sync_queue mapping each submission's timeline point to its submitted VDM/CDM ordinals, and advance retired_ord[sq] whenever completed advances from drmSyncobjQuery() or the cached queue signalled_value. - Prune retired ordinals (<= retired_ord[sq]) to 0 in last_absolute, tail, and resolved prior/prior_ta fields before computing ta_split. - Prefer DRM_ASAHI_RENDER_FRAGMENT_BARRIERS over DRM_ASAHI_RENDER_STAGE_PREFIXES when the vertex half has no render prefix and an unchanged compute prefix, preserving identical ordering with a smaller render command eligible for direct kernel publication. Signed-off-by: DjDeveloperr --- src/asahi/vulkan/hk_queue.c | 38 +++++++++--- src/asahi/vulkan/hk_sync.c | 64 +++++++++++++++++++++ src/asahi/vulkan/hk_sync.h | 14 ++++- src/asahi/vulkan/tests/test-hk-sync-prior.c | 49 ++++++++++++++++ 4 files changed, 155 insertions(+), 10 deletions(-) diff --git a/src/asahi/vulkan/hk_queue.c b/src/asahi/vulkan/hk_queue.c index ffc1a4e61b6e..9f25d2c8f4ba 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -2216,18 +2216,20 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, nr_cdm++; } - /* Tile hazards against this queue's earlier submissions need the point - * the queue has completed; fetched only when a partial-tile render pass - * instance is submitted while such a footprint may still be running. On - * failure every earlier submission is treated as still running. */ - uint64_t completed = 0; + /* Tile hazards against this queue's earlier submissions and retired exact + * prior pruning need the point the queue has completed. Start from the + * cached monotonic lower bound and refresh via drmSyncobjQuery() when live + * footprints or unretired exact priors exist. On failure, earlier + * submissions remain conservatively treated as running. */ + uint64_t completed = p_atomic_read(&queue->drm.signalled_value); if (hk_sync_submit_needs_completed( queue, (struct hk_cmd_buffer *const *)submit->command_buffers, - submit->command_buffer_count)) { + submit->command_buffer_count) || + hk_sync_submit_has_unretired_priors(queue)) { uint64_t point = 0; if (drmSyncobjQuery(dev->dev.fd, &queue->drm.syncobj, &point, 1) == 0) { hk_queue_note_signalled(queue, point); - completed = point; + completed = MAX2(completed, point); } } @@ -2574,18 +2576,36 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, } hk_query_stamps_note_cs(dev, queue, queue->drm.timeline_value, cs); + /* Prefer DRM_ASAHI_RENDER_FRAGMENT_BARRIERS over staged render + * prefixes whenever the vertex half needs no render prefix and the + * compute prefix is unchanged: both describe identical ordering, + * while FRAGMENT_BARRIERS keeps the smaller render payload and + * stays eligible for direct kernel publication. */ + const bool m5_frag_barriers_only = + (dev->dev.params.features & + DRM_ASAHI_FEATURE_FRAGMENT_BARRIERS) && + cs->sync.ta_split && + cs->sync.barrier_ta[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE && + cs->sync.prior_ta[HK_SYNC_VDM] == 0 && + ta_command_prior[HK_SYNC_VDM] == 0 && + cs->sync.barrier_ta[HK_SYNC_CDM] == cs->sync.barrier[HK_SYNC_CDM] && + cs->sync.prior_ta[HK_SYNC_CDM] == cs->sync.prior[HK_SYNC_CDM] && + ta_command_prior[HK_SYNC_CDM] == command_prior[HK_SYNC_CDM] && + (cs->sync.barrier[HK_SYNC_VDM] != DRM_ASAHI_BARRIER_NONE || + cs->sync.prior[HK_SYNC_VDM] != 0); + /* M5: the vertex half's own render and compute prefixes, when * they are narrower than the whole render's, so its tiling can * overlap the fragment work of the renders only its fragment * half depends on. Supersedes the single-subqueue forms below. */ const bool m5_staged_v2 = agx_chip_g17_m5(dev->dev.chip) && !queue->sync.serialize && - queue->sync.exact_prior && + queue->sync.exact_prior && !m5_frag_barriers_only && (dev->dev.params.features & DRM_ASAHI_FEATURE_RENDER_STAGE_PREFIXES_V2) && cs->sync.ta_split; const bool stage_prefixes = - !m5_staged_v2 && + !m5_staged_v2 && !m5_frag_barriers_only && agx_chip_g17_m5(dev->dev.chip) && !queue->sync.serialize && queue->sync.exact_prior && (dev->dev.params.features & diff --git a/src/asahi/vulkan/hk_sync.c b/src/asahi/vulkan/hk_sync.c index 2ad16a775a02..3beb648cd6c4 100644 --- a/src/asahi/vulkan/hk_sync.c +++ b/src/asahi/vulkan/hk_sync.c @@ -2524,6 +2524,51 @@ hk_sync_submit_needs_completed(const struct hk_queue *queue, return false; } +bool +hk_sync_submit_has_unretired_priors(const struct hk_queue *queue) +{ + const struct hk_sync_queue *q = &queue->sync; + return q->exact_prior && q->retired_count > 0 && + (q->submitted[HK_SYNC_VDM] > q->retired_ord[HK_SYNC_VDM] || + q->submitted[HK_SYNC_CDM] > q->retired_ord[HK_SYNC_CDM]); +} + +static void +sync_advance_retired(struct hk_sync_queue *q, uint64_t completed) +{ + const uint32_t n = MIN2(q->retired_count, HK_SYNC_RETIRED_RING); + for (uint32_t i = 0; i < n; ++i) { + if (q->retired[i].point && q->retired[i].point <= completed) { + for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) + q->retired_ord[sq] = MAX2(q->retired_ord[sq], q->retired[i].ord[sq]); + } + } + + for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) { + const uint64_t ret = q->retired_ord[sq]; + if (!ret) + continue; + for (unsigned k = 0; k < HK_SYNC_NUM_KINDS; ++k) { + if (q->last_absolute[k][sq] <= ret) + q->last_absolute[k][sq] = 0; + if (q->tail[k][sq] <= ret) + q->tail[k][sq] = 0; + } + } +} + +static void +sync_record_retired(struct hk_sync_queue *q, uint64_t point) +{ + if (!point) + return; + q->retired[q->retired_next].point = point; + for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) + q->retired[q->retired_next].ord[sq] = q->submitted[sq]; + q->retired_next = (q->retired_next + 1) % HK_SYNC_RETIRED_RING; + q->retired_count = MIN2(q->retired_count + 1, HK_SYNC_RETIRED_RING); +} + static uint8_t recent_conflict(const struct hk_sync_queue *q, const struct hk_sync_footprint *fp, uint64_t completed, const struct recent_tile_index *index) @@ -2719,6 +2764,8 @@ hk_sync_resolve_submit_ex(struct hk_queue *queue, if (queue->sync.submitted[sq] > UINT64_MAX - added[sq]) return false; } + if (completed) + sync_advance_retired(&queue->sync, completed); } bool indexed = !queue->sync.precise_tile_evictions || @@ -3004,6 +3051,21 @@ hk_sync_resolve_submit_ex(struct hk_queue *queue, covered[sq] = cs->sync.index; } + /* Prune exact historical priors already retired on the queue timeline + * before computing ta_split so stale ordinals (e.g. an old GFX_MEM + * render) do not block fragment-only barriers or direct submission. */ + if (queue->sync.exact_prior) { + for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) { + const uint64_t ret = queue->sync.retired_ord[sq]; + if (ret) { + if (cs->sync.prior[sq] <= ret) + cs->sync.prior[sq] = 0; + if (cs->sync.prior_ta[sq] <= ret) + cs->sync.prior_ta[sq] = 0; + } + } + } + /* A render's vertex half may wait for less than its fragment half * only when the kernel orders the halves separately and nothing * above raised the whole render's fields afterwards (the @@ -3080,6 +3142,8 @@ hk_sync_resolve_submit_ex(struct hk_queue *queue, *tail_raised_out = tail_raised; for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) queue->sync.submitted[sq] += ss.count[sq]; + if (ss.count[HK_SYNC_VDM] || ss.count[HK_SYNC_CDM]) + sync_record_retired(&queue->sync, queue->drm.timeline_value); } /* This queue has now run these kinds. */ diff --git a/src/asahi/vulkan/hk_sync.h b/src/asahi/vulkan/hk_sync.h index 1725281a151d..b347bcf3b526 100644 --- a/src/asahi/vulkan/hk_sync.h +++ b/src/asahi/vulkan/hk_sync.h @@ -531,12 +531,23 @@ struct hk_sync_queue { /* A single command buffer already owns the complete footprint list. */ bool direct_single_footprints; uint64_t last_preserve, evicted_preserve; - /* Recent VDM renders' attachment blooms for exact-image HK_SYNC_GFX_IMG * narrowing across submissions. */ struct hk_sync_vdm_att recent_vdm_att[HK_SYNC_RECENT_VDM_ATT]; uint32_t recent_vdm_att_next, recent_vdm_att_count; uint64_t recent_vdm_att_evicted; + + /* Retired-ordinal map for exact priors: each entry pairs a queue timeline + * point with the submitted VDM/CDM ordinals through that submission. Once + * `completed >= point`, every ordinal up to `ord[sq]` has retired and can + * be pruned to 0. */ +#define HK_SYNC_RETIRED_RING 64 + struct { + uint64_t point; + uint64_t ord[HK_SYNC_NUM_SQ]; + } retired[HK_SYNC_RETIRED_RING]; + uint32_t retired_next, retired_count; + uint64_t retired_ord[HK_SYNC_NUM_SQ]; }; void hk_sync_reset(struct hk_cmd_buffer *cmd); @@ -758,6 +769,7 @@ void hk_sync_apply_dep(struct hk_cmd_buffer *cmd, struct hk_cs *into, bool hk_sync_submit_needs_completed(const struct hk_queue *queue, struct hk_cmd_buffer *const *cmdbufs, unsigned count); +bool hk_sync_submit_has_unretired_priors(const struct hk_queue *queue); /* The driver's own resolve pass after a render: waits on the render. */ void hk_sync_resolve_begin(struct hk_cmd_buffer *cmd); diff --git a/src/asahi/vulkan/tests/test-hk-sync-prior.c b/src/asahi/vulkan/tests/test-hk-sync-prior.c index 711eff9d02a6..24106e4be531 100644 --- a/src/asahi/vulkan/tests/test-hk-sync-prior.c +++ b/src/asahi/vulkan/tests/test-hk-sync-prior.c @@ -3097,6 +3097,55 @@ main(void) p_atomic_read(&mq->drm.signalled_value) == 12); } test_vertex_image_bloom(); + /* Retired exact prior pruning drops completed VDM/CDM ordinals while + * preserving unretired dependencies and FRAGMENT_BARRIERS ta_split. */ + { + struct hk_queue *rq = new_queue(true); + /* Submit 1 (point 10): HK_SYNC_GFX_MEM render (ord 1) + barrier. */ + rq->drm.timeline_value = 10; + struct hk_cmd_buffer *s1 = new_cmd(); + struct hk_cs *r_mem = add(s1, false); + hk_sync_note_memory_write(s1, r_mem); + hk_sync_barrier(s1, HK_SYNC_ALL, HK_SYNC_ALL); + T("retired-prior setup s1", + hk_sync_resolve_submit(rq, &s1, 1, false, false, 0, NULL)); + T("retired-prior reports unretired priors after s1", + hk_sync_submit_has_unretired_priors(rq)); + + /* Submit 2 (point 11): plain render (ord 2) + fragment-only barrier, + * while point 10 is still in flight (completed = 0). */ + rq->drm.timeline_value = 11; + struct hk_cmd_buffer *s2 = new_cmd(); + struct hk_cs *r2 = add(s2, false); + hk_sync_barrier(s2, HK_SYNC_GFX, HK_SYNC_GFX_FRAG); + T("retired-prior s2 unretired", + hk_sync_resolve_submit(rq, &s2, 1, false, false, 0, NULL)); + T("retired-prior keeps unretired GFX_MEM prior_ta", + r2->sync.prior[HK_SYNC_VDM] == 1 && r2->sync.prior_ta[HK_SYNC_VDM] == 1); + + /* Submit 3 (point 12): point 10 has retired (completed = 10), point 11 + * is still live -> prior_ta[VDM] (1) prunes to 0 while prior[VDM] (2) + * stays live with ta_split=true! */ + rq->drm.timeline_value = 12; + struct hk_cmd_buffer *s3 = new_cmd(); + struct hk_cs *r3 = add(s3, false); + T("retired-prior s3 partial retirement", + hk_sync_resolve_submit(rq, &s3, 1, false, false, 10, NULL)); + T("retired-prior prunes retired prior_ta while keeping live fragment prior", + r3->sync.prior[HK_SYNC_VDM] == 2 && r3->sync.prior_ta[HK_SYNC_VDM] == 0 && + r3->sync.ta_split); + + /* Submit 4 (point 13): everything through point 12 has retired -> both + * prior and prior_ta prune to 0 and ta_split clears. */ + rq->drm.timeline_value = 13; + struct hk_cmd_buffer *s4 = new_cmd(); + struct hk_cs *r4 = add(s4, false); + T("retired-prior s4 full retirement", + hk_sync_resolve_submit(rq, &s4, 1, false, false, 12, NULL)); + T("retired-prior clears both priors and ta_split when all retired", + r4->sync.prior[HK_SYNC_VDM] == 0 && r4->sync.prior_ta[HK_SYNC_VDM] == 0 && + !r4->sync.ta_split); + } printf("RESULT checks=%u failures=%u\n", tests, failures); ralloc_free(test_context); return failures ? EXIT_FAILURE : EXIT_SUCCESS;