Skip to content

agx: enable load flattening on AGX3B, fold post-DMA selects (AGX3B/M5), and flatten tile-load bursts - #761

Closed
DjDeveloperr wants to merge 2 commits into
perf/agx3b-wave7-blur-fm-deepfrom
perf/agx3b-flatten-tile-load-bursts
Closed

DjDeveloperr wants to merge 2 commits into
perf/agx3b-wave7-blur-fm-deepfrom
perf/agx3b-flatten-tile-load-bursts

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 23, 2026 •

Copy link
Copy Markdown

What

This PR is stacked on #758, which is stacked on #743 and #735. It has two commits:

  1. agx: enable load flattening on AGX3B and fold post-DMA selects. This runs agx_nir_opt_flatten_loads on AGX3B and AGX3C (not AGX3D or AGX2). It marks the binary-tail chunk ifs from agx_nir_unroll_runtime_loops as don't-flatten, so the uniform 4/2/1 tail cascades are not flattened into dummy-page loads. When there is no preamble, it runs the standard nir_opt_peephole_select and cleanup after agx3_nir_lower_uniform_dma.
  2. agx: sink common store_shared and flatten tile-load bursts. If both arms of a single-block if/else end in a matching store_shared with an equivalent pure-ALU offset, the two stores become one store of a phi after the if. Volatile and atomic stores are refused. Bursts of four or more sibling small single-load divergent ifs are flattened, with a per-arm cap of 24. UBO loads stay out so uniform DMA keeps them.

Rebase note for #757: it also edits the agx3_nir_lower_uniform_dma block in agx_compile.c. Whichever PR merges second should feed the peephole_select progress into post_opt_progress and keep the nir->options = original_options restore.

Tested

Head 8e2d12181e181ef9b001adab30581eaa4b33d35a. The only change from R3 f9f69ac793a9 is the designator order in the two nir_store_shared calls in FlattenLoadsKeeps.StoreSharedMismatchedOrVolatileNotSunk, from the review. The driver sources are identical to R3, so the R3 hardware rows still apply:

With GCC 16.1.1, test-flatten-loads.cpp now compiles. At R3 it failed with the reported designator-order error. Clang 23 -Werror no longer reports the two -Wreorder-init-list diagnostics, and only the 41 existing -Wc99-designator diagnostics remain, the same count as R3.

Risk

Two-way on AGX3B (M3, M4, A18 Pro) and AGX3C (M5). Flattening turns divergent single-load ifs into unconditional loads, so a wrong guard would read memory the original branch skipped.

Not yet

  • agx: omit unreachable wrap fallback on exact-tail binary peels #758 and the stack below it must merge first. The stack forked from aurora-wip at 902bafb on 11-21. At that fork point, test-agx3-vision-opts.cpp:387/389 also fails under GCC. aurora-wip replaced that test in c05f1c4, so a rebased stack drops the error.
  • An exact-head Asahi unit run at the rebased head.
  • A second-generation row at the rebased head.

@RyanTheTide

Copy link
Copy Markdown

Review at cf49487.

Read at cf49487 (not built here). I could not construct a miscompile in the store sink or the burst flatten, and "AGX2 and G18G unchanged" holds by reading. What blocks it:

  1. No hardware row at this head, and this fork's CI reports nothing. The change alters codegen in every stage on G15G/G16G/G17P and on M5. Owed: dEQP-VK compute + SSBO/UBO + robustness and a GL row on M3 (G15G), M4 (G16G), A18 Pro and M4 Pro T6040 (both G17P), plus one M5 (G17G/G17S) row; M4 Max T6041 is reached with no row. Please add an ABBA perf row per encoding: the "every CF boundary waits for loads in flight" premise comes from AGX3C.
  2. agx_nir_unroll_runtime_loops.c:713: dont_flatten is also honoured by nir_opt_peephole_select (nir_opt_peephole_select.c:351), so tail chunk ifs that peephole select folded before (pure ALU, speculatable loads) now stay branches on every AGX3B and M5 compile, including AGX3B shaders this PR does not otherwise touch. The commit message only describes the effect on the flatten pass. Show the shader-db delta of this hunk alone, or use a marker only agx_nir_opt_flatten_loads reads.
  3. agx_compile.c:16566-16585: the new select runs after agx_optimize_nir has finished, i.e. after agx_nir_lower_algebraic_late (:10855), agx_nir_fuse_selects (:10863), nir_opt_sink (:10896) and phi scalarization. The precedent it cites (:10777) runs before all of those. Only a hand-picked subset is redone, and UniformDmaNoPreamblePreservesScalarizedVecStore records a crash this ordering already produced. The new selects are never fused and the hoists undo the sink at :10896. Either lower DMA and select before the late lowering, or list which late passes are provably no-ops here. It also reaches M5 no-preamble fragment programs (hk_shader.c:5491-5495), which title and commit subject do not say.
  4. agx_compile.c:10529-10531 reuses peephole_select_options, whose expensive_limit is g17p ? 24 : 0 (:10516, from key->dev.chip == AGX_CHIP_G17P at :16532). The loop used to be AGX3C-only; it now runs on all AGX3B, so G15G/G16G and G17P flatten differently on the same encoding by chip name, while the post-DMA select uses 0 for every chip. Please key it on the encoding or use one value.
  5. agx_nir_opt_flatten_loads.c:670,696: the >= 4 and 24 thresholds have a rationale but no measurement. A burst makes every lane issue the load (zero page when untaken), and "sibling" means anywhere in the CF list, not adjacent. Please post the shader-db instruction/CF deltas and one cold-arm case.
  6. Nit: same_pure_expr (:439-503) retries the swapped operand order at every level without memoization, 4^8 calls worst case per store pair. The tests only compile AGX3B with chip = AGX_CHIP_G17P; add a G15G key (scheduling stalls).

@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-flatten-tile-load-bursts branch 2 times, most recently from 0dee159 to 73cad5b Compare November 23, 2026 02:22
@RyanTheTide

Copy link
Copy Markdown

Review at 73cad5b.

Re-read after the force-push (now ecfce54 + 73cad5b; not built here). Status of our points from cf49487:

  1. Hardware rows: still open. Nothing has been posted at this head. The same set is owed: dEQP-VK compute + SSBO/UBO + robustness and a GL row on M3 (G15G), M4 (G16G), A18 Pro and M4 Pro T6040 (G17P), and one M5, plus an ABBA perf row per encoding.
  2. dont_flatten on binary tails: fixed. The marker is gone from agx_nir_unroll_runtime_loops.c, so peephole select folds tail chunks as it did before this PR, and collect_binary_tail_ifs() (agx_nir_opt_flatten_loads.c:870) keeps only the load flattener away from them. The matcher fits what the unroller emits (factor 8, step +/-1, conditions on the loaded IV), so starting chains only at 4/2 is enough today. Please confirm that SkipsBinaryTailCascade fails with the skip_ifs lookup removed. The unroller-driven BinaryTailChunksSkippedLocallyWithoutDontFlatten would return false even without the skip set if its chunk bodies have no load to flatten.
  3. Post-DMA select ordering: partly fixed. The rerun now happens only when the select made progress, and it redoes the late tail. But agx_compile.c:16583-16617 is a hand copy of the end of agx_optimize_nir() (:10855-10905), and it has already drifted. It drops agx_nir_fold_extract16_bfe, agx_nir_fold_pixel_coord and nir_opt_combine_barriers. It drops nir_move_load_ssbo from the G17P sink. And it always uses the agx3_compute_opts fuse-select loop, even where agx_optimize_nir() takes the single-call branch. Please move that tail into one helper that both places call. Then the post-DMA path can't drift, and the "which late passes are no-ops" question goes away. Scope: the commit message now names M5 no-preamble fragment programs. The PR title still doesn't.
  4. Chip-keyed expensive_limit in the flatten loop: fixed. flatten_select_options (agx_compile.c:10528) uses 0 for every encoding.
  5. Burst thresholds: partly fixed. A burst must now be a contiguous run, broken by barriers, other CF and volatile stores (block_breaks_burst_run, :680), which answers the "anywhere in the CF list" concern. The >= 4 and 24 numbers still have no shader-db delta and no cold-arm measurement.
  6. same_pure_expr blowup and G15G coverage: fixed (64-node budget; G15G keys in both test files).

Commits have only the human Signed-off-by.

Verdict: NITS on code (the shared late-tail helper in 3, plus the test control in 2). Merge waits on the rows in 1 and the shader-db numbers in 5. Both are owed by the author.

Enable agx_nir_opt_flatten_loads on AGX3B alongside AGX3C (sharing the
(AGX3B || AGX3C) && !emit_agx3d gate, leaving AGX2 and G18G untouched),
using a single encoding-uniform peephole_select expensive_limit = 0 inside
the load-flattening loop so G15G/G16G/G17P/G17S flatten identically without
chip-name divergence.

Detect uniform 4/2/1 IV-stepped binary-tail cascades locally inside
agx_nir_opt_flatten_loads rather than marking nir_selection_control_dont_flatten
in agx_nir_unroll_runtime_loops, keeping nir_opt_peephole_select free to fold
pure-ALU / speculatable tail chunks while still preventing dummy-page load
speculation across tail cascades.

When !form_preamble runs agx3_nir_lower_uniform_dma (including AGX3B compute
and M5 no-preamble fragment programs), promoting UBO loads inside short-circuit
branches to load_preamble makes those branches trivial. Run
nir_opt_peephole_select (expensive_limit = 0) and, when it makes progress,
re-run the full late-lowering tail (int64 scalarize/lower,
agx_nir_lower_algebraic_late, agx_nir_fuse_selects,
agx_nir_fold_const_select_alu, constant folding, CSE, scalarization, sink/move
with derivative guard, phi scalarization, copy prop, DCE, and divergence).

Reaches all shader stages on AGX3B (G15/G16/G17P/G17A) and AGX3C without
emit_agx3d (G17G/G17S / M5), including M5 no-preamble fragment shaders.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-flatten-tile-load-bursts branch from 73cad5b to f9f69ac Compare November 23, 2026 02:56
@DjDeveloperr DjDeveloperr changed the title agx: enable load flattening on AGX3B and flatten tile-load bursts agx: enable load flattening on AGX3B, fold post-DMA selects (AGX3B/M5), and flatten tile-load bursts Nov 23, 2026
@RyanTheTide

Copy link
Copy Markdown

Review at f9f69ac.

