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
7 changes: 3 additions & 4 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2450,11 +2450,10 @@ agx_emit_load(agx_builder *b, agx_index dest, nir_intrinsic_instr *instr)
load->memory_byte_offset = byte_offset;
load->memory_is_packed = packed_count != 0;
/* nir_opt_access proved nothing writes this memory and it is not
* volatile, so the read is a function of its address alone. The AGX3
* spiller may then repeat it in place of filling the value from a spill
* slot. */
* volatile, so the read is a function of its address alone. On AGX3 the
* spiller may repeat it in place of filling a spill slot; on AGX2 wait
* insertion may keep it in flight across a threadgroup barrier. */
load->memory_can_reorder =
agx_isa_is_agx3(b->shader->key->dev.encoding) &&
nir_intrinsic_can_reorder(instr) && !nir_is_coherent(instr);
agx_emit_cached_split(b, dest, instr->def.num_components);
}
Expand Down
21 changes: 18 additions & 3 deletions src/asahi/compiler/agx_insert_waits.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ struct slot {
* AGX_MAX_PENDING for correct results.
*/
uint8_t nr_pending;

/* Whether a threadgroup barrier must wait for this slot. Read-only
* non-aliasing device loads write only private registers and cannot race
* with threadgroup memory, so they may stay in flight across a
* threadgroup_barrier until their destination registers are consumed (or a
* memory_barrier/block exit is reached).
*/
bool drain_on_tg_barrier;
};

/*
Expand Down Expand Up @@ -84,9 +92,12 @@ agx_insert_waits_local(agx_context *ctx, agx_block *block)
}

/* Check for barriers */
if (I->op == AGX_OPCODE_THREADGROUP_BARRIER ||
I->op == AGX_OPCODE_MEMORY_BARRIER) {

if (I->op == AGX_OPCODE_THREADGROUP_BARRIER) {
for (unsigned slot = 0; slot < ARRAY_SIZE(slots); ++slot) {
if (slots[slot].nr_pending && slots[slot].drain_on_tg_barrier)
wait_mask |= BITSET_BIT(slot);
}
} else if (I->op == AGX_OPCODE_MEMORY_BARRIER) {
for (unsigned slot = 0; slot < ARRAY_SIZE(slots); ++slot) {
if (slots[slot].nr_pending)
wait_mask |= BITSET_BIT(slot);
Expand Down Expand Up @@ -115,6 +126,7 @@ agx_insert_waits_local(agx_context *ctx, agx_block *block)

BITSET_ZERO(slots[slot].writes);
slots[slot].nr_pending = 0;
slots[slot].drain_on_tg_barrier = false;
}

/* Record access */
Expand All @@ -128,6 +140,9 @@ agx_insert_waits_local(agx_context *ctx, agx_block *block)
agx_index_size_16(I->dest[d]));
}

if (I->op != AGX_OPCODE_DEVICE_LOAD || !I->memory_can_reorder)
slots[I->scoreboard].drain_on_tg_barrier = true;

