Skip to content

hk,agx: version aligned robust compute loops on G16G and refine preamble weights - #1494

Merged
DjDeveloperr merged 1 commit into
aurora-silicon:aurora-wipfrom
IronsideXXVI:j604-hk-aligned-robust-loops
Dec 2, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-silicon:aurora-wipfrom
IronsideXXVI:j604-hk-aligned-robust-loops

Conversation

@IronsideXXVI

Copy link
Copy Markdown

What: Add g16_version_aligned_robust_loops() in src/asahi/vulkan/hk_shader.c for G16G (!soft_fault) compute loops with AoS bounded buffer loads, and update src/asahi/compiler/agx_nir_opt_preamble.c to model nir_op_usub_sat at its 2-instruction AGX cost (2.0) and preserve preamble hoisting weight for loops nested inside uniform branches.

Tested:

  • Built on aurora-wip (f6ea9a1b, conflict-free against 866b091e) in an aarch64 Linux container (gcc 16.1.1) with 0 warnings in hk_shader.c and agx_nir_opt_preamble.c; agx_tests: 1884 passed / 0 failed (1 skipped); libvulkan_asahi.so linked cleanly.
  • Shader-level verification (ASAHI_SHIM_CHIP=g16g, NIR_DEBUG=validate, agx_validate()): 27/27 G16G vk-sdb shaders and 32/32 omarchy-mlx Qwen3.5-2B shaders pass with 0:0 spills and 0 occupancy regressions; real llama-bench Vulkan compute hot loops drop MUL_MAT q4_K 530 -> 466 (-64 / -12.1%), MUL_MAT q6_K 748 -> 626 (-122 / -16.3%), and FLASH_ATTN_EXT 2399 -> 2391 (-8), keeping 127 GPRs / 832 threads.
  • Apple M4 MacBook Pro 14" (J604, T8132 / G16G), Llama-3.2-1B-Instruct-Q4_K_M (llama-bench, n=10, 0 GPU faults):
    • Combined 1.8317 (0025+0026+0027 vs 1.8314): pp512 1379.00 tok/s (+8.90%, spread=0.52%, 0 faults).
    • Shipped combined 1.8320 (on GOOD 1.1677): pp512 1445.90 tok/s (+14.18%, 89.8% of native Metal, spread=0.50%), tg128 92.50 tok/s (+4.18%, iqr=0.20%), 0 GPU faults.

Risk: two-way door · M4 (G16G / T8132) and AGX preamble heuristic — g16_version_aligned_robust_loops() is gated to AGX_CHIP_G16G && !soft_fault and preserves exact robustBufferAccess / robustBufferAccess2 / nullDescriptor semantics by checking bound % stride == 0 once before the loop and routing any unaligned descriptor size to the unmodified fallback loop; preamble cost/weight changes only tune hoisting priority within existing uniform budgets.

Not yet: Standalone hardware A/B built directly from aurora-wip (a37a6ac1) and a non-M4 cross-generation hardware regression row for the agx_nir_opt_preamble.c heuristic change (we only have an M4 J604 locally).

Details and verification

Summary of changes (a37a6ac1 on aurora-wip f6ea9a1b)

  1. src/asahi/vulkan/hk_shader.c (g16_emit_global_load, g16_version_aligned_robust_loops):
    • On G16G (!soft_fault), compute loops containing AoS bounded buffer loads of the form offset = amul(ib, stride) + local with provable local + load_size <= stride <= 4096 < AGX_ZERO_PAGE_SIZE check bound % stride == 0 once before the loop.
    • In the aligned fast loop, all fields of each struct element share a single (valid, vbase, vblock) check (valid = block < bound), redirecting OOB blocks to AGX_ZERO_PAGE_ADDRESS while emitting unbounded global loads (nir_load_global / nir_load_global_constant) so downstream vectorization merges adjacent fields without per-component tail masks.
    • Unaligned descriptor sizes execute the unmodified fallback loop with exact robustBufferAccess / robustBufferAccess2 / nullDescriptor behavior.
    • Note: agx_nir_opt_shared_byte_windows from our earlier e705eca833f carry is already covered on aurora-wip by fold_elem_offsets_impl() in agx_nir_lower_shared_bitsize.c, and the 4-line nir_isub cleanup inside g16_merge_bounded_loads depends on hk,agx: vectorize bounded buffer loads on G16G without soft faults #1472 and is omitted here to keep this PR orthogonal and conflict-free.
  2. src/asahi/compiler/agx_nir_opt_preamble.c:
    • Give nir_op_usub_sat its true 2-instruction AGX cost (2.0 for iadd + icmpsel) instead of 1.0.
    • Preserve preamble hoisting weight for loops nested inside uniform branches (weight *= 8.0f per enclosing loop, clamped to MIN2(weight, 1.0f)).

Verification

  • ninja -C /build -j6 src/asahi/compiler/agx_tests src/asahi/vulkan/libvulkan_asahi.so: 0 warnings in hk_shader.c and agx_nir_opt_preamble.c.
  • /build/src/asahi/compiler/agx_tests: 1884 passed, 1 skipped, 0 failed.

@IronsideXXVI
IronsideXXVI requested review from a team and DjDeveloperr as code owners December 1, 2026 17:50
@github-actions github-actions Bot added area:borealis Area from CODEOWNERS (bot) area:compiler Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Merge readiness at 4ab76867: not ready yet

Check Detail
CI at head ok no CI for this change (the hardware row is the gate)
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head no no line with the head SHA 4ab76867, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · M4 (G16G / T8132) and AGX preamble heuristic — g16versionalignedrobustloops() is gated to AGXCHIPG16G && !softfault and preserves exact robustBufferAccess / robustBufferAccess2 / nullDescriptor semantics by checking bound % stride == 0 once before the loop and routing any unaligned descriptor size to the unmodified fallback loop; preamble cost/weight changes only tune hoisting priority within existing uniform budgets.

Areas: area:borealis, area:compiler

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

@DjDeveloperr DjDeveloperr 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.

g16_parse_invariant_bound accepts any bound defined outside the loop. That proves loop invariance but allows a per-invocation bound from a nonuniform SSBO array index. For example, with 32-byte records and lanes selecting 64- or 68-byte descriptors, g16_version_aligned_robust_loops splits the subgroup between the two loop copies. A subgroup reduction inside the original loop then sees a different active-lane set, and a workgroup barrier would also become divergent. Please require workgroup-uniform bounds for versioning and add a nonuniform descriptor test that leaves the loop intact.

@github-actions github-actions Bot added the changes-requested A reviewer requested changes (bot) label Dec 1, 2026
…ble weights

Two compute-kernel compiler improvements:

1. hk_shader.c (g16_version_aligned_robust_loops):
   - On G16G (!soft_fault), version compute loops containing AoS bounded
     buffer loads (offset = amul(ib, stride) + local, provable
     local + load_size <= stride <= 4096 < AGX_ZERO_PAGE_SIZE) on
     bound % stride == 0 once before the loop.
   - Require workgroup-uniform bounds via
     nir_divergence_analysis_impl(..., nir_divergence_across_subgroups) and
     nir_def_is_divergent_at_use_block() so per-invocation or per-subgroup
     nonuniform descriptor bounds never split subgroups or workgroup
     barriers.
   - Inside the fast loop, share one (valid, vbase, vblock) check per
     struct element across all fields of the element and emit unbounded
     global loads so downstream load vectorization merges adjacent fields
     without per-component tail masks.
   - Arbitrary unaligned descriptor sizes take the unmodified fallback
     loop with exact robustBufferAccess / robustBufferAccess2 /
     nullDescriptor semantics.
   - Add hk_aligned_robust_loop_test covering uniform descriptor
     versioning and per-lane / per-subgroup nonuniform descriptor rejection.

2. agx_nir_opt_preamble.c:
   - Model nir_op_usub_sat at its true 2-instruction AGX cost (iadd +
     icmpsel = 2.0) instead of 1.0.
   - Preserve preamble hoisting weight for loops nested inside uniform
     branches (weight *= 8.0f per enclosing loop, clamped to 1.0f).

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
@IronsideXXVI

Copy link
Copy Markdown
Author

Fixed g16_parse_invariant_bound() / g16_version_aligned_robust_loops() to require workgroup-uniform bounds:

  • Run nir_divergence_analysis_impl(impl, nir->options->divergence_analysis_options | nir_divergence_across_subgroups) before matching each loop and reject any outside-loop bound where nir_def_is_divergent_at_use_block(def, before_loop) is true (covering both per-invocation and per-subgroup nonuniform SSBO descriptor indices, while keeping dispatch-uniform descriptor loads and workgroup-uniform bounds eligible).
  • Added hk_aligned_robust_loop_test (src/asahi/vulkan/tests/test-hk-aligned-robust-loop.c) covering:
    1. Workgroup-uniform SSBO descriptor -> versions loop cleanly.
    2. Per-lane nonuniform SSBO descriptor array index with a subgroup reduction inside the loop -> leaves loop intact.
    3. Per-subgroup nonuniform SSBO descriptor array index with a workgroup barrier inside the loop -> leaves loop intact.

Verified offline with -Db_ndebug=false (hk_aligned_robust_loop_test: 3/3 PASS; agx_tests: 1891 PASS, 0 FAIL, 1 SKIPPED).

@IronsideXXVI
IronsideXXVI force-pushed the j604-hk-aligned-robust-loops branch from a37a6ac to 4ab7686 Compare December 1, 2026 21:54
@github-actions github-actions Bot added the needs-hw-row No hardware row at the PR head (bot) label Dec 1, 2026

@DjDeveloperr DjDeveloperr 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.

Rechecked the uniformity fix at 4ab768672c5d. Both outside-loop bound paths now require workgroup-uniform values, and divergence analysis treats differences between subgroups as divergent. The new tests cover the uniform case and leave the original loop intact for both lane-varying and subgroup-varying descriptors. The author reports all three cases passed. Please record a J604 hardware run at this head before landing, including the G16G robust-loop path.

@DjDeveloperr

Copy link
Copy Markdown

Row: 4ab768672c5daea596314f750f014d8fd1f55106 + J700 MacBook Neo (A18 Pro, G17P B1) + PASS. Same stock Fedora boot, kernel 7.3.0-41.fc44.aurora.aarch64, SELinux Enforcing. Against f6ea9a1be834, all 8,392 compute, robust buffer access and SSBO CTS statuses matched: 7,188 Pass and 1,204 NotSupported per arm, zero failures or GPU faults. The stock boot and Vulkan driver were verified at handback.

@IronsideXXVI Do you already have the J604 run booked? @erikwb If J604 is still pending, can J713 run the G16G comparison on its existing boot? I have the checksum-verified 10,152-case kit ready to send. It loads the two ICDs through VK_DRIVER_FILES without a reboot or package changes. The G16G result should cover the aligned robust-loop fast path and the unaligned fallback with matching output.

@RyanTheTide

Copy link
Copy Markdown

Non-M4 cross-generation row at exact head 4ab76867: PASS, 0 regressions, 0 GPU faults. This covers the non-M4 regression row in "Not yet" for the agx_nir_opt_preamble.c heuristic.

Method: base f6ea9a1b (merge-base with aurora-wip) vs PR, both cross-built from source and loaded through VK_DRIVER_FILES on the same boot (no install, no reboot).

Board Kernel VK CTS: compute, robustness buffer_access, robustness2 compute buffers, SSBO (28,076)
J615 MacBook Air M3 (G15G) 7.3.0-aurora-edge, Enforcing 9351 Pass→Pass, 18,725 NotSupported, 0 Fail (base and PR)

