Skip to content

borealis: add packed vec4 compute alternates for certified copy, im2col, fadd, and 4D pad kernels - #687

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/hk-vec4-compute-alternates
Nov 24, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/hk-vec4-compute-alternates

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 22, 2026 •

Copy link
Copy Markdown

Add general NIR pattern matchers and packed vec4 compute shader alternates in Borealis (hk) for four certified compute kernel families on native AGX3 GPUs (agx_chip_native_agx3()):

  1. 1D linear vec4 copy / offset-copy / elementwise fadd (hk_nir_copy_identity4.c):
    • Certifies 1D elementwise copy, offset-copy, 1x1 stride-1 im2col, and fadd kernels at NIR level (hk_nir_copy_identity4()).
    • Emits a flattened 1D 128x1x1 vec4 alternate transferring 4 coalesced vec4s (64 B) per lane with a workgroup-uniform interior fast path and safe tail path.
    • Dispatch-time selector (hk_select_identity_copy4()) validates push-constant shapes, 16-byte alignment, backing-buffer bounds, and interval disjointness/in-place safety before selecting the alternate.
  2. Packed vec4 general im2col (hk_nir_im2col_vec4()):
    • Certifies general 2D im2col kernels with channel count divisible by 4 and builds a 32x4x1 vec4 alternate selected when runtime push constants and backing buffers satisfy 16-byte alignment and non-overlap.
  3. Packed vec4 4D tensor pad (build_pad4d_vec4()):
    • Certifies 4D zero-pad kernels (N,H,W,C) and builds a 64x2x1 2D-grid vec4 alternate with an out_n == 1 uniform fast path avoiding udiv/umod on the outer dimension.
  4. Shader cache & descriptor tracking:
    • Tracks storage-buffer backing ranges in hk_buffer / hk_descriptor_set for exact runtime aliasing/bounds checks.
    • Serializes/deserializes alternate metadata and binaries cleanly in hk_shader.c, bypassing hk_shader_binary_cache when alternates are attached so alternate blobs are never silently dropped.
    • Adds unit test src/asahi/vulkan/tests/test-hk-identity-copy4.c.

Verification

  • DEFAULT_BRANCH=aurora-wip .github/aurora/agent-policy.sh pr HEAD: PR head is clean
  • FEDORA-CI on exact head 430009309aa79f9a8b8aa42dd42e35ecfe0291e6: 157/0 PASS (Ok 157 Fail 0, including hk_identity_copy4)
  • Live MacBook Neo (G17P B1) verification (combined Wave 2G 1e747f6cb01, kernel 7.3.0-41.fc44.aurora.aarch64, PMU=30 00 -> 30 00, kfaults=0, avc=0):
    • PAD4 (50d93768): 2.688 -> 2.197 ms (-18.3%)
    • im2col_vec4 (5a400c45): 2.069 -> 1.333 ms (-35.6%)
    • Targeted Vulkan CTS (6 suites, 83,536 cases): 27,740 Pass / 0 Fail / 0 Recov
    • Zero regressions across all 8 Geekbench 6 Vulkan workloads.

@RyanTheTide

Copy link
Copy Markdown

