From 3d1613b9511a8a5c35fc5baf0d92ac07217153ac Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Sun, 22 Nov 2026 02:04:52 -0500 Subject: [PATCH] agx: track minifloat select immediates on AGX3 FCMPSEL explicitly Decoding AGX3 FCMPSEL select immediates as minifloats from raw immediate values is ambiguous even when both compare/select pairs appear tied (for example `(h < 0.5h) ? h : uint16(0x0020)` leaves imm 0x20 in both src1 and src3). Extract a shared agx_fcmpsel_is_tied_minmax() helper in agx_compiler.h used by both agx_g17p_tied_minmax() and agx_materialize_agx3_alu_constants(), record I->fcmpsel_minifloat_select = true at fold time when agx_optimizer_inline_imm() places a minifloat into a tied select slot, and decode select immediates as minifloats only when fcmpsel_minifloat_select is set. Update test-optimizer.cpp expectations and test materialized MOV_IMM values directly across G15G, G16G, and G17S for untied select (0x0020), adversarial `(h < 0.5h) ? h : 0x0020` (0x0020), and tied `max(h, 0.5h)` (0x3800). Signed-off-by: Diljit Singh --- src/asahi/compiler/agx_compile.c | 25 ++-- src/asahi/compiler/agx_compiler.h | 27 ++++- src/asahi/compiler/agx_optimizer.c | 25 ++-- .../test/test-native-form-regressions.cpp | 107 ++++++++++++++++++ src/asahi/compiler/test/test-optimizer.cpp | 6 +- 5 files changed, 155 insertions(+), 35 deletions(-) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 3f98eda9b15f..cea842735361 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -7092,17 +7092,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 @@ -7128,19 +7127,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); @@ -7150,6 +7148,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 75c1d8aef1d3..6bda321f2395 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -486,8 +486,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 */ @@ -1454,6 +1454,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 bca2f9c73998..04a217ff68dd 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); } /* @@ -658,9 +644,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 719bb0967128..771c2f84c521 100644 --- a/src/asahi/compiler/test/test-native-form-regressions.cpp +++ b/src/asahi/compiler/test/test-native-form-regressions.cpp @@ -7489,3 +7489,110 @@ TEST(NativeForms, M5FenceMemToTexUsesNonRendezvousFences) free(part.binary); ralloc_free(b.shader); } + +/* 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); + } + } +} diff --git a/src/asahi/compiler/test/test-optimizer.cpp b/src/asahi/compiler/test/test-optimizer.cpp index 964038d0b16c..008427f6ed9a 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);