Skip to content

agx: canonicalize shared-memory element indices across disjoint ior and imadshl_agx - #1497

Merged
DjDeveloperr merged 1 commit into
aurora-silicon:aurora-wipfrom
IronsideXXVI:j604-agx-shared-index-reassoc
Dec 2, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-silicon:aurora-wipfrom
IronsideXXVI:j604-agx-shared-index-reassoc

Conversation

@IronsideXXVI

@IronsideXXVI IronsideXXVI commented Dec 1, 2026 •

Copy link
Copy Markdown

What: Add agx_nir_opt_shared_index_reassoc() for AGX3 compute shaders with shared memory so modular 32-bit u2u16(...) element indices are canonicalized into (sorted variable terms) + constant even when the constant offset exceeds LDTG/STTG's 8-bit immediate byte offset. Uses known-zero-bit analysis to peel affine constants out of disjoint ior(x, c), imul/amul/ishl, and imadshl_agx (including nested shift-left imadshl_agx(0, 1, inner_imadshl, sh)), followed by CSE and a second pass when progress occurs so sibling tile loads/stores share one base index register.

Tested:

  • Offline unit tests on origin/aurora-wip (3f9b0bcf, gcc 16.1.1, aarch64, -Db_ndebug=false): agx_tests 1902 passed / 0 failed (1 skipped; 30/30 SharedByteOffsets including SharedIndexReassocDisjointOrAndNestedImadshl and SharedIndexReassocImadshlNonzeroShiftValueEquiv), 0 warnings on modified files.
  • Offline ASAHI_SHIM_CHIP=g16g NIR_DEBUG=validate across all 16 llama-bench (Llama-3.2-1B-Instruct-Q4_K_M) pipelines and all 27 G16G vk-sdb pipelines: 0 validation errors, 0 spills (27/27 vk-sdb pass; mlx_matmul_coopmat -114 instrs, qmm_vec_q4_word_sub_bf16 -52 instrs, mul_mm_f16_full -28 instrs & 832->896 thr).
  • Hardware on J604 (14-inch MacBook Pro M4, T8132 / G16G), Llama-3.2-1B-Instruct-Q4_K_M (llama-bench, n=10 QUIET, 0 GPU faults): shipped combined 1.8321 build vs 1.8320 measured pp512 1507.59 tok/s (+4.27% vs 1445.90, min=1498.80 max=1509.98 spread=0.74%), tg128 92.59 tok/s (iqr=0.32% spread=0.58%), live PERF62 q4_K=270.5ms (-11.1ms), q6_K=48.5ms (-3.6ms), fa=22.4ms (-0.4ms), 0 GPU faults.

Risk: two-way door · M4 / M5 (AGX3 compute compiler) — modular 32-bit addition truncated by u2u16 is unconditionally associative and commutative; ior(x, c) distribution is strictly gated on (known_zero_bits_u32(x) & c) == c so carry-free equivalence x | c == x + c holds for every input. Scoped to AGX3 shaders with shared_size > 0.

Not yet: Standalone per-PR M4 benchmark isolation at head 3f9b0bcf and a second-generation (M1/M2/M5) regression capture.

Offline & hardware details
  • Base: origin/aurora-wip (866b091e, standalone, zero stacking dependencies)
  • Files: src/asahi/compiler/agx_nir_lower_shared_bitsize.c, src/asahi/compiler/agx_nir.h, src/asahi/compiler/agx_compile.c, src/asahi/compiler/test/test-shared-byte-offsets.cpp
  • Combined 1.8321 vs 1.8320 on ASAHI_SHIM_CHIP=g16g: MUL_MAT q4_K hot loop 375 -> 299 (-20.3%), MUL_MAT q6_K hot loop 508 -> 377 (-25.8%), FLASH_ATTN_EXT 159 -> 153 GPRs, 0:0 spills across all 16 pipelines.