range-diff against 73cad5b: commit 1 changed, commit 2 is identical. Status of our points:

  1. Post-DMA late tail: fixed. The end of agx_optimize_nir() is now agx_optimize_nir_late_tail() (agx_compile.c:10403), and the main path (:10924) and the post-DMA path (:16600) both call it. So the post-DMA path gets back agx_nir_fold_extract16_bfe, agx_nir_fold_pixel_coord, nir_opt_combine_barriers and the G17P nir_move_load_ssbo sink. I checked the arguments against the caller at :16545. g17p is the same chip == G17P, g18_scans is emit_agx3d, and post_dma_agx3_opts works out to the same (AGX3B || AGX3C) && !emit_agx3d that agx3_compute_opts becomes inside agx_optimize_nir(). Small nit: :16596-16599 computes that a second time; is_agx3 && !key->dev.emit_agx3d is already in scope and says the same thing. The PR title now names the M5 scope.
  2. SkipsBinaryTailCascade control: there is no answer in the thread, but from reading the code I think it is a real control. Its three ifs have uniform conditions and one load each. With the skip_ifs lookup (agx_nir_opt_flatten_loads.c:961) removed, they pass the uniform check at :965 and have a load to redirect (:982), so they would flatten and the EXPECT_FALSE/ifs == 3 would fail. A one-line "checked, it fails without the skip" from the author would close it.
  3. Burst thresholds: still open. There is no shader-db delta yet for the >= 4 and 24 choices. The body says the 4-chip sweep will be posted.
  4. Rows: still open. Nothing has been posted at this head.

Commits have only the human Signed-off-by. Nothing new found in the helper extraction.

Verdict: APPROVE on code (the duplicate gate is a nit). Merge waits on rows owed by the author at f9f69ac: dEQP-VK compute + SSBO/UBO + robustness and a GL row on M3 (G15G), M4 (G16G), A18 Pro and M4 Pro (G17P), and one M5, plus an ABBA perf row per encoding and the shader-db numbers for point 5. #762 is stacked on this PR.

@DjDeveloperr

Copy link
Copy Markdown
Author

Bare-Metal M5 Pro (G17S B1 / J714s) Verification — R2 (73cad5bf062c)

Tested exact R2 head 73cad5bf062c55624f2772545e40dafbdfe0285d on Apple MacBook Pro 14-inch M5 Pro (J714s / G17S B1 / AGX3C) running Linux 7.3.0-m5-n1keep-r3 (MESA_SHADER_CACHE_DISABLE=1).

Build Catch (-Dbuild-tests=true under GCC 16.1.1)

Compiling agx_tests fails under C++20/GCC 16.1.1 (c++ (GCC) 16.1.1 20260430) on src/asahi/compiler/test/test-flatten-loads.cpp:783 and :789:

src/compiler/nir/nir_builder_opcodes.h:22453:84: error: designator order for field '_nir_store_shared_indices::access' does not match declaration order in '_nir_store_shared_indices'

In struct _nir_store_shared_indices, .access is declared before .write_mask and .align_mul (base, access, write_mask, align_mul, align_offset), so moving .access right after .base = 0 in FlattenLoadsKeeps::StoreSharedMismatchedOrVolatileNotSunk will fix the C++20 build.

Unmodified Driver Targets (libvulkan_asahi.so + Native Asahi Gallium)

Building the unmodified driver targets directly at exact head 73cad5bf062c succeeded cleanly and passed all targeted VK and Native Asahi GL CTS runs with 0 failures and 0 GPU faults:

API / Driver CTS Group Passed Failed Not Supported
Vulkan (Borealis) dEQP-VK.compute.pipeline.basic.* (full) 80/81 0 1
Vulkan (Borealis) dEQP-VK.ssbo.* (489 evenly-sampled of 12,225) 487/489 0 2
Vulkan (Borealis) dEQP-VK.ubo.* (491 evenly-sampled of 13,240) 491/491 0 0
Vulkan (Borealis) dEQP-VK.robustness.buffer_access.* (303 evenly-sampled of 1,212) 255/303 0 48
Vulkan (Borealis) dEQP-VK.glsl.loops.* (312 evenly-sampled of 624) 312/312 0 0
Native Asahi GLES3 dEQP-GLES3.functional.ubo.single_basic_type.* (full) 968/968 0 0
Native Asahi GLES3 dEQP-GLES3.functional.shaders.loops.for_constant_iterations.* (full) 64/64 0 0
Native Asahi GLES3 dEQP-GLES3.functional.color_clear.* (full) 19/19 0 0
  • GPU faults (dmesg): 0 (delta = 0).
  • Note: Because agx_tests failed to compile under GCC 16.1.1, meson test --suite asahi is blocked until the designated-initializer order in test-flatten-loads.cpp:783,789 is fixed.

@DjDeveloperr

Copy link
Copy Markdown
Author

Addressed all remaining review items from #issuecomment-7089933037 in R3 (f9f69ac793a9, commits 274f207b11b + f9f69ac793a, stacked on #758 01a39fa4f91):

  1. Shared late-tail helper & PR title (Item 3):

    • Extracted agx_optimize_nir_late_tail(nir_shader *nir, bool g17p, bool g18_scans, bool agx3_compute_opts) in src/asahi/compiler/agx_compile.c and called it from both agx_optimize_nir() and the post-DMA if (dma_folded) cleanup path in agx_compile_shader_nir(). Both paths now share the exact same late-lowering tail (agx_nir_fold_extract16_bfe, agx_nir_lower_algebraic_late, agx_nir_avoid_square_overflow, agx_nir_fold_pixel_coord, agx3_compute_opts-gated agx_nir_fuse_selects loop + agx_nir_fold_const_select_alu, nir_opt_constant_folding, nir_opt_combine_barriers, copy-prop/DCE/CSE, scalarize ALU/const, nir_opt_sink/nir_opt_move with g17p SSBO-sink + derivative guard, phi scalarize + copy-prop/DCE) so they cannot drift.
    • Updated the PR title to explicitly note AGX3B/M5 reach for post-DMA select folding (including M5 no-preamble fragment programs).
  2. Binary-tail test control check (Item 2):

    • Confirmed empirically: temporarily bypassing _mesa_set_search(skip_ifs, nif) != NULL in agx_nir_opt_flatten_loads.c causes both tests to fail immediately:
      • AGX3VisionOpts.BinaryTailChunksSkippedLocallyWithoutDontFlatten: agx_nir_opt_flatten_loads(b.shader, 48, 4) returns true (expected false).
      • FlattenLoadsKeeps.SkipsBinaryTailCascade: agx_nir_opt_flatten_loads(b.shader, 64, 4) returns true (expected false) and remaining ifs drops 3 -> 0.
  3. Burst threshold measurements, 4-chip shader-db, CF deltas & cold-arm case (Item 5):

    • 4-chip 80-SPV GB6 Vulkan sweep (f9f69ac793a9 vs base agx: omit unreachable wrap fallback on exact-tail binary peels #758 01a39fa4f91, 80/80 OK, 0 spills, 0 thread drops on all 4 chips; R3 vs R2 73cad5bf is 0/122 diffs across all 4 chips):
      • G15G / G16G (M3 / M4): 20/80 changed, total instrs 16458 -> 16149 (-1.88%), bytes 157874 -> 155248 (-1.66%). Top wins: face-gemm-edge (56d3ae6a) 678 -> 595 (-83i, -774B), face-depthwise (720b8163) 256 -> 238 (-18i, -144B), face-im2col (1742fbd8) 230 -> 220 (-10i, -68B).
      • G17P (A18 Pro / M4 Pro): 20/80 changed, total instrs 17249 -> 16393 (-4.96%), bytes 174888 -> 168136 (-3.86%), 20/20 reductions (0 regressions): face-gemm-edge 724 -> 495 (-229i, -1868B), face-depthwise 256 -> 229 (-27i, -198B), 3c258afa 150 -> 133 (-17i, -112B), f652726b 129 -> 112 (-17i, -112B).
      • G17S (M5): 14/80 changed, total instrs 13760 -> 13456 (-2.21%), bytes 128990 -> 127022 (-1.53%) (face-gemm-edge 635 -> 557 (-78i), plus 8 binary-tail shaders stopped being over-flattened for -7..-45i each).
    • CF / load deltas (#758 01a39fa4 -> #761 R3 f9f69ac7, jmp_exec_none / pop_exec / loads):
      Shader G15G/G16G G17P G17S (M5)
      face-gemm-edge.spv jmp 6->6, pop 200->89 (-111), loads 128->96 (-83i) jmp 38->22 (-16), pop 200->89 (-111), loads 128->96 (-229i) jmp 6->6, pop 137->89 (-48), loads 128->96 (-78i)
      3c258afa83d48480.spv (cold-arm border) jmp 5->7, pop 42->27 (-15), loads 52->39 (+13i) jmp 13->10 (-3), pop 42->27 (-15), loads 52->39 (-17i) jmp 5->7, pop 36->27 (-9), loads 52->39 (+36i)
      f652726b67957eab.spv (cold-arm border) jmp 5->6, pop 42->27 (-15), loads 52->39 (+16i) jmp 13->10 (-3), pop 42->27 (-15), loads 52->39 (-17i) jmp 5->7, pop 36->27 (-9), loads 52->39 (+51i)
      8b798f621cbac2d3.spv jmp 2->2, pop 41->20 (-21), loads 47->36 (+10i) jmp 9->8 (-1), pop 41->38 (-3), loads 47->36 (-2i) jmp 0->1, pop 35->14 (-21), loads 47->36 (+22i)
    • Cold-arm case (3c258afa / f652726b, Canny hysteresis): Inside weak-edge handling, if (0 < x && 0 < y && x < W-1 && y < H-1) takes the unconditional 8-neighbor interior path for >99% of pixels and skips the border else arm completely via jmp_exec_none (so interior warps execute zero flattened/zero-page loads). Only 1-pixel border warps enter the else arm containing a contiguous burst of 8 guarded 1-byte neighbor loads, where flattening removes 15 (G15G/G16G/G17P) / 9 (G17S) pop_exec CF waits at a cost of +13..+16i (G15G/G16G), -17i (G17P), or +36..+51i (G17S).
    • Threshold sensitivity ((min_run, max_ops) vs no-burst, full 122-SPV corpus, 0 spills everywhere):
      Config G15G delta G17P delta G17S delta Shaders touched
      min2_op24 +232i / +1640B -403i / -3452B +281i / +3682B 8 (adds isolated 2-3 load branches af2e2f32, f0e90722)
      min3_op24 +227i / +1630B -368i / -3062B +241i / +3240B 7 (still pulls in 3-load branch f0e90722)
      min4_op24 (PR) +189i / +1282B -324i / -2642B +205i / +2888B 6 (face-gemm-edge, 8b798f62, 3c258afa, f652726b)
      min4_op12 / op16 / op32 identical to min4_op24 identical to min4_op24 identical to min4_op24 flat across 12..32 (24 leaves headroom for strided/3D index setup)
      min6_op24 +136i / +810B -314i / -2586B +118i / +2094B 4 (face-gemm-edge, 8b798f62)
      min8_op24 +126i / +708B -312i / -2574B +96i / +1770B 3 (face-gemm-edge only)
  4. CI & Hardware Verification (Item 1):

    • Local assert-enabled (-Db_ndebug=false) meson test --suite asahi --suite poly: 170/170 OK (0 fails) at f9f69ac793a9.
    • fedora-mesaci at f9f69ac793a9: 146/0 PASS (ICD git-f9f69ac793, 58s).
    • Apple A18 Pro (G17P B1, Borealis) paired ABCCBA (slot25-split-r2, measured on 01a39fa4f91 -> 73cad5bf062; R3 f9f69ac793a9 is 0/122 compute shader-db diffs vs 73cad5bf062):
      • Face Detection: 39.582 ms (39.585 / 39.580) -> 38.163 ms (38.176 / 38.151, -3.58%; face-depthwise 8.080 -> 7.268 ms -10.05%, face-im2col 12.345 -> 11.881 ms -3.76%, face-pointwise 3.412 -> 3.215 ms -5.77%).
      • Background Blur: 24.114 ms -> 23.792 ms (-1.34%).
      • Edge Detection: 20.392 ms -> 20.203 ms (-0.92%).
      • Particle Physics: 126.865 ms -> 125.234 ms (-1.29%).
      • Horizon / Stereo / Gaussian / FM: flat within noise (-0.13% to +0.47%).
      • gpubench-vk (16/16 vision/compute cases): 16/16 OK, 16/16 bit-exact output CRC match across all 6 ABCCBA passes.
      • Vulkan CTS (1189 cases, verified on top of stack e8c1ebc3, 0 fail, 0 recov, FAULTS_DELTA=0, AVC_DELTA=0): glsl_loops 624/624 Pass, ssbo_std430 303/303 Pass, compute_basic 79 Pass / 2 NotSupported, ubo_gfx 25/25 Pass, robustness_supported 132 Pass / 24 NotSupported.
    • Exact-head R3 ARM64 tarball (f9f69ac793a9, SHA-256 f198cf2e3b16...) is built and handed off for cross-chip M4 (G16G) and M5 (G17S) ABBA + CTS + GL rows.

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

@DjDeveloperr

Copy link
Copy Markdown
Author

Bare-Metal M5 Pro (G17S B1 / J714s) Verification — R3 (f9f69ac793a9)

Tested exact R3 head f9f69ac793a97c9089af1708fca6543af166a778 (prebuilt mesa-pr761-r3-f9f69ac7.tar.zst, sha256 f198cf2e3b165ce9e2032cd88493fd5d74ec7c43cc95ed8e30704142ab846149, driverInfo = Mesa 26.3.0-devel (git-f9f69ac793)) on Apple MacBook Pro 14-inch M5 Pro (J714s / G17S B1 / AGX3C) running Linux 7.3.0-m5-n1keep-r3 (MESA_SHADER_CACHE_DISABLE=1):

CTS Group Passed Failed Not Supported
dEQP-VK.compute.pipeline.basic.* (full) 80/81 0 1
dEQP-VK.ssbo.* (489 evenly-sampled of 12,225) 487/489 0 2
dEQP-VK.ubo.* (491 evenly-sampled of 13,240) 491/491 0 0
dEQP-VK.robustness.buffer_access.* (303 evenly-sampled of 1,212) 255/303 0 48
dEQP-VK.glsl.loops.* (312 evenly-sampled of 624) 312/312 0 0
  • GPU faults (dmesg): 0 (delta = 0).

@DjDeveloperr

DjDeveloperr commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Pushed 8e2d12181e181ef9b001adab30581eaa4b33d35a. The review blocker is fixed: both nir_store_shared calls in StoreSharedMismatchedOrVolatileNotSunk now pass .access right after .base, in declaration order.

  • GCC 16.1.1: test-flatten-loads.cpp compiles at the new head. R3 failed with the reported error.
  • Clang 23 -Werror: the two -Wreorder-init-list diagnostics are gone. Only the 41 existing -Wc99-designator diagnostics remain, the same count as R3.
  • The driver sources are unchanged from R3, so the J714s and J773g R3 rows below still apply.
  • One more GCC error in agx_tests is inherited: test-agx3-vision-opts.cpp:387/389 comes from the stack's fork point 902bafb. aurora-wip replaced that test in c05f1c4, so it goes away when agx: omit unreachable wrap fallback on exact-tail binary peels #758 and the stack below it rebase.

Earlier R3 row on J773g, kept for reference:

Exact-head verification on Apple M4 (G16G B0, J773g / T8132) for R3 (f9f69ac793a9, stacked on #758 R2 01a39fa4f91f):

  • Environment & driver identity: Kernel 7.3.0-ARCH-m4prod23 (zero reboot); exact-head prebuilt ARM64 GL+VK kits verified via gpubench-vk --info (Borealis, Vulkan 1.4.363) and gpubench-gl --info (4.6 (Core Profile)):
    • #758 R2 (stack parent): Mesa 26.3.0-devel (git-01a39fa4f9), SHA256 4ca62c3fca60aeb07e0551c7359d50b1a82a3c8b2266458706d2474fec8ed2e0
    • #761 R3: Mesa 26.3.0-devel (git-f9f69ac793), SHA256 f198cf2e3b165ce9e2032cd88493fd5d74ec7c43cc95ed8e30704142ab846149

Single-Threaded CTS (MESA_SHADER_CACHE_DISABLE=true, DMESG_CLEAN_DELTA=0)

Suite / Group Passed NS Failed
dEQP-VK.compute.pipeline.basic.* 79/81 2 0
dEQP-VK.glsl.loops.* 624/624 0 0
dEQP-VK.ssbo.layout.single_basic_type.std430.* 303/303 0 0
dEQP-VK.ubo.{random,2_level_struct,single_basic} 118/160 42 0
dEQP-VK.robustness.*robustness2.* (1d comp/v/f) 36/36 0 0
dEQP-VK.robustness.index_access.* 31/40 9 0
dEQP-VK.robustness.buffer_access.{v,f,c} + rob2 120/156 36 0
VK unique total (deduplicated) 1,275/1,364 89 0
dEQP-GLES31.functional.compute.basic.* 41/41 0 0
dEQP-GLES31.functional.ssbo.layout...std430.* 121/121 0 0
GLES31 total 162/162 0 0

gpubench-vk ABCCBA: #761 R3 (f9f69ac793) vs #758 R2 (01a39fa4f9)

72/72 pairwise (108/108 unique 6-leg stack) CRCs bit-identical PASS, 0 non-cooling dmesg lines. Highlights -1.74% (d=1) / -1.42% (d=4) GPU win on compute.edge kind=harris.

Workload d Wall (us_per_submit) #758 R2 -> #761 R3 Wall delta / speedup GPU (gpu_us) #758 R2 -> #761 R3 GPU delta / speedup CRC
compute.conv2d kind=gaussian,res=1080p 1 1506.07 -> 1242.24 -17.52% (1.212×) 939.55 -> 909.38 -3.21% (1.033×) 2d55a14d PASS
compute.conv2d kind=gaussian,res=1080p 4 913.24 -> 913.24 +0.00% (1.000×) 910.10 -> 910.08 -0.00% (1.000×) 2d55a14d PASS
compute.conv2d kind=bilateral,res=1080p 1 1754.39 -> 1746.76 -0.43% (1.004×) 1469.71 -> 1469.73 +0.00% (1.000×) 6cdc0716 PASS
compute.conv2d kind=bilateral,res=1080p 4 1481.48 -> 1481.48 +0.00% (1.000×) 1472.54 -> 1472.60 +0.00% (1.000×) 6cdc0716 PASS
compute.stereo disp=64 1 20000.00 -> 19090.91 -4.55% (1.048×) 18726.90 -> 18727.35 +0.00% (1.000×) c62aadf9 PASS
compute.stereo disp=64 2 18181.82 -> 18181.82 +0.00% (1.000×) 18723.64 -> 18721.82 -0.01% (1.000×) c62aadf9 PASS
compute.edge kind=sobel,res=1080p 1 798.41 -> 793.65 -0.60% (1.006×) 510.42 -> 513.50 +0.60% (0.994×) 27c79e6d PASS
compute.edge kind=sobel,res=1080p 4 518.13 -> 522.20 +0.78% (0.992×) 515.09 -> 518.67 +0.69% (0.993×) 27c79e6d PASS
compute.edge kind=harris,res=1080p 1 961.74 -> 954.70 -0.73% (1.007×) 674.83 -> 663.06 -1.74% (1.018×) 4cebadfc PASS
compute.edge kind=harris,res=1080p 4 701.75 -> 690.85 -1.55% (1.016×) 698.46 -> 688.53 -1.42% (1.014×) 4cebadfc PASS
compute.uber kind=divergent 1 727.27 -> 722.02 -0.72% (1.007×) 499.98 -> 500.02 +0.01% (1.000×) 9963f56c PASS
compute.uber kind=divergent 4 504.41 -> 504.41 +0.00% (1.000×) 502.21 -> 502.20 -0.00% (1.000×) 9963f56c PASS
compute.uber kind=uniform 1 411.10 -> 411.52 +0.10% (0.999×) 185.77 -> 185.77 +0.00% (1.000×) b022fbaf PASS
compute.uber kind=uniform 4 190.39 -> 190.57 +0.10% (0.999×) 187.88 -> 187.85 -0.01% (1.000×) b022fbaf PASS
compute.uber kind=ilp 1 611.64 -> 610.70 -0.15% (1.002×) 383.46 -> 383.47 +0.00% (1.000×) 19f35e30 PASS
compute.uber kind=ilp 4 389.48 -> 389.86 +0.10% (0.999×) 386.88 -> 387.06 +0.05% (1.000×) 19f35e30 PASS
compute.light 1 139.84 -> 138.81 -0.74% (1.007×) 1.58 -> 1.58 -0.03% (1.000×) n/a
compute.light 4 55.69 -> 56.00 +0.56% (0.994×) 1.58 -> 1.58 +0.00% (1.000×) n/a
compute.heavy 1 790.53 -> 787.40 -0.40% (1.004×) 544.33 -> 544.31 -0.00% (1.000×) n/a
compute.heavy 4 547.95 -> 547.95 +0.00% (1.000×) 544.33 -> 544.33 +0.00% (1.000×) n/a
compute.cull_mdi 1 2439.39 -> 2469.14 +1.22% (0.988×) 1268.09 -> 1273.13 +0.40% (0.996×) 57276de2 PASS
compute.cull_mdi 4 1793.75 -> 1753.65 -2.24% (1.023×) 1339.14 -> 1338.41 -0.05% (1.001×) 57276de2 PASS

gpubench-gl ABCCBA: #761 R3 vs #758 R2

16/16 pairwise (24/24 unique 6-leg stack) CRCs PASS, DMESG_DELTA=0, 0 regressions.

Workload d Wall (us_per_submit) #758 R2 -> #761 R3 Wall delta / speedup GPU (gpu_us) #758 R2 -> #761 R3 GPU delta / speedup CRC
compute.uber kind=divergent 1 738.92 -> 735.29 -0.49% (1.005×) n/a n/a 9963f56c PASS
compute.uber kind=divergent 4 506.76 -> 506.76 +0.00% (1.000×) n/a n/a 9963f56c PASS
compute.uber kind=uniform 1 421.40 -> 427.96 +1.56% (0.985×) n/a n/a b022fbaf PASS
compute.uber kind=uniform 4 192.31 -> 192.93 +0.32% (0.997×) n/a n/a b022fbaf PASS
compute.image 1 456.63 -> 453.86 -0.61% (1.006×) n/a n/a n/a
compute.image 4 191.94 -> 192.80 +0.45% (0.996×) n/a n/a n/a
compute.heavy 1 795.76 -> 800.01 +0.53% (0.995×) 5.06 -> 5.10 +0.83% (0.992×) n/a
compute.heavy 2 561.80 -> 561.80 +0.00% (1.000×) 5.15 -> 5.25 +2.02% (0.980×) n/a
compute.heavy 4 561.80 -> 561.80 +0.00% (1.000×) 5.06 -> 5.35 +5.77% (0.945×) n/a
compute.heavy 8 561.80 -> 561.80 +0.00% (1.000×) 5.21 -> 5.29 +1.60% (0.984×) n/a
compute.heavy 16 561.80 -> 561.80 +0.00% (1.000×) 5.13 -> 5.38 +4.86% (0.954×) n/a
compute.heavy 32 559.70 -> 559.70 +0.00% (1.000×) 5.15 -> 5.42 +5.08% (0.952×) n/a
compute.heavy 64 559.70 -> 559.70 +0.00% (1.000×) 5.25 -> 5.40 +2.94% (0.971×) n/a
  • Post-flight health: DMESG_CLEAN_DELTA=0, NON_COOLING_CLEAN_DELTA=0, 0 GPU faults / kernel bugs (REAL_FULL_BOOT_KERNEL_BUGS=0, SCHED_WHILE_ATOMIC=0, INTERRUPT_RC_WARN=0), IDLE_CLEAN.

@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: FINISH, then merge

Checked: Full diff across src/asahi/compiler/agx_compile.c, src/asahi/compiler/agx_nir_opt_flatten_loads.c, src/asahi/compiler/test/test-agx3-vision-opts.cpp, and src/asahi/compiler/test/test-flatten-loads.cpp, plus native assert-enabled -Dbuild-tests=true compilation under g++ (GCC) 16.1.1 on Mac Studio M5 Max (J775c, T6050).

Blockers:

  1. src/asahi/compiler/test/test-flatten-loads.cpp:783-785 and :789-791 (FlattenLoadsKeeps::StoreSharedMismatchedOrVolatileNotSunk): both nir_store_shared(...) calls pass .access after .write_mask and .align_mul, violating C++20 designated-initializer declaration order for struct _nir_store_shared_indices (base, access, write_mask, align_mul, align_offset). This breaks -Dbuild-tests=true compilation of agx_tests under GCC 16.1.1 (also noted on R2 and still present at R3 f9f69ac793a9). Move .access immediately after .base = 0 in both calls so agx_tests builds cleanly under strict C++20 compilers as well as Clang.

Notes:

  • Compiler logic in agx_optimize_nir_late_tail(), agx_compile_shader_nir_once(), sink_common_store_shared(), collect_burst_ifs(), and collect_binary_tail_ifs() is clean and addresses prior review points.
Evidence
  • Head audited: f9f69ac793a97c9089af1708fca6543af166a778 (aurora-silicon/mesa#761)
  • Native build check (g++ (GCC) 16.1.1 20260430, -Dbuild-tests=true -Db_ndebug=false, ninja src/asahi/compiler/agx_tests): fails compilation with out-of-order designated initializers; src/asahi/compiler/test/test-flatten-loads.cpp:783-785 and :789-791 still place .access after .write_mask and .align_mul.

In agx_nir_opt_flatten_loads, sink matching terminal store_shared
instructions out of single-block if/else diamonds into one
store_shared(phi(v_then, v_else), saddr) after the if when both arms compute
structurally equivalent pure-ALU shared offsets (intersecting access flags,
refusing volatile/atomic stores, and capping same_pure_expr work to a 64-node
budget to prevent exponential recursion).

Also flatten contiguous runs of >= 4 small single-load divergent ifs in the
same CF list (separated only by pure ALU or non-volatile store_shared, reset on
barriers or other CF nodes, and excluding load_global_constant so UBO reads
stay intact for uniform DMA):
- Burst threshold (>= 4): isolated 1-3 divergent loads rarely amortize
  speculative zero-page redirection when an arm is cold, whereas >= 4
  consecutive guarded tile loads remove >= 4 serial load-wait CF barriers so
  global loads issue back-to-back with overlapped latency and let LICM hoist
  shared tile-address math.
- Per-arm cap (MIN2(limit, 24)): admits a 2D/3D index + byte offset +
  descriptor setup (~8-16 NIR ops) while keeping speculative untaken ALU
  bounded across a multi-if burst.

Reaches all shader stages under (AGX3B || AGX3C) && !emit_agx3d
(AGX3B: G15/G16/G17P/G17A; AGX3C: G17G/G17S / M5). AGX2 and G18G are
unchanged.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/agx3b-flatten-tile-load-bursts branch from f9f69ac to 8e2d121 Compare November 26, 2026 07:57
@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr Status of the AGX3 vision stack above #680 as of today, with what each PR still needs. This is one list so you don't get a separate ping on every PR.

Stack base. #657 is merged. #680 is retargeted to aurora-wip and forward-merged (851615a456e6). It waits on a row at that head (see #680). #700, #704, #735, #743 and #758 are approved by satchlj at their current heads. Each of them conflicts with current aurora-wip in files it touches (agx_nir_unroll_runtime_loops.c, agx_compile.c, agx_nir_opt_guard_selected_loads.c, agx_nir_opt_licm.c, agx_optimizer.c, agx_pressure_schedule.c, test-agx3-vision-opts.cpp). So each needs a forward-merge and a row at the new head before it can land. If folding them into fewer PRs against aurora-wip is easier for you, that works too.

#761 (this PR). satchlj's CHANGES_REQUESTED at f9f69ac793a9 was the designated-initializer order in test-flatten-loads.cpp:783-791. 8e2d12181e18 fixes it; I checked that f9f69ac..8e2d121 touches only that test file (+5/-5). Your R3 M5 Pro and M4 rows therefore still apply. @satchlj, could you re-review at 8e2d12181e18?

#762. It now conflicts with #761's new head in test-flatten-loads.cpp. Merge #761's head into it. It also has no review yet.

#801, open items, no reply or push since 382d594c3af6 (11-23):

  1. satchlj (11-25): is_ballot_true_def() (agx_nir_lower_subgroups.c:95-114) never checks s.comp == 0. subgroupBallotExclusiveBitCount(uvec4(b.y, 0, 0, 0)) with b = subgroupBallot(true) folds to the active-lane index instead of 0. opt_compute_ballot_lt_mask_count() (:189-192) and is_subgroup_lt_mask() (:149, :164) pass .def and drop .comp. The ask: require comp == 0, pass the nir_scalar through, and add a negative test for uvec4(b.y, 0, 0, 0).
  2. My 11-23 review: the active-lane rank rewrite takes the rank at the consumer. A ballot(true) taken before a divergent branch still counts the lanes outside it. Emit the rank under the ballot's execution mask, or restrict the rewrite to a proven same-mask region. Add a divergent-consumer regression for both the intrinsic and the lowered bit-count form.

#804, open items, no reply or push since 179980b3a6d7 (11-23):

  1. satchlj (11-25): test-agx3-vision-opts.cpp:4864,4921,4925 put .access before .write_mask in nir_store_global, so agx_tests fails to build under GCC 16. Use .write_mask, .access, .align_mul order.
  2. My 11-23 follow-up: add a negative regression with a possibly aliasing load or a coherent store between the candidate stores, and a positive case where address order differs from instruction order. The AGX3C counterpart I asked for is covered by your M5 Pro row at this head.

#805 and #920 are approved at head and wait on the stack below them.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at 8e2d121 after #758 and its bases land and the integrated check passes.

Checked: The direct delta is confined to the two test call sites. Both designators follow the generated C++ declaration order and share the original volatility value. The author reports GCC 16.1.1 now compiles the affected file and the driver source is byte-identical to the hardware-tested revision.

@RyanTheTide
RyanTheTide dismissed satchlj’s stale review November 29, 2026 01:39

The initializer order in the two shared-store fixtures is corrected at 8e2d121. Stack order and integrated validation remain. 8e2d12181e18

@github-actions github-actions Bot added area:compiler Area from CODEOWNERS (bot) blast:m3 Macs affected, from Merge risk (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 1, 2026
@RyanTheTide

Copy link
Copy Markdown

I started carrying the remaining changes onto current aurora-wip after #1353 replaced the old stack base. The shared-store part passed on a saved J615 M3 Air boot running 7.3.0-aurora-edge. Local preview 269dc2689004 and trunk d7045710aa48 each matched 2,048 CPU reference words and left all 2,048 padding words untouched. The compute shader reads a neighbouring lane after a workgroup barrier. Its compiler dump shows two branch-local stores on trunk and one store after the branch in the preview. There were no kernel events.

The preview also passed 393 Asahi and Poly host tests with one skip and an AArch64 build. This covers the store-sinking part. I’m carrying the sibling tile-load bursts and post-DMA select folding forward separately so their interactions with #681 and #757 can be checked.

@RyanTheTide

Copy link
Copy Markdown

The post-DMA select slice now folds the guarded preamble read on J615. With the same saved Edge boot and isolated ICDs, parent preview af04731e88ce had one divergent branch and the post-DMA preview 0121e1374230 had a select instead. Both matched 2,048 CPU reference outputs and preserved all 2,048 padding words. No kernel entries were recorded. The focused G15G/G17P compiler tests and vector-store fixture passed, as did 393 Asahi and Poly host tests with one skip.

The broad sibling-burst admission hit an existing occupancy check on the guarded G17P im2col dual-copy shader at 136 GPRs and 768 resident threads. The narrower admission passes that check and the J615 exclusion controls. A hardware workload that exercises the narrowed burst is still due.

@RyanTheTide

Copy link
Copy Markdown

Thanks, Dj. I opened #1537 with your terminal shared-store change on current aurora-wip. The exact head passed 393 host tests with one skip. On J615 it reduced two branch-local stores to one and matched all 1,048,576 reference values in each run. GPU time stayed within run variation across the larger paired comparison.

I filed #1535 for sibling burst qualification and #1536 for the post-DMA timing regression. If you want to take either follow-up, feel free to open a PR against current trunk. I’ll keep the stack moving after #1537 lands.

@RyanTheTide

Copy link
Copy Markdown

The shared-store continuation is now #1538. It preserves Dj's original sign-off and declines to merge stores whose access qualifiers differ, including the coherent mismatch Satya flagged. The refreshed J615 row and host results are in the replacement. #762's rotated-stencil integration passed its current-trunk comparison and is staged locally on the revised parent.

@RyanTheTide

Copy link
Copy Markdown

Thanks, Dj. The terminal shared-store sinking landed via #1538 in da7d055143cbf, with your original sign-off preserved. It keeps matching access qualifiers and passed the J615 and J775c rows. Closing this original stack in favor of the landed slice. The burst and binary-tail qualification is tracked in #1535, and the post-DMA select timing in #1536. Both follow-ups are still open if you’d like to pick either one up.

@RyanTheTide RyanTheTide closed this Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:compiler Area from CODEOWNERS (bot) blast:m3 Macs affected, from Merge risk (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants