Repository navigation
agx: don't unroll runtime loops whose main loop could run at most once - #1204
Conversation
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>
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
left a comment
There was a problem hiding this comment.
Verdict: FINISH, then merge at e0d35b6aff2b8204cc109d2b65436572864b7f50
Blockers:
src/asahi/compiler/agx_nir_unroll_runtime_loops.cmax_trips(): the branch conflicts with aurora-wip (DIRTY), and once it is merged onto aurora-wip65866bbd5bb3the function divides bycl->step, which is 0 for the runtime-step loops that #1183 added (step_defset). A runtime-step read-only loop with a constant bound and a start of 0 hitsDIV_ROUND_UP(room, 0). On x86 that is SIGFPE. On arm64 the divide returns 0, sotripscomes 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 inagx_testson the union merge. Fix: inmax_trips(), returnUINT64_MAXwhencl->step_def.defis set orcl->step == 0(your "Not yet" line describes this bail). Also skip theoffset_is_stepform, becauseoffsetdoes not carry the test's offset there. Please merge aurora-wip, add that bail and this case toTripBoundLimitsFactor, 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 startedM5RuntimeLoops.ReviewProbeRuntimeStepConstBound;TripBoundLimitsFactorOK
Logs: buildbox traces/gpu/20261127-merge-blitz-rows/review3-1204/.
|
J700 (MacBook Neo, G17P B1) at
|
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>
|
J714s (MacBook Pro 14" M5 Pro, G17S B1) row at Setup:
1. conv2d (median of the per-run medians, 4 runs per arm; invalid records 0):
2. Vulkan CTS on the candidate ( 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. |
|
J516s (MacBook Pro 16" M3 Pro, G15S) row at
|
RyanTheTide
left a comment
There was a problem hiding this comment.
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:
- 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:
7d7cc368fbe1has the same driver, and3288d18only adds a unit test. - Ordering with #1202, which I approved at
3ea292e40fc1. Both PRs change the tail ofchoose_factor()inagx_nir_unroll_runtime_loops.c, and they conflict there: #1202 turns> max_instrsinto> 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 againstbudget) 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 returnsUINT64_MAXforcl->step <= 0and forcl->offset_is_step. Inanalyze_loop(), a runtime step always setsstep = 0withstep_def(around line 500), and a constant step of 0 is refused at line 509. Sostep <= 0catches every runtime-step loop and nothing else reaches the divide with step 0. Skippingoffset_is_stepalso removes the case whereoffsetdid 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.TripBoundLimitsFactordies with SIGFPE. - Merge. The head merges cleanly into aurora-wip
5ab97d0f2c60and 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.
What
agx_nir_unroll_runtime_loops()picks how many iterations a loop runs per test from the body size alone. gpubench-vkcompute.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:
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:Tested
Head
3288d18337b4:7d7cc368fbe1merges aurora-wip65866bbd5bb3. The aurora-wip side adds loops that step by a runtime value(
step0, the step instep_def) and exit tests that readiv + step_def(offset_is_step).max_trips()givesno bound for both: it divides by the constant step and reads the constant offset.
3288d18337b4adds that case toM5RuntimeLoops.TripBoundLimitsFactor: a push-constant-step read-only reductionwith constant bounds of 4096 and 64 keeps factor 8.
meson test --suite asahi214/214,M5RuntimeLoops.*14/14. With the bailremoved,
agx_testsdies inTripBoundLimitsFactor(SIGFPE), so the test covers it.65866bbd5bb3: only the conv2d gaussian andbilateral 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;3288d18337b4adds only the test) against aurora-wip65866bbd5bb3, stockkernel
7.3.0-ARCH-m4mainefed1ae3, mirrored B C C B ×2 (4 runs per arm), all records valid, 0 faults:The other hardware rows below are at the older head
e0d35b6aff2bon basee1b5658fe695; the remaining rows at thishead are under Not yet.
J773g (M4 Mac mini, G16G B0), stock kernel
7.3.0-ARCH-m4mainefed1ae3, gpubench-vk 13 (ae90a40). Mesa overlay viaVK_DRIVER_FILES, Vulkan-only release builds. Basee1b5658fe695(aurora-wip), candidatee0d35b6aff2b.agx_tests1659/1659 (M5RuntimeLoops.*12/12),meson test --suite asahi214/214.(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).
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):
Per-arm spread is under 1 % on every row.
An earlier A/B on base
8ac326f31d4fcovered 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:
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
e0d35b6aff2bagainste1b5658fe695, 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:
change on those GPUs.