diff --git a/src/asahi/lib/agx_chip.h b/src/asahi/lib/agx_chip.h index d22b2b0be4c5..e79f23a7bccf 100644 --- a/src/asahi/lib/agx_chip.h +++ b/src/asahi/lib/agx_chip.h @@ -380,13 +380,19 @@ 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. */ + * 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) { - 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..7fe206519018 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -1093,10 +1093,15 @@ 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. */ + /* 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 + * 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 +2567,33 @@ 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: + * 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; + 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 +2691,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..c2fda3b3bcb4 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,30 @@ 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. 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--; + 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 ? 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 +735,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 @@ -2652,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 && 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; }