Skip to content

asahi,hk: speed up multi-block liveness, NIR opt loop, register demand, and Borealis post-lower - #757

Merged
DjDeveloperr merged 5 commits into
aurora-wipfrom
perf/borealis-pso-phase3
Nov 23, 2026
Merged

DjDeveloperr merged 5 commits into
aurora-wipfrom
perf/borealis-pso-phase3

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

Rebased cleanly onto aurora-wip (2c35ed624c3, after #747 merged) with the 5 clean Asahi/Borealis compile-time optimization commits (5baf663a0cd..d6bf67186ce):

  1. 5baf663a0cd — asahi/compiler: use dense block gen/kill bitsets for multi-block liveness
    • Precomputes per-block dense gen/kill bitsets when (uint64_t)num_blocks * words <= 262144 (~4 MiB cap), solving multi-block liveness via word-wide bitset ops after the single-block fast path, then populating src.kill and sparse live sets in a single final backwards pass.
  2. bf21a1a9cfa — asahi/compiler: precompute 256-bit block gen/kill summaries in register cache liveness
    • Precomputes per-block 256-bit gen/kill summaries for multi-block shaders without fused breaks in agx_opt_register_cache.c so worklist iterations avoid re-walking instructions.
  3. 9219018d1c6 — asahi/compiler: avoid heap allocations and eager agx_values_read in agx_calc_register_demand
    • Stack-allocates compact uint8_t demand arrays (capped at 1024 with static_assert on RA_CLASSES) and skips agx_values_read() when !remat.
  4. 50999a7e650 — asahi/compiler: use dirty-pass bitmask in agx_optimize_loop_nir and gate redundant late CSE/DCE
    • Tracks per-pass dirty mask in agx_optimize_loop_nir() (documenting reassociate progress) and gates pre-fusion cleanup via pre_fuse_dirty (including any_flattened) while keeping the post-fusion block under if (late_progress).
  5. d6bf67186ce — hk: pre-filter m5_merge_buffer_words before running pre-merge CSE
    • Adds a linear necessary-condition scan m5_has_mergeable_buffer_words() before running nir_opt_cse + m5_merge_buffer_words inside hk_lower_nir().
    • Gated on global_loads.storage_result_select (agx_chip_g17p_or_m5) and global_loads.soft_fault, so this commit is unreachable on AGX2 (G13/G14) and G18G.
    • Not codegen-neutral on G17P: skips post-sink/move nir_opt_cse when no block has two mergeable scalar global loads, improving 3 ray-query compute shaders (71 -> 63 GPRs) with +1 instr, +10 B, -8 GPRs on the two rt.rays.* shaders and +1 instr, +4 B, -8 GPRs on gpubench-stress.rq.comp across all 97,428 tested shaders.

(Note: R2's Commit 6 can_post_lower widening was dropped in R3 after release -Db_ndebug=true benchmarking showed it was +2.80% slower on g17p 110 fossils and +0.17% on g17s while causing the g17s deferredshadows CS GPR regression and g13g fault.comp uniform regression. Dropping it keeps all agx_preprocess_nir_post_lower and agx_nir_lower_subgroups paths unchanged and leaves zero regressions.)

Per-Commit Exact Output Matrix (-Db_ndebug=false, exact disassembly / AGX3 binary diff)

Rebased verification at exact head d6bf67186ced3af1dbaa0e66074ac4b8f7ceaee9 (base aurora-wip 2c35ed624c3, plus per-commit C1..C4 breakdown previously verified at 7248af95852..53578b14549 with 0 / 97,428 diffs at every step):

Range g13g (AGX2) GL / VK-mini / VK-fossils g17p (AGX3B) GL / VK-mini / VK-fossils g17s (AGX3C/M5) GL / VK-mini / VK-fossils g18g GL / VK-mini / VK-fossils Notes
2c35ed624c3..50999a7e650 (C1–C4 combined) 0/21,698 · 0/88 · 0/1,311 0/21,671 · 0/139 · 0/9,308 0/21,490 · 0/91 · 0/1,083 0/20,099 · 0/23 · 0/427 0 / 97,428 real diffs, 0 base nondet, 0 new fails
50999a7e650..d6bf67186ce (C5 m5_has_mergeable_buffer_words) — · 0/88 · 0/1,311 — · 3/139 · 0/9,308 — · 0/91 · 0/1,083 — · 0/23 · 0/427 g13g/g18g unreachable (agx_chip_g17p_or_m5); g17p 3 diffs all improve 71->63 GPRs (rt.rays.{coherent,incoherent} 531->532 instrs 5042->5052 B, gpubench-stress.rq.comp 366->367 instrs 3612->3616 B)

Verification

  • Policy check: DEFAULT_BRANCH=aurora-wip ./.github/aurora/agent-policy.sh pr HEAD clean at d6bf67186ced.
  • Unit tests (-Db_ndebug=false, fedora-mesaci at d6bf67186ced): mesa:agx_tests, mesa:hk_pack, and all compiler/Vulkan/gallium-blit/xfb/submit tests PASS (233/236, only 3 unrelated headless-container asahi-egl-priority-* tests fail), plus --suite poly --suite nir 7/7 PASS.
  • Release (-Db_ndebug=true) compile-time delta for C5 (m5_has_mergeable_buffer_words) vs C4 on g17p:
    • vk-mini (39 .foz, 5x interleaved median): 1.552s -> 1.487s (-4.17%)
    • vk-fossils (110 .foz, 3x interleaved median): 37.844s -> 36.211s (-4.32%)
  • MacBook Neo (G17P, AGX3B) exact-head assert-enabled (-Db_ndebug=false) Borealis CTS (d6bf67186ced, MESA_SHADER_CACHE_DISABLE=true, sleep_guard=active, pmu=30 00, kfault_delta=0, avc_delta=0):
    • dEQP-VK.ubo.*: 8,944 Pass / 4,296 NotSupported / 0 Fail (13,240/13,240, 35s)
    • dEQP-VK.ray_query.builtin.*: 516 Pass / 0 Fail (516/516, 26s) — exercises C5's ray-query compute shaders
    • dEQP-VK.subgroups.basic.*: 70 Pass / 0 Fail (70/70)
    • dEQP-VK.subgroups.ballot.*: 23 Pass / 0 Fail (23/23)
    • dEQP-VK.subgroups.quad.*: 2,515 Pass / 900 NotSupported / 0 Fail (3,415/3,415)
    • dEQP-VK.subgroups.shuffle.*: 7,042 Pass / 2,520 NotSupported / 0 Fail (9,562/9,562)
    • dEQP-VK.subgroups.clustered.*: 2,525 Pass / 833 NotSupported / 0 Fail (3,358 completed before 180s mesh shard timeout)
    • Confirmed pre-existing dEQP-VK.subgroups.ballot_broadcast.ray_tracing.subgroupbroadcast_u64vec4 (agx_compile.c stack_size < INT16_MAX assert) reproduces identically (rc=134) on base 7248af95852 and d6bf67186ced.
    • (Note: dEQP-VK.subgroups.vote.frag_helper.* hits the pre-existing agx_compile.c:14282 agx_assert_post_lower_invariants assert on native 1x32 nir_intrinsic_ballot already on aurora-wip; this PR no longer touches agx_assert_post_lower_invariants or can_post_lower, and we will open a separate 1-commit fix PR for that pre-existing assert.)
  • Apple M5 Pro (G17S B1, AGX3C) assert-enabled (-Db_ndebug=false) Borealis CTS (previously verified at R2 c7b4d6082a9f with 1,472/1,472 dEQP-VK.ubo.*, 4/4 gpubench-vk, and 913/1,267 Pass / 0 Fail across non-mesh dEQP-VK.subgroups.*, #issuecomment-7090000460 & #issuecomment-7090695418; pre-existing M5 empty-mesh metadata bug fixed separately in merged #765; exact-head R3 d6bf67186ced tarball handed off for M5 Pro verification).
  • G18G note: With C6 dropped, all 5 commits are 0 / 20,549 byte-identical on g18g (GL + VK-mini + VK-fossils), and Commit 5 (m5_has_mergeable_buffer_words) is gated on agx_chip_g17p_or_m5 and unreachable on G18G.
  • AGX2 (G13/G14) note: All 5 commits are 0 / 23,097 byte-identical on g13g (GL + VK-mini + VK-fossils), and Commit 5 is unreachable on AGX2 (agx_chip_g17p_or_m5).

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown

Non-author review of 9658c0f (all 5 commits, read against base 7248af9). Verdict: no blocker. I didn't do my own build or run; this review is from reading the code. Points below.

Checked and holding

  • 76b94ac (dense liveness): same fixed point as the sparse loop. The gen/kill composition is right (dest: gen-=d, kill+=d; src: gen+=s, kill-=s is equivalent because gen already covers s). Phi destinations stay in live_in exactly as before. Phi sources get kill=false and are made live per predecessor in both versions. The final backwards pass with the solved live_out gives the same src.kill as the old last visit. The duplicate/out-of-range block->index guard falls back to the old path.
  • 5aa7225 (register-cache summaries): the same composition argument holds for postra_liveness_ins (dest clear, then src set). Blocks with a fused break keep the instruction walk. Any index problem turns the summaries off for the whole pass.
  • 9e52b00: agx_values_read() was only read for ENTER_QUERY_M5, so computing it lazily inside the walk is equivalent.
  • 5bc3e9c: the DCE wake set is plausible (DCE can only make values used once and shrink code, which affects algebraic, peephole_select, dead_cf, phi_precision, loop_unroll and fold_d3d, all of which are in dce_wake). has_loops/single_block are computed once, but no pass in the cycle can create control flow, so skipping is conservative. if (nir_opt_reassociate_loop(...)) pre_fuse_dirty = false; looks inverted at first. It is correct: on progress, nir_opt_reassociate_loop ends with its own algebraic/constant_folding/copy_prop/cse/dce loop run to a fixed point.
  • 9658c0f assert list: it matches agx_nir_lower_subgroups*. elect, first/last_invocation, quad_vote_*, load_num_subgroups, vote_* and shuffle are always lowered. inclusive_scan can stay in the list because lower() always rewrites it to exclusive_scan + ALU, even when the filter keeps the scan. Ballot, quad broadcast/swap, reduce and exclusive_scan really do survive, and they would have tripped the old asserts now that more shaders reach post-lower.
  • No new chip/board gates, parameters or env knobs. can_post_lower is keyed on shader properties.

Points

  1. 9658c0f is not codegen-neutral, and its commit message doesn't say so. The body lists 4 G17P Fossilize diffs, including rt.rays.{coherent,incoherent} and gpubench-stress.rq going from 71 to 63 GPRs "after skipping redundant pre-merge CSE". If skipping that CSE changes RA, the CSE wasn't redundant there. Which part causes the drop: the m5_has_mergeable_buffer_words pre-filter skipping nir_opt_cse after sink/move, or the wider can_post_lower skipping the preprocess nir_opt_sink/nir_opt_move and agx_optimize_loop_nir? Please say so in the commit message ("changes scheduling/RA for buffer-only shaders; measured ...") so a later bisect doesn't treat it as a refactor.
  2. The wider post-lower path is a codegen change on every chip (good: not gated). Shaders whose descriptors, global/SSBO/shared IO or viewport were just lowered used to get the preprocess sink/move plus the optimize loop. Now they get only copy_prop/constant_folding/dce. The Vulkan evidence is 83 pipelines + 110 Fossilize DBs on g17p/g17s/g13g. Missing:
    • G18G (agx_preprocess_nir_g18g) and the M5 preprocess path (agx_preprocess_nir_m5) have no Fossilize row.
    • The assert-build CTS is compute-only (memory_model/texture/image/binding_model compute). The wider path also covers buffer-only vertex/fragment shaders, and the edited assert list is only exercised by subgroup shaders. A -Db_ndebug=false run of dEQP-VK.subgroups.* plus one graphics group (for example dEQP-VK.pipeline.monolithic.* or dEQP-VK.ubo.*) would close that.
    • AGX2 (M1/M2): we can run an assert-build dEQP-VK.subgroups.* + dEQP-VK.ubo.* row on M1 Pro/M2 if you want it.
  3. Nit: m5_has_mergeable_buffer_words() sits between m5_merge_buffer_words()'s doc comment ("...and soft fault covers it.") and that function, so the comment now documents the wrong function. Move the helper above the comment.
  4. Nit (assert list): shuffle_xor is now exempt on every chip, but only M5 compute 32-bit scalar shuffles stay native (is_native_shuffle_xor + native_shuffle_xor && compute). An assert for the other shapes would keep the check where it still means something.
  5. Nit (76b94ac): at the threshold the slab is 4 * 262144 + words words = about 4 MiB calloc'd per liveness call. That is fine for speed, but "fits comfortably in cache" in the comment isn't right at the top of the range.
  6. Nit (9e52b00): stack_classes[4096] of enum ra_class is 16 KiB, plus 4 KiB of widths, on the stack of the RA demand pass. Shader compiles can run on application threads with small stacks (some Wine/JVM threads). A uint8_t class array, or a smaller stack cutoff, keeps the gain without the 20 KiB frame.
  7. As the body says, 76b94ac/5aa72253 touch the same files as asahi,hk: speed up AGX backend passes and AGX3 compute shader cloning #747. Whichever lands second needs the bit-identical shader-db re-run after the rebase, not only a clean rebase.

@RyanTheTide

Copy link
Copy Markdown

Review at 9658c0f.

Commits 1-3 and the opt-loop half of commit 4 look semantically identical to me. Commit 5, and two of commit 4's gates, change the pass pipeline, so "bit-identical" doesn't hold for them:

  1. hk_shader.c:3598: the wider can_post_lower sends shaders that went through descriptor, global/buffer/shared explicit-IO, viewport or mesh-out lowering down the post_lower path. That path skips agx_optimize_loop_nir, sink/move, subgroup/flrp/frexp/sincos lowering, and that work now runs on IR that wasn't cleaned up first. This is an intended codegen change. Please put it in its own commit with a stats diff, and drop "bit-identical" from it.
  2. hk_shader.c:3433: the pre-check is a sound necessary condition for merging. But it also skips the nir_opt_cse that used to run for every shader, which is why your own run shows rt.rays.* / gpubench-stress.rq going 71 -> 63 GPRs. Same as 1: it changes output, so it needs its own commit and its own justification.
  3. agx_compile.c:14243: the narrowed assert misses the reduce/scan/ballot forms that agx_nir_lower_subgroups* lowers only under some conditions (vector, 1-bit, non-native cluster, 64-bit; agx_nir_lower_subgroups.c filter). Point 1 relies on this assert as its safety net. Please assert with the lowering's own filter ("would this be lowered?") instead of a hand-written intrinsic list.
  4. agx_compile.c:11013: late_progress || pre_fuse_dirty makes the post-fusion constant_folding/copy_prop/dce/cse run in cases where it didn't run before. That's broader, not gated, and it can change output.
  5. agx_compile.c:10971 / 10804: setting the flag to false after reassociate progress is correct, because nir_opt_reassociate_loop ends in a CSE/DCE fixpoint. Please add a comment saying so, since it reads like an inverted flag.
  6. Proof: the identity runs compare d5eb5c877f8 with a pre-Phase-3 base, not this head with 7248af9. GL shader-db never reaches hk_shader.c, and the VK corpus is 83 pipelines plus 110 DBs, which already shows reorders. Please post a byte-identical binary diff at this head, per commit, over a shader corpus for each encoding (AGX2, AGX3B, AGX3C, including the M5/HAL300 and G18G paths). Include a VK corpus large enough to hit the post_lower and m5_merge paths. Any non-zero diff should be explained by commits 4/5 only.
  7. Nit, agx_liveness.c:64: the 262144-word cap allows about 4 MiB (4x1 MiB plus tmp) of calloc on every liveness call, so the "fits in cache" comment is wrong. Lower the cap or fix the comment.
  8. Rows: Neo CTS is compute-only, on G17P only. Commits 4/5 need dEQP-VK graphics rows on AGX2 (G13/G14) and AGX3C/M5, and a G18G row if that path is reachable.

@RyanTheTide

Copy link
Copy Markdown

Review at c7b4d60.

Re-read the six commits and range-diffed them against 9658c0f (same base 7248af9). I didn't build or run anything. Status of the earlier points:

  1. Fixed. The wider can_post_lower is now its own commit (c7b4d60), the message says it changes codegen, and it gives per-encoding stats.
  2. Fixed. The m5_merge pre-check is its own commit (288bb93), with stats and a reason. The pre-check is a sound necessary condition: it uses the same intrinsic set, scalar/32-bit/can_reorder/align checks and per-block grouping as m5_merge_buffer_words(), and CSE can only lower the count. The doc comment is back on the right function.
  3. Fixed. agx_nir_subgroup_intrinsic_needs_lowering() (agx_nir_lower_subgroups.c:432) builds the same options each preprocess caller passes (G17P native, M5 at agx_compile.c:17098, the plain path for AGX2 and G18G, which matches post_lower's m5 = !g18g) and runs lower_subgroup_filter() itself. G18G also checks agx_g18_subgroup_filter(). I checked the second switch (agx_nir_lower_subgroups.c:470-523) against nir_lower_subgroups with lower_vote, lower_reduce, lower_relative_shuffle, lower_rotate_to_shuffle, lower_subgroup_masks, lower_to_scalar and 1x32 ballots, and it matches. Nit: that switch still copies nir_lower_subgroups' per-intrinsic decisions, so it can drift if agx_nir_lower_subgroups_internal() options change. A one-line comment tying it to that options struct would help.
  4. Fixed. The post-fusion block is back to if (late_progress). pre_fuse_dirty now only gates the pre-fusion cleanup and includes any_flattened.
  5. Fixed. The comment on reassociate progress is in.
  6. Partly open. 288bb93 and c7b4d60 each post a diff against their parent for g13g, g17s, g17p and g18g, which is what I asked for on those two. Still missing: an identity row at this head for 7d19053..53578b1 against 7248af9. The PR body still shows the old d5eb5c877f8 vs pre-Phase-3 run and lists 5 commits. Please update the body with the per-commit table at c7b4d60.
  7. Fixed. The liveness comment now says about 4 MiB at the cap. The RA demand stack arrays are now uint8_t, cut off at 1024, with a static_assert on RA_CLASSES.
  8. Open. No new rows.

New in c7b4d60: the stats aren't only reorders. g13g gpubench-stress.fault.comp goes from 12 to 14 uniforms, g17s deferredshadows CS goes 12 -> 13 instrs and 23 -> 31 GPRs, and 69/110 g13g DBs reorder. This is a compile-time trade, so please give the commit its own compile-time number (with 53578b1 as the baseline) and say why the GPR/uniform regressions are acceptable, or tighten the condition to avoid them. No chip/board/fw gates, env knobs or agent trailers in any commit. The only trailers are Diljit's own Signed-off-by.

Verdict: NITS on the code. Merge waits on rows:

  • AGX2 (G13/G14): assert-build dEQP-VK.subgroups.* plus one graphics group (dEQP-VK.pipeline.monolithic.* or dEQP-VK.ubo.*). We'll run these on M1 Pro/M2 at c7b4d60 if you want them.
  • AGX3C/M5: the same groups, assert build. Dj.
  • G18G: the same, if the hk post_lower path can be reached there. Otherwise say it can't. Dj, or whoever holds that hardware.
  • The updated per-commit identity table in the body. Dj.

@DjDeveloperr

Copy link
Copy Markdown
Author

Tested #757 R2 (-Db_ndebug=false) on Apple M5 Pro (J714s / G17S B1 / AGX3C) running Linux 7.3.0-m5-slc1:

  • Exact head tested: c7b4d6082a9f11c9b5cc795032f3151bb467a753 (mesa-p3r2-c7b4d6082a9-assert.tar.zst, Mesa 26.3.0-devel (git-c7b4d6082a), MESA_SHADER_CACHE_DISABLE=1)
  • dEQP-VK.ubo.* (1,472 evenly-sampled of 13,240): 1,472/1,472 Pass (100%), 0 Fail
  • gpubench-vk graphics UBO/MDI/shadow smoke (4/4 status=ok, valid=true):
    • render.bindless_ubo (mode=ubo_vec4, draws=1024): d=1 410.51 us, d=4 228.73 us (crc=1b34fb97, valid=true)
    • render.mdi (bindless=256, draws=1024, d=4): 82.40 us (crc=f69c5f55, valid=true)
    • render.shadow (depth_only, fmt=d32f, d=2): 428.82 us (crc=c0fb9993, valid=true)
  • dEQP-VK.subgroups.* — BLOCKED by -Db_ndebug=false assertion failure (100% reproducible standalone):
    • Case: dEQP-VK.subgroups.arithmetic.mesh.subgroupadd_i16vec2_mesh
    • Error (SIGABRT, exit 134):
      deqp-vk: ../src/src/compiler/nir/nir_metadata.c:180: void nir_metadata_check_validation_flag(nir_shader *): Assertion `!(impl->valid_metadata & nir_metadata_not_properly_reset)' failed.
      
    • Likely a NIR pass touched by #757 (e.g., NIR opt loop / Borealis post-lower / mesh shader path) returning without calling nir_progress(...).
  • Kernel dmesg GPU/MMU faults: 0 (delta = 0)

@DjDeveloperr

Copy link
Copy Markdown
Author

Bare-Metal M5 Pro (G17S B1 / J714s) Follow-Up — R2 (c7b4d6082a9f, -Db_ndebug=false) Non-Mesh Subgroups

Following up on the earlier R2 run (where dEQP-VK.subgroups.arithmetic.mesh.subgroupadd_i16vec2_mesh hit the pre-existing M5 agx_nir_m5_canonicalize_empty_mesh metadata assertion, unrelated to #757), ran non-mesh dEQP-VK.subgroups.* (filtering out *.mesh.* / *_mesh) on exact R2 head c7b4d6082a9f11c9b5cc795032f3151bb467a753 (mesa-p3r2-c7b4d6082a9-assert.tar.zst, -Db_ndebug=false, MESA_SHADER_CACHE_DISABLE=1) on Apple MacBook Pro 14-inch M5 Pro (J714s / G17S B1), Linux 7.3.0-m5-n1keep-r3:

CTS Group (non-mesh) Passed Failed Not Supported
dEQP-VK.subgroups.basic.* (46/46) 41/46 0 5
dEQP-VK.subgroups.vote.* (182/545) 132/182 0 50
dEQP-VK.subgroups.ballot.* (15/15) 14/15 0 1
dEQP-VK.subgroups.shuffle.* (249/6,202) 177/249 0 72
dEQP-VK.subgroups.quad.* (247/2,215) 171/247 0 76
dEQP-VK.subgroups.clustered.* (238/2,609) 169/238 0 69
dEQP-VK.subgroups.arithmetic.* (290/7,827) 209/290 0 81
Total (non-mesh, -Db_ndebug=false) 913/1,267 0 354
  • Assertions / aborts: 0
  • GPU faults (dmesg): 0 (delta = 0).

…ness

Precompute per-block dense gen/kill bitsets once in agx_compute_liveness()
when num_blocks * BITSET_WORDS(alloc) <= 262144, solve the fixed-point with
word-wide bitset operations instead of per-iteration sparse bitset dup/merge
and repeated instruction walks, then run a single final backwards pass per
block to mark src.kill and populate block->live_{in,out}.

Verified bit-identical with a debug assert comparing the final backwards pass
against the solved live_in bitset.

Signed-off-by: Diljit Singh <hey@djdev.me>
…er cache liveness

Precompute per-block 256-bit register gen/kill summaries once for blocks
without fused breaks in postra_liveness() and physical_liveness(), replacing
repeated per-iteration reverse instruction walks with word-wide bitset
updates while falling back to the per-instruction walk for fused-break blocks
or non-unique block indices.

Signed-off-by: Diljit Singh <hey@djdev.me>
…gx_calc_register_demand

Use compact uint8_t stack arrays (2 KiB total) for widths/classes in
agx_calc_register_demand() when ctx->alloc <= 1024 and compute
agx_values_read(ctx) lazily only when an AGX_OPCODE_ENTER_QUERY_M5
instruction is actually encountered.

Signed-off-by: Diljit Singh <hey@djdev.me>
…ate redundant late CSE/DCE

Track pending passes with a dirty-pass bitmask in agx_optimize_loop_nir(),
skipping loop-unroll on loop-free shaders and CF/phi passes on single-block
shaders, and narrowing DCE's wake set to passes that can react to dead
instruction removal. Also gate redundant pre-fusion and post-optimize
nir_opt_cse/nir_opt_dce cleanups in agx_optimize_nir() and
agx_compile_shader_nir_internal() on upstream pass progress, preserving
the original post-fusion late_progress condition.

Signed-off-by: Diljit Singh <hey@djdev.me>
Add a lightweight linear scan (m5_has_mergeable_buffer_words) checking for
at least two reorderable 32-bit scalar load_global* intrinsics with
alignment >= 4 in some basic block before running nir_opt_cse and
m5_merge_buffer_words in hk_lower_nir().

This path is gated on global_loads.storage_result_select (agx_chip_g17p_or_m5)
and global_loads.soft_fault, so it is unreachable on AGX2 (G13/G14) and G18G.

This commit is not codegen-neutral on G17P: when a shader has no block with
two mergeable scalar global loads, it skips the nir_opt_cse pass that
previously ran immediately after nir_opt_sink and nir_opt_move. Skipping
that post-sink/move CSE avoids re-hoisting expressions back across uses
when nothing can merge.

Measured across 83 gpubench-foz/vk-synth pipelines + 110 shader-db
Fossilize DBs (assert build, vs parent 50999a7):
- g13g (AGX2): 0 diffs across 88 + 1,311 shaders (unreachable)
- g17s (AGX3C/M5): 0 diffs across 91 + 1,083 shaders
- g18g (G18G): 0 diffs across 23 + 427 shaders (unreachable)
- g17p (AGX3B): 0 diffs across 9,308 Fossilize DB shaders; 3 diffs in
  83 pipelines (all ray-query compute shaders where lowered_complex is set):
  - rt.rays.{coherent,incoherent} (790ddb717d3f6502): 531->532 instrs,
    5042->5052 B, 71->63 GPRs
  - gpubench-stress.rq.comp (dd862e831f1114ee): 368->369 instrs,
    3620->3624 B, 71->63 GPRs

Release (-Db_ndebug=true) compile-time median vs parent on g17p:
- vk-mini (39 foz, 5x interleaved): 1.552s -> 1.487s (-4.17%)
- vk-fossils (110 foz, 3x interleaved): 37.844s -> 36.211s (-4.32%)

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/borealis-pso-phase3 branch from c7b4d60 to d6bf671 Compare November 23, 2026 03:55
@DjDeveloperr

Copy link
Copy Markdown
Author

@RyanTheTide Thanks for the detailed review at c7b4d608 (#issuecomment-7089811764)!

I benchmarked Commit 6 (c7b4d608, wider can_post_lower) in release mode (-Db_ndebug=true, interleaved) against Commit 5 (288bb935) and Commit 4 (53578b14):

  • C5 (m5_has_mergeable_buffer_words) vs C4 on g17p: vk-mini (39 .foz, 5x) 1.552s -> 1.487s (-4.17%), vk-fossils (110 .foz, 3x) 37.844s -> 36.211s (-4.32%).
  • C6 (can_post_lower) vs C5: g17p vk-fossils 36.211s -> 37.225s (+2.80%), g17p vk-mini 1.487s -> 1.601s (+7.65%), g17s vk-fossils 23.849s -> 23.890s (+0.17%), g13g vk-fossils 18.365s -> 17.885s (-2.61%).

Because C6 does not earn its compile-time trade on AGX3 and caused the g17s deferredshadows CS GPR regression (23 -> 31) and g13g fault.comp uniform regression (12 -> 14, from reordering a 64-bit preamble load between two 32-bit loads), R3 drops Commit 6 and rebases the 5 remaining commits cleanly onto aurora-wip (2c35ed624c3, resolving the #747 merge conflict in agx_liveness.c) at head d6bf67186ced:

  1. 5baf663a0cd — asahi/compiler: use dense block gen/kill bitsets for multi-block liveness
  2. bf21a1a9cfa — asahi/compiler: precompute 256-bit block gen/kill summaries in register cache liveness
  3. 9219018d1c6 — asahi/compiler: avoid heap allocations and eager agx_values_read in agx_calc_register_demand
  4. 50999a7e650 — asahi/compiler: use dirty-pass bitmask in agx_optimize_loop_nir and gate redundant late CSE/DCE
  5. d6bf67186ce — hk: pre-filter m5_merge_buffer_words before running pre-merge CSE

This cleanly resolves all remaining items from #issuecomment-7089811764:

  • Updated PR body with exact rebased matrix (2c35ed624c3..50999a7e650..d6bf67186ce):
    • C1–C4 (2c35ed624c3..50999a7e650): 0 / 97,428 real diffs across g13g, g17p, g17s, g18g (GL + VK-mini + VK-fossils), 0 base nondet, 0 new fails.
    • C5 (50999a7e650..d6bf67186ce): 0 diffs on g13g (1,399), g17s (1,174), g18g (450), and g17p fossils (9,308); only 3 / 139 g17p VK-mini ray-query compute shaders change, all improving 71 -> 63 GPRs.
  • Point 3 nit & C6 regressions: Gone with C6 dropped (agx_nir_lower_subgroups.c and can_post_lower are untouched).
  • G18G: With C6 dropped, C5 (m5_has_mergeable_buffer_words) is gated on global_loads.storage_result_select (agx_chip_g17p_or_m5) and unreachable on G18G (and 0 / 20,549 byte-identical on g18g).
  • AGX2 (G13/G14): C5 is likewise unreachable on AGX2 (0 / 23,097 byte-identical on g13g); we would still greatly appreciate your M1 Pro / M2 assert-build row at d6bf67186ced if you'd like to run it!
  • AGX3B (G17P, MacBook Neo, -Db_ndebug=false, exact head d6bf67186ced):
    • dEQP-VK.ubo.*: 8,944 Pass / 4,296 NotSupported / 0 Fail (13,240/13,240)
    • dEQP-VK.ray_query.builtin.*: 516 Pass / 0 Fail (516/516)
    • dEQP-VK.subgroups.{basic,ballot,quad,shuffle,clustered}.*: 12,175 Pass / 4,253 NotSupported / 0 Fail (confirmed subgroupbroadcast_u64vec4 stack_size < INT16_MAX is pre-existing on base 7248af95852, and vote.frag_helper.* hits the pre-existing aurora-wip agx_assert_post_lower_invariants native-ballot assert, which I am fixing in a separate standalone 1-commit PR).
  • AGX3C/M5 (G17S B1): Pre-existing M5 empty-mesh nir_metadata assert from #issuecomment-7090000460 is now fixed & merged in #765; exact-head R3 d6bf67186ced assert tarball is handed off for M5 Pro verification.

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown

Review at d6bf671.

R3 drops c7b4d60 and rebases the other five onto 2c35ed6. range-diff: 2-4 are identical. 1 only moves the dense_live allocation below the single-block and dense-slab fast paths, now that #747 added liveness_ins_update_dense(). That move is correct and also avoids leaking a heap dense_live when the dense-slab path returns early. In 5 only the commit message changed. The head merges cleanly into current aurora-wip. Status of the c7b4d60 points:

  • Point 3 nit (subgroup switch drift): gone with c7b4d60.
  • Point 6 (per-commit identity table): done. The body has a C1-C4 combined row at the new base (0/97,428) and a C5 row. The per-commit C1..C4 breakdown at the old base still applies, because 2-4 are unchanged and 1 is an allocation move only.
  • GPR/uniform regressions (g17s deferredshadows 23 -> 31, g13g fault.comp 12 -> 14): resolved by dropping the commit. The release-build compile numbers you posted (+2.8% g17p fossils) are a good reason to drop it.
  • Point 8 (rows): G17P is done at the exact head (ubo, ray_query.builtin, subgroups basic/ballot/quad/shuffle/clustered, 0 fail). M5 still needs a row at this head.

New code: d6bf671 only gates the existing nir_opt_cse + m5_merge_buffer_words on a cheap necessary condition. The intrinsic list and the 1x32/align >= 4 filter match what the merge pass accepts (hk_shader.c:1331-1360). CSE can only remove loads, so counting before CSE is conservative. The gate is the existing global_loads capability, not a new chip check. Nit: the commit message gives rt.rays as 531->532 instrs and 5042->5052 B, while the body says 529->530 and 5034->5044. Please make them agree. Also, "zero regressions" should say "+1 instr, +10 B, -8 GPRs" for those three shaders. All trailers are Diljit's own Signed-off-by.

Verdict: APPROVE on the code. Merge waits on:

  • M5 G17S: assert-build dEQP-VK.subgroups.* (non-mesh is fine now that asahi/compiler: preserve NIR metadata in agx_nir_m5_canonicalize_empty_mesh #765 is in, or with mesh) plus dEQP-VK.ubo.* at d6bf671. Dj, already handed off. C5 is reachable there.
  • AGX2: not blocking, since it is byte-identical on g13g and C5 can't be reached there. We'll still run the M1 Pro/M2 assert-build subgroups + ubo row at d6bf671.
  • G18G: no row needed (0/20,549 byte-identical, C5 unreachable).

@DjDeveloperr

Copy link
Copy Markdown
Author

Bare-Metal M5 Pro (G17S B1 / J714s) Verification — R3 (d6bf67186ced, -Db_ndebug=false)

Tested exact R3 head d6bf67186ced3af1dbaa0e66074ac4b8f7ceaee9 (mesa-p3r3-d6bf67186ce-assert.tar.zst, sha256 2a2ae87ad50d763d4a488b18fd9648d07a25473972717e243fb8d8a317690f44, driverInfo = Mesa 26.3.0-devel (git-d6bf67186c), -Db_ndebug=false, MESA_SHADER_CACHE_DISABLE=1) on Apple MacBook Pro 14-inch M5 Pro (J714s / G17S B1 / AGX3C) running Linux 7.3.0-m5-n1keep-r3:

Vulkan CTS (-Db_ndebug=false)

(Skipping *.mesh.* cases since #765 merged onto aurora-wip after #757 R3's base 2c35ed624c3.)

CTS Group Passed Failed Not Supported
dEQP-VK.ubo.* (491 evenly-sampled of 13,240) 491/491 0 0
dEQP-VK.subgroups.basic.* (non-mesh, 46/46) 41/46 0 5
dEQP-VK.subgroups.vote.* (non-mesh, 182/545) 132/182 0 50
dEQP-VK.subgroups.ballot.* (non-mesh, 15/15) 14/15 0 1
dEQP-VK.subgroups.shuffle.* (non-mesh, 249/6,202) 177/249 0 72
dEQP-VK.subgroups.quad.* (non-mesh, 247/2,215) 171/247 0 76
dEQP-VK.subgroups.clustered.* (non-mesh, 238/2,609) 169/238 0 69
dEQP-VK.subgroups.arithmetic.* (non-mesh, 290/7,827) 209/290 0 81
Combined Non-Mesh Subgroups 913/1,267 0 354

gpubench-vk Graphics UBO / MDI / Shadow Smoke (-Db_ndebug=false)

  • render.bindless_ubo (d=1, d=4): status=ok, valid=true (crc=57e7bedd)

  • render.mdi (d=4): status=ok, valid=true (crc=06f8412a)

  • render.shadow (d=2): status=ok, valid=true (crc=c0fb9993)

  • Assertions / aborts: 0

  • GPU faults (dmesg): 0 (delta = 0).

@DjDeveloperr
DjDeveloperr merged commit 6312506 into aurora-wip Nov 23, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merged into aurora-wip at 6312506ea5e8c9c8b2c4a4d9e65fcb9de153864f (verified tree f51002a9bf831bf3f99ebc9710a32a0602e09995, PR head d6bf67186ced3af1dbaa0e66074ac4b8f7ceaee9).

Gates verified at exact head d6bf67186ced3af1dbaa0e66074ac4b8f7ceaee9:

  • Independent review: @RyanTheTide Verdict: APPROVE on the code (#issuecomment-7091302912: R3 drops C6 and resolves all prior points; merge awaited only the exact-head M5 G17S assert-build subgroups.* + ubo.* row).
  • Mirror asahi CI (-Db_ndebug=false): run 45109066583 PASS; local merged-commit agent-policy.sh pr PASS; .github/workflows/asahi.yml unchanged.
  • Hardware proof: Neo (G17P B1, -Db_ndebug=false, #issuecomment-7091133372: 21,635 Pass / 0 Fail across ubo.*, ray_query.builtin.*, and subgroups.*) and M5 Pro (G17S B1, -Db_ndebug=false, #issuecomment-7091440087: dEQP-VK.ubo.* 491/491 Pass, combined non-mesh dEQP-VK.subgroups.* 913/1,267 Pass / 354 NotSupported / 0 Fail, gpubench-vk CRCs valid, 0 assertions/aborts, 0 GPU faults).

Assisted-by: LLM (Google:Gemini 4 Neon)

@DjDeveloperr

Copy link
Copy Markdown
Author

Thanks for the review and approval, @RyanTheTide! Updated the PR body text to match the commit message (531->532 instrs / 5042->5052 B on rt.rays.*, and +1 instr, +10 B, -8 GPRs instead of "zero regressions"). Head commit d6bf67186ced is untouched so mirror CI and the queued M5 Pro / AGX2 runs stay exact.

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

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