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
61 changes: 47 additions & 14 deletions src/asahi/vulkan/hk_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ brl_sync_transition_kinds(const VkDependencyInfo *info,

static struct hk_cs *previous_on_sq(struct hk_cmd_buffer *cmd,
struct hk_cs *cs);
static struct hk_cs *previous_on_sq_kinds(struct hk_cmd_buffer *cmd,
struct hk_cs *cs, uint8_t kinds);

void
hk_sync_src_dep(struct hk_cmd_buffer *cmd, uint8_t src_kinds,
Expand Down Expand Up @@ -1332,42 +1334,57 @@ hk_sync_stream_created(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
}
}

/* Whether the compute stream cs already waits for every compute stream before
* it: for the one before it, which covers the earlier ones as a prefix. */
/* Whether the compute stream cs already waits for every earlier compute stream
* matching kinds: for the latest one before it, which covers the rest as a
* prefix. When kinds is HK_SYNC_STREAM_KINDS, every earlier compute stream
* counts; application requirements (HK_SYNC_ALL) ignore pure query-memory
* streams (HK_SYNC_STAMP, HK_SYNC_QRESET), which no application dependency
* names. */
static bool
cdm_waits_for_prefix(struct hk_cmd_buffer *cmd, struct hk_cs *cs)
cdm_waits_for_prefix(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
uint8_t kinds)
{
const struct hk_cs *prev = previous_on_sq(cmd, cs);
const struct hk_cs *prev =
kinds == HK_SYNC_STREAM_KINDS ? previous_on_sq(cmd, cs)
: previous_on_sq_kinds(cmd, cs, kinds);
const struct hk_cs *cut = cs->sync.dep[HK_SYNC_CDM].cut;
return !prev || (cut && cut != cs && cut->sync.seq >= prev->sync.seq);
}

/* Whether work recorded into the open compute stream cs meets the requirement
* dep: every stream dep names must precede cs. A cut at cs itself names the
* compute streams before cs as well as the work inside it. The stream orders
* only its own work, and the kernel may run earlier compute streams of the
* queue on other firmware channels (DRM_ASAHI_FEATURE_COMPUTE_MULTI_CHANNEL),
* so that cut is met only if cs already waits for those streams. */
* earlier compute streams of prefix_kinds as well as the work inside cs. The
* stream orders only its own work, and the kernel may run earlier compute
* streams of the queue on other firmware channels
* (DRM_ASAHI_FEATURE_COMPUTE_MULTI_CHANNEL), so that cut is met only if cs
* already waits for those streams. */
static bool
cs_meets(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
const struct hk_sync_dep dep[HK_SYNC_NUM_SQ])
const struct hk_sync_dep dep[HK_SYNC_NUM_SQ], uint8_t prefix_kinds)
{
for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) {
if (dep[sq].cut && dep[sq].cut->sync.seq > cs->sync.seq)
return false;
}

return dep[HK_SYNC_CDM].cut != cs || cdm_waits_for_prefix(cmd, cs);
return dep[HK_SYNC_CDM].cut != cs ||
cdm_waits_for_prefix(cmd, cs, prefix_kinds);
}

/* Whether the open general compute stream may receive work of the given
* kinds (cs_meets()). */
* kinds (cs_meets()). Recorded requirements (cmd->sync.req) come from
* application dependencies and never name pure query-memory streams
* (HK_SYNC_STAMP, HK_SYNC_QRESET), so application work only needs the open
* stream to cover earlier HK_SYNC_ALL compute streams. */
static bool
cs_reusable(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t kinds)
{
struct hk_sync_dep dep[HK_SYNC_NUM_SQ];
req_for_kinds(cmd, kinds, dep);
return cs_meets(cmd, cs, dep);
const uint8_t prefix_kinds =
(kinds & (HK_SYNC_STAMP | HK_SYNC_QRESET)) ? HK_SYNC_STREAM_KINDS
: HK_SYNC_ALL;
return cs_meets(cmd, cs, dep, prefix_kinds);
}

