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
74 changes: 63 additions & 11 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,10 @@ agx_unpack_integer_8_agx3(agx_builder *b, agx_index dest,
{
agx_index channels[4] = {agx_null()};

/* AGX3C: unpack each byte in one 16-bit operation: even bytes mask
/* AGX3B/AGX3C: unpack each byte in one 16-bit operation: even bytes mask
* the low byte and odd bytes shift right by 8, clearing the upper byte. */
if (b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3C &&
if ((b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3B ||
b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) &&
!b->shader->key->dev.emit_agx3d) {
for (unsigned i = 0; i < nr_channels; ++i) {
channels[i] = (i & 1) ? agx_ushr(b, halfs[i / 2], agx_immediate(8))
Expand Down Expand Up @@ -2143,6 +2144,30 @@ agx_emit_store(agx_builder *b, nir_intrinsic_instr *instr)
assert(nr_packed == DIV_ROUND_UP(nr, 2));
value = agx_emit_collect(b, nr_packed, packed);
mask = BITFIELD_MASK(nr);
} else if ((b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3B ||
b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) &&
!b->shader->key->dev.emit_agx3d &&
fmt == AGX_FORMAT_I8 &&
nir_src_bit_size(instr->src[0]) == 8 &&
nir_src_num_components(instr->src[0]) == 1) {
/* A scalar W8 device_store writes only the low 8 bits of its 16-bit
* source register, so skip an explicit u2u8 0xff mask when feeding one. */
nir_scalar sc_val = nir_scalar_resolved(instr->src[0].ssa, 0);
if (nir_scalar_is_alu(sc_val) &&
nir_scalar_alu_op(sc_val) == nir_op_u2u8) {
nir_scalar wide = nir_scalar_chase_alu_src(sc_val, 0);
if (wide.def->num_components == 1 && wide.def->bit_size == 16) {
value = agx_def_index(wide.def);
} else if (wide.def->num_components == 1 && wide.def->bit_size == 32) {
value = agx_temp(b->shader, AGX_SIZE_16);
agx_subdivide_to(b, value, agx_def_index(wide.def), 0);
} else {
value = agx_recollect_vector(b, instr->src[0]);
}
} else {
value = agx_recollect_vector(b, instr->src[0]);
}
mask = 1;
} else {
value = agx_recollect_vector(b, instr->src[0]);
mask = BITFIELD_MASK(nir_src_num_components(instr->src[0]));
Expand Down Expand Up @@ -4761,17 +4786,20 @@ agx_lower_agx3_half_converts(agx_context *ctx)
I->src[0].value != AGX_CONVERT_S8_TO_F)))
continue;

/* M5 converts a signed byte to a half or a float natively (every
* byte value at every position of a half, both result widths, tested
* on G17C). G18 keeps the halfword fallback below. */
if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && !is_g18g &&
/* AGX3B/AGX3C convert signed bytes to f16/f32 and unsigned bytes to f32
* natively (verified by exhaustive sweep of every byte value and
* position on G17C and G17P B1 hardware). G18 keeps the halfword
* fallback below. */
if ((ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B ||
ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) && !is_g18g &&
I->src[0].value == AGX_CONVERT_S8_TO_F && I->round == AGX_ROUND_RTE &&
(I->dest[0].size == AGX_SIZE_16 || I->dest[0].size == AGX_SIZE_32) &&
I->src[1].size == AGX_SIZE_16 && !I->src[1].abs && !I->src[1].neg &&
!I->src[1].memory)
continue;

if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && !is_g18g &&
if ((ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B ||
ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) && !is_g18g &&
I->src[0].value == AGX_CONVERT_U8_TO_F && I->round == AGX_ROUND_RTE &&
I->dest[0].size == AGX_SIZE_32 && I->src[1].size == AGX_SIZE_16 &&
!I->src[1].abs && !I->src[1].neg && !I->src[1].memory)
Expand Down Expand Up @@ -16563,11 +16591,23 @@ agx_compile_shader_nir_once(nir_shader *nir, struct agx_shader_key *key,
assign_coefficient_regs(nir, &info->varyings.fs);
}

if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C) {
const bool agx3b_compute =
key->dev.encoding == AGX_ISA_ENCODING_AGX3B && !key->dev.emit_agx3d &&
mesa_shader_stage_is_compute(nir->info.stage);

if (agx3b_compute) {
bool unswitched_if = false;
NIR_PASS(unswitched_if, nir, agx_nir_opt_unswitch_invariant_if);
if (unswitched_if)
nir_divergence_analysis(nir);
}

if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C || agx3b_compute) {
/* Hoist only what the register file can hold through the loop: the
* file a workgroup allows (agx_ra), less what spilling reserves.
*/
unsigned regs = ROUND_DOWN_TO(AGX3C_NUM_REGS, 8);
unsigned regs = ROUND_DOWN_TO(
agx3b_compute ? AGX3B_NUM_REGS : AGX3C_NUM_REGS, 8);
if (mesa_shader_stage_is_compute(nir->info.stage)) {
const unsigned threads = nir->info.workgroup_size_variable
? 1024
Expand All @@ -16576,7 +16616,19 @@ agx_compile_shader_nir_once(nir_shader *nir, struct agx_shader_key *key,
regs = MIN2(regs, agx_max_registers_for_occupancy(threads));
}

NIR_PASS(_, nir, agx_nir_opt_licm, regs - 8);
unsigned licm_budget = regs - 8;
bool licm_speculative = false;
if (agx3b_compute) {
/* Empirically cap AGX3B compute LICM at 64 half-registers: while
* agx_max_registers_for_occupancy allows up to 104 halves, capping
* hoisted live-in demand at 64 halves avoids Dynamic Caching and
* register-pressure regressions measured on G17P compute loops.
* Enable speculative pure-ALU/preamble hoisting on non-internal
* compute shaders. */
licm_budget = MIN2(licm_budget, 64);
licm_speculative = !nir->info.internal;
}
NIR_PASS(_, nir, agx_nir_opt_licm, licm_budget, licm_speculative);
}

if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C) {
Expand All @@ -16585,7 +16637,7 @@ agx_compile_shader_nir_once(nir_shader *nir, struct agx_shader_key *key,
NIR_PASS(_, nir, nir_opt_dce);
}

if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C) {
if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C || agx3b_compute) {
NIR_PASS(_, nir, agx_nir_opt_canonical_minmax);
NIR_PASS(_, nir, nir_opt_dce);
}
Expand Down
3 changes: 2 additions & 1 deletion src/asahi/compiler/agx_nir.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct nir_def *agx_nir_quad_ddy(struct nir_builder *b, struct nir_def *v);
* still guarantees it for any AGX3 target. */
bool agx_nir_lower_shared_element_index(struct nir_shader *s);
bool agx_nir_opt_shared_byte_offsets(struct nir_shader *s);
bool agx_nir_opt_licm(struct nir_shader *shader, unsigned budget);
bool agx_nir_opt_licm(struct nir_shader *shader, unsigned budget, bool speculative);
bool agx_nir_lower_fminmax(struct nir_shader *s, bool ftz_only);
bool agx_nir_lower_fminmax_signed_zero(struct nir_shader *s, bool ftz_only);
bool agx_nir_lower_word_bounds(struct nir_shader *s);
Expand Down Expand Up @@ -373,6 +373,7 @@ bool agx_nir_opt_uniform_stride(struct nir_shader *shader);
bool agx_nir_fold_m5_offsets(struct nir_shader *shader);
bool agx_nir_unroll_runtime_loops(struct nir_shader *shader, unsigned factor,
unsigned max_instrs, unsigned *widest);
bool agx_nir_opt_unswitch_invariant_if(struct nir_shader *shader);
bool agx_nir_unroll_and_jam(struct nir_shader *shader, unsigned factor,
unsigned *widest);
bool agx_nir_opt_u8_absdiff_fp(struct nir_shader *shader);
Expand Down
11 changes: 11 additions & 0 deletions src/asahi/compiler/agx_nir_opt_guard_selected_loads.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,17 @@ same_base_ptr(nir_def *a, nir_def *b)
nir_intrinsic_base(ina) == nir_intrinsic_base(inb))
return true;
}
if (ia->type == nir_instr_type_alu && ib->type == nir_instr_type_alu) {
nir_alu_instr *aa = nir_instr_as_alu(ia);
nir_alu_instr *ab = nir_instr_as_alu(ib);
if (aa->op == nir_op_pack_64_2x32_split &&
ab->op == nir_op_pack_64_2x32_split) {
return atom_cmp(scalar_atom(nir_scalar_resolved(aa->src[0].src.ssa, aa->src[0].swizzle[0])),
scalar_atom(nir_scalar_resolved(ab->src[0].src.ssa, ab->src[0].swizzle[0]))) == 0 &&
atom_cmp(scalar_atom(nir_scalar_resolved(aa->src[1].src.ssa, aa->src[1].swizzle[0])),
scalar_atom(nir_scalar_resolved(ab->src[1].src.ssa, ab->src[1].swizzle[0]))) == 0;
}
}
return false;
}

