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
84 changes: 8 additions & 76 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14765,12 +14765,11 @@ m5_lower_predicated_load(nir_builder *b, nir_intrinsic_instr *intr,


static bool
agx_compile_function_nir_attempt(nir_shader *nir, nir_function_impl *impl,
agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
struct agx_shader_key *key,
struct util_dynarray *binary,
struct agx_shader_info *out, unsigned *offset_out,
unsigned ra_budget_cut, bool disable_remat, bool *retry_without_remat,
bool agx3_reg_float_consts)
unsigned ra_budget_cut, bool agx3_reg_float_consts)
{
const unsigned initial_binary_size = binary->size;
const struct agx_shader_info initial_out = *out;
Expand Down Expand Up @@ -15126,24 +15125,13 @@ agx_compile_function_nir_attempt(nir_shader *nir, nir_function_impl *impl,

/* Record provenance before RA can create stack instructions. */
const bool imports_only_before_ra = agx3_cprog_spill_provenance(ctx);
if (disable_remat)
agx_ra_without_remat(ctx);
else
agx_ra(ctx);
agx_ra(ctx);

/* A failed allocation is refused and retried below; its AIR is not a
* program to validate (agx_validate exits on it). */
if (!ctx->ra_failed)
agx_validate(ctx, disable_remat ? "agx_ra_without_remat" : "agx_ra");
agx_validate(ctx, "agx_ra");

/* The occupancy rematerialization attempt has already modified AIR.
* Discard the entire context; changing its budget cannot undo spills. */
if (retry_without_remat && ctx->remat_fallback_spilled) {
*retry_without_remat = true;
binary->size = initial_binary_size;
ralloc_free(ctx);
return false;
}
if (ctx->ra_failed) {
/* Spilling only targets total demand; pinned values can still leave the
* shuffle no room. Recompile with an ever smaller spill budget unless
Expand All @@ -15155,10 +15143,9 @@ agx_compile_function_nir_attempt(nir_shader *nir, nir_function_impl *impl,
binary->size = initial_binary_size;
/* Constant promotion and others accumulate into out. */
*out = initial_out;
return agx_compile_function_nir_attempt(
return agx_compile_function_nir(
nir, impl, key, binary, out, offset_out,
ra_budget_cut ? ra_budget_cut * 2 : 16, disable_remat,
retry_without_remat, agx3_reg_float_consts);
ra_budget_cut ? ra_budget_cut * 2 : 16, agx3_reg_float_consts);
}
agx_set_refusal(ctx, "register allocation found no legal placement",
__LINE__);
Expand Down Expand Up @@ -16057,61 +16044,6 @@ agx_opt_agx3_normflt_byte_pairs(agx_context *ctx)
ralloc_free(memctx);
}

/* Retry eligible preambles without changing scratch admission or launch rules. */
static bool
agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
struct agx_shader_key *key,
struct util_dynarray *binary,
struct agx_shader_info *out, unsigned *offset_out,
bool agx3_reg_float_consts)
{
const bool eligible =
impl->function->is_preamble && nir->info.stage == MESA_SHADER_FRAGMENT &&
key->dev.encoding == AGX_ISA_ENCODING_AGX3C &&
(key->g17p_native_uniforms || key->agx3_native_cprog) &&
debug_get_bool_option("ASAHI_M5_CPROG_REMAT_RETRY", true);
if (!eligible)
return agx_compile_function_nir_attempt(nir, impl, key, binary, out,
offset_out, 0, false, NULL,
agx3_reg_float_consts);

/* Preserve all compiler-owned state, including earlier main code/rodata.
* The first attempt owns a deep NIR clone and a fresh AIR context. */
nir_shader *attempt = nir_shader_clone(NULL, nir);
if (!attempt)
return agx_compile_function_nir_attempt(nir, impl, key, binary, out,
offset_out, 0, false, NULL,
agx3_reg_float_consts);

nir_function_impl *attempt_impl = NULL;
nir_foreach_function_with_impl(func, candidate, attempt) {
if (func->is_preamble) {
assert(attempt_impl == NULL);
attempt_impl = candidate;
}
}
assert(attempt_impl != NULL);
const struct agx_shader_info saved_info = *out;
const struct agx_shader_key saved_key = *key;
const size_t saved_size = binary->size;
bool retry = false;
bool success = agx_compile_function_nir_attempt(
attempt, attempt_impl, key, binary, out, offset_out, 0, false, &retry,
agx3_reg_float_consts);
ralloc_free(attempt);
if (!retry)
return success;

assert(!success && binary->size == saved_size);
*out = saved_info;
*key = saved_key;
binary->size = saved_size;
/* Exactly one retry. Real full-budget spills retain the old refusal. */
return agx_compile_function_nir_attempt(nir, impl, key, binary, out,
offset_out, 0, true, NULL,
agx3_reg_float_consts);
}

/*
* The hardware frcp instruction is sometimes off by 1 ULP. For correctly
* rounded frcp, a refinement step is required. This routine has been
Expand Down Expand Up @@ -19363,7 +19295,7 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
const struct agx_shader_key saved_key = *key;
const unsigned saved_size = binary.size;
bool compiled = agx_compile_function_nir(nir, impl, key, &binary,
&out->info, &offset, false);
&out->info, &offset, 0, false);

/* The G17G packer refuses shapes it has no grounded encoding for and
* rolls the binary back. Most refusals in real clients (Skia/ANGLE)
Expand All @@ -19376,7 +19308,7 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
*key = saved_key;
binary.size = saved_size;
compiled = agx_compile_function_nir(nir, impl, key, &binary,
&out->info, &offset, true);
&out->info, &offset, 0, true);
}

if (!compiled) {
Expand Down
9 changes: 5 additions & 4 deletions src/asahi/compiler/agx_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,10 @@ typedef struct {
/* M5 CEM owns halves 8..15, beyond CF/shuffle/parallel-copy scratch. */
bool agx3_cem_low_scratch;

/* The remat-only spiller had to spill a value to scratch; register
* allocation must finish with real spilling under the full budget. */
/* A remat-only spill pass left a value in memory. The remat-only spiller
* keeps every value it cannot rematerialize in a register, so this is a
* backstop for memory values that other paths create; when set, register
* allocation finishes with real spilling under the full budget. */
bool remat_fallback_spilled;
unsigned spill_pcopy_base;

