Repository navigation
agx: sink epilogue defs past high-pressure loops and fold imadshl bitfield slices - #1496
Conversation
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
-
src/asahi/compiler/agx_nir_opt_licm.c:1964indexesest.halveswith SSA IDs created after the array was allocated. When the first high-pressure loop rematerializes a value,nir_instr_insert()assigns the clones new IDs starting at the originalssa_alloc. The next high-pressure loop's Pass B visits those definitions and reads pastest.halves. Skip definitions outside the estimate's original SSA range and cover two sequential loops where the first one clones a tree. -
src/asahi/compiler/agx_nir_lower_shared_bitsize.c:1010–1033silently drops a term when flattening fills the array. With animadshl_agxroot whose left subtree consumes 12 slots, the shifted right operand is omitted. The collector returns those 12 terms as if the tree were complete, so a slice merge can rebuild the expression without the right operand. Preserve an unflattened subtree or abandon the rewrite on exhaustion, and check the value of an over-budget tree in the regression test.
|
Both items fixed and rebased onto updated #1495 (
Verified offline ( |
403cc7c to
d6e7c83
Compare
DjDeveloperr
left a comment
There was a problem hiding this comment.
Rechecked both fixes. Pass B skips SSA IDs outside the original estimate before indexing it. The collector also abandons the entire rewrite when either a leaf or the shifted imadshl operand would exceed capacity. The sequential-loop and over-budget value tests cover both paths. Approved the fork CI runs. The #1495 stack and current-head hardware coverage remain open.
c409f4a to
92ab040
Compare
|
Rebased onto Note on the red |
|
Row: Correction: an earlier version of this comment called the J616s an M3 Max. It is an M4 Pro (
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
The J616s results qualify the AGX3 regression run at the reported heads. Please hold the stack for the corrected #1494 at 4ab768672c5d. The current #1495 and #1496 heads both contain 9798b55b7d75, which predates #1494’s workgroup-uniformity guard. Once #1494 lands, update #1495 and then #1496 so both carry the corrected pass. The updated heads need CI and hardware results.
|
Row: This is the non-M4 cross-generation regression row from Not yet.
|
Extend g16_version_aligned_robust_loops() on G16G with g16_try_version_bounded_loop() for fixed-workgroup compute loops with workgroup-uniform affine trip bounds. Tighten g16_build_ub64() for high-bit alignment masks in nir_op_iand so aligned masks (e.g. 0xffffffe0) intersect with the operand upper bound instead of saturating to the raw mask, and deduplicate dominated (base, const) buffer-end and nowrap checks in the pre-loop guard prologue. Add hk_bounded_loop_test covering dynamic alignment masks, exact-bound vs 1-byte-short guards, and 32-bit wrap rejection. Add agx_nir_opt_bitfield_slices() on AGX3, fold split_lo(zext_16_to_32(x16)) -> x16 in agx_opt_collect_split(), and widen f16 MOV_IMM sources of f32 FADD/FMUL/FFMA to exact f32 before agx_opt_promote_constants() on AGX3B. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
…field slices
Two AGX3 compute compiler improvements:
1. agx_nir_opt_sink_past_loops() (agx_nir_opt_licm.c, agx_nir.h, agx_compile.c):
- Runs before agx_nir_opt_licm() on AGX3 compute shaders with high-pressure
loops (>= 96 estimated live halves).
- Sinks pure epilogue-only definitions past the loop into the post-loop
block and rematerializes pure zero-register-leaf (system-value / uniform /
constant) epilogue trees after the loop so post-loop store addressing
does not consume live-through registers across the hot loop body.
2. agx_nir_opt_bitfield_slices() (agx_nir_lower_shared_bitsize.c):
- Extends slice parsing and associative term collection to recognize
imadshl_agx(a, 1, b, k), power-of-two imul/amul shifts,
iand(ushr(x, k), mask), and ushr(iand(x, mask), k) narrowing so
multi-slice decode trees fold into a single ubitfield_extract.
Adds BitfieldSlicesImadshlAndShiftedMask and
SinkEpilogueDefsPastHighPressureLoop unit tests in test-shared-byte-offsets.cpp.
Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
Define each initial value in the preheader and add both operands before inserting the phi. This restores the tested fixtures that were lost while resolving the rebase conflict. Signed-off-by: DjDeveloperr <hey@djdev.me>
ba957cd to
3048958
Compare
|
Restacked onto the updated Verified offline with |
DjDeveloperr
left a comment
There was a problem hiding this comment.
This head carries #1495 at 408af606, including the #1494 uniformity guard. The upper-layer patches, including the regression-test phi fix, are unchanged by the restack. The author reports both loop suites and 1,912 AGX tests passed. This resolves my change request. Please land #1495 first and record CI and hardware results for this head.
|
Re-row at exact head
|
satchlj
left a comment
There was a problem hiding this comment.
Verdict: FINISH, then merge
Blockers:
check_sink_remat_def()skips checking sources ofload_push_constant/load_kernel_input, whileclone_sink_remat_def()recursively clones them unconditionally (src/asahi/compiler/agx_nir_opt_licm.c:1720-1721, 1761-1771, 1799-1804):is_zero_reg_remat_leaf()returnstruefornir_intrinsic_load_push_constantandnir_intrinsic_load_kernel_input, both of which take an offset operand (num_srcs = 1,src[0]).check_sink_remat_def()returnstrueimmediately in theis_zero_reg_remat_leaf()branch without callingnir_foreach_src(instr, check_sink_remat_src, ctx).clone_sink_remat_def()then loops overnir_intrinsic_infos[intr->intrinsic].num_srcsand recursively deep-clones everyintr->src[i].ssaintoafter.- If a
load_push_constantorload_kernel_inputhas a dynamic offset sourced from anir_phi_instr, a memory load (load_global/load_shared), an atomic, a trapping integer division, or an unbounded ALU tree, Pass B approves it and clones the unchecked dependency chain intoafter— producing invalid NIR (for a cloned phi) or duplicating side-effecting / post-loop-mutated loads. - Fix: run
nir_foreach_src(instr, check_sink_remat_src, ctx); return ctx->ok;inside theis_zero_reg_remat_leaf()branch (or requirenir_src_is_const(intr->src[0])forload_push_constant/load_kernel_inputinis_zero_reg_remat_leaf()), and add a unit test with a non-rematerializable dynamic offset (e.g.load_global) feedingload_push_constantto verify Pass B leaves it untouched.
- Stack and CI gating:
#1494(4ab768672c5d) and#1495(408af606be87) are still open againstaurora-wip, and3048958d8292has no hosted or fork CI run recorded yet (hosted Actions is currently disabled;pr-readinessis still atba957cd0). Land#1494and#1495first, fix thecheck_sink_remat_def()source check above, and record CI + exact-head hardware results on the updated head; ready to flip toAPPROVEonce those are in.
Checked:
src/asahi/compiler/agx_nir_lower_shared_bitsize.c:is_imadshl_add_shift(),parse_bit_slice_shifted()(includingushr(iand(x, mask), k)andiand(ushr(x, k), mask)),collect_add_or_terms()over-budget bailout, andopt_bitfield_slice_instr()iand/imadshl_agxslice merges are sound (exact 32-bit scalar algebraic identities, disjoint bit ranges foriaddslice merges).src/asahi/compiler/agx_nir_opt_licm.c: Pass A reverse-order sinking intonir_after_phis(after)preserves dominance order across chained defs; Pass Bdef->index >= est.ssa_allocguard prevents out-of-bounds reads across sequential high-pressure loops.src/asahi/compiler/test/test-shared-byte-offsets.cpp: regression tests including3048958d8292valid loop phis cover the sequential-loop bounds, over-budgetimadshl_agxpreservation, and slice merges.- Exact-head
3048958dhardware PASS rows on J616s (G16S) and J615 (G15G, non-M4) with 9,670 Pass->Pass, 0 Fail, 0 GPU faults.
Evidence
- Audited
#1496delta408af606be87...3048958d8292(agx_compile.c,agx_nir.h,agx_nir_lower_shared_bitsize.c,agx_nir_opt_licm.c,test-shared-byte-offsets.cpp). - Verified
nir_intrinsics.py:1419(load("push_constant", [1], ...)) andnir_intrinsics.py:1443(load("kernel_input", [1], ...)) both carry 1 source operand (src[0]). - Verified
#1494(4ab768672c5d) and#1495(408af606be87) are open,statusCheckRollupis empty at3048958d8292, and exact-head J616s/J615 hardware rows passed (#issuecomment-7337065825).
check_sink_remat_def() returned true immediately for zero-reg remat leaves including nir_intrinsic_load_push_constant and nir_intrinsic_load_kernel_input, both of which take an offset operand (src[0]), while clone_sink_remat_def() recursively clones every intrinsic source into the post-loop block. Call nir_foreach_src(instr, check_sink_remat_src, ctx) inside the is_zero_reg_remat_leaf() branch so dynamic non-rematerializable offsets (e.g. load_global, load_shared, phis, atomics, trapping integer div, or over-budget ALU trees) are rejected cleanly. Add a regression test in test-shared-byte-offsets.cpp covering a load_global offset feeding load_push_constant across a high-pressure loop. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
@satchlj Great catch — fixed in
Once |
…tfield-slices Resolve conflicts against aurora-wip (e86174f) after #1494 and #1497 merged: - src/asahi/vulkan/hk_shader.c: keep g16_try_version_bounded_loop() and its call before the landed #1494 aligned-loop fallback. - src/asahi/compiler/agx_nir_lower_shared_bitsize.c: keep #1497 shared-index reassociation followed by #1495 bitfield-slice folding. - src/asahi/compiler/test/test-shared-byte-offsets.cpp: keep #1497 shared-index tests followed by #1495 bitfield-slice tests. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
Forward-merged updated Verified offline on AArch64 (
|
|
Exact-head M5 Max ( Correctness (
|
| Arm | pp128 (t/s) |
pp512 (t/s) |
tg32 (t/s) |
|---|---|---|---|
aurora-wip e86174f6 (A1 / A2) |
769.00 ± 0.46 / 769.05 ± 1.31 |
877.32 ± 2.18 / 872.84 ± 3.94 |
121.81 / 122.03 |
#1495 b1b7663c (B1 / B2) |
768.16 ± 0.84 / 767.33 ± 0.98 |
871.82 ± 4.05 / 868.87 ± 2.79 |
122.37 / 122.30 |
#1496 3e1583db (C1 / C2) |
745.40 ± 1.05 / 745.01 ± 1.06 (-3.1%) |
831.11 ± 3.36 / 833.87 ± 4.47 (-4.5%) |
122.39 / 122.51 |
Root-cause isolation (AGX_MESA_DEBUG=shaderdb + pass toggle)
Disabling only agx_nir_opt_sink_past_loops at 3e1583db5f13 (leaving the imadshl bitfield slice folding active) fully restores throughput on the same boot (-r 3):
#1496withoutagx_nir_opt_sink_past_loops:pp128766.83 ± 0.36 t/s,pp512872.98 ± 4.83 t/s,tg32122.08 ± 0.28 t/s#1496full (3e1583db):pp128744.92 ± 0.60 t/s,pp512835.37 ± 1.09 t/s,tg32122.49 ± 0.29 t/s#1495(b1b7663c):pp128767.44 ± 1.76 t/s,pp512868.61 ± 4.93 t/s,tg32122.24 ± 0.34 t/s
AGX_MESA_DEBUG=shaderdb pinpoints the exact shader change on the hot Q4_0 prefill compute kernel (239 gprs, 384 threads, 1 loops):
#1495(b1b7663c):10058 instrs,100342 code size,239 gprs,384 scratch,384 threads,233:278 spills:fills,10721 native,24 carriers,4 loop uniform copies#1496withoutsink_past_loops:10056 instrs(-2),100314 code size(-28),239 gprs,384 scratch,384 threads,233:278 spills:fills,10719 native,24 carriers,4 loop uniform copies#1496full (3e1583db):10206 instrs(+148),101180 code size(+838),239 gprs,352 scratch(-32),384 threads(no occupancy change),256:299 spills:fills(+23 spills, +21 fills),10863 native(+142),15 carriers,6 loop uniform copies(+2)
agx_nir_opt_sink_past_loops drops scratch (384 -> 352) and carriers (24 -> 15) without crossing an occupancy tier (239 gprs, 384 threads), while increasing spills:fills by +23:+21 and instructions by +148 inside an already-spilling high-pressure matmul loop.
DjDeveloperr
left a comment
There was a problem hiding this comment.
At src/asahi/compiler/agx_compile.c:21416, the new loop-sinking pass runs for AGX3C compute shaders. The exact-head J775c A/B shows a reproducible Vulkan Q4_0 prefill regression: pp128 falls 3.1% and pp512 falls 4.5% versus aurora-wip. Compared with #1495, the hot shader adds 23 spills, 21 fills and 148 instructions, with the same occupancy. Disabling only the sinking pass restores both prefill rates. J775c measurements
Please fix the profitability check or leave loop sinking disabled on AGX3C for now. Keep the independent bitfield folding enabled. Re-run the same J775c prefill comparison against #1495 on the updated head and check that throughput is restored.
On AGX3C (e.g. J775c M5 Max), sinking epilogue definitions past an already-spilling high-pressure Q4_0 matmul loop can reduce carriers without crossing an occupancy tier while increasing spill/fill traffic and instructions, regressing Vulkan prefill throughput. Gate agx_nir_opt_sink_past_loops() in agx_compile_shader_nir_internal() to is_agx3b compute shaders while keeping agx_nir_opt_bitfield_slices() enabled across all AGX3 targets. Add an end-to-end compile test (SharedByteOffsets.SinkEpilogueGatedToAgx3b) verifying that AGX3B sinks an epilogue-only imul past a high-pressure compute loop while AGX3C leaves it before the loop. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
@DjDeveloperr @satchlj Thanks for the J775c A/B isolation and review. Updated in
Verified offline on AArch64 (
I only have M4 ( |
|
Re-verified at exact head
The Untested in this row: live |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Rechecked the fix at c9663ba. Loop sinking now runs only for AGX3B compute, and bitfield folding remains enabled for AGX3C. The compiler test covers both paths. The J775c exact-head A/B against #1495 puts pp128 and pp512 within 0.3% of #1495 in both modes, with zero GPU faults. This resolves my change request in review 6151487550.
Asahi CI on the merge with aurora-wip d704571 passed 396 tests with one skip, and agent policy passed. M4 and J615 CTS results on the current candidate are still needed before merge.
satchlj
left a comment
There was a problem hiding this comment.
Reviewed c9663bad4ef9 on top of merged #1495 (b1b7663c): gating agx_nir_opt_sink_past_loops() to is_agx3b && mesa_shader_stage_is_compute(nir->info.stage) (ISA encoding property) cleanly disables loop sinking on AGX3C while keeping agx_nir_opt_bitfield_slices() active across all AGX3 targets, and SharedByteOffsets.SinkEpilogueGatedToAgx3b verifies both the AGX3B sink and AGX3C no-sink paths end-to-end.
Re-verified at exact head c9663bad4ef9 on M5 Max (AGX3C): prefill throughput is fully restored (-0.02% / +0.29% vs #1495), 302/302 unit tests OK, 39/39 SharedByteOffsets.* pass including SinkEpilogueGatedToAgx3b, and 0 GPU faults. LGTM.
|
Row: This is the AGX3B row at the exact head. Since
|
|
Follow-up row: merge candidate + J700 (MacBook Neo, A18 Pro,
|
|
J700 fired-path row: PASS. This covers exact head Setup
Correctness
Fired path (probe ICD)
Faults and handback
|
|
Current merge-tree qualification for
|
What: Add
agx_nir_opt_sink_past_loops()beforeagx_nir_opt_licm()on AGX3 compute shaders (src/asahi/compiler/agx_nir_opt_licm.c,agx_nir.h,agx_compile.c) to sink pure epilogue-only definitions and rematerialize pure zero-register-leaf (system-value / uniform / constant) epilogue trees past high-pressure loops (>= 96estimated live halves); and extendagx_nir_opt_bitfield_slices()(src/asahi/compiler/agx_nir_lower_shared_bitsize.c) to recognizeimadshl_agx(a, 1, b, k)add-with-shift trees, power-of-twoimul/amulshifts,iand(ushr(x, k), mask), andushr(iand(x, mask), k)narrowing.Tested:
j604-agx-sink-past-loops-and-imadshl-bitfield-slices(403cc7c7, stacked onj604-hk-agx-uniform-loop-bounds-and-bitfield-slices0b73d291-> hk,agx: version aligned robust compute loops on G16G and refine preamble weights #1494 ->aurora-wip866b091e) in an aarch64 Linux container (gcc 16.1.1) with 0 warnings across all 5 modified files:hk_bounded_loop_test:PASS: hk_bounded_loop (4/4 cases).agx_tests:1896 passed / 0 failed(1skipped, +2 new unit testsBitfieldSlicesImadshlAndShiftedMaskandSinkEpilogueDefsPastHighPressureLoop);libvulkan_asahi.solinked cleanly.ASAHI_SHIM_CHIP=g16g,NIR_DEBUG=validate,agx_validate(), combined1.8320stack one705eca833f,217/217meson tests,1242/1242agx_tests):llama-benchVulkan compute shaders (1.8318 -> 1.8320):MUL_MAT q4_Khot loop410 -> 375(-35/-8.5%,-19.5%vs1.8317,127GPRs /832thr,0:0spills),MUL_MAT q6_Khot loop560 -> 508(-52/-9.3%,-18.8%vs1.8317,127GPRs /832thr,0:0spills),FLASH_ATTN_EXT159GPRs /640thr,0regressions across all 16 pipelines.omarchy-mlxQwen3.5-2B shaders (both stock andG14C-FAST, apples-to-apples1.8318 -> 1.8320):2improved ([4]949 -> 929,[8]999 -> 979@127GPRs /832thr),30same,0regressed,0:0spills.vk-sdbshaders:0occupancy regressions vs1.8318,0:0spills.J604,T8132/G16G),Llama-3.2-1B-Instruct-Q4_K_M(llama-bench,n=10QUIET,0GPU faults): shipped combined stack (1.8320vs1.8314on GOOD1.1677) measuredpp512 1445.90 tok/s(+14.18%,89.8%of native Metal,min=1440.22 max=1447.42 spread=0.50%),tg128 92.50 tok/s(+4.18%,iqr=0.20% spread=0.61%), livePERF62per-opq4_K=281.7ms,q6_K=52.2ms,fa=22.8ms,0GPU faults.Risk: two-way door · AGX3 compute compiler —
agx_nir_opt_sink_past_loops()only moves pure non-trapping defs whose uses are all dominated by the post-loop block and rematerializes pure zero-register-leaf (load_const/load_preamble/load_push_constant/ workgroup/invocation system-value) trees bounded to 64 ops on compute shaders with high-pressure loops (>= 96live halves);agx_nir_opt_bitfield_slices()extensions are exact bit-level algebraic identities.Not yet: Depends on #1494 and
j604-hk-agx-uniform-loop-bounds-and-bitfield-slices(0b73d291) foragx_nir_opt_bitfield_slices(); standalone hardware A/B built directly fromaurora-wip(403cc7c7) and a non-M4 cross-generation hardware regression row (we only have an M4J604locally).Details and verification
Summary of changes (
403cc7c7stacked on0b73d291/ #1494 /aurora-wip866b091e)src/asahi/compiler/agx_nir_opt_licm.c,agx_nir.h,agx_compile.c(agx_nir_opt_sink_past_loops):agx_nir_opt_licm()on AGX3 compute shaders containing high-pressure loops (>= 96estimated live halves).nir_after_phis(after)).load_const,undef,load_preamble,load_push_constant,load_kernel_input, workgroup/invocation/subgroup system values, capped at 64 ops with a sharedclone_map) used after the loop, freeing live-through registers across the loop body foragx_nir_opt_licm()and register allocation.SinkEpilogueDefsPastHighPressureLoopunit test insrc/asahi/compiler/test/test-shared-byte-offsets.cpp.src/asahi/compiler/agx_nir_lower_shared_bitsize.c(agx_nir_opt_bitfield_slices):parse_bit_slice_shifted()andcollect_add_or_terms()to matchimadshl_agx(a, 1, b, k)add-with-shift trees, power-of-twoimul/amulshifts,iand(ushr(x, k), mask), andushr(iand(x, mask), k)width narrowing so multi-slice bit-unpack expressions fold into a singleubitfield_extract.BitfieldSlicesImadshlAndShiftedMaskunit test insrc/asahi/compiler/test/test-shared-byte-offsets.cpp.Verification
ninja -C /build src/asahi/compiler/agx_tests src/asahi/vulkan/hk_bounded_loop_test src/asahi/vulkan/libvulkan_asahi.so: 0 warnings in all 5 modified files./build/src/asahi/vulkan/hk_bounded_loop_test:PASS: hk_bounded_loop (4/4 cases)./build/src/asahi/compiler/agx_tests --gtest_brief=1:1896 passed, 1 skipped, 0 failed.