Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
216 changes: 214 additions & 2 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ static const struct debug_named_value agx_debug_options[] = {
{"nopromote", AGX_DBG_NOPROMOTE, "Do not promote constants to uniforms"},
{"latecov", AGX_DBG_LATECOV, "Keep G17P coverage updates at the end of the fragment"},
{"refuse", AGX_DBG_REFUSE, "Name refused instructions, without dumping shaders"},
{"compsched", AGX_DBG_COMPSCHED, "Run the register-pressure scheduler on compute shaders"},
{"nolocalfold",AGX_DBG_NOLOCALFOLD,"Do not fold shared-access constants into the index immediate"},
DEBUG_NAMED_VALUE_END
};
/* clang-format on */
Expand Down Expand Up @@ -2940,6 +2942,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 +2980,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 +11168,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 +15543,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). Compute shaders on G13 therefore skip it unless
* AGX_MESA_DEBUG=compsched is set. 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.g13 &&
!(agx_compiler_debug & AGX_DBG_COMPSCHED));
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 +19596,11 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
NIR_PASS(_, nir, agx_nir_narrow_subgroup0_barriers);
}

if (key->dev.g13 && !(agx_compiler_debug & AGX_DBG_NOLOCALFOLD)) {
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
5 changes: 5 additions & 0 deletions src/asahi/compiler/agx_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ struct agx_device_key {
* function's code after packing. See agx3_transcode.c for the evidence.
*/
bool emit_agx3d;

/* Is the target GPU generation 13 (M1 family)? Selects the compute
* codegen defaults that are measured on G13 only.
*/
bool g13;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Supply human attribution and sign-off for this commit

The reviewed commit is authored and committed as Codex <codex@openai.com>, has no human Signed-off-by, and includes an Assisted-by: trailer in its commit message. These directly violate the hard attribution rule in AGENTS.md, which requires human-only commit identity and sign-off and places agent disclosure in the PR body rather than commits. Provide a policy-compliant commit before merging.

AGENTS.md reference: AGENTS.md:L50-L50

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the branch commits on GitHub directly (8164551db2a, 65b665c8518, 76adbf0fe9b): all three already have Joshua Warren <816217+joshuaswarren@users.noreply.github.com> as both author and committer, include Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>, and have no Assisted-by: trailer in any commit message (disclosure is in the PR body only). The Codex <codex@openai.com> identity appears to come from the automated review harness synthetic checkout rather than the PR branch commits.

};

/* AGX3D uses the AGX3C packer and transcodes its output. Resolve the
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
2 changes: 2 additions & 0 deletions src/asahi/compiler/agx_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ enum agx_compiler_dbg {
AGX_DBG_LATECOV = BITFIELD_BIT(13),
AGX_DBG_REFUSE = BITFIELD_BIT(14),
AGX_DBG_BINARIES = BITFIELD_BIT(15),
AGX_DBG_COMPSCHED = BITFIELD_BIT(16),
AGX_DBG_NOLOCALFOLD = BITFIELD_BIT(17),
};
/* clang-format on */

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
1 change: 1 addition & 0 deletions src/asahi/lib/agx_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,7 @@ agx_gather_device_key(struct agx_device *dev)
struct agx_device_key key = {
.needs_g13x_coherency = u_tristate_make(g13x_coh),
.soft_fault = agx_has_soft_fault(dev),
.g13 = dev->params.gpu_generation == 13,
};
agx_device_key_set_target(&key, dev->chip);
key.shader_quad_control = agx_chip_agx3_shader_quad_control(key.chip);
Expand Down