Expand Down Expand Up @@ -807,7 +809,7 @@ typedef struct {
/* Halves taken off the spill budget after a failed allocation, so the
* live-range shuffle gets free room (agx_compile_function_nir retries). */
unsigned ra_budget_cut;
/* Set by agx_ra_impl() while it assigns registers: a failed allocation
/* Set by agx_ra() while it assigns registers: a failed allocation
* that has no legal region left at all leaves RA through it (a
* jmp_buf *), see find_best_region_to_evict(). */
void *ra_abort;
Expand Down Expand Up @@ -1421,7 +1423,6 @@ bool agx_fold_agx3_spill_constant_phis(agx_context *ctx);
void agx_repair_ssa(agx_context *ctx);
void agx_reindex_ssa(agx_context *ctx);
void agx_ra(agx_context *ctx);
void agx_ra_without_remat(agx_context *ctx);
bool agx_ra_find_regs_simple(const BITSET_WORD *used, unsigned count,
unsigned align, unsigned minimum, unsigned limit,
unsigned *out);
Expand Down
102 changes: 34 additions & 68 deletions src/asahi/compiler/agx_register_allocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ find_best_region_to_evict(struct ra_ctx *rctx, unsigned size,
}

/* Every region was already evicted for this value: there is nothing left
* to finish the allocation with. Leave RA now (agx_ra_impl() frees its
* to finish the allocation with. Leave RA now (agx_ra() frees its
* state); the caller sees ra_failed and recompiles with a smaller spill
* budget. Continuing used to assert here in debug builds and index out of
* bounds in release builds (geometry shader of
Expand Down Expand Up @@ -3769,7 +3769,7 @@ agx_ra_mem_bound(agx_context *ctx)
: AGX3_G17P_MAX_SPILL_B;

/* Reserve the 16-byte AGX3B atomic-return scratch word placed directly
* above align(scratch_size_B, 4) in agx_compile_function_nir_attempt(). */
* above align(scratch_size_B, 4) in agx_compile_function_nir(). */
if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B) {
bool has_atomic_return = false;
agx_foreach_instr_global(ctx, I) {
Expand Down Expand Up @@ -3817,8 +3817,8 @@ agx_licm_demand_bound(agx_context *ctx, unsigned demand,
return MIN2(max_possible_regs, ALIGN_POT(demand, ra_file_alignment(ctx)));
}

static void
agx_ra_impl(agx_context *ctx, bool disable_remat)
void
agx_ra(agx_context *ctx)
{
ctx->uniforms_written = agx_has_uniform_store(ctx);
agx_lower_agx3b_atomic_returns(ctx);
Expand Down Expand Up @@ -4001,7 +4001,7 @@ agx_ra_impl(agx_context *ctx, bool disable_remat)
* waves. If we only have a single wave regardless, this is pointless.
* Without a static register split there is no wave to save.
*/
if (!disable_remat && agx_has_static_register_split(ctx) &&
if (agx_has_static_register_split(ctx) &&
effective_demand > agx_round_registers(1) && !spilling) {
unsigned effective_demand_remat = agx_calc_register_demand(ctx, true);

Expand Down Expand Up @@ -4046,60 +4046,39 @@ agx_ra_impl(agx_context *ctx, bool disable_remat)
if (ctx->ra_failed)
return;

if (agx_is_g18g(&ctx->key->dev)) {
/* Rematerialization changes instruction lifetimes. Measure the
* actual result before deciding whether the optional wave-saving
* budget held.
/* Rematerialization changes instruction lifetimes. Measure the actual
* result before deciding whether the optional wave-saving budget held.
*/
agx_compute_liveness(ctx);
effective_demand = agx_calc_register_demand(ctx, false);

if (remat && (ctx->remat_fallback_spilled ||
effective_demand > max_possible_regs)) {
/* The reduced budget is only an occupancy optimization. The
* remat-only pass keeps every value it cannot rematerialize in a
* register (agx_spill.c keep_in_registers()), so it can miss that
* estimate but never adds scratch traffic, and its result is no
* larger than the original program, which fit the full budget.
* Retain the rematerialized program and allocate it under the
* original hardware budget. This used to recompile fragment
* preambles without rematerialization once the remat-only pass had
* spilled to scratch.
*/
agx_compute_liveness(ctx);
effective_demand = agx_calc_register_demand(ctx, false);

if (remat && (ctx->remat_fallback_spilled ||
effective_demand > max_possible_regs)) {
/* The reduced budget is only an occupancy optimization.
* Constants reintroduced at their uses can exceed that estimate
* even without a scratch spill. Retain the rematerialized
* program and allocate under the original hardware budget,
* spilling only if needed.
*/
max_possible_regs = full_budget;
remat = false;
spilling = ctx->remat_fallback_spilled ||
effective_demand > max_possible_regs;
if (effective_demand > max_possible_regs) {
if (!ctx->key->has_scratch) {
ctx->ra_failed = true;
return;
}
agx_spill(ctx, max_possible_regs, false);
if (ctx->ra_failed)
return;
agx_compute_liveness(ctx);
effective_demand = agx_calc_register_demand(ctx, false);
max_possible_regs = full_budget;
remat = false;
spilling = ctx->remat_fallback_spilled ||
effective_demand > max_possible_regs;
if (effective_demand > max_possible_regs) {
if (!ctx->key->has_scratch) {
ctx->ra_failed = true;
return;
}
}
} else {
if (remat && ctx->remat_fallback_spilled) {
/* The wave-saving remat-only pass could not meet its estimated
* budget and spilled to scratch; finish under the full budget. */
max_possible_regs = full_budget;
remat = false;
spilling = true;
agx_spill(ctx, max_possible_regs, false);
if (ctx->ra_failed)
return;
agx_compute_liveness(ctx);
if (agx_calc_register_demand(ctx, false) > max_possible_regs) {
if (!ctx->key->has_scratch) {
ctx->ra_failed = true;
return;
}
agx_spill(ctx, max_possible_regs, false);
if (ctx->ra_failed)
return;
}
effective_demand = agx_calc_register_demand(ctx, false);
}

/* After spilling, recalculate liveness and demand */
agx_compute_liveness(ctx);
effective_demand = agx_calc_register_demand(ctx, false);
}

/* The resulting program can now be assigned registers */
Expand Down Expand Up @@ -5008,16 +4987,3 @@ agx_ra_impl(agx_context *ctx, bool disable_remat)
free(classes);
free(visited);
}

void
agx_ra(agx_context *ctx)
{
agx_ra_impl(ctx, false);
}

/* Retry from a fresh compiler context, retaining the original full budget. */
void
agx_ra_without_remat(agx_context *ctx)
{
agx_ra_impl(ctx, true);
}
Loading