diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 776dc4809081..8825146af87a 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -7302,17 +7302,16 @@ agx_materialize_agx3_alu_constants(agx_context *ctx) /* AGX3C's tied min/max consumes full register operands in both * positions. Keep constants tied after compact-constant folding. */ if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && - I->op == AGX_OPCODE_FCMPSEL && I->nr_srcs == 4 && - I->dest[0].size == AGX_SIZE_32 && !I->invert_cond && - (I->fcond == AGX_FCOND_LTN || I->fcond == AGX_FCOND_GTN)) { + I->dest[0].size == AGX_SIZE_32 && agx_fcmpsel_is_tied_minmax(I)) { for (unsigned c = 0; c < 2; ++c) { if (I->src[c].type != AGX_INDEX_IMMEDIATE || - !agx_is_equiv(I->src[c], I->src[c + 2])) + (!I->fcmpsel_minifloat_select && I->src[c].value != 0)) continue; const agx_index constant = agx_mov_imm( &b, 32, fui(agx_minifloat_decode(I->src[c].value))); I->src[c] = I->src[c + 2] = constant; } + I->fcmpsel_minifloat_select = false; } /* General FSEL has register comparators or an implicit zero. Nonzero @@ -7338,19 +7337,18 @@ agx_materialize_agx3_alu_constants(agx_context *ctx) if (agx_isa_is_agx3(ctx->key->dev.encoding) && I->op == AGX_OPCODE_FCMPSEL && I->dest[0].size == AGX_SIZE_16 && !agx3c_fsel16_imm_encodable(ctx, I)) { - /* Select alternatives are bit patterns, not minifloats -- except - * in a tied minimum/maximum, where the optimizer deposited the - * compared minifloat into the select slot too. */ + /* Select alternatives are raw bit patterns unless the optimizer + * recorded that it folded a minifloat into the select slot + * (fcmpsel_minifloat_select). Half-tied shapes such as + * `(0.5 < x) ? 0.5 : y` never arrive here with a minifloat in a + * select slot because agx_is_float_src() is false for s >= 2 and + * agx_optimizer_inline_imm() only copies a minifloat into + * src[s + 2] for a full AGX3B tied min/max. */ for (unsigned s = 2; s < 4; ++s) { if (I->src[s].type != AGX_INDEX_IMMEDIATE) continue; - const agx_index cmp = I->src[s - 2]; - const bool tied = cmp.type == AGX_INDEX_IMMEDIATE && - cmp.value == I->src[s].value && - cmp.abs == I->src[s].abs && - cmp.neg == I->src[s].neg; uint32_t bits = I->src[s].value; - if (tied) { + if (I->fcmpsel_minifloat_select) { float value = agx_minifloat_decode(I->src[s].value); if (I->src[s].abs) value = fabsf(value); @@ -7360,6 +7358,7 @@ agx_materialize_agx3_alu_constants(agx_context *ctx) } I->src[s] = agx_mov_imm(&b, 16, bits); } + I->fcmpsel_minifloat_select = false; } /* G18G's half FSEL is grounded only with all four sources in half * registers (agx3_alu_fcmp_select_half_gpu1). Compare slots hold diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 3bf9271146dc..487f2fb864a7 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -493,8 +493,8 @@ typedef struct { /* NIR permits either sign for a zero result of this floating add. */ bool no_signed_zero : 1; - /* M5 FCMPSEL: a selected immediate is a minifloat (native fself32), not - * raw integer bits. */ + /* FCMPSEL: a selected immediate was folded as a minifloat by the optimizer + * (AGX3C 32-bit fself or AGX3B tied min/max), rather than raw integer bits. */ bool fcmpsel_minifloat_select : 1; /* Output modifiers */ @@ -1503,6 +1503,29 @@ agx_is_float_src(const agx_instr *I, unsigned s) return info.is_float || (s < 2 && fcmp); } +/* Whether an FCMPSEL is a fully tied minNum/maxNum pattern: + * (a LTN b) ? a : b or (a GTN b) ? a : b, with identical sizes, modifiers, + * and memory flags on each compare/select pair. */ +static inline bool +agx_fcmpsel_is_tied_minmax(const agx_instr *I) +{ + if (I->op != AGX_OPCODE_FCMPSEL || I->nr_srcs != 4 || I->invert_cond || + I->saturate || + (I->fcond != AGX_FCOND_GTN && I->fcond != AGX_FCOND_LTN)) + return false; + + /* agx_is_equiv() ignores modifiers, and a modifier on one half of a tie + * would make the two operands different values. Require them identical. */ + for (unsigned i = 0; i < 2; ++i) { + const agx_index a = I->src[i], b = I->src[i + 2]; + if (!agx_is_equiv(a, b) || a.size != b.size || a.abs != b.abs || + a.neg != b.neg || a.memory != b.memory) + return false; + } + + return true; +} + /* The M5 select with a uniform first comparand that native G17G * fragments use (agx3_alu_fcmp_select32_gpu1 cmp0_uniform): a 32-bit * result choosing a zero immediate or a 32-bit register after comparing an diff --git a/src/asahi/compiler/agx_optimizer.c b/src/asahi/compiler/agx_optimizer.c index 5ad38e07d1ca..65c1fb7b1746 100644 --- a/src/asahi/compiler/agx_optimizer.c +++ b/src/asahi/compiler/agx_optimizer.c @@ -118,22 +118,8 @@ agx_preserve_g17p_tied_fcmpsel(agx_context *ctx, agx_instr *I, unsigned src) static bool agx_g17p_tied_minmax(agx_context *ctx, const agx_instr *I) { - if (!(ctx->key && ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B && - I->op == AGX_OPCODE_FCMPSEL && I->nr_srcs == 4 && !I->invert_cond && - !I->saturate && - (I->fcond == AGX_FCOND_GTN || I->fcond == AGX_FCOND_LTN))) - return false; - - /* agx_is_equiv() ignores modifiers, and a modifier on one half of a tie - * would make the two operands different values. Require them identical. */ - for (unsigned i = 0; i < 2; ++i) { - const agx_index a = I->src[i], b = I->src[i + 2]; - if (!agx_is_equiv(a, b) || a.size != b.size || a.abs != b.abs || - a.neg != b.neg || a.memory != b.memory) - return false; - } - - return true; + return ctx->key && ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B && + agx_fcmpsel_is_tied_minmax(I); } /* @@ -678,9 +664,12 @@ agx_optimizer_inline_imm(agx_context *ctx, agx_instr **defs, agx_instr *I) if (!agx_minifloat_exact(f)) continue; - /* Both halves of a tied minimum/maximum, or neither. */ - if (s < 2 && agx_g17p_tied_minmax(ctx, I)) + /* Both halves of a tied minimum/maximum, or neither. Record that the + * select slot now holds a minifloat rather than raw integer bits. */ + if (s < 2 && agx_g17p_tied_minmax(ctx, I)) { I->src[s + 2] = agx_immediate_f(f); + I->fcmpsel_minifloat_select = true; + } I->src[s] = agx_immediate_f(f); } else if (value == def->imm) { diff --git a/src/asahi/compiler/test/test-native-form-regressions.cpp b/src/asahi/compiler/test/test-native-form-regressions.cpp index 0314bb71f2f9..2436051237fa 100644 --- a/src/asahi/compiler/test/test-native-form-regressions.cpp +++ b/src/asahi/compiler/test/test-native-form-regressions.cpp @@ -8051,6 +8051,113 @@ TEST(NativeForms, M5TaskAndMeshMultisampleFetchLowerAndCompile) } } +/* A 16-bit FCMPSEL select immediate must keep raw integer bits unless the + * optimizer explicitly folded a minifloat into the select slot + * (fcmpsel_minifloat_select): + * - untied `(0.5f < x) ? 0x0020 : 0` (8x MSAA sample 5 discard mask) -> 0x0020 + * - adversarial `(h < 0.5h) ? h : uint16(0x0020)` (ties src0/src2 and has + * immediate 0x20 in both src1 and src3) -> 0x0020 + * - tied `max(h, 0.5h)` positive control -> 0x3800 (FP16 0.5). */ +TEST(NativeForms, Agx3HalfFcmpselUntiedImmediateNotDecodedAsMinifloat) +{ + const struct { + enum agx_isa_encoding encoding; + enum agx_chip chip; + } targets[] = { + {AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G15G}, + {AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G16G}, + {AGX_ISA_ENCODING_AGX3C, AGX_CHIP_G17S}, + }; + + auto find_mov_imm = [](agx_context *ctx, agx_index ssa) -> const agx_instr * { + if (ssa.type != AGX_INDEX_NORMAL) + return NULL; + agx_foreach_instr_global(ctx, I) { + if (I->nr_dests && I->dest[0].type == AGX_INDEX_NORMAL && + I->dest[0].value == ssa.value) + return I; + } + return NULL; + }; + + for (const auto &target : targets) { + SCOPED_TRACE(target.chip); + + /* Case 1: untied (0.5f < x) ? uint16(0x0020) : 0 */ + { + void *mem = ralloc_context(NULL); + agx_builder *b = agx_test_builder(mem); + agx_shader_key key = {}; + key.dev.encoding = target.encoding; + key.dev.chip = target.chip; + b->shader->key = &key; + agx_index x = agx_preload(b, agx_register(0, AGX_SIZE_32)); + agx_index half_f32 = agx_mov_imm(b, 32, 0x3f000000); + agx_index bit5 = agx_mov_imm(b, 16, 0x0020); + agx_index zero = agx_mov_imm(b, 16, 0x0000); + agx_instr *sel = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_16), + half_f32, x, bit5, zero, AGX_FCOND_LT); + agx_unit_test(b, sel->dest[0]); + agx_optimizer_forward(b->shader); + agx_materialize_agx3_alu_constants(b->shader); + const agx_instr *def = find_mov_imm(b->shader, sel->src[2]); + ASSERT_NE(def, nullptr); + EXPECT_EQ(def->op, AGX_OPCODE_MOV_IMM); + EXPECT_EQ(def->dest[0].size, AGX_SIZE_16); + EXPECT_EQ(def->imm, 0x0020u); + ralloc_free(mem); + } + + /* Case 2: adversarial (h < 0.5h) ? h : uint16(0x0020) */ + { + void *mem = ralloc_context(NULL); + agx_builder *b = agx_test_builder(mem); + agx_shader_key key = {}; + key.dev.encoding = target.encoding; + key.dev.chip = target.chip; + b->shader->key = &key; + agx_index h = agx_preload(b, agx_register(0, AGX_SIZE_16)); + agx_index half_f16 = agx_mov_imm(b, 16, 0x3800); + agx_index raw_20 = agx_mov_imm(b, 16, 0x0020); + agx_instr *sel = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_16), h, + half_f16, h, raw_20, AGX_FCOND_LTN); + agx_unit_test(b, sel->dest[0]); + agx_optimizer_forward(b->shader); + EXPECT_FALSE(sel->fcmpsel_minifloat_select); + agx_materialize_agx3_alu_constants(b->shader); + const agx_instr *def = find_mov_imm(b->shader, sel->src[3]); + ASSERT_NE(def, nullptr); + EXPECT_EQ(def->op, AGX_OPCODE_MOV_IMM); + EXPECT_EQ(def->dest[0].size, AGX_SIZE_16); + EXPECT_EQ(def->imm, 0x0020u); + ralloc_free(mem); + } + + /* Case 3: positive control tied max(h, 0.5h) -> 0x3800 */ + { + void *mem = ralloc_context(NULL); + agx_builder *b = agx_test_builder(mem); + agx_shader_key key = {}; + key.dev.encoding = target.encoding; + key.dev.chip = target.chip; + b->shader->key = &key; + agx_index h = agx_preload(b, agx_register(0, AGX_SIZE_16)); + agx_index half_f16 = agx_mov_imm(b, 16, 0x3800); + agx_instr *sel = agx_fcmpsel_to(b, agx_temp(b->shader, AGX_SIZE_16), h, + half_f16, h, half_f16, AGX_FCOND_GTN); + agx_unit_test(b, sel->dest[0]); + agx_optimizer_forward(b->shader); + agx_materialize_agx3_alu_constants(b->shader); + const agx_instr *def = find_mov_imm(b->shader, sel->src[3]); + ASSERT_NE(def, nullptr); + EXPECT_EQ(def->op, AGX_OPCODE_MOV_IMM); + EXPECT_EQ(def->dest[0].size, AGX_SIZE_16); + EXPECT_EQ(def->imm, 0x3800u); + ralloc_free(mem); + } + } +} + TEST(NativeForms, Agx3bUsatSub32NativeAnd16Lowered) { /* 32-bit usub_sat compiles to a native saturating IADD on AGX3B, while diff --git a/src/asahi/compiler/test/test-optimizer.cpp b/src/asahi/compiler/test/test-optimizer.cpp index 9fa422213a01..4a55ba781603 100644 --- a/src/asahi/compiler/test/test-optimizer.cpp +++ b/src/asahi/compiler/test/test-optimizer.cpp @@ -412,7 +412,8 @@ TEST_F(Optimizer, G17PFoldsBothHalvesOfATiedConstantFmax) /* Burn the index the dead constant held, so only the folding differs. */ (void)agx_temp(expected->shader, AGX_SIZE_32); out = agx_temp(expected->shader, AGX_SIZE_32); - agx_fcmpsel_to(expected, out, wx, folded, wx, folded, AGX_FCOND_GTN); + agx_fcmpsel_to(expected, out, wx, folded, wx, folded, AGX_FCOND_GTN) + ->fcmpsel_minifloat_select = true; agx_unit_test(expected, out); agx_optimize_and_dce(actual->shader); @@ -465,7 +466,8 @@ TEST_F(Optimizer, G17PFoldsBothHalvesOfATiedConstantFmaxWrittenFirst) const agx_index folded = agx_immediate_f(0.0f); (void)agx_temp(expected->shader, AGX_SIZE_32); out = agx_temp(expected->shader, AGX_SIZE_32); - agx_fcmpsel_to(expected, out, folded, wx, folded, wx, AGX_FCOND_GTN); + agx_fcmpsel_to(expected, out, folded, wx, folded, wx, AGX_FCOND_GTN) + ->fcmpsel_minifloat_select = true; agx_unit_test(expected, out); agx_optimize_and_dce(actual->shader);