Skip to content

borealis: derive general dispatch-time loop bounds for compute submit packing - #1180

Merged
RyanTheTide merged 3 commits into
aurora-silicon:aurora-wipfrom
oreater:pr/borealis-dispatch-loop-bounds
Dec 1, 2026
Merged

RyanTheTide merged 3 commits into
aurora-silicon:aurora-wipfrom
oreater:pr/borealis-dispatch-loop-bounds

Conversation

@oreater

@oreater oreater commented Nov 27, 2026

Copy link
Copy Markdown

What: Derive sound, fail-closed dispatch-time loop bounds (hk_nir_loop_bound.h, hk_pack_loop_bound.h) for general compute shaders with push-constant, grid-stride, halving, countdown, sliding-window stencil, additive-limit, and bottom-tested IV loops, integrating with work_ops / hk_pack_eval_effective_instrs_dispatch() so multi-CB compute graphs containing bounded user loops pack into a single CDM command instead of splitting at every looping dispatch.

Tested: J716c Apple M5 Max at 5342bc5a53c: 334/334 Clang debug CI tests (--suite asahi --suite poly) PASS, 64-CB push-constant-bounded compute graph collapses 64 -> 1 CDM command (runs=0 -> 1) and wall-clock time -81.2% (1.131 -> 0.212 ms, 5.33x) with exact arithmetic readback, 159/160 Vulkan CTS PASS (0 fail, 0 diffs), 0 kernel errors.

Risk: two-way door · all Macs with compute submit packing — A plain revert restores pack_loops = true for general user looping compute shaders. Every new op (PUSH_U32, PUSH_I32_POS, PUSH_I32_NONNEG, GRID_GROUPS, DIV_CONST, LOG2_PLUS1, ASSERT_I32, CEIL_DIV_U32, CEIL_DIV_I32) fails closed on zero step, negative non-neg inputs, out-of-bounds push offset, 32-bit wrap hazards, indirect dispatches, non-zero base_group, or SSBO-dependent bounds, falling back to pack_loops = true.

Not yet: SSBO/UBO-bounded loops, indirect dispatches, and non-zero base_group dispatches conservatively remain unbounded (pack_loops = true). Hardware-tested on J716c Apple M5 Max.

Details and verification evidence

Summary

  • src/asahi/vulkan/hk_pack_loop_bound.h:
    • Add 9 fail-closed dispatch-time ops (PUSH_U32, PUSH_I32_POS, PUSH_I32_NONNEG, GRID_GROUPS, DIV_CONST, LOG2_PLUS1, ASSERT_I32, CEIL_DIV_U32, CEIL_DIV_I32) and *_dispatch() evaluators (hk_pack_eval_ops_dispatch(), hk_pack_eval_loop_bound_dispatch(), hk_pack_eval_work_bound_dispatch(), hk_pack_eval_effective_instrs_dispatch()), preserving existing entry points and CF-weighted work_ops support.
  • src/asahi/vulkan/hk_nir_loop_bound.h:
    • Extend hk_nir_lb_loop_trips() to recognize top- and bottom-tested IV exits, counting-up and counting-down IVs, push-constant and dispatch-grid expressions, additive non-negative limit expressions, bit-smeared next_pow2(C) bounds, lowered halving updates (floor(log2(init)) + 1), and signed sliding-window/stencil clamps (including pre-peephole if-phi min/max).
  • src/asahi/vulkan/hk_shader.c, hk_cmd_dispatch.c:
    • Analyze non-internal compute shaders in hk_lower_nir() before descriptor lowering (while push constants and grid intrinsics remain canonical), propagate into byte-stencil / 2D-stencil / compute-opt variants, and evaluate via hk_pack_eval_effective_instrs_dispatch() on direct zero-base dispatches with grid.count <= INT32_MAX.
  • src/asahi/vulkan/tests/test-hk-pack.c, test-hk-compute-pair.c:
    • Add unit tests for all new evaluator ops, wrap/zero/negative/OOB fail-closed checks, 2D grid-stride + push-bounded loops, softmax bit-smear countdown/halving loops, sliding-window stencil if-phi clamps, bottom-tested additive limits, and SSBO-bounded fail-closed rejection.

Build & Artifacts

  • Host: J716c Apple M5 Max (G17C B1), Fedora Remix 44, kernel 7.3.0-40.j716c19.fc44.aurora.aarch64
  • Base (origin/aurora-wip 41fe336dffa): libvulkan_asahi.so SHA-256 0ea0ee4efd3bb78ef2eab7770e5d2bf9dacd59a4cd4f8809729941acff43e476
  • Head (5342bc5a53c): libvulkan_asahi.so SHA-256 1e8a77b75cae55fdd5e0d8f6eef1a65051acd9adf688dfcea9829244a0f09def

