From 2d7a83b199a6a323e185ed9f90cff864d0b8fbe9 Mon Sep 17 00:00:00 2001 From: Joshua Warren Date: Sun, 22 Nov 2026 13:29:30 -0600 Subject: [PATCH 1/2] hk: elide the trailing flush between consecutive application dispatches on AGX2 The AGX3 native path already drops the weak ordering barrier between application dispatches on chips where agx_chip_elide_app_dispatch_barrier() holds. The AGX2 path flushes after every launch instead: the conservative record agx_cdm_barrier() documents ('set these after every launch to be safe'). An application-requested dependency still emits its own barrier, which ends elision until the next pair of adjacent dispatches. Mirror the AGX3 mechanism on the AGX2 launch path: each application dispatch marks the stream tail with app_dispatch_end, and the following application dispatch rewinds over the previous launch's trailing flush when nothing else recorded in between. Meta work, indirect launches and internal stages keep the flush-after-launch contract. Extend agx_chip_elide_app_dispatch_barrier() to the AGX2 command-stream generation. --- src/asahi/lib/agx_chip.h | 9 ++++- src/asahi/vulkan/hk_cmd_buffer.c | 2 +- src/asahi/vulkan/hk_cmd_buffer.h | 38 +++++++++++++++---- src/asahi/vulkan/hk_cmd_dispatch.c | 18 +++++++-- src/asahi/vulkan/hk_cmd_draw.c | 6 +-- .../tests/test-hk-execution-allowance.c | 34 +++++++++++++++++ 6 files changed, 91 insertions(+), 16 deletions(-) diff --git a/src/asahi/lib/agx_chip.h b/src/asahi/lib/agx_chip.h index d22b2b0be4c5..78e1693bd9e3 100644 --- a/src/asahi/lib/agx_chip.h +++ b/src/asahi/lib/agx_chip.h @@ -382,11 +382,16 @@ agx_chip_cdm_wide_visibility(enum agx_chip chip) * ordering barrier between them. Vulkan orders dispatches only through * explicit barriers, and Apple's G15 encoder emits none between independent * dispatches; every other record (internal or indirect launch, cache flush, - * link, stream end) still follows a barrier. */ + * link, stream end) still follows a barrier. AGX2 chips take the same + * elision: their per-launch flush is the conservative record + * agx_cdm_barrier() documents ("set these after every launch to be safe"), + * and an application-requested dependency still emits its own barrier, + * which ends elision until the next pair of adjacent dispatches. */ static inline bool agx_chip_elide_app_dispatch_barrier(enum agx_chip chip) { - return agx_chip_native_g15(chip); + return agx_chip_native_g15(chip) || + agx_chip_command_stream_generation(chip) == AGX_CS_GENERATION_AGX2; } /* Number of helper-table slots in the USC code-container lead block. G15 diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index 49772d7f0d66..39b47e8a0e2b 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -3694,7 +3694,7 @@ hk_dispatch_precomp(struct hk_cmd_buffer *cmd, struct agx_grid grid, hk_dispatch_with_usc_launch(dev, cs, prog->b.launch, agx_usc_addr(&dev->dev, t.gpu), grid, - prog->b.workgroup); + prog->b.workgroup, false); } uint64_t diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index 38941cb1eb5c..7a98386c9aa6 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -1093,10 +1093,14 @@ struct hk_cs { * fold the leading record into the destination's trailing one. */ void *leading_barrier_end; - /* AGX3 CDM: weak_barrier_end when that barrier ends an application - * dispatch. Where agx_chip_elide_app_dispatch_barrier() holds, a following - * application dispatch overwrites it, so consecutive application dispatches - * carry no barrier between them; any other record still follows it. */ + /* weak_barrier_end when that barrier ends an application dispatch. Where + * agx_chip_elide_app_dispatch_barrier() holds, a following application + * dispatch overwrites or rewinds it, so consecutive application + * dispatches carry no barrier between them; any other record still + * follows it. On AGX3 the barrier is the launch's weak tail record and the + * following launch overwrites it; on AGX2 it is the launch's trailing + * cache flush and the following launch rewinds over it + * (hk_cs_elide_dispatch_tail()). */ void *app_dispatch_end; /* Whether there is more than just the root chunk */ @@ -2562,15 +2566,31 @@ void hk_cdm_visibility_flush(struct hk_device *dev, struct hk_cs *cs, bool hk_cdm_visibility_flush_needed(const struct hk_device *dev, const struct hk_cs *cs, bool memory); +/* AGX2 CDM: rewind the trailing cache flush of an application dispatch when + * the next record would be another application dispatch and the chip elides + * inter-dispatch barriers. Exact only while the marker is the stream tail: + * any record in between — an application barrier, meta work, a stream link — + * moves current past it and keeps the flush. */ +static inline bool +hk_cs_elide_dispatch_tail(struct hk_cs *cs) +{ + if (!cs->app_dispatch_end || cs->current != cs->app_dispatch_end) + return false; + cs->current = (uint8_t *)cs->current - AGX_CDM_BARRIER_LENGTH; + cs->app_dispatch_end = NULL; + return true; +} + void hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs, struct agx_cdm_launch_word_0_packed launch, uint32_t usc, struct agx_grid grid, - struct agx_workgroup local_size); + struct agx_workgroup local_size, + bool app_dispatch); void hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs, struct agx_shader_info *info, uint32_t usc, struct agx_grid grid, - struct agx_workgroup local_size); + struct agx_workgroup local_size, bool app_dispatch); void hk_dispatch_m5_native(struct hk_cmd_buffer *cmd, struct hk_cs *cs, struct hk_shader *s, struct agx_grid grid, @@ -2668,7 +2688,11 @@ hk_dispatch_with_local_size(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint32_t usc = uploaded; hk_reserve_scratch(cmd, cs, s); - hk_dispatch_with_usc(dev, cs, &s->b.info, usc, grid, local_size); + /* Only an application compute dispatch may elide the previous one's + * trailing barrier. Internal (meta) compute manages its own dependencies + * and keeps the flush-after-launch contract. */ + hk_dispatch_with_usc(dev, cs, &s->b.info, usc, grid, local_size, + !cmd->in_meta && s->info.stage == MESA_SHADER_COMPUTE); } void hk_dispatch_precomp(struct hk_cmd_buffer *cmd, struct agx_grid grid, diff --git a/src/asahi/vulkan/hk_cmd_dispatch.c b/src/asahi/vulkan/hk_cmd_dispatch.c index e51e41fd222b..dd071052168b 100644 --- a/src/asahi/vulkan/hk_cmd_dispatch.c +++ b/src/asahi/vulkan/hk_cmd_dispatch.c @@ -671,7 +671,7 @@ void hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs, struct agx_cdm_launch_word_0_packed launch, uint32_t usc, struct agx_grid grid, - struct agx_workgroup wg) + struct agx_workgroup wg, bool app_dispatch) { switch (agx_chip_command_stream_generation(dev->dev.chip)) { case AGX_CS_GENERATION_AGX2: @@ -701,16 +701,27 @@ hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs, hk_ensure_cs_has_space(cs->cmd, cs, 0x2000 /* TODO */); cs->stats.cmds++; + + /* Two application dispatches in a row need no barrier between them, as on + * the native AGX3 path: rewind the previous launch's trailing flush, + * which nothing else has followed or strengthened, and let this launch's + * own flush end the pair instead. */ + if (app_dispatch && agx_chip_elide_app_dispatch_barrier(dev->dev.chip) && + hk_cs_elide_dispatch_tail(cs)) + cs->stats.flushes--; + cs->current = agx_cdm_launch(cs->current, dev->dev.chip, grid, wg, launch, usc); hk_cdm_cache_flush(dev, cs); + cs->app_dispatch_end = app_dispatch ? cs->current : NULL; } void hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs, struct agx_shader_info *info, uint32_t usc, - struct agx_grid grid, struct agx_workgroup local_size) + struct agx_grid grid, struct agx_workgroup local_size, + bool app_dispatch) { struct agx_cdm_launch_word_0_packed launch; agx_pack(&launch, CDM_LAUNCH_WORD_0, cfg) { @@ -721,7 +732,8 @@ hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs, cfg.preshader_register_count = info->nr_preamble_gprs; } - hk_dispatch_with_usc_launch(dev, cs, launch, usc, grid, local_size); + hk_dispatch_with_usc_launch(dev, cs, launch, usc, grid, local_size, + app_dispatch); } static bool diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 81c7c77c5d5e..71acd3d2ce2e 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -6044,7 +6044,7 @@ hk_launch_gs_prerast(struct hk_cmd_buffer *cmd, struct hk_cs *cs, vs->info.stage == MESA_SHADER_VERTEX ? gfx->linked[MESA_SHADER_VERTEX] : vs->only_linked), - grid_vs, wg); + grid_vs, wg, false); } /* Transform feedback and various queries require extra dispatching, @@ -6219,12 +6219,12 @@ hk_launch_tess(struct hk_cmd_buffer *cmd, struct hk_cs *cs, hk_dispatch_with_usc( dev, cs, &vs->b.info, hk_upload_usc_words(cmd, vs, gfx->linked[MESA_SHADER_VERTEX]), - grid_vs, agx_workgroup(64, 1, 1)); + grid_vs, agx_workgroup(64, 1, 1), false); hk_dispatch_with_usc( dev, cs, &tcs->b.info, hk_upload_usc_words(cmd, tcs, tcs->only_linked), grid_tcs, - agx_workgroup(tcs->info.tess.tcs_output_patch_size, 1, 1)); + agx_workgroup(tcs->info.tess.tcs_output_patch_size, 1, 1), false); } /* First generate counts, then prefix sum them, and then tessellate. */ diff --git a/src/asahi/vulkan/tests/test-hk-execution-allowance.c b/src/asahi/vulkan/tests/test-hk-execution-allowance.c index c528f9e42413..ed8efb4ed1d4 100644 --- a/src/asahi/vulkan/tests/test-hk-execution-allowance.c +++ b/src/asahi/vulkan/tests/test-hk-execution-allowance.c @@ -174,6 +174,39 @@ check_merge_memory_scope(void) assert(a.memory_flush_end == a.current); } +/* AGX2 CDM: where the chip elides the ordering barrier between application + * dispatches, a following application dispatch rewinds the previous launch's + * trailing flush so the pair carries one barrier. The tail marker only + * holds while nothing else recorded after it. */ +static void +check_elide_dispatch_tail(void) +{ + uint8_t bytes[HK_CS_SPACE_PADDING + 128] = {0}; + struct hk_cs cs = { + .type = HK_CS_CDM, .start = bytes, + .current = bytes + LAUNCH_BYTES + AGX_CDM_BARRIER_LENGTH, + .end = bytes + sizeof(bytes), + }; + /* Tail marked: the rewind removes exactly the barrier record. */ + cs.app_dispatch_end = cs.current; + assert(hk_cs_elide_dispatch_tail(&cs)); + assert(cs.current == bytes + LAUNCH_BYTES); + assert(cs.app_dispatch_end == NULL); + assert(!hk_cs_elide_dispatch_tail(&cs)); + + /* A record emitted after the tail moves current past the marker. */ + cs.current = bytes + LAUNCH_BYTES + AGX_CDM_BARRIER_LENGTH; + cs.app_dispatch_end = bytes + LAUNCH_BYTES; + assert(!hk_cs_elide_dispatch_tail(&cs)); + assert(cs.current == bytes + LAUNCH_BYTES + AGX_CDM_BARRIER_LENGTH); + assert(cs.app_dispatch_end == bytes + LAUNCH_BYTES); + + /* No marker: nothing to elide. */ + cs.app_dispatch_end = NULL; + assert(!hk_cs_elide_dispatch_tail(&cs)); + assert(cs.current == bytes + LAUNCH_BYTES + AGX_CDM_BARRIER_LENGTH); +} + static void check_as_contracts(void) { @@ -233,5 +266,6 @@ int main(void) for (unsigned link = 0; link < 2; ++link) check_merge(a, b, link); check_merge_memory_scope(); + check_elide_dispatch_tail(); return 0; } From 02e2350953bf19281fc30b40f543479567d3eba4 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:55:40 -0600 Subject: [PATCH 2/2] hk: keep the trailing flush on indirect dispatches on AGX2 Marcelo's review of the AGX2 trailing-flush elision: - agx_chip.h and hk_cs_elide_dispatch_tail() claimed an application barrier inserts a record that moves current past the marker. On AGX2 an application barrier ends the compute stream instead (hk_CmdPipelineBarrier2), so a dispatch after one always opens a fresh stream with no marker to rewind. Reword both, and note the AGX3 weak_barrier_end field is never set on AGX2. - vkCmdDispatchIndirect reached hk_dispatch_with_usc_launch() marked as an application dispatch, so its trailing flush could be rewound even though the documented contract keeps the barrier on indirect launches. Gate the marker and the rewind on !agx_is_indirect(grid), at both the AGX2 launch path and the AGX3 native compute path. Also add the suggested assert(cs->type == HK_CS_CDM) in hk_cs_elide_dispatch_tail(). Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- src/asahi/lib/agx_chip.h | 15 ++++++++------- src/asahi/vulkan/hk_cmd_buffer.h | 9 ++++++--- src/asahi/vulkan/hk_cmd_dispatch.c | 10 +++++++--- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/asahi/lib/agx_chip.h b/src/asahi/lib/agx_chip.h index 78e1693bd9e3..e79f23a7bccf 100644 --- a/src/asahi/lib/agx_chip.h +++ b/src/asahi/lib/agx_chip.h @@ -380,13 +380,14 @@ agx_chip_cdm_wide_visibility(enum agx_chip chip) /* Whether consecutive application dispatches of one CDM stream may omit the * ordering barrier between them. Vulkan orders dispatches only through - * explicit barriers, and Apple's G15 encoder emits none between independent - * dispatches; every other record (internal or indirect launch, cache flush, - * link, stream end) still follows a barrier. AGX2 chips take the same - * elision: their per-launch flush is the conservative record - * agx_cdm_barrier() documents ("set these after every launch to be safe"), - * and an application-requested dependency still emits its own barrier, - * which ends elision until the next pair of adjacent dispatches. */ + * explicit barriers; Apple's G15 encoder emits none between independent + * dispatches. AGX2 chips take the same elision: their per-launch flush is + * the conservative record agx_cdm_barrier() documents ("set these after + * every launch to be safe"), while an application-requested dependency ends + * the compute stream (hk_CmdPipelineBarrier2) rather than emitting a + * record, so a dispatch after one always opens a fresh stream with no + * marker to rewind. Internal and indirect launches, cache flushes, links + * and stream ends still carry or follow their barrier. */ static inline bool agx_chip_elide_app_dispatch_barrier(enum agx_chip chip) { diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index 7a98386c9aa6..7fe206519018 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -1093,7 +1093,8 @@ struct hk_cs { * fold the leading record into the destination's trailing one. */ void *leading_barrier_end; - /* weak_barrier_end when that barrier ends an application dispatch. Where + /* On AGX3, weak_barrier_end when that barrier ends an application + * dispatch; on AGX2 the launch's trailing cache-flush record end. Where * agx_chip_elide_app_dispatch_barrier() holds, a following application * dispatch overwrites or rewinds it, so consecutive application * dispatches carry no barrier between them; any other record still @@ -2569,11 +2570,13 @@ bool hk_cdm_visibility_flush_needed(const struct hk_device *dev, /* AGX2 CDM: rewind the trailing cache flush of an application dispatch when * the next record would be another application dispatch and the chip elides * inter-dispatch barriers. Exact only while the marker is the stream tail: - * any record in between — an application barrier, meta work, a stream link — - * moves current past it and keeps the flush. */ + * meta work or a stream link moves current past it and keeps the flush; an + * application barrier ends the compute stream (hk_CmdPipelineBarrier2), so + * a dispatch after one always opens a fresh stream with no marker. */ static inline bool hk_cs_elide_dispatch_tail(struct hk_cs *cs) { + assert(cs->type == HK_CS_CDM); if (!cs->app_dispatch_end || cs->current != cs->app_dispatch_end) return false; cs->current = (uint8_t *)cs->current - AGX_CDM_BARRIER_LENGTH; diff --git a/src/asahi/vulkan/hk_cmd_dispatch.c b/src/asahi/vulkan/hk_cmd_dispatch.c index dd071052168b..c2fda3b3bcb4 100644 --- a/src/asahi/vulkan/hk_cmd_dispatch.c +++ b/src/asahi/vulkan/hk_cmd_dispatch.c @@ -705,8 +705,11 @@ hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs, /* Two application dispatches in a row need no barrier between them, as on * the native AGX3 path: rewind the previous launch's trailing flush, * which nothing else has followed or strengthened, and let this launch's - * own flush end the pair instead. */ - if (app_dispatch && agx_chip_elide_app_dispatch_barrier(dev->dev.chip) && + * own flush end the pair instead. Indirect launches keep their barrier + * (agx_chip_elide_app_dispatch_barrier()), so they neither rewind the + * previous marker nor leave one of their own. */ + const bool app = app_dispatch && !agx_is_indirect(grid); + if (app && agx_chip_elide_app_dispatch_barrier(dev->dev.chip) && hk_cs_elide_dispatch_tail(cs)) cs->stats.flushes--; @@ -714,7 +717,7 @@ hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs, agx_cdm_launch(cs->current, dev->dev.chip, grid, wg, launch, usc); hk_cdm_cache_flush(dev, cs); - cs->app_dispatch_end = app_dispatch ? cs->current : NULL; + cs->app_dispatch_end = app ? cs->current : NULL; } void @@ -2664,6 +2667,7 @@ hk_dispatch_g17p_native(struct hk_cmd_buffer *cmd, struct hk_cs *cs, * strengthened, and let this launch's own tail end the stream instead. */ const bool app_dispatch = !cmd->in_meta && s && s->info.stage == MESA_SHADER_COMPUTE && + !agx_is_indirect(grid) && agx_chip_elide_app_dispatch_barrier(dev->dev.chip); if (app_dispatch && cs->app_dispatch_end && cs->current == cs->app_dispatch_end &&