Skip to content
Merged
53 changes: 52 additions & 1 deletion src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -16648,6 +16648,44 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
AGX_PASS(ctx, agx_opt_pack_bool_or_branch);
AGX_PASS(ctx, agx_opt_compact_constants);

/* On AGX3B, f32 FADD/FMUL/FFMA has no native mixed f16+f32 form (later
* widened via fadd #-0.0), whereas aligned f32 uniforms fold directly
* into FADD/FMUL/FFMA. Widen f16 constant sources to exact f32 before
* constant promotion so values like -32.0h promote as f32 uniforms. */
if (key->dev.encoding == AGX_ISA_ENCODING_AGX3B) {
agx_instr **defs = calloc(ctx->alloc, sizeof(agx_instr *));
if (defs) {
agx_foreach_instr_global(ctx, I) {
agx_foreach_ssa_dest(I, d)
defs[I->dest[d].value] = I;
}
agx_foreach_instr_global(ctx, I) {
if ((I->op != AGX_OPCODE_FADD && I->op != AGX_OPCODE_FMUL &&
I->op != AGX_OPCODE_FFMA) ||
I->nr_dests != 1 || I->dest[0].size != AGX_SIZE_32 ||
agx_channels(I->dest[0]) != 1)
continue;
agx_foreach_src(I, s) {
if (I->src[s].type != AGX_INDEX_NORMAL ||
I->src[s].size != AGX_SIZE_16)
continue;
agx_instr *def = defs[I->src[s].value];
if (!def || def->op != AGX_OPCODE_MOV_IMM ||
def->dest[0].size != AGX_SIZE_16)
continue;
float fval = _mesa_half_to_float((uint16_t)def->imm);
if (I->src[s].abs)
fval = fabsf(fval);
if (I->src[s].neg)
fval = -fval;
agx_builder b = agx_init_builder(ctx, agx_before_instr(I));
I->src[s] = agx_mov_imm(&b, 32, fui(fval));
}
}
free(defs);
}
}

/* After inlining constants, promote what's left */
if (key->promote_constants && !key->secondary &&
!(agx_compiler_debug & AGX_DBG_NOPROMOTE)) {
Expand Down Expand Up @@ -21370,9 +21408,21 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
NIR_PASS(_, nir, nir_opt_dce);
}
}
if (is_agx3)
if (is_agx3) {
NIR_PASS(post_opt_progress, nir,
agx_nir_reassociate_loop_invariant_eq);
/* Gate loop sinking to AGX3B compute shaders: on AGX3C
* already-spilling matmul loops it can increase spill/fill traffic
* without improving occupancy. */
if (is_agx3b && mesa_shader_stage_is_compute(nir->info.stage)) {
bool sunk = false;
NIR_PASS(sunk, nir, agx_nir_opt_sink_past_loops);
if (sunk) {
post_opt_progress = true;
NIR_PASS(_, nir, nir_opt_dce);
}
}
}
NIR_PASS(post_opt_progress, nir, agx_nir_opt_licm, regs, cap_rt_compute);
} else if (key->dev.encoding == AGX_ISA_ENCODING_AGX2 &&
nir->info.stage == MESA_SHADER_COMPUTE && !nir->info.internal &&
Expand All @@ -21389,6 +21439,7 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
NIR_PASS(post_opt_progress, nir, agx_nir_hoist_loop_invariant_guard_bits);

if (is_agx3) {
NIR_PASS(post_opt_progress, nir, agx_nir_opt_bitfield_slices);
if (nir->info.shared_size > 0)
NIR_PASS(post_opt_progress, nir, agx_nir_opt_shared_byte_offsets);
bool cse_progress = false;
Expand Down
2 changes: 2 additions & 0 deletions src/asahi/compiler/agx_nir.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ bool agx_nir_opt_shared_index_reassoc(struct nir_shader *s);
* chips with AGX_CAP_FOLD_SHARED_OFFSETS (four 32-bit elements per unit). */
#define G13X_I32_SHARED_INDEX_UNIT_B 16
bool agx_nir_opt_g13x_shared_offsets(struct nir_shader *s);
bool agx_nir_opt_bitfield_slices(struct nir_shader *s);
bool agx_nir_reassociate_loop_invariant_eq(struct nir_shader *shader);
bool agx_nir_opt_sink_past_loops(struct nir_shader *shader);
bool agx_nir_opt_licm(struct nir_shader *shader, unsigned budget,
bool cap_rt_compute);
bool agx_nir_opt_licm_agx2_integer(struct nir_shader *shader, unsigned budget);
Expand Down
Loading