Skip to content

agx: keep f16 GPR sources native in unmodified AGX3B F32 FFMA - #702

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
perf/agx3b-ffma-native-f16-src
Nov 22, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
perf/agx3b-ffma-native-f16-src

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 22, 2026 •

Copy link
Copy Markdown

Extend mixed-width F32 FFMA native f16 GPR source preservation (as in #683 for M5 / AGX3C) to AGX3B (non-G18G), removing redundant fmov f32, f16 widening moves ahead of unmodified all-scalar-register F32 FFMA instructions.

Changes

  • src/asahi/compiler/agx_optimizer.c: allow folding unmodified scalar f16 GPR fmov sources into unmodified, non-saturated, all-scalar-register F32 FFMA on AGX_ISA_ENCODING_AGX3B. FADD, FMUL, G18G, saturated, modified, immediate, and uniform-mixed forms keep their existing widening behavior.
  • src/asahi/compiler/agx_compile.c: key all_scalar_regs in agx_lower_agx3_fma_sources() strictly on AGX_ISA_ENCODING_AGX3B (leaving AGX3C untouched) to preserve unmodified scalar f16 GPR sources (AGX_INDEX_NORMAL and AGX_INDEX_REGISTER, single- or multi-use).
  • src/asahi/compiler/agx3_pack_binary.c: route unmodified mixed-width scalar AGX3B F32 FFMA through agx3_falu_fma_mod_runtime() so per-operand f16/f32 width bits and runtime lifetime/wait hints pack natively.
  • Unit tests:
    • Optimizer.Agx3bFoldsUnmodifiedHalfGprIntoFfma (test-optimizer.cpp): positive/negative folding cases (src0/src1/addend/dual, saturated, modified, FADD, G18G).
    • NativeForms.Agx3bFfmaKeepsUnmodifiedHalfGprNative (test-native-form-regressions.cpp): lowering cases (normal + precoloured register indices, modified, uniform-mixed, G18G, agx3c_src0_widened) plus direct agx3b_tables.txt physical bit-field assertions across 6-byte form f32fma 2 and 8-byte form f32fma 3 (group 270) encodings.

10,982-Shader AGX3B (g17p) Corpus Sweep (c0b51ef2455 vs d7a02f6dc20d)

Across gpubench-foz, mc-packs, mc-vanilla, shaderdb, and vk-synth (10,982 shaders):

  • Instructions: -2,403 (-0.11%) (up to -101 instructions on mc-packs composite4)
  • Code size: -13,312 B
  • Register pressure / occupancy: -142 GPRs, +320 threads, -51 fills, -44 scratch
  • Preamble instructions: -235
  • Packer refusals / failures: 0 new failures, 0 new agx3_pack_binary refusals (406 -> 406 pre-existing)

Verification (Exact Head d7a02f6dc20ddbc6b8e5494b60fc3d9f866c29d2)

  • Host CI (fedora-mesaci): 156/156 PASS (0 fail, 0 timeout, 0 skipped).
  • Mirror asahi CI (DjDeveloperr/mesa): GREEN (45019754703).
  • Policy check: DEFAULT_BRANCH=aurora-wip bash .github/aurora/agent-policy.sh pr HEAD clean.
  • Apple A18 Pro (G17P B1, kfault_delta=0, avc_delta=0):
    • 1,024-case subnormal/NaN/signed-zero/overflow FMA hardware oracle: 1,024/1,024 bit-exact (0 diffs) vs widened baseline.
    • Targeted Vulkan CTS: 2,724 supported Pass / 0 Fail (float_controls.fp16.* 2,252/0, precision*.fma.* 16/0, float16.arithmetic.* 456/0).
  • Apple M4 (G16G B0, J773g, 0 faults):
    • 1,024-case FMA hardware oracle: 1,024/1,024 bit-exact (0 diffs) vs base c0b51ef24554; native mixed-width ffma confirmed in disassembly.
    • Vulkan CTS: 2,799 Pass / 0 Fail (vk-mustpass-short 87/87, float16.arithmetic.* 456/456, float_controls.fp16.* 2,240/0, precision*.fma.* 16/0).
    • 4-round ABBA gpubench (8 rows): 8/8 bit-exact CRCs (0.9970× wall / 0.9964× GPU geomean — neutral wall time; primary win is the -2,403 removed widening instructions / -13,312 B / -142 GPRs / +320 threads across the 10,982-shader corpus).

Assisted-by: LLM (Google:Gemini 4 Neon)

@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased onto aurora-wip (c0b51ef2455, new head 8c7b1260e6ffaf0fb96a23b330e9460bef2d9b73) and completed physical MacBook Neo (G17P B1) hardware + CTS verification:

  • Offline G17P nosoft IR verification: Confirmed agx3b-ffma-f16-denorm.spv emits native mixed-width ffma across all four operand combinations (src0_16, src1_16, addend_16, dual-f16) with zero widening moves and zero packer refusals.
  • Physical Neo G17P B1 bit-exact denormal/NaN oracle (c0b51ef2455 widened vs 8c7b1260e6f native across f16 subnormals 0x0001/0x8001/0x03ff/0x83ff, ±0.0h, ±Inf, qNaN/sNaN payloads, and normals): 1024/1024 bit-exact (0 diffs).
  • Physical Neo G17P B1 targeted VK CTS (dEQP-VK.spirv_assembly.instruction.{compute,graphics}.float16.arithmetic.* + dEQP-VK.glsl.builtin.precision{,_fp16_storage16b}.fma.*): 468/468 supported Pass (471 ran, 3 NotSupported, 0 Fail), kfault_delta=0, avc_delta=0.
  • Host compiler CI (fedora-mesaci): 8c7b1260e6ffaf0fb96a23b330e9460bef2d9b73 — 156/0 PASS.
  • Policy check: DEFAULT_BRANCH=aurora-wip bash .github/aurora/agent-policy.sh pr HEAD -> PR head is clean.

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-ffma-native-f16-src branch from 04fafc6 to 8c7b126 Compare November 22, 2026 11:11
@RyanTheTide

Copy link
Copy Markdown

Thanks. The Neo oracle (hardware widened vs native, subnormals, NaN payloads and signed zeros in every slot) is the right kind of evidence. A few things before this goes in:

  • This also changes AGX3C (M5), not only AGX3B. agx_compile.c:5251 uses agx_isa_is_agx3(), which is AGX3B or AGX3C. On plain AGX3C the optimizer already folds f16 GPR moves into FFMA, and until now this lowering re-widened them. After this PR M5 FFMA keeps native halves. That is the open agx: keep unmodified f16 GPR sources native in AGX3C F32 FFMA #683 change (same hunk, keyed == AGX3C). The two will conflict, and this PR has no M5 evidence. Please key the lowering on AGX3B here and leave AGX3C to agx: keep unmodified f16 GPR sources native in AGX3C F32 FFMA #683, or say so in the message and add an M5 row. No unit case covers plain AGX3C today.
  • The byte-exact pack test is circular. test-native-form-regressions.cpp:7392-7397 gets its expected bytes from agx3_falu_fma_mod_runtime(), the same encoder the packer calls at agx3_pack_binary.c:891. It proves the routing at :771, not the encoding. The AGX3B table does have independent width fields for f32fma: 43 and 26 in forms 2/3/6, and the addend width 60 is free in group 270, while forms 0/1 tie it to the destination (groups 266/267). But no AGX3B FMA vector with a 16-bit operand is checked anywhere; the existing ones in test-agx3-falu-mod-core.c:318-380 are all f32. Please add hard-coded expected bytes from an independent source, or check the deposited width bits directly, the way test-agx3-falu-mod-core.c:385 does for the wait bits.
  • The comment rewrite at agx_optimizer.c:241-249 drops the "not generally grounded on G17P" statement. Please say what grounds the change (the hardware oracle). fma_addend_imm_width_mod still refuses half sources on AGX3B (agx3_falu_mod_core.c:546).
  • agx_optimizer.c:262: !agx_is_g18g() can never be false when encoding == AGX3B, because G18G is AGX3C with emit_agx3d. It can go. The G18G test case passes only because of the outer condition at :249.
  • Semantics: the removed move is fadd h, -0.0. It quiets sNaN, keeps -0, and turns an f16 denormal into a normal f32. f16 flush-to-zero is not advertised (agx_compile.c:9639), so a native half operand that flushed would silently differ. Neo shows G17P matches. G15/G16 share the encoding but not the datapath, so the same oracle is needed there.
  • Possible new refusals: the runtime FMA encoder refuses killed sources (agx3_pack_binary.c:881), so a shaderdb sweep for new refusals on AGX3B would be useful.

Hardware: all rows are from G17P (Neo). AGX3B also covers G15G (M3), G16G (M4) and G16S (M4 Pro). Could you add, at 8c7b126, the 1024-case widened-vs-native oracle plus dEQP-VK.glsl.builtin.precision*.fma.* and dEQP-VK.spirv_assembly.instruction.*.float16.arithmetic.* on an M3, an M4 and an M4 Pro? Please also run one denorm-sensitive group, e.g. dEQP-VK.spirv_assembly.instruction.compute.float_controls.fp16.* (denorm preserve), with f16 subnormal inputs feeding fma. If the AGX3C half stays, an M5 row is needed as well.

Assisted-by: LLM (Claude:Opus 5.5)

Allow unmodified all-scalar-register F32 FFMA instructions on AGX3B
(non-G18G) to consume f16 GPR sources directly instead of emitting
redundant f16->f32 widening moves, while leaving AGX3C untouched:

- In agx_optimizer.c, allow folding unmodified scalar f16 GPR widenings
  into unmodified, non-saturated, all-scalar-register F32 FFMA on AGX3B
  (keeping FADD, FMUL, G18G, saturated, modified, immediate, and uniform
  cases on their existing paths).
- In agx_compile.c:agx_lower_agx3_fma_sources(), preserve unmodified
  f16 GPR sources (matching both AGX_INDEX_NORMAL and AGX_INDEX_REGISTER
  and excluding saturate) on AGX3B only.
- In agx3_pack_binary.c:agx3_try_float_modifier(), route unmodified
  mixed-width AGX3B F32 FFMA through agx3_falu_fma_mod_runtime(), which
  encodes per-operand width bits directly.
- Add compiler regression tests in test-optimizer.cpp and
  test-native-form-regressions.cpp covering optimizer folding, lowering
  (normal, precoloured, modified, saturated, uniform, G18G, and AGX3C
  cases), and end-to-end AGX3B binary packing checked directly against
  the agx3b_tables.txt width-bit layout.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Author

Addressed all five R1 items in d7a02f6dc20d:

  1. AGX3B-only gating (agx_compile.c): Keyed all_scalar_regs in agx_lower_agx3_fma_sources() strictly on ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B, leaving AGX3C completely untouched, and added an agx3c_src0_widened regression case in test-native-form-regressions.cpp.
  2. Non-circular pack test (test-native-form-regressions.cpp): Replaced the agx3_falu_fma_mod_runtime() comparison with direct physical bit-field assertions against agx3b_tables.txt across all 7 PackCases (6-byte form f32fma 2 width bits w0[3], w0[8], w1[8] and 8-byte form f32fma 3 group 270 addend width bit w3[1], including high-register f32 vs f16 addend pairs).
  3. Comment & dead-check cleanup (agx_optimizer.c): Updated the comment to cite the 1,024-case G17P hardware oracle and note that fma_addend_imm_width_mod still refuses half sources on AGX3B, and removed the redundant !agx_is_g18g(&ctx->key->dev) check inside the AGX3B branch.
  4. Killed-source / AGX3B refusal sweep (agx3_pack_binary.c:883): Structurally, agx_register_allocate.c:1903 clears I->src[s].kill = false on every instruction and post-RA agx_opt_register_cache() sets .cache/.discard only (never .kill). Empirically verified across a 10,982-shader AGX3B (g17p) shader-db/Fossilize sweep (gpubench-foz, mc-packs, mc-vanilla, shaderdb, vk-synth) comparing c0b51ef2455 vs d7a02f6dc20d: 0 new failures, 0 new agx3_pack_binary refusals (406 -> 406 pre-existing failures), with -2,403 instructions (-0.11%), -13,312 B code size, -142 GPRs, +320 threads, -51 fills, -44 scratch, and -235 preamble instructions (up to -101 instructions on mc-packs composite4).
  5. Exact-head verification (d7a02f6dc20ddbc6b8e5494b60fc3d9f866c29d2):
    • Host CI (fedora-mesaci): 156/156 pass (0 fail), agent-policy.sh pr HEAD clean.
    • Physical G17P (G17P B1, kernel 7.3.0-41.fc44.aurora.aarch64, kfault_delta=0, avc_delta=0):
      • 1,024-case subnormal / NaN-payload / signed-zero / overflow FMA hardware oracle (c0b51ef2455 vs d7a02f6dc20): 1024/1024 bit-exact (0 diffs).
      • dEQP-VK.spirv_assembly.instruction.{compute,graphics}.float_controls.fp16.* (including denorm-preserve): 2,252/2,252 supported Pass (1,550 NotSupported, 0 Fail).
      • dEQP-VK.glsl.builtin.precision*.fma.*: 16/16 supported Pass (9 NotSupported, 0 Fail).
      • dEQP-VK.spirv_assembly.instruction.{compute,graphics}.float16.arithmetic.*: 456/456 Pass (0 Fail).

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-ffma-native-f16-src branch from 8c7b126 to d7a02f6 Compare November 22, 2026 11:55
@RyanTheTide

Copy link
Copy Markdown

Thanks, re-checked at d7a02f6:

  • AGX3C scope: fixed. The lowering is keyed on AGX3B only (agx_compile.c:5250) and agx3c_src0_widened covers plain AGX3C.
  • Pack test: fixed. The expected bits are now independent of the encoder. I checked them against agx3b_tables.txt: form f32fma 2/3 selectors, the 10/26/43 width LUT bits (w0.3, w0.8, w1.8) and the group 270 addend width bit (w3.1) all match. Optional: also assert the register fields, so that a wrong odd-half register number cannot pass.
  • Comment and dead !agx_is_g18g() check: fixed.
  • Killed sources: the 10,982-shader sweep with no new refusals answers it.
  • Still open: hardware on the other AGX3B GPUs. Every row is still from G17P (Neo), and the shader-db sweep only compiles, so it cannot show how the operand path treats f16 subnormals, NaN payloads or -0 on G15/G16. Please run, at d7a02f6, the same 1024-case widened-vs-native oracle plus float_controls.fp16.*, precision*.fma.* and float16.arithmetic.* on an M3 (G15G), an M4 (G16G) and an M4 Pro (G16S).

This touches the same agx_optimizer.c block and test files as #707, so whichever lands second needs a rebase.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr

Copy link
Copy Markdown
Author

Exact-head M4 (Apple M4 G16G B0, J773g, Linux 7.3.0-ARCH-m4prod13) hardware verification at d7a02f6dc20ddbc6b8e5494b60fc3d9f866c29d2 (d7a02f6dc20d) vs shared base c0b51ef24554 (dcp74e-pr702r2-pr707r2):

  • Verdict: PASS (0 GPU faults, 0 recoveries, 0 hang kills, 0 dmesg GPU errors).
  • Hardware oracle (agx3b-ffma-f16-denorm): 1,024 / 1,024 32-bit IEEE-754 output words bit-exact (diffs=0) vs base c0b51ef24554; post-RA AGX3B disassembly confirmed native mixed-width ffma with r2l/r2h/r3l/r3h.
  • Vulkan CTS (--jobs 8): 2,799 / 0 total (0 fail/crash/timeout):
    • vk-mustpass-short: 87 / 87
    • float16.arithmetic.*: 456 / 456
    • float_controls.fp16.*: 2,240 / 0 (1,562 NS)
    • precision*.fma.*: 16 / 0 (9 NS)
  • 4-round ABBA gpubench (<55°C, 8 rows — compute.fp16 alu_mix/gemv + compute.conv2d gaussian/bilateral at d=1, 4): 8/8 EXACT CRCs (== base == macOS), neutral perf (0.9970× wall / 0.9964× GPU geomean):
Benchmark Depth Base wall (µs) Head wall (µs) Wall Base GPU (µs) Head GPU (µs) GPU CRC
compute.conv2d (bilateral,1080p) 1 1562.50 1562.50 1.000× 1262.04 1262.21 1.000× 6cdc0716 EXACT
compute.conv2d (bilateral,1080p) 4 1269.04 1269.04 1.000× 1265.79 1265.70 1.000× 6cdc0716 EXACT
compute.conv2d (gaussian,1080p) 1 1070.71 1077.59 0.994× 743.46 743.47 1.000× 2d55a14d EXACT
compute.conv2d (gaussian,1080p) 4 759.91 764.53 0.994× 751.46 751.00 1.001× 2d55a14d EXACT
compute.fp16 (alu_mix) 1 783.70 783.70 1.000× 531.25 531.19 1.000× 5166a42c EXACT
compute.fp16 (alu_mix) 4 534.19 534.19 1.000× 531.19 531.21 1.000× 5166a42c EXACT
compute.fp16 (gemv) 1 467.57 470.37 0.994× 181.50 184.00 0.986× 6d353504 EXACT
compute.fp16 (gemv) 4 190.47 191.54 0.994× 183.08 186.06 0.984× 6d353504 EXACT

Assisted-by: LLM (Gemini:4 Neon)

@RyanTheTide

Copy link
Copy Markdown

Thanks. The M4 row at the exact head closes the hardware question: the denorm/NaN/-0 oracle is 1024/1024 bit-exact against base, fp16 arithmetic/float_controls/fma CTS have 0 failures, and the disassembly shows native mixed-width ffma. The correctness side is done from my side.

One note for the description: the M4 ABBA is neutral (0.997x wall), so the benefit here is the removed widening moves rather than measured time. If the 10,982-shader sweep has instruction-count totals (fmov removed, ALU delta), please add them to the description so the merge is justified by numbers. Approving.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr
DjDeveloperr merged commit acd6874 into aurora-wip Nov 22, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merged into aurora-wip at acd6874dae55 (verified tree 7b2b2248ec85).

All merge gates green on exact head d7a02f6dc20d:

  • Review approved by @RyanTheTide (#issuecomment-7079751142; 10,982-shader AGX3B sweep totals added to PR description)
  • Mirror asahi CI green (45019754703), fedora-mesaci 156/156 pass, agent-policy.sh pr clean
  • Exact-head hardware verification passed on Apple A18 Pro (G17P B1) and Apple M4 (G16G B0): 1,024/1,024 bit-exact FMA oracle, 0 CTS failures, 0 GPU faults

Assisted-by: LLM (Google:Gemini 4 Neon)

DjDeveloperr added a commit that referenced this pull request Nov 22, 2026
Extend #702's agx_lower_agx3_fma_sources() native half-GPR path cleanly
from AGX3B to all non-G18G AGX3 encodings (AGX3B and AGX3C): on AGX3C
(non-G18G), unmodified all-scalar-register F32 FFMA natively encodes
per-operand width bits (src0_16, src1_16, addend_16) via
agx3_falu_fma_mod_runtime(). Skip the redundant f16->f32 widening move
for both SSA and precoloured half GPR sources while keeping AGX3B native
and preserving widening for modified sources, uniform-mixed forms, and
G18G.

Add exact-byte src0_16 encoding coverage in test-agx3-alu-core.c and
update/extend test-native-form-regressions.cpp for AGX3C (including
precoloured AGX_INDEX_REGISTER half sources and end-to-end AGX3C shader
compilation).

Signed-off-by: Diljit Singh <hey@djdev.me>
@RyanTheTide
RyanTheTide deleted the perf/agx3b-ffma-native-f16-src branch November 27, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants