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
215 changes: 213 additions & 2 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,11 @@ agx_emit_local_load(agx_builder *b, agx_index dst, nir_intrinsic_instr *instr)
/* Shared offsets are relative to the workgroup allocation. */
agx_index base = agx3 ? agx_local_immediate_base(b, instr) : address;
agx_index index = agx3 ? address : agx_zero();
if (!agx3 && nir_intrinsic_base(instr)) {
unsigned unit = 4 * (instr->def.bit_size / 8);
assert(nir_intrinsic_base(instr) % unit == 0);
index = agx_immediate(nir_intrinsic_base(instr) / unit);
}
assert(address.size == AGX_SIZE_16);

enum agx_format format = format_for_bitsize(instr->def.bit_size);
Expand Down Expand Up @@ -2973,6 +2978,11 @@ agx_emit_local_store(agx_builder *b, nir_intrinsic_instr *instr)
const bool agx3 = b->shader->key->dev.encoding != AGX_ISA_ENCODING_AGX2;
agx_index base = agx3 ? agx_local_immediate_base(b, instr) : address;
agx_index index = agx3 ? address : agx_zero();
if (!agx3 && nir_intrinsic_base(instr)) {
unsigned unit = 4 * (nir_src_bit_size(instr->src[0]) / 8);
assert(nir_intrinsic_base(instr) % unit == 0);
index = agx_immediate(nir_intrinsic_base(instr) / unit);
}
assert(address.size == AGX_SIZE_16);

enum agx_format format = format_for_bitsize(nir_src_bit_size(instr->src[0]));
Expand Down Expand Up @@ -11156,6 +11166,190 @@ agx_mem_vectorize_cb(unsigned align_mul, unsigned align_offset,
return true;
}

/* Peel a constant addend out of a 16-bit shared address (through the u2u16
* narrowing and nested iadds). The lload/lstore index immediate adds 4 elements
* of the access type per unit (measured on G13G), so the multiple-of-unit part
* of the constant moves into nir_intrinsic_base (bytes) and the remainder stays
* in the address. */
static nir_scalar
agx_peel_const(nir_scalar s, int64_t *c)
{
if (!nir_scalar_is_alu(s))
return s;
nir_op op = nir_scalar_alu_op(s);
if (op == nir_op_u2u16 || op == nir_op_u2u32) {
/* A u2u16 truncation and the widening back both leave the address
* mod-2^16 addressable; accumulate constants mod 2^16 below. */
return agx_peel_const(nir_scalar_chase_alu_src(s, 0), c);
}
if (op == nir_op_iadd) {
nir_scalar a = nir_scalar_chase_alu_src(s, 0);
nir_scalar bb = nir_scalar_chase_alu_src(s, 1);
if (nir_scalar_is_const(bb)) {
*c = (*c + (int64_t)nir_scalar_as_uint(bb)) & 0xffff;
return agx_peel_const(a, c);
}
if (nir_scalar_is_const(a)) {
*c = (*c + (int64_t)nir_scalar_as_uint(a)) & 0xffff;
return agx_peel_const(bb, c);
}
}
return s;
}

/* The peeled scalar as a single-component address def. */
static nir_def *
agx_scalar_def(nir_builder *b, nir_scalar s)
{
if (s.def->num_components == 1)
return s.def;
return nir_channel(b, s.def, s.comp);
}

struct fold_cand {
nir_intrinsic_instr *intr;
unsigned oi;
nir_scalar stripped; /* address with constant addends removed */
nir_scalar key; /* grouping key: the value under the u2u16 narrowing */
int64_t c; /* constant addend (bytes) */
int64_t unit; /* bytes per index unit for this access */
};

