Skip to content

hk: fuse compute producer/clamp pairs lazily and widen depthwise to vec4 - #688

Merged
DjDeveloperr merged 3 commits into
aurora-wipfrom
perf/hk-compute-pair-fusion-depthwise
Nov 24, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-wipfrom
perf/hk-compute-pair-fusion-depthwise

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 22, 2026 •

Copy link
Copy Markdown

Summary

Fuse adjacent single-dispatch compute producer/pointwise-clamp pairs (depthwise convolution or tiled GEMM followed by a 1D FP32 clamp epilogue) lazily at submit time in hk_pack_runs(), and add a generalized vec4 depthwise convolution alternate on native AGX3 and M5 GPUs (agx_chip_native_or_m5()).

Stacked on #687 (perf/hk-vec4-compute-alternates @ d74cf837d62eb6cb0819a3223e7674de35b7ac44).

Round 2 Changes (Addressing @RyanTheTide #issuecomment-7076609180 + M5 Cross-Chip Support)

  1. Multi-command stream safety (fixed silent command drop):
    • hk_cs_invalidate_compute_pair(cs) clears cs->compute_pair on any second dispatch (dispatch_grid()), stream merge (hk_cs_merge_cdm_until()), or stream teardown (hk_cs_destroy()).
    • hk_compute_pair_stream_eligible(cs) enforces strict single-dispatch CDM invariants (stats.cmds == 1 && stats.calls == 1 && pack_dispatches == 1, no stream_linked, calls_stream, sib_indirect, uses_global_atomics, imm_writes, query_access, query_reset_pending, timestamps, gen1_native_rq, as_build, alias_sites, or g18_dispatch_sealed) on both producer and consumer streams at submit time.
  2. Submit-time lazy fusion without shadow hk_cs or record_result hacks:
    • Deleted record-time shadow hk_cs allocation (compute_pair_alternate) and cmd->vk.record_result = VK_SUCCESS mutation.
    • Refcounted struct hk_compute_pair_producer holds cloned preprocessed producer NIR and references on set_layouts[], lazily compiling and caching up to 8 fused shader variants per producer under simple_mtx_t.
    • Submit-time hk_emit_fused_compute_dispatch() emits fused CDM dispatches directly into a submission-owned IOTO BO (AGX_BO_EXEC | AGX_BO_LOW_VA) supporting both HAL200 (G17P/G16) and HAL300 (M5), with clean unfused fallback on failure.
    • Generalized hk_cmd_pending_begin() to accept extra_bos / extra_bo_count so every fused shader BO and submission IOTO BO is pinned for the lifetime of the submission.
  3. Zero core NIR changes:
    • Reverted src/compiler/nir/{nir.h,nir_clone.c,nir_serialize.c} (0 lines diff vs #687 R2).
    • Compute-pair matchers run directly in hk_compile_shaders() on post-preprocess NIR without adding fields to nir_shader.
  4. Generalized matchers, splicer, and depthwise_vec4:
    • General FP32 pointwise clamp epilogue: lower-only fmax(x, lo), upper-only fmin(x, hi), or both in either order (fmin(fmax(x, lo), hi) / fmax(fmin(x, hi), lo)), arbitrary finite bounds, preserved fp_math_ctrl (validated against nir_op_infos[fmin/fmax].valid_fp_math_ctrl) and matching float_controls_execution_mode.
    • Generalized depthwise matcher and hk_nir_build_depthwise_vec4(..., epilogue) supporting biased (flag-guarded or unconditional) and biasless kernels with arbitrary push-constant layouts.
    • Generalized GEMM matcher supporting 1..16 rows_per_thread, guarded or unguarded stores, and 0/1/2/3 bias arms (both store-per-arm and load-per-arm forms).
    • Replaced duplicated splicers with one hk_nir_splice_epilogue().
  5. Explicit field-by-field serialization & cross-chip widening:
    • Explicit hk_compute_pair_info_serialize() / hk_compute_pair_info_deserialize() (zero struct-padding reads).
    • Widened capability checks from agx_chip_native_agx3() to agx_chip_native_or_m5() so M5 (G17G/G17S/G17C) gets full compute-pair fusion and depthwise_vec4 support out of the box.

Verification

  • FEDORA-CI: 158/0 PASS on d91733575ad20a3b3c015d54ed1b4a4986b0a724 (0 failures, 0 skipped, includes new hk_compute_pair_test + updated hk_pack_test).
  • test-hk-compute-pair: bit-exact hk_eval_dispatch verification across all 5 clamp shapes (G17P + M5 preprocess), biased/biasless/unconditional-bias depthwise_vec4 + fused variants, and generalized GEMM (4 & 8 rows/thread, 0/1/2/3 bias modes, guarded/unguarded).
  • test-hk-pack: 190 tests, 0 failures (20 new test_compute_pair_stream_safety assertions).
  • DEFAULT_BRANCH=aurora-wip .github/aurora/agent-policy.sh pr HEAD: clean.

@RyanTheTide

Copy link
Copy Markdown

I reviewed the commit on top of #687 at 2c06578.

  • Blocker: submit-time fusion can drop commands. hk_record_compute_pair() (hk_cmd_dispatch.c:2903) records a candidate only for the first dispatch in a stream, and hk_record_compute_pair_alternate() (:2958) builds the shadow only while stats.cmds == 1. Nothing invalidates either record when more commands are recorded into the same stream afterwards. compute_pair_can_replace() (hk_queue.c:2675) does not re-check the dispatch count, and hk_pack_run_member() admits streams with up to 256 dispatches (hk_pack.c:104, hk_pack.h:32). hk_pack_runs() then emits the shadow instead of the whole producer stream (hk_queue.c:2822) and skips the whole consumer stream (:2818). Suppose command buffer A records [depthwise, barrier, X] and command buffer B records [in-place relu6, Y], both in one submit. X and Y are never executed. Please require exactly one dispatch and no other content in both streams at submit time, or clear the records on any later command, and add an hk_pack_runs test for this case.

  • hk_record_compute_pair_alternate() records a second full dispatch for every eligible producer, whether or not a clamp ever follows. It restores only cmd->state.cs.descriptors (:2975) and then forces cmd->vk.record_result = VK_SUCCESS (:2982). Please explain why nothing else in the command buffer state that hk_dispatch_with_local_size() touches needs restoring. Otherwise, build the fused stream lazily at submit time.

  • Scope of the matchers. Nothing is keyed by hash or name, but the patterns are fingerprints of specific shaders, not general transforms:

    • The GEMM matcher accepts only "24 stores across 3 mutually exclusive bias_mode arms, each writing 8 rows per thread" (hk_nir_match_pointwise_clamp.h:89, rows_per_thread = 8 at .c:1560).
    • The fusion accepts only a clamp to exactly [0, 6.0f] with exactly nir_fp_preserve_inf on both ops (hk_queue.c:2688), so ReLU, other clamp bounds, or the same kernel compiled with different float controls fall back.
    • The depthwise matcher requires one exact program structure (3 reads, 2 breaks, a 14-field push block, has_bias == 1).

    The numbers in the description are Geekbench 6 Face Detection and Background Blur. As written, this helps those kernels and stops helping as soon as they change. Please either generalize it, for example by fusing any in-place elementwise fmin/fmax epilogue with arbitrary constants and ctrls into any producer whose single store covers the same interval, or show which other workloads benefit.

  • nir_shader::driver_data (nir.h, nir_clone.c, nir_serialize.c) puts driver-private matcher state into core NIR. The comment says semantic hashes exclude it, but nir_serialize with info now includes it. A driver-side side table keyed by the shader, or recomputing the match where it is needed, would avoid touching common code.

  • Bit-exactness: the relu6 epilogue clones should be exact. I checked that the consumer's ctrls are required to match and that the value is the one that would have been stored. For depthwise_vec4 I don't see a reorder of the accumulation, but the only bit-exact row is for GEMM. Please add an original-vs-alternate output compare for depthwise covering stride 1 and 2, 3x3 and non-3x3, odd ow (the has_p1 tail), padding borders and has_bias 0/1. The current unit test covers only the footprint arithmetic, and CTS cannot reach these matchers.

  • agx_chip_native_agx3() also enables this on G15 and G16G, and the 3x3 path's register-pressure comment (hk_nir_match_pointwise_clamp.c:2011) is tuned for G17P. The only hardware row is Neo. Please add rows for an M3 (J615), an M4 (J713) and the M4 Pro at this head.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-vec4-compute-alternates branch from 4300093 to d74cf83 Compare November 22, 2026 09:41
DjDeveloperr added a commit that referenced this pull request Nov 22, 2026
Fuse adjacent single-dispatch compute producer/pointwise-clamp pairs
(depthwise convolution or tiled GEMM followed by a 1D FP32 clamp
epilogue) at submit time in hk_pack_runs(), and add a generalized
vec4 depthwise convolution alternate on native AGX3 and M5 GPUs.

Round 2 review rework (RyanTheTide #688 + M5 cross-chip support):
- Single-dispatch stream safety: invalidate cs->compute_pair on any
  second dispatch or stream merge (hk_cs_invalidate_compute_pair) and
  enforce hk_compute_pair_stream_eligible() on both producer and
  consumer streams at submit time so multi-command streams never drop
  extra dispatches, barriers, queries, or side effects.
- Submit-time lazy fusion without shadow hk_cs or record_result hacks:
  keep a refcounted hk_compute_pair_producer holding the cloned
  preprocessed producer NIR and set layouts, lazily compile and cache
  up to 8 fused variants per producer under a mutex, emit fused CDM
  dispatches directly on HAL200 (G17P/G16) and HAL300 (M5), and pin
  every fused shader BO and the submission IOTO BO via
  hk_cmd_pending_begin().
- Zero core NIR changes: run compute-pair matchers directly in
  hk_compile_shaders() on post-preprocess NIR without adding fields to
  nir_shader.
- Generalized matchers, splicer, and vec4 depthwise builder:
  support arbitrary finite FP32 clamp epilogues (lower-only,
  upper-only, or both in either order with preserved fp_math_ctrl and
  float_controls_execution_mode), biased/biasless/unconditional-bias
  depthwise kernels with arbitrary push layouts, arbitrary GEMM
  rows_per_thread (1..16) and 0/1/2/3 bias arms, and one unified
  hk_nir_splice_epilogue().
- Explicit field-by-field serialization of hk_compute_pair_info with no
  struct-padding reads; widen chip capability checks to
  agx_chip_native_or_m5().
- Add bit-exact NIR interpreter tests in test-hk-compute-pair.c and
  20 stream-safety tests in test-hk-pack.c.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-compute-pair-fusion-depthwise branch from 2c06578 to d917335 Compare November 22, 2026 10:35
@DjDeveloperr DjDeveloperr changed the title borealis: fuse certified compute producer->clamp pairs and add vec4 depthwise alternate hk: fuse compute producer/clamp pairs lazily and widen depthwise to vec4 Nov 22, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Thanks for the thorough review @RyanTheTide — especially catching the multi-command stream drop bug! Everything from #issuecomment-7076609180 plus M5 cross-chip support (agx_chip_native_or_m5()) is addressed in Round 2 (d91733575ad20a3b3c015d54ed1b4a4986b0a724, rebased onto #687 R2 d74cf837d62eb6cb0819a3223e7674de35b7ac44):

  1. Multi-command stream safety (silent drop fix):
    • Added hk_cs_invalidate_compute_pair(cs) on any second dispatch in dispatch_grid(), stream merges in hk_cs_merge_cdm_until(), and stream teardown in hk_cs_destroy().
    • Added hk_compute_pair_stream_eligible(cs) checked on both producer and consumer streams in compute_pair_can_replace(), requiring stats.cmds == 1 && stats.calls == 1 && pack_dispatches == 1 and rejecting stream_linked, calls_stream, sib_indirect, uses_global_atomics, imm_writes, query_access, query_reset_pending, timestamps, gen1_native_rq, as_build, alias_sites, and g18_dispatch_sealed.
    • Covered by 20 new unit tests in test-hk-pack.c (test_compute_pair_stream_safety, 190/190 pass).
  2. Submit-time lazy fusion (removed shadow hk_cs & record_result = VK_SUCCESS hack):
    • Deleted compute_pair_alternate, hk_record_compute_pair_alternate(), and the cmd->vk.record_result mutation.
    • Producers retain a refcounted struct hk_compute_pair_producer (cloned preprocessed NIR + referenced set_layouts[]) and lazily compile/cache up to 8 fused variants under simple_mtx_t on first matching submit.
    • hk_pack_runs() allocates a submission-owned ioto_bo (AGX_BO_EXEC | AGX_BO_LOW_VA), emits fused CDM via hk_emit_fused_compute_dispatch() (supporting both HAL200 and HAL300/M5) with clean unfused fallback on failure, and pins every fused shader BO + ioto_bo via generalized hk_cmd_pending_begin(..., extra_bos, extra_bo_count).
  3. Zero core NIR changes:
    • Reverted all changes to src/compiler/nir/{nir.h,nir_clone.c,nir_serialize.c} (0 lines diff vs #687 R2). Matching runs directly in hk_compile_shaders() on post-preprocess NIR.
  4. Generalized matchers, splicer, and depthwise_vec4:
    • Generalized clamp epilogue to arbitrary finite FP32 lower/upper/both-in-either-order clamps, preserving fp_math_ctrl (validated against nir_op_infos[nir_op_fmin/fmax].valid_fp_math_ctrl) and requiring matching float_controls_execution_mode.
    • Generalized hk_nir_match_depthwise_output() and hk_nir_build_depthwise_vec4(..., epilogue) to arbitrary push layouts and biased (flag-guarded or unconditional) + biasless kernels.
    • Generalized hk_nir_match_gemm_output() to 1..16 rows_per_thread, guarded or unguarded stores, and 0/1/2/3 bias arms (both store-per-arm and load-per-arm forms).
    • Unified the duplicated splicers into one hk_nir_splice_epilogue().
    • Also caught and fixed a loop-header phi dominance / pre-header bug in hk_nir_build_depthwise_vec4() surfaced by nir_validate_shader() in the new unit test suite.
  5. Field-by-field serialization & cross-chip M5 support:
    • Replaced raw struct serialization with explicit hk_compute_pair_info_serialize() / deserialize() (no uninitialized struct padding).
    • Widened capability checks to agx_chip_native_or_m5().
  6. Verification:
    • FEDORA-CI: 158/0 PASS on d91733575ad20a3b3c015d54ed1b4a4986b0a724 (including bit-exact NIR interpreter tests in test-hk-compute-pair.c on both G17P and M5 preprocess paths, plus test-hk-pack.c 190/0).
    • DEFAULT_BRANCH=aurora-wip .github/aurora/agent-policy.sh pr HEAD: clean.

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

@DjDeveloperr

Copy link
Copy Markdown
Author

Clean-room Apple M5 Pro (J714s / G17S B1 / AGX3C) runtime verification against R2 head d91733575ad20a3b3c015d54ed1b4a4986b0a724 (Arch Linux ARM 7.3.0-m5-slc1, GCC 16.1.1, -Dbuildtype=release, stacked onto mw-mega15a = #687 R2 with hk_descriptor_set.c M5 fix + #700 R2 as mw-mega15):

1. Correctness & Stability

  • hk_pack_test: 206/206 PASS (0 failures, including all 20 new compute_pair tests).
  • hk_compute_pair_test: PASS once assertions are active (see release-build -DNDEBUG bug below).
  • Exactness (gb-gaussian checked): 12/12 PASS.
  • VK CTS: 3,296/3,296 supported PASS (522/678 targeted compute/SSBO/FMA/sync + 2,774/3,755 broad glsl.loops.*, subgroups.arithmetic.compute.*, robustness2.*), 0 Failed, 0 Warnings.
  • GPU faults (dmesg): 0 new faults.
  • Geekbench 6.7.1 Vulkan runs (stacked mw-mega15): https://browser.geekbench.com/v6/compute/6985541 and https://browser.geekbench.com/v6/compute/6985544.

2. Three Concrete Findings on M5 Pro (G17S B1) & -Dbuildtype=release

  1. fused=0 on M5 Pro Geekbench Background Blur & Face Detection (HK_PACK_CDM_TRACE=1):
    • Observed 19x HK_PACK runs=1 members=165 fused=0 eligible=165 of 165 on Background Blur and 19x HK_PACK runs=2 members=242 fused=0 eligible=242 of 242 on Face Detection (Background Blur: 2.749 -> 2.798 ms, Face Detection: 5.040 -> 5.060 ms; the 12i/7g clamp shader still executes separately at 3.095 ms/iter).
    • Blocker A (hk_pack.c, hk_compute_pair_stream_eligible()): requires !cs->alias_sites, but on M5 Pro host_alias.dynamic records 1 tail-barrier alias patch site per command buffer (alias_sites=165/165 on Background Blur, alias_sites=242/242 on Face Detection), unconditionally rejecting 100% of M5 Pro streams. (When fused, hk_pack_runs emits a fresh fused dispatch + hk_cdm_visibility_flush(dev, cs, eff_last || hk_cdm_alias_scope(dev)) under host_alias.patch_lock rather than copying the original tail.)
    • Blocker B (hk_shader.c, shader matching cp=0): zero Geekbench Background Blur / Face Detection compute shaders matched hk_nir_match_depthwise_output, hk_nir_match_gemm_output, or hk_nir_match_pointwise_clamp at hk_compile_shaders() time on M5 Pro (cp=0(dw=0,gm=0,cl=0) across all 165 and 242 members), so neither lazy fusion nor depthwise_vec4 activated yet. (Note: hk_compile_identity_copy4 runs nir_opt_access + nir_lower_explicit_io(..., nir_var_mem_push_const, nir_address_format_32bit_offset) on a clone before matching, whereas the hk_compute_pair matcher runs directly on pre-lowered nir.)
  2. Release-build (-DNDEBUG) SIGSEGV in src/asahi/vulkan/tests/test-hk-compute-pair.c:
    • 17 side-effecting calls (hk_nir_match_depthwise_output(..., &info), hk_nir_match_gemm_output(..., &info), hk_nir_match_pointwise_clamp(..., &matched), hk_nir_*_footprint(...)) are placed directly inside assert(...). Under -Dbuildtype=release (-DNDEBUG), they compile out, leaving info zeroed and causing hk_nir_build_depthwise_vec4() to return NULL and SIGSEGV in run_depthwise_case. Moving the calls outside assert(...) (or using an always-active check helper) makes hk_compute_pair_test pass cleanly in release builds.

@DjDeveloperr

Copy link
Copy Markdown
Author

Follow-up on Apple M5 Pro (J714s / G17S B1 / AGX3C) — root-caused all 3 M5 Pro blockers in d91733575ad20a3b3c015d54ed1b4a4986b0a724 and verified #688 R2 lazy fusion LIVE on M5 Pro with 0 failures / 0 GPU faults:

Exact Root Causes & Minimal Fixes (3 surgical hunks)

  1. Blocker B (src/asahi/vulkan/hk_nir_match_pointwise_clamp.c): scalar_op() checked value.def->num_components == 1, rejecting pre-scalarized 32x3 vector load_global_invocation_id + load_base_workgroup_id * local_size in strip_zero_add(), match_base_wg_scaled(), and sysval_id_axis(). Adding a component-agnostic alu_op() helper for those 3 functions immediately matched all 37 Background Blur and 48 Face Detection pairs (cp=92(dw=17,gm=38,cl=37) and cp=118(dw=14,gm=56,cl=48)).
  2. Blocker A (src/asahi/vulkan/hk_pack.c): hk_compute_pair_stream_eligible() checked !cs->alias_sites, rejecting M5 Pro's single tail-barrier alias patch site under host_alias.dynamic (alias_sites=1 on 165/165 BG Blur and 242/242 Face Detection streams). Relaxing to cs->alias_sites <= 1 (safe because hk_pack_runs() runs under host_alias.patch_lock and emits a fresh hk_cdm_visibility_flush(dev, cs, eff_last || hk_cdm_alias_scope(dev)) instead of copying the original tail) admitted 100% of pairs (el=92/92, el=118/118).
  3. Blocker C (src/asahi/vulkan/hk_queue.c:2907): agx_bo_create(&dev->dev, ioto_total, AGX_BO_EXEC | AGX_BO_LOW_VA, 0, "Packed compute fused Ioto") had align (arg 3) and flags (arg 4) swapped (agx_bo_create(dev, size, align, flags, label)), causing out->ioto_bo to allocate outside the USC window and silently fall back to fused=0. Swapping to (..., 0, AGX_BO_EXEC | AGX_BO_LOW_VA, ...) immediately enabled live fused emission!

Live M5 Pro Verification Results (stacked mw-mega16 = mw-mega15a + #688 R2 + 3 fixes above)

  • HK_PACK_CDM_TRACE=1:
    • Background Blur: 19x HK_PACK runs=1 members=165 fused=37 eligible=165 of 165 (37/37 pairs fused live)
    • Face Detection: 19x HK_PACK runs=2 members=242 fused=48 eligible=242 of 242 (48/48 pairs fused live)
  • Quiet-GPU Anatomy (HK_FRAME_ANATOMY_TS=all, mw-mega16 vs mw-mega15a):
    • Background Blur: 2.749 ms -> 2.576 ms (-0.173 ms, -6.30%; -10.47% vs mw-mega14 2.877 ms) — standalone 12i/7g clamp kernel completely eliminated from BG Blur.
    • Face Detection: 5.040 ms -> 4.549 ms (-0.491 ms, -9.76%; -13.87% vs mw-mega14 5.282 ms) — standalone 12i/7g clamp dropped 3.088 -> 1.696 ms/iter.
    • 8-workload geomean busy speedup: +2.31% over mw-mega15a (+3.45% over mw-mega14, total busy 40.691 -> 39.551 ms, projecting ~149.5k / ~106.5% of Apple M5 Pro macOS 27.0 Metal 6619028 / 140,326).
  • Tests & Correctness:
    • hk_pack_test: 207/207 PASS (0 failures)
    • hk_compute_pair_test: PASS (with active assertions)
    • hk_identity_copy4_test: PASS
    • gb-gaussian checked: 12/12 PASS
    • VK CTS: 3,296/3,296 supported PASS (522/678 + 2,774/3,755, 0 fail, 0 warn)
    • Kernel dmesg GPU faults: 0 new faults.
  • Uploaded Geekbench 6.7.1 Vulkan runs (taskset -c 12-17): https://browser.geekbench.com/v6/compute/6985609 and https://browser.geekbench.com/v6/compute/6985613.

@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-vec4-compute-alternates branch from d74cf83 to 7f319bd Compare November 22, 2026 12:46
DjDeveloperr added a commit that referenced this pull request Nov 22, 2026
Fuse adjacent single-dispatch compute producer/pointwise-clamp pairs
(depthwise convolution or tiled GEMM followed by a 1D FP32 clamp
epilogue) at submit time in hk_pack_runs(), and add a generalized
vec4 depthwise convolution alternate on native AGX3 and M5 GPUs.

Round 2 review rework (RyanTheTide #688 + M5 cross-chip support):
- Single-dispatch stream safety: invalidate cs->compute_pair on any
  second dispatch or stream merge (hk_cs_invalidate_compute_pair) and
  enforce hk_compute_pair_stream_eligible() on both producer and
  consumer streams at submit time so multi-command streams never drop
  extra dispatches, barriers, queries, or side effects.
- Submit-time lazy fusion without shadow hk_cs or record_result hacks:
  keep a refcounted hk_compute_pair_producer holding the cloned
  preprocessed producer NIR and set layouts, lazily compile and cache
  up to 8 fused variants per producer under a mutex, emit fused CDM
  dispatches directly on HAL200 (G17P/G16) and HAL300 (M5), and pin
  every fused shader BO and the submission IOTO BO via
  hk_cmd_pending_begin().
- Zero core NIR changes: run compute-pair matchers directly in
  hk_compile_shaders() on post-preprocess NIR without adding fields to
  nir_shader.
- Generalized matchers, splicer, and vec4 depthwise builder:
  support arbitrary finite FP32 clamp epilogues (lower-only,
  upper-only, or both in either order with preserved fp_math_ctrl and
  float_controls_execution_mode), biased/biasless/unconditional-bias
  depthwise kernels with arbitrary push layouts, arbitrary GEMM
  rows_per_thread (1..16) and 0/1/2/3 bias arms, and one unified
  hk_nir_splice_epilogue().
- Explicit field-by-field serialization of hk_compute_pair_info with no
  struct-padding reads; widen chip capability checks to
  agx_chip_native_or_m5().
- Add bit-exact NIR interpreter tests in test-hk-compute-pair.c and
  20 stream-safety tests in test-hk-pack.c.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-compute-pair-fusion-depthwise branch from d917335 to d92fc36 Compare November 22, 2026 12:55
@DjDeveloperr

Copy link
Copy Markdown
Author

Pushed R3 (d92fc36fa6b, rebased cleanly onto #687 R3 7f319bd7a50):

  • Blocker C (src/asahi/vulkan/hk_queue.c): Fixed swapped align and flags arguments to agx_bo_create() for out->ioto_bo (0, AGX_BO_EXEC | AGX_BO_LOW_VA) so the fused Ioto BO stays in the USC window across all chips.
  • Blocker B (src/asahi/vulkan/hk_nir_match_pointwise_clamp.c): Added alu_op() helper for strip_zero_add(), match_base_wg_scaled(), and sysval_id_axis() so pre-scalarized 32x3 vector load_global_invocation_id + load_base_workgroup_id * workgroup_size from nir_lower_compute_system_values matches cleanly on both G17P and M5. Verified post-preprocess matching on real SPIR-V modules (face-depthwise, aligned-original, edge-original) on both g17p and g17s-kernel.
  • Blocker A (src/asahi/vulkan/hk_pack.c, test-hk-pack.c): Relaxed !cs->alias_sites to cs->alias_sites <= 1 in hk_compute_pair_stream_eligible() (safe under host_alias.patch_lock since hk_pack_runs() discards both original tails and emits a fresh fused dispatch + hk_cdm_visibility_flush(..., hk_cdm_alias_scope())), with explicit unit tests covering both alias_sites == 1 (accepted) and alias_sites == 2 (rejected).
  • Release-build test fix (src/asahi/vulkan/tests/test-hk-compute-pair.c): Switched all checks from assert() to always-active HK_EVAL_CHECK() so -Dbuildtype=release (-DNDEBUG) compiles and executes every match/footprint check cleanly, and expanded post-preprocess unit tests to run the full nir_lower_system_values + agx_preprocess_nir_*_defer_idiv + nir_lower_load_const_to_scalar + nir_lower_compute_system_values(.has_base_workgroup_id = true) pipeline including M5 5-component _map descriptors.
  • Verified with fedora-mesaci (158/0 PASS, 0 failures, 0 core NIR touches).

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

@DjDeveloperr

Copy link
Copy Markdown
Author

Clean-room M5 Pro (J714s / G17S B1 / AGX3C) Triple-Chip Signoff — R3 (d92fc36fa6bb46264da5b2242fcf1a2e4c200acb)

  • Machine / kernel: MacBook Pro M5 Pro (J714s / G17S B1 / AGX3C), 7.3.0-m5-slc1, MESA_SHADER_CACHE_DISABLE=1, taskset -c 12-17
  • Build: -Dbuildtype=release -Db_ndebug=true (-DNDEBUG), libvulkan_asahi.so SHA-256:
    • Base (f3016978f09e): 4cf6d80a08a85d3f53ff4eeebed2cc83b51178e1fbbc8db9aa3f7822431434a6
    • #687 R3 (7f319bd7a50f): 075382e8ed0ae8770bde2f0836d2e7333996ebc93a12fc713eb5b549928b71b6
    • #688 R3 (d92fc36fa6bb): 6c916b8145f04f8c28157d0e9421aa26ca45e5e26ef2eaa4e495472de7de8fe5

Unit Tests, Live Fusion Trace, Correctness & Fault Check

Check Result
meson test --suite asahi (-Db_ndebug=true) 157/157 OK, 0 fail (hk_identity_copy4 OK 0.01s, hk_compute_pair OK 0.02s, hk_pack OK 1.12s)
HK_PACK_CDM_TRACE=1 live fusion counts (GB6 Vulkan) Background Blur: 37/37 per iter (740 total across 20/20 iters); Face Detection: 48/48 per iter (960 total across 20/20 iters)
gb-gaussian (checked, 2 repeats/case) 12/12 PASS (bit-exact vs CPU reference)
Vulkan CTS 1.4.7.1 (/tmp/cts-mega12.txt) 522/678 pass / 0 fail / 0 warn / 156 unsupported
Geekbench 6.7.1 Vulkan spills (sp>0) & dmesg 0 spills / 0 new dmesg lines / 0 GPU faults

Interleaved A/B/C -> C/B/A Geekbench 6.7.1 Vulkan (busy/iter median ms, HK_FRAME_ANATOMY_TS=all)

Workload Base f3016978f09e (avg) #687 R3 7f319bd7a50f (avg) #688 R3 d92fc36fa6bb (C1 / C2 / avg) #688 vs #687 #688 vs Base
Background Blur 7.410 7.287 6.943 / 6.943 (6.943) -4.72% (1.050x) -6.31% (1.067x)
Face Detection 10.961 10.451 8.910 / 8.904 (8.907) -14.78% (1.173x) -18.74% (1.231x)
Horizon Detection 6.149 6.172 6.186 / 6.181 (6.184) +0.18% +0.56%
Edge Detection 4.960 4.936 4.962 / 4.962 (4.962) +0.53% +0.05%
Gaussian Blur 6.984 6.986 6.989 / 6.989 (6.989) +0.04% +0.06%
Feature Matching 15.823 15.843 15.787 / 15.705 (15.746) -0.61% -0.49%
Stereo Matching 7.239 7.398 7.086 / 7.395 (7.240) -2.13% +0.01%
Particle Physics 24.646 24.631 24.615 / 24.627 (24.621) -0.04% -0.10%
8-WL Geomean 1.0000x 1.0054x 1.0345x vs Base 1.0290x (-2.82%) 1.0345x (-3.34%)

M5 Pro (AGX3C) signoff: GREEN (37/37 BG Blur & 48/48 Face Detection live fusions, -14.78% Face Detection / -4.72% Background Blur over #687 R3, 157/157 release unit tests, 12/12 gb-gaussian, 0 CTS fails, 0 spills, 0 GPU faults).

Assisted-by: LLM (Antigravity:Gemini 4 Neon)

…ol, fadd, and 4D pad kernels

Signed-off-by: Diljit Singh <hey@djdev.me>
Fuse adjacent single-dispatch compute producer/pointwise-clamp pairs
(depthwise convolution or tiled GEMM followed by a 1D FP32 clamp
epilogue) at submit time in hk_pack_runs(), and add a generalized
vec4 depthwise convolution alternate on native AGX3 and M5 GPUs.

Round 2 review rework (RyanTheTide #688 + M5 cross-chip support):
- Single-dispatch stream safety: invalidate cs->compute_pair on any
  second dispatch or stream merge (hk_cs_invalidate_compute_pair) and
  enforce hk_compute_pair_stream_eligible() on both producer and
  consumer streams at submit time so multi-command streams never drop
  extra dispatches, barriers, queries, or side effects.
- Submit-time lazy fusion without shadow hk_cs or record_result hacks:
  keep a refcounted hk_compute_pair_producer holding the cloned
  preprocessed producer NIR and set layouts, lazily compile and cache
  up to 8 fused variants per producer under a mutex, emit fused CDM
  dispatches directly on HAL200 (G17P/G16) and HAL300 (M5), and pin
  every fused shader BO and the submission IOTO BO via
  hk_cmd_pending_begin().
- Zero core NIR changes: run compute-pair matchers directly in
  hk_compile_shaders() on post-preprocess NIR without adding fields to
  nir_shader.
- Generalized matchers, splicer, and vec4 depthwise builder:
  support arbitrary finite FP32 clamp epilogues (lower-only,
  upper-only, or both in either order with preserved fp_math_ctrl and
  float_controls_execution_mode), biased/biasless/unconditional-bias
  depthwise kernels with arbitrary push layouts, arbitrary GEMM
  rows_per_thread (1..16) and 0/1/2/3 bias arms, and one unified
  hk_nir_splice_epilogue().
- Explicit field-by-field serialization of hk_compute_pair_info with no
  struct-padding reads; widen chip capability checks to
  agx_chip_native_or_m5().
- Add bit-exact NIR interpreter tests in test-hk-compute-pair.c and
  20 stream-safety tests in test-hk-pack.c.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-vec4-compute-alternates branch from 7f319bd to 0f86446 Compare November 24, 2026 12:35
@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-compute-pair-fusion-depthwise branch from d92fc36 to 85d3fbd Compare November 24, 2026 12:44
@DjDeveloperr

Copy link
Copy Markdown
Author

Ported onto rebased #687 (0f864464e97, on aurora-wip 4af141fe167, merge-clean against 617c7fe6f02d1d0f651183942f663f049e8c0244).

Port summary:

  • Moved compute-pair / depthwise_vec4 cache flags into variant_flags3 (bits 1 and 2, mask ~3u) to avoid colliding with aurora-wip's byte_stencil in variant_flags2, bumping HK_AGX3_SHADER_BINARY_VERSION from 46 to 47.
  • Adapted submit-time fusion to aurora-wip's queue-owned reusable submit packets (hk_pack_add_bo() for fused shader / IOTO BOs, preserving #919's sibling-queue host-flush check and hk_cmd_pending_begin() pinning).
  • Updated fused G17P dispatch emission to go through hk_g17p_linked_program_desc() + agx3_g17p_compute_program_generate() so post-#643 compute preambles are emitted.
  • Verified with assert-on unit CI (180/180 pass, including test_fused_packet_reuse and test_pack_add_bo) and live MacBook Neo (G17P B1) validation (0 diffing words vs #687, 1,308/1,308 targeted CTS pass under ASAHI_MESA_DEBUG=verify, 0 GPU faults, GB6 Face Detection -12.5% and Background Blur -4.1%).

@DjDeveloperr
DjDeveloperr changed the base branch from perf/hk-vec4-compute-alternates to aurora-wip November 24, 2026 19:49
@DjDeveloperr

Copy link
Copy Markdown
Author

Verdict at ba16342f2b04: Approve.

Blockers

None.

Checked

  • Verified single-dispatch stream safety (hk_compute_pair_stream_eligible() + hk_cs_invalidate_compute_pair() + test_compute_pair_stream_safety), lazy submit-time fusion with BO pinning (hk_pack_add_bo() and hk_cmd_pending_begin()), clean OOM/USC fallback on ioto_bo, and no core NIR changes.
  • Verified HK_AGX3_SHADER_BINARY_VERSION bump (49 -> 50) and explicit field-by-field hk_compute_pair_info / variant_flags3 serialization round-trip.
  • Verified Neo (G17P B1) and M5 Pro (G17S B1) hardware rows plus merge-result mirror CI (asahi 45478727344, policy 45478727700) pass.

@DjDeveloperr
DjDeveloperr merged commit d027ba2 into aurora-wip Nov 24, 2026
@RyanTheTide
RyanTheTide deleted the perf/hk-compute-pair-fusion-depthwise 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