Repository navigation
asahi/compiler: fold a negated FFMA multiplicand into a minifloat multiplier - #1096
Merged
Merged
Conversation
…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>
|
|
satchlj
approved these changes
Nov 25, 2026
satchlj
left a comment
Member
There was a problem hiding this comment.
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 forfma(-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.abslowering 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 asahi210/0 pass in release and assert builds plus clean clang-Werroron 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 parent3001252fae9with 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, parent3001252fae9f5f783aed8b1f4b5111f44e930b4f):src/asahi/compiler/agx_compile.c:agx_lower_agx3_fma_sources()already filtersunsupported_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. Fors in {0, 1}, whensrc[s].negis set on a non-immediate andsrc[1 - s]is an 8-bit non-memory immediate (<= 0xff), clearingsrc[s].negand toggling bit 7 (^= 0x80) moves the negation into the minifloat multiplier's sign bit. At most one iteration matches per instruction, andagx_lower_uniform_sources()runs afterwards.src/asahi/compiler/test/test-signed-zero-fma.cpp: updatesSignedZeroFMA.KeepsNativeModifierShapescase 4 comment and addsSignedZeroFMA.NegatedMultiplicandFoldsIntoImmediatecovering AGX3B and AGX3C, 7 minifloat values (0x00,0x80,0x30,0x40,0xb0,0x7f,0xff), both operand orders,absfalse/true, exact emitted instruction counts, and bit-exactfmafcomparison across0.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 at69a5673bec6f): 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 over3001252fae9): 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.
- J616s (
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, sofma(x, #-k, c)gives the same bits for everyx, including zeros, infinities and NaNs. This is the shape of3 - 2tin smoothstep and ofx * 2 - 1decodes.Tested: J714s (M5 Pro, G17S), kernel
7.3.0-m5-cdirect15, againstaurora-wip3001252fae9: BeamNG.drivewest_coast_usaat 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 asahi210/0 in a release (b_ndebug=true) and an assert build (clang), and clang-Werroris 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 itsneg. 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):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.NegatedMultiplicandFoldsIntoImmediatechecks thatfma(-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 of3001252fae9(base) and this branch, installed into isolated kits (no/usr/libchange).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.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.crashed=0,quarantined=0.