@IronsideXXVI
IronsideXXVI requested review from a team and DjDeveloperr as code owners December 1, 2026 20:09
@github-actions github-actions Bot added area:compiler Area from CODEOWNERS (bot) merge-conflict Conflicts with the base branch (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Merge readiness at 3f9b0bcf: 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 3f9b0bcf, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text no add a 'Tested:' line; a 'Risk:' line (door and Macs) (guide)

Areas: area:compiler

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

@IronsideXXVI
IronsideXXVI force-pushed the j604-agx-shared-index-reassoc branch from 00f8df9 to bf295d6 Compare December 1, 2026 20:20
@github-actions github-actions Bot removed the merge-conflict Conflicts with the base branch (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.

Two shared-address rewrites use the wrong IMADSHL semantics. NIR defines imadshl_agx(a, b, c, k) as a*b + (c << k). In gather_sum_mod32, peeling the 1 from imadshl_agx(x + 1, 2, y, 1) adds 4 to the replacement, though it should add 2. The nested cases must also scale the inner product when folding an outer shift. In known_zero_bits_u32, imadshl_agx(x, 1, y, 1) can be odd when x is odd, so its low bit cannot be used to turn OR 1 into +1. Please fix both paths and add value-equivalence cases with nonzero shifts, including the nested case. The current test uses shift zero on the inner IMADSHL and only checks opcode counts.

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

Add agx_nir_opt_shared_index_reassoc() for AGX3 compute shaders with
shared memory. Modular 32-bit index sums truncated by u2u16 are
unconditionally associative and commutative, even when the constant
offset exceeds LDTG/STTG's 8-bit immediate byte offset.

Using known-zero-bit analysis, peel affine constants out of:
- disjoint ior(x, c) where (known_zero_bits(x) & c) == c,
- imul/amul/ishl(x + c, k) and imul/amul/ishl(x | c, k), and
- imadshl_agx(x + c, k, addend, sh) = ((x + c) * k) + (addend << sh),
  including nested outer shifts (ishl and imadshl_agx(0, _, inner, sh))
  which scale the inner multiplier by (1 << outer_sh), peel
  (c * k) << outer_sh, and enforce the hardware shift domain (tot_sh <= 4).

Run a second reassoc+CSE pass when progress occurs so sibling tile
loads and stores share a single base index register.

Add unit tests SharedIndexReassocDisjointOrAndNestedImadshl and
SharedIndexReassocImadshlNonzeroShiftValueEquiv verifying exact value
equivalence across direct/nested nonzero shifts, odd-product ior
preservation, and shift-domain limits.

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

Copy link
Copy Markdown
Author

Good catch on both imadshl_agx(a, b, c, k) = (a * b) + (c << k) paths. Fixed in agx_nir_lower_shared_bitsize.c:

  1. gather_sum_mod32:
    • Direct imadshl_agx: peeled constant is now cv * kv (unshifted by sh).
    • Nested outer shift (ishl and zero-product imadshl_agx(0, _, inner, sh)): now scales the inner multiplier to kv << outer_sh, peels (cv * kv) << outer_sh (shifted by the outer shift only), and enforces the hardware imadshl_agx shift domain tot_sh <= 4.
  2. known_zero_bits_u32:
    • Non-zero-product imadshl_agx now computes min(tz(a) + tz(b), tz(c) + k) instead of assuming the product is shifted by k, so odd products like imadshl_agx(x, 1, y, 1) cannot fold | 1 into + 1.
    • Zero-product imadshl_agx(0, _, c, k) preserves exact c << k zero bits.
  3. Tests (test-shared-byte-offsets.cpp):
    • Added eval_shared_u16_offsets() value-equivalence checks to SharedIndexReassocDisjointOrAndNestedImadshl and added SharedIndexReassocImadshlNonzeroShiftValueEquiv covering direct nonzero shift (imadshl_agx(x + 1, 2, y, 1)), nested ishl + nested zero-product imadshl_agx with nonzero inner shifts, odd-product ior(..., 1) rejection, and tot_sh > 4 rejection.

Verified offline with -Db_ndebug=false (agx_tests: 1902 PASS, 0 FAIL, 1 SKIPPED; 30/30 SharedByteOffsets).

@IronsideXXVI
IronsideXXVI force-pushed the j604-agx-shared-index-reassoc branch from bf295d6 to 3f9b0bc Compare December 1, 2026 21:50
@IronsideXXVI

Copy link
Copy Markdown
Author

Quick hardware update: the revised imadshl_agx + known_zero_bits_u32 fix from 3f9b0bcf just shipped on J604 (14-inch MacBook Pro M4, T8132, G16G B0) in our combined 1.8323 (e705eca833f-based) build:

  • llama-bench (Llama-3.2-1B-Instruct-Q4_K_M, n=10/10): pp512 = 1509.82 tok/s, tg128 = 92.49 tok/s, q4_K = 269.77 ms, GL smoke pass, 0 GPU faults.
  • Standalone A/B and cross-generation CTS at the exact aurora-wip head 3f9b0bcf remain under Not yet.

@RyanTheTide

Copy link
Copy Markdown

Hardware row at exact head 3f9b0bcf: PASS, 0 regressions, 0 GPU faults on M2 (AGX2) and the Neo. This covers the M1/M2/M5 part of the PR's "Not yet": M2 is covered, M1 and M5 are not (no row on those here).

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

Board Kernel / system Mesa VK list A: compute shared/workgroup + memory_model (19,790) VK list B: 8/16-bit storage, float16, variable pointers, compute basic (8,918) GLES31 compute (195)
J473 Mac mini M2 (T8112, G14G) 7.3.0-aurora-fleet94, Enforcing 4902 Pass→Pass, 0 Fail 1808 Pass→Pass, 0 Fail 17 Pass→Pass, 178 Fail→Fail*
J700 Neo (G17P) 7.3.0-aurora-edge, Enforcing 4942 Pass→Pass, 0 Fail 1810 Pass→Pass, 0 Fail 195 Pass→Pass

All other cases were NotSupported on both base and PR. KERNEL_GPU_FAULTS: 0 on both boards.

* The J473 GLES31 compute failures are the same on base and PR. They come from the known M2 kernel issue linux#3851 (an empty compute stream returns EINVAL and later compute in the same context fails), not from this PR.

Not covered: M1 and M5 hardware, and the per-PR M4 benchmark.

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

Re-reviewed at 3f9b0bcf. Both IMADSHL paths now follow a*b + (c << k): the direct peel adds cv*kv unshifted, the nested and zero-product folds scale the inner multiplier by the outer shift and stay within the shift-4 limit, and imadshl_agx(x, 1, y, 1) no longer counts as even. The new value-equivalence cases cover nonzero shifts, the nested forms and the refused shift-5 case. This clears my earlier request.

@DjDeveloperr

Copy link
Copy Markdown

J773g (Apple M4 Mac mini, T8132, G16G B0) same-boot Vulkan A/B receipt — PASS.

  • Hardware / Kernel: J773g (G16G B0), stock 7.3.0-ARCH-m4main-6c83a2a6 (6c83a2a6453968b794a7925f0d6b59bc4d4592b3, boot bdc9b5c8-7648-4db4-8d48-640073f090ba, plasmalogin active, DP-3 connected)
  • Base arm: 24054d41dc390274aa54838bfb4327b3fb1fe413 (libvulkan_asahi.so sha256 0a560a914b8c3172a958132c641adf0803a51fd8aeb5efc47e7526d41d85faab)
  • Head arm: 3f9b0bcfabcb9da2323f8befea55a42ed165d0bd (libvulkan_asahi.so sha256 d3a19abd6b4f4fded1f045570d2b6d1ab974384eda6d3bf635acf81b413a63a5)
  • VK CTS A/B (1/10th stride across 64,939 compute shared/workgroup, memory_model, 8/16-bit storage, float16, variable_pointers, compute.pipeline.basic cases = 6,494 cases, staged VK_DRIVER_FILES, MESA_SHADER_CACHE_DISABLE=true, VK_LOADER_DEBUG=driver verified):
    • Base: 1,625 Pass, 4,869 NotSupported, 0 Fail
    • Head: 1,625 Pass, 4,869 NotSupported, 0 Fail
    • Diffs: 0
  • GPUBench --verify A/B (compute.light d=1/4):
    • compute.light d=1: 111.16 us -> 111.51 us (valid=true)
    • compute.light d=4: 43.66 us -> 44.28 us (valid=true)
  • GPU health: health: failed 0, recoveries 0, affected_full 0 aborted 0, 0 new kernel GPU faults.

@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 + 3f9b0bcfabcb9da2323f8befea55a42ed165d0bd (clean git merge-tree 487643cfd665a6eda0dd1791359b07d4e828689e, after #1509 shared-memory tree-reduce pass in agx_compile_shader_nir_internal).
  • 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-1497 sha256 2d8797a7bb3ae3f1c604e03a9a1a87e18cf4041be90c3885478fa6dc861a8763).
  • Line 68 CTS lists A+B (pr1497-run.txt, 6,494 cases: 1/10th stride across 64,939 compute shared/workgroup, memory_model, 8/16-bit storage, float16, variable pointers, compute.pipeline.basic, solo, MESA_SHADER_CACHE_DISABLE=true):
    • tip 3db5ab65f06f: 1,661 Pass / 0 Fail / 4,833 NotSupported / 0 Warnings.
    • tip + #1497: 1,661 Pass / 0 Fail / 4,833 NotSupported / 0 Warnings.
    • Per-case status diffs: 0/6,494.
  • GPU health / faults: health: failed 0 errno 0 degraded 0, recoveries 0, hang_kills 0, quarantined 0, 0 new GPU faults in dmesg.

@DjDeveloperr
DjDeveloperr merged commit e86174f into aurora-silicon:aurora-wip Dec 2, 2026
1 check passed
@DjDeveloperr

Copy link
Copy Markdown

Merged as e86174f60fbc onto aurora-wip b131e6a37551 (#1494). The merged tree is 41a7be7881ae, the same tree local CI ran on.

Local CI on pc, since hosted CI is off. It used the Fedora 44 image from .github/ci/Containerfile.mesa with clang 22.1.8. The commands were bash .github/ci/mesa-arm64.sh, the same script with CFLAGS/CXXFLAGS="-march=x86-64-v3 -funsigned-char", and meson test --suite asahi --suite vulkan --suite poly.

Tree mesa-arm64.sh x86-64-v3 variant asahi+vulkan+poly
base b131e6a3 (tree c39d3417) 389 ok / 0 fail / 1 skip 389 / 0 / 1 447 / 0 / 1
base + #1497 (tree 41a7be78) 388 / 0 / 1, 1 timeout 389 / 0 / 1 447 / 0 / 1
  • Timeout: borealis-sync-runner hit its 30 s limit as the last test, with pc at load 63. It passed in the other two runs of the same tree, and 10 of 10 times on its own (5 per build, 3 to 11 s). It hit the same timeout once on the hk,agx: version aligned robust compute loops on G16G and refine preamble weights #1494-only tree under the same load. This PR doesn't touch sync code.
  • New tests: SharedIndexReassocDisjointOrAndNestedImadshl and SharedIndexReassocImadshlNonzeroShiftValueEquiv pass in both builds.
  • Skip: hk-g17p-cross-queue (exit 77), the same as on the base.

Hardware: the J773g merge-tree row ran this head on 3db5ab65. Since then the tip gained #1521, #1420, #1438, #1528, #1422 and #1494. None of them touch the shared-memory index path this PR changes, and #1494 only rewrites global loads.

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 added a commit that referenced this pull request Dec 2, 2026
Forward-merge aurora-wip at e86174f (through #1497).

test-shared-byte-offsets.cpp: both sides appended new tests at the
end of the file (AffineStridedCrossBlockCluster/AffineStridedGuards
here, the SharedIndexReassoc tests from #1497). Keep both, this
branch's tests first.

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
…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:compiler Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants