Repository navigation
asahi/compiler: guard AGX3B mixed-precision falu modifier and uniform folding - #1476
Conversation
… folding
On AGX3B (G17P), agx_optimizer_fmov() visits source operands in index order.
When an unsaturated 32-bit FMUL/FADD/FFMA had src[0] = f2f32(f16_gpr) and
src[1] = fmov(-f32), src[0] checked !other.neg on the still-unfolded SSA
temporary (peeking only through unmodified uniform/immediate fmovs) and
folded the 16-bit GPR into src[0]. On the next iteration, the same-width
f32 fmov folded .neg onto src[1] without checking whether another operand
was already AGX_SIZE_16, emitting FMUL(d32, h16, -r32), which the AGX3B
packer refuses in the mixed-precision FMUL path.
Similarly:
- agx_can_fold_half_uniform() did not peek through modified fmovs or
exclude preambles, allowing FMUL(widen(u16), fmov(-u32)) to fold both
uniforms and later copy out an unencodable FMUL(h16, -r32) in
agx_lower_uniform_sources() or turn u16 into a half GPR in
agx3_lower_cprog_inputs() after the main legalizers had already run;
- folding -abs(h16) into a 32-bit widening fmov produced a combined
modifier rejected by agx3_alu_widen_half().
Fix all layers while preserving encodable AGX3B mixed forms:
- In agx_optimizer_fmov(): peek through non-saturating fmov defs (including
.abs/.neg) in agx_can_fold_half_uniform() and allow_agx3b_{ffma,fmul,fadd}_gpr;
disallow half-uniform folding in preambles; preserve native
agx3_alu_fmul_abs_mixed (f16 * |f32|), modified single-half FADD, and
saturated mixed FMUL; guard same-width folding against creating
unencodable AGX3B mixed-precision FMUL/FADD/FFMA or half-uniform forms;
and prevent folding combined -abs(h16) into widening fmovs on AGX3B and
non-G18G AGX3C.
- In agx_compile.c: extend agx_lower_agx3_fma_sources() to widen half GPRs
on unsaturated AGX3B FMUL whenever f32.neg / f16.abs / f16.neg or two half
GPRs are present; in agx_materialize_agx3_alu_constants(), lower
FADD(d32, -|h16|, -0.0), widen half GPRs on AGX3B FADD with non-negzero
or saturated immediates, and widen one half GPR on unmodified two-half
32-bit FADD; and re-run agx_materialize_agx3_alu_constants() and
agx_lower_agx3_fma_sources() after agx3_lower_cprog_inputs() in native
preambles so 16-bit uniform inputs lowered to half GPRs are legalized.
- In agx_lower_uniform_sources.c: widen materialized 16-bit uniforms (both
newly extracted and reused) when lowered into an AGX3B 32-bit FADD,
FMUL, or FFMA that cannot take a 16-bit GPR in that position.
- Add unit tests in test-native-form-regressions.cpp covering operand-order
symmetry, positive controls (including f16 * |f32|, modified single-half
FADD, and saturated mixed FMUL), direct legalizer + uniform-lowering
backstops, and end-to-end G17P compute + native constant-program
compilation.
Signed-off-by: Marcelo Alcantara <maralc@gmail.com>
54ad936 to
38b967a
Compare
…ard) Add upstream PR aurora-silicon#1476 (38b967a) to carry.toml on integration/aurora-7.3 with matching annotated tag carry-input/1476-38b967a0e3d1. Keeps intake at 8a40c21. Signed-off-by: Marcelo Alcantara <maralc@gmail.com>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
6 open issues
Whenwiden_agx3b_half_uniformis true in the reuse path, this creates a freshfmov+ temp for… · New Whenwiden_agx3b_half_uniformis true in the reuse path, this creates a freshfmov+ temp for… · New The new AGX3B folding guard is functionally important but quite large and highly nested, mixing… · New This single TEST covers several logically distinct behaviors (optimizer symmetry, folding… · New This single TEST covers several logically distinct behaviors (optimizer symmetry, folding… · New This single TEST covers several logically distinct behaviors (optimizer symmetry, folding… · New
What changed in this PR
Guards AGX3B (G17P) mixed-precision FMUL/FADD/FFMA folding/legalization paths so optimizer modifier/uniform folding and post-legalizer lowering can’t produce unencodable native forms; adds regression tests to lock in operand-order symmetry and backstops.
Changes:
- Add
agx_peek_fmov_src()and tighten AGX3B folding rules inagx_optimizer_fmov()to prevent illegal mixed-half + modifier/immediate/uniform combinations. - Extend legalization/materialization/uniform-lowering to widen half operands when required (including after constant-program input lowering).
- Add an end-to-end + unit-level regression test covering the previously unencodable scenarios.
| File | Description |
|---|---|
| src/asahi/compiler/agx_optimizer.c | Adds fmov “peek” helper and AGX3B-specific folding guards to avoid creating unencodable mixed-precision ALU forms. |
| src/asahi/compiler/agx_compile.c | Extends AGX3B legalization/materialization and re-runs key passes after cprog input lowering to keep sources encodable. |
| src/asahi/compiler/agx_lower_uniform_sources.c | Widens materialized 16-bit uniforms when lowering into AGX3B 32-bit ops that can’t accept half GPRs. |
| src/asahi/compiler/test/test-native-form-regressions.cpp | Adds regression coverage for operand-order folding symmetry, legalization backstops, and end-to-end compilation. |
Review effort: Lite
| bool widen_agx3b_half_uniform = false; | ||
| if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B && | ||
| idx.size == AGX_SIZE_16 && I->nr_dests == 1 && | ||
| I->dest[0].size == AGX_SIZE_32) { | ||
| if (I->op == AGX_OPCODE_FADD && I->nr_srcs == 2) { | ||
| widen_agx3b_half_uniform = | ||
| (I->src[1 - s].type == AGX_INDEX_IMMEDIATE && | ||
| (I->saturate || | ||
| !agx_is_equiv(I->src[1 - s], agx_negzero()))) || | ||
| (!I->saturate && !I->src[s].abs && !I->src[s].neg && | ||
| !I->src[1 - s].abs && !I->src[1 - s].neg && | ||
| I->src[1 - s].type != AGX_INDEX_IMMEDIATE && | ||
| I->src[1 - s].size == AGX_SIZE_16); | ||
| } else if (I->op == AGX_OPCODE_FMUL && I->nr_srcs == 2) { | ||
| widen_agx3b_half_uniform = | ||
| I->src[1 - s].type == AGX_INDEX_IMMEDIATE || | ||
| (!I->saturate && | ||
| (I->src[s].abs || I->src[s].neg || I->src[1 - s].neg || | ||
| I->src[1 - s].size == AGX_SIZE_16)); | ||
| } else if (I->op == AGX_OPCODE_FFMA && I->nr_srcs == 3) { | ||
| bool has_imm_or_mod = I->saturate || I->src[s].abs || | ||
| I->src[s].neg; | ||
| bool has_other_uniform = false; | ||
| agx_foreach_src(I, k) { | ||
| if (k == s) | ||
| continue; | ||
| has_imm_or_mod |= I->src[k].type == AGX_INDEX_IMMEDIATE || | ||
| I->src[k].abs || I->src[k].neg; | ||
| has_other_uniform |= I->src[k].type == AGX_INDEX_UNIFORM; | ||
| } | ||
| widen_agx3b_half_uniform = | ||
| has_imm_or_mod || has_other_uniform; | ||
| } | ||
| } |
| agx_index src_val = materialized_to[m]; | ||
| if (widen_agx3b_half_uniform) { | ||
| agx_index wide = agx_temp(ctx, AGX_SIZE_32); | ||
| agx_fmov_to(&b, wide, src_val); | ||
| src_val = wide; | ||
| } |
| /* On AGX3B, mixed-precision f32 FMUL/FADD/FFMA with a half-width source | ||
| * requires operands that fit its native mixed encodings (the packer | ||
| * refuses f32.neg / f16.abs / f16.neg on unsaturated mixed FMUL, and | ||
| * half-uniform forms still require a full-width GPR). Keep the fmov | ||
| * separate whenever folding it onto another operand would break that | ||
| * contract, while still allowing encodable modified single-half FADD | ||
| * and saturated mixed FMUL. */ | ||
| if (ctx->key && ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B && | ||
| ins->nr_dests == 1 && ins->dest[0].size == AGX_SIZE_32 && | ||
| !agx_is_fmov(ins) && | ||
| (ins->op == AGX_OPCODE_FMUL || ins->op == AGX_OPCODE_FADD || | ||
| ins->op == AGX_OPCODE_FFMA)) { | ||
| if (folded.size == AGX_SIZE_16 && (folded.abs || folded.neg) && | ||
| !ins->saturate && | ||
| (ins->op == AGX_OPCODE_FMUL || ins->op == AGX_OPCODE_FFMA)) | ||
| continue; | ||
|
|
||
| unsigned other_half_gprs = 0; | ||
| unsigned other_half_uniforms = 0; | ||
| bool other_full_gpr = false; | ||
| agx_foreach_src(ins, k) { | ||
| if (k == s) | ||
| continue; | ||
| const agx_index o = ins->src[k]; | ||
| if ((o.type == AGX_INDEX_NORMAL || o.type == AGX_INDEX_REGISTER) && | ||
| o.size == AGX_SIZE_16) | ||
| other_half_gprs++; | ||
| else if (o.type == AGX_INDEX_UNIFORM && o.size == AGX_SIZE_16) | ||
| other_half_uniforms++; | ||
| else if ((o.type == AGX_INDEX_NORMAL || | ||
| o.type == AGX_INDEX_REGISTER) && | ||
| o.size == AGX_SIZE_32 && !o.memory && | ||
| agx_channels(o) == 1) | ||
| other_full_gpr = true; | ||
| } | ||
| if (other_half_uniforms > 0 && | ||
| (folded.type == AGX_INDEX_UNIFORM || | ||
| folded.type == AGX_INDEX_IMMEDIATE) && | ||
| !other_full_gpr) | ||
| continue; | ||
| if (other_half_gprs > 0) { | ||
| if (ins->op == AGX_OPCODE_FMUL) { | ||
| /* Native agx3_alu_fmul_abs_mixed admits f32.abs without .neg | ||
| * for unsaturated mixed FMUL; saturated mixed FMUL uses | ||
| * agx3_falu_mul_mod_runtime (registers only). */ | ||
| if ((!ins->saturate && folded.neg) || | ||
| folded.type == AGX_INDEX_UNIFORM || | ||
| folded.type == AGX_INDEX_IMMEDIATE) | ||
| continue; | ||
| } else if (ins->op == AGX_OPCODE_FADD) { | ||
| /* Modified single-half register FADD encodes via | ||
| * agx3_falu_add_mod_runtime; reject non-GPR operands. */ | ||
| if (folded.type == AGX_INDEX_UNIFORM || | ||
| folded.type == AGX_INDEX_IMMEDIATE) | ||
| continue; | ||
| } else { | ||
| if (ins->saturate) { | ||
| if (folded.type == AGX_INDEX_UNIFORM || | ||
| folded.type == AGX_INDEX_IMMEDIATE) | ||
| continue; | ||
| } else { | ||
| if (folded.abs || folded.neg) | ||
| continue; | ||
| if (folded.type == AGX_INDEX_UNIFORM && | ||
| (ctx->is_preamble || other_half_gprs > 1 || | ||
| folded.size != AGX_SIZE_32 || (folded.value & 1) || | ||
| folded.value >= 512)) | ||
| continue; | ||
| if (folded.type == AGX_INDEX_IMMEDIATE && s == 2) { | ||
| const bool mul_imm = | ||
| (ins->src[0].type == AGX_INDEX_IMMEDIATE && | ||
| ins->src[0].value <= 255 && !ins->src[0].abs && | ||
| !ins->src[0].neg) != | ||
| (ins->src[1].type == AGX_INDEX_IMMEDIATE && | ||
| ins->src[1].value <= 255 && !ins->src[1].abs && | ||
| !ins->src[1].neg); | ||
| if (!mul_imm || folded.value > 255) | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
| TEST(NativeForms, Agx3bMixedFloatModifierFoldingOrder) | ||
| { | ||
| /* 1. Optimizer symmetry for FMUL/FADD/FFMA with f2f32(f16) + fmov(-f32). */ | ||
| for (unsigned op_idx = 0; op_idx < 3; ++op_idx) { | ||
| for (bool half_first : {true, false}) { | ||
| SCOPED_TRACE(op_idx); | ||
| SCOPED_TRACE(half_first); | ||
| void *mem = ralloc_context(NULL); | ||
| agx_builder *b = agx_test_builder(mem); | ||
| agx_shader_key key = {}; | ||
| key.dev.encoding = AGX_ISA_ENCODING_AGX3B; | ||
| key.dev.chip = AGX_CHIP_G17P; | ||
| b->shader->key = &key; | ||
|
|
||
| agx_index h16 = agx_preload(b, agx_register(0, AGX_SIZE_16)); | ||
| agx_index x32 = agx_preload(b, agx_register(2, AGX_SIZE_32)); | ||
| agx_index z32 = agx_preload(b, agx_register(4, AGX_SIZE_32)); | ||
| agx_index wide = agx_temp(b->shader, AGX_SIZE_32); | ||
| agx_fmov_to(b, wide, h16); | ||
| agx_index negx = agx_temp(b->shader, AGX_SIZE_32); | ||
| agx_fmov_to(b, negx, agx_neg(x32)); | ||
|
|
||
| agx_index a = half_first ? wide : negx; | ||
| agx_index c = half_first ? negx : wide; | ||
| agx_index out = agx_temp(b->shader, AGX_SIZE_32); | ||
| agx_instr *alu = NULL; | ||
| if (op_idx == 0) | ||
| alu = agx_fmul_to(b, out, a, c); | ||
| else if (op_idx == 1) | ||
| alu = agx_fadd_to(b, out, a, c); | ||
| else | ||
| alu = agx_ffma_to(b, out, a, c, z32); | ||
| agx_unit_test(b, out); | ||
|
|
||
| agx_optimizer_forward(b->shader); | ||
| bool has_half = false, has_mod = false; | ||
| agx_foreach_src(alu, s) { | ||
| has_half |= alu->src[s].size == AGX_SIZE_16; | ||
| has_mod |= alu->src[s].abs || alu->src[s].neg; | ||
| } | ||
| EXPECT_FALSE(has_half && has_mod); | ||
| ralloc_free(mem); | ||
| } | ||
| } | ||
|
|
| for (bool no_preamble : {true, false}) { | ||
| SCOPED_TRACE(no_preamble); | ||
| nir_builder b = nir_builder_init_simple_shader( | ||
| MESA_SHADER_COMPUTE, &agx_nir_options, "agx3b mixed falu neg"); |
| } | ||
| } |
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
The modifier-fold checks preserve the supported mixed forms, and the added legalization covers half uniforms materialized after the first pass. The tests check both operand orders and the affected packer forms. Merge still needs an exact-head AGX3B hardware row.
|
Row: This covers the on-hardware G17P item under Not yet.
The red CI checks aren't part of this row. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Rechecked the merge of aurora-wip. It was conflict-free, the PR diff has the same stable patch ID, and the new base contains the fix for the policy selftest fixture. The J700 CTS row at 38b967a carries forward. J773g FP16 CTS is the remaining shared-compiler control.
|
Re-row at exact head
|
|
Merge train status at Two items remain:
|
|
J773g (
|
|
Local CI at the merge result: pass. Hosted Actions on this repo hasn't started a run since 00:15 UTC today. Tree: head
The same three runs also passed on the previous base The DjDeveloperr approved this head, the J773g same-boot VK CTS A/B passed at it (8,628 cases, 0 diffs, 0 faults) and a J700 exact-head row exists. Merging with admin under the override. |


What: Prevent order-dependent
agx_optimizer_fmov()modifier/uniform folding and post-legalizer uniform/constant-program lowering from producing unencodable AGX3B (G17P) mixed-precision 32-bitFMUL/FADD/FFMAinstructions (such as unsaturatedfmul [d32, s0=16:r, s1=32:r.neg]) and add regression unit tests.Tested: Asahi build and the AGX compiler suite passed at
38b967a0e3d1. On J700 (A18 Pro, G17P B1), all 8,628 targeted Vulkan CTS statuses matched the baseline and there were zero GPU faults. Asahi run, J700 receipt.Risk: two-way door · M4 (AGX3B / G17P) and Apple Silicon Macs
Not yet: Same-boot FP16 CTS A/B on J773g (G16G) to cover the shared AGX3B compiler path. The historical policy selftest on this head fails in the default-branch healing fixture fixed by #1474.
Details
Root Cause
On AGX3B (
AGX_ISA_ENCODING_AGX3B/ G17P),agx_optimizer_fmov()visits source operands in index order:FMUL/FADD/FFMAhadsrc[0] = f2f32(f16_gpr)andsrc[1] = fmov(-f32),allow_agx3b_{fmul,fadd,ffma}_gprinspectedins->src[1]befores == 1had been folded and did not peek through.neg/.absfmovdefs, sos == 0folded the 16-bit GPR intosrc[0].s == 1), same-width folding (src.size == AGX_SIZE_32 && def->src[0].size == AGX_SIZE_32) blindly applied.negontosrc[1]even though another operand was alreadyAGX_SIZE_16.agx3_pack_binary.cgreedily routes unsaturated AGX3BFMULwith anyAGX_SIZE_16register source to the mixed-precision path (agx3_alu_fmul_mixed/agx3_alu_fmul_abs_mixed), which refusesf32.neg,f16.abs,f16.neg, or twof16GPRs (AGX3_REFUSE(I)).agx_can_fold_half_uniform()did not peek through modifiedfmovdefs or exclude preambles (ctx->is_preamble), allowingFMUL(widen(u16), fmov(-u32))to fold both uniforms and later copy outFMUL(h16, -r32)inagx_lower_uniform_sources()(or turnu16intoh16inagx3_lower_cprog_inputs()after the main legalizers already ran);-abs(h16)into a 32-bit wideningfmovproducedFADD(d32, -|h16|, -0.0), whichagx3_alu_widen_half()refuses on AGX3B and non-G18G AGX3C;FADD(h16, h16)andFADD(h16, #imm)(non--0.0or saturated) lacked AGX3B legalizer widening.Changes
src/asahi/compiler/agx_optimizer.c:agx_peek_fmov_src()and use it inagx_can_fold_half_uniform()andallow_agx3b_{ffma,fmul,fadd}_gprwhile preserving nativeagx3_alu_fmul_abs_mixed(f16 * |f32|).!ctx->is_preamble) fromallow_half_uniform.fmovfolding on AGX3B 32-bitFMUL/FADD/FFMAso folding modifiers, uniforms, or immediates alongside existing half operands cannot create unencodable forms, while still allowing encodableFMUL(h16, |f32|), modified single-halfFADD, and saturated mixedFMUL.fmovagainst folding combinedabs && negontoAGX_SIZE_16on AGX3B and non-G18G AGX3C.src/asahi/compiler/agx_compile.c:agx_lower_agx3_fma_sources()for unsaturated AGX3BFMULto widen half GPRs wheneverf32.neg,f16.abs,f16.neg, or two half GPRs are present.agx_materialize_agx3_alu_constants(), handleFADD(d32, -|h16|, -0.0)bit-materialization, widen half GPRs on AGX3BFADDpaired with non--0.0or saturated immediates, and widen one half GPR on unmodified two-half 32-bitFADD.agx_materialize_agx3_alu_constants()andagx_lower_agx3_fma_sources()afteragx3_lower_cprog_inputs()in native preambles so 16-bit uniforms lowered to half GPRs are legalized before RA and packing.src/asahi/compiler/agx_lower_uniform_sources.c:FADD,FMUL, orFFMAthat cannot accept a 16-bit GPR in that position.src/asahi/compiler/test/test-native-form-regressions.cpp:NativeForms.Agx3bMixedFloatModifierFoldingOrdertesting operand-order symmetry, positive controls (f16 * |f32|, modified single-halfFADD, saturated mixedFMUL), direct legalizer and uniform-lowering backstops, and end-to-end G17P compute + native constant-program compilation.Evidence
AGX_ISA_ENCODING_AGX3B):54ad936a2339f566bfff2edc9a17ae4bece9ec50based on greenaurora-wipcommita4153c8fefd6d3acdf64020c624008637c158e8e.