/* Group the shared accesses of one block that differ only by constants.
* Shared addressing wraps mod 2^16, so peeled constants and member deltas
* are 16-bit: the member with the lowest real address anchors a shared
* base, and every other member's delta lands in the lload/lstore index
* immediate (4 elements of the access type per unit) with any remainder
* kept in the address. The index immediate add does not wrap, so the fold
* is sound only when the anchor plus delta cannot exceed 2^16: with
* shared_size <= 32 KiB every in-bounds address is below 2^15, the signed
* 16-bit delta between two real addresses is exact, and base + delta stays
* under 2^16. */
static bool
agx_fold_shared_block(nir_builder *b, nir_block *block)
{
struct fold_cand cands[512];
unsigned n = 0;
nir_foreach_instr(instr, block) {
if (instr->type != nir_instr_type_intrinsic || n == ARRAY_SIZE(cands))
continue;
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
unsigned oi;
if (intr->intrinsic == nir_intrinsic_load_shared)
oi = 0;
else if (intr->intrinsic == nir_intrinsic_store_shared)
oi = 1;
else
continue;
if (nir_intrinsic_base(intr) != 0)
continue;
nir_def *addr = intr->src[oi].ssa;
if (addr->bit_size != 16)
continue;
int64_t c = 0;
nir_scalar stripped = agx_peel_const(nir_scalar_resolved(addr, 0), &c);
unsigned bits = intr->intrinsic == nir_intrinsic_load_shared
? intr->def.bit_size
: nir_src_bit_size(intr->src[0]);
nir_scalar key = stripped;
if (nir_scalar_is_alu(stripped) &&
nir_scalar_alu_op(stripped) == nir_op_u2u16)
key = nir_scalar_chase_alu_src(stripped, 0);
cands[n++] = (struct fold_cand){intr, oi, stripped, key, c,
4 * (int64_t)(bits / 8)};
}
bool progress = false;
for (unsigned i = 0; i < n; i++) {
if (!cands[i].intr)
continue;
/* collect the group sharing this stripped address */
unsigned members[512];
unsigned m = 0;
for (unsigned j = i; j < n; j++) {
if (cands[j].intr && cands[j].key.def == cands[i].key.def &&
cands[j].key.comp == cands[i].key.comp)
members[m++] = j;
}
if (m < 2) {
cands[i].intr = NULL;
continue;
}
/* Signed 16-bit deltas relative to the first member; the lowest
* member's real address anchors the shared base. A member folds
* only when its delta covers a whole index unit; when none does,
* the rewrite would only replace each address with an equivalent
* one, so the group is left alone. */
int64_t min_delta = 0;
for (unsigned k = 1; k < m; k++) {
int64_t delta =
(int16_t)(uint16_t)(cands[members[k]].c - cands[i].c);
min_delta = MIN2(min_delta, delta);
}
bool any_fold = false;
for (unsigned k = 0; k < m; k++) {
struct fold_cand *cd = &cands[members[k]];
int64_t d =
(int16_t)(uint16_t)(cd->c - cands[i].c) - min_delta;
if (d >= cd->unit)
any_fold = true;
}
if (!any_fold) {
for (unsigned k = 0; k < m; k++)
cands[members[k]].intr = NULL;
continue;
}
struct fold_cand *first = &cands[members[0]];
b->cursor = nir_before_instr(&first->intr->instr);
nir_def *base = agx_scalar_def(b, cands[i].stripped);
if (base->bit_size != 16)
base = nir_u2u16(b, base);
uint32_t cbase = (uint16_t)(cands[i].c + min_delta);
if (cbase)
base = nir_iadd_imm(b, base, cbase);
for (unsigned k = 0; k < m; k++) {
struct fold_cand *cd = &cands[members[k]];
int64_t delta =
(int16_t)(uint16_t)(cd->c - cands[i].c);
int64_t d = delta - min_delta;
int64_t fold = (d / cd->unit) * cd->unit;
int64_t rest = d - fold;
if (fold / cd->unit >= 0x10000) {
cd->intr = NULL;
continue;
}
nir_def *na = base;
if (rest) {
b->cursor = nir_before_instr(&cd->intr->instr);
na = nir_iadd_imm(b, base, rest);
}
nir_intrinsic_set_base(cd->intr, (int)fold);
nir_src_rewrite(&cd->intr->src[cd->oi], na);
progress = true;
cd->intr = NULL;
}
}
return progress;
}

static bool
agx_fold_shared_offsets(nir_shader *nir)
{
/* The fold's in-bounds proof needs every real address below 2^15; a
* larger shared window can address past it. */
if (nir->info.shared_size == 0 || nir->info.shared_size > 32768)
return false;

bool progress = false;
nir_foreach_function_impl(impl, nir) {
nir_builder b = nir_builder_create(impl);
nir_foreach_block(block, impl) {
progress |= agx_fold_shared_block(&b, block);
}
nir_progress(progress, impl, nir_metadata_none);
}
return progress;
}

static bool
set_speculate(nir_builder *b, nir_instr *instr, UNUSED void *_)
{
Expand Down Expand Up @@ -15347,10 +15541,21 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
if (likely(!(agx_compiler_debug & AGX_DBG_NOOPT)))
AGX_PASS(ctx, agx_opt_sink_immediates);

if (likely(!(agx_compiler_debug & AGX_DBG_NOSCHED))) {
/* The bottom-up register-pressure scheduler moves each load next to its
* use, which serializes the memory round trips of small compute kernels
* (the LLM decode chain: +1.7% tokens/s with it off, bit-identical
* results). Chips with AGX_CAP_COMPUTE_SKIP_PRESSURE_SCHED skip it on
* compute shaders; other generations keep it until they are measured. */
bool skip_sched = (agx_compiler_debug & AGX_DBG_NOSCHED) ||
(nir->info.stage == MESA_SHADER_COMPUTE &&
ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX2 &&
agx_chip_has_cap(ctx->key->dev.chip,
AGX_CAP_COMPUTE_SKIP_PRESSURE_SCHED));
if (likely(!skip_sched)) {
AGX_PASS(ctx, agx_pressure_schedule);
AGX_PASS(ctx, agx_ilp_schedule);
}
if (likely(!(agx_compiler_debug & AGX_DBG_NOSCHED)))
AGX_PASS(ctx, agx_ilp_schedule);

AGX_PASS(ctx, agx3_order_fragment_device_loads);

Expand Down Expand Up @@ -19389,6 +19594,12 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
NIR_PASS(_, nir, agx_nir_narrow_subgroup0_barriers);
}

if (key->dev.encoding == AGX_ISA_ENCODING_AGX2 &&
agx_chip_has_cap(key->dev.chip, AGX_CAP_FOLD_SHARED_OFFSETS)) {
NIR_PASS(_, nir, agx_fold_shared_offsets);
NIR_PASS(_, nir, nir_opt_dce);
}

if (agx_should_dump(nir, AGX_DBG_SHADERS))
nir_print_shader(nir, stdout);

Expand Down
2 changes: 1 addition & 1 deletion src/asahi/compiler/agx_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ agx_validate_ra(UNUSED agx_context *ctx)
#endif

enum agx_size agx_split_width(const agx_instr *I);
bool agx_allows_16bit_immediate(agx_instr *I);
bool agx_allows_16bit_immediate(agx_instr *I, enum agx_isa_encoding enc);
unsigned agx_negate_src_index(agx_instr *I);

static inline bool
Expand Down
7 changes: 6 additions & 1 deletion src/asahi/compiler/agx_ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
#include "agx_opcodes.h"

bool
agx_allows_16bit_immediate(agx_instr *I)
agx_allows_16bit_immediate(agx_instr *I, enum agx_isa_encoding enc)
{
/* The local load/store index immediate exists only in the AGX2 local
* form; AGX3 keeps the offset in the base operand. */
const bool local = (I->op == AGX_OPCODE_LOCAL_LOAD) ||
(I->op == AGX_OPCODE_LOCAL_STORE);
return (I->op == AGX_OPCODE_DEVICE_LOAD) ||
(I->op == AGX_OPCODE_DEVICE_STORE) ||
(local && enc == AGX_ISA_ENCODING_AGX2) ||
(I->op == AGX_OPCODE_STACK_LOAD) ||
(I->op == AGX_OPCODE_STACK_STORE) ||
(I->op == AGX_OPCODE_UNIFORM_STORE) || (I->op == AGX_OPCODE_ATOMIC) ||
Expand Down
3 changes: 2 additions & 1 deletion src/asahi/compiler/agx_optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ agx_optimizer_inline_imm(agx_context *ctx, agx_instr **defs, agx_instr *I)
I->src[s] = agx_immediate_f(f);
} else if (value == def->imm) {
I->src[s] = agx_immediate(value);
} else if (value_u16 == def->imm && agx_allows_16bit_immediate(I)) {
} else if (value_u16 == def->imm &&
agx_allows_16bit_immediate(I, ctx->key->dev.encoding)) {
I->src[s] = agx_abs(agx_immediate(value_u16));
} else if ((I->op == AGX_OPCODE_IADD || I->op == AGX_OPCODE_IMAD) &&
s == agx_negate_src_index(I)) {
Expand Down
6 changes: 3 additions & 3 deletions src/asahi/compiler/agx_validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ is_stack_valid(agx_instr *I)
}

static bool
agx_validate_sources(agx_instr *I)
agx_validate_sources(agx_instr *I, enum agx_isa_encoding enc)
{
agx_foreach_src(I, s) {
agx_index src = I->src[s];
Expand All @@ -97,7 +97,7 @@ agx_validate_sources(agx_instr *I)
agx_validate_assert(!src.cache);
agx_validate_assert(!src.discard);

bool ldst = agx_allows_16bit_immediate(I);
bool ldst = agx_allows_16bit_immediate(I, enc);

/* Immediates are encoded as 8-bit (16-bit for memory load/store). For
* integers, they extend to 16-bit. For floating point, they are 8-bit
Expand Down Expand Up @@ -758,7 +758,7 @@ agx_validate(agx_context *ctx, const char *after)
}

agx_foreach_instr_global(ctx, I) {
if (!agx_validate_sources(I)) {
if (!agx_validate_sources(I, ctx->key->dev.encoding)) {
fprintf(stderr, "Invalid sources form after %s\n", after);
agx_print_instr(I, stderr);
fail = true;
Expand Down
3 changes: 2 additions & 1 deletion src/asahi/compiler/test/test-agx3-pack-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3941,7 +3941,8 @@ TEST_F(AGX3PackBinary, LocalAtomicReturnedImmediateAddressG17P)

agx_instr immediate_form = {};
immediate_form.op = AGX_OPCODE_LOCAL_ATOMIC;
EXPECT_TRUE(agx_allows_16bit_immediate(&immediate_form));
EXPECT_TRUE(
agx_allows_16bit_immediate(&immediate_form, AGX_ISA_ENCODING_AGX3B));

const struct agx3_local_atomic_desc d = {
.op = AGX3_ATOMIC_XCHG,
Expand Down
23 changes: 21 additions & 2 deletions src/asahi/lib/agx_chip_caps.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,32 @@ enum agx_chip_cap {
* reuse, push-descriptor elision, and direct fragment-root comparisons
* without shadow snapshots. Qualified on G17P and M5 (G17G, G17S, G17C). */
AGX_CAP_CACHED_CMD_MEMORY = 1u << 22,

/* Compute shaders skip the bottom-up register-pressure scheduler: it
* moves each load next to its use, which serializes the memory round
* trips of small compute kernels (the LLM decode chain). The ILP
* scheduler still runs. Measured on G13G and G13X (+1.7% tokens/s,
* bit-identical results); other generations keep the scheduler until
* they are measured. */
AGX_CAP_COMPUTE_SKIP_PRESSURE_SCHED = 1u << 23,

/* Constant deltas between a block's shared-memory accesses fold into the
* lload/lstore index immediate (4 elements of the access type per unit).
* Measured on G13G and G13X; other generations keep the unfolded
* addresses until they are measured. */
AGX_CAP_FOLD_SHARED_OFFSETS = 1u << 24,
};

/* What the admitted AGX2 chips (G13G, G13X, G14G, G14X) share. */
#define AGX_CAPS_AGX2 \
(AGX_CAP_SPARSE_BINDING | AGX_CAP_SPARSE_RESIDENCY_3D | \
AGX_CAP_DESCRIPTOR_BUFFER | AGX_CAP_COOPERATIVE_MATRIX)

/* What the measured G13 chips (G13G, G13X) add over the AGX2 set. */
#define AGX_CAPS_G13 \
(AGX_CAPS_AGX2 | AGX_CAP_COMPUTE_SKIP_PRESSURE_SCHED | \
AGX_CAP_FOLD_SHARED_OFFSETS)

/* What the five native AGX3 chips (G15G, G15S, G15C, G16G, G17P) share. */
#define AGX_CAPS_NATIVE \
(AGX_CAP_NATIVE_INDIRECT_COMPUTE | AGX_CAP_NATIVE_IMAGEBLOCK | \
Expand Down Expand Up @@ -205,8 +224,8 @@ enum agx_chip_cap {
* predicates do.
*/
#define AGX_CHIP_CAPS(X, ...) \
X(G13G, AGX_CAPS_AGX2, __VA_ARGS__) \
X(G13X, AGX_CAPS_AGX2, __VA_ARGS__) \
X(G13G, AGX_CAPS_G13, __VA_ARGS__) \
X(G13X, AGX_CAPS_G13, __VA_ARGS__) \
X(G14G, AGX_CAPS_AGX2, __VA_ARGS__) \
X(G14X, AGX_CAPS_AGX2, __VA_ARGS__) \
X(G15P, 0, __VA_ARGS__) \
Expand Down
Loading