Expand Down
72 changes: 57 additions & 15 deletions src/asahi/compiler/agx_nir_opt_licm.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,48 @@ instr_invariant(nir_instr *instr, struct invariance *inv)
}
}

/* Pure, side-effect-free instructions safe to hoist speculatively out of
* loops whose body follows a header exit test (e.g. `if (i >= n) break;`).
*/
static bool
instr_speculatable(nir_instr *instr, nir_loop *loop)
{
/* Only speculatively hoist from top-level loop blocks (e.g. loop body
* following a header break), not from inside conditional branches.
*/
if (instr->block->cf_node.parent != &loop->cf_node)
return false;

if (instr->type == nir_instr_type_intrinsic)
return nir_instr_as_intrinsic(instr)->intrinsic ==
nir_intrinsic_load_preamble;

if (instr->type != nir_instr_type_alu)
return false;

switch (nir_instr_as_alu(instr)->op) {
case nir_op_idiv:
case nir_op_udiv:
case nir_op_imod:
case nir_op_umod:
case nir_op_irem:
return false;
default:
return true;
}
}

static bool
agx_licm_filter(nir_instr *instr, nir_loop *loop, bool block_dominates_exit)
{
return block_dominates_exit || instr_speculatable(instr, loop);
}

static bool
loop_hoists(nir_loop *loop)
loop_hoists(nir_loop *loop, bool *has_terminate)
{
*has_terminate = false;

if (!nir_loop_has_back_edge(loop))
return false;

Expand All @@ -171,7 +210,7 @@ loop_hoists(nir_loop *loop)
nir_intrinsic_terminate ||
nir_instr_as_intrinsic(instr)->intrinsic ==
nir_intrinsic_terminate_if))
return false;
*has_terminate = true;
}
}

