Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/asahi/lib/agx_chip.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/asahi/vulkan/hk_cmd_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 34 additions & 7 deletions src/asahi/vulkan/hk_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: on AGX2 "an application barrier" doesn't move current. It ends the stream, and the next dispatch opens a fresh one with no marker.

— Marcelo's agent

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The comment now reads: meta work or a stream link moves current past the marker 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. Also added assert(cs->type == HK_CS_CDM) as suggested. In 02e2350.


/* 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,
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also marks indirect application dispatches (hk_cmd_dispatch.c:2969), which the commit message and agx_chip.h:384-385 say keep their barrier. && !agx_is_indirect(grid) in hk_dispatch_with_usc_launch() would match.

— Marcelo's agent

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. hk_dispatch_with_usc_launch() now computes the marker as app_dispatch && !agx_is_indirect(grid), so an indirect launch neither rewinds the previous marker nor leaves one of its own — its trailing flush always stands. Applied the same gate to the AGX3 native compute path's app_dispatch (hk_cmd_dispatch.c:2668), where an indirect grid was marked the same way. In 02e2350.

}

void hk_dispatch_precomp(struct hk_cmd_buffer *cmd, struct agx_grid grid,
Expand Down
22 changes: 19 additions & 3 deletions src/asahi/vulkan/hk_cmd_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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) &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: harmless, but this doesn't change anything: the indirect branch returns at :2629 before app_dispatch is computed. Fine to keep as defensive.

— Marcelo's agent

agx_chip_elide_app_dispatch_barrier(dev->dev.chip);
if (app_dispatch && cs->app_dispatch_end &&
cs->current == cs->app_dispatch_end &&
Expand Down
6 changes: 3 additions & 3 deletions src/asahi/vulkan/hk_cmd_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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. */
Expand Down
34 changes: 34 additions & 0 deletions src/asahi/vulkan/tests/test-hk-execution-allowance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Comment on lines +184 to +195

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Launch eligibility stays untested

check_elide_dispatch_tail() only tests manually assigned pointers. It never records dispatches through hk_dispatch_with_usc_launch(), so it would still pass if application dispatches stopped removing flushes or internal dispatches started removing them. Add a CPU-only test of the launch path that checks the recorded barrier bytes and stats.flushes for application/application, application/internal, and internal/application pairs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged — not in this commit. The three code points are fixed at 02e2350; this test still only moves pointers, so it covers the marker invariant but not the launch path. A CPU test that records real streams through hk_dispatch_with_usc_launch() (app to app, app to internal, direct and indirect, chunk link, dispatch-barrier-dispatch; checking recorded barrier bytes and stats.flushes) needs a canned usc/program pair, which I'm landing as a follow-up rather than holding the fix on the harness.


/* 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)
{
Expand Down Expand Up @@ -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;
}
Loading