Skip to content

asahi: enable the signed-zero fmin/fmax select lowering on M5 - #1086

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
perf/m5-signed-zero-minmax-select
Nov 25, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
perf/m5-signed-zero-minmax-select

Conversation

@DjDeveloperr

Copy link
Copy Markdown

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's chip == AGX_CHIP_G17P test 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=false builds; clang -Werror passes 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-caps checks 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 fp32 fmin/fmax into bcsel(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 of nir_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.

  • The native and post-lowering preprocess helpers ask the chip's row with agx_chip_has_cap().
  • The M5 helpers (agx_preprocess_nir_m5(), agx_preprocess_nir_m5_defer_idiv()) serve G17G, G17S and G17C, which share one row, so they read AGX_CAPS_M5. Borealis and the Gallium driver both use them on M5, so both get the lowering.
  • G17P keeps exactly what it had: Vulkan through 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 without DenormPreserve) computes min, max, min/max against +0.0 and -0.0, and clamp over 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.

  • With DenormPreserve, every min/max result matches the reference exactly, and the output is bit-identical to the base driver's.
  • Without it, M5's compares flush denormal inputs, so the select against a zero constant returns min(+denormal, +0) = +denormal where 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 -0 on 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_RA regresses: gpubench-vk compute.fp16 alu_mix +6.6% (357 -> 390 instructions, 87 -> 95 GPRs), compute.uber 512 -> 448 threads, Geekbench Gaussian Blur +1.2% and Particle Physics +1.3%.
  • interleave_alu in 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_WORKGROUPS works on M5 (Geekbench dispatches go from 4x to 16x fewer workgroups, gpubench compute.heavy bw 16,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=false build of the candidate. The candidate source on the device is the base fetched from GitHub plus an overlay of the four changed files, checked with git write-tree against tree a4fb2fbc. MESA_SHADER_CACHE_DISABLE=1 for every run.

Arm Commit libvulkan_asahi.so sha256 libgallium-26.3.0-devel.so sha256
A, base aurora-wip 6abdc187888 3779706bc430…53ce d56617ced3ac…5b3b
B, this PR tree a4fb2fbc (head abfa17cdaf5) 5ddec92e9b8a…7d9e 6c8af090b139…eb71

aurora-wip has since moved to 55f33ab11a1; the branch merges cleanly and the new agx_compile.c hunks are far from this change's.

Build and unit checks

Check base this PR
meson test --suite asahi, release 322/322 322/322
same, -Db_ndebug=false — 322/322
new gcc warning lines in rebuilt objects — 0
clang build + -fsyntax-only -Werror: agx_compile.c, agx_nir_lower_fminmax.c, test-chip-caps.c and the header's includers agx_register_allocate.c, hk_shader.c, hk_cmd_dispatch.c, hk_physical_device.c pass pass

The clang harness also checks four untouched files (hk_image.c, hk_cmd_meta.c, agx_pipe.c, layout_core.c): hk_cmd_meta.c fails with the same three pre-existing -Winitializer-overrides diagnostics on both arms.

Geekbench 6.7.1 Vulkan (geekbench6 --gpu Vulkan --no-upload under systemd-run -p PrivateNetwork=yes, nothing uploaded). GPU busy time per measured iteration (median of 20), from HK_FRAME_ANATOMY firmware timestamps, run order 1A 2B 3B 4A, 0 validation errors in every run:

Workload 1A 2B 3B 4A B vs A
Background Blur 2.481 2.477 2.474 2.476 -0.11%
Face Detection 3.138 3.168 3.137 3.129 +0.62%
Horizon Detection 4.890 4.910 4.901 4.897 +0.24%
Edge Detection 3.654 3.641 3.638 3.621 +0.05%
Gaussian Blur 2.382 2.388 2.382 2.392 -0.07%
Feature Matching 6.880 6.828 6.846 6.849 -0.39%
Stereo Matching 0.671 0.670 0.670 0.673 -0.23%
Particle Physics 9.509 9.516 9.515 9.512 +0.06%

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 row status ok with 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:

Row A1 A2 B1 B2
compute.light 106.8 106.3 106.5 106.6
compute.heavy kind=alu 375.9 376.9 377.4 378.3
compute.heavy kind=bw 630.3 628.9 630.3 630.3
compute.uber kind=divergent 354.2 353.8 354.6 354.2
compute.uber kind=uniform 201.9 201.1 201.1 201.6
compute.uber kind=ilp 256.4 256.8 255.8 257.3
compute.fp16 mode=alu_mix 262.5 262.9 262.7 263.2
compute.fp16 mode=gemv 141.7 149.9 143.3 153.6
compute.conv2d kind=gaussian 354.6 354.2 358.4 355.5
compute.edge kind=sobel 261.6 260.6 267.6 264.6
compute.stereo 3750.0 3750.0 3750.0 3750.0
render.light 130.2 130.0 130.2 130.2
render.heavy kind=fill 260.4 260.4 260.6 260.4
render.heavy kind=texture 403.8 404.3 404.3 403.2
render.heavy kind=geom 290.1 291.0 290.7 289.6
render.postfx 389.1 393.2 394.2 391.1

Conformance (deqp-runner, 16 jobs; the same case list on both arms; status compared case by case)

Suite Cases base this PR Status differences
dEQP-VK (vk-default mustpass) 123,353 66,055 pass, 186 fail, 1 flake, 57,111 skip 66,056 pass, 186 fail, 57,111 skip 1: the base's flake
GLES3/GLES31, zink 31,761 31,655 pass, 6 fail, 100 skip same 0
GLES3/GLES31, native asahi 31,761 31,621 pass, 6 fail, 2 warning, 132 skip same 0
  • VK selection: spirv_assembly.instruction.compute.* (19,901, including every float_controls and float_controls2 group), 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), and subgroups.{arithmetic,clustered}.compute.* (subgroup min/max reductions).
  • GLES selection: dEQP-GLES3.functional.shaders.* and dEQP-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)".
  • The 186 VK failures are the same on both arms: robustness2 push and bind cases on 1D sampled images without a format qualifier, 32-bit R, RG and RGBA float and integer formats (158), and spirv_assembly...untyped_pointers...cooperative_matrix...store (28). The 6 GLES failures on both drivers are shaders.preprocessor.conditional_inclusion defined cases.
  • The base's flake was 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-existing dma-fence warning at queue teardown. This branch's runs logged no kernel lines at all.
  • GPU online=1, crashed=0, every queue quarantined=0 before and after the rows; 0 AVC denials; the one failed unit (tailscaled) fails on every boot of this install.

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>
@DjDeveloperr
DjDeveloperr requested a review from a team as a code owner November 25, 2026 20:36

@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 abfa17cdaf5e

Blockers: none

Checked:

  • Hard rules: no Apple material, no chip/board/firmware gates (replaces chip == AGX_CHIP_G17P with AGX_CAP_SIGNED_ZERO_MINMAX_SELECT in agx_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 in src/asahi/compiler/agx_compile.c plus agx_nir_lower_fminmax_signed_zero() in src/asahi/compiler/agx_nir_lower_fminmax.c: non-M5 chips keep identical behavior by construction (G17P stays enabled on native/post-lower paths, every other non-M5 chip stays off), while G17G/G17S/G17C pick up AGX_CAP_SIGNED_ZERO_MINMAX_SELECT through AGX_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 asahi passes 322/322 (including test_signed_zero_minmax_select() in asahi-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, 0 GPU 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, kernel 7.3.0, MESA_SHADER_CACHE_DISABLE=1) Vulkan CTS at abfa17cdaf5e:
    • dEQP-VK.spirv_assembly.instruction.compute.float_controls.fp32.input_args.*{min,max,clamp}*: 42/42 PASS, 0 FAIL
    • dEQP-VK.spirv_assembly.instruction.compute.float_controls2.fp32.input_args.*{min,max}*: 356/358 PASS (2 NotSupported), 0 FAIL
    • dEQP-VK.glsl.builtin.precision.{min,max,clamp}.*: 24/30 PASS (6 NotSupported), 0 FAIL
    • Total: 422 PASS / 0 FAIL, GPU_FAULT_DELTA=0.

@RyanTheTide
RyanTheTide merged commit d3cbba0 into aurora-wip Nov 25, 2026
erikwb added a commit that referenced this pull request Nov 26, 2026
…-sink-fminmax-preamble-bases

Drop the already-landed AGX3C nir_move_load_ssbo and signed-zero fminmax hunks (merged in #647 and #1086), keeping the non-G18G AGX3C place_load_bases_low enablement and its regression test.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@RyanTheTide
RyanTheTide deleted the perf/m5-signed-zero-minmax-select 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.

3 participants