Skip to content

agx: peel proven non-wrapping exact loop tails in 4/2/1 chunks - #1353

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
closeout/743-758-exact-binary-tail
Nov 29, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
closeout/743-758-exact-binary-tail

Conversation

@RyanTheTide

Copy link
Copy Markdown

What: Replace the scalar remainder of an eligible factor-eight exact-tail loop with guarded 4/2/1 chunks when induction cannot wrap. This adapts the binary tail and wrap proof from Dj’s #743 and #758 to the current counted-loop unroller.

Tested: J615 M3 Air G15G and J700 Neo G17P at production 3478c98. Both checked all 16,384 words per run and exercised the 4/2/1 tail. PR head a8b9e2d adds only regression tests and passed 1,834 host compiler tests with one skip. J615 integer convolution improved 4.27%–9.24% and J700 improved 8.13%–14.02%. Main shader size increased by 242 bytes for convolution and 374 for the other fixtures. Strided J615 timing was inconclusive.

Risk: two-way door · shared AGX3B and AGX3C runtime-loop optimization. Bounded code duplication can increase shader size. Mutable or extreme bounds and existing integer-only bottom rotation keep their scalar path.

Not yet: Native M4 and M5 timing, or a stable result on the J615 strided fixture.

Revert: Revert the merge commit. No persistent state changes.

Details

The pass requires the existing exact-tail live-out conditions, a unit step, a factor-eight main loop, and the 2× body-instruction estimate. The initial value must be a known non-extreme constant. Inclusive bounds must be known and below the directional extreme. The scalar tail remains for all other cases.

The compare now leads to guarded chunks with 4/2/1 iterations, using the current register-backed accumulator and invariant live-outs. The compiler can vectorize the tail loads. The regression tests run original and optimized NIR through ascending and descending wrap on inclusive signed and unsigned limits, and check finite runtime bounds.

Evidence

Baseline 8bad0a5. Native candidate 3478c98. Proposed a8b9e2d changes only the compiler test file from the native candidate. Both devices used Linux 7.3.0-aurora-edge from 933f822675e4d6d7197084f833d4aec3aecaa574. Stage 2 SHA-256 c5d7ef6ceb959d97242837005dfbf099cab273b4af54844c4617bab107de74f7 on J615 and c0654b4996d2d03e41c59897e628e0abd94ed7834020268a7b254eab704d6936 on J700. The kernel journals returned “No entries” during the paired runs. Both saved boots, Enforcing and zero failed units stayed the same.

Device Integer convolution bounds Baseline GPU median Proposed GPU median Median paired change
J615 G15G 0–7 6.292 µs 5.750 µs −7.39%
J615 G15G 8–15 6.834 µs 6.375 µs −9.24%
J615 G15G 0–63 10.167 µs 9.688 µs −4.27%
J700 G17P 0–7 8.166 µs 7.167 µs −14.02%
J700 G17P 8–15 9.167 µs 8.042 µs −11.61%
J700 G17P 0–63 14.375 µs 13.250 µs −8.13%

Six ABBA blocks per row, 160 warmups and ten timestamps per process. Overlapping loads improved 4.36%–10.57%. The J615 strided identical-ICD control varied from −45.82% to +37.54% across individual blocks. The two strided A/B runs disagree on the larger bounds.

src/asahi/compiler/agx_tests --gtest_brief=1
VK_DRIVER_FILES=<isolated-icd-manifest> MESA_SHADER_CACHE_DISABLE=1 MESA_TAIL_RANGE=tail-only MESA_TAIL_WARMUPS=160 ./convolution-aarch64
VK_DRIVER_FILES=<isolated-icd-manifest> MESA_SHADER_CACHE_DISABLE=1 AGX_MESA_DEBUG=shaders ./convolution-aarch64 --dump

Benchmark sources, commands, raw timings and ICD hashes

Adapt the exact bottom-tested tail from DjDeveloperr's 13a65a6
and the no-wrap proof in 01a39fa to the current counted-loop
unroller. Reuse the existing register-backed live-outs and guard math,
and retain the scalar tail where the first step or inclusive endpoint
could wrap.

Keep the bottom-rotated integer-only main loop on its current path.
Compare optimized NIR execution against the original loop for divergent
bounds, boundary wrap controls and carried and invariant live-outs.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide
RyanTheTide requested review from a team and DjDeveloperr as code owners November 29, 2026 12:36
@RyanTheTide

Copy link
Copy Markdown
Author

@aurora-silicon-review please review a8b9e2d. The native J615 and J700 runs cover the same production source and the final commit adds inclusive-bound fallback tests only. The hardware matrix and raw timings are in the description.

@RyanTheTide

Copy link
Copy Markdown
Author

@aurora-silicon/aurora-maintainers @DjDeveloperr, please review a8b9e2d for the Mesa code-owner gate. The PR includes the J615 and J700 hardware results and the tests that retain the scalar fallback when inclusive bounds can wrap.

@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 a8b9e2dd

Blockers: None.

