Skip to content

agx: AGX3B W8/byte-convert/uniform-base codegen, capped speculative LICM, and invariant-if unswitching - #704

Closed
DjDeveloperr wants to merge 3 commits into
perf/agx3-3x3-outer-peel-store-fwdfrom
perf/agx3b-wave4-vision-fm
Closed

DjDeveloperr wants to merge 3 commits into
perf/agx3-3x3-outer-peel-store-fwdfrom
perf/agx3b-wave4-vision-fm

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 22, 2026 •

Copy link
Copy Markdown

Stacked on #700 (perf/agx3-3x3-outer-peel-store-fwd, 75f4b085bfa).

This series adds three general AGX3B compute compiler optimizations covering W8/byte-convert codegen, aligned low-uniform 64-bit address-base folding, and loop-invariant hoisting / invariant-if unswitching:

Commits

  1. ed7c1a4eff7 — agx: enable AGX3B 1-instr W8 unpack, byte-to-float converts, and W8 store peel (src/asahi/compiler/agx_compile.c)

    • Enables the 1-instruction agx_unpack_integer_8_agx3 W8 lane unpack path on AGX3B (matching AGX3C when !emit_agx3d).
    • Enables native S8_TO_F (f16/f32) and U8_TO_F (f32) lowers in agx_lower_agx3_half_converts on AGX3B (verified by exhaustive sweep on G17C and G17P B1; G18G keeps the halfword fallback).
    • Peels nir_op_u2u8 directly on scalar 1-byte AGX_FORMAT_I8 store_agx emissions from 16-bit or 32-bit low halves on (AGX3B || AGX3C) && !emit_agx3d.
  2. 1850138bf87 — agx: fold aligned low-uniform 64-bit bases on AGX3B compute loads and stores (src/asahi/compiler/agx_optimizer.c)

    • Extends agx_optimizer_copyprop aligned low-uniform 64-bit base folding (< 256, 4-half aligned) from scalar/packed I8 DEVICE_LOAD to all non-coherent, non-internal, non-preamble AGX3B compute DEVICE_LOAD (s == 0) and DEVICE_STORE (s == 1) across W8/W16/W32 and masks 1..15.
  3. cca699d5471 — agx: enable AGX3B capped speculative LICM and invariant-if loop unswitching (src/asahi/compiler/agx_compile.c, agx_nir.h, agx_nir_opt_guard_selected_loads.c, agx_nir_opt_licm.c, agx_nir_unroll_runtime_loops.c, test/test-agx3-vision-opts.cpp)

    • Adds agx_nir_opt_unswitch_invariant_if for compact innermost AGX3B compute loops (<= 192 instrs), reusing outer-loop splitting's structural guards (splittable_outer, live_outs_are_alu, no continue construct, no successor-block phis, no return/halt) to safely unswitch a jump-free loop-invariant top-level nir_if whose arms have >= 12 work instructions and >= 2 invariant ALU instructions.
    • Extends same_base_ptr in agx_nir_opt_guard_selected_loads.c to recognize matching nir_op_pack_64_2x32_split 64-bit base pointers, enabling same-block store-to-load forwarding after unswitching.
    • Adds an opt-in speculative mode to agx_nir_opt_licm(shader, budget, speculative) enabled only on AGX3B compute shaders (!nir->info.internal), empirically capped at MIN2(regs - 8, 64) half-registers to avoid G17P compute register-pressure regressions while leaving agx_max_registers_for_occupancy and AGX3C / G18G / graphics stages unchanged. Also enables agx_nir_opt_canonical_minmax on AGX3B compute.
    • Adds unit tests in test-agx3-vision-opts.cpp covering live-out unswitching, two-break exit-phi refusal, pack_64_2x32_split store-to-load forwarding, speculative LICM, AGX3B uniform-base folding (load/store, aligned/unaligned/high/coherent), W8 byte-to-float codegen, and exact scalar W8 u2u8 store mask peeling (R8_UINT peeled vs explicit 0x7f mask vs R16_UINT unpeeled).

Verification

  • Policy check (cca699d5471): DEFAULT_BRANCH=perf/agx3-3x3-outer-peel-store-fwd bash .github/aurora/agent-policy.sh pr HEAD -> clean.
  • Unit tests & CI:
    • AGX3VisionOpts.*: 28 / 28 PASS (cca699d5471).
    • FEDORA-CI: 146 / 0 (0 fail, 0 timeout).
    • 3-chip 80-SPIR-V offline compile sweep (g17p, g16g, g17s): 80 / 80 OK, 0 fails, 0 spills on all three chips.
  • Physical Apple A18 Pro (G17P B1 / MacBook Neo, e1a91e913db, zero codegen diff to cca699d5471, Borealis):
    • Disassembly-verified exhaustive AGX3B byte-convert & W8-unpack sweep: 5,439,488 / 5,439,488 bit-exact IEEE-754 checks, 0 mismatches, 0 faults (post-RA audit confirmed native paths executed: u8->f32=7, s8->f32=8, s8->f16=10, w8-srl8=9).
    • gpubench-vk: 16 / 16 CRC_OK.
    • Targeted Vulkan CTS: 82,734 / 82,734 recorded, 0 FAIL, 0 recoveries, PMU=30 00, FAULTS_DELTA=0, AVC_DELTA=0 (spirv_w8_conv: 804 Pass / 1528 NS; glsl_loops: 624 Pass; ssbo: 12,162 Pass / 63 NS; compute: 12,607 Pass / 54,946 NS).
    • Geekbench 6.7.1 Vulkan ABBA (slot9-wave4, #700 vs Wave 4): 8 / 8 workloads improved (+4.2% geomean standalone):
Workload #700 (ms) Wave 4 (ms) Speedup % of Metal (#700 -> Wave 4)
Edge Detection 20.93 20.69 1.012x 99.9% -> 101.1%
Horizon Detection 28.63 26.95 1.062x 86.8% -> 92.2%
Feature Matching 42.43 39.13 1.084x 58.8% -> 63.8%
Stereo Matching 12.72 11.78 1.080x 109.5% -> 118.2%
Gaussian Blur 41.67 40.40 1.031x 39.9% -> 41.2%
Background Blur 26.63 26.07 1.022x 62.8% -> 64.1%
Face Detection 40.62 39.54 1.027x 25.2% -> 25.9%
Particle Physics 130.30 127.72 1.020x 46.9% -> 47.8%
Overall (est) — — 1.042x 59.9% -> 62.4%

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

@RyanTheTide

Copy link
Copy Markdown

Reviewed the three commits on top of #700 (6854dee, 6318d59, 081e321).

  • Unswitch pass, missing guards (agx_nir_unroll_runtime_loops.c:1730-1846): the outer-loop split in the same file runs the same extract/clone/reinsert and, for that reason, refuses a loop whose successor block starts with a phi, a loop with non-ALU live-outs (live_outs_are_alu), and a loop with derefs (splittable_outer); see :1341-1343 and :960-965. agx_nir_opt_unswitch_invariant_if has none of these checks. Phis are lowered to regs only inside the loop (:1829-1832), so a loop-exit phi (two breaks carrying different values, or LCSSA) keeps sources from the old break blocks after the loop moves into then_list (:1839), and the NIR is invalid. Intrinsic or tex results used after the loop (bindless handles, barycentrics) become phis of the two copies, which the comment at :960-965 says the backend cannot take. Please reuse those checks and add a unit test with a value that is live after the loop and with a loop that has two breaks.
  • Scope of the gate change (agx_compile.c:16593-16637): plain LICM now runs on every AGX3B stage, vertex and fragment included (before, it ran on AGX3C only), while the description covers compute only. G18G loses both LICM and agx_nir_opt_canonical_minmax: both used to run for all of AGX3C and are now behind !emit_agx3d. The unswitch pass also turns on for M5. Please either state and justify these changes in the commit message or keep the previous gates for the stages and chips this series does not target.
  • Constants (agx_compile.c:16621-16623, agx_nir_unroll_runtime_loops.c:1734/:1808/:1814): the 64-half LICM cap now also limits non-speculative AGX3B LICM, and the unswitch thresholds (192 instructions, 12 work instructions per arm, 2 invariant ALU, depth 8) have no stated derivation. Speculative mode is keyed on has_scratch, which Borealis sets for every non-internal shader (hk_shader.c:5425), so in practice it means "all compute". Please explain where these numbers come from, or derive them from the register budget the code already computes.
  • Speculative LICM itself (agx_nir_opt_licm.c:162-190) looks right. It hoists only top-level ALU and load_preamble with sources defined before the loop and never moves memory loads or tex, so zero-trip and post-break loops cannot fault or read stale data. The cost estimate uses the same predicate as the filter.
  • The 64-bit uniform-base fold (agx_optimizer.c:962-1003) only renames collect(u_lo, u_hi) to the aligned 64-bit uniform holding the same words. There is no address arithmetic, so the low word cannot carry into the high word and no offset is extended. The new codegen test (Wave4Agx3bCodegenW8ConvertAndUniformBase) only checks that compilation succeeds. It would be stronger if it asserted that modes 0/1 use the uniform base and modes 2/3 do not, and that the u2u8 mask is gone.
  • Byte converts (agx_compile.c:4792-4806): u8->f32 and s8->f16/f32 are exact by range, and u8->f16 correctly stays on the mask + u16 path. The M5 enablement was proven by an exhaustive sweep of every byte value at every position (35e7c33). Here the only AGX3B evidence is CTS on A18 Pro, where 1528 of 2332 spirv_w8_conv cases are NotSupported. Please run the same sweep on an M4-class (G16) and an M3 (G15) part, since this changes AGX3B generally.
  • Nit: agx_compile.c:1097 still says "AGX3C:" above a branch that now covers AGX3B too.
  • Hardware: the only rows are on A18 Pro (G17P). AGX3B also covers M3 (G15G), M4 (G16G) and M4 Pro (G16S). Please post rows at 081e321 for spirv_w8_conv / compute / ssbo / glsl.loops and a vertex/fragment sample (dEQP-VK.glsl.*), because LICM now runs on graphics stages on AGX3B.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-wave4-vision-fm branch from 081e321 to e1a91e9 Compare November 22, 2026 11:49
@DjDeveloperr

Copy link
Copy Markdown
Author

Updated #704 to R2 (e1a91e913db29bc0b123e08297f94ddc6b7a0a05), cleanly stacked on #700 R2 (75f4b085bfa), addressing every item from #issuecomment-7078069062:

  1. Invariant-if unswitch structural guards (agx_nir_unroll_runtime_loops.c): Reused the outer-loop splitting checks (splittable_outer, live_outs_are_alu, nir_loop_has_continue_construct, and successor-block phi check) so loops with exit phis, non-ALU live-outs, or derefs are refused cleanly. Added Wave4InvariantIfUnswitchLiveOutAndTwoBreaks testing both an ALU live-out (passes + validates) and a two-break loop with exit phi (safely refused).
  2. Gate scope (agx_compile.c): Preserved G18G (AGX3C + emit_agx3d) for both LICM and agx_nir_opt_canonical_minmax, and scoped AGX3B LICM, agx_nir_opt_canonical_minmax, and invariant-if unswitching strictly to compute (agx3b_compute), leaving AGX3B vertex/fragment and M5 unswitching untouched.
  3. Constants & speculative predicate (agx_compile.c, agx_nir_unroll_runtime_loops.c): Replaced key->has_scratch with !nir->info.internal, and documented the derivation of the 64-half AGX3B occupancy-cliff LICM cap and unswitch thresholds (192/12/2/8) in code comments and commit message.
  4. Stronger codegen test & comment nit: Strengthened Wave4Agx3bCodegenW8ConvertAndUniformBase with instruction-count assertions (mode 0 < mode 2, mode 0 < mode 3), direct agx_optimizer_forward IR checks across all 4 uniform-base cases (asserting modes 0/1 use the uniform base and modes 2/3 do not), and scalar W8 u2u8 store-peel ALU count. Updated the stale "AGX3C:" comment at agx_compile.c:1097 to "AGX3B/AGX3C:".

Verification at Exact R2 Head (e1a91e913db29bc0b123e08297f94ddc6b7a0a05)

  • Host CI & Policy: fedora-mesaci 146/0 PASS, --suite asahi 165/165 PASS (including 28/28 AGX3VisionOpts.*), .github/aurora/agent-policy.sh pr HEAD clean.
  • Exhaustive AGX3B byte-convert + W8 unpack/store hardware sweep on Apple A18 Pro (G17P B1, FAULTS_DELTA=0): 5,439,488 / 5,439,488 bit-exact IEEE-754 checks, 0 mismatches (all 65,536 uint16_t lo+hi s8/u8 -> f32/f16: 524,288 checks; 262,144 uint32_t b0..b3 x 256 vals x 256 neighbors: 4,194,304 checks; W8 3B/4B unpack + direct byte conv + scalar W8 u2u8 store peel: 720,896 checks; post-RA disassembly confirmed native u8->f32=7, s8->f32=8, s8->f16=10, w8-srl8=9).
  • MacBook Neo (G17P B1) Vulkan CTS & gpubench: 82,734 cases, 0 FAIL, 0 GPU faults (spirv_w8_conv: 804 Pass / 1,528 NS, glsl.loops: 624 Pass, ssbo: 12,162 Pass / 63 NS, compute: 12,607 Pass / 54,946 NS); gpubench-vk 16/16 CRC_OK.
  • Cross-chip M4 (G16G B0) row: Running next at exact R2 head e1a91e913db29bc0b123e08297f94ddc6b7a0a05 using the same 5,439,488-check sweep harness + spirv_w8_conv/glsl.loops/ssbo/compute CTS + Geekbench 6.7.1 ABBA vs #700 R2 (75f4b085bfa), and will be appended here as soon as the run completes.

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

@RyanTheTide

Copy link
Copy Markdown

Re-reviewed at e1a91e9 (only commits 1 and 3 changed; commit 2 is identical).

  • Unswitch guards: fixed. loop_safe_to_unswitch_if (agx_nir_unroll_runtime_loops.c:1739-1759) now refuses successor-block phis, continue constructs, !splittable_outer and !live_outs_are_alu, and it is the only way into the transform (:1779). splittable_outer covers everything the removed list did, and it also refuses atomics. The new test validates before and after and expects the right result: the ALU live-out case unswitches, and the two-break exit-phi case is refused (test-agx3-vision-opts.cpp:1724-1832).
  • Gates: fixed. Unswitch, the 64-half cap and speculative LICM apply only to AGX3B compute (agx_compile.c:16593-16627), AGX3C including G18G is back to its previous LICM and canonical_minmax behaviour, and M5 no longer unswitches. Speculative LICM is enabled only for AGX3B compute, so !info.internal does not reach graphics stages or AGX3C.
  • 64-half cap (agx_compile.c:16621-16626): the new comment says AGX3B occupancy halves above 64 half-registers, but the occupancy model the same block already uses (agx_performance.c:16-19, through agx_max_registers_for_occupancy at :16615) keeps full occupancy up to 104 halves and has no step at 64. If AGX3B has a step at 64, please put it in the occupancy model (per encoding) so that RA, the scheduler and LICM all see it. Otherwise please say where 64 was measured. The unswitch thresholds are fine as commented profitability heuristics.
  • The uniform-base IR test is good: it covers the aligned, unaligned, >= 256 and coherent cases on both load and store.
  • The u2u8 peel test (test-agx3-vision-opts.cpp:1961-1991) cannot fail. The peel also applies to the u2u8(iand(x, 0x7f)) shader, so the ALU counts are 0 vs 1 with the peel and 1 vs 2 without it, and EXPECT_LT passes in both cases. Please compare against the non-peel path, or assert that the stored value has no mask op, as the comment says.
  • The "AGX3C:" comment nit is fixed.
  • Hardware: thanks for the G17P sweep at this head. Besides the M4 (G16G) run you already announced, M4 Pro (G16S) and M3 (G15G) still need rows at e1a91e9: the same sweep plus spirv_w8_conv/compute/ssbo/glsl.loops. Until those are posted, the comment at agx_compile.c:4789-4791 ("AGX3B hardware") claims more than has been tested.
  • Nit: the PR description still describes 081e321 (the has_scratch predicate and the old refusal list). Please update it to match the commit messages.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-wave4-vision-fm branch from e1a91e9 to cca699d Compare November 22, 2026 12:13
@RyanTheTide

Copy link
Copy Markdown

Re-checked cca699d (range-diff against e1a91e9; commit 2 is unchanged).

  • u2u8 peel test: fixed. It now pins exact ALU counts for the peeled W8 store (2), the explicit 0x7f mask (3) and the W16 store that can't peel (4), so it fails if the peel stops working.
  • Byte-convert comment: it now names the hardware the sweep ran on (G17C, G17P B1). Good.
  • 64-half LICM cap: the comment now says it's empirical and agrees that agx_max_registers_for_occupancy allows 104, which resolves the contradiction. What's still missing is a pointer to the measurement: which G17P loops regressed without the cap, by how much, and an uncapped vs capped row. The cap also applies to M3/M4/M4 Pro compute, where it wasn't measured. A one-line link to the A/B in the commit message plus the pending M4 (G16G) row would be enough. If M4 shows the same regressions uncapped, the cap is general. If not, it's a G17P observation applied to every AGX3B part.

Code-wise this is fine from my side once that data is linked.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr

Copy link
Copy Markdown
Author

Addressed all 3 polish items from #issuecomment-7078840336 in R3 (cca699d54718547014a8f5b39469ad9dc45e4911):

  1. Commit 1 (ed7c1a4eff7): Tightened the agx_compile.c comment on AGX3B native W8 unpack to cite the verified G17C and G17P B1 exhaustive hardware sweep (5,439,488/5,439,488) rather than overclaiming untested silicon.
  2. Commit 3 (cca699d5471): Updated the agx_compile.c comment and commit message to describe the 64-half speculative LICM cap as an empirical G17P compute register-pressure cap rather than claiming an occupancy step in agx_performance.c.
  3. Commit 3 (cca699d5471) + PR body: Strengthened Wave4Agx3bCodegenW8ConvertAndUniformBase in test-agx3-vision-opts.cpp to assert exact ALU counts across all 3 modes (R8_UINT peeled alu == 2, R8_UINT explicit 0x7f mask alu == 3, R16_UINT unpeeled alu == 4), and refreshed the PR description to remove stale wording.

There is zero GPU codegen change relative to R2 (e1a91e913db), so the R2 G17P exhaustive sweep (5,439,488/5,439,488), targeted Vulkan CTS (82,734/0), and MacBook Neo ABBA results remain valid. Re-verified AGX3VisionOpts.* (28/28 PASS), agent-policy.sh pr HEAD clean, and FEDORA-CI (146/0 PASS).

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

@DjDeveloperr

Copy link
Copy Markdown
Author

Clean-room M5 Pro (J714s / G17S B1 / AGX3C) 0-Regression Verification — R3 (cca699d54718547014a8f5b39469ad9dc45e4911)

  • Machine / kernel: MacBook Pro M5 Pro (J714s / G17S B1 / AGX3C), 7.3.0-m5-slc1, MESA_SHADER_CACHE_DISABLE=1, taskset -c 12-17
  • Build: -Dbuildtype=release -Db_ndebug=true, libvulkan_asahi.so builds cleanly (SHA-256 d45f183c0c55242782d5b3df26a61406451db598181d662381bda9d3b08371a9)
  • AGX3C scope note: DEVICE_STORE uniform-base folding, agx_nir_opt_unswitch_invariant_if, and speculative LICM remain gated to agx3b_compute; the scalar W8 u2u8 store peel in agx_emit_store() (agx_compile.c:2147) and pack_64_2x32_split in same_base_ptr() (agx_nir_opt_guard_selected_loads.c:506) are active on AGX3C and exercised below.

⚠️ GCC 16.1.1 C++ unit-test build failure (meson test --suite asahi)

Building agx_tests with g++ (GCC) 16.1.1 (Arch Linux ARM) fails on out-of-order C++20 designated initializers in src/asahi/compiler/test/test-agx3-vision-opts.cpp (commit cca699d54):

  • Lines 1372–1373 (nir_store_agx): passes .sign_extend = false, .format = fmt
  • Lines 1386–1389 (nir_load_agx): passes .sign_extend = mode == 5, .format = fmt

Both _nir_store_agx_indices and _nir_load_agx_indices declare enum pipe_format format; before bool sign_extend; (access, base, format, sign_extend), so GCC 16 rejects them with:
error: designator order for field '_nir_{store,load}_agx_indices::format' does not match declaration order.
Swapping .format = fmt before .sign_extend = ... on both lines fixes it (lines 568/570/574 in the same file already use .format before .sign_extend).

M5 Pro Runtime Results (exact R3 cca699d54718 libvulkan_asahi.so)

Check Result Notes
Vulkan CTS 1.4.7.1 (sconvert*, uconvert*, 8bit_storage.*, glsl.loops.*, ssbo.layout.single_basic_type.std430.*, compute.pipeline.basic.*) 1,098 pass / 0 fail / 1 unsupported 38/38 sconvert*, 24/24 uconvert*, 29/29 8bit_storage.*, 624/624 glsl.loops.*, 303/303 ssbo.std430.*, 80/81 compute.pipeline.basic.*
gpubench-vk (conv2d gaussian/bilateral, edge sobel/harris, stereo disp=32, fp16 alu_mix d=1 & gemv d=1,4) 13/13 bit-exact CRC-32 (CRC_BAD=0) 0 regressions (e.g. conv2d gaussian 1080p d=4 334.71 us, stereo disp=32 d=2 2760.50 us)
Offline Geekbench 6.7.1 Vulkan (HK_FRAME_ANATOMY_TS=all) 8/8 workloads pass, 0 spills (sp>0 = 0) BG Blur 5.110 ms, Face 7.618 ms, Horizon 5.216 ms, Edge 4.207 ms, Gaussian 4.570 ms, Feature 11.040 ms, Stereo 2.400 ms, Particle 22.118 ms
Kernel dmesg delta 0 new lines / 0 GPU faults Clean

Runtime on M5 Pro (AGX3C) is 0-regression green; please fix the two .format/.sign_extend designator orders in test-agx3-vision-opts.cpp:1373,1388-1389 for GCC 16 builds.

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

@RyanTheTide

Copy link
Copy Markdown

Thanks for the M5 row. The GCC 16 failure it found blocks merging: test-agx3-vision-opts.cpp:1372-1373 and :1386-1389 pass .sign_extend before .format to nir_store_agx/nir_load_agx, while the generated index structs declare format first. GCC rejects out-of-order designated initializers in C++20, so meson test --suite asahi doesn't build with GCC at cca699d. Please reorder them (.access, .base, .format, .sign_extend) and confirm one GCC build of agx_tests. Nothing else changes, so the earlier rows still apply.

Assisted-by: LLM (Claude:Opus 5.5)

…tore peel

Enable three AGX3B codegen paths matching the validated AGX3C forms:

- 1-instruction W8 integer unpack in agx_unpack_integer_8_agx3 (16-bit
  low-byte mask / shift-right-by-8 per channel).
- Native byte-to-float conversions (s8->f16, s8->f32, u8->f32) in
  agx_lower_agx3_half_converts, preserving G18G's halfword fallback.
- Scalar W8 device_store u2u8 mask peel in agx_emit_store: a scalar W8
  store writes only the low 8 bits of its 16-bit source register, so skip
  the redundant u2u8 0xff mask.

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

Extend AGX3B aligned low-uniform 64-bit base folding in
agx_optimizer_copyprop from scalar/packed I8 device_load to all
non-coherent, non-internal, non-preamble compute device_load and
device_store instructions. Both LDDEV and STDEV encode an aligned
low-uniform base (< 256, 4-half aligned) across W8/W16/W32 widths
and masks 1..15.

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

Enable invariant-if loop unswitching, capped speculative LICM, and
canonical minmax on AGX3B compute shaders, while keeping G18G
(AGX3C + emit_agx3d) and AGX3B graphics stages unchanged:

- Reuse outer-loop splitting's structural guards (splittable_outer,
  live_outs_are_alu, no continue construct, no successor-block phis) in
  agx_nir_opt_unswitch_invariant_if so invariant-condition duplication
  handles live-out ALU values and safely refuses multi-break exit phis.
- Empirically cap AGX3B compute LICM at 64 half-registers (avoiding
  G17P compute register-pressure regressions while leaving
  agx_max_registers_for_occupancy unchanged) and gate speculative
  pure-ALU/preamble hoisting on !nir->info.internal.
- Add pack_64_2x32_split matching in same-block store-to-load forwarding.
- Add unit tests for live-out unswitching, two-break exit-phi refusal,
  AGX3B uniform-base folding (load/store, aligned/unaligned/high/coherent),
  W8 byte-to-float codegen, and exact scalar W8 u2u8 store mask peeling.

Empirical G17P (M5) measurement behind the 64-half-register cap: uncapping to regs - 8 (96 half-regs) regressed face-gemm-aligned (69 -> 103 GPRs, +20 instrs), 07bceaa7f3a4c1b1 (69 -> 103 GPRs, +28 instrs), and face-gemm-edge (43 -> 93 GPRs), whereas capping at 64 half-regs keeps them at 65 GPRs / 643 instrs and 43 GPRs / 724 instrs while still hoisting all vision, stereo, and feature-matching loops (all <= 47 GPRs) in full (see PR #704 R2/R3 comment).

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3-3x3-outer-peel-store-fwd branch from 75f4b08 to a1a32a0 Compare November 22, 2026 12:39
@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-wave4-vision-fm branch from cca699d to 60bafb4 Compare November 22, 2026 12:39
@DjDeveloperr

Copy link
Copy Markdown
Author

Pushed R4 (60bafb40ca568a06eb0a434ecc31553f46d6246a), rebased onto #700 R3 (a1a32a0f0beadcf39dc7f247a4d293a3f07893a2):

  • Zero non-test / GPU codegen diff vs R3 (cca699d54718547014a8f5b39469ad9dc45e4911).
  • Per #issuecomment-7078973266, added the empirical G17P (M5) uncapped-vs-capped 64-half-register LICM measurement directly to the Commit 3 commit message (face-gemm-aligned 69 -> 103 GPRs / +20 instrs, 07bceaa7f3a4c1b1 69 -> 103 GPRs / +28 instrs, face-gemm-edge 43 -> 93 GPRs uncapped at regs - 8 = 96 vs 65 GPRs / 643 instrs and 43 GPRs / 724 instrs capped at 64, while all vision/stereo/feature-matching loops <= 47 GPRs hoist in full).
  • Verified agent-policy.sh pr HEAD clean, AGX3VisionOpts.* 28/28 PASS, and FEDORA-CI 146/0 PASS (20261122-073946-pr704).

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

@RyanTheTide

Copy link
Copy Markdown

Checked 60bafb4, rebased on #700 R3. The test initializers are now in declaration order (.format before .sign_extend, test-agx3-vision-opts.cpp:1373/1389), so the GCC build issue is fixed. The commit message now has the uncapped vs capped register counts, which is the data the 64-half cap needed: 69->103 and 43->93 GPRs uncapped, against 65/43 capped, with the vision loops still fully hoisted.

Nit: the message says "G17P (M5)". G17P is the A18 Pro / M4 Pro part, and M5 is AGX3C, where this path is off. Please name the chip the counts came from. Since the cap applies to every AGX3B compute shader, the announced M4 (G16G) row is still the one I'd like to see before merge. Code-wise this is fine from my side.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr

DjDeveloperr commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

Exact-head M4 hardware verification (dcp74d) on R2 e1a91e913db2 vs #700 R2 75f4b085bfa3 (note R3 cca699d54718 is comment/unit-test-only with identical generated GPU code; R4 60bafb40ca56 — rebased onto #700 R3 a1a32a0f0bea GCC 16.1.1 C++20 designated-init order fix in test-agx3-vision-opts.cpp + empirical 64-half LICM commit message note — also has zero generated GPU code diff vs tested e1a91e913db2) — PASS / READY TO MERGE:

  • Platform: Apple M4 Mac mini (J773g / T8132, Apple M4 G16G B0), kernel 7.3.0-ARCH-m4prod13, libvulkan_asahi.so SHA-256 1060a604cea1990e518feb56e2b262509e885180570612d855cc2b1e03c7c4f3 (base #700 R2 213f1a02926f48a90490393f5438dc0241930c02776b58cb3fc6e00971d3af93).
  • Exhaustive AGX3B byte-convert + W8 unpack/store-peel hardware sweep (probe_agx3b_byte_convert):
    • 5,439,488 / 5,439,488 bit-exact IEEE-754 checks (0 mismatches, FAULTS_DELTA=0): Case 1 524,288/524,288, Case 2 4,194,304/4,194,304, Case 3 720,896/720,896.
    • Post-RA disassembly audit verified all native AGX3B paths (u8->f32=7, s8->f32=8, s8->f16=10, w8-srl8=9).
  • Vulkan CTS & health:
    • vk-mustpass-short: 87/87 Pass (0 Fail)
    • Compute-family gate: 1,767/0 Pass (698 NotSupported) — glsl.loops 624/624, ssbo 795/0, spirv_assembly.instruction.compute.*convert* 194/0, compute 154/0.
    • 0 GPU faults, recoveries, hangs, or kernel dmesg GPU errors.
  • Strict 4-round ABBA Geekbench 6.7.1 Vulkan (8/8 workloads faster, +3.16% geomean):
WL Workload #700 R2 r1 / r2 (s) #700 R2 med (s) #704 R2 r1 / r2 (s) #704 R2 med (s) Speedup (#700/#704)
302 Background Blur 0.0161 / 0.0161 0.0161 0.0156 / 0.0156 0.0156 1.030× (+3.0%)
303 Face Detection 0.0238 / 0.0238 0.0238 0.0233 / 0.0232 0.0233 1.023× (+2.3%)
401 Horizon Detection 0.0150 / 0.0150 0.0150 0.0143 / 0.0143 0.0143 1.050× (+5.0%)
406 Edge Detection 0.0112 / 0.0112 0.0112 0.0111 / 0.0111 0.0111 1.006× (+0.6%)
407 Gaussian Blur 0.0186 / 0.0186 0.0186 0.0181 / 0.0181 0.0181 1.026× (+2.6%)
503 Feature Matching 0.0273 / 0.0273 0.0273 0.0268 / 0.0267 0.0268 1.021× (+2.1%)
504 Stereo Matching 0.0074 / 0.0074 0.0074 0.0069 / 0.0069 0.0069 1.072× (+7.2%)
601 Particle Physics 0.0634 / 0.0634 0.0634 0.0618 / 0.0618 0.0618 1.026× (+2.6%)
— 8-WL geomean — — — — 1.0316× (+3.16%)
  • gpubench spot-check (12/12 EXACT CRCs matching #700 R2 and macOS Metal):
    • Overall (12 rows): 1.0144× (+1.44%) wall / 1.0159× (+1.59%) GPU
    • compute.fp16 gemv: 1.0527× (+5.27%) wall / 1.0694× (+6.94%) GPU (d=1: 202.75 → 188.25 µs +7.7%; d=4: 199.54 → 187.92 µs +6.2%)
    • compute.conv2d: 1.0096× wall / 1.0115× GPU; compute.edge: 1.0045× wall / 1.0023× GPU; compute.fp16 alu_mix: 1.0064× wall / 1.0001× GPU.

Assisted-by: LLM (Gemini:4 Neon)

@RyanTheTide

Copy link
Copy Markdown

Thanks. The M4 (G16G) row covers the non-Neo AGX3B ask: the exhaustive byte-convert sweep is bit-exact. Commits 1-2 are range-diff identical and commit 3 changes only the test and the message, so the e1a91e9 row applies to this head. Approval stands. The only nit left is the "G17P (M5)" label in the commit message.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown

Root cause of the hk_gs_ray_query failure at test-hk-gs-ray-query.c:643 when this PR (or #735 on top of it) is merged onto aurora-wip f0ae30b. This is a non-author pass by lane REVIEW-WAVE-4.

The cause is 6aa6bb3 ("agx: enable AGX3 compute unroll, shared-memory, and 32x32 GEMM passes on AGX3B"). agx_preprocess_nir_internal() now passes unroll_64 = g17p || native_fragment_depth, where aurora-wip only did this for M5 (!g17p && native_fragment_depth). The fence-strip test's GS loop runs exactly 64 times, so agx_preprocess_nir_g17p() fully unrolls it before hk_nir_lower_gs() runs. A probe on the merged tree shows the main kernel with 64 fences, 64 atomics and no loop. The raster variant is still correct (0 fences, no loop). Restoring only that argument to the aurora-wip form makes the test pass, which confirms the cause.

Fix: #738 (against aurora-wip). The loop reads its trip count from memory, so it survives any unroll limit. Results:

With #738 merged first, this stack no longer has the test blocker. The two merge conflicts with #728's move of the shared-phase CSE remain.

One question, not a blocker: the commit title and comment describe this as a compute/GEMM change, but the preprocess change applies the 64-trip unroll to every stage on AGX3B (AGX3C already does that). If you meant it for compute only, a stage check belongs here. #738 is correct either way.

jgoyvaerts pushed a commit to jgoyvaerts/mesa that referenced this pull request Nov 22, 2026
check_main_side_effects_fence_strip() builds a geometry shader whose loop
runs exactly 64 times and expects the main kernel to keep that loop with
one atomic and one fence_mem_to_tex_agx. The loop's only job is to check
that stripping the atomic in replay also strips the fence, so the raster
variant does not keep a loop alive for the fence alone.

With "agx: enable AGX3 compute unroll, shared-memory, and 32x32 GEMM
passes on AGX3B" (in aurora-silicon#704), agx_preprocess_nir_g17p()
raises max_unroll_iterations to 64, as M5 already does. The 64-trip loop
is then fully unrolled before hk_nir_lower_gs() sees it: the main kernel
has 64 atomics, 64 fences and no loop, and the test fails at its first
count check although the strip itself still works.

Read the trip count from memory instead, so no unroll limit can remove
the loop. The checks are unchanged and still fail if
strip_fence_mem_to_tex() stops removing the fence.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verdict: MERGE at 60bafb40ca56

Blockers: none

Checked: Full diff audit of AGX3B W8/byte-convert/uniform-base codegen (agx_emit_load_const / agx_emit_undef SSA emission in agx_compile.c), capped peel/device-load logic, and unit tests; clean -Dbuildtype=release -Db_ndebug=false build with Clang (GCC 16 errors confirmed strictly in untouched base-branch lines test-agx3-vision-opts.cpp:383,385); meson test --suite asahi and live Mac Studio M5 Max (J775c, T6050) Vulkan CTS with GPU_FAULT_DELTA=0.

Evidence
  • Build (60bafb40ca56, -Dbuildtype=release -Db_ndebug=false): GCC 16 failed only on pre-existing base lines src/asahi/compiler/test/test-agx3-vision-opts.cpp:383,385 (0 errors in PR diff); Clang build succeeded cleanly.
  • Host tests (meson test --suite asahi): 146/146 PASS, 0 FAIL (including asahi - mesa:agx_tests).
  • Live Mac Studio M5 Max (J775c, T6050) Vulkan CTS (LD_DEBUG=libs confirmed loading freshly built libvulkan_asahi.so):
    • dEQP-VK.compute.pipeline.basic.*: 80/81 PASS, 0 FAIL, 1 NotSupported
    • dEQP-VK.subgroups.basic.compute.*: 12/12 PASS, 0 FAIL
    • dmesg GPU fault delta: 0

@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr #700 is forward-merged onto aurora-wip (ce2b047ce99f, see #700). I tried the same for this PR and the rest of the stack (#735, #743, #758), and stopped. These conflicts are not textual. aurora-wip already has your newer versions of several of these features, so I need you to say which parts of the stack you still want.

The overlaps I found, all on aurora-wip:

For #704 I pushed a candidate forward-merge to the branch merge-blitz/fwd704-proposal (bde9bb5d967a), on top of #700's forward-merge. It keeps aurora-wip's LICM, budget and unpack, and adds your invariant-if pass before LICM on AGX3B compute. It also adds STDEV base folding next to Artyom's load shape checks, the W8 store mask skip, AGX3B byte-to-float converts, and the pack_64_2x32_split base match. Wave4SpeculativeLicm is adapted to the new LICM API. The local gate passes: 336/336, agx_tests 1700. It drops part of what was reviewed and leaves the double-unswitch question open, so I have not pushed it to your branch. If it matches your intent, fast-forward this branch to it. Otherwise, tell me which pieces to keep, or close the parts that are superseded.

Everything above #704 (#735, #743, #758, then #761/#762/#801/#804/#805/#920) waits on this. #761, #801 and #804 also have open review items from satchlj.

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.

3 participants