/* Whether the compute stream cs already waits for the render r: directly,
Expand Down Expand Up @@ -1577,13 +1594,14 @@ hk_sync_get_cs_after(struct hk_cmd_buffer *cmd,
for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq)
dep_max(&full[sq], &dep[sq]);

if (cs && !cmd->sync.pin_cs && !cs_meets(cmd, cs, full))
if (cs && !cmd->sync.pin_cs && !cs_meets(cmd, cs, full, HK_SYNC_STREAM_KINDS))
hk_cmd_buffer_end_compute(cmd);

cs = hk_cmd_buffer_get_cs(cmd, true);
if (cs) {
/* A pinned stream stays open: it waits for the streams before it. */
if (full[HK_SYNC_CDM].cut == cs && !cdm_waits_for_prefix(cmd, cs))
if (full[HK_SYNC_CDM].cut == cs &&
!cdm_waits_for_prefix(cmd, cs, HK_SYNC_STREAM_KINDS))
full[HK_SYNC_CDM].cut = previous_on_sq(cmd, cs);
hk_sync_stream_wait(cs, full);
}
Expand Down Expand Up @@ -1760,6 +1778,21 @@ previous_on_sq(struct hk_cmd_buffer *cmd, struct hk_cs *cs)
return NULL;
}

/* The latest stream of any of the given kinds on the same subqueue that
* precedes cs in the list. */
static struct hk_cs *
previous_on_sq_kinds(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
uint8_t kinds)
{
for (struct list_head *n = cs->node.prev; n != &cmd->control_streams;
n = n->prev) {
struct hk_cs *it = list_entry(n, struct hk_cs, node);
if (it->type == cs->type && (it->sync.kind & kinds))
return it;
}
return NULL;
}

static void
replace_pointer(struct hk_cs **slot, struct hk_cs *from, struct hk_cs *to)
{
Expand Down
105 changes: 105 additions & 0 deletions src/asahi/vulkan/tests/test-hk-sync-prior.c
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,111 @@ test_compute_self_cut(void)
: !flush && (cmd->sync.host_flush_kinds &
HK_SYNC_COMPUTE));
}

/* A pure query-memory predecessor (HK_SYNC_STAMP or HK_SYNC_QRESET)
* is never named by an application barrier: c2 stays open across a
* compute barrier when no earlier HK_SYNC_ALL compute stream sits
* behind it. */
const uint8_t non_app[] = {HK_SYNC_STAMP, HK_SYNC_QRESET};
for (unsigned k = 0; k < ARRAY_SIZE(non_app); ++k) {
struct hk_cmd_buffer *cmd = timestamp_cmd(chips[i], exact);
cmd->sync.kind_override = non_app[k];
struct hk_cs *s0 = add(cmd, true);
cmd->sync.kind_override = 0;
s0->stats.cmds = 1;
struct hk_cs *c2 = add(cmd, true);
cmd->current_cs.cs = c2;
c2->stats.cmds = 1;
hk_sync_barrier(cmd, HK_SYNC_COMPUTE, HK_SYNC_COMPUTE);
hk_sync_check_cs(cmd);
T("self cut: pure query-memory predecessor keeps c2 open",
cmd->current_cs.cs == c2 &&
c2->sync.dep[HK_SYNC_CDM].cut == NULL);
}

/* An earlier unwaited application compute stream behind a pure
* HK_SYNC_STAMP stream still forces c2 to end across a barrier. */
{
struct hk_cmd_buffer *cmd = timestamp_cmd(chips[i], exact);
struct hk_cs *c0 = add(cmd, true);
c0->stats.cmds = 1;
cmd->sync.kind_override = HK_SYNC_STAMP;
struct hk_cs *s1 = add(cmd, true);
cmd->sync.kind_override = 0;
s1->stats.cmds = 1;
struct hk_cs *c2 = add(cmd, true);
cmd->current_cs.cs = c2;
c2->stats.cmds = 1;
hk_sync_barrier(cmd, HK_SYNC_COMPUTE, HK_SYNC_COMPUTE);
hk_sync_check_cs(cmd);
T("self cut: unwaited compute behind stamp stream still ends c2",
cmd->current_cs.cs == NULL &&
c2->sync.dep[HK_SYNC_CDM].cut == NULL);
}

/* Once c2 already waits for the earlier application compute stream
* c0, an intervening pure HK_SYNC_STAMP stream s1 does not split c2
* across a compute barrier. */
{
struct hk_cmd_buffer *cmd = timestamp_cmd(chips[i], exact);
struct hk_cs *c0 = add(cmd, true);
c0->stats.cmds = 1;
hk_sync_barrier(cmd, HK_SYNC_COMPUTE, HK_SYNC_COMPUTE);
cmd->sync.kind_override = HK_SYNC_STAMP;
struct hk_cs *s1 = add(cmd, true);
cmd->sync.kind_override = 0;
s1->stats.cmds = 1;
struct hk_cs *c2 = add(cmd, true);
cmd->current_cs.cs = c2;
c2->stats.cmds = 1;
T("self cut: c2 waits on c0 and skips intervening stamp stream",
c2->sync.dep[HK_SYNC_CDM].cut == c0);
hk_sync_barrier(cmd, HK_SYNC_COMPUTE, HK_SYNC_COMPUTE);
hk_sync_check_cs(cmd);
T("self cut: intervening stamp stream keeps waited c2 open",
cmd->current_cs.cs == c2 &&
c2->sync.dep[HK_SYNC_CDM].cut == c0);
}

/* A query/event caller through hk_sync_get_cs_after() with a
* dependency on an earlier HK_SYNC_STAMP stream must still end c2
* (or make a pinned c2 wait on the stamp stream) when a compute
* barrier produced a self cut at c2. */
{
struct hk_cmd_buffer *cmd = timestamp_cmd(chips[i], exact);
cmd->sync.kind_override = HK_SYNC_STAMP;
struct hk_cs *s0 = add(cmd, true);
cmd->sync.kind_override = 0;
s0->stats.cmds = 1;
struct hk_cs *c2 = add(cmd, true);
cmd->current_cs.cs = c2;
c2->stats.cmds = 1;
hk_sync_barrier(cmd, HK_SYNC_COMPUTE, HK_SYNC_COMPUTE);
struct hk_sync_dep qdep[HK_SYNC_NUM_SQ];
hk_sync_src_dep(cmd, HK_SYNC_QUERY_SCOPE, c2, qdep);
cmd->vk.record_result = VK_ERROR_OUT_OF_HOST_MEMORY;
hk_sync_get_cs_after(cmd, qdep);
T("self cut: query scope after stamp stream still ends c2",
cmd->current_cs.cs == NULL &&
c2->sync.dep[HK_SYNC_CDM].cut == NULL);
}
{
struct hk_cmd_buffer *cmd = timestamp_cmd(chips[i], exact);
cmd->sync.kind_override = HK_SYNC_STAMP;
struct hk_cs *s0 = add(cmd, true);
cmd->sync.kind_override = 0;
s0->stats.cmds = 1;
struct hk_cs *c2 = add(cmd, true);
cmd->current_cs.cs = c2;
c2->stats.cmds = 1;
hk_sync_barrier(cmd, HK_SYNC_COMPUTE, HK_SYNC_COMPUTE);
struct hk_sync_dep qdep[HK_SYNC_NUM_SQ];
hk_sync_src_dep(cmd, HK_SYNC_QUERY_SCOPE, c2, qdep);
cmd->sync.pin_cs = true;
T("self cut: pinned query scope after stamp stream waits on s0",
hk_sync_get_cs_after(cmd, qdep) == c2 &&
c2->sync.dep[HK_SYNC_CDM].cut == s0);
}
}
}
}
Expand Down