Skip to content

asahi/compiler: fold a negated FFMA multiplicand into a minifloat multiplier - #1096

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
perf/agx3-ffma-neg-minifloat
Nov 25, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
perf/agx3-ffma-neg-minifloat

Conversation

@DjDeveloperr

Copy link
Copy Markdown

What: On AGX3, fma(-x, #k, c) with a minifloat multiplier now flips the immediate's sign bit instead of materializing the negation (a sign-bit move plus an XOR) before the FMA. The exact product's sign is the XOR of the operand signs and the FMA still rounds once, so fma(x, #-k, c) gives the same bits for every x, including zeros, infinities and NaNs. This is the shape of 3 - 2t in smoothstep and of x * 2 - 1 decodes.

Tested: J714s (M5 Pro, G17S), kernel 7.3.0-m5-cdirect15, against aurora-wip 3001252fae9: BeamNG.drive west_coast_usa at 3024×1964 fullscreen 36.02 → 36.04 FPS (no change, as the size of the change predicts), and 4,237 targeted Vulkan CTS cases (FMA, smoothstep, mix, fp32 float controls, unary minus) 4,021 pass / 0 fail / 216 not supported on both builds with 0 per-case differences. --suite asahi 210/0 in a release (b_ndebug=true) and an assert build (clang), and clang -Werror is clean on both touched files.

Risk: two-way door · every AGX3 Mac (M3, M4, A18 Pro, M5): the fold is in the shared AGX3 FMA source legalization, with no chip gate. Run on M5 Pro only.

Not yet: rows on M4, M3 and Neo.

Details

agx_lower_agx3_fma_sources() already materializes a negated or absolute register multiplicand, because AGX3 FMA has no source modifiers. When the other multiplicand is an 8-bit minifloat immediate, the negation now moves into the immediate's sign bit (imm ^= 0x80) and the register source loses its neg. An absolute value is still materialized as before; -|x| keeps only the absolute value.

Offline static stats (drm-shim, release, the same 9 corpora on each chip; base d3cbba0905b, same diff):

chip corpus instrs GPRs spills
G17S (M5) BeamNG (robust) 42,143 → 42,050 (−0.22%, 11 shaders, none worse) = =
G17S Minecraft packs −0.06% (207 better) 2 shaders +GPRs 3,333 → 3,339 (2 shaders)
G17S shaderdb −0.01% (46 better) = =
G17P (Neo) BeamNG (robust) −0.14% (15 better, 1 worse) = =
G16G (M4) BeamNG (robust) −0.30% (16 better) = 713 → 711
all three GB6, gpubench, vk-synth no change

The two Minecraft-pack shaders that gain registers lose 13–23 instructions; the pre-RA scheduler picks a different order for them, and with scheduling off base and this change use the same registers.

The unit test SignedZeroFMA.NegatedMultiplicandFoldsIntoImmediate checks that fma(-x, #k, c) emits no sign-bit move and no XOR, and that -|x| still materializes only the absolute value.

Evidence

Hardware: J714s, 7.3.0-m5-cdirect15, Vulkan-only release builds of 3001252fae9 (base) and this branch, installed into isolated kits (no /usr/lib change).

  • BeamNG.drive 0.40.6 west_coast_usa, DX11/DXVK under FEX, Fullscreen 3024×1964, VSync off, 20 s windows after the level loads: base 36.46 / 35.58 FPS, this branch 36.15 / 35.92 FPS. 0 Mesa errors, 0 GPU faults.
  • Vulkan CTS 1.4.7.1, caselist of 4,237 cases from vk-default: glsl.builtin.precision*.{fma,smoothstep,mix}, glsl.builtin.function.common.{fma,smoothstep,mix}*, spirv_assembly.instruction.{compute,graphics}.float_controls{,2}.fp32.*, glsl.operator.unary_operator.minus*. Both builds: 4,021 / 0 / 216, identical per case.
  • Kernel log over the slot: 0 GPU/MMU faults, 0 err-prio lines, crashed=0, quarantined=0.

…tiplier

On AGX3, fma(-x, #k, #c) has no native form with a modifier, so the
negation was materialized as a sign-bit move and an XOR before the FMA.
The exact product's sign is the XOR of the operand signs and the FMA
still rounds once, so fma(x, #-k, #c) gives the same bits for every x,
including zeros, infinities and NaNs. Flip the minifloat's sign bit
instead. This is the shape of 3 - 2t in smoothstep and of x * 2 - 1
decodes.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested a review from a team as a code owner November 25, 2026 21:53
@RyanTheTide

Copy link
Copy Markdown

69a5673bec6f J616s (Apple M4 Pro (G16S B1), AGX3B, 7.3.0-aurora-fleet89, assert-enabled isolated Borealis overlay 11a96af41dc06f27, MESA_SHADER_CACHE_DISABLE=1, zero reboot) PASS: dEQP-VK.glsl.loops.* 624/624, dEQP-VK.ssbo.layout.single_basic_array.* 351/351, dEQP-VK.glsl.matrix.inverse.* 12/12, dEQP-VK.api.smoke.* 8/8 (995/995 Pass / 0 Fail); 0 assertions, 0 GPU faults, 0 new kernel errors, 0 new AVC, 0 failed units, Enforcing; stock Borealis verified after overlay removal.

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 69a5673bec6f

Blockers: none

Checked:

  • Full diff audited (src/asahi/compiler/agx_compile.c, src/asahi/compiler/test/test-signed-zero-fma.cpp): IEEE 754 product-sign identity holds for fma(-x, #k, c) -> fma(x, #-k, c) across all finite, subnormal, signed-zero, infinite, and NaN operands; minifloat sign-bit flip (imm->value ^= 0x80) preserves single rounding and leaves .abs lowering intact for -|x|.
  • Commit DCO (Signed-off-by) verified; no AI attribution, no Apple or decompiled material, no chip/board/firmware gates, no hardcoded safety limits, Borealis naming clean in prose.
  • CI waiver applied (Mesa repository has no GitHub CI gate); author reports --suite asahi 210/0 pass in release and assert builds plus clean clang -Werror on both touched files.
  • Two-generation hardware rows at head 69a5673bec6f: J616s (M4 Pro, G16S, AGX3B) 995/995 Vulkan CTS pass with 0 assertions/GPU faults/kernel errors/AVC denials/failed units, and J714s (M5 Pro, G17S, AGX3C) built over parent 3001252fae9 with 4,021/0/216 targeted Vulkan CTS parity (0 diffs) and BeamNG.drive A/B 36.02 -> 36.04 FPS with 0 GPU faults.
Evidence
  • Diff review (69a5673bec6f1e48780b791fadaaa0dcc5a63c88, parent 3001252fae9f5f783aed8b1f4b5111f44e930b4f):
    • src/asahi/compiler/agx_compile.c: agx_lower_agx3_fma_sources() already filters unsupported_modifier (requiring modified sources to be non-immediate 32-bit values and immediates unmodified) and skips native modifier-bearing shapes before reaching the fold loop. For s in {0, 1}, when src[s].neg is set on a non-immediate and src[1 - s] is an 8-bit non-memory immediate (<= 0xff), clearing src[s].neg and toggling bit 7 (^= 0x80) moves the negation into the minifloat multiplier's sign bit. At most one iteration matches per instruction, and agx_lower_uniform_sources() runs afterwards.
    • src/asahi/compiler/test/test-signed-zero-fma.cpp: updates SignedZeroFMA.KeepsNativeModifierShapes case 4 comment and adds SignedZeroFMA.NegatedMultiplicandFoldsIntoImmediate covering AGX3B and AGX3C, 7 minifloat values (0x00, 0x80, 0x30, 0x40, 0xb0, 0x7f, 0xff), both operand orders, abs false/true, exact emitted instruction counts, and bit-exact fmaf comparison across 0.0, -0.0, normals, subnormals, max float, and ±INFINITY.
  • Hardware rows:
    • J616s (Apple M4 Pro (G16S B1), AGX3B, 7.3.0-aurora-fleet89, assert-enabled isolated Borealis overlay at 69a5673bec6f): 995/995 Vulkan CTS pass (0 fail, 0 assertions, 0 GPU faults, 0 new kernel errors, 0 new AVC, 0 failed units, Enforcing).
    • J714s (M5 Pro, G17S, AGX3C, 7.3.0-m5-cdirect15, branch over 3001252fae9): BeamNG.drive 36.02 -> 36.04 FPS, 4,237 targeted Vulkan CTS cases 4,021 pass / 0 fail / 216 not supported (0 per-case diffs), 0 GPU/MMU faults, 0 err-prio lines.

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.

3 participants