Not covered: a standalone A/B from aurora-wip on M4, and performance.

@DjDeveloperr

Copy link
Copy Markdown

J773g (M4 Mac mini, T8132, G16G B0) hardware row PASS at exact head 4ab768672c5d vs exact PR base f6ea9a1be834 on a aurora.fault_control=0 boot (DRM_ASAHI_FEATURE_SOFT_FAULTS disabled, verified chip=T8132 gpu=G16G features=0x41026001fc06 soft_faults=disabled so g16_version_aligned_robust_loops() is active).

  • Kernel: stock 7.3.0-ARCH-m4main-6c83a2a6 (boot_id=0592f16f-cada-4fd6-ba00-5568cf439988, cmdline with aurora.fault_control=0)
  • Drivers (same-boot A/B): Mesa 26.3.0-devel (git-f6ea9a1be8) vs Mesa 26.3.0-devel (git-4ab768672c)
  • Staged 10,152-case Vulkan CTS A/B (cases-1494.txt, 4-way sharded):
    • Base (f6ea9a1be834): 8,628 Pass / 0 Fail / 1,524 NotSupported (10s)
    • Candidate (4ab768672c5d): 8,628 Pass / 0 Fail / 1,524 NotSupported (10s)
    • 0 diffs
  • Aligned fast loop + unaligned fallback verification (36-byte AoS struct compute loop with robustBufferAccess=VK_TRUE, MESA_SHADER_CACHE_DISABLE=true):
    • Confirmed loop versioning fired on candidate (AGX3 binary size 2038 -> 3984, preamble 160 -> 252, jmp_exec_any count 6 -> 12)
    • Verified bit-exact output match (diff = 0) between base and candidate across 4 aligned descriptor ranges (range=2304, 1152, 360, 36, mod 36 == 0, incl. OOB tail iterations) and 5 unaligned fallback ranges (range=2300, 1148, 356, 20, 4, mod 36 != 0, incl. partial-struct tail reads)
  • GPU health & kernel state: health: failed 0 errno 0 degraded 0, classes: render_failed 0 compute_failed 0, recovery_receipts: affected_full 0 aborted 0, 0 GPU faults/Oops/WARN.

@DjDeveloperr

Copy link
Copy Markdown

Merge-tree hardware verification on Apple M4 Mac mini (J773g, T8132, G16G B0, aurora.fault_control=0): PASS, 0 diffs, 0 GPU faults, 0 recoveries.

  • Commits: aurora-wip tip 3db5ab65f06f5432c65a9449024e35ee3227ae21 vs merge tree tip + 4ab768672c5daea596314f750f014d8fd1f55106 (clean git merge-tree 488a99bd527479d4cd0313dd2ebddd97417552ba, after #1489 load_global sinking in hk_lower_nir_ex).
  • Environment: 7.3.0-ARCH-m4main-6c83a2a6 (boot_id=0592f16f-cada-4fd6-ba00-5568cf439988, soft_faults=disabled), same-boot A/B via VK_DRIVER_FILES, clean-builddir AArch64 Borealis release ICDs (tip sha256 2e87c3acd9255191690e48115e6145ea9d45c6a8549388f5d7782b0ec8ec1a00, merge-1494 sha256 ad6449ee758956003ddcb13fd9eba9a26057f551eb0d32e64343104a0893ab4f).
  • Line 118 CTS case set (cases-1494.txt, 10,152 cases, solo, MESA_SHADER_CACHE_DISABLE=true):
    • tip 3db5ab65f06f: 8,628 Pass / 0 Fail / 1,524 NotSupported / 0 Warnings.
    • tip + #1494: 8,628 Pass / 0 Fail / 1,524 NotSupported / 0 Warnings.
    • Per-case status diffs: 0/10,152.
  • 36-byte AoS aligned/unaligned compute probe (aos36.spv, MESA_SHADER_CACHE_DISABLE=true):
    • AGX_MESA_DEBUG=shaders confirms g16_version_aligned_robust_loops() fires on the merge tree (AGX3 main 2830 -> 4776 B, preamble 160 -> 228 B).
    • Bit-exact output match vs tip 3db5ab65f06f across all 9 cases: 4 aligned fast-loop ranges (2304, 1152, 360, 36 B, mod 36 == 0) and 5 unaligned fallback ranges (2300, 1148, 356, 20, 4 B, mod 36 != 0).
  • GPU health / faults: health: failed 0 errno 0 degraded 0, recoveries 0, hang_kills 0, quarantined 0, 0 new GPU faults in dmesg.

@DjDeveloperr

Copy link
Copy Markdown

Local CI passed on 4ab768672c5d merged into aurora-wip 0493532f54dd (tree c39d3417c37e). The Fedora x86-64 Asahi workflow equivalent passed 389 Asahi/poly tests with one skip, all 58 Vulkan tests, and the agent-policy selftest.

The Asahi compiler, NIR, and HK shader pipeline source matches the J773g merge-tree A/B byte for byte. That run exercised both the aligned fast loop and unaligned fallback with matching outputs, zero CTS differences across 10,152 cases, and zero GPU faults. The J700 control and the earlier Arm64 CI also passed.

@DjDeveloperr
DjDeveloperr merged commit b131e6a into aurora-silicon:aurora-wip Dec 2, 2026
8 checks passed
DjDeveloperr added a commit that referenced this pull request Dec 2, 2026
Forward-merge aurora-wip at e86174f (through #1497).

hk_shader.c: this branch moved the pre-bounds optimization and bounds
lowering out of hk_lower_nir_ex() into hk_lower_nir_buffer_bounds();
#1494 added the G16G aligned robust-loop versioning to that block.
Carry #1494's step into hk_lower_nir_buffer_bounds() unchanged and pass
the pipeline robustness state in, which both callers already hold. The
function body now matches aurora-wip's inline block except for the
optimize parameter this branch introduced.

hk_cmd_draw.c: keep sparse_bda_free_stages next to #1522's
native-AGX3 bind_tail_compact and draw_state_offset fields.

meson.build: keep both new tests (hk_m5_lazy_sparse_bda and
hk_queue_family_transfer).

Signed-off-by: DjDeveloperr <hey@djdev.me>
DjDeveloperr pushed a commit that referenced this pull request Dec 2, 2026
…tfield-slices

Resolve conflicts against aurora-wip (e86174f) after #1494 and #1497 merged:
- src/asahi/vulkan/hk_shader.c: keep g16_try_version_bounded_loop() and its call before the landed #1494 aligned-loop fallback.
- src/asahi/compiler/agx_nir_lower_shared_bitsize.c: keep #1497 shared-index reassociation followed by #1495 bitfield-slice folding.
- src/asahi/compiler/test/test-shared-byte-offsets.cpp: keep #1497 shared-index tests followed by #1495 bitfield-slice tests.

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
DjDeveloperr pushed a commit that referenced this pull request Dec 2, 2026
Resolve conflicts in src/asahi/vulkan/hk_shader.c after #1494 landed on
aurora-wip (e86174f):
- Keep both g16_version_aligned_robust_loops() (on AGX_CHIP_G16G, with
  follow-up CSE when versioned) and hk_nir_g16_merge_bounded_loads()
  under !global_loads.soft_fault in hk_lower_nir_ex().
- Deduplicate identical static helper g16_emit_global_load() shared by
  both passes.

All other hunks auto-merged cleanly without changing any of #1472's own logic.

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
DjDeveloperr pushed a commit that referenced this pull request Dec 2, 2026
…s-and-imadshl-bitfield-slices

Brings in the forward-merge of aurora-wip (e86174f, after #1494 and
#1497 landed) from #1495 (b1b7663) cleanly with zero conflicts and
no changes to #1496's own hunks.

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
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) area:compiler Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) changes-requested A reviewer requested changes (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants