Skip to content

asahi,borealis: speed up cold AGX3/Borealis pipeline compilation - #1245

Merged
RyanTheTide merged 5 commits into
aurora-wipfrom
perf/agx3-pso-compile-fastpaths
Nov 27, 2026
Merged

RyanTheTide merged 5 commits into
aurora-wipfrom
perf/agx3-pso-compile-fastpaths

Conversation

@DjDeveloperr

Copy link
Copy Markdown

Summary

Eliminate wasted CPU time during cold Vulkan pipeline compilation (gpubench-vk compile.pso compute, graphics, and uber) across NIR, the AGX3 backend/binder, and Borealis with zero shader-codegen regressions:

  1. NIR & AGX3 compiler/scheduler fast paths (0d342cb6e57):

    • nir_opt_fp_math_ctrl: clear nir_fp_preserve_signed_zero without FP range analysis when no consumer observes -0, and early-out when no ALU, derivative, or IO load can change.
    • agx_compile: gate loop-only and multi-block-only NIR passes on agx_nir_has_loop() / agx_nir_is_single_block(), skip cloning in agx_compile_shader_nir_unrolled() when loop-free, gate agx_nir_opt_shared_byte_offsets on shared_size > 0, gate post-forward-merge DCE on progress, and expose agx_compile_shader_nir_capture_ex() / agx_compile_shader_nir_postopt() for trivial monolithic fragment epilogs.
    • agx_pressure_schedule: replace the O(n²) ready-node scan in ilp_schedule_block() with a dense BITSET_WORD ready set (preserving strictly ascending tie-break order) and switch ilp_max_pressure() / ilp_block_peak() from sparse to dense liveness bitsets.
    • Unit tests in src/asahi/compiler/test/test-agx3-pso-compile-fastpath.c.
  2. AGX3 binder fast path & TLS scratch cache (c27925c4c81):

    • Cache the per-thread binder scratch pointer in an 8-byte _Thread_local while keeping tss_set() for thread-exit cleanup.
    • Shrink agx3_bind_memo::field to uint8_t, skip zero baseline writes, and reject mismatched explicit encoding-point probes via agx3_form_preds_match() before building a memo key or locking.
    • Add agx3_bind_compose_ep_range() to probe an encoding-point range with a single scratch populate/clear cycle, used by uniform_float_encode() and the wide fsel/fseli16 probe loops.
    • Unit tests in src/asahi/compiler/test/test-agx3-bind-memo.c.
  3. Borealis compute pre-filters & post-opt monolithic fragment reuse (d9c742c9171):

    • Add lightweight pre-filters before cloning/lowering NIR in the identity_copy4, tensor-op, byte-stencil, 2D-stencil, and pointwise-clamp compute matchers.
    • Switch hk_shader.c:nir_opts() to a circular worklist fixed point and extend can_post_lower in hk_lower_nir() to loop-free IO-only shaders without shared memory.
    • Capture transient post-agx_optimize_nir NIR for non-preamble opaque fragments and reuse it via agx_compile_shader_nir_postopt() when pre-linking trivial non-ALU 1x ALL_SAMPLES (0xFF) epilogs, freeing the clone immediately after pre-linking.

Benchmarks (Exact-Head ABBA+BAAB, 4 base + 4 head, count=32, shader cache disabled)

MacBook Neo (J700, Apple A18 Pro G17P B1, stock 7.3.0-41.fc44.aurora.aarch64, isolated overlays)

Tested exact-head b2a7eecc318+0 vs parent 1e793d856d4+0 (same boot 2b400cd8-57f6-4af8-b7ae-fcd12b592205, stock.sh PRE==POST 0 diffs, kfaults=0, avc_delta=0). Rebased cleanly onto aurora-wip (ef132f590b2) with identical logic (d9c742c9171).

P-core pinned (cpu5, eliminating A18 Pro E-core/P-core migration jitter):

compile.pso kind Base per_pso (ms) Head per_pso (ms) Speedup Base total (ms) Head total (ms) Total delta
compute 0.6054 0.5620 1.077x (-7.16%) 19.49 17.99 -7.74%
graphics 1.2839 1.1845 1.084x (-7.74%) 41.55 37.95 -8.67%
uber 1.4354 1.2835 1.118x (-10.58%) 47.41 42.63 -10.07%

Unpinned (default EAS scheduler):

compile.pso kind Base per_pso (ms) Head per_pso (ms) Speedup Base total (ms) Head total (ms) Total delta
compute 1.1939 1.1905 1.003x (-0.29%) 40.18 41.80 +4.04%*
graphics 1.8558 1.7473 1.062x (-5.84%) 67.46 63.17 -6.36%
uber 1.9185 1.8129 1.058x (-5.51%) 73.31 69.01 -5.86%

*Unpinned compute runs ~40 ms total and randomly stays on E-cores (~1.52–1.56 ms) vs migrating to P-cores (~0.55–1.15 ms).

Host drm-shim (g17p, P-core pinned, 4-round ABBA)

  • compile.pso[compute]: 0.9016 -> 0.8489 ms/pso (1.062x, total -8.03%)
  • compile.pso[graphics]: 1.8325 -> 1.6915 ms/pso (1.083x, total -7.99%)
  • compile.pso[uber]: 2.1993 -> 1.9927 ms/pso (1.104x, total -9.13%)
  • vk-mini (39 foz, 297 shaders): 1418.6 -> 1366.4 ms (1.038x, -3.69%)

Verification

  • ShaderDB parity (0 regressions):
    • g17p: compile.pso (64 shaders: 0 diffs), gb6-horizon-edge (8 foz, 28 shaders: 0 diffs), vk-mini (39 foz, 297 shaders: 296 identical, 1 improvement — gpubench-stress.fault.comp.foz CProg gpr_halves 8 -> 6, same 62 bytes).
    • g17s (M5): compile.pso (56 shaders: 0 diffs), vk-mini (178 shaders: 0 diffs), gb6-horizon-edge (28 shaders: 0 diffs).
  • Neo J700 bit-exact CRCs + ASAHI_MESA_DEBUG=verify (8/8 PASS):
    • render.postfx (5deb69ac), render.deferred (2cfe9a54), render.shadow (c0fb9993), render.mdi (06f8412a), compute.edge (27c79e6d), compute.conv2d (2d55a14d), compute.uber (9963f56c), compute.blur_fp32 (abee0456) — all bit-exact across base, head, and head+verify with empty verify.err.
  • Neo J700 targeted Vulkan CTS (deqp-vk, shader cache disabled) — 1,311 / 1,311 PASS, 0 FAIL:
    • dEQP-VK.pipeline.monolithic.cache.*: 774/774 PASS
    • dEQP-VK.pipeline.monolithic.blend.format.r8g8b8a8_unorm.*: 100/100 PASS
    • dEQP-VK.shader_object.binary.*: 437/437 PASS
  • Fedora CI (fedora-mesaci.sh):
    • b2a7eecc318 (pre-rebase exact-head): 217/217 PASS, 0 FAIL, 0 new warnings.
    • d9c742c9171 (rebased onto ef132f590b2): 217/218 PASS (only failure is pre-existing hk_sync_stamps line 1237 on ef132f590b2 tip, untouched by this PR).

…bitsets

Eliminate wasted cold pipeline compile time with zero codegen changes:

- nir_opt_fp_math_ctrl: drop preserve_signed_zero without FP range
  analysis when no consumer observes -0, and early-out when no ALU,
  derivative, or IO load can be updated.
- agx_compile: gate loop-only and multi-block-only NIR passes on
  agx_nir_has_loop() / agx_nir_is_single_block(), skip cloning in
  agx_compile_shader_nir_unrolled() when loop-free, skip
  agx_nir_opt_shared_byte_offsets when shared_size == 0, gate
  post-forward-merge DCE on progress, and expose
  agx_compile_shader_nir_capture_ex() / agx_compile_shader_nir_postopt()
  for trivial monolithic fragment epilogs.
- agx_pressure_schedule: replace the O(n^2) ready-node scan in
  ilp_schedule_block() with a dense BITSET_WORD ready set (preserving
  strictly ascending tie-break order) and switch ilp_max_pressure() and
  ilp_block_peak() from sparse to dense liveness bitsets.
- Add unit tests in test-agx3-pso-compile-fastpath.c.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Reduce AGX3 instruction-packing overhead during cold shader compiles:

- Cache the per-thread binder scratch pointer in an 8-byte _Thread_local
  while keeping tss_set() for thread-exit cleanup.
- Shrink agx3_bind_memo::field from uint16_t to uint8_t, skip zero
  baseline writes, and reject mismatched explicit encoding-point probes
  via agx3_form_preds_match() before building a memo key or locking.
- Add agx3_bind_compose_ep_range() to probe an encoding-point range with
  a single scratch populate/clear cycle, and adopt it in
  uniform_float_encode() and the wide fsel/fseli16 probe loops.
- Add unit tests in test-agx3-bind-memo.c.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Avoid redundant cloning and NIR optimization during Vulkan pipeline compilation:

- Add lightweight pre-filters before cloning/lowering NIR in the
  identity_copy4, tensor-op, byte-stencil, 2D-stencil, and
  pointwise-clamp compute matchers.
- Switch hk_shader.c nir_opts() to a circular worklist fixed point and
  extend can_post_lower in hk_lower_nir() to loop-free IO-only shaders
  without shared memory.
- Capture transient post-agx_optimize_nir NIR for non-preamble opaque
  fragments and reuse it via agx_compile_shader_nir_postopt() when
  pre-linking trivial non-ALU 1x epilogs, freeing the clone right after
  pre-linking.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 27, 2026 17:01
@DjDeveloperr

Copy link
Copy Markdown
Author

J714s (MacBook Pro M5 Pro, G17S, kernel 7.3.0-m5-cdirect16, second generation) at exact head d9c742c9171765a8e49c0dad8191b2efaa6e57fd against ef132f590b2: PASS

  • Build & unit tests: Clang 22.1.8 assert (-Db_ndebug=false) and GCC release (-Db_ndebug=true) --suite asahi: 214/215 PASS each (agx3_bind_memo, agx3_pso_compile_fastpath, agx_tests, hk_compute_pair PASS; the sole failure is the pre-existing aurora-wip ef132f590b2 #1213+#1235 hk_sync_stamps line 1232/1237 interaction).
  • gpubench-vk compile.pso ABBA vs ef132f590b2 (12/12 status=ok):
    • kind=compute: cold_total_ms 28.20 -> 22.75 ms (-19.3%), cold_per_pso_ms 0.85 -> 0.67 ms (-21.2%).
    • kind=graphics: cold_total_ms 62.83 -> 55.26 ms (-12.0%), cold_per_pso_ms 1.96 -> 1.82 ms (-7.1%).
    • kind=uber: cold_total_ms 63.24 -> 61.50 ms (-2.8%), cold_per_pso_ms 1.96 -> 1.87 ms (-5.0%).
  • ASAHI_MESA_DEBUG=verify CRC sanity: render.deferred (1080p) and compute.conv2d (1080p): 2/2 valid=True, status=ok.
  • Vulkan CTS (cts1471, 482 PASS, 0 Fail):
    • dEQP-VK.compute.shader_object_binary.basic.*: 71/71 PASS, 0 Fail.
    • dEQP-VK.compute.pipeline.basic.*: 80/81 PASS, 0 Fail (1 Not supported).
    • dEQP-VK.pipeline.monolithic.blend.format.r8g8b8a8_unorm.*: 100/100 PASS, 0 Fail.
    • dEQP-VK.pipeline.fast_linked_library.blend.format.r8g8b8a8_unorm.*: 100/100 PASS, 0 Fail.
    • dEQP-VK.api.command_buffers.*: 131/131 PASS, 0 Fail.
  • Health: boot=same, online=1, crashed=0, quarantined=0, 0 kernel errors, 0 GPU faults.

…ompile-fastpaths

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

Copy link
Copy Markdown

8c87753b9db5 exact-head Borealis multi-gen PASS (GCC 16.2.1 -Db_ndebug=false, isolated VK_DRIVER_FILES, stock Mesa untouched, vs aurora-wip 99658583598f):

  • J700 (MacBook Neo, G17P B1, 7.3.0-aurora-fleet89):
    • P-core pinned (cpu5, MESA_SHADER_CACHE_DISABLE=true) ABBA+BAAB (n=4/arm) gpubench-vk compile.pso (count=32): compute per_pso 1.0733 -> 1.0321 ms (-3.84%) / total 36.18 -> 34.18 ms (-5.52%); graphics per_pso 2.1050 -> 1.9988 ms (-5.05%) / total 69.84 -> 64.60 ms (-7.50%); uber per_pso 3.0962 -> 2.8081 ms (-9.31%) / total 98.26 -> 89.18 ms (-9.24%).
    • Correctness: deqp-vk 773 PASS / 0 FAIL (api.smoke.* 8/8, compute.pipeline.basic.* 80/81, pipeline.monolithic.push_constant.* 61/65, glsl.loops.* 624/624); 0 GPU faults, Enforcing, 0 failed units, 0 new AVC.
  • J413 (M2 Air, G14G B0, 7.3.0-aurora-fleet89): deqp-vk 147 PASS / 0 FAIL (api.smoke.* 8/8, compute.pipeline.basic.* 78/81, pipeline.monolithic.push_constant.* 61/65); 0 GPU faults, Enforcing, 0 failed units, 0 new AVC.

…ompile-fastpaths

Bring in mesa#1255 (AGX3 ILP tile-store payload bank bound), mesa#1239,
mesa#1228 and mesa#1257.

mesa#1255's ilp_tile_store_bank_overflows() walks the candidate and
original orders over sparse live sets, while this branch moved
ilp_schedule_block()'s live_end/scratch to dense BITSET_WORD arrays.
Port the tile-store bank helpers to the dense sets (BITSET_FOREACH_SET,
memcpy, sched_liveness_update_dense) so the check runs over the same
live set the pressure check uses. The bank fit and the reject rule
(overflow when the original order fits, or a higher peak than the
original order) are unchanged.

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

Copy link
Copy Markdown

eab548e306f1 (post-merge with aurora-wip / dense-bitset #1255 port) 3-gen Borealis PASS (J316s + J615 + J700, Clang 23.1.4 -Dbuildtype=release -Db_ndebug=false, isolated VK_DRIVER_FILES overlay, stock Mesa SHA-256 unchanged on all 3):

  • J316s (G13S C0, 7.3.0-aurora-fleet89): meson test --suite asahi 215/215 PASS (BgShaderSchedule.* 7/7 PASS); CTS 141 PASS / 0 FAIL (api.smoke 8/8, push_constant 61/65, multiview.multisample_resolve 8/8, texelfetch 64/96), 0 GPU faults/recoveries, Enforcing, 0 failed, 0 AVC.
  • J615 (G15G C0, 7.3.0-aurora-fleet90-airs2): CTS 137 PASS / 0 FAIL (8/8, 61/65, 8/8, 60/96), 0 GPU faults/recoveries, Enforcing, 0 failed, 0 AVC.
  • J700 (G17P B1, 7.3.0-aurora-fleet89): CTS 173 PASS / 0 FAIL (8/8, 61/65, 8/8, 96/96), 0 GPU faults/recoveries, Enforcing, 0 failed, 0 AVC.

@DjDeveloperr

Copy link
Copy Markdown
Author

J714s (M5 Pro, G17S) refresh at eab548e, kernel 7.3.0-m5-cdirect16. Built on the machine, driver loaded only through VK_DRIVER_FILES.

  • Clang assert and GCC release --suite asahi: 215/215 each. BgShaderSchedule.*: 7/7. The Clang warnings in touched files are all on lines this PR doesn't change.

  • agx: #1190 ILP interleave overflows tile-store payload bank on M5 layered 8x RGBA32 reload shaders #1253 repro (clear_color_attachment.multiple_layers.r32g32b32a32_{sfloat,sint,uint}_64x11_sample_count_8, deqp-runner): 3/3 pass.

  • gpubench compile.pso (count=32, shader cache off, pinned to one core), base 41857fd interleaved ABBA, n=4 per side, cold ms per PSO:

    • compute: 0.539 → 0.489 (−9.4 %)
    • graphics: 1.266 → 1.101 (−13.0 %)
    • uber: 1.251 → 1.107 (−11.5 %)

    Every head run is faster than every base run.

  • No GPU faults. The GPU stayed online with nothing quarantined.

@RyanTheTide RyanTheTide 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: MERGE at eab548e306f194b1f719b024f66b9fcdcc0f6267

Blockers: none

Checked: I read the whole diff (22 files) at the exact head and tried to break each fast path.

  • Cache and keys: nothing new reaches a cache key or a serialized blob. g17p_mono_postopt_nir lives in memory only. It is NULL after a binary-cache clone and freed once prelink finishes, so a shader loaded from cache takes the old deserialize path. The postopt path is only taken for a 1x, no-preamble, non-uniform-blend, non-inline-samples epilog made of mov/vec/load_exported/store_local_pixel with a constant 0xFF mask. Uniform exports fall back to the pre-preamble NIR. For that shape the skipped agx_nir_lower_g17p_tile_samples, zs hoist and refusal scan are no-ops. The key fields that differ from g17p_mono_key (no_stop, secondary, rodata hint, partitions) are the ones the existing preoptimized path already accepts.
  • Skipped passes: every pass newly gated on has_loops walks loops only (monotonic_bit_insert, top2_min, factor_loop_mac, const/runtime unroll, hoist_readonly_loop_loads, unswitch_affine_bounds, loop-invariant guard bits). shared_index_sort and shared_byte_offsets now need shared_size > 0, and both only touch shared memory. The single-block skips only drop if/phi passes. The worst case for a stale single_block is a missed optimization, not wrong code. agx_nir_has_loop scans every impl. The loop-free shortcut in agx_compile_shader_nir_unrolled drops only the factor-4 and factor-1 retries, and m5_unroll_factor only gates loop passes, so those retries would give the same binary.
  • Preprocess: the signed-zero min/max block now runs only on the first preprocess. The generic lower_fminmax_signed_zero is still set in agx_nir_options, so ops added before the post-lower pass are still lowered correctly, just generically. The widened can_post_lower is backed by the debug asserts in agx_assert_post_lower_invariants (flrp, frexp, sincos and subgroups already lowered), and the assert-enabled gate below passes with them on.
  • nir_opt_fp_math_ctrl: the early clear of preserve_signed_zero gives the same result as the old !pass_flags || branch. Running it before nir_analyze_fp_class can only widen the zero bits (handle_sz), which matter only for the flag that has already been cleared. The any_work pre-scan covers every case where the pass can make progress (ALU sz/inf/nan flags, ddx/ddy sz, IO loads without no_signed_zero).
  • nir_opts worklist: the wake sets are sound. copy_prop, cse and constant_folding do not re-wake themselves, and DCE cannot create work for copy_prop, cse, constant folding, undef or remove_phis.
  • Binder: the AGX3D refusal in agx3_bind_compose_ep_range matches the old per-ep agx3_bind_compose, which also rejected AGX3D with an explicit ep. Leaving out zero-valued fields depends on the scratch vector starting at zero, and clear_nonzero plus the stack memset keep it that way. The uint8_t memo field holds indices up to 160, and memo hits compare everything under the lock. The _Thread_local pointer only caches what tss_get returns, and tss still owns the cleanup.
  • Scheduler: the ready bitset is set only at init and at the single --ndeps == 0 site. Iteration stays ascending, so dropping i < best keeps the same tie-break.
  • Determinism and codegen parity: asahi_clc precomp (libagx for all 8 targets, g13g through g18g, including g17p/g17g) is byte-identical between tip c9ec6b97 and this head, and three head runs give identical output.
  • NO-GATES: no new chip or board gates. The one agx_chip_g17_m5() use was moved, not added.
  • Rows at this exact head: J316s (G13S), J615 (G15G), J700 (G17P) and J714s (G17S, with an interleaved cold compile.pso ABBA: -9.4 / -13.0 / -11.5 %).

Non-blocking:

  1. test_fp_math_ctrl_fast_paths would also pass against the old pass. It checks equivalence, not a regression, which is fine for a pure speed change. A case with a load that already has no_signed_zero plus a flagged ALU would exercise the any_work scan's false-negative direction.
  2. Nothing at this head exercises cold-compile timing on J700 (the J700 timing was at 8c87753b), and there are no J413 or J773g rows. Two generations are present, so this does not hold the merge.
Evidence
  • Scratch merge of eab548e3 into aurora-wip c9ec6b97 (clean, tree e96e01f9). x86 drm-shim gate (-Dbuildtype=release -Db_ndebug=false, meson test --suite asahi --suite poly): 339/339 OK. Tip baseline: 339/339 OK.
  • libagx_shaders.h sha256 77fe1f5c...e90a and libagx_shaders.c sha256 90ff37d2...f8 are identical for the tip build, the head build and two extra head asahi_clc runs.

@RyanTheTide
RyanTheTide merged commit a0164c9 into aurora-wip Nov 27, 2026
@RyanTheTide
RyanTheTide deleted the perf/agx3-pso-compile-fastpaths branch November 27, 2026 22:19
DjDeveloperr added a commit that referenced this pull request Nov 27, 2026
#1245 changed when hk_lower_nir() may take the post-lower preprocessing
(can_post_lower). This branch moved texture lowering and that
preprocessing into hk_lower_nir_post_tex(), so the new condition now
feeds the hk_lower_nir_post_tex() call at the end of hk_lower_nir_ex();
lowered_tex is still checked inside it.

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

Copy link
Copy Markdown

Post-merge correction to my review above: I missed a real defect, and the tip gate caught it after the merge.

agx_compile_shader_nir_internal() sets nir->options = &agx3_options, a stack local (agx_compile.c:19768/19911), and restores it only at line 20031. The capture *post_opt_nir_out = nir_shader_clone(NULL, nir) at line 20004 sits inside that window. As a result, g17p_mono_postopt_nir->options points into a dead frame when hk_g17p_inline_epilog_part() uses it during pre-linking.

Repro on tip a0164c9 (x86 drm-shim, -Db_ndebug=false): ASAHI_SHIM_CHIP=g16s g17p-vulkan-layered-depth --shim --expect-compressed with an empty shader cache aborts 20/20 with "NIR validation failed after nir_opt_copy_prop in hk_cmd_draw.c:9734" (validate_float_mul_add, shader vk-meta-copy-buffer-to-image-frag). It passes when the cache is warm, which is why my pre-merge gate run passed. c9ec6b9 (before this PR) aborts 0/20.

Fix and a deterministic unit-test check: #1264.

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