Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/asahi/lib/agx_chip.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ agx_chip_command_stream_generation(enum agx_chip chip)
static inline uint32_t
agx2_cdm_barrier_default_word(enum agx_chip chip, uint32_t gpu_generation)
{
if (chip == AGX_CHIP_G13X ||
if (chip == AGX_CHIP_G13G || chip == AGX_CHIP_G13X ||
(chip == AGX_CHIP_G14X && gpu_generation == 14))
return 0x1ffu;
return AGX_CDM_BARRIER_FULL_WORD;
Expand Down
10 changes: 9 additions & 1 deletion src/asahi/lib/agx_chip_caps.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ enum agx_chip_cap {
* Measured on G13G and G13X; other generations keep the unfolded
* addresses until they are measured. */
AGX_CAP_FOLD_SHARED_OFFSETS = 1u << 24,

/* AGX2 compute streams defer the post-launch CDM cache flush until the
* next launch sees a declared barrier, a non-deferred/meta launch runs,
* or the compute stream closes, avoiding redundant 0x1ff barriers between
* independent dispatches. Measured on G13G (+2.16% to +3.33% LLM decode
* combined with 0x1ff/0x160; +1.16% to +1.25% incremental over 0x1ff/0x160,
* bit-identical output, 0 regressions on synchronization/memory_model CTS). */
AGX_CAP_CDM_DEFER_FLUSH = 1u << 25,
};

/* What the admitted AGX2 chips (G13G, G13X, G14G, G14X) share. */
Expand Down Expand Up @@ -224,7 +232,7 @@ enum agx_chip_cap {
* predicates do.
*/
#define AGX_CHIP_CAPS(X, ...) \
X(G13G, AGX_CAPS_G13, __VA_ARGS__) \
X(G13G, AGX_CAPS_G13 | AGX_CAP_CDM_DEFER_FLUSH, __VA_ARGS__) \
X(G13X, AGX_CAPS_G13, __VA_ARGS__) \
X(G14G, AGX_CAPS_AGX2, __VA_ARGS__) \
X(G14X, AGX_CAPS_AGX2, __VA_ARGS__) \
Expand Down
3 changes: 2 additions & 1 deletion src/asahi/lib/tests/test-agx-cdm-barrier-word.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ main(void)
{
/* The reduced word 0x1ff on the dies it was measured on: the G13X family
* (the gen-13 S/C/D parts fold to it) and G14X at GPU generation 14. */
assert(agx2_cdm_barrier_default_word(AGX_CHIP_G13G, 13) == 0x1ffu);
assert(agx2_cdm_barrier_default_word(AGX_CHIP_G13X, 13) == 0x1ffu);
assert(agx2_cdm_barrier_default_word(AGX_CHIP_G14X, 14) == 0x1ffu);

/* The full word everywhere else: the gen-13/14 base parts and every
* later chip, including a hypothetical other-generation G14X. */
const enum agx_chip full_chips[] = {
AGX_CHIP_G13G, AGX_CHIP_G14G,
AGX_CHIP_G14G,
AGX_CHIP_G15P, AGX_CHIP_G15G, AGX_CHIP_G15S, AGX_CHIP_G15C,
AGX_CHIP_G15D,
AGX_CHIP_G16P, AGX_CHIP_G16G, AGX_CHIP_G16S, AGX_CHIP_G16C,
Expand Down
14 changes: 13 additions & 1 deletion src/asahi/lib/tests/test-chip-caps.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static const unsigned all_caps =
AGX_CAP_HAL200_LOSSLESS_COMPRESSION | AGX_CAP_HAL200_MSAA_COLOR_COMPRESSION |
AGX_CAP_SIGNED_ZERO_MINMAX_SELECT | AGX_CAP_CDM_OVERLAP_SPLIT |
AGX_CAP_CACHED_CMD_MEMORY | AGX_CAP_COMPUTE_SKIP_PRESSURE_SCHED |
AGX_CAP_FOLD_SHARED_OFFSETS;
AGX_CAP_FOLD_SHARED_OFFSETS | AGX_CAP_CDM_DEFER_FLUSH;
#undef CAP_BIT

/* Each chip of AGX_CHIPS has exactly one row, and the rows are in its
Expand Down Expand Up @@ -283,6 +283,17 @@ test_g13_compute_codegen_caps(void)
}
}

/* AGX_CAP_CDM_DEFER_FLUSH is enabled on the measured G13G row only. */
static void
test_cdm_defer_flush(void)
{
for (unsigned i = 0; i <= AGX_CHIP_UNKNOWN; ++i) {
const enum agx_chip c = (enum agx_chip)i;
const bool expected = c == AGX_CHIP_G13G;
CHECK_CHIP(agx_chip_has_cap(c, AGX_CAP_CDM_DEFER_FLUSH) == expected, c);
}
}

int
main(void)
{
Expand All @@ -300,6 +311,7 @@ main(void)
test_hal200_msaa_color_compression();
test_signed_zero_minmax_select();
test_g13_compute_codegen_caps();
test_cdm_defer_flush();

if (failures) {
fprintf(stderr, "agx_chip_caps: %u of %u checks failed\n", failures,
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 @@ -5438,7 +5438,7 @@ hk_dispatch_precomp(struct hk_cmd_buffer *cmd, struct agx_grid grid,
cmd->state.cs.cdm_stream_has_images = true;
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
6 changes: 5 additions & 1 deletion src/asahi/vulkan/hk_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,9 @@ struct hk_cs {
* flush requested with nothing recorded since it can be elided. */
void *strong_flush_end;

/* AGX2 CDM (AGX_CAP_CDM_DEFER_FLUSH): post-launch flush deferred. */
bool cdm_flush_pending;

/* Likewise for the weak ordering barrier attached to a native launch. A
* later cache-flush request upgrades this record in place instead of
* emitting two adjacent barriers. */
Expand Down Expand Up @@ -3869,7 +3872,8 @@ bool hk_cdm_visibility_flush_needed(const struct hk_device *dev,
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 allow_defer);

void hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs,
struct agx_shader_info *info, uint32_t usc,
Expand Down
34 changes: 24 additions & 10 deletions src/asahi/vulkan/hk_cmd_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ hk_cdm_flush_impl(struct hk_device *dev, struct hk_cs *cs, bool memory,

switch (agx_chip_command_stream_generation(dev->dev.chip)) {
case AGX_CS_GENERATION_AGX2:
cs->cdm_flush_pending = false;
if (cs->current == cs->strong_flush_end)
return;

Expand Down Expand Up @@ -808,15 +809,20 @@ hk_cdm_end_stream_flush(struct hk_cmd_buffer *cmd, struct hk_cs *cs)
* funnels here, so no caller can end the compute stream with a declared
* dependency left unemitted. This also keeps the drain out of
* hk_cmd_buffer.h: standalone test binaries stub this function. */
const bool flush_pending = cs->cdm_flush_pending;
bool barrier_pending = false;
if (cs == cmd->current_cs.cs) {
cmd->state.cs.cdm_stream_has_images = false;
if (cmd->state.cs.cdm_barrier_pending) {
cmd->state.cs.cdm_barrier_pending = false;
cmd->state.cs.cdm_barrier_full_needed = false;
hk_ensure_cs_has_space(cmd, cs, AGX_CDM_BARRIER_LENGTH);
hk_cdm_cache_flush(dev, cs);
barrier_pending = true;
}
}
if (barrier_pending || flush_pending) {
hk_ensure_cs_has_space(cmd, cs, AGX_CDM_BARRIER_LENGTH);
hk_cdm_cache_flush(dev, cs);
}


/* A dependency recorded while no compute stream holding work is open
Expand Down Expand Up @@ -902,7 +908,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 allow_defer)
{
switch (agx_chip_command_stream_generation(dev->dev.chip)) {
case AGX_CS_GENERATION_AGX2:
Expand Down Expand Up @@ -930,13 +936,13 @@ hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs,
return;
}

const bool defer =
allow_defer && cs == cs->cmd->current_cs.cs && !cs->cmd->in_meta &&
!HK_PERF(dev, FULLCDMBARRIER) &&
agx_chip_has_cap(dev->dev.chip, AGX_CAP_CDM_DEFER_FLUSH);

if (cs == cs->cmd->current_cs.cs && !HK_PERF(dev, FULLCDMBARRIER) &&
cs->cmd->state.cs.cdm_barrier_pending) {
/* The app declared a memory dependency since the last launch. Because
* every launch ends with the conservative default barrier word, a pure
* compute-to-compute SSBO boundary narrows that preceding trailing
* barrier in place to 0x160 before any chunk switch; every other
* boundary keeps the default word. */
const bool pure_ssbo =
!cs->cmd->state.cs.cdm_barrier_full_needed && cs->stats.cmds > 0;
cs->cmd->state.cs.cdm_barrier_pending = false;
Expand All @@ -947,6 +953,9 @@ hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs,
hk_ensure_cs_has_space(cs->cmd, cs, AGX_CDM_BARRIER_LENGTH);
hk_cdm_cache_flush(dev, cs);
}
} else if (!defer && cs->cdm_flush_pending) {
hk_ensure_cs_has_space(cs->cmd, cs, AGX_CDM_BARRIER_LENGTH);
hk_cdm_cache_flush(dev, cs);
}

hk_ensure_cs_has_space(cs->cmd, cs, 0x2000 /* TODO */);
Expand All @@ -955,7 +964,11 @@ hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs,
cs->current =
agx_cdm_launch(cs->current, dev->dev.chip, grid, wg, launch, usc);

hk_cdm_cache_flush(dev, cs);
if (defer) {
cs->cdm_flush_pending = true;
} else {
hk_cdm_cache_flush(dev, cs);
}
}

void
Expand All @@ -978,7 +991,8 @@ hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs,
cs->cmd->state.cs.cdm_barrier_full_needed = true;
cs->cmd->state.cs.cdm_stream_has_images = true;
}
hk_dispatch_with_usc_launch(dev, cs, launch, usc, grid, local_size);
hk_dispatch_with_usc_launch(dev, cs, launch, usc, grid, local_size,
info->stage == MESA_SHADER_COMPUTE);
}

static bool
Expand Down
137 changes: 137 additions & 0 deletions src/asahi/vulkan/tests/test-hk-cdm-barrier-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ emit_launch(struct fixture *f, struct agx_shader_info *info)
return (uint32_t *)f->cs.current - 1;
}

static uint32_t *
emit_precomp_launch(struct fixture *f)
{
struct agx_cdm_launch_word_0_packed launch = {0};
f->cmd.state.cs.cdm_barrier_full_needed = true;
f->cmd.state.cs.cdm_stream_has_images = true;
hk_dispatch_with_usc_launch(&f->dev, &f->cs, launch, 0x1000, agx_1d(64),
agx_workgroup(64, 1, 1), false);
return (uint32_t *)f->cs.current - 1;
}

/* Pure SSBO barrier between two AGX2 launches narrows the barrier word between
* them in place to 0x160 while leaving the second launch's tail at 0x1ff, and
* late submit-time sparse patching still upgrades both records to 0xfffff. */
Expand Down Expand Up @@ -346,6 +357,131 @@ test_stream_open_limits(void)
fixture_fini(&f);
}

static void
test_defer_flush_g13g(void)
{
struct fixture f;
struct agx_shader_info pure = make_compute_shader_info(false);
VkMemoryBarrier2 mb_ssbo, mb_broad;
VkDependencyInfo dep_ssbo = make_ssbo_dep(&mb_ssbo);
VkDependencyInfo dep_broad = make_broad_dep(&mb_broad);

/* 1. Two independent launches: zero barriers between them, 0x1ff at close. */
fixture_init(&f, AGX_CHIP_G13G, 13, 0);
emit_launch(&f, &pure);
assert(f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 0);
size_t len1 = (uint8_t *)f.cs.current - f.stream;
emit_launch(&f, &pure);
assert(f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 0);
assert((size_t)((uint8_t *)f.cs.current - f.stream) == 2 * len1);
hk_cdm_end_stream_flush(&f.cmd, &f.cs);
assert(!f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 1);
uint32_t *end_w = (uint32_t *)f.cs.current - 1;
assert(*end_w == barrier_record(0x1ffu));
assert(sparse_site_recorded(&f.cmd, end_w));
fixture_fini(&f);

/* 2. Pure SSBO barrier between two launches emits 0x160; stream close emits
* 0x1ff; late sparse patching upgrades both to 0xfffff. */
fixture_init(&f, AGX_CHIP_G13G, 13, 0);
emit_launch(&f, &pure);
void *after_l1 = f.cs.current;
assert(!hk_cdm_record_pipeline_barrier(&f.cmd, &dep_ssbo));
emit_launch(&f, &pure);
uint32_t *mid_w = (uint32_t *)after_l1;
assert(*mid_w == barrier_record(0x160u));
assert(sparse_site_recorded(&f.cmd, mid_w));
assert(f.cs.cdm_flush_pending);
hk_cdm_end_stream_flush(&f.cmd, &f.cs);
assert(!f.cs.cdm_flush_pending);
uint32_t *tail_w = (uint32_t *)f.cs.current - 1;
assert(*tail_w == barrier_record(0x1ffu));
assert(sparse_site_recorded(&f.cmd, tail_w));
p_atomic_set(&f.dev.sparse_used, 1);
struct vk_command_buffer *cmds[] = {&f.cmd.vk};
hk_submit_patch_sparse_sites(&f.dev, cmds, 1);
assert(*mid_w == barrier_record(AGX_CDM_BARRIER_FULL_WORD));
assert(*tail_w == barrier_record(AGX_CDM_BARRIER_FULL_WORD));
fixture_fini(&f);

/* 3. Broad barrier between two launches emits 0x1ff. */
fixture_init(&f, AGX_CHIP_G13G, 13, 0);
emit_launch(&f, &pure);
after_l1 = f.cs.current;
assert(!hk_cdm_record_pipeline_barrier(&f.cmd, &dep_broad));
emit_launch(&f, &pure);
mid_w = (uint32_t *)after_l1;
assert(*mid_w == barrier_record(0x1ffu));
assert(f.cs.cdm_flush_pending);
fixture_fini(&f);

/* 4. Deferred launch followed by in_meta launch flushes 0x1ff before meta
* and meta emits trailing 0x1ff. */
fixture_init(&f, AGX_CHIP_G13G, 13, 0);
emit_launch(&f, &pure);
after_l1 = f.cs.current;
f.cmd.in_meta = true;
uint32_t *meta_tail = emit_launch(&f, &pure);
f.cmd.in_meta = false;
mid_w = (uint32_t *)after_l1;
assert(*mid_w == barrier_record(0x1ffu));
assert(*meta_tail == barrier_record(0x1ffu));
assert(!f.cs.cdm_flush_pending);
fixture_fini(&f);

/* 5. Internal precomp launches (e.g. query reset -> query copy on the same
* stream) never defer: each emits its own trailing 0x1ff flush. */
fixture_init(&f, AGX_CHIP_G13G, 13, 0);
uint32_t *p1_tail = emit_precomp_launch(&f);
assert(*p1_tail == barrier_record(0x1ffu));
assert(!f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 1);
uint32_t *p2_tail = emit_precomp_launch(&f);
assert(*p2_tail == barrier_record(0x1ffu));
assert(!f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 2);
fixture_fini(&f);

/* 6. Deferred app dispatch followed by precomp drains 0x1ff before precomp
* and emits trailing 0x1ff after precomp. */
fixture_init(&f, AGX_CHIP_G13G, 13, 0);
emit_launch(&f, &pure);
assert(f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 0);
after_l1 = f.cs.current;
p1_tail = emit_precomp_launch(&f);
mid_w = (uint32_t *)after_l1;
assert(*mid_w == barrier_record(0x1ffu));
assert(*p1_tail == barrier_record(0x1ffu));
assert(!f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 2);
fixture_fini(&f);

/* 7. Deferred app dispatch -> pure SSBO barrier -> precomp must upgrade the
* pending barrier to full 0x1ff (not 0x160) and flush after precomp. */
fixture_init(&f, AGX_CHIP_G13G, 13, 0);
emit_launch(&f, &pure);
after_l1 = f.cs.current;
assert(!hk_cdm_record_pipeline_barrier(&f.cmd, &dep_ssbo));
p1_tail = emit_precomp_launch(&f);
mid_w = (uint32_t *)after_l1;
assert(*mid_w == barrier_record(0x1ffu));
assert(*p1_tail == barrier_record(0x1ffu));
assert(!f.cs.cdm_flush_pending);
assert(f.cs.stats.flushes == 2);
fixture_fini(&f);

/* 8. fullcdmbarrier disables deferral and emits 0xfffff per launch. */
fixture_init(&f, AGX_CHIP_G13G, 13, HK_PERF_FULLCDMBARRIER);
uint32_t *b1 = emit_launch(&f, &pure);
assert(*b1 == barrier_record(AGX_CDM_BARRIER_FULL_WORD));
assert(!f.cs.cdm_flush_pending);
fixture_fini(&f);
}

int
main(void)
{
Expand All @@ -355,6 +491,7 @@ main(void)
test_conservative_cases_keep_default_word();
test_full_word_and_overrides();
test_stream_open_limits();
test_defer_flush_g13g();
printf("hk_cdm_barrier_stream: all checks passed\n");
return 0;
}