Repository navigation
asahi: enable the signed-zero fmin/fmax select lowering on M5 - #1086
Merged
Merged
Conversation
agx_preprocess_nir_internal() ran agx_nir_lower_fminmax_signed_zero() (and nir_opt_fp_math_ctrl() before it) only when its helper was asked for AGX_CHIP_G17P. Name what the lowering needs instead: AGX_CAP_SIGNED_ZERO_MINMAX_SELECT, the fp32 compares ordering denormals exactly whenever the shader preserves them, so a signed-zero fmin/fmax can be the hardware min/max with an integer min/max on equal operands. The capability is introduced with exactly the chip of the test it replaces (G17P), and test-chip-caps checks that on every chip. The native and post-lowering helpers ask the chip's row; the M5 helpers, which serve G17G, G17S and G17C, read the shared AGX_CAPS_M5 row, which does not carry the capability yet. No chip changes behaviour. Signed-off-by: Diljit Singh <hey@djdev.me>
Add AGX_CAP_SIGNED_ZERO_MINMAX_SELECT to the M5 row (G17G, G17S, G17C). Borealis and the Gallium driver then lower a signed-zero preserving fp32 fmin/fmax on M5 with agx_nir_lower_fminmax_signed_zero() (the hardware min/max, with an integer min/max of the bits on equal operands) instead of nir_lower_alu's generic emulation, and run nir_opt_fp_math_ctrl() first to drop the flag where no use can tell -0 from +0. G17P is unchanged; every other chip keeps the generic lowering. The lowering needs the fp32 compares to order denormals exactly when the shader preserves them. Measured on an M5 Pro (G17S) with a compute shader that declares SignedZeroInfNanPreserve, with and without DenormPreserve, and takes min, max, min/max against +0 and -0, and clamp over every pair of +-0, two denormals of each sign, the largest denormal, the smallest normal, +-1, +-inf and a NaN: - With DenormPreserve, every min/max result is the IEEE 754-2019 minimumNumber/maximumNumber and bit-identical to the generic lowering. - Without it, the compares flush denormal inputs, so min(+denormal, +0) returns the denormal: it differs from the exact result only by a denormal, which Vulkan lets either way flush. On the same machine, Geekbench 6.7.1 Vulkan's Feature Matching has six kernels that use it (820 -> 816 instructions each); its GPU time drops 0.4%. No other Geekbench or gpubench-vk program changes. 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 abfa17cdaf5e
Blockers: none
Checked:
- Hard rules: no Apple material, no chip/board/firmware gates (replaces
chip == AGX_CHIP_G17PwithAGX_CAP_SIGNED_ZERO_MINMAX_SELECTinagx_chip_caps.h), no hardcoded safety limits, human DCO on both commits (Signed-off-by: Diljit Singh), zero AI attribution. - Full diff and all
agx_preprocess_nir_internal()call sites insrc/asahi/compiler/agx_compile.cplusagx_nir_lower_fminmax_signed_zero()insrc/asahi/compiler/agx_nir_lower_fminmax.c: non-M5 chips keep identical behavior by construction (G17Pstays enabled on native/post-lower paths, every other non-M5 chip stays off), whileG17G/G17S/G17Cpick upAGX_CAP_SIGNED_ZERO_MINMAX_SELECTthroughAGX_CAPS_M5. - Exact-head native GCC 16.1.1 assert-enabled build (
-Dbuildtype=release -Db_ndebug=false -Dbuild-tests=true -Dtools=drm-shim):meson test --suite asahipasses322/322(includingtest_signed_zero_minmax_select()inasahi-agx-chip-caps). - Exact-head live hardware rows on two M5 parts: author's M5 Pro (
J714s,G17S B1) conformance + Geekbench ABBA + signed-zero/denormal oracle, plus this review's exact-head Mac Studio M5 Max (T6050 / J775c,G17C B1) Vulkan CTS (422 PASS / 0 FAIL,0GPU faults).
Evidence
- Build & unit tests at
abfa17cdaf5e: GCC 16.1.1 (-Dbuildtype=release -Db_ndebug=false -Dbuild-tests=true -Dtools=drm-shim),MALLOC_PERTURB_=42 meson test -C build --suite asahi --print-errorlogs: 322 ok / 0 fail. - Live Mac Studio M5 Max (
T6050 / J775c,G17C B1, kernel7.3.0,MESA_SHADER_CACHE_DISABLE=1) Vulkan CTS atabfa17cdaf5e:dEQP-VK.spirv_assembly.instruction.compute.float_controls.fp32.input_args.*{min,max,clamp}*: 42/42 PASS, 0 FAILdEQP-VK.spirv_assembly.instruction.compute.float_controls2.fp32.input_args.*{min,max}*: 356/358 PASS (2 NotSupported), 0 FAILdEQP-VK.glsl.builtin.precision.{min,max,clamp}.*: 24/30 PASS (6 NotSupported), 0 FAIL- Total: 422 PASS / 0 FAIL,
GPU_FAULT_DELTA=0.
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: Enable the signed-zero fmin/fmax select lowering (
agx_nir_lower_fminmax_signed_zero, until now G17P only) on M5 (G17G, G17S, G17C). The first commit replaces the compiler'schip == AGX_CHIP_G17Ptest with a capability,AGX_CAP_SIGNED_ZERO_MINMAX_SELECT, carried by the G17P row; it changes no chip. The second adds the capability to the M5 row. On M5 the lowering is exact and saves Geekbench 6 Feature Matching 0.4% of GPU time.Tested: J714s M5 Pro (G17S B1), kernel 7.3.0-m5-cdirect15, at abfa17c (tree a4fb2fbc, the exact tree of every row below) against its base aurora-wip 6abdc18: asahi unit suite 322/322 in release and
-Db_ndebug=falsebuilds; clang-Werrorpasses on every touched file; dEQP-VK 123,353 cases, GLES3/GLES31 31,761 cases on zink and on native asahi, each with 0 status differences from the base; 0 GPU faults on this branch's runs. Same-machine Geekbench 6.7.1 Vulkan ABBA: Feature Matching -0.39% GPU time, the six Feature Matching kernels 820 -> 816 instructions each; every other Geekbench and gpubench-vk program is byte-identical. Other chips: generated code unchanged by construction (test-chip-capschecks the capability row of every chip).Risk: two-way · M5 (G17G, G17S, G17C), Borealis and the Gallium driver
Not yet: G17G and G17C rows (G17S only here). A second-generation hardware row for the shared compiler file, whose generated code is unchanged off M5. A DXVK or vkd3d-proton title on M5 (they mark every float op signed-zero preserving, so they take this path most).
Details
What the lowering needs.
agx_nir_lower_fminmax_signed_zero()turns a signed-zero preserving fp32fmin/fmaxintobcsel(a == b, imin(a, b), fmin_agx(a, b))(or the bare hardware min/max against a nonzero constant, or one ordered select against a zero constant), in place ofnir_lower_alu's generic emulation of up to 9 instructions. It is exact only if the fp32 compares order denormals exactly when the shader preserves them. The new capability names that property; the compiler asks for it instead of for a chip.Where it applies.
agx_chip_has_cap().agx_preprocess_nir_m5(),agx_preprocess_nir_m5_defer_idiv()) serve G17G, G17S and G17C, which share one row, so they readAGX_CAPS_M5. Borealis and the Gallium driver both use them on M5, so both get the lowering.agx_preprocess_nir_native(); its Gallium path takes no chip and keeps the generic emulation. G15, G16 and G18G have no capability and are unchanged.M5 compare semantics, measured. A compute shader declaring
SignedZeroInfNanPreserve(with and withoutDenormPreserve) computesmin,max,min/maxagainst+0.0and-0.0, andclampover every pair of 17 values: +-0, two denormals of each sign, the largest denormal, the smallest normal, +-1, +-inf and a quiet NaN. The CPU reference is IEEE 754-2019 minimumNumber/maximumNumber.DenormPreserve, every min/max result matches the reference exactly, and the output is bit-identical to the base driver's.min(+denormal, +0) = +denormalwhere the base returns+0. That is the only difference (102 bytes of the dump): a denormal in place of a zero of the same sign, which Vulkan allows here because denormals may be flushed.clamp(+0, -0, 1)returns-0on both builds; that FClamp signed-zero tie is not touched by this change.Why only this gate. The other G17P-only gates were screened on the same machine in the same way, one toggle per build against the same base, and stay off on M5:
AGX_CAP_MOD3_REG_BANK_RAregresses: gpubench-vkcompute.fp16 alu_mix+6.6% (357 -> 390 instructions, 87 -> 95 GPRs),compute.uber512 -> 448 threads, Geekbench Gaussian Blur +1.2% and Particle Physics +1.3%.interleave_aluin pure-ALU loop blocks changes no program: M5 compute blocks take the compute load-hoisting schedule before that call site.AGX_CAP_COALESCE_PURE_GLOBAL_WORKGROUPSworks on M5 (Geekbench dispatches go from 4x to 16x fewer workgroups, gpubenchcompute.heavy bw16,384 -> 8,192, CRCs unchanged) but moves no workload by more than 0.2%.Evidence
Machine: J714s M5 Pro (G17S B1), Arch Linux ARM, kernel 7.3.0-m5-cdirect15 (unchanged, no reboot), MTE and SEP writes on, headless (surfaceless EGL, no window on any display). Native builds on the J714s (
-j18, ccache):--libdir=lib -Dgallium-drivers=asahi,zink -Dvulkan-drivers=asahi -Dplatforms= -Dtools=drm-shim -Dbuild-tests=true, release (-Dbuildtype=release -Db_ndebug=true) plus a-Db_ndebug=falsebuild of the candidate. The candidate source on the device is the base fetched from GitHub plus an overlay of the four changed files, checked withgit write-treeagainst treea4fb2fbc.MESA_SHADER_CACHE_DISABLE=1for every run.libvulkan_asahi.sosha256libgallium-26.3.0-devel.sosha2566abdc1878883779706bc430…53ced56617ced3ac…5b3ba4fb2fbc(headabfa17cdaf5)5ddec92e9b8a…7d9e6c8af090b139…eb71aurora-wiphas since moved to55f33ab11a1; the branch merges cleanly and the newagx_compile.chunks are far from this change's.Build and unit checks
meson test --suite asahi, release-Db_ndebug=false-fsyntax-only -Werror:agx_compile.c,agx_nir_lower_fminmax.c,test-chip-caps.cand the header's includersagx_register_allocate.c,hk_shader.c,hk_cmd_dispatch.c,hk_physical_device.cThe clang harness also checks four untouched files (
hk_image.c,hk_cmd_meta.c,agx_pipe.c,layout_core.c):hk_cmd_meta.cfails with the same three pre-existing-Winitializer-overridesdiagnostics on both arms.Geekbench 6.7.1 Vulkan (
geekbench6 --gpu Vulkan --no-uploadundersystemd-run -p PrivateNetwork=yes, nothing uploaded). GPU busy time per measured iteration (median of 20), fromHK_FRAME_ANATOMYfirmware timestamps, run order 1A 2B 3B 4A, 0 validation errors in every run:Program signature from the same captures: Feature Matching's six signed-zero kernels compile to 816 instead of 820 instructions (51 GPRs, 1024 threads, unchanged), and every program of the other seven workloads is byte-identical. Their differences are run-to-run variation, as Face Detection's single 2B outlier shows. In the earlier one-toggle screen on the same base, Feature Matching read 6.821 ms against base runs of 6.860 and 6.862 ms: all three candidate runs sit below all four base runs.
gpubench-vk
ae90a40(--mode queue --depths 1 --warmup-ms 150 --runs 3 --run-ms 300), us per submit, run order A1 B1 B2 A2, every rowstatus okwith the same CRC on both arms. Every program of all 16 rows (39 programs) is byte-identical between the arms, so the differences below are run-to-run variation:Conformance (deqp-runner, 16 jobs; the same case list on both arms; status compared case by case)
spirv_assembly.instruction.compute.*(19,901, including everyfloat_controlsandfloat_controls2group),spirv_assembly.instruction.graphics.float_controls*(8,954),glsl.builtin.*(precision and common functions, including min/max/clamp),glsl.operator.*,glsl.{loops,functions,switch,conditionals,return,arrays,struct,indexing}.*,compute.pipeline.{basic,indirect_dispatch,builtin_var}.*,robustness.robustness2.*(82,600), andsubgroups.{arithmetic,clustered}.compute.*(subgroup min/max reductions).dEQP-GLES3.functional.shaders.*anddEQP-GLES31.functional.{compute,ssbo,shaders}.*. zink ran Borealis ("zink Vulkan 1.4(Apple M5 Pro (G17S B1) (AURORA_BOREALIS))"); native asahi reported "Apple M5 Pro (G17S B1)".robustness2push and bind cases on 1D sampled images without a format qualifier, 32-bit R, RG and RGBA float and integer formats (158), andspirv_assembly...untyped_pointers...cooperative_matrix...store(28). The 6 GLES failures on both drivers areshaders.preprocessor.conditional_inclusiondefinedcases.spirv_assembly.instruction.graphics.float_controls.fp16.generated_args.composite_zero_preserve_nostorage_vert, a 2 s compute-watchdog timeout that passed on retry, with the pre-existingdma-fencewarning at queue teardown. This branch's runs logged no kernel lines at all.online=1,crashed=0, every queuequarantined=0before and after the rows; 0 AVC denials; the one failed unit (tailscaled) fails on every boot of this install.