Skip to content

asahi: promote loop-weighted M5 select/FMA constants and reuse rodata - #1564

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
feat/agx-loop-const-promote
Dec 3, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
feat/agx-loop-const-promote

Conversation

@erikwb

@erikwb erikwb commented Dec 3, 2026 •

Copy link
Copy Markdown

What: Weight constant-promotion priority by loop nesting depth (1 << (3 * depth)) so hot loop constants win uniform slots over cold outside-loop constants when the uniform file fills, promote M5 (AGX3C) constants directly into native uniform-operand FCMPSEL (including tied fmin/fmax), canonicalized left-constant ICMPSEL, and paired FFMA (R*U+U / R.neg*U+U) instructions, fold literal modifiers when materializing modified M5 FMA immediates, and let the late M5 float promotion round reuse already-uploaded rodata constants.

Tested: J716c M5 Max (G17C), J713 M4 Pro (G16G), and J313 M1 (G13G) at 8bf7dce78d1a (Borealis vs e4346bdb3a55 on unified daily kernel 7.3.0-daily2+): 15,947 Vulkan CTS cases each (ssbo, glsl.operator.*, glsl.builtin.function.common.*, spirv_assembly.*.opselect.*) — 15,860 pass / 0 fail / 87 not supported, 0 diffs, 0 GPU errors on all three Macs; host agx_tests 1,973/1,973 pass (1 skipped, same as base).

Risk: two-way door · all Macs (loop-depth weighting runs on AGX2/AGX3B/AGX3C; new FMA constant promotion, modified-FMA modifier folding and late-round rodata reuse apply to AGX3C, G18G included; select constant promotion applies to M5 only, not emit_agx3d / G18G).

Not yet: J716c 60,838-case compute CTS, 4-round Cyberpunk replay screen, and 1+1 Cyberpunk 2.5 T2 A/B ran on e6e80f1b2ca3 prior to adding the retry-path (!ctx->agx3_reg_float_consts) and FP32 normal-only swap guards in 8bf7dce78d1a (normal-path finite-normal shaders are unchanged); G18G hardware was not tested.

Details
  • Loop-depth weighting (agx_opt_promote_constants.c): Generalized build_in_loop_map() to build_loop_depth_map() (depth capped at 4) and replaced flat per-use counts with uint64_t weighted_uses incremented by 1u << (3u * depth) (1, 8, 64, 512, 4096), preserving AGX3B's in-loop half-float uniform exclusion and inline-blend g17p_rodata_hint compatibility.
  • M5 ICMPSEL constant promotion: Exported agx3_m5_isel_native_uniform() (excluding emit_agx3d) and added m5_isel_best_mask() so agx_opt_promote_constants() promotes single or multi-constant native 32-bit ICMPSEL operands and canonicalizes const op reg to reg op' uniform (reversing ULT/UGT and SLT/SGT) directly during promotion.
  • M5 FCMPSEL constant promotion: Exported agx3_m5_fcmpsel_native_uniform() (querying the shared agx3_m5_fcmpsel_gpu1_desc() + agx3_alu_fcmp_select32_gpu1() encoder) so Borealis promotes constants into general 1- and 2-uniform FCMPSEL forms as well as tied fmin/fmax (LTN/GTN). Left-comparand tied fmin/fmax ((C, x, C, x)) is canonicalized to (x, u, x, u) only for finite normal FP32 constants (excluding ±0.0, subnormals, ±Inf, and NaN) and skipped on the agx3_reg_float_consts retry path so retry's LE/GE rewrite preserves NaN behavior.
  • M5 paired FFMA promotion and modifier folding: Exported agx3_m5_float_native_uniform() and added m5_constant_fma_pair() in both rounds to promote (multiplicand, addend) constant pairs into native R*U+U and R.neg*U+U forms. In agx3_lower_m5_modified_fma_immediates(), folded literal.abs and literal.neg directly into the decoded 32-bit float bit pattern (& 0x7fffffffu, ^ 0x80000000u) so the late M5 float round can promote unmodified constants such as R*R + (-1.0f).
  • Late-round rodata reuse: In agx_opt_promote_m5_float_constants(), scan already-uploaded aligned entries in ctx->rodata before allocating new uniform slots (including when push_count has already reached 512), and continue past oversized constants instead of stopping early on new_count > max_uniforms.
Evidence

Exact-head host tests (8bf7dce78d1a)

  • agx_tests: 1,973 passed, 0 failed, 1 skipped (NativeFMA.ExactDyadicOracle, same as base e4346bdb3a55), including new unit tests AGX3UniformALU.M5LoopWeightedConstantPromotionAndRodataReuse and AGX3UniformALU.M5PromoteIselLeftConstantAndFcmpselAndFmaPair (covering loop priority, push_count == 512 rodata + FMA-pair reuse, left-constant ICMPSEL + G18G exclusion, general and tied FCMPSEL with ±0.0/subnormal/NaN/retry guards, paired FFMA, and modified-FMA literal modifier folding).

Exact-head Vulkan CTS A/B (e4346bdb3a55 .so d1f6bd527e89 vs 8bf7dce78d1a .so 07ad779abf17, kernel 7.3.0-daily2+, aurora-cts-ab)

Suites: ssbo (12,225), dEQP-VK.glsl.operator.* (3,630), dEQP-VK.glsl.builtin.function.common.* (80), dEQP-VK.spirv_assembly.instruction.*.opselect.* (12).

Mac GPU Cases Base Pass Head Pass Diffs after retry dmesg GPU errors
J716c G17C (M5 Max) 15,947 15,860 15,860 0 0
J713 G16G (M4 Pro) 15,947 15,860 15,860 0 0
J313 G13G (M1) 15,947 15,860 15,860 0 0

Additional J716c verification on e6e80f1b2ca3 (.so 8c222f1df107 vs e4346bdb3a55 .so d1f6bd527e89)

  • J716c compute + glsl CTS (aurora-cts-ab, 64,548 cases): 32,855 pass, 24 pre-existing fail, 198 pre-existing internal error, 31,471 not supported on both arms — 0 diffs after retry, 0 GPU errors.
  • Offline G17C 788-shader Cyberpunk 2077 shaderdb census (e4346bdb3a55 vs e6e80f1b2ca3):
    • 589/788 shaders changed: -3,629 instrs, -3,533 ALU, -34,790 code bytes, -3,179 native ldimm, -587 loop immediates, -6 loop uniform copies; 19 occupancy gains vs 2 losses.
  • J716c 4-round native Cyberpunk replay A/B (replay-ab.py, 7.3.0-daily2+):
    • fragment_ms: 8.492±0.046 → 8.437±0.035 ms (-0.65%, t = -2.7)
    • compute_ms: 18.533±0.102 → 18.494±0.089 ms (-0.21%, t = -0.8)
    • sum_ms: 30.491±0.148 → 30.398±0.112 ms (-0.30%, t = -1.4)
    • replay_fps: 24.171±0.132 → 24.207±0.112 (+0.15%, t = +0.6), 0 GPU errors.
  • J716c Cyberpunk 2077 2.5 T2 (2560×1600, run-ab.py, 7.3.0-daily2+, cur-fex-main-vkmain, 1+1 A/B):
    • Base (d1f6bd527e89): 55.36 FPS (min 46.36, V=2.09 ms F=6.68 ms C=17.37 ms)
    • Candidate (8c222f1df107): 55.66 FPS (min 46.99, V=2.09 ms F=6.61 ms C=17.25 ms), +0.30 FPS (+0.54%), 0 GPU faults.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb requested review from a team and DjDeveloperr as code owners December 3, 2026 01:11
The mask searches cover every compare-select operand M5 reads from the
uniform file, so the per-source hooks were unreachable there. The FCMPSEL
one still promoted a first comparand on G18G, which keeps its own select
forms. Remove both hooks.

Two constants of one value share a uniform, which could turn an unordered
LTN/GTN select into a tied min/max with a different NaN result. Refuse
that, using the shared tied min/max predicate.

Test every reversed ICMPSEL condition, the G18G comparand, the NaN case,
and that late rodata reuse compares the whole word.

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

erikwb commented Dec 3, 2026

Copy link
Copy Markdown
Author

We carry this PR in our unified portable daily Mesa build across J716c (M5 Max, T6050), J713 (M4 Air, T8132), and J313 (M1 Air, T8103).

Loop-weighted constant promotion for M5 FCMPSEL/ICMPSEL/FFMA and rodata reuse shrinks hot inner-loop shaders in Borealis (-3,629 instructions and -587 loop immediates across 788 Cyberpunk shaders), contributing to our 60.14 FPS Cyberpunk 2077 2.5 T2 run on J716c with zero GPU faults.

Verified across J716c, J713, and J313 (desktop + Vulkan CTS smoke, zero GPU errors).

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: FINISH, then merge, at c8e980ef281c. Only hardware rows are missing; no open code finding. We pushed a small fix commit on top of 8bf7dce78d1a, so this still needs an approval from someone other than DjDeveloperr, or an admin merge.
Blockers: rows at this head: J714s (M5 Pro, G17S) CTS plus a perf A/B, and a no-regression run on J773g (M4, G16G) and J700 (A18 Pro, G17P). We are queuing all three on our Macs; nothing for you to do.

Fixed in c8e980ef281c (plain commit, 69 lines out, 7 code lines in, one test):

  1. agx_opt_promote_constants.c constant_use_accepts_uniform(): the new FCMPSEL line has no effect on M5, because the mask search already covers it. It did promote on G18G: 286 GTA V vertex shaders changed on M6, although the description says selects stay off there. Removed, together with the ICMPSEL line that the mask search also replaces.
  2. (x LTN c) ? x : c' with two constants of one value became a tied min/max after promotion, which returns c instead of the NaN. fmin/fmax always arrive tied, so as far as I can see no shader reaches this today. It now uses the existing agx_fcmpsel_is_tied_minmax() for both checks.
  3. One test: every reversed ICMPSEL condition (the signed pair was not covered), the G18G comparand, the NaN case, and the late round comparing the whole constant and keeping its base. It fails on 8bf7dce78d1a and on each of these broken again.

Shader statistics at c8e980ef281c equal 8bf7dce78d1a for all 26,443 GTA V executables on G17S and 4,429 each on G16G, G17P and G13X.

Checked: the encodings each promoted form uses, NaN, signed zero and denormals, 16/32 bit, the retry path, a full uniform file, rodata reuse, loop weights on AGX2/AGX3B. The G18G gate is the emit_agx3d ISA bit. No Apple material, no chip gate, commits and sign-offs fine. I corrected the Risk line: M6 does get the FMA pairs, modifier folding, late rodata reuse and loop weights (328 of 12,720 GTA V executables change, 300 shorter, 6 longer), only the select promotion is off.

Evidence and notes
  • x86-64, asserts on, agx_tests: base e4346bdb3a55 1,971 passed, 8bf7dce78d1a 1,973, c8e980ef281c 1,974, 1 skipped each. asahi, poly and vulkan suites: 464 ok, 0 failed on base and 8bf7dce78d1a; 463 ok and one timeout under load on c8e980ef281c (borealis-sync-runner, passes 3 of 3 on its own).
  • 400,000 random shaders (M5, G18G, AGX3B, AGX2; push counts up to 512; 81 million evaluations with NaN, ±0, denormals, ±Inf, equal and opposite inputs, retry rewrite included): 0 wrong values apart from item 2, 0 promoted constants copied back to a register. 1.2 million select and 250,000 FMA forms the SSA query admits all pack with random registers, lifetimes and waits.
  • Mutations: 35 changes to the new rules at 8bf7dce78d1a. 15 fail your tests; of the 20 that don't, 5 change a value: signed reversal, half-word rodata compare, late base reset, inverted tied select, abs of a negative literal. The first three are in the new test, the fourth now goes through the shared helper, the last is still untested. The others are equivalent, or only cost a promotion.
  • Offline, base against head, GTA V fossils:
executables changed instructions shorter / longer other
G17S 26,443 13,538 -0.287 % 10,626 / 1,246 threads 247 up, 12 down; spills 22,701 to 22,696; uniforms +0.81 %; immediate loads -13.5 %
G18G (at c8e980ef281c) 12,720 328 -0.015 % 300 / 6
G17P 4,429 6 -13 4 / 2
G16G 4,429 1 -4 1 / 0
G15S 4,429 1 +12 0 / 1
G13X, G14G 4,429 66 -46 28 / 11 loop immediates -5.5 %
  • A second G17S set (12,897 executables): -0.318 %, 5,492 shorter, 465 longer, threads 211 up, 18 down.
  • Nearly all of the G17S change is the float select (about 25,800 of the 26,676 instructions saved, and most of the longer ones: one fewer instruction before register allocation, then extra copies). FMA pairs -385, left-constant integer select -61, loop weights 21 executables; the literal folding changes none of this set.
  • Compile cost, user instructions over 400 graphics and 60 compute pipelines: G17S +0.06 %, G16G and G13X +0.02 %.
  • Performance: instruction counts are not a speed claim. The Cyberpunk and replay numbers ran on e6e80f1b2ca3, which is not published, and the Cyberpunk one is a single run per arm (+0.54 %). The J714s A/B decides whether this is a measured win or a merge without an fps claim.
  • Your CTS rows: the groups and 15,947 count match CTS 1.4.7.1 (builtin.function.common has 81 there, you list 80). Our rows add float_controls, float_controls2 and glsl.builtin.precision, which test the NaN, denormal and signed-zero rules of min/max.
  • Not blocking: the FMA pair replacement is the same 25 lines in both rounds and would fit one helper.

@DjDeveloperr

Copy link
Copy Markdown

J773g (Apple M4 Mac mini, Mac16,10, T8132, G16G B0, os-fw 27.2 / 26B123) row at exact head c8e980ef281c vs base e4346bdb3a55 — PASS (no regression).

  • Kernel: 7.3.0-ARCH-m4main-6c83a2a6 (boot_id=79ca11a7-ef0b-4797-9ce0-5215845ae0a9, zero-reboot on stock).
  • Artifacts:
    • base git-e4346bdb3a (libvulkan_asahi.so sha256 cde68937954b...)
    • head git-c8e980ef28 (libvulkan_asahi.so sha256 0d384133c557...)
    • caselist.txt (31,771 cases, sha256 7128ab2ab218...)
  • dEQP-VK CTS (serial base then head):
    • base: 26,332 Pass / 0 Fail / 5,439 NotSupported / 0 Warnings / 0 Waived
    • head: 26,332 Pass / 0 Fail / 5,439 NotSupported / 0 Warnings / 0 Waived
    • CTS_DIFF_LINES=0 (exact status match across all 31,771 cases)
  • GPUBench (compute.* 20 configs + render.light, ABBA base head head base, --verify, steady effective S10 across slots):
    • Verification: 168/168 rows valid=true with exact crc == expected match.
    • Performance (ABBA mean us_per_submit / gpu_us): no regression across all 42 rows (38/42 within -0.93% to +0.39%; compute.uber d=4 474.778 -> 474.778 us +0.00%, compute.heavy alu d=4 552.870 -> 552.870 us +0.00%, compute.heavy bw d=4 1308.261 -> 1308.261 us +0.00%, compute.desc_search d=4 364.223 -> 365.631 us +0.39%, compute.small_dispatches 1227.955 -> 1227.949 us -0.00%, compute.edge harris d=4 650.434 -> 645.686 us -0.73%; minor single-slot noise on compute.chain d=1 / compute.tile_reduce d=4 1080p / render.light had head Slot 3 matching or beating base).
  • Health: 0 new GPU faults (FAULTS delta=0).

@DjDeveloperr

Copy link
Copy Markdown

J700 (MacBook Neo, A18 Pro, G17P B1): PASS at c8e980ef281c, no regression

Stock Fedora 44 kernel 7.3.0-41 (feature word 0x0000007a2001fc06, soft faults off), zero reboot, userspace only. Base is the merge base e4346bdb3a55. Both arms are cross-builds with identical options, loaded per process. Shader cache off. The row ran in two slots on 2026-12-03; every unit was counted once.

Part Result
CTS, the recipe's 31,771 cases 31,771 of 31,771 compared: 26,332 Pass and 5,439 NotSupported on both arms, 0 status differences
GPUBench, 42 points, 6 ABBA rounds (264 runs) 0 points outside the base spread. One point was outside after three rounds and not after six, so the extra rounds it earned decided it
Compiled code the PR does change compiled code here: 5,932 shader parts on each arm, 2 only in the base and 2 only in the head
Kernel log 0 GPU-side lines, 0 WARN

Caveats: the J700 was hot (117 C hottest sensor) with a cooling device active and the CPU frequency caps moving through the run, in both arms. The row compares arms inside that. It says no regression; it isn't a speed claim (the compile-time changes in the recipe's x86 data aren't measured on hardware here).

@DjDeveloperr

Copy link
Copy Markdown

J714s (M5 Pro, T6050, G17S B1, AGX3C, stock 7.3.0-m5-pr67, boot eca1eecc, SLC ON 16 planes, headless) #1564 c8e980ef281c06081a9f01923a1bc02c1c93c12e vs base e4346bdb3a55dbef2c2cf81d0c860b28d1e6c060: PASS (dEQP-VK 31,771-case caselist.txt 7128ab2ab218 incl. float_controls* & *opselect: 26,332 Pass / 5,439 NS / 0 Fail on both arms, 0 diffs; GPUBench --verify ABBA x3 n=6/6 across all 45 configs (20 compute.* + render.deferred/postfx/light): bad_ok=0, bad_crc=0, wins=0, regressions=0; 0 new GPU/kernel faults; DSOs base cde68937954b, head 0d384133c557).

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: clean at c8e980ef281c, merging. A second, independent read of the pushed commit found nothing.

Checked

  • The two removed per-operand lines are covered: the integer-select one by the mask search, with the same encoder query; the float-select one is new in this PR and does nothing on M5 that the mask search doesn't. G18G keeps no select promotion, as on base.
  • agx_fcmpsel_is_tied_minmax() in both rounds, and the equality check before and after promotion: two constants in one uniform can't turn an unordered select into a min/max. While counting uses, every operand probes the same slot, so the check can only refuse a promotion there, not allow a wrong one.
  • Reversed ICMPSEL conditions, the late round's constant reuse, and the literal abs/neg folding.
  • None of the files changed on aurora-wip since the merge base are in this PR. #1567 and #1568 change shift masks, range bounds and shift packing, not constant promotion or selects.

Merge tree: ae6d93c0a130 (aurora-wip 1d87219a0716, with #1565, + this head). x86-64, asserts on, drm-shim: asahi + poly + vulkan 465 ok, 0 failed, 1 skipped; agx_tests 1,979 passed, 1 skipped (three more than without this PR). Run on our own machine, not hosted CI.

Rows: J773g, J700 and J714s at this head, 31,771 CTS cases each, 0 differences; GPUBench on J714s (45 configs) shows no win and no regression. So this merges without an fps claim from us.

@DjDeveloperr
DjDeveloperr merged commit b8a216b into aurora-wip Dec 3, 2026
@DjDeveloperr
DjDeveloperr deleted the feat/agx-loop-const-promote branch December 3, 2026 05:42
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