Repository navigation
asahi: unswitch counted loops on overflow-safe affine bounds - #656
Conversation
|
I worked through the guard arithmetic. For the iterations it models it holds. The value is built only from mod-2^32 ring ops, the 64-bit product check plus
Side effects look fine. Barriers, subgroup ops, derivatives, demote and calls are rejected, each lane runs exactly one copy, and the rematerialized guard inputs are pure or already run before the body. Assisted-by: LLM (Claude:Opus 5.5) |
065bbf9 to
28e4e0d
Compare
|
@RyanTheTide Thanks for catching both edge-case bugs and reviewing the guard math! All items addressed in head
Assisted-by: LLM (Antigravity:Gemini 4 Neon) |
Signed-off-by: Diljit Singh <hey@djdev.me>
28e4e0d to
ab0909b
Compare
|
Rebased cleanly onto
|
satchlj
left a comment
There was a problem hiding this comment.
Verdict at ab0909be8bd2
APPROVE — Audited all 5 changed files, verified non-layout asahi unit tests (177/177 PASS, including agx_unswitch_affine_bounds), and ran targeted loop/compute/robustness Vulkan CTS on live Apple M5 Max (904 passed, 0 failed, 0 GPU faults).
Blockers
None.
Checked
- Audited all 5 changed files (
src/asahi/compiler/agx_compile.c,src/asahi/compiler/agx_nir.h,src/asahi/compiler/agx_nir_opt_unswitch_affine_bounds.c,src/asahi/compiler/meson.build,src/asahi/compiler/test/test-agx-unswitch-affine-bounds.c):- Loop matching (
match_simple_loop) restricts to single-exit 32-bit uniform-bounded0-initialized unit-stepped loops (top or bottom exit), rejects continue constructs,dont_unroll, barriers, subgroup/quadgroup/lane-dependent intrinsics, implicit derivatives, calls/derefs, and non-ALU/const/undef/phi live-outs (loop_tree_safe). - Affine analysis and invariant rematerialization restrict to single-loop affine expressions and safe invariant/reorderable header loads.
- Pre-loop guard math proves exact 32-bit signed non-overflow across
iv in [0, bound - 1]: requiresbound > 0,limit > 0(soult(val, limit)is strictly equivalent to0 <= (int32_t)val < (int32_t)limit), checksimul_high(bound - 1, step) == (span >> 31)andspan != INT32_MINso-spancannot overflow, and verifiesult(base - lo_req, width)for bothstep >= 0andstep < 0. - Live-out merge PHIs are constructed before folding matched checks in the cloned
thenbranch (so folded live-out checks cleanly rewrite thethenPHI source to the replacement constant), the fallbackelseloop is markednir_loop_control_dont_unroll, and sibling loops continue traversal cleanly afternir_cf_extract. - Unit tests (
agx_unswitch_affine_bounds) cover basic unswitching, live-out folded checks, sibling loops, negative loop/header shapes, and guard overflow/unsigned-limit edge cases.
- Loop matching (
- Untested scope:
asahi_layout_tests(pre-existing GCC 16 designated-initializer build issue insrc/asahi/layout/tests/test-layout.cpp), AGX3B / AGX2 hardware execution (runtime validation performed on Apple M5 Max / AGX3C), and full VK-GL-CTS sweep.
Evidence
- Build & unit suite (isolated worktree at
ab0909be8bd2):- Non-layout
asahiMeson suite:177/177PASS (0fail), includingagx_unswitch_affine_bounds.
- Non-layout
- Live Vulkan CTS on Apple M5 Max (
G17C B1, Linux 7.3,MESA_SHADER_CACHE_DISABLE=1,0new kernel GPU faults):dEQP-VK.glsl.loops.*:624/624PASSdEQP-VK.compute.pipeline.basic.*:80/80supported PASS (1N/S,0fail)dEQP-VK.robustness.buffer_access.through_pointers.compute.*:200/200PASS- Total:
904passed,0failed,0GPU faults.
Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
What: Add agx_nir_opt_unswitch_affine_bounds to unswitch counted loops on overflow-safe affine bounds with dedicated unit tests.
Tested: Rebased onto aurora-wip (01871ed), assert-ON Fedora asahi CI 185/185 PASS (0 Fail, including agx_unswitch_affine_bounds), plus prior G17P B1 & M5 Pro verification.
Risk: two-way · AGX2/AGX3 (self-contained NIR pass)
Not yet: None.
Details
Unswitches counted compute loops whose body contains affine range checks (
0 <= base + iv*step < limit, signed or unsigned) into a guard-protected fast copy with the proven range checks folded to constants and an unmodified border copy.Changes in Round 2 (head
28e4e0d70dc)nir_imm_boolat the start of thethenblock sonir_def_replace()updates both in-loop uses and live-out phi sources cleanly.in_top_exit_header()and reject checks residing innir_loop_first_block(loop)when!bottom_exit(for both outer and inner loops) so exit-tripiv == boundevaluations are never mis-folded.(g17p || m5) && !g18_scanschip gate inagx_compile.c; the pass is target-independent NIR and now runs unconditionally.try_unswitch_loop()returns the insertednir_if *unswitchandprocess_cf_list()advancesnext = unswitch->cf_node.node.nextso subsequent sibling loops are visited.safe_invariant_def_cached()andanalyze_affine_cached()to keep shared-DAG complexity O(N).test-agx-unswitch-affine-bounds.cnow checks that the fast loop dropsiltwhile the border loop keepsilt, validates live-out folded checks, tests sibling loops, verifies negative shapes (init != 0,step != 1, top-exit header check), and constant-folds edge guard checks ((n-1)*stepoverflow and unsignedlimit >= 2^31).Verification (head
28e4e0d70dc)156/0GREEN (agx_unswitch_affine_boundspasses all new positive, negative, live-out, sibling, and overflow-guard tests).G17P B1) CTS (combined stack1ab06ac48f5):dEQP-VK.glsl.loops.*(624/624 Pass),dEQP-VK.ssbo.*(12,162 Pass / 0 Fail),dEQP-VK.compute.*(12,607 Pass / 0 Fail).G17S B1) validation: Tested alongside#655+#654on M5 Pro with0validation errors and0CTS failures (NIR_DEBUG=validate).