Skip to content

agx: don't unroll runtime loops whose main loop could run at most once - #1204

Merged
RyanTheTide merged 3 commits into
aurora-wipfrom
perf/agx-unroll-trip-bound
Nov 27, 2026
Merged

RyanTheTide merged 3 commits into
aurora-wipfrom
perf/agx-unroll-trip-bound

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 27, 2026 •

Copy link
Copy Markdown

What

agx_nir_unroll_runtime_loops() picks how many iterations a loop runs per test from the body size alone. gpubench-vk
compute.conv2d fills its shared-memory halo tile with a loop striped over the 16-wide workgroup:
for (dx = lx; dx < 32; dx += 16). Every invocation runs that loop at most twice, so its 8-way main loop never runs.
The guard and the eight copies of the body still cost code size and issue time: the horizontal pass had 9 device loads
and 9 shared stores where the source has one of each.

The pass now bounds the trip count of a counting-up loop when two things hold:

  • the loop has a constant bound;
  • its start cannot be negative (nir_unsigned_upper_bound(), as for a local invocation index).

It also checks that the largest start can't wrap before the test that ends the loop. It then lowers the factor until
the main loop could run at least twice. A main loop that can run at most once saves at most f − 1 loop tests and costs
its guard and f body copies either way, so such a loop stays rolled. Loops without such a bound keep the factor they had.

A new unit test, M5RuntimeLoops.TripBoundLimitsFactor, covers:

  • 2-trip bounds stay rolled;
  • 4, 15 and 16 trips get factors 2, 4 and 8;
  • a push-constant start, which may be negative, keeps factor 8.

Tested

Head 3288d18337b4:

  • 7d7cc368fbe1 merges aurora-wip 65866bbd5bb3. The aurora-wip side adds loops that step by a runtime value
    (step 0, the step in step_def) and exit tests that read iv + step_def (offset_is_step). max_trips() gives
    no bound for both: it divides by the constant step and reads the constant offset.
  • 3288d18337b4 adds that case to M5RuntimeLoops.TripBoundLimitsFactor: a push-constant-step read-only reduction
    with constant bounds of 4096 and 64 keeps factor 8.
  • Fedora build with tests at the head: meson test --suite asahi 214/214, M5RuntimeLoops.* 14/14. With the bail
    removed, agx_tests dies in TripBoundLimitsFactor (SIGFPE), so the test covers it.
  • Offline G16G drm-shim, all 84 gpubench-vk plan tests, head vs aurora-wip 65866bbd5bb3: only the conv2d gaussian and
    bilateral binaries change (compile.pso differs between any two runs). Their sizes are the same as at e0d35b6aff2b:
    gaussian 2858/3184 → 2056/2266 bytes, bilateral 5352/5670 → 4550/4760.

J773g at this head (driver of 7d7cc368fbe1; 3288d18337b4 adds only the test) against aurora-wip 65866bbd5bb3, stock
kernel 7.3.0-ARCH-m4mainefed1ae3, mirrored B C C B ×2 (4 runs per arm), all records valid, 0 faults:

row µs/submit base → candidate GPU µs base → candidate
conv2d gaussian 1080p d4 549.5 → 482.2 (−12.2 %) 549.4 → 481.6 (−12.3 %)
conv2d gaussian 1080p d1 796.2 → 726.8 (−8.7 %) 539.8 → 470.9 (−12.8 %)
conv2d bilateral 1080p d4 1111.1 → 1079.9 (−2.8 %) 1110.4 → 1079.5 (−2.8 %)

The other hardware rows below are at the older head e0d35b6aff2b on base e1b5658fe695; the remaining rows at this
head are under Not yet.

J773g (M4 Mac mini, G16G B0), stock kernel 7.3.0-ARCH-m4mainefed1ae3, gpubench-vk 13 (ae90a40). Mesa overlay via
VK_DRIVER_FILES, Vulkan-only release builds. Base e1b5658fe695 (aurora-wip), candidate e0d35b6aff2b.

  • Fedora build with tests: agx_tests 1659/1659 (M5RuntimeLoops.* 12/12), meson test --suite asahi 214/214.
  • Offline G16G drm-shim run of all 84 gpubench-vk plan tests: only the conv2d gaussian and bilateral binaries change
    (both passes). compile.pso is the only other test that differs, and it differs between any two runs of the same
    build (25 to 33 binaries).
  • The same rule changes conv2d on G15S, G17P and G17S under the shim (for example G17S gaussian 2964/3218 →
    2252/2388 bytes), since the pass runs there too.

gpubench, gpubench-vk --test compute.conv2d --param kind=<gaussian|bilateral> --param res=1080p --depths <1,4|4> --warmup-ms 300 --runs 5 --run-ms 250 --timed-runs 3, mirrored base/candidate (B C C B ×2, 4 runs per arm), medians,
all 24 records valid (output CRC matches):

row µs/submit base → candidate GPU µs base → candidate
conv2d gaussian 1080p d4 550.1 → 481.7 (−12.4 %) 549.8 → 481.3 (−12.5 %)
conv2d gaussian 1080p d1 798.7 → 729.9 (−8.6 %) 540.8 → 471.0 (−12.9 %)
conv2d bilateral 1080p d4 1111.1 → 1077.6 (−3.0 %) 1110.8 → 1076.8 (−3.1 %)

Per-arm spread is under 1 % on every row.

An earlier A/B on base 8ac326f31d4f covered two forms of the rule, mirrored, 4 runs per arm, all 36 records valid.
One form caps the factor at the trip bound, which gives conv2d a 2-way main loop. The other is this one:

row µs/submit base cap at bound this change
conv2d gaussian 1080p d4 549.5 492.1 (−10.4 %) 482.2 (−12.2 %)
conv2d gaussian 1080p d1 796.2 741.9 (−6.8 %) 735.3 (−7.6 %); GPU −12.8 %
conv2d bilateral 1080p d4 1111.1 1077.6 (−3.0 %) 1077.6 (−3.0 %)

For comparison, gpubench's Metal lane on this J773g under macOS runs gaussian d4 at 503.0 µs/submit. A lab build
with runtime unrolling off on AGX3B entirely gave conv2d the same code as this change, and the same −12 %. That build
also cost uber divergent +2.4 %, uniform +1.3 %, heavy alu +1.0 % and fp16 gemv GPU time +9.9 %, which is why this is
a bound on the factor and not a switch.

CTS on the candidate at --jobs 1, 4,619 cases: dEQP-VK.compute.pipeline.{basic, zero_initialize_workgroup_memory,
workgroup_memory_explicit_layout, indirect_dispatch, builtin_var}.* and dEQP-VK.spirv_assembly.instruction.compute.
{opphi, loop_control}.*. Result: Pass 3,620, NotSupported 999, Fail 0. 0 kernel fault lines, 0 GPU recoveries, GPU
health clean, 0 failed units.

J700 (MacBook Neo, G17P B1), older head e0d35b6aff2b against e1b5658fe695, stock kernel, mirrored, 4 runs per arm,
all 64 results ok: conv2d gaussian 1080p d1 1563 → 1427 µs/submit (−8.7 %), d4 1389 → 1295 (−6.7 %), bilateral d1/d4
−1.2 %, edge and stereo within 1 %. All of dEQP-VK.compute.* (67,553 cases): 12,943 Pass, 54,610 NotSupported,
0 Fail. 0 GPU faults, 0 recoveries.

Risk: two-way · AGX3B compute shaders (M3 family, M4) and every stage on M5, plus G17P (A18 Pro): runtime-unrolled
loops with a constant bound and a provably non-negative start now get a smaller factor, or stay rolled. Only conv2d
changes among the gpubench plan tests.

Not yet:

  • Rows at this head: J516s (M3 Pro, G15S), J700 (A18 Pro, G17P) and J714s (M5 Pro). The shim shows the same conv2d
    change on those GPUs.

agx_nir_unroll_runtime_loops() picks the iterations per test from the body
size alone. A loop striped over the workgroup with a constant bound, such as
the halo fill of a tiled convolution (x = local_id.x; x < 32; x += 16), runs
at most twice in every invocation. Its 8-way main loop never runs, yet its
guard and eight copies of the body still cost code size and issue time.

