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
52 changes: 47 additions & 5 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -16213,6 +16213,7 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
struct util_dynarray *binary,
struct agx_shader_info *out, unsigned *offset_out,
unsigned ra_budget_cut, bool agx3_reg_float_consts,
bool disable_sampled_cap,
bool opt_select_shifted_half,
bool *did_select_shifted_half)
{
Expand All @@ -16237,6 +16238,7 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
agx_context *ctx = rzalloc(NULL, agx_context);
ctx->nir = nir;
ctx->ra_budget_cut = ra_budget_cut;
ctx->disable_sampled_cap = disable_sampled_cap;
ctx->is_preamble = impl->function->is_preamble;
ctx->agx3_reg_float_consts = agx3_reg_float_consts;
if (ctx->is_preamble)
Expand Down Expand Up @@ -16599,6 +16601,20 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
agx_validate(ctx, "agx_ra");

if (ctx->ra_failed) {
/* If the optional sampled-compute occupancy cap tightened the file,
* drop it first and retry from the whole register file before giving
* up (including on RA_MEM exhaustion) or cutting the GPR budget. */
if (ctx->sampled_cap_applied) {
assert(!disable_sampled_cap && !ra_budget_cut);
ralloc_free(ctx);
binary->size = initial_binary_size;
*out = initial_out;
return agx_compile_function_nir(
nir, impl, key, binary, out, offset_out, 0,
agx3_reg_float_consts, true, opt_select_shifted_half,
did_select_shifted_half);
}

/* Spilling only targets total demand; pinned values can still leave the
* shuffle no room. Recompile with an ever smaller spill budget unless
* RA_MEM itself was exhausted, where cutting the GPR budget only forces
Expand All @@ -16612,14 +16628,30 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
return agx_compile_function_nir(
nir, impl, key, binary, out, offset_out,
ra_budget_cut ? ra_budget_cut * 2 : 16, agx3_reg_float_consts,
opt_select_shifted_half, did_select_shifted_half);
disable_sampled_cap, opt_select_shifted_half,
did_select_shifted_half);
}
agx_set_refusal(ctx, "register allocation found no legal placement",
__LINE__);
ralloc_free(ctx);
return false;
}

/* The sampled-compute cap trades moderate static TPS spills for extra
* resident threads. If it caused a spill storm (too large a spill frame,
* too many scalar spills/fills, or loop-weighted spill traffic outpacing
* texture reads), drop the optional cap and retry from the whole file. */
if (ctx->sampled_cap_applied && agx_sampled_cap_spills_excessive(ctx)) {
assert(!disable_sampled_cap && !ra_budget_cut);
ralloc_free(ctx);
binary->size = initial_binary_size;
*out = initial_out;
return agx_compile_function_nir(
nir, impl, key, binary, out, offset_out, 0,
agx3_reg_float_consts, true, opt_select_shifted_half,
did_select_shifted_half);
}

/* Apple's G17P compiler publishes a 16-byte scratch footprint for compute,
* vertex and fragment shaders that consume a device atomic's return.
* The otherwise equivalent return-unused shaders publish zero, and the
Expand Down Expand Up @@ -16707,6 +16739,16 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl,
: AGX3_G17P_MAX_SPILL_B;

if (frame_B > limit_B) {
if (ctx->sampled_cap_applied) {
assert(!disable_sampled_cap && !ra_budget_cut);
ralloc_free(ctx);
binary->size = initial_binary_size;
*out = initial_out;
return agx_compile_function_nir(
nir, impl, key, binary, out, offset_out, 0,
agx3_reg_float_consts, true, opt_select_shifted_half,
did_select_shifted_half);
}
agx_set_refusal(ctx, "AGX3 scratch frame exceeds target limit",
__LINE__);
binary->size = initial_binary_size;
Expand Down Expand Up @@ -21362,7 +21404,7 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
bool folded_shifted_half = false;
bool compiled = agx_compile_function_nir(nir, impl, key, &binary,
&out->info, &offset, 0, false,
opt_select_shifted_half,
false, opt_select_shifted_half,
&folded_shifted_half);

/* Outside AGX3B the shifted-half fold commits without a speculative pack
Expand All @@ -21377,7 +21419,7 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
binary.size = saved_size;
compiled = agx_compile_function_nir(nir, impl, key, &binary,
&out->info, &offset, 0, false,
false, NULL);
false, false, NULL);
}

/* The G17G packer refuses shapes it has no grounded encoding for and
Expand All @@ -21393,7 +21435,7 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
folded_shifted_half = false;
compiled = agx_compile_function_nir(nir, impl, key, &binary,
&out->info, &offset, 0, true,
opt_select_shifted_half,
false, opt_select_shifted_half,
&folded_shifted_half);
if (!compiled && folded_shifted_half) {
if (agx_compiler_debug & AGX_DBG_SHADERS)
Expand All @@ -21405,7 +21447,7 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key,
binary.size = saved_size;
compiled = agx_compile_function_nir(nir, impl, key, &binary,
&out->info, &offset, 0, true,
false, NULL);
false, false, NULL);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/asahi/compiler/agx_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ struct agx_shader_key {
* AGX3 and AGX2_MAX_SPILL_SLOTS (4 KiB) of spill slots on AGX2. */
uint32_t max_scratch_B;

/* Whether the program's launch also takes the scratch of optional spills,
* such as those of the sampled compute register cap
* (agx_sampled_compute_register_cap()). Some launch paths refuse scratch,
* or bound its size, even where has_scratch is set.
*/
bool optional_spills;

/* Whether we're compiling the helper program used for scratch allocation.
* This has special register allocation requirements.
*/
Expand Down Expand Up @@ -879,6 +886,14 @@ struct agx_occupancy {
struct agx_occupancy agx_occupancy_for_register_count(unsigned halfregs);
unsigned agx_max_registers_for_occupancy(unsigned occupancy);

/* General half-registers one thread can allocate on this device, or 0 when
* the encoding's register file is not known. */
unsigned agx_register_file_halves(const struct agx_device_key *dev);

/* Register budget for compute programs with several texture reads, derived
* from the register file and the occupancy steps; 0 means no cap. */
unsigned agx_sampled_compute_register_cap(const struct agx_device_key *dev);

/* Vertex attributes a vertex prolog can hand to the main shader on this
* device through the register ABI (AGX_ABI_VIN_ATTRIB): component i of the
* compacted attribute slots occupies half-registers 2 * (8 + i) and the one
Expand Down
7 changes: 7 additions & 0 deletions src/asahi/compiler/agx_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ typedef struct {
* Retrying with a smaller GPR budget cannot help and only increases spill
* pressure. */
bool ra_mem_exhausted;
/* Disable the optional sampled-compute occupancy cap on a retry after a
* capped allocation failed (including on RA_MEM exhaustion). */
bool disable_sampled_cap;
/* Set by agx_ra_impl() when agx_sampled_compute_register_cap() actually
* lowered max_possible_regs on this attempt. */
bool sampled_cap_applied;
/* 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;
Expand Down Expand Up @@ -1449,6 +1455,7 @@ 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);
bool agx_sampled_cap_spills_excessive(const 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
90 changes: 90 additions & 0 deletions src/asahi/compiler/agx_performance.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,96 @@ agx_max_registers_for_occupancy(unsigned occupancy)
return max_regs;
}

unsigned
agx_register_file_halves(const struct agx_device_key *dev)
{
switch (dev->encoding) {
case AGX_ISA_ENCODING_AGX2:
return AGX_NUM_REGS;
case AGX_ISA_ENCODING_AGX3B:
return AGX3B_NUM_REGS;
case AGX_ISA_ENCODING_AGX3C:
/* G18G compiles as AGX3C with emit_agx3d; both files end at r125. */
return dev->emit_agx3d ? AGX3D_NUM_REGS : AGX3C_NUM_REGS;
case AGX_ISA_ENCODING_AGX3D:
return AGX3D_NUM_REGS;
default:
return 0;
}
}

/* Encodings whose register spills lower to launch-allocated static
* thread-private storage (copy/rld with hardware scoreboarding) rather than
* AGX2's helper-backed device-memory stack with explicit 2-slot scoreboard
* waits. Optional occupancy-driven spilling only pays on static TPS: measured
* on G13G (AGX2), capping sampled compute to 160 halves adds ~55x higher
* per-core spill cost than on G17C (AGX3C), slowing ALU-heavy sampled compute
* by +16% to +259% while yielding no meaningful gain on texture-bound shaders
* (-0.9% to +2.4%, vs -7.0% to -14.5% on G17C for the same shaders).
*/
static bool
agx_isa_has_static_tps(enum agx_isa_encoding encoding)
{
return encoding == AGX_ISA_ENCODING_AGX3B ||
encoding == AGX_ISA_ENCODING_AGX3C ||
encoding == AGX_ISA_ENCODING_AGX3D;
}

/*
* A compute program that issues several texture reads spends much of its
* time waiting on them, and more resident threads hide that wait. Capping its
* registers trades scratch spills for those threads. The cap follows from
* three properties of the device:
*
* - launch-allocated static thread-private storage (agx_isa_has_static_tps()),
* - the occupancy steps (occupancies[] above): how many threads a core keeps
* resident at each per-thread register budget, and
* - the per-thread register file (agx_register_file_halves()).
*
* The target is 5/8 of the most threads a core keeps resident (the first
* step): 640 of 1024 threads, 20 of 32 SIMD groups. The cap is the largest
* budget that still reaches that step, 160 halves with the current steps.
* Tuned on G17C against the uncapped file and a 192-half cap.
* A device gets no cap when its ISA lacks static TPS (AGX2), when the cap
* would not raise its resident threads by at least 1.5x over what its whole
* file allows, or when its register file is unknown
* (agx_register_file_halves() returns 0).
*
* AGX3C and AGX3D currently resolve to 160: both use static TPS and their
* 252-half files reach 384 threads uncapped, so the cap raises that to 640.
* AGX3B (192 halves) already reaches 512, too small a step, so it gets no
* cap. AGX2 uses helper-backed device-memory stack spills with explicit waits
* and gets no cap. Launches that take no scratch (such as M5 without compute
* resources) leave optional_spills false instead of overriding the derivation
* here. All encodings share one occupancy table; a part whose steps differ
* needs its own table before this derivation is right for it.
*/
unsigned
agx_sampled_compute_register_cap(const struct agx_device_key *dev)
{
if (!agx_isa_has_static_tps(dev->encoding))
return 0;

const unsigned file = agx_register_file_halves(dev);
if (!file)
return 0;

const unsigned target = occupancies[0].max_threads * 5 / 8;
const unsigned cap = agx_max_registers_for_occupancy(target);

/* The spills only pay for a large step in resident threads: half again
* as many or more. 384 -> 640 threads gained on G17C; 512 -> 640 lost
* 7-10% on G16G with the same sample workload.
*/
const unsigned capped = agx_occupancy_for_register_count(cap).max_threads;
const unsigned uncapped =
agx_occupancy_for_register_count(file).max_threads;
if (cap >= file || capped * 2 < uncapped * 3)
return 0;

return cap;
}

/* Crude cycle model for G13G */
enum alu_unit {
NONE,
Expand Down
Loading