slots[I->scoreboard].nr_pending++;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/asahi/compiler/agx_spill.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ remat_load_elem_bytes(const agx_instr *L)
static bool
can_remat_load(const agx_context *ctx, const agx_instr *L)
{
if (L->op != AGX_OPCODE_DEVICE_LOAD || !L->memory_can_reorder ||
if (!ctx->key || !agx_isa_is_agx3(ctx->key->dev.encoding) ||
L->op != AGX_OPCODE_DEVICE_LOAD || !L->memory_can_reorder ||
L->memory_is_packed || ctx->is_preamble || L->nr_dests != 1 ||
L->dest[0].type != AGX_INDEX_NORMAL || L->dest[0].memory ||
L->mask != BITFIELD_MASK(agx_channels(L->dest[0])))
Expand Down
1 change: 1 addition & 0 deletions src/asahi/compiler/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ if with_tests
'test/test-shared-phase-cse.cpp',
'test/test-u8x4-hsum.cpp',
'test/test-guard-chains.cpp',
'test/test-insert-waits.cpp',
# agx_chip_isa_encoding() (test-native-form-regressions.cpp) lives
# in libasahi_lib, which this test does not link.
'../lib/agx_chip.c',
Expand Down
95 changes: 95 additions & 0 deletions src/asahi/compiler/test/test-insert-waits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2026 Aurora Silicon
* SPDX-License-Identifier: MIT
*/

#include <gtest/gtest.h>
#include "agx_builder.h"
#include "agx_compiler.h"
#include "agx_test.h"

static unsigned
count_waits_before(agx_block *block, agx_instr *stop)
{
unsigned waits = 0;
agx_foreach_instr_in_block(block, I) {
if (I == stop)
break;
if (I->op == AGX_OPCODE_WAIT)
waits++;
}
return waits;
}

class InsertWaits : public testing::Test {
protected:
InsertWaits() { mem_ctx = ralloc_context(NULL); }
~InsertWaits() { ralloc_free(mem_ctx); }
void *mem_ctx;
};

TEST_F(InsertWaits, ReorderableDeviceLoadStaysInFlightAcrossThreadgroupBarrier)
{
agx_builder *b = agx_test_builder(mem_ctx);
agx_block *blk = agx_start_block(b->shader);

agx_index addr = agx_register(0, AGX_SIZE_64);
agx_index off = agx_register(4, AGX_SIZE_32);
agx_index dst = agx_register(6, AGX_SIZE_32);
agx_index sum = agx_register(8, AGX_SIZE_32);

agx_instr *ld =
agx_device_load_to(b, dst, addr, off, AGX_FORMAT_I32, 0x1, 0, 0);
ld->memory_can_reorder = true;
agx_instr *barrier = agx_threadgroup_barrier(b);
agx_instr *use = agx_iadd_to(b, sum, dst, off, 0);

agx_insert_waits(b->shader);

EXPECT_EQ(count_waits_before(blk, barrier), 0u);
EXPECT_EQ(count_waits_before(blk, use), 1u);
}

TEST_F(InsertWaits, NonReorderableDeviceLoadDrainsAtThreadgroupBarrier)
{
agx_builder *b = agx_test_builder(mem_ctx);
agx_block *blk = agx_start_block(b->shader);

agx_index addr = agx_register(0, AGX_SIZE_64);
agx_index off = agx_register(4, AGX_SIZE_32);
agx_index dst = agx_register(6, AGX_SIZE_32);
agx_index sum = agx_register(8, AGX_SIZE_32);

agx_instr *ld =
agx_device_load_to(b, dst, addr, off, AGX_FORMAT_I32, 0x1, 0, 0);
ld->memory_can_reorder = false;
agx_instr *barrier = agx_threadgroup_barrier(b);
agx_instr *use = agx_iadd_to(b, sum, dst, off, 0);

agx_insert_waits(b->shader);

EXPECT_EQ(count_waits_before(blk, barrier), 1u);
EXPECT_EQ(count_waits_before(blk, use), 1u);
}

TEST_F(InsertWaits, ReorderableDeviceLoadDrainsAtMemoryBarrier)
{
agx_builder *b = agx_test_builder(mem_ctx);
agx_block *blk = agx_start_block(b->shader);

agx_index addr = agx_register(0, AGX_SIZE_64);
agx_index off = agx_register(4, AGX_SIZE_32);
agx_index dst = agx_register(6, AGX_SIZE_32);
agx_index sum = agx_register(8, AGX_SIZE_32);

agx_instr *ld =
agx_device_load_to(b, dst, addr, off, AGX_FORMAT_I32, 0x1, 0, 0);
ld->memory_can_reorder = true;
agx_instr *barrier = agx_memory_barrier(b);
agx_instr *use = agx_iadd_to(b, sum, dst, off, 0);

agx_insert_waits(b->shader);

EXPECT_EQ(count_waits_before(blk, barrier), 1u);
EXPECT_EQ(count_waits_before(blk, use), 1u);
}