Checked:

  • Audited binary_tail_no_wrap(), tail_continuation_is_pure(), emit_tail_chunk(), loop_continue(), and the 4/2/1 tail emission in src/asahi/compiler/agx_nir_unroll_runtime_loops.c:
    • Wrap proof (binary_tail_no_wrap): analyze_loop_opts() restricts iv/bound to 32-bit scalars and enforces (rel == REL_LT || rel == REL_LE) == (step > 0); requiring exact_tail, factor == 8, unit step (±1), constant iv0 != extreme, and either strict (REL_LT/REL_GT) or constant bound != extreme for inclusive relations proves neither the forced first bottom-tested iteration nor any main/4/2/1 chunk can wrap across all signed/unsigned ascending/descending extremes.
    • Guard math & exact trip count: guard_bound() + emit_guard_cont(..., exact_tail=true) for count = 4, 2 and the final count = 1 guard nir_ieq(iv, iv0) || loop_continue(iv, bound) preserve exact bottom-tested semantics across 0..7 remaining trips as well as iv0 already past bound (guaranteed 1 iteration; because induction is strictly monotonic without wrap, iv == iv0 is true at the 1-chunk iff no earlier chunk ran).
    • Live-outs and CFG cleanup: tail_continuation_is_pure() guards the extracted continuation branch, bottom-rotated integer-only main loops keep binary_tail = false, outs[i] live-out loads/rematerializations are emitted right after the 1-chunk before removing the original loop, and nir_lower_reg_intrinsics_to_ssa_impl() runs immediately afterward.
    • Head commit a8b9e2dd touches only src/asahi/compiler/test/test-agx3-vision-opts.cpp (adding inclusive-bound wrap fallback tests); production code is unchanged from hardware-verified 3478c981.
  • Host agx_tests (1,834 pass / 1 skip, including NIR interpreter differential tests across signed/unsigned, ±1, inclusive/exclusive, and wrap fallbacks) and J615 G15G (-4.27% to -9.24%) / J700 G17P (-8.13% to -14.02%) integer convolution runs with 16,384/16,384 exact word readbacks and zero kernel errors support merging.
Evidence
  • src/asahi/compiler/agx_nir_unroll_runtime_loops.c: verified analyze_loop_opts() (L361–569), guard_bound() (L582–641), back_edge_live_outs() (L709–747), binary_tail_no_wrap() (L823–837), tail_continuation_is_pure() (L840–858), emit_tail_chunk() (L861–875), and unroll_loop() (L901–1130).
  • src/asahi/compiler/test/test-agx3-vision-opts.cpp: verified BinaryTailExactLiveOuts, BinaryTailRetainsUnprovenFallback, and BinaryTailRetainsInclusiveBoundFallback (nir_validate_shader() + NIR execution parity).
  • Reported hardware rows at 3478c9815c32 (identical production source to a8b9e2dd): J615 G15G -7.39% / -9.24% / -4.27% and J700 G17P -14.02% / -11.61% / -8.13% across 0–7, 8–15, 0–63 bounds, 16,384/16,384 words exact.

@RyanTheTide

Copy link
Copy Markdown
Author

Rechecked exact a8b9e2d on aurora-wip 58abf3b in preview baf4e84a8f5d. The patch ID matches the original branch. Host compiler tests passed 1,834 with one skip. J615 and J700 integer-convolution readbacks matched all 16,384 words per invocation, and each compute binary matched its earlier capture byte for byte. Paired GPU time fell 2.62%–5.50% on J615 and 8.05%–11.85% on J700 across the three bound distributions. The saved boots remained unchanged and the run windows had no kernel messages.

@RyanTheTide

Copy link
Copy Markdown
Author

Integration with aurora-wip at 22d31fe4d468 preserves the reviewed a8b9e2ddacaae patch. The host suite passed 1,834 tests with one skip. On J700 the baseline and candidate each passed 42/42 focused Vulkan CTS cases and the paired convolution runs matched all 16,384 outputs per invocation. Paired GPU time fell 5.65%–8.46% across the three tested distributions. The saved boot was unchanged and the kernel journal had no new entries. The earlier J615 comparison still covers the same production code.

@RyanTheTide

Copy link
Copy Markdown
Author

J713 M4 G16G peer on saved fleet85. Compared landed 293cc49fc266 against clean integration 06c1afeea5f2 containing exact a8b9e2ddacaae. Each integer-convolution invocation matched all 16,384 CPU-reference outputs. Across six ABBA blocks per distribution, paired median GPU time fell 7.00% for tail-only, 8.06% for one-block and 5.18% for mixed. Mixed and one-block improved in every block. Tail-only ranged from −20.20% to +0.81%. The main shader grew from 550 to 792 bytes. Saved boot unchanged and no new kernel messages.

@RyanTheTide

Copy link
Copy Markdown
Author

@DjDeveloperr, code-owner heads-up: App approval at a8b9e2ddacaa from a different lane. This PR goes into the merge train's next combined A/B (aurora-wip 293cc49f + #1175 #1260 #1266 #1349 #1351 #1353) on M1 Pro G13S + M4 G16G. If that row is clean, I plan to land it via --admin under the 6-point rule no earlier than one hour from now, unless you post a hold.

@RyanTheTide

Copy link
Copy Markdown
Author

Merge-train landing at a8b9e2ddacaa.

Revert: git revert -m 1 <merge commit> on aurora-wip. A regression gets reverted first and fixed after.

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.

1 participant