From 8bf7dce78d1a3d08daaf9eac74856a93705c74dc Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Wed, 2 Dec 2026 19:33:35 -0500 Subject: [PATCH 1/2] asahi: promote loop-weighted M5 select/FMA constants and reuse rodata Signed-off-by: Erik Bourget --- src/asahi/compiler/agx_compiler.h | 12 + .../compiler/agx_lower_uniform_sources.c | 36 +- .../compiler/agx_opt_promote_constants.c | 566 ++++++++++++++++-- .../compiler/test/test-agx3-uniform-alu.cpp | 219 +++++++ 4 files changed, 792 insertions(+), 41 deletions(-) diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 1f3820e888c5..29012175eb0f 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -1336,18 +1336,30 @@ agx3_tex_coord(const agx_instr *I, unsigned c) bool agx3_tex_uniform_pair_unwitnessed(const agx_context *ctx, const agx_instr *I, agx_index texture, agx_index sampler); +/* Whether an M5 main-program FADD/FMUL/FFMA is a native uniform-operand form + * (agx3_uniform_float_encode). */ +bool agx3_m5_float_native_uniform(const agx_context *ctx, const agx_instr *I); + /* Whether replacing source s of an M5 main-program FADD/FMUL/FFMA with * `uniform` gives a native uniform-operand form (agx3_uniform_float_encode). */ bool agx3_m5_float_accepts_uniform(const agx_context *ctx, const agx_instr *I, unsigned s, unsigned uniform, enum agx_size size); +/* Whether an M5 main-program 32-bit ICMPSEL is a native uniform-operand form + * (agx3_isel_encode). */ +bool agx3_m5_isel_native_uniform(const agx_context *ctx, const agx_instr *I); + /* Whether replacing source s of an M5 main-program 32-bit ICMPSEL with * `uniform` gives a native uniform-operand form (agx3_isel_encode). */ bool agx3_m5_isel_accepts_uniform(const agx_context *ctx, const agx_instr *I, unsigned s, unsigned uniform, enum agx_size size); +/* Whether an M5 main-program 32-bit FCMPSEL is a native uniform-operand form + * (agx3_alu_fcmp_select32_gpu1). */ +bool agx3_m5_fcmpsel_native_uniform(const agx_context *ctx, const agx_instr *I); + bool agx_instr_accepts_uniform(enum agx_isa_encoding encoding, enum agx_opcode op, unsigned src_index, unsigned value, enum agx_size size); diff --git a/src/asahi/compiler/agx_lower_uniform_sources.c b/src/asahi/compiler/agx_lower_uniform_sources.c index d69bb3293f25..4209d8dcd951 100644 --- a/src/asahi/compiler/agx_lower_uniform_sources.c +++ b/src/asahi/compiler/agx_lower_uniform_sources.c @@ -324,11 +324,12 @@ agx3_lower_m5_modified_fma_immediates(agx_context *ctx, agx_instr *I) if (literal.type != AGX_INDEX_IMMEDIATE) continue; - agx_index value = agx_mov_imm( - &b, 32, fui(agx_minifloat_decode(literal.value))); - value.abs = literal.abs; - value.neg = literal.neg; - I->src[s] = value; + uint32_t bits = fui(agx_minifloat_decode(literal.value)); + if (literal.abs) + bits &= 0x7fffffffu; + if (literal.neg) + bits ^= 0x80000000u; + I->src[s] = agx_mov_imm(&b, 32, bits); } } @@ -579,6 +580,12 @@ m5_float_uniforms_to_keep(const agx_context *ctx, const agx_instr *I, return false; } +bool +agx3_m5_float_native_uniform(const agx_context *ctx, const agx_instr *I) +{ + return m5_native_float_uniform(ctx, I); +} + bool agx3_m5_float_accepts_uniform(const agx_context *ctx, const agx_instr *I, unsigned s, unsigned uniform, @@ -687,6 +694,12 @@ m5_native_isel_uniform(const agx_context *ctx, const agx_instr *I) return agx3_isel_encode(AGX_ISA_ENCODING_AGX3C, &d, bytes, &length); } +bool +agx3_m5_isel_native_uniform(const agx_context *ctx, const agx_instr *I) +{ + return !ctx->key->dev.emit_agx3d && m5_native_isel_uniform(ctx, I); +} + bool agx3_m5_isel_accepts_uniform(const agx_context *ctx, const agx_instr *I, unsigned s, unsigned uniform, enum agx_size size) @@ -822,6 +835,19 @@ m5_native_fsel_uniforms(const agx_context *ctx, const agx_instr *I, agx3_alu_fcmp_select32_gpu1(&d, bytes, &length); } +bool +agx3_m5_fcmpsel_native_uniform(const agx_context *ctx, const agx_instr *I) +{ + if (ctx->key->dev.encoding != AGX_ISA_ENCODING_AGX3C || + ctx->key->dev.emit_agx3d || ctx->is_preamble || + I->op != AGX_OPCODE_FCMPSEL || I->nr_srcs != 4) + return false; + struct agx3_fsel32_gpu1_desc d; + uint8_t bytes[AGX3_ALU_MAX_LENGTH], length; + return agx3_m5_fcmpsel_gpu1_desc(ctx, I, true, &d) && + agx3_alu_fcmp_select32_gpu1(&d, bytes, &length); +} + /* The uniform operands an M5 float select reads in place: every one when * that encodes, otherwise the first one that encodes alone. A tied min/max * keeps all of its uniforms or none, so its compared and selected operands diff --git a/src/asahi/compiler/agx_opt_promote_constants.c b/src/asahi/compiler/agx_opt_promote_constants.c index b4f3c9fc9dab..3d952598e6ec 100644 --- a/src/asahi/compiler/agx_opt_promote_constants.c +++ b/src/asahi/compiler/agx_opt_promote_constants.c @@ -22,8 +22,8 @@ struct constant_info { uint64_t value; - /* Number of uses of the constant that could be promoted */ - unsigned nr_promotable_uses; + /* Loop-weighted count of uses of the constant that could be promoted */ + uint64_t weighted_uses; /* Order of the constant's first promotable use in the shader */ unsigned first_use; @@ -41,16 +41,18 @@ struct constant_info { /* * Choosing constants to promote is similar to the 0-1 knapsack problem. We use * a well-known heuristic: sort by benefit divided by size. We approximate - * benefit by use count. + * benefit by loop-weighted use count so hot loop constants take priority over + * cold outside-loop constants when the uniform file fills up (and land in the + * low half of the uniform file where more forms can read them directly). */ -static int +static uint64_t constant_priority(const struct constant_info *const info) { - int size = info->align_16; + unsigned size = info->align_16; assert(size == 1 || size == 2 || size == 4); - int inverse_size = (size == 1) ? 4 : (size == 2) ? 2 : 1; + uint64_t inverse_size = (size == 1) ? 4 : (size == 2) ? 2 : 1; - return info->nr_promotable_uses * inverse_size; + return info->weighted_uses * inverse_size; } static int @@ -62,9 +64,10 @@ priority_compare(const void *A_, const void *B_) /* This is backwards from qsort's documentation, because we want descending * order and qsort returns ascending. */ - int delta = constant_priority(*B) - constant_priority(*A); - if (delta) - return delta; + uint64_t pa = constant_priority(*A); + uint64_t pb = constant_priority(*B); + if (pa != pb) + return pb > pa ? 1 : -1; /* Break ties by first use. The array is gathered from a hash table whose * iteration order depends on the host's pointer width and qsort is not @@ -76,7 +79,7 @@ priority_compare(const void *A_, const void *B_) static void record_use(void *memctx, struct hash_table_u64 *constants, uint64_t imm, - enum agx_size size) + enum agx_size size, uint32_t weight) { struct constant_info *info = _mesa_hash_table_u64_search(constants, imm); @@ -87,7 +90,7 @@ record_use(void *memctx, struct hash_table_u64 *constants, uint64_t imm, _mesa_hash_table_u64_insert(constants, imm, info); } - info->nr_promotable_uses++; + info->weighted_uses += weight; info->align_16 = MAX2(info->align_16, agx_size_align_16(size)); } @@ -121,26 +124,233 @@ agx3b_avoids_half_float_uniform_in_loop(const agx_context *ctx, } } -static bool * -build_in_loop_map(agx_context *ctx, void *memctx, unsigned *nr_blocks_out) +static uint8_t * +build_loop_depth_map(agx_context *ctx, void *memctx, unsigned *nr_blocks_out) { unsigned nr_blocks = 0; agx_foreach_block(ctx, block) nr_blocks = MAX2(nr_blocks, block->index + 1); *nr_blocks_out = nr_blocks; - bool *in_loop = rzalloc_array(memctx, bool, MAX2(nr_blocks, 1u)); + uint8_t *loop_depth = rzalloc_array(memctx, uint8_t, MAX2(nr_blocks, 1u)); + unsigned *max_back = rzalloc_array(memctx, unsigned, MAX2(nr_blocks, 1u)); agx_foreach_block(ctx, block) { agx_foreach_successor(block, succ) { - if (succ->loop_header && succ->index <= block->index) { - for (unsigned i = succ->index; i <= block->index; ++i) - in_loop[i] = true; - } + if (succ->loop_header && succ->index <= block->index) + max_back[succ->index] = MAX2(max_back[succ->index], block->index + 1); } if (block->loop_header) - in_loop[block->index] = true; + max_back[block->index] = MAX2(max_back[block->index], block->index + 1); + } + + for (unsigned h = 0; h < nr_blocks; ++h) { + if (!max_back[h]) + continue; + for (unsigned i = h; i < max_back[h]; ++i) { + if (loop_depth[i] < 4) + loop_depth[i]++; + } + } + return loop_depth; +} + +static bool +m5_fcmpsel_accepts_uniform(const agx_context *ctx, const agx_instr *I, + unsigned s, unsigned uniform, enum agx_size size) +{ + if (ctx->is_preamble || I->op != AGX_OPCODE_FCMPSEL || s != 0 || + I->nr_srcs != 4 || uniform >= 256) + return false; + + agx_instr candidate = *I; + agx_index sources[4]; + memcpy(sources, I->src, sizeof(sources)); + candidate.src = sources; + sources[0] = agx_replace_index(sources[0], agx_uniform(uniform, size)); + unsigned cond; + bool swap; + return agx3_m5_fcmpsel_uniform_cmp0(&candidate, &cond, &swap); +} + +static bool +m5_isel_reverse_icond(enum agx_icond in, enum agx_icond *rev) +{ + switch (in) { + case AGX_ICOND_UEQ: + case AGX_ICOND_SEQ: + *rev = in; + return true; + case AGX_ICOND_ULT: + *rev = AGX_ICOND_UGT; + return true; + case AGX_ICOND_UGT: + *rev = AGX_ICOND_ULT; + return true; + case AGX_ICOND_SLT: + *rev = AGX_ICOND_SGT; + return true; + case AGX_ICOND_SGT: + *rev = AGX_ICOND_SLT; + return true; + default: + return false; + } +} + +static bool +m5_isel_mask_encodes(const agx_context *ctx, const agx_instr *I, unsigned mask, + const unsigned uniforms[4], bool *swap_out, + enum agx_icond *rev_out) +{ + if (!mask) + return false; + const bool swap_cmp = (mask & 1u) != 0; + enum agx_icond rev = I->icond; + if (swap_cmp && (I->src[1].type != AGX_INDEX_NORMAL || + !m5_isel_reverse_icond(I->icond, &rev))) + return false; + + agx_instr candidate = *I; + agx_index sources[4]; + memcpy(sources, I->src, sizeof(sources)); + candidate.src = sources; + for (unsigned s = 0; s < 4; ++s) { + if (mask & (1u << s)) { + if (I->src[s].size != AGX_SIZE_32 || (uniforms[s] & 1u) || + uniforms[s] >= 512) + return false; + sources[s] = + agx_replace_index(sources[s], agx_uniform(uniforms[s], AGX_SIZE_32)); + } + } + if (swap_cmp) { + agx_index tmp = sources[0]; + sources[0] = sources[1]; + sources[1] = tmp; + candidate.icond = rev; + } + if (!agx3_m5_isel_native_uniform(ctx, &candidate)) + return false; + if (swap_out) + *swap_out = swap_cmp; + if (rev_out) + *rev_out = rev; + return true; +} + +static unsigned +m5_isel_best_mask(const agx_context *ctx, const agx_instr *I, unsigned avail, + const unsigned uniforms[4], bool *swap_out, + enum agx_icond *rev_out) +{ + /* Never promote both cmp0 and cmp1 at once (M5 ISEL needs one reg cmp0). */ + if ((avail & 3u) == 3u) + avail &= ~1u; + + unsigned best = 0; + unsigned best_cnt = 0; + for (unsigned sub = avail; sub; sub = (sub - 1) & avail) { + unsigned cnt = util_bitcount(sub); + if (cnt <= best_cnt) + continue; + if (m5_isel_mask_encodes(ctx, I, sub, uniforms, swap_out, rev_out)) { + best = sub; + best_cnt = cnt; + } + } + if (best) + m5_isel_mask_encodes(ctx, I, best, uniforms, swap_out, rev_out); + return best; +} + +static bool +m5_fcmpsel_tied_minmax(const agx_instr *I) +{ + return !I->invert_cond && + (I->fcond == AGX_FCOND_LTN || I->fcond == AGX_FCOND_GTN) && + agx_is_equiv(I->src[0], I->src[2]) && + agx_is_equiv(I->src[1], I->src[3]); +} + +static bool +m5_fcmpsel_normal_f32(uint64_t val) +{ + uint32_t exp = (uint32_t)val & 0x7f800000u; + return exp != 0 && exp != 0x7f800000u; +} + +static bool +m5_fcmpsel_mask_encodes(const agx_context *ctx, const agx_instr *I, + unsigned mask, const unsigned uniforms[4]) +{ + if (!mask) + return false; + agx_instr candidate = *I; + agx_index sources[4]; + memcpy(sources, I->src, sizeof(sources)); + candidate.src = sources; + for (unsigned s = 0; s < 4; ++s) { + if (mask & (1u << s)) { + if (uniforms[s] >= 512 || + (I->src[s].size == AGX_SIZE_32 && (uniforms[s] & 1u))) + return false; + sources[s] = agx_replace_index( + sources[s], agx_uniform(uniforms[s], I->src[s].size)); + } } - return in_loop; + if (agx3_m5_fcmpsel_native_uniform(ctx, &candidate)) + return true; + return mask == 1u && + m5_fcmpsel_accepts_uniform(ctx, I, 0, uniforms[0], I->src[0].size); +} + +static unsigned +m5_fcmpsel_best_mask(const agx_context *ctx, const agx_instr *I, + unsigned avail, const unsigned uniforms[4]) +{ + unsigned best = 0; + unsigned best_cnt = 0; + for (unsigned sub = avail; sub; sub = (sub - 1) & avail) { + unsigned cnt = util_bitcount(sub); + if (cnt <= best_cnt) + continue; + if (m5_fcmpsel_mask_encodes(ctx, I, sub, uniforms)) { + best = sub; + best_cnt = cnt; + } + } + return best; +} + +/* Native M5 FMA admits R*U+U and R.neg*U+U (agx3_uniform_float_encode). + * Probing either constant alone fails for R.neg*C1+C2, so probe the pair + * together just like m5_constant_mad_pair(). */ +static bool +m5_constant_fma_pair(agx_context *ctx, const agx_instr *I, + const BITSET_WORD *is_immediate, unsigned uniform, + unsigned *multiplier) +{ + if (ctx->key->dev.encoding != AGX_ISA_ENCODING_AGX3C || ctx->is_preamble || + I->op != AGX_OPCODE_FFMA || I->nr_srcs != 3 || uniform > 508 || + I->src[0].type != AGX_INDEX_NORMAL || + I->src[1].type != AGX_INDEX_NORMAL || + I->src[2].type != AGX_INDEX_NORMAL || + !BITSET_TEST(is_immediate, I->src[2].value)) + return false; + const bool a = BITSET_TEST(is_immediate, I->src[0].value); + const bool b = BITSET_TEST(is_immediate, I->src[1].value); + if (a == b) + return false; + *multiplier = a ? 0 : 1; + agx_instr candidate = *I; + agx_index sources[3]; + memcpy(sources, I->src, sizeof(sources)); + candidate.src = sources; + sources[*multiplier] = agx_replace_index( + sources[*multiplier], agx_uniform(uniform, I->src[*multiplier].size)); + sources[2] = agx_replace_index(sources[2], + agx_uniform(uniform + 2, I->src[2].size)); + return agx3_m5_float_native_uniform(ctx, &candidate); } /* Reuse the packer's full instruction-domain proof for AGX3 integer operands. @@ -171,6 +381,12 @@ constant_use_accepts_uniform(agx_context *ctx, const agx_instr *I, unsigned s, agx3_m5_isel_accepts_uniform(ctx, I, s, uniform, size)) return true; + /* M5 32-bit float compare-select reads an aligned low 32-bit uniform as its + * first comparand when selecting between zero and a register. */ + if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + m5_fcmpsel_accepts_uniform(ctx, I, s, uniform, size)) + return true; + if (I->nr_srcs > 3 || uniform >= AGX_NUM_UNIFORMS) return false; @@ -246,43 +462,137 @@ pass(agx_context *ctx, void *memctx, bool late_m5_float) * (FFMA, FADD, FMUL, FCMPSEL) refuse an odd index. Probing the raw count * after a preamble that ends on a 16-bit export refused every such use. */ - const unsigned probe_uniform = + unsigned probe_uniform = ctx->key->dev.encoding != AGX_ISA_ENCODING_AGX2 ? ALIGN_POT(ctx->out->push_count, 2) : ctx->out->push_count; + if (late_m5_float && probe_uniform >= 512 && ctx->out->rodata.size_16 >= 2) + probe_uniform = ctx->out->rodata.base_uniform; + const unsigned pair_probe = + late_m5_float && ctx->out->rodata.size_16 >= 2 && probe_uniform > 508 + ? MIN2(ctx->out->rodata.base_uniform, 508u) + : probe_uniform; const unsigned push_before = ctx->out->push_count; unsigned nr_blocks = 0; - bool *in_loop = ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B - ? build_in_loop_map(ctx, memctx, &nr_blocks) - : NULL; + uint8_t *loop_depth = build_loop_depth_map(ctx, memctx, &nr_blocks); /* Gather constant definitions and use */ agx_foreach_block(ctx, block) { - const bool block_in_loop = - in_loop && block->index < nr_blocks && in_loop[block->index]; + const uint8_t depth = + block->index < nr_blocks ? loop_depth[block->index] : 0; + const bool block_in_loop = depth > 0; + const uint32_t weight = 1u << (3u * depth); agx_foreach_instr_in_block(block, I) { if (I->op == AGX_OPCODE_MOV_IMM) { assert(I->dest[0].type == AGX_INDEX_NORMAL); BITSET_SET(is_immediate, I->dest[0].value); values[I->dest[0].value] = I->imm; } else if (late_m5_float) { + unsigned multiplier; + if (m5_constant_fma_pair(ctx, I, is_immediate, pair_probe, + &multiplier)) { + record_use(memctx, constants, values[I->src[multiplier].value], + I->src[multiplier].size, weight); + record_use(memctx, constants, values[I->src[2].value], + I->src[2].size, weight); + continue; + } agx_foreach_ssa_src(I, s) { if (BITSET_TEST(is_immediate, I->src[s].value) && late_m5_float_use(ctx, I, s, probe_uniform, I->src[s].size)) record_use(memctx, constants, values[I->src[s].value], - I->src[s].size); + I->src[s].size, weight); } } else { unsigned multiplier; if (m5_constant_mad_pair(ctx, I, is_immediate, probe_uniform, + &multiplier) || + m5_constant_fma_pair(ctx, I, is_immediate, probe_uniform, &multiplier)) { record_use(memctx, constants, values[I->src[multiplier].value], - I->src[multiplier].size); + I->src[multiplier].size, weight); record_use(memctx, constants, values[I->src[2].value], - I->src[2].size); + I->src[2].size, weight); continue; } + if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + !ctx->key->dev.emit_agx3d && !ctx->is_preamble && + I->op == AGX_OPCODE_ICMPSEL && I->nr_srcs == 4 && + probe_uniform < 512) { + unsigned avail = 0; + unsigned uniforms[4] = {probe_uniform, probe_uniform, + probe_uniform, probe_uniform}; + agx_foreach_ssa_src(I, s) { + if (BITSET_TEST(is_immediate, I->src[s].value)) + avail |= 1u << s; + } + unsigned best = + m5_isel_best_mask(ctx, I, avail, uniforms, NULL, NULL); + if (best) { + u_foreach_bit(s, best) { + record_use(memctx, constants, values[I->src[s].value], + I->src[s].size, weight); + } + continue; + } + } + if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + !ctx->key->dev.emit_agx3d && !ctx->is_preamble && + I->op == AGX_OPCODE_FCMPSEL && I->nr_srcs == 4 && + probe_uniform < 512) { + if (m5_fcmpsel_tied_minmax(I)) { + const bool imm0 = I->src[0].type == AGX_INDEX_NORMAL && + BITSET_TEST(is_immediate, I->src[0].value); + const bool imm1 = I->src[1].type == AGX_INDEX_NORMAL && + BITSET_TEST(is_immediate, I->src[1].value); + if (imm1 && !imm0) { + unsigned uniforms[4] = {0, probe_uniform, 0, + probe_uniform}; + if (m5_fcmpsel_mask_encodes(ctx, I, 0xa, uniforms)) { + record_use(memctx, constants, values[I->src[1].value], + I->src[1].size, weight); + continue; + } + } else if (imm0 && !imm1 && + !ctx->agx3_reg_float_consts && + I->src[0].size == AGX_SIZE_32 && + !I->src[0].abs && !I->src[0].neg && + I->src[1].type == AGX_INDEX_NORMAL && + m5_fcmpsel_normal_f32( + values[I->src[0].value])) { + agx_instr swapped = *I; + agx_index ssrc[4] = {I->src[1], I->src[0], I->src[3], + I->src[2]}; + swapped.src = ssrc; + unsigned uniforms[4] = {0, probe_uniform, 0, + probe_uniform}; + if (m5_fcmpsel_mask_encodes(ctx, &swapped, 0xa, + uniforms)) { + record_use(memctx, constants, values[I->src[0].value], + I->src[0].size, weight); + continue; + } + } + } else { + unsigned avail = 0; + unsigned uniforms[4] = {probe_uniform, probe_uniform, + probe_uniform, probe_uniform}; + agx_foreach_ssa_src(I, s) { + if (BITSET_TEST(is_immediate, I->src[s].value)) + avail |= 1u << s; + } + unsigned best = + m5_fcmpsel_best_mask(ctx, I, avail, uniforms); + if (best) { + u_foreach_bit(s, best) { + record_use(memctx, constants, values[I->src[s].value], + I->src[s].size, weight); + } + continue; + } + } + } agx_foreach_ssa_src(I, s) { if (BITSET_TEST(is_immediate, I->src[s].value) && constant_use_accepts_uniform(ctx, I, s, probe_uniform, @@ -290,7 +600,7 @@ pass(agx_context *ctx, void *memctx, bool late_m5_float) block_in_loop)) { record_use(memctx, constants, values[I->src[s].value], - I->src[s].size); + I->src[s].size, weight); } } } @@ -323,26 +633,49 @@ pass(agx_context *ctx, void *memctx, bool late_m5_float) : AGX_NUM_UNIFORMS; /* Promote as many constants as we can */ + bool has_rodata = late_m5_float && ctx->out->rodata.size_16; for (unsigned i = 0; i < flat_count; ++i) { struct constant_info *info = flat[i]; - assert(info->nr_promotable_uses > 0); + assert(info->weighted_uses > 0); + + /* In the late M5 round, reuse constants already uploaded in rodata from + * the ordinary round before allocating new uniform slots. */ + if (late_m5_float && ctx->out->rodata.size_16) { + const unsigned base = ctx->out->rodata.base_uniform; + const unsigned size_16 = ctx->out->rodata.size_16; + bool reused = false; + for (unsigned k = 0; k + info->align_16 <= size_16; + k += info->align_16) { + if (((base + k) & (info->align_16 - 1)) == 0 && + memcmp(&ctx->rodata[k], &info->value, info->align_16 * 2) == + 0) { + info->uniform = base + k; + info->promoted = true; + reused = true; + break; + } + } + if (reused) + continue; + } /* Try to assign a uniform */ /* Native Ioto publishes 32-bit uniform words. Align the beginning of * the constant range even when its first constant is only 16 bits. */ unsigned range_align = ctx->key->dev.encoding != AGX_ISA_ENCODING_AGX2 && - i == 0 ? MAX2(info->align_16, 2) : info->align_16; + !has_rodata ? MAX2(info->align_16, 2) : info->align_16; unsigned uniform = ALIGN_POT(ctx->out->push_count, range_align); unsigned new_count = uniform + info->align_16; if (new_count > max_uniforms) - break; + continue; /* Set the base for the first constant. This avoids uploading an extra * zero if we needed to pad, compared to doing this before the loop. Same * shader code, but slightly more efficient USC record. */ - if (i == 0 && !(late_m5_float && ctx->out->rodata.size_16)) { + if (!has_rodata) { ctx->out->rodata.base_uniform = uniform; + has_rodata = true; } info->uniform = uniform; @@ -406,9 +739,36 @@ pass(agx_context *ctx, void *memctx, bool late_m5_float) /* Promote in the IR */ agx_foreach_block(ctx, block) { const bool block_in_loop = - in_loop && block->index < nr_blocks && in_loop[block->index]; + block->index < nr_blocks && loop_depth[block->index] > 0; agx_foreach_instr_in_block(block, I) { if (late_m5_float) { + unsigned multiplier; + if (m5_constant_fma_pair(ctx, I, is_immediate, pair_probe, + &multiplier)) { + struct constant_info *m = _mesa_hash_table_u64_search( + constants, values[I->src[multiplier].value]); + struct constant_info *c = _mesa_hash_table_u64_search( + constants, values[I->src[2].value]); + if (m && c && m->promoted && c->promoted) { + agx_instr candidate = *I; + agx_index sources[3]; + memcpy(sources, I->src, sizeof(sources)); + candidate.src = sources; + sources[multiplier] = agx_replace_index( + sources[multiplier], + agx_uniform(m->uniform, I->src[multiplier].size)); + sources[2] = agx_replace_index( + sources[2], agx_uniform(c->uniform, I->src[2].size)); + if (agx3_m5_float_native_uniform(ctx, &candidate)) { + agx_replace_src( + I, multiplier, + agx_uniform(m->uniform, I->src[multiplier].size)); + agx_replace_src(I, 2, + agx_uniform(c->uniform, I->src[2].size)); + continue; + } + } + } agx_foreach_ssa_src(I, s) { if (!BITSET_TEST(is_immediate, I->src[s].value)) continue; @@ -436,6 +796,140 @@ pass(agx_context *ctx, void *memctx, bool late_m5_float) continue; } } + if (m5_constant_fma_pair(ctx, I, is_immediate, probe_uniform, + &multiplier)) { + struct constant_info *m = _mesa_hash_table_u64_search( + constants, values[I->src[multiplier].value]); + struct constant_info *c = _mesa_hash_table_u64_search( + constants, values[I->src[2].value]); + if (m && c && m->promoted && c->promoted) { + agx_instr candidate = *I; + agx_index sources[3]; + memcpy(sources, I->src, sizeof(sources)); + candidate.src = sources; + sources[multiplier] = agx_replace_index( + sources[multiplier], + agx_uniform(m->uniform, I->src[multiplier].size)); + sources[2] = agx_replace_index( + sources[2], agx_uniform(c->uniform, I->src[2].size)); + if (agx3_m5_float_native_uniform(ctx, &candidate)) { + agx_replace_src( + I, multiplier, + agx_uniform(m->uniform, I->src[multiplier].size)); + agx_replace_src(I, 2, + agx_uniform(c->uniform, I->src[2].size)); + continue; + } + } + } + if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + !ctx->key->dev.emit_agx3d && !ctx->is_preamble && + I->op == AGX_OPCODE_ICMPSEL && I->nr_srcs == 4) { + unsigned avail = 0; + unsigned uniforms[4] = {0}; + agx_foreach_ssa_src(I, s) { + if (!BITSET_TEST(is_immediate, I->src[s].value)) + continue; + struct constant_info *info = _mesa_hash_table_u64_search( + constants, values[I->src[s].value]); + if (info && info->promoted) { + avail |= 1u << s; + uniforms[s] = info->uniform; + } + } + bool swap_cmp = false; + enum agx_icond rev = I->icond; + unsigned best = + m5_isel_best_mask(ctx, I, avail, uniforms, &swap_cmp, &rev); + if (best) { + u_foreach_bit(s, best) { + agx_replace_src(I, s, + agx_uniform(uniforms[s], I->src[s].size)); + } + if (swap_cmp) { + agx_index tmp = I->src[0]; + I->src[0] = I->src[1]; + I->src[1] = tmp; + I->icond = rev; + } + continue; + } + } + if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + !ctx->key->dev.emit_agx3d && !ctx->is_preamble && + I->op == AGX_OPCODE_FCMPSEL && I->nr_srcs == 4) { + if (m5_fcmpsel_tied_minmax(I)) { + const bool imm0 = I->src[0].type == AGX_INDEX_NORMAL && + BITSET_TEST(is_immediate, I->src[0].value); + const bool imm1 = I->src[1].type == AGX_INDEX_NORMAL && + BITSET_TEST(is_immediate, I->src[1].value); + if (imm1 && !imm0) { + struct constant_info *info = _mesa_hash_table_u64_search( + constants, values[I->src[1].value]); + if (info && info->promoted) { + unsigned uniforms[4] = {0, info->uniform, 0, + info->uniform}; + if (m5_fcmpsel_mask_encodes(ctx, I, 0xa, uniforms)) { + agx_replace_src( + I, 1, agx_uniform(info->uniform, I->src[1].size)); + agx_replace_src( + I, 3, agx_uniform(info->uniform, I->src[3].size)); + continue; + } + } + } else if (imm0 && !imm1 && !ctx->agx3_reg_float_consts && + I->src[0].size == AGX_SIZE_32 && + !I->src[0].abs && !I->src[0].neg && + I->src[1].type == AGX_INDEX_NORMAL && + m5_fcmpsel_normal_f32( + values[I->src[0].value])) { + struct constant_info *info = _mesa_hash_table_u64_search( + constants, values[I->src[0].value]); + if (info && info->promoted) { + agx_instr swapped = *I; + agx_index ssrc[4] = {I->src[1], I->src[0], I->src[3], + I->src[2]}; + swapped.src = ssrc; + unsigned uniforms[4] = {0, info->uniform, 0, + info->uniform}; + if (m5_fcmpsel_mask_encodes(ctx, &swapped, 0xa, + uniforms)) { + agx_index tmp0 = I->src[0], tmp2 = I->src[2]; + I->src[0] = I->src[1]; + I->src[1] = tmp0; + I->src[2] = I->src[3]; + I->src[3] = tmp2; + agx_replace_src( + I, 1, agx_uniform(info->uniform, I->src[1].size)); + agx_replace_src( + I, 3, agx_uniform(info->uniform, I->src[3].size)); + continue; + } + } + } + } else { + unsigned avail = 0; + unsigned uniforms[4] = {0}; + agx_foreach_ssa_src(I, s) { + if (!BITSET_TEST(is_immediate, I->src[s].value)) + continue; + struct constant_info *info = _mesa_hash_table_u64_search( + constants, values[I->src[s].value]); + if (info && info->promoted) { + avail |= 1u << s; + uniforms[s] = info->uniform; + } + } + unsigned best = m5_fcmpsel_best_mask(ctx, I, avail, uniforms); + if (best) { + u_foreach_bit(s, best) { + agx_replace_src(I, s, + agx_uniform(uniforms[s], I->src[s].size)); + } + continue; + } + } + } agx_foreach_ssa_src(I, s) { if (!BITSET_TEST(is_immediate, I->src[s].value)) continue; diff --git a/src/asahi/compiler/test/test-agx3-uniform-alu.cpp b/src/asahi/compiler/test/test-agx3-uniform-alu.cpp index f470ecb873cc..a12b71330acc 100644 --- a/src/asahi/compiler/test/test-agx3-uniform-alu.cpp +++ b/src/asahi/compiler/test/test-agx3-uniform-alu.cpp @@ -2494,3 +2494,222 @@ TEST(AGX3UniformALU, Agx3bIselImmComparandKeepsUniformSelects) EXPECT_EQ(isel->src[3].type, AGX_INDEX_UNIFORM); ralloc_free(mem_ctx); } + +TEST(AGX3UniformALU, M5LoopWeightedConstantPromotionAndRodataReuse) +{ + /* 1. With only 1 f32 uniform slot left (push_count = 510), a constant used + * once inside a loop beats a constant used 3 times outside the loop. */ + { + void *mem = ralloc_context(NULL); + agx_shader_key key = {}; + key.dev.encoding = AGX_ISA_ENCODING_AGX3C; + key.dev.chip = AGX_CHIP_G17S; + key.promote_constants = true; + agx_builder *b = agx_test_builder(mem); + agx_context *ctx = b->shader; + ctx->key = &key; + ctx->out = rzalloc(mem, struct agx_shader_info); + ctx->out->push_count = 510; + ctx->stage = MESA_SHADER_COMPUTE; + + agx_block *entry = agx_start_block(ctx); + agx_block *loop = agx_test_block(ctx); + loop->loop_header = true; + agx_block_add_successor(entry, loop); + agx_block_add_successor(loop, loop); + entry->index = 0; + loop->index = 1; + + agx_index x = agx_temp(ctx, AGX_SIZE_32); + agx_index cold = agx_mov_imm(b, 32, fui(3.1415926f)); + agx_instr *c1 = agx_fmul_to(b, agx_temp(ctx, AGX_SIZE_32), x, cold); + agx_instr *c2 = agx_fmul_to(b, agx_temp(ctx, AGX_SIZE_32), c1->dest[0], cold); + agx_instr *c3 = agx_fmul_to(b, agx_temp(ctx, AGX_SIZE_32), c2->dest[0], cold); + + b->cursor = agx_after_block(loop); + agx_index hot = agx_mov_imm(b, 32, fui(2.7182818f)); + agx_instr *h1 = agx_fmul_to(b, agx_temp(ctx, AGX_SIZE_32), c3->dest[0], hot); + + agx_opt_promote_constants(ctx); + EXPECT_EQ(h1->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(h1->src[1].value, 510u); + EXPECT_EQ(c1->src[1].type, AGX_INDEX_NORMAL); + EXPECT_EQ(ctx->out->push_count, 512u); + + /* 2. Late M5 float round reuses already-uploaded rodata constants even + * when push_count has already reached 512, including FMA pairs. */ + agx_index hot2 = agx_mov_imm(b, 32, fui(2.7182818f)); + agx_instr *h2 = agx_fadd_to(b, agx_temp(ctx, AGX_SIZE_32), h1->dest[0], hot2); + agx_index hot3 = agx_mov_imm(b, 32, fui(2.7182818f)); + agx_index hot4 = agx_mov_imm(b, 32, fui(2.7182818f)); + agx_instr *h3 = agx_ffma_to(b, agx_temp(ctx, AGX_SIZE_32), + agx_neg(h2->dest[0]), hot3, hot4); + agx_opt_promote_m5_float_constants(ctx); + EXPECT_EQ(h2->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(h2->src[1].value, 510u); + EXPECT_EQ(h3->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(h3->src[1].value, 510u); + EXPECT_EQ(h3->src[2].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(h3->src[2].value, 510u); + EXPECT_EQ(ctx->out->push_count, 512u); + + ralloc_free(mem); + } +} + +TEST(AGX3UniformALU, M5PromoteIselLeftConstantAndFcmpselAndFmaPair) +{ + /* 1. ICMPSEL with constant in src[0] swaps comparands and reverses icond on + * M5 (and leaves G18G untouched). */ + for (unsigned g18 = 0; g18 < 2; ++g18) { + void *mem = ralloc_context(NULL); + agx_shader_key key = {}; + key.dev.encoding = AGX_ISA_ENCODING_AGX3C; + key.dev.chip = g18 ? AGX_CHIP_G18G : AGX_CHIP_G17S; + key.dev.emit_agx3d = g18 != 0; + key.promote_constants = true; + agx_builder *b = agx_test_builder(mem); + b->shader->key = &key; + b->shader->out = rzalloc(mem, struct agx_shader_info); + b->shader->out->push_count = 16; + b->shader->stage = MESA_SHADER_COMPUTE; + + agx_index bound = agx_mov_imm(b, 32, 0x240u); + agx_index x = agx_temp(b->shader, AGX_SIZE_32); + agx_index t = agx_temp(b->shader, AGX_SIZE_32); + agx_index f = agx_temp(b->shader, AGX_SIZE_32); + agx_instr *sel = agx_icmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + bound, x, t, f, AGX_ICOND_ULT); + agx_opt_promote_constants(b->shader); + if (!g18) { + EXPECT_EQ(sel->src[0].type, AGX_INDEX_NORMAL); + EXPECT_EQ(sel->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(sel->icond, AGX_ICOND_UGT); + } else { + EXPECT_EQ(sel->src[0].type, AGX_INDEX_NORMAL); + EXPECT_EQ(sel->src[1].type, AGX_INDEX_NORMAL); + EXPECT_EQ(sel->icond, AGX_ICOND_ULT); + } + ralloc_free(mem); + } + + /* 2. FCMPSEL general uniform, tied fmin/fmax in both orders, non-swap + * guard for ±0.0f / subnormal / NaN, and retry-path preservation. */ + { + void *mem = ralloc_context(NULL); + agx_shader_key key = {}; + key.dev.encoding = AGX_ISA_ENCODING_AGX3C; + key.dev.chip = AGX_CHIP_G17S; + key.promote_constants = true; + agx_builder *b = agx_test_builder(mem); + b->shader->key = &key; + b->shader->out = rzalloc(mem, struct agx_shader_info); + b->shader->out->push_count = 16; + b->shader->stage = MESA_SHADER_COMPUTE; + + agx_index x = agx_temp(b->shader, AGX_SIZE_32); + agx_index t = agx_temp(b->shader, AGX_SIZE_32); + agx_index f = agx_temp(b->shader, AGX_SIZE_32); + agx_index c9 = agx_mov_imm(b, 32, fui(0.9f)); + agx_index c04 = agx_mov_imm(b, 32, fui(0.04f)); + agx_index cz = agx_mov_imm(b, 32, 0u); + agx_index csub = agx_mov_imm(b, 32, 0x00000001u); + agx_index cnan = agx_mov_imm(b, 32, 0x7fc00000u); + + agx_instr *gen = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + x, c9, t, f, AGX_FCOND_LT); + agx_instr *max_r = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + x, c04, x, c04, AGX_FCOND_GTN); + agx_instr *max_l = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + c04, x, c04, x, AGX_FCOND_GTN); + agx_instr *max_z = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + cz, x, cz, x, AGX_FCOND_GTN); + agx_instr *max_sub = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + csub, x, csub, x, AGX_FCOND_GTN); + agx_instr *max_nan = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + cnan, x, cnan, x, AGX_FCOND_GTN); + + agx_opt_promote_constants(b->shader); + EXPECT_EQ(gen->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(max_r->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(max_r->src[3].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(max_r->src[1].value, max_r->src[3].value); + EXPECT_TRUE(agx_is_equiv(max_l->src[0], x)); + EXPECT_EQ(max_l->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(max_l->src[3].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(max_l->src[1].value, max_l->src[3].value); + EXPECT_TRUE(agx_is_equiv(max_z->src[0], cz)); + EXPECT_TRUE(agx_is_equiv(max_z->src[1], x)); + EXPECT_TRUE(agx_is_equiv(max_sub->src[0], csub)); + EXPECT_TRUE(agx_is_equiv(max_sub->src[1], x)); + EXPECT_TRUE(agx_is_equiv(max_nan->src[0], cnan)); + EXPECT_TRUE(agx_is_equiv(max_nan->src[1], x)); + ralloc_free(mem); + + /* On the M5 retry path (agx3_reg_float_consts), do not swap tied + * GTN(C, x, C, x) so retry's LE rewrite still returns C for x = NaN. */ + mem = ralloc_context(NULL); + b = agx_test_builder(mem); + b->shader->key = &key; + b->shader->out = rzalloc(mem, struct agx_shader_info); + b->shader->out->push_count = 16; + b->shader->stage = MESA_SHADER_COMPUTE; + b->shader->agx3_reg_float_consts = true; + + x = agx_temp(b->shader, AGX_SIZE_32); + c04 = agx_mov_imm(b, 32, fui(0.04f)); + agx_instr *retry_max = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_32), + c04, x, c04, x, AGX_FCOND_GTN); + agx_opt_promote_constants(b->shader); + EXPECT_TRUE(agx_is_equiv(retry_max->src[0], c04)); + EXPECT_TRUE(agx_is_equiv(retry_max->src[1], x)); + agx_lower_g17g_retry_float_operands(b->shader); + EXPECT_EQ(retry_max->fcond, AGX_FCOND_LE); + EXPECT_TRUE(agx_is_equiv(retry_max->src[0], c04)); + EXPECT_TRUE(agx_is_equiv(retry_max->src[1], x)); + EXPECT_TRUE(agx_is_equiv(retry_max->src[2], x)); + EXPECT_TRUE(agx_is_equiv(retry_max->src[3], c04)); + ralloc_free(mem); + } + + /* 3. Paired M5 FFMA R.neg * C1 + C2 promotes both constants, and modified + * FMA immediate lowering folds .neg into the literal so Round 2 promotes + * R * R + (-1.0f) cleanly. */ + { + void *mem = ralloc_context(NULL); + agx_shader_key key = {}; + key.dev.encoding = AGX_ISA_ENCODING_AGX3C; + key.dev.chip = AGX_CHIP_G17S; + key.promote_constants = true; + agx_builder *b = agx_test_builder(mem); + b->shader->key = &key; + b->shader->out = rzalloc(mem, struct agx_shader_info); + b->shader->out->push_count = 16; + b->shader->stage = MESA_SHADER_COMPUTE; + + agx_index x = agx_temp(b->shader, AGX_SIZE_32); + agx_index y = agx_temp(b->shader, AGX_SIZE_32); + agx_index c1 = agx_mov_imm(b, 32, fui(0.107509f)); + agx_index c2 = agx_mov_imm(b, 32, fui(0.318310f)); + agx_instr *pair = agx_ffma_to(b, agx_temp(b->shader, AGX_SIZE_32), + agx_neg(x), c1, c2); + agx_instr *mod_imm = agx_ffma_to(b, agx_temp(b->shader, AGX_SIZE_32), + x, y, agx_neg(agx_immediate(0x30))); + + agx_opt_promote_constants(b->shader); + EXPECT_EQ(pair->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(pair->src[2].type, AGX_INDEX_UNIFORM); + + agx_lower_uniform_sources(b->shader); + EXPECT_FALSE(mod_imm->src[2].neg); + agx_opt_promote_m5_float_constants(b->shader); + ASSERT_EQ(mod_imm->src[2].type, AGX_INDEX_UNIFORM); + uint32_t bits = 0; + memcpy(&bits, + &b->shader->rodata[mod_imm->src[2].value - + b->shader->out->rodata.base_uniform], + sizeof(bits)); + EXPECT_EQ(bits, fui(-1.0f)); + ralloc_free(mem); + } +} From c8e980ef281c06081a9f01923a1bc02c1c93c12e Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 21:00:34 -0500 Subject: [PATCH 2/2] asahi: promote M5 select constants in one place The mask searches cover every compare-select operand M5 reads from the uniform file, so the per-source hooks were unreachable there. The FCMPSEL one still promoted a first comparand on G18G, which keeps its own select forms. Remove both hooks. Two constants of one value share a uniform, which could turn an unordered LTN/GTN select into a tied min/max with a different NaN result. Refuse that, using the shared tied min/max predicate. Test every reversed ICMPSEL condition, the G18G comparand, the NaN case, and that late rodata reuse compares the whole word. Signed-off-by: DjDeveloperr --- src/asahi/compiler/agx_compiler.h | 6 -- .../compiler/agx_lower_uniform_sources.c | 18 ---- .../compiler/agx_opt_promote_constants.c | 52 ++--------- .../compiler/test/test-agx3-uniform-alu.cpp | 87 +++++++++++++++++++ 4 files changed, 94 insertions(+), 69 deletions(-) diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 29012175eb0f..43c7f1ce3e36 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -1350,12 +1350,6 @@ bool agx3_m5_float_accepts_uniform(const agx_context *ctx, const agx_instr *I, * (agx3_isel_encode). */ bool agx3_m5_isel_native_uniform(const agx_context *ctx, const agx_instr *I); -/* Whether replacing source s of an M5 main-program 32-bit ICMPSEL with - * `uniform` gives a native uniform-operand form (agx3_isel_encode). */ -bool agx3_m5_isel_accepts_uniform(const agx_context *ctx, const agx_instr *I, - unsigned s, unsigned uniform, - enum agx_size size); - /* Whether an M5 main-program 32-bit FCMPSEL is a native uniform-operand form * (agx3_alu_fcmp_select32_gpu1). */ bool agx3_m5_fcmpsel_native_uniform(const agx_context *ctx, const agx_instr *I); diff --git a/src/asahi/compiler/agx_lower_uniform_sources.c b/src/asahi/compiler/agx_lower_uniform_sources.c index 4209d8dcd951..4252e98503c5 100644 --- a/src/asahi/compiler/agx_lower_uniform_sources.c +++ b/src/asahi/compiler/agx_lower_uniform_sources.c @@ -700,24 +700,6 @@ agx3_m5_isel_native_uniform(const agx_context *ctx, const agx_instr *I) return !ctx->key->dev.emit_agx3d && m5_native_isel_uniform(ctx, I); } -bool -agx3_m5_isel_accepts_uniform(const agx_context *ctx, const agx_instr *I, - unsigned s, unsigned uniform, enum agx_size size) -{ - if (ctx->key->dev.encoding != AGX_ISA_ENCODING_AGX3C || - ctx->key->dev.emit_agx3d || ctx->is_preamble || - I->op != AGX_OPCODE_ICMPSEL || s == 0 || s >= 4 || I->nr_srcs != 4 || - size != AGX_SIZE_32 || (uniform & 1) || uniform >= 512) - return false; - - agx_instr candidate = *I; - agx_index sources[4]; - memcpy(sources, I->src, sizeof(sources)); - candidate.src = sources; - sources[s] = agx_replace_index(sources[s], agx_uniform(uniform, size)); - return m5_native_isel_uniform(ctx, &candidate); -} - /* On AGX3C, native uniform ISEL takes a uniform right comparand (cmp1_uniform) * beside a 32-bit register left comparand. Canonicalize `u op r` to `r op' u` * when that yields a native form. */ diff --git a/src/asahi/compiler/agx_opt_promote_constants.c b/src/asahi/compiler/agx_opt_promote_constants.c index 3d952598e6ec..ac9925e33529 100644 --- a/src/asahi/compiler/agx_opt_promote_constants.c +++ b/src/asahi/compiler/agx_opt_promote_constants.c @@ -154,24 +154,6 @@ build_loop_depth_map(agx_context *ctx, void *memctx, unsigned *nr_blocks_out) return loop_depth; } -static bool -m5_fcmpsel_accepts_uniform(const agx_context *ctx, const agx_instr *I, - unsigned s, unsigned uniform, enum agx_size size) -{ - if (ctx->is_preamble || I->op != AGX_OPCODE_FCMPSEL || s != 0 || - I->nr_srcs != 4 || uniform >= 256) - return false; - - agx_instr candidate = *I; - agx_index sources[4]; - memcpy(sources, I->src, sizeof(sources)); - candidate.src = sources; - sources[0] = agx_replace_index(sources[0], agx_uniform(uniform, size)); - unsigned cond; - bool swap; - return agx3_m5_fcmpsel_uniform_cmp0(&candidate, &cond, &swap); -} - static bool m5_isel_reverse_icond(enum agx_icond in, enum agx_icond *rev) { @@ -263,15 +245,6 @@ m5_isel_best_mask(const agx_context *ctx, const agx_instr *I, unsigned avail, return best; } -static bool -m5_fcmpsel_tied_minmax(const agx_instr *I) -{ - return !I->invert_cond && - (I->fcond == AGX_FCOND_LTN || I->fcond == AGX_FCOND_GTN) && - agx_is_equiv(I->src[0], I->src[2]) && - agx_is_equiv(I->src[1], I->src[3]); -} - static bool m5_fcmpsel_normal_f32(uint64_t val) { @@ -298,10 +271,11 @@ m5_fcmpsel_mask_encodes(const agx_context *ctx, const agx_instr *I, sources[s], agx_uniform(uniforms[s], I->src[s].size)); } } - if (agx3_m5_fcmpsel_native_uniform(ctx, &candidate)) - return true; - return mask == 1u && - m5_fcmpsel_accepts_uniform(ctx, I, 0, uniforms[0], I->src[0].size); + /* Two constants of one value share a uniform. That must not turn an + * unordered select into a tied min/max, which treats NaN differently. */ + return agx_fcmpsel_is_tied_minmax(&candidate) == + agx_fcmpsel_is_tied_minmax(I) && + agx3_m5_fcmpsel_native_uniform(ctx, &candidate); } static unsigned @@ -375,18 +349,6 @@ constant_use_accepts_uniform(agx_context *ctx, const agx_instr *I, unsigned s, agx3_m5_float_accepts_uniform(ctx, I, s, uniform, size)) return true; - /* M5 32-bit integer compare-select reads an aligned 32-bit uniform in its - * right comparand and/or either selected operand natively. */ - if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && - agx3_m5_isel_accepts_uniform(ctx, I, s, uniform, size)) - return true; - - /* M5 32-bit float compare-select reads an aligned low 32-bit uniform as its - * first comparand when selecting between zero and a register. */ - if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && - m5_fcmpsel_accepts_uniform(ctx, I, s, uniform, size)) - return true; - if (I->nr_srcs > 3 || uniform >= AGX_NUM_UNIFORMS) return false; @@ -541,7 +503,7 @@ pass(agx_context *ctx, void *memctx, bool late_m5_float) !ctx->key->dev.emit_agx3d && !ctx->is_preamble && I->op == AGX_OPCODE_FCMPSEL && I->nr_srcs == 4 && probe_uniform < 512) { - if (m5_fcmpsel_tied_minmax(I)) { + if (agx_fcmpsel_is_tied_minmax(I)) { const bool imm0 = I->src[0].type == AGX_INDEX_NORMAL && BITSET_TEST(is_immediate, I->src[0].value); const bool imm1 = I->src[1].type == AGX_INDEX_NORMAL && @@ -858,7 +820,7 @@ pass(agx_context *ctx, void *memctx, bool late_m5_float) if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && !ctx->key->dev.emit_agx3d && !ctx->is_preamble && I->op == AGX_OPCODE_FCMPSEL && I->nr_srcs == 4) { - if (m5_fcmpsel_tied_minmax(I)) { + if (agx_fcmpsel_is_tied_minmax(I)) { const bool imm0 = I->src[0].type == AGX_INDEX_NORMAL && BITSET_TEST(is_immediate, I->src[0].value); const bool imm1 = I->src[1].type == AGX_INDEX_NORMAL && diff --git a/src/asahi/compiler/test/test-agx3-uniform-alu.cpp b/src/asahi/compiler/test/test-agx3-uniform-alu.cpp index a12b71330acc..8e811c7f0c1a 100644 --- a/src/asahi/compiler/test/test-agx3-uniform-alu.cpp +++ b/src/asahi/compiler/test/test-agx3-uniform-alu.cpp @@ -2713,3 +2713,90 @@ TEST(AGX3UniformALU, M5PromoteIselLeftConstantAndFcmpselAndFmaPair) ralloc_free(mem); } } + +TEST(AGX3UniformALU, M5PromotedSelectsKeepTheirMeaning) +{ + struct env { + void *mem; + agx_shader_key key; + agx_builder *b; + agx_index x, t, f; + + env(bool g18g, unsigned push_count) + { + mem = ralloc_context(NULL); + key = {}; + key.dev.encoding = AGX_ISA_ENCODING_AGX3C; + key.dev.chip = g18g ? AGX_CHIP_G18G : AGX_CHIP_G17S; + key.dev.emit_agx3d = g18g; + key.promote_constants = true; + b = agx_test_builder(mem); + b->shader->key = &key; + b->shader->out = rzalloc(mem, struct agx_shader_info); + b->shader->out->push_count = push_count; + b->shader->stage = MESA_SHADER_COMPUTE; + x = agx_temp(b->shader, AGX_SIZE_32); + t = agx_temp(b->shader, AGX_SIZE_32); + f = agx_temp(b->shader, AGX_SIZE_32); + } + ~env() { ralloc_free(mem); } + agx_index dst() { return agx_temp(b->shader, AGX_SIZE_32); } + }; + + /* c op x becomes x op' c for every condition, signed ones included. */ + const std::array, 6> conds = {{ + {AGX_ICOND_UEQ, AGX_ICOND_UEQ}, {AGX_ICOND_SEQ, AGX_ICOND_SEQ}, + {AGX_ICOND_ULT, AGX_ICOND_UGT}, {AGX_ICOND_UGT, AGX_ICOND_ULT}, + {AGX_ICOND_SLT, AGX_ICOND_SGT}, {AGX_ICOND_SGT, AGX_ICOND_SLT}, + }}; + for (const auto &c : conds) { + env e(false, 16); + agx_instr *I = agx_icmpsel_to(e.b, e.dst(), agx_mov_imm(e.b, 32, 0x240u), + e.x, e.t, e.f, c[0]); + agx_opt_promote_constants(e.b->shader); + EXPECT_TRUE(agx_is_equiv(I->src[0], e.x)); + EXPECT_EQ(I->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(I->icond, c[1]); + } + + /* The comparand moves into the uniform file on M5 and stays in a register + * on G18G, which has its own compare-select forms. */ + for (bool g18g : {false, true}) { + env e(g18g, 16); + agx_instr *I = agx_fcmpsel_to(e.b, e.dst(), + agx_mov_imm(e.b, 32, fui(0.3f)), e.x, + agx_zero(), e.f, AGX_FCOND_GE); + agx_opt_promote_constants(e.b->shader); + EXPECT_EQ(I->src[0].type, + g18g ? AGX_INDEX_NORMAL : AGX_INDEX_UNIFORM); + } + + /* Two constants of one value share a uniform, but (x LTN c) ? x : c' takes + * x when x is NaN and must not become the minimum of x and c. */ + { + env e(false, 16); + agx_instr *I = agx_fcmpsel_to( + e.b, e.dst(), e.x, agx_mov_imm(e.b, 32, fui(0.3f)), e.x, + agx_mov_imm(e.b, 32, fui(0.3f)), AGX_FCOND_LTN); + agx_opt_promote_constants(e.b->shader); + EXPECT_FALSE(agx_fcmpsel_is_tied_minmax(I)); + } + + /* The late round reuses a constant only when the whole word matches, and + * appends any other without moving the uploaded range. */ + { + env e(false, 16); + agx_instr *first = agx_fmul_to(e.b, e.dst(), e.x, + agx_mov_imm(e.b, 32, 0x3fc01234u)); + agx_opt_promote_constants(e.b->shader); + ASSERT_EQ(first->src[1].type, AGX_INDEX_UNIFORM); + ASSERT_EQ(first->src[1].value, 16u); + agx_instr *other = agx_fmul_to(e.b, e.dst(), e.x, + agx_mov_imm(e.b, 32, 0x40001234u)); + agx_opt_promote_m5_float_constants(e.b->shader); + EXPECT_EQ(other->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(other->src[1].value, 18u); + EXPECT_EQ(e.b->shader->out->rodata.base_uniform, 16u); + EXPECT_EQ(e.b->shader->out->rodata.size_16, 4u); + } +}