Bound the trip count of a counting-up loop with a constant bound whose start
cannot be negative, which the range analysis shows for a local invocation
index. Then lower the factor until the main loop could run at least twice.
Loops with no such bound keep the factor they had.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 27, 2026 05:26
aurora-wip now also counts loops that step by a loop-invariant runtime
value (step 0, with the step in step_def) and loops whose exit test reads
iv + step_def (offset_is_step). max_trips() divides by the constant step
and reads the constant offset, so it now gives no bound for both.

The conflict was only the placement of the new helpers before
choose_factor(): both sides are kept.

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

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

Blockers:

  1. src/asahi/compiler/agx_nir_unroll_runtime_loops.c max_trips(): the branch conflicts with aurora-wip (DIRTY), and once it is merged onto aurora-wip 65866bbd5bb3 the function divides by cl->step, which is 0 for the runtime-step loops that #1183 added (step_def set). A runtime-step read-only loop with a constant bound and a start of 0 hits DIV_ROUND_UP(room, 0). On x86 that is SIGFPE. On arm64 the divide returns 0, so trips comes out as 0 (or 1 for a rotated loop), and the loop quietly stays rolled, which undoes #1183 for those loops. The probe test below passes on aurora-wip and dies with SIGFPE in agx_tests on the union merge. Fix: in max_trips(), return UINT64_MAX when cl->step_def.def is set or cl->step == 0 (your "Not yet" line describes this bail). Also skip the offset_is_step form, because offset does not carry the test's offset there. Please merge aurora-wip, add that bail and this case to TripBoundLimitsFactor, then push once.

Checked: I read the whole diff. I went through the bound in max_trips() for both the signed and unsigned compares. A start of at most INT32_MAX (signed) is non-negative. For unsigned compares with a negative offset, the starts below -offset wrap on the first test and exit at once, so the start-0 count is still an upper bound. The wrap check at the exit test covers every start up to start_max, and the +1 for a rotated loop is right. A wrong bound would only change the factor, not the results: the remainder loop still runs every iteration. Local drm-shim gate (x86, asahi + poly): aurora-wip 65866bbd gives 336/336. The union merge (conflict = two adjacent new functions, both kept) builds, and TripBoundLimitsFactor passes, but agx_tests dies with SIGFPE in the probe. Non-blocking: the 8x -> 1 rule also changes conv2d on G15S, G17P and G17S, so rows are owed there.
Row owed: J516s (G15S), J700 (G17P), J714s (M5 Pro), at the merged head.

Evidence

Probe (appended to src/asahi/compiler/test/test-m5-runtime-loops.cpp): a top-tested for (off = 0; off < 4096; off += push_constant) read-only reduction, with agx_nir_unroll_runtime_loops(s, 8, 320, &widest) expected to unroll by 8.

  • aurora-wip 65866bb + probe: [ OK ] M5RuntimeLoops.ReviewProbeRuntimeStepConstBound, gate 336/336
  • aurora-wip + e0d35b6 (union resolution) + probe: agx_tests ... killed by signal 8 SIGFPE, last test started M5RuntimeLoops.ReviewProbeRuntimeStepConstBound; TripBoundLimitsFactor OK

Logs: buildbox traces/gpu/20261127-merge-blitz-rows/review3-1204/.

@DjDeveloperr

DjDeveloperr commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

J700 (MacBook Neo, G17P B1) at 3288d18337b4, against base 65866bbd5bb3: PASS. compute.conv2d gaussian is about 8–9 % faster, the controls are unchanged, CRCs are clean, and compute CTS shows 0 Fail.

  • Setup:

    • Stock kernel 7.3.0-41 with no reboot, Vulkan overlays through VK_DRIVER_FILES.
    • The drivers are the M4 lane's prebuilt kit for this head. The candidate is built from 7d7cc368fbe1; 3288d18 only adds test-m5-runtime-loops.cpp.
    • vulkaninfo on the Apple A18 Pro showed git-65866bbd5b for the base and git-7d7cc368fb for the candidate.
    • Shader cache off, MAX_RECOVERIES=0.
  • run-pr1204.sh (mirrored B C C B B C C B, 4 runs per arm, 0 invalid records):

    row us per submit GPU us
    conv2d gaussian d1 1396.7 → 1280.2, −8.3 % 1190.2 → 1078.6, −9.4 %
    conv2d gaussian d4 1269.2 → 1150.8, −9.3 % 1303.8 → 1193.1, −8.5 %
    conv2d bilateral d4 2673.9 → 2619.6, −2.0 % 2717.6 → 2682.1, −1.3 %
  • gpubench-vk ABBA+BAAB with controls, same boot (b h h b h b b h). All 64 results ok, with 0 invalid checks. Medians of us per submit:

    • conv2d gaussian: d1 −8.1 % (1524 → 1401), d4 −7.7 % (1366 → 1261). Every head pass is faster than every base pass.
    • conv2d bilateral: d1 −1.2 %, d4 −1.4 %.
    • edge (sobel): d1 −1.4 %, d4 −1.1 %.
    • stereo: d1 and d4 both 0.0 %.
  • Vulkan CTS on the head: all of dEQP-VK.compute.*, 67,553 cases (pipeline, shader_object_spirv, shader_object_binary): 12,943 Pass, 54,610 NotSupported, 0 Fail.

  • Health: 0 GPU faults, 0 recoveries, 0 AVC denials, 0 failed units and 0 core dumps. stock.sh was identical before and after.

  • Earlier head: the row at e0d35b6aff2b gave the same picture: gaussian −8.7 % (d1) and −6.7 % (d4), compute CTS 67,553 with 0 Fail.

Loops that step by a loop-invariant runtime value carry step 0 and the
step in step_def. max_trips() gives no bound for them, so the factor stays
what the body size allows: 8 for this read-only reduction, with a constant
bound of 4096 or 64.

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

Copy link
Copy Markdown
Author

J714s (MacBook Pro 14" M5 Pro, G17S B1) row at 3288d18337b4: PASS. conv2d gaussian is 15 % faster on the GPU, and no compute CTS failures.

Setup:

  • The prebuilt overlay kits for this head:
    • base aurora-wip 65866bbd5bb3;
    • candidate 3288d18337b4, the driver of 7d7cc368fbe1 (3288d18 adds only a unit test).
  • Loaded through VK_DRIVER_FILES; nothing installed. driverInfo reads git-65866bbd5b and git-7d7cc368fb.
  • The default kernel 7.3.0-m5-cdirect16, headless and silent.
  • run-pr1204.sh unchanged, with GB= pointing at the M5's gpubench-vk.
    • Each row is compute.conv2d at 1080p, 5 × 250 ms runs plus 3 timed runs.
    • The order is base cand cand base base cand cand base, 4 runs per arm.
    • MESA_SHADER_CACHE_DISABLE=true.

1. conv2d (median of the per-run medians, 4 runs per arm; invalid records 0):

Row Base GPU µs Cand GPU µs Δ GPU Base µs/submit Cand µs/submit Δ
gaussian d1 239.0 202.8 −15.2 % 365.8 328.5 −10.2 %
gaussian d4 239.1 202.5 −15.3 % 239.3 202.6 −15.4 %
bilateral d4 450.5 426.6 −5.3 % 450.5 428.2 −4.9 %
  • Per-run spread: gaussian is tight on both arms (base 238.2–239.5 µs, cand 202.4–202.9 µs, except one 244.4 / 207.5 pair at d4). Bilateral has one slow run on each arm (493.8 base, 461.7 cand); the other three are 450.0–450.6 vs 420.5–431.8 µs.

2. Vulkan CTS on the candidate (deqp-vk 1.4.7.1, 16 jobs): every 5th dEQP-VK.compute.* case, 13,511 cases. 3,039 Pass, 10,472 NotSupported, 0 Fail.

3. Kernel log: no lost device, GPU fault or WARN during this row's runs. The GPU was online, with 0 crashed and 0 quarantined owners, before and after.

@DjDeveloperr

Copy link
Copy Markdown
Author

J516s (MacBook Pro 16" M3 Pro, G15S) row at 3288d18337b4: PASS. conv2d gaussian is 5.5–9.9 % faster. Kernel: linux#3353 456bce008249, one-shot boot. Base: aurora-wip 65866bbd5bb3.

  • gpubench-vk compute.conv2d at 1080p, mirrored B C C B B C C B, 4 runs per arm (median µs per submission, base → this head):
    • gaussian d1: 585.7 → 553.7 (−5.5 %); GPU time 375.5 → 338.5 (−9.8 %);
    • gaussian d4: 387.9 → 349.4 (−9.9 %); GPU time −10.0 %;
    • bilateral d4: 764.5 → 750.8 (−1.8 %).
    • 0 invalid records.
    • These runs used the prebuilt release overlays (cand driver 7d7cc368fbe1).
  • CTS at this head, one deqp-vk process, isolated kits built on the Mac:
    • 4,609 compute.pipeline cases (workgroup_memory_explicit_layout, zero_initialize_workgroup_memory, basic, indirect_dispatch, builtin_var, 64b_indexing, device_group): 3,606 Pass, 1,003 NotSupported, 0 Fail;
    • 600 sampled compute.pipeline.cooperative_matrix cases: 21 Pass, 579 NotSupported, 0 Fail.
  • 0 GPU faults or recoveries, 0 failed units.

@RyanTheTide RyanTheTide 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: FINISH, then merge at 3288d18337b4d687d211cce1abd37d80dec359d2

Blockers: none. The max_trips() divide by zero from my review at e0d35b6aff2b is fixed. Still open before merge:

  1. The J516s (G15S) row listed in my last review. The 8x -> 1 rule changes conv2d there too. The J700 and J714s rows are at this head: 7d7cc368fbe1 has the same driver, and 3288d18 only adds a unit test.
  2. Ordering with #1202, which I approved at 3ea292e40fc1. Both PRs change the tail of choose_factor() in agx_nir_unroll_runtime_loops.c, and they conflict there: #1202 turns > max_instrs into > budget, and this PR adds the trip clamp in front of that return. Whichever lands second needs a merge of aurora-wip (keep the clamp, then return against budget) and a short conv2d rerun at the merged head. Since #1202 only needs its M5 row, I'd land #1202 first and forward-merge this one.

Checked:

  • The bail. max_trips() now returns UINT64_MAX for cl->step <= 0 and for cl->offset_is_step. In analyze_loop(), a runtime step always sets step = 0 with step_def (around line 500), and a constant step of 0 is refused at line 509. So step <= 0 catches every runtime-step loop and nothing else reaches the divide with step 0. Skipping offset_is_step also removes the case where offset did not hold the test's offset.
  • The new test. runtime_step_factor() is the push-constant-step reduction from my probe, run with bounds 4096 and 64 and expecting factor 8.
  • The test is real. With the bail weakened to cl->step < 0, agx_tests --gtest_filter=M5RuntimeLoops.TripBoundLimitsFactor dies with SIGFPE.
  • Merge. The head merges cleanly into aurora-wip 5ab97d0f2c60 and passes the gate there.
Evidence

Local drm-shim gate (x86 clang, release + asserts, asahi + poly):

  • aurora-wip 5ab97d0f2c60: Ok 338, Fail 0
  • aurora-wip 5ab97d0f2c60 + 3288d18337b4 (clean merge): Ok 338, Fail 0
  • same tree with max_trips() cl->step <= 0 -> cl->step < 0: agx_tests --gtest_filter=M5RuntimeLoops.TripBoundLimitsFactor: Floating point exception (core dumped), exit 136

git merge-tree of 3288d18337b4 onto aurora-wip + #1202 (3ea292e40fc1): CONFLICT in agx_nir_unroll_runtime_loops.c, choose_factor() return.

@RyanTheTide
RyanTheTide merged commit a97571c into aurora-wip Nov 27, 2026
DjDeveloperr added a commit that referenced this pull request Nov 27, 2026
…20261126

Resolve choose_factor() conflict against #1204 (a97571c) in
agx_nir_unroll_runtime_loops.c by keeping #1204's max_trips() bound and
ending with #1202's budget check. Also brace the EXPECT_EQ in
test-opt-licm.cpp to silence -Wdangling-else.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@RyanTheTide
RyanTheTide deleted the perf/agx-unroll-trip-bound branch November 27, 2026 13:20
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.

2 participants