Expand Down Expand Up @@ -205,7 +244,8 @@ use_stays_in_loop(struct licm_estimate *est, nir_src *use, nir_loop *loop)

/* Halves of hoisted results that instructions staying in the loop read */
static unsigned
loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop)
loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop,
bool has_terminate, bool speculative)
{
struct invariance inv = {est, loop};
nir_block *exit = nir_loop_successor_block(loop);
Expand All @@ -214,12 +254,12 @@ loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop)
memset(est->invariant, 0, BITSET_BYTES(ssa_alloc));

nir_foreach_block_in_cf_node(block, &loop->cf_node) {
if (!nir_block_dominates(block, exit))
continue;
bool dom_exit = !has_terminate && nir_block_dominates(block, exit);

nir_foreach_instr(instr, block) {
nir_def *def = nir_instr_def(instr);
if (def && instr_invariant(instr, &inv))
if (def && instr_invariant(instr, &inv) &&
(dom_exit || (speculative && instr_speculatable(instr, loop))))
BITSET_SET(est->invariant, def->index);
}
}
Expand All @@ -244,27 +284,29 @@ loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop)

static bool
loops_fit(struct licm_estimate *est, unsigned ssa_alloc,
struct exec_list *list, unsigned budget)
struct exec_list *list, unsigned budget, bool speculative)
{
foreach_list_typed(nir_cf_node, node, node, list) {
switch (node->type) {
case nir_cf_node_if: {
nir_if *nif = nir_cf_node_as_if(node);
if (!loops_fit(est, ssa_alloc, &nif->then_list, budget) ||
!loops_fit(est, ssa_alloc, &nif->else_list, budget))
if (!loops_fit(est, ssa_alloc, &nif->then_list, budget, speculative) ||
!loops_fit(est, ssa_alloc, &nif->else_list, budget, speculative))
return false;
break;
}

case nir_cf_node_loop: {
nir_loop *loop = nir_cf_node_as_loop(node);
if (loop_hoists(loop)) {
unsigned cost = loop_hoist_cost(est, ssa_alloc, loop);
bool has_terminate = false;
if (loop_hoists(loop, &has_terminate) && (!has_terminate || speculative)) {
unsigned cost =
loop_hoist_cost(est, ssa_alloc, loop, has_terminate, speculative);
if (cost && loop_peak(est, loop) + cost > budget)
return false;
}

if (!loops_fit(est, ssa_alloc, &loop->body, budget))
if (!loops_fit(est, ssa_alloc, &loop->body, budget, speculative))
return false;
break;
}
Expand All @@ -282,7 +324,7 @@ loops_fit(struct licm_estimate *est, unsigned ssa_alloc,
* halves with the invariants hoisted.
*/
bool
agx_nir_opt_licm(nir_shader *shader, unsigned budget)
agx_nir_opt_licm(nir_shader *shader, unsigned budget, bool speculative)
{
nir_foreach_function_impl(impl, shader) {
nir_metadata_require(impl, nir_metadata_block_index |
Expand All @@ -302,12 +344,12 @@ agx_nir_opt_licm(nir_shader *shader, unsigned budget)
nir_foreach_def(instr, record_halves, &est);
}

bool fits = loops_fit(&est, impl->ssa_alloc, &impl->body, budget);
bool fits = loops_fit(&est, impl->ssa_alloc, &impl->body, budget, speculative);
ralloc_free(mem_ctx);

if (!fits)
return false;
}

return nir_opt_licm(shader, NULL);
return nir_opt_licm(shader, speculative ? agx_licm_filter : NULL);
}
Loading