Verification

  • CI-matching Clang 22.1.8 debugoptimized (--suite asahi --suite poly, Gallium + Vulkan + drm-shim, assertions enabled): 334/334 PASS, 0 FAIL.
  • Live M5 Max A/B (strict 3-round ABBA = 6 runs per arm, MESA_SHADER_CACHE_DISABLE=1, 64-CB push-constant-bounded compute loop graph × 50 iters, exact SSBO value 2550109440 identical across all 12 runs):
Workload (64-CB push-constant loop graph) Base median Head median Delta
CDM packed runs / commands (HK_PACK_CDM_TRACE=1) 0 runs (64 cmds) 1 run (1 cmd) -98.4% cmds
Wall-clock time per submit 1.1307 ms 0.2121 ms -81.2% (5.33x)
  • Vulkan CTS (deqp-vk, base vs head, MESA_SHADER_CACHE_DISABLE=1):
    • 160-case slice across dEQP-VK.compute.pipeline.basic.* + dEQP-VK.api.command_buffers.*: 159/160 PASS, 0 FAIL, 1 NotSupported (0 status diffs vs base).
  • Host health: SELinux=Enforcing, 0 DRM/asahi/GPU kernel errors.

… packing

Previously, compute submit packing only computed NIR-derived loop bounds
for driver-synthesized compute-pair alternates, leaving general user
compute shaders with push-constant, grid-stride, halving, countdown,
stencil, or bottom-tested loops marked unbounded (pack_loops = true,
limiting packets to one looping stream).

- Extend hk_pack_loop_bound.h with fail-closed dispatch-time ops
  (PUSH_U32, PUSH_I32_POS, PUSH_I32_NONNEG, GRID_GROUPS, DIV_CONST,
  LOG2_PLUS1, ASSERT_I32, CEIL_DIV_U32, CEIL_DIV_I32) and *_dispatch()
  evaluators covering both ops[] and CF-weighted work_ops[].
- Extend hk_nir_loop_bound.h to analyze general compute loops: top- and
  bottom-tested IV exits, counting-up and counting-down IVs, push-constant
  and grid-dimension bounds, additive limit expressions, bit-smeared
  next_pow2(C) bounds, lowered halving updates, and signed sliding-window
  stencil clamps (including pre-peephole if-phi min/max).
- Analyze non-internal compute shaders in hk_lower_nir() before descriptor
  lowering while push-constant and grid intrinsics remain canonical (and
  propagate into stencil/opt compute variants), and evaluate via
  hk_pack_eval_effective_instrs_dispatch() on direct zero-base dispatches.
- Add unit tests in test-hk-pack and test-hk-compute-pair covering all
  ops, NIR patterns, additive bottom-tested exits, and fail-closed
  overflow/negative/SSBO cases.

Signed-off-by: oreater <145754695+oreater@users.noreply.github.com>
@oreater
oreater requested review from a team and DjDeveloperr as code owners November 27, 2026 00:23
@RyanTheTide

Copy link
Copy Markdown

This branch no longer merges into current aurora-wip (d2e883ab2e3): git merge-tree reports a conflict in src/asahi/vulkan/tests/test-hk-pack.c. Could you merge aurora-wip into the branch (a normal merge, no rebase or force push) and re-run the asahi + poly suites at the new head? I will review the content once it merges cleanly. Several open queue/pack PRs touch the same file, so resolving against the tip as it is now will save a second round.

@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: HOLD at 5342bc5a53ce6