Thanks. I read the matchers and the dispatch selectors at 4300093. Most of this is safe in the sense that a miss falls back to the original shader. My main concern is how narrow some of the matching is.

  • The linear copy, offset copy and fadd matcher (match_linear_1d, hk_nir_copy_identity4.c:1000) is a reasonable general transform. It covers any if (gid < count) dst[gid] = src[gid] / src0 + src1 with discovered push offsets. The selector refuses non-multiple-of-4 counts, misalignment and partial overlap, and the result is bit-identical (raw 32-bit words, or per-component fadd with the original's fp_math_ctrl). No concerns there beyond test coverage.
  • The im2col path is not a general transform. It is a fingerprint of one program's push-constant ABI. match() requires num_uniforms == 44 (hk_nir_copy_identity4.c:857) and a fixed field layout (enum copy_push, :458), including "Byte 20 is declared by the application and unused by its program" (:464). hk_identity_copy4_required_bytes() and hk_select_im2col_vec4() hardcode push[0..10] (hk_cmd_dispatch.c:2910, :3017), and the stride-dependent pad_x_tot = unit ? pad_x * 2 : pad_x copies that program's semantics. An im2col with any other field order or push size gets nothing, and the speedup disappears if that program changes. Please either make the im2col matcher discover its parameters from the NIR, as the linear and pad matchers do, or drop it. A driver should not ship hand-written replacements keyed to one application's shader layout.
  • The pad4d matcher depends on the exact integer-division lowering, including the 0x4f7ffffe reciprocal refinement from agx_nir_lower_fdiv (:120, :219-455). Any change to that lowering silently turns the alternate off. It would be more robust to match before idiv lowering, or on udiv/umod.
  • Correctness evidence for pad and im2col is missing. tests/test-hk-identity-copy4.c covers only the linear matcher. There is no im2col or pad case and no output comparison. CTS cannot exercise these matchers, so the CTS row does not cover them. Please add a test that runs the original and the alternate on the same inputs and compares the outputs bit for bit, covering border and interior, unit and non-unit stride, out_n > 1, non-zero channel padding, and the count/pw sizes at the edge of a workgroup.
  • agx_chip_native_agx3() also enables this on G15 and G16G, but the only hardware row is G17P (Neo). Please add rows for an M3 (J615), an M4 (J713) and the M4 Pro at this head, showing the alternate is selected and the output matches the scalar path.
  • Small things:
    • blob_write_bytes(blob, &obj->identity_copy4_info, sizeof(...)) (hk_shader.c:9377) serializes struct padding, which makes shader binaries nondeterministic. Zero-initialize the struct or write it field by field.
    • hk_deserialize_optional_compute_alt() returns VK_SUCCESS when calloc fails (hk_shader.c:8836).

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr

Copy link
Copy Markdown
Author

Addressed all review points in Round 2 (d74cf837d62):

  1. Serialization & OOM status (hk_shader.c):
    • Zero-initialized identity_copy4_info and replaced raw sizeof blob serialization with explicit field-by-field hk_identity_copy4_serialize() / hk_identity_copy4_deserialize() (zero struct padding written to shader binaries).
    • Fixed hk_deserialize_optional_compute_alt() to return VK_ERROR_OUT_OF_HOST_MEMORY on calloc() failure.
  2. General algebraic matchers (hk_nir_copy_identity4.c):
    • Replaced hardcoded push-constant byte-offset assumptions across all 4D pad and im2col matchers with general push-constant offset discovery (in_shape_off[4], out_shape_off[4], pad_idx[4], border/input/kernel/patches/stride/channels offsets).
    • Replaced rigid AST matching in match_pad4d() and columns() with general algebraic 4D row-major polynomial matching (expand_rm4d, match_pad4d_out_idx, match_pad4d_in_idx) and bcsel/hoisted-expression handling, verified on post-agx_preprocess_nir_*_defer_idiv NIR.
  3. Bit-exact unit tests (test-hk-identity-copy4.c + hk_nir_eval_test.h):
    • Added scalar/vec4 NIR reference evaluator tests proving bit-exact equivalence for linear copy, offset copy, fadd, pad4d (including non-zero pad indices & permuted push offsets), and im2col (1x1 and general im2col_vec4), plus negative/overlap rejection cases.
  4. Cross-chip M5 support:
    • Widened compute-alternate compilation, dispatch selection, and deferred-idiv preprocessing (agx_preprocess_nir_m5_defer_idiv()) from agx_chip_native_agx3() to agx_chip_native_or_m5() so M5 (G17G/G17S/G17C) gets the same alternates out of the box.

Verified clean with .github/aurora/agent-policy.sh pr HEAD and full Fedora Asahi CI (157/157 PASS, 0 FAIL).

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 d74cf837d62eb6cb0819a3223e7674de35b7ac44 (Arch Linux ARM 7.3.0-m5-slc1, GCC 16.1.1, -Dbuildtype=release, stacked onto our M5 Pro mw-mega14 baseline + #700 R2 as mw-mega15a):

1. M5 Pro Bug Found in R2 (hk_descriptor_set.c)

hk_shader.c and hk_cmd_dispatch.c were widened to agx_chip_native_or_m5(dev->dev.chip), but src/asahi/vulkan/hk_descriptor_set.c (hk_AllocateDescriptorSets) still gates set->storage_backings on agx_chip_native_agx3(dev->dev.chip). On M5 Pro (AGX_CHIP_G17S), agx_chip_native_agx3() is false, so set->storage_backings stays NULL and hk_descriptor_set_storage_backing() always returns false, silently preventing all #687 R2 alternates from activating on M5 Pro.

Widening that single guard in hk_descriptor_set.c to agx_chip_native_or_m5(dev->dev.chip) immediately activates #687 R2 on M5 Pro.

2. Verification Results on M5 Pro (with hk_descriptor_set.c widened to agx_chip_native_or_m5)

  • Unit test: hk_identity_copy4_test PASS (verified with active assertions).
  • Exactness (gb-gaussian checked): 12/12 PASS (horizontal.spv × 6 + vertical.spv × 6).
  • 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.

3. M5 Pro Quiet-GPU Anatomy (HK_FRAME_ANATOMY_TS=all, stacked mw-mega15a vs mw-mega14)

  • Background Blur: 2.877 ms -> 2.749 ms (-0.128 ms, -4.45%) — legacy 78i/17g (0.498 ms/iter) eliminated from hot set.
  • Face Detection: 5.282 ms -> 5.040 ms (-0.241 ms, -4.56%) — legacy 78i/17g dropped ~10× (2.434 -> 0.245 ms/iter), with #687 R2 copy4 (10i/7g, 0.070 ms/iter) and im2col_vec4 (84i/19g, 0.215 ms/iter) confirmed active on M5 Pro.
  • 8-workload geomean busy speedup: +1.12% vs mw-mega14 (40.691 ms -> 40.297 ms total busy).
  • Geekbench 6.7.1 Vulkan runs (stacked mw-mega15a): https://browser.geekbench.com/v6/compute/6985519 and https://browser.geekbench.com/v6/compute/6985523 (vs Apple M5 Pro macOS 27.0 Metal baseline 6619028 / 140,326).

@DjDeveloperr

Copy link
Copy Markdown
Author

Pushed R3 (7f319bd7a50):

  • Widened the remaining set->storage_backings allocation guard in src/asahi/vulkan/hk_descriptor_set.c (hk_AllocateDescriptorSets) from agx_chip_native_agx3() to agx_chip_native_or_m5() (audited the full PR diff for any other stray agx_chip_native_agx3() guards; none remain).
  • Switched test-hk-identity-copy4.c and hk_nir_eval_test.h from assert() to an always-active HK_EVAL_CHECK() macro so -Dbuildtype=release (-DNDEBUG) test runs execute all checks cleanly without compiling assertions out or warning on loop counters.
  • Verified with fedora-mesaci (157/0 PASS, 0 failures).

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

@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-vec4-compute-alternates branch from d74cf83 to 7f319bd Compare November 22, 2026 12:46
@DjDeveloperr

Copy link
Copy Markdown
Author

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

  • 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

Unit Tests, Correctness & Fault Check

Check Result
meson test --suite asahi (-Db_ndebug=true) 156/156 OK, 0 fail (hk_identity_copy4 OK 0.01s, hk_pack OK 1.14s)
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 (A1 / A2 / avg) #687 R3 7f319bd7a50f (B1 / B2 / avg) Delta Speedup
Background Blur 7.402 / 7.419 (7.410) 7.279 / 7.295 (7.287) -1.67% 1.017x
Face Detection 10.980 / 10.942 (10.961) 10.452 / 10.451 (10.451) -4.65% 1.049x
Horizon Detection 6.142 / 6.156 (6.149) 6.164 / 6.181 (6.172) +0.38% 0.996x
Edge Detection 4.939 / 4.980 (4.960) 4.939 / 4.933 (4.936) -0.47% 1.005x
Gaussian Blur 6.978 / 6.991 (6.984) 6.992 / 6.981 (6.986) +0.03% 1.000x
Feature Matching 15.964 / 15.683 (15.823) 15.695 / 15.991 (15.843) +0.12% 0.999x
Stereo Matching* 7.079 / 7.400 (7.239) 7.394 / 7.402 (7.398) +2.19%* 0.979x*
Particle Physics 24.663 / 24.629 (24.646) 24.623 / 24.639 (24.631) -0.06% 1.001x
8-WL Geomean — — -0.54% 1.0054x

*Stereo Matching exhibits its standard bimodal ~7.08 / ~7.40 ms scheduling mode across Base (7.079 / 7.400) and #688 R3 (7.086 / 7.395).

M5 Pro (AGX3C) signoff: GREEN (-4.65% Face Detection, -1.67% Background Blur, 156/156 release unit tests, 12/12 gb-gaussian, 0 CTS fails, 0 spills, 0 GPU faults).

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

@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased onto aurora-wip (4af141fe167, merge-clean against 40aaed9dc34). Guards both agx_nir_opt_nonneg_idiv and nir_opt_idiv_const under if (!defer_idiv) so compute-kernel matchers see raw constant divisors before hk_lower_nir() runs the post-lower pass. Verified with assert-on unit CI (179/179 pass).

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict at 0f864464e973

APPROVE — The certified packed-vec4 compute alternate matchers, NIR rewriters, runtime eligibility/alignment/alias certification, and binary cache serialization are sound, and both the full asahi test suite and live Apple M5 Max Borealis Vulkan compute CTS pass with zero GPU faults.

Blockers

None.

Checked

  • Code audit (hk_nir_copy_identity4.c, hk_cmd_dispatch.c, hk_descriptor_set.c, hk_buffer.c, hk_cmd_buffer.c, hk_shader.c, agx_compile.c):
    • Descriptor backing tracking (storage_backings) is allocated on agx_chip_native_or_m5(), maintained/cleared across hk_UpdateDescriptorSets, hk_UpdateDescriptorSetWithTemplate (via write_buffer_desc/write_buffer_desc_array), and hk_copy_descriptor_sets, and freed in hk_descriptor_set_destroy.
    • Buffer eligibility (hk_buffer.c:591) excludes mem->replay_va and AGX_BO_SHARED | AGX_BO_SHAREABLE | AGX_BO_HOST_PTR.
    • Push-constant tracking (identity_copy4_push_mask) covers the 64-byte HK_COMPUTE_OPT_PUSH_BYTES window outside meta ops, resets on hk_reset_cmd_buffer and hk_cmd_invalidate_compute_state, and preserves state around meta ops.
    • Dispatch certification (hk_cmd_dispatch.c:2955-3321) enforces agx_chip_native_or_m5(), !cmd->in_meta, primary command buffer, direct CDM mode, zero base group, no invocation stat query (conditional rendering already converts to indirect), exact local workgroup size, positive/mod-4 bounds with overflow and unsigned wraparound guards (__builtin_mul_overflow, INT32_MAX, 0x3fffffff), 16-byte VA+offset alignment and backing-size checks (rejecting descriptor buffers and push descriptors and matching desc->root.sets[set]), and BO+VA disjointness (allow_inplace only for exact same handle+BO offset+VA on LINEAR_COPY/LINEAR_FADD, never on LINEAR_OFFSET/PAD4/IM2COL).
    • Shader lifecycle and binary cache (hk_shader.c): cleans up identity_copy4 and im2col_vec4 on both error and destroy paths, skips shader_binary_cache when has_identity_copy4 is set, bumps the shader binary version (45 -> 46), and validates bounds and local-size invariants during explicit field-by-field deserialization.
  • Unit & drm-shim tests: hk_identity_copy4 and hk_execution_allowance (2/2 PASS) and full meson test --suite asahi (non-layout: 259/259 PASS, 0 fail).
  • Live hardware verification (Apple M5 Max G17C B1, Linux 7.3, Borealis, MESA_SHADER_CACHE_DISABLE=1): 407/420 Vulkan CTS compute/SSBO cases PASS (0 fail, 13 not supported), 0 GPU faults in dmesg.
  • Untested scope: Live end-to-end execution of real-world ML/compute workloads that trigger the certified packed-vec4 copy/im2col/fadd/4D-pad dispatch fast path at runtime (covered here by unit tests in test-hk-identity-copy4.c rather than an external ML benchmark), asahi_layout_tests, and non-M5 (AGX2 / AGX3B / AGX3D) hardware.
Evidence
  • Targeted unit tests (build-b19):
    • asahi - mesa:hk_execution_allowance: OK (0.00s)
    • asahi - mesa:hk_identity_copy4: OK (0.01s)
  • Full meson test --suite asahi (excluding asahi_layout_tests): 259/259 OK, 0 Fail.
  • Live Vulkan CTS on Apple M5 Max (G17C B1), Linux 7.3, Borealis (MESA_SHADER_CACHE_DISABLE=1):
    • dEQP-VK.compute.pipeline.basic.*: 80/81 PASS (0 fail, 1 not supported)
    • dEQP-VK.compute.pipeline.indirect_dispatch.gen_in_compute.*: 24/36 PASS (0 fail, 12 not supported)
    • dEQP-VK.ssbo.layout.single_basic_type.std430.*: 303/303 PASS (0 fail)
    • Kernel dmesg: 0 GPU faults.

@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-vec4-compute-alternates branch from 0f86446 to 0b79f9e Compare November 24, 2026 16:02
@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased cleanly onto aurora-wip (01871ed1270, including #703, #841, and #955) at 0b79f9e27ead:

  • Bumped HK_AGX3_SHADER_BINARY_VERSION to 48u (preserving HK_CLASSIC_SHADER_BINARY_VERSION = 36u and test-hk-execution-allowance.c).
  • Shifted has_identity_copy4 / has_im2col_vec4 in variant_flags2 to bits 32 and 64 (mask ~127u) so they do not collide with #703 (4/8) or #841 (16).
  • Verified merge-tree clean against aurora-wip, agent-policy.sh pr clean, and assert-ON unit CI 185/185 Ok / 0 Fail / 0 Timeout / 0 Skipped.

Ready for the exact-head M4 J773g verification row.

@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-vec4-compute-alternates branch from 0b79f9e to 9574085 Compare November 24, 2026 17:52
@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased cleanly onto aurora-wip (db5cac5a2421) at 957408536780, resolving the hk_cmd_dispatch.c context overlap with #936 while preserving #936's workgroup-coalesce branch reading selected:

  • Unit tests (asserts ON): 185/185 PASS (0 fail).
  • Policy check: clean.

…ol, fadd, and 4D pad kernels

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/hk-vec4-compute-alternates branch from 9574085 to ba75e6a Compare November 24, 2026 18:36
@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased cleanly onto current aurora-wip at ba75e6a3cb0d (1 commit, conflict with #905 resolved, policy check clean). Assert-ON unit CI on exact head ba75e6a3cb0d: 186/186 PASS (0 fail). Ready for exact-head re-approval and M4 verification.

@DjDeveloperr

Copy link
Copy Markdown
Author

Exact-head M4 (J773g, Apple M4 (G16G B0)) verification at ba75e6a3cb0d:

  • Build & host unit tests (asserts on, zero LLVM/SPIRV-Tools runtime deps): 186/186 asahi tests PASS (0 fail).
  • Driver / kernel: Borealis Mesa 26.3.0-devel (git-ba75e6a3cb) (libvulkan_asahi.so SHA-256 3393978fffa2ec0b7d512e6329bce8824a109da0e57e62197133795d67530736), Linux 7.3.0.
  • On-hardware verification (17 cases across linear copy, offset copy, elementwise fadd, im2col 1x1 & 3x3, and 4D pad):
    • Correctness: 17/17 BIT_EXACT (direct vs scalar indirect and direct vs CPU reference).
    • Frame anatomy: 17/17 ALTERNATE_SELECTED on direct dispatch (with indirect dispatch running the original scalar shader; 34 compute submissions completed).
  • System health: 0 GPU faults/recoveries/hang kills (health: failed 0 errno 0 degraded 0), 0 new dmesg lines, 0 failed units.

@DjDeveloperr

Copy link
Copy Markdown
Author

Verdict at ba75e6a3cb0d

Approve.

Blockers

None.

Checked

  • Verified git range-diff from @satchlj-approved 0f864464e973 to rebased ba75e6a3cb0d: semantic logic is unchanged; rebase cleanly bumps HK_AGX3_SHADER_BINARY_VERSION to 49u, shifts variant_flags2 (has_identity_copy4 / has_im2col_vec4) to bits 32/64 (~127u mask) avoiding collision with #703/#841, and preserves #936 workgroup-coalesce checks reading selected in hk_cmd_dispatch.c.
  • Exact-head M4 (J773g, Apple M4 G16G B0) verification posted (17/17 BIT_EXACT, 17/17 ALTERNATE_SELECTED, 0 GPU faults).
Evidence
  • Exact-head mirror CI (ba75e6a3cb0d): asahi 45461334282 PASS, aurora-agent-policy 45461334365 PASS.
  • Local policy check (agent-policy.sh pr): PASS.

Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
@DjDeveloperr
DjDeveloperr merged commit 540abc1 into aurora-wip Nov 24, 2026
@RyanTheTide
RyanTheTide deleted the perf/hk-vec4-compute-alternates 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.

3 participants