Repository navigation
agx: pack loop-invariant 8-bit mux trees, pre-negate byte diffs, and hoist AGX3B readonly loop loads - #735
agx: pack loop-invariant 8-bit mux trees, pre-negate byte diffs, and hoist AGX3B readonly loop loads#735DjDeveloperr wants to merge 2 commits into
Conversation
|
Review at 1f2c12e.
I checked these and found nothing wrong: the mux packing (contiguous and strided index math, mask peel), the Rows owed at this head: hardware runs exist only for G17P. Missing: an M3/M4-class AGX3B hardware dEQP-VK compute/ssbo/loops row (G15/G16), a G17G/G17S (AGX3C) hardware dEQP row that includes graphics/fragment loops (this LICM change reaches all stages there), and a G13/G14 row, or scope the algebraic rule to AGX3. |
…2-bit isub
Add agx_nir_opt_pack_narrow_mux_tree on AGX3B/AGX3C compute shaders:
1. Fold canonicalized 32-bit iadd(a, ineg(b)) back into native isub
(removing the dead ineg immediately so downstream consumers see a
single use), scoped to AGX3 compute only.
2. Pack power-of-two tables of 8..32 loop-invariant 8-bit SSA values
selected via a bounded bcsel tree into nwords = n / 4 32-bit words in
the loop preheader (automatically selecting contiguous vs strided
layout to minimize preheader register pressure) and replace the byte
tree with an (nwords)-way 32-bit select plus one ubitfield_extract.
- Require internal_nodes > (nwords - 1) + 3 (strictly fewer in-loop
ALU ops than the lowered word selects + index/shift/ubfe).
- Require distinct > 2 * nwords (strictly fewer live-across-loop
16-bit half-registers than the distinct runtime 8-bit leaves).
3. Peel redundant power-of-two masks (raw_idx & (n - 1)) before subfield
extraction when strided, and conditionally bitwise-complement packed
words and c in the preheader when a loop-invariant signed difference
(inv_cond ? -(c - b) : (c - b)) has c in [0, 255], using
(~b & 0xff) - (255 - c) == c - b to collapse in-loop ineg + bcsel
into a single isub.
Signed-off-by: DjDeveloperr <hey@djdev.me>
1f2c12e to
5267549
Compare
|
@RyanTheTide Thanks for the thorough review — pushed R2 (
Exact-Head Verification —
|
|
Re-checked at 5267549 against 1f2c12e. All four earlier points are fixed:
I found nothing wrong in the new code. Store/barrier/coherent rejection, zero-trip loops (guard plus init-value merge phi) and the pre-negation (zero-extended bytes only, so no sign edge) all hold up. One small nit: Rows still missing at 5267549: the G17P hardware row (queued), and AGX3B compute dEQP rows (compute/ssbo/loops) on G15 and G16G, because the readonly hoist reaches those. The pack pass also runs on AGX3C compute, which so far only has the compile sweep. Assisted-by: LLM (Claude:Opus 5.5) |
Exact-head
|
| Workload | #704 base |
#735 R2 (5267549c) |
Speedup | Est Score (base -> head) |
Neo macOS Metal | % of Metal (base -> head) |
|---|---|---|---|---|---|---|
| Feature Matching | 39.00 ms | 36.92 ms | 1.056x (+5.6%) | 7,277 -> 7,687 | 11,374 | 64.0% -> 67.6% |
| Edge Detection | 20.69 ms | 20.72 ms | 0.998x | 30,475 -> 30,422 | 30,142 | 101.1% -> 100.9% |
| Horizon Detection | 27.13 ms | 27.07 ms | 1.002x | 26,265 -> 26,325 | 28,664 | 91.6% -> 91.8% |
| Stereo Matching | 11.78 ms | 11.78 ms | 1.000x | 105,498 -> 105,504 | 89,218 | 118.2% -> 118.3% |
| Gaussian Blur | 42.06 ms | 42.09 ms | 0.999x | 13,081 -> 13,071 | 33,051 | 39.6% -> 39.5% |
| Background Blur | 26.04 ms | 26.03 ms | 1.000x | 8,596 -> 8,597 | 13,388 | 64.2% -> 64.2% |
| Face Detection | 39.53 ms | 39.54 ms | 1.000x | 7,145 -> 7,143 | 27,571 | 25.9% -> 25.9% |
| Particle Physics | 126.98 ms | 126.96 ms | 1.000x | 33,451 -> 33,456 | 69,542 | 48.1% -> 48.1% |
| OVERALL (est) | — | — | 1.007x | 18,933 -> 19,063 | 30,474 | 62.1% -> 62.6% |
Feature Matching Per-Kernel Breakdown (#704 base -> #735 R2)
fm-fast(442ebe38 -> 2f820096): 9.19 ms (301i / 73g) -> 7.27 ms (310i / 63g) (1.264x / -20.9%)fm-scale(7804f45a -> 81269e52): 1.96 ms (188i / 55g) -> 1.87 ms (188i / 55g) (1.048x / -4.6%)
Exact head 5267549c68095aa7561d0951dbc5d63b0c13231b is unchanged and matches green mirror asahi CI (45048200756).
Assisted-by: LLM (Antigravity:Gemini 4 Neon)
|
Executed check at 5267549 merged onto aurora-wip f0ae30b. The merge has two conflicts, in agx_compile.c and test-shared-byte-offsets.cpp: the stack under this PR still carries the shared-phase CSE that #728 moved to its own file. I resolved them by hand (dropped the duplicated helpers, put back the nir_instr_set.h include, kept both test sets). Results with clang release+asserts, asahi+poly suites: merged 235/236, this head alone 216/216, aurora-wip alone 235/235.
|
|
The stack blocker reported in the last pass ( |
In agx_nir_opt_licm:
- Count pack_64_2x32_split of uniform preamble/constant halves as 0 GPR
halves in the register-pressure estimator.
- When fits_all && speculative (AGX3B non-internal compute), add
hoist_pure_readonly_loop to hoist budget-fitting loop-invariant pure
loads from pure read-only loops (no stores, atomics, barriers, calls,
or volatile/coherent memory accesses):
* Phase 1 hoists exit-dominating invariant loads directly before loop.
* Phase 2 evaluates the header exit condition at entry and wraps the
loop in a zero-speculation guard before hoisting body-invariant
loads, bailing cleanly whenever the loop successor block starts with
a phi (e.g. early-break searches) so SSA dominance is preserved.
Adds unit tests in test-agx3-vision-opts.cpp:
- PackNarrowMuxTreeAndFoldIsub
- PackNarrowMuxTreePreNegateAndMaskPeel
- PureReadonlyLoopLicmHoist (including early-break exit-phi search and
speculative == false negative controls).
Signed-off-by: DjDeveloperr <hey@djdev.me>
5267549 to
2db79e7
Compare
|
Checked 2db79e7. The stray doc comment is gone, and the test now pins the zero-trip guard polarity ( Assisted-by: LLM (Claude:Opus 5.5) |
|
Pushed R3 at
Assisted-by: LLM (Antigravity:Gemini 4 Neon) |
|
Clean-room M5 Pro (
Assisted-by: LLM (Google:Gemini 4 Neon) |
|
Exact-head M4 (
Assisted-by: LLM (Gemini:4 Neon) |
satchlj
left a comment
There was a problem hiding this comment.
Verdict: MERGE
Checked: Full diff across src/asahi/compiler/agx_compile.c, src/asahi/compiler/agx_nir.h, src/asahi/compiler/agx_nir_opt_guard_selected_loads.c, src/asahi/compiler/agx_nir_opt_licm.c, and src/asahi/compiler/test/test-agx3-vision-opts.cpp, native assert-enabled build (-Db_ndebug=false), meson test --suite asahi, and live Vulkan CTS on Mac Studio M5 Max (J775c, T6050).
Notes:
agx_nir_opt_pack_narrow_mux_tree()restricts packing to power-of-two index bounds in{8, 16, 32}(iand,umod,umin,ubitfield_extract), selects contiguous vs strided packing to minimize preheader register pressure, peels redundant power-of-two masks only oniand(x, n - 1), and pre-complements packed words/constants via(255 - b) - (255 - c) == c - bwith all four(byte_is_rhs, neg_is_true)polarity cases verified.hoist_pure_readonly_loop()is strictly gated behindfits_all && speculative, requiresloop_is_pure_readonly(loop)(no calls, writes, atomics, barriers, or volatile/coherent loads), checksloop_peak_after_hoist() <= budget, and wraps Phase 2 body hoists in a zero-speculation entry guard with merge phis for live-out header phis (bailing cleanly whenafter_loophas exit phis).- Note on pre-existing stacked base
#704(902bafb17):test-agx3-vision-opts.cpp:383-386has out-of-order.format/.accessdesignators under GCC 16 C++20 (not introduced by#735);#735's own new tests use valid declaration order and compile and pass cleanly underclang 23.1.4(-Db_ndebug=false).
Evidence
- Head audited:
2db79e7e482ba5ca8502f376491cfa7a4d15f8d4(aurora-silicon/mesa#735) - Host build & unit tests on Mac Studio M5 Max (J775c, T6050):
clang 23.1.4,-Db_ndebug=false,meson test --suite asahi:146/146 PASS(0 fail). - Live Borealis Vulkan CTS on Mac Studio M5 Max (J775c, T6050),
LD_DEBUG=libsconfirmed/tmp/mesa-pr735-build/src/asahi/vulkan/libvulkan_asahi.so:dEQP-VK.compute.pipeline.basic.*80/80 PASS(1 NotSupported,0 fail),dEQP-VK.ssbo.layout.single_basic_type.std430.int*2/2 PASS(0 fail),GPU_FAULT_DELTA=0.
|
@DjDeveloperr thanks for the conditional polarity optimization. #1351 carries it into the packed byte LUT pass on current aurora-wip. The exact J615 and J700 comparisons match the CPU reference and reduce GPU time on the long-loop fixture. Short and unchanged controls are within run variation. The linked benchmark has the shader source and paired timings. |
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
Summary
Stacked on #704 (
perf/agx3b-wave4-vision-fm,60bafb40ca56). Three general compiler optimizations targeting tight loop-invariant byte-table lookups and pure read-only nested search loops on Borealis (AGX3B/AGX3C compute):isubfolding (3c2ec59c66f2):agx_nir_opt_pack_narrow_mux_treepacks power-of-two tables of 8..32 loop-invariant 8-bit SSA values selected via a boundedbcseltree into 32-bit words in the loop preheader (automatically choosing contiguous vs strided layout to minimize preheader register pressure) and replaces the byte mux tree with an(N/4)-way 32-bit select plus oneubitfield_extract.iadd(a, ineg(b))back to nativeisubafter late select fusion.1cd7812eb17e):peel_straight_prefixtocount <= 8and bottom-tested exact peels, and extendsunswitch_nestedto 3-deep nests whose middle loop is pure read-only wrapping a[0, bound)inner loop reading an outer-invariant 32-bit array, so specializingbound == 8peels the inner loop straight-line and allows the 4-word load merger to emit two32x4device loads.agx_nir_opt_licm, countspack_64_2x32_splitof uniform preamble/constant halves as 0 GPR halves and addshoist_pure_readonly_loop(budget-gated exit-dominating loads + zero-speculation guarded body loads for pure read-only loops with no writes, atomics, barriers, calls, or volatile/coherent accesses).1f2c12e70745):raw_idx & (n - 1)) before subfield extraction when strided.ineginfold_iadd_ineg_to_isuband skips unused mux roots during reverse traversal.inv_cond ? -(c - b) : (c - b)withcin[0, 255], conditionally bitwise-complements the packed words andcin the preheader using(~b & 0xff) - (255 - c) == c - b, collapsing the in-loopineg + bcselinto a singleisub.Unit tests added in
src/asahi/compiler/test/test-agx3-vision-opts.cpp:PackNarrowMuxTreeAndFoldIsub,Exact8ReadonlyNestedPeel,PureReadonlyLoopLicmHoist, andPackNarrowMuxTreePreNegateAndMaskPeel.Exact-Head Verification —
1f2c12e70745342ae6b54972387c076abdfc02b9CI, Unit Tests & 3-Chip Offline Sweep
DEFAULT_BRANCH=perf/agx3b-wave4-vision-fm bash .github/aurora/agent-policy.sh pr HEADPR head is clean)meson test --suite asahi(-Db_ndebug=false, assert-enabled)AGX3VisionOpts: 32/32 PASS)FEDORA-CI 1f2c12e70745342ae6b54972387c076abdfc02b90fail,0timeout,0skipped,ICD git-1f2c12e707)g17p,g16g,g17s-kernel, assert-enabled)Physical MacBook Neo (
Apple A18 Pro / G17P B1 / AGX3B, kernel7.3.0-41.fc44.aurora.aarch64,ICD git-1f2c12e707)No-reboot ABBA (
w2#704vsv11f2c12e70745, 20 warmed iters/round,MESA_SHADER_CACHE_DISABLE=true,PMU=30 00,FAULTS_DELTA=0,AVC_DELTA=0, pre/post GPU liveness1/1 PASS):#704Base (w2)v1)v1 / w2w2Estv1Estw2 / Mtlv1 / Mtl20.80 ms20.71 ms1.005x27.08 ms26.93 ms1.006x39.03 ms36.32 ms1.075x11.78 ms11.82 ms0.997x41.46 ms41.41 ms1.001x26.09 ms26.03 ms1.002x39.54 ms39.53 ms1.000x127.74 ms128.64 ms0.993x1.010x*Particle Physics NIR and binaries are 100% bit-identical between
w2andv1for every workgroup size (cs-5115b05f.nirwg32f5d95c91...,cs-b61b3f70.nirwg64d4fcee2b...);w2_p2(128.62 ms),v1_p1(128.65 ms), andv1_p2(128.62 ms) all ran wg64 whilew2_p1autotuned wg32 (126.85 ms).Key Feature Matching kernel wins on
G17P B1:076a1df6->3e48808b):9.07 ms(300i/73g) ->7.13 ms(309i/63g) =-1.94 ms(1.272x), dropping 10 GPRs below the 64-half tier and eliminating 3 ALU ops/iter in the inner loop.7804f45a->76d52c82):1.96 ms(188i/55g) ->1.03 ms(250i/55g) =-0.93 ms(1.90x),0extra GPRs.Correctness on Physical MacBook Neo (
v11f2c12e70745)gpubench-vkVision/Compute ABBA (16 configs)CRC_OKdEQP-VK.glsl.loops.*0fail,0recoveries)ssbomustpass63NotSupported,0fail,0recoveries)dEQP-VK.robustness.robustness2.*41,948NotSupported,0fail,0recoveries)PMU=30 00,FAULTS_DELTA=0,AVC_DELTA=0