Blockers: two of the new bounds come out smaller than the real trip count. With a bound that is too small, hk_pack_eval_effective_instrs_dispatch() (hk_cmd_dispatch.c:5051-5062) lets the packer put more work into one CDM command than the static-work budget allows, and that breaks the "sound, fail-closed" claim.

  1. src/asahi/vulkan/hk_nir_loop_bound.h:1365-1372 together with :1474-1497. The new penultimate-if path accepts a break-if just before an empty latch, which makes it a bottom test. When the condition compares the header phi itself and not the incremented value, bottom_tested stays false, so the loop gets the top-tested count ceil((lim - init) / step). But everything ahead of that if runs trips + 1 times. The edge walk (:1734-1740) and the work walk (:1795-1801) add only the header block once more, so an inner loop or other work in the body is undercounted by one whole outer iteration.
    • Input: int i = 0; do { for (int j = 0; j < 1000; j++) { ... } } while (i++ < n);. In NIR (after vars_to_ssa, copy_prop, dce) this is: header phi i, the inner loop, i' = i + 1; if (i >= n) break;, then an empty latch.
    • Result at the head: n = 0 gives bound 1, real 1001; n = 4 gives bound 4005, real 5005. It is the same with n from a push constant.
    • Fix: on the penultimate-if path, when the compared value is the phi, count trips + 1. Or accept that position only when bottom_tested is true. Add the shape above as a regression test.
  2. src/asahi/vulkan/hk_nir_loop_bound.h:814-824, reached from the count-up dynamic limit at :1657-1658. That call passes require_nonneg = false, so each imul/amul factor is emitted as PUSH_I32_POS, which clamps a negative value to 0. A negative constant factor is clamped the same way. But the product of two negatives is positive in the shader.
    • Input: for (int i = 0; i < pc.a * pc.b; i++) with a = b = -1000. The shader runs 1,000,000 iterations; the bound evaluates to 1. pc.n * -4 with a negative n breaks the same way.
    • Fix: in the imul/amul case, recurse with require_nonneg = true (PUSH_I32_NONNEG, which fails closed on a negative input), and reject negative constant factors there. Add a test with two negative factors.

Checked: Read the whole diff: hk_nir_loop_bound.h, hk_pack_loop_bound.h, hk_shader.c, the hk_cmd_dispatch.c hunk and both test files. Things that are fine:

  • Push offsets match lower_load_push_constant's layout of root.push, and the bound is read after the im2col push has been restored.
  • Indirect dispatches, in-thread dispatches and a non-zero base_group all fail closed. Grid dimensions are capped at INT32_MAX.
  • The CEIL_DIV_I32/CEIL_DIV_U32 wrap checks, the limit >= 0 check on countdown loops, and the requirement that the step is non-negative and non-zero are correct.
  • A loop with an extra continue fails closed (the IV phi must have exactly two sources). Extra breaks only end a loop sooner, so they are safe.
  • The halving loop is sound for every loop that terminates.
  • No chip, board or firmware gates.

The new tests check exact values of new functions, so they cannot pass without the change, but neither shape above is covered. Gate: a scratch merge into aurora-wip (the conflict is only the appended tests in tests/test-hk-pack.c) passes 336/336. Those suites do not exercise these shapes.

Also owed before merge: the only row is on the J716c M5 Max. This is shared code for every Mac with compute packing, so it needs rows at the merged head on G17P (Neo; #1168 just showed G17P regressions that M5 rows miss) and on G13 or G14, plus the regression Mac.

Non-blocking (pre-existing or minor):

  • The constant count-up path, already in the base, does not check that limit - 1 + step stays within INT32_MAX. The IV can wrap and run about twice the computed trips.
  • A bottom-tested dynamic loop whose start is above the limit can wrap once on init + step (at most about one extra trip).
  • orig_groups is taken before hk_select_tensor_op() and hk_select_identity_copy4() rewrite grid. Consider asserting that GRID_GROUPS is only used when selected == s.
  • Landing order: this conflicts with #1179 only in tests/test-hk-pack.c. Either order is fine.
Evidence

Probe: two NIR-builder cases added locally to test-hk-compute-pair.c at 5342bc5a53ce6 (not pushed). Each builds the shader, runs hk_nir_compute_pack_loop_bound() and then hk_pack_eval_loop_bound_dispatch(), and compares the result with the real back-edge count:

PROBE penult push=0 n=0: analyzed=1 valid=1 eval=1 bound=1 real=1001 UNSOUND
PROBE penult push=0 n=4: analyzed=1 valid=1 eval=1 bound=4005 real=5005 UNSOUND
PROBE penult push=1 n=0: analyzed=1 valid=1 eval=1 bound=1 real=1001 UNSOUND
PROBE penult push=1 n=4: analyzed=1 valid=1 eval=1 bound=4005 real=5005 UNSOUND
PROBE imul a=100 b=100: analyzed=1 eval=1 bound=10001 real=10001 ok
PROBE imul a=-1000 b=-1000: analyzed=1 eval=1 bound=1 real=1000001 UNSOUND

Local x86 drm-shim gate (docker aurora/hk6356-mesa-x86-tests:local, meson test --suite asahi --suite poly): base aurora-wip fa8970cda14f 336/0, and the scratch merge of 5342bc5a53ce6 into it 336/0.

@RyanTheTide

Copy link
Copy Markdown

@oreater, merge-train check-in. Four of your PRs landed today (#1162, #1176, #1179, #1187; we forward-merged aurora-wip into them). This one still has the blocking review from 11-27 07:3xZ and conflicts with aurora-wip. Could you address the listed items and merge aurora-wip in (normal merge, no force push)? If you'd rather a maintainer carry it, say so and we'll do the follow-up commits on your branch.

Reject late exits on the old induction value when an inner body has already executed. Require nonnegative factors before bounding a signed product. Cover both cases in the compute-pair fixture.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Carry the loop budget through the current NIR lowering alongside deferred minimum LOD. Retain the landed compute-pair and packet tests as well as the dispatch-bound cases.

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.

Re-review at 3e9645a (fix commit 3e9645a on top of the aurora-wip merges). Both blockers from my 5342bc5 review are fixed, fail-closed:

  1. Late break on the old induction value: the penultimate-if position now sets late_break, the canonical path skips it unless bottom_tested, and the halving path rejects a header-phi compare there. The do { inner loop } while (i++ < n) shape now yields no bound instead of the top-tested count, and late_break_old_phi covers it.
  2. Signed products: imul/amul recurse with require_nonneg = true, so a push factor becomes PUSH_I32_NONNEG and a negative constant factor returns -1. signed_product_limit checks that 45 bounds to >= 20 and -1000-1000 fails to evaluate.
    Other paths are fine: ishl and constant division can only make a negative limit more negative, and a clamp to 0 is still an upper bound for a count-up loop.
    Still open before merge: the PR is DIRTY against aurora-wip and needs its exact-head hardware row. This approval is for the code only.

@RyanTheTide

Copy link
Copy Markdown

Note on my approval: GitHub recorded it at the live head cd80338, not 3e9645a. Between them there are only aurora-wip forward merges: all 118 non-merge commits in 3e9645a..cd80338 are already on aurora-wip, and hk_nir_loop_bound.h and hk_pack_loop_bound.h are byte-identical. Remaining before merge: a hardware row at cd80338. The J716c row in the body is at 5342bc5, which predates the fail-closed fixes.

@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr, code-owner heads-up: App approval at cd8033880ef1 from a different lane. This PR goes into the merge train's next combined A/B (aurora-wip 84ba0bee + #570 #1128 #1180 #1184 #1260 #1266 #1424 #1428 #1430 #1435) 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.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Merge readiness at cd803388: ready to merge

Check Detail
CI at head ok no CI for this change (the hardware row is the gate)
Review ok approved by RyanTheTide
Requested changes ok none
Hardware row at head ok comment at cd803388
Conflicts ok none
PR text ok Risk: two-way · all Macs with compute submit packing — A plain revert restores packloops = true for general user looping compute shaders. Every new op (PUSHU32, PUSHI32POS, PUSHI32NONNEG, GRIDGROUPS, DIVCONST, LOG2PLUS1, ASSERTI32, CEILDIVU32, CEILDIVI32) fails closed on zero step, negative non-neg inputs, out-of-bounds push offset, 32-bit wrap hazards, indirect dispatches, non-zero basegroup, or SSBO-dependent bounds, falling back to packloops = true.

Areas: area:borealis
Merge: gh pr merge 1180 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr

Copy link
Copy Markdown

Merged aurora-wip at cd8033880ef1 after fixing the two loop undercounts. hk_nir_loop_bound.h:815 requires nonnegative multiplication factors and line 1507 rejects a late break on the old induction value. The new signed-product check failed against the original header. The fixed compute-pair suite and all 380 packet checks passed, and hk_shader.c compiled on the merged sources. The remaining checks are the M5 target and shared G17P/AGX2 dispatch-packing rows, followed by Ryan's re-review.

@RyanTheTide

Copy link
Copy Markdown

Merging at cd8033880ef1 (B2 merge train, 12-01 merge bar). Approval at this head: RyanTheTide. Row: j713 (M4 Air, G16G, fleet91) full VK+GL row of the M1 candidate vs tip: VK Pass 14679 vs 14659, GL Pass 7497 vs 7448, 0 firmware crashes/fail-stops; every Pass->non-Pass case re-ran clean (6 suspects passed 5/5 alone on both builds). Traces: traces/gpu/20261201-b2-mesa-M1. CI: mesa train M1 = aurora-wip 69a6b8d + #570 #1180 #1184 #1266 #1428 #1435 #1415 #1433 #1437 #631 #664 #673 (candidate e56f7487): host meson asahi+poly 351 OK / 26 fail vs tip 344 / 26 (same 26 pre-existing, 0 new; 7 new tests pass), agx_tests pass. Fork Actions don't run in this repo, so this local run is the CI. Revert: git revert -m 1 <merge commit>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:borealis Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants