Repository navigation
borealis: derive general dispatch-time loop bounds for compute submit packing - #1180
Conversation
… 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>
|
This branch no longer merges into current |
RyanTheTide
left a comment
There was a problem hiding this comment.
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.
src/asahi/vulkan/hk_nir_loop_bound.h:1365-1372together 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_testedstays false, so the loop gets the top-tested countceil((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 phii, 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_testedis true. Add the shape above as a regression test.
- Input:
src/asahi/vulkan/hk_nir_loop_bound.h:814-824, reached from the count-up dynamic limit at:1657-1658. That call passesrequire_nonneg = false, so eachimul/amulfactor is emitted asPUSH_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++)witha = b = -1000. The shader runs 1,000,000 iterations; the bound evaluates to 1.pc.n * -4with a negativenbreaks the same way. - Fix: in the
imul/amulcase, recurse withrequire_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.
- Input:
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 ofroot.push, and the bound is read after the im2col push has been restored. - Indirect dispatches, in-thread dispatches and a non-zero
base_groupall fail closed. Grid dimensions are capped atINT32_MAX. - The
CEIL_DIV_I32/CEIL_DIV_U32wrap checks, thelimit >= 0check on countdown loops, and the requirement that the step is non-negative and non-zero are correct. - A loop with an extra
continuefails 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 + stepstays withinINT32_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_groupsis taken beforehk_select_tensor_op()andhk_select_identity_copy4()rewritegrid. Consider asserting thatGRID_GROUPSis only used whenselected == 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.
|
@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 |
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
left a comment
There was a problem hiding this comment.
Re-review at 3e9645a (fix commit 3e9645a on top of the aurora-wip merges). Both blockers from my 5342bc5 review are fixed, fail-closed:
- 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.
- 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.
|
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. |
|
@DjDeveloperr, code-owner heads-up: App approval at |
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
|
Merged aurora-wip at |
|
Merging at |
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 withwork_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 collapses64 -> 1CDM 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 = truefor 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-zerobase_group, or SSBO-dependent bounds, falling back topack_loops = true.Not yet: SSBO/UBO-bounded loops, indirect dispatches, and non-zero
base_groupdispatches 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: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-weightedwork_opssupport.src/asahi/vulkan/hk_nir_loop_bound.h: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-smearednext_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: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 viahk_pack_eval_effective_instrs_dispatch()on direct zero-base dispatches withgrid.count <= INT32_MAX.src/asahi/vulkan/tests/test-hk-pack.c,test-hk-compute-pair.c:Build & Artifacts
J716c Apple M5 Max (G17C B1), Fedora Remix 44, kernel7.3.0-40.j716c19.fc44.aurora.aarch64origin/aurora-wip41fe336dffa):libvulkan_asahi.soSHA-2560ea0ee4efd3bb78ef2eab7770e5d2bf9dacd59a4cd4f8809729941acff43e4765342bc5a53c):libvulkan_asahi.soSHA-2561e8a77b75cae55fdd5e0d8f6eef1a65051acd9adf688dfcea9829244a0f09defVerification
--suite asahi --suite poly, Gallium + Vulkan +drm-shim, assertions enabled):334/334 PASS, 0 FAIL.MESA_SHADER_CACHE_DISABLE=1, 64-CB push-constant-bounded compute loop graph × 50 iters, exact SSBO value2550109440identical across all 12 runs):HK_PACK_CDM_TRACE=1)0 runs(64 cmds)1 run(1 cmd)1.1307 ms0.2121 ms5.33x)deqp-vk, base vs head,MESA_SHADER_CACHE_DISABLE=1):160-case slice acrossdEQP-VK.compute.pipeline.basic.*+dEQP-VK.api.command_buffers.*:159/160 PASS, 0 FAIL, 1 NotSupported(0status diffs vs base).SELinux=Enforcing,0DRM/asahi/GPU kernel errors.