Skip to content
Closed
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
22 changes: 15 additions & 7 deletions src/asahi/compiler/agx_pressure_schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1500,15 +1500,23 @@ agx_ilp_schedule(agx_context *ctx)
AGX_ILP_FS_SAMPLE_SLACK);
}
if (loads && m5_load_cap > 0) {
/* Prefer to hoist only a few device loads ahead of the oldest
* one no instruction has read yet: four, else two, else one,
* whichever first fits the program's occupancy step. This
* ranks candidates; register pressure and scoreboard waits
* remain what keeps the schedule safe. */
static const unsigned windows[] = {4, 2, 1};
/* Prefer to hoist a bounded window of device loads ahead of the
* oldest unread one, whichever first fits the program's
* occupancy step. M5 stays within {4, 2, 1}; AGX3B compute
* tries {8, 6, 4, 2, 1} so unrolled 8-tap stencil and feature
* kernels can issue a full row/column batch in flight when
* occupancy headroom allows. Register pressure and scoreboard
* waits remain what keeps the schedule safe. */
static const unsigned m5_windows[] = {4, 2, 1};
static const unsigned agx3b_windows[] = {8, 6, 4, 2, 1};
const unsigned *windows =
agx3b_compute ? agx3b_windows : m5_windows;
const unsigned nr_windows = agx3b_compute
? ARRAY_SIZE(agx3b_windows)
: ARRAY_SIZE(m5_windows);
const signed load_growth =
m5_load_cap > peak ? m5_load_cap - peak : 0;
for (unsigned w = 0; w < ARRAY_SIZE(windows); ++w) {
for (unsigned w = 0; w < nr_windows; ++w) {
if (ilp_schedule_block(ctx, block, memctx, native_uniforms,
alu_latency, 16 * alu_latency,
load_growth, 0, false, true,
Expand Down
69 changes: 69 additions & 0 deletions src/asahi/compiler/test/test-agx3-vision-opts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3268,6 +3268,75 @@ TEST(AGX3VisionOpts, Agx3bComputeDeviceLoadIlpSchedule)
EXPECT_EQ(run_sched(AGX_ISA_ENCODING_AGX2, AGX_CHIP_G13G, 0), 1u);
}

TEST(AGX3VisionOpts, Agx3bComputeDeviceLoadIlpWideWindow)
{
auto run_sched = [](enum agx_isa_encoding enc, enum agx_chip chip,
unsigned extra_live_32) -> unsigned {
void *mem = ralloc_context(NULL);
struct agx_shader_key key = {};
key.dev.encoding = enc;
key.dev.chip = chip;

agx_builder *b = agx_test_builder(mem);
b->shader->key = &key;
b->shader->stage = MESA_SHADER_COMPUTE;
b->shader->nir = rzalloc(mem, nir_shader);
b->shader->nir->info.stage = MESA_SHADER_COMPUTE;

agx_block *entry = agx_start_block(b->shader);
agx_block *body = agx_test_block(b->shader);
agx_block *exit = agx_test_block(b->shader);
body->loop_header = true;
agx_block_add_successor(entry, body);
agx_block_add_successor(body, body);
agx_block_add_successor(body, exit);

agx_index base = agx_uniform(4, AGX_SIZE_64);
agx_index off = agx_preload(b, agx_register(2, AGX_SIZE_32));
agx_index acc = agx_preload(b, agx_register(4, AGX_SIZE_32));
agx_index held[100];
assert(extra_live_32 <= 100);
for (unsigned i = 0; i < extra_live_32; ++i)
held[i] = agx_mov_imm(b, 32, 100 + i);

/* Emit 8 independent device_loads in pressure order (load, immediate use). */
b->cursor = agx_after_block(body);
for (unsigned i = 0; i < 8; ++i) {
agx_index addr_off = agx_iadd(b, off, agx_immediate(i * 4), 0);
agx_index val = agx_temp(b->shader, AGX_SIZE_32);
agx_instr *ld = agx_device_load_to(
b, val, base, addr_off, AGX_FORMAT_I32, 0x1, 0, 0);
ld->memory_can_reorder = true;
acc = agx_fadd(b, acc, val);
}
for (unsigned i = 0; i < extra_live_32; ++i)
acc = agx_iadd(b, acc, held[i], 0);
agx_device_store(b, acc, agx_uniform(8, AGX_SIZE_64), off, AGX_FORMAT_I32,
0x1, 0, 0);

agx_ilp_schedule(b->shader);

unsigned loads_before_first_fadd = 0;
agx_foreach_instr_in_block(body, I) {
if (I->op == AGX_OPCODE_FADD)
break;
if (I->op == AGX_OPCODE_DEVICE_LOAD)
loads_before_first_fadd++;
}
ralloc_free(mem);
return loads_before_first_fadd;
};

/* AGX3B compute with headroom issues all 8 loads ahead of first consumer. */
EXPECT_EQ(run_sched(AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, 0), 8u);
/* AGX3B high register pressure still falls back cleanly inside the step. */
EXPECT_EQ(run_sched(AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, 45), 1u);
/* M5 (AGX3C) keeps its existing {4, 2, 1} window untouched. */
unsigned m5_loads = run_sched(AGX_ISA_ENCODING_AGX3C, AGX_CHIP_G17S, 0);
EXPECT_GE(m5_loads, 2u);
EXPECT_LE(m5_loads, 4u);
}

static nir_def *
push_u32(nir_builder *b, unsigned offset)
{
Expand Down