Skip to content

borealis: preserve hoisted shared-memory offsets across post-LICM folding - #1626

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
satchlj/agx-shared-offsets-preserve-hoist
Dec 4, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
satchlj/agx-shared-offsets-preserve-hoist

Conversation

@satchlj

@satchlj satchlj commented Dec 4, 2026

Copy link
Copy Markdown
Member

What: Emit rewritten shared-memory offset expressions in agx_nir_opt_shared_byte_offsets() at the original u2u16 instruction rather than before the load_shared/store_shared intrinsic, so post-LICM runs after agx_nir_opt_bitfield_slices() (#1559) never sink loop-invariant shared-memory bases back into loops. Restores +401.8 t/s (+3.80%) on Qwen3.8-2B Q4_K_M pp512 on J775c while keeping all #1559 bitfield-slice fusions intact.

Tested: J775c (M5 Max, G17C B1, 7.3.0-1-54.20261204-ARCH) at 1407a4774d9c: meson test --suite asahi 255/255 OK (including 2 new regression tests in test-shared-byte-offsets.cpp), llama.cpp test-backend-ops -b Vulkan0 -o MUL_MAT 2883/2883 OK, dEQP-VK.api.smoke.* 8/8 OK, dEQP-VK.compute.pipeline.basic.* 80/81 OK (0 fail, 1 not supported), Qwen3.8-2B Q4_K_M fa=1 pp512 ABBA +401.8 t/s (+3.80%, 10563.4 -> 10965.1 t/s, 4/4 non-overlapping), Llama-2-7B Q4_0 fa=1 parity, 0 GPU faults, 0 failed units.

Risk: two-way door · AGX3 Macs (M3/M4/M5)

Not yet: M3/M4 (G15/G16) hardware run; change is AGX3 shared-offset insertion placement only and verified by 255/255 asahi host tests.

Details

On AGX3, agx_compile_shader_nir_internal() runs agx_nir_opt_shared_byte_offsets() twice: once before agx_nir_opt_licm(), and again after agx_nir_opt_licm() and agx_nir_opt_bitfield_slices().

When #1559 (agx_nir_opt_bitfield_slices()) fuses a disjoint ior into imadshl_agx, term_upper_bound() can now prove a loop-invariant shared-memory index expression bounded that was previously opaque. On the second pass of agx_nir_opt_shared_byte_offsets(), fold_elem_offsets_impl() therefore rewrites the already-hoisted offset.

Previously, both fold_elem_offsets_impl() (all three rewrite paths) and fold_shifted_byte_offset() set b.cursor = nir_before_instr(&intr->instr). When intr lives inside a loop and its u2u16 offset had already been hoisted to the preheader by agx_nir_opt_licm(), this re-emitted the 32-bit address arithmetic and u2u16 inside the loop body after LICM had already finished. On Qwen3.8-2B Q4_K_M cooperative-matrix matmul kernels, six hoisted shared-memory bases were rematerialized inside the inner K loop (+11 ALU, +4 loop immediates, +2 loop uniform copies, +8 GPRs, dropping occupancy from 576 to 512 threads on the hottest kernel, and +18 ALU on the second q4_K kernel).

Emitting at nir_before_instr(u2u16_instr) preserves SSA dominance by construction: every term gathered by chasing ALU sources from u2u16_instr strictly dominates u2u16_instr, and u2u16_instr dominates intr. Hoisted bases stay in their preheader block (and CSE merges them there) while keeping all #1559 fusions active.

Evidence

Base vs PR head

  • Base (origin/aurora-wip): 291dd57b86d8
  • PR head: 1407a4774d9c
  • Machine: J775c (Mac Studio M5 Max, G17C B1, 64 GiB)
  • Kernel: 7.3.0-1-54.20261204-ARCH (aurora-silicon/linux@main 35ddf947b124), 0 failed units, 0 GPU faults

Qwen3.8-2B Q4_K_M (-p 512 -n 128 -fa 1 -r 5) ABBA

Arm Build pp512 (t/s) tg128 (t/s)
A1 291dd57b86d8 10562.02 ± 45.49 252.92 ± 0.51
B1 1407a4774d9c 10970.70 ± 41.61 253.55 ± 0.18
B2 1407a4774d9c 10959.58 ± 12.18 253.09 ± 0.18
A2 291dd57b86d8 10564.73 ± 14.84 253.75 ± 0.14
Mean B vs A +401.76 (+3.80%) -0.02 (-0.01%)

Llama-2-7B Q4_0 (-p 512 -n 128 -fa 1 -r 3 -d 0,2048) ABBA

Metric Mean A (291dd57b86d8) Mean B (1407a4774d9c) Delta
pp512 3568.26 t/s 3566.29 t/s -0.06%
tg128 122.13 t/s 122.40 t/s +0.22%
pp512 @ d2048 3001.89 t/s 2998.95 t/s -0.10%
tg128 @ d2048 97.99 t/s 97.54 t/s -0.46%

Shader-DB diff (Qwen3.8-2B, 291dd57b86d8 -> 1407a4774d9c)

-CS shader: 1237 instrs, 935 alu, 935 fscib, 516 ic, 12486 code size, 191 gprs, 148 uniforms, 0 scratch, 512 threads, 2 loops, 0:0 spills:fills, 54 preamble inst, 1312 native, 17 native ldimm, 2 carriers, 1 cf drains, 1 loop cf drains, 0 staging copies, 4 loop immediates, 2 loop uniform copies
+CS shader: 1226 instrs, 924 alu, 924 fscib, 516 ic, 12364 code size, 183 gprs, 148 uniforms, 0 scratch, 576 threads, 2 loops, 0:0 spills:fills, 54 preamble inst, 1301 native, 17 native ldimm, 2 carriers, 1 cf drains, 1 loop cf drains, 0 staging copies, 0 loop immediates, 0 loop uniform copies
-CS shader: 1151 instrs, 871 alu, 871 fscib, 500 ic, 11652 code size, 175 gprs, 146 uniforms, 0 scratch, 576 threads, 2 loops, 0:0 spills:fills, 54 preamble inst, 1226 native, 13 native ldimm, 2 carriers, 1 cf drains, 1 loop cf drains, 0 staging copies, 1 loop immediates, 2 loop uniform copies
+CS shader: 1133 instrs, 853 alu, 853 fscib, 500 ic, 11460 code size, 175 gprs, 146 uniforms, 0 scratch, 576 threads, 2 loops, 0:0 spills:fills, 54 preamble inst, 1208 native, 13 native ldimm, 2 carriers, 1 cf drains, 1 loop cf drains, 0 staging copies, 1 loop immediates, 2 loop uniform copies

Unit tests & Conformance (1407a4774d9c)

  • meson test --suite asahi: 255/255 OK, 0 Fail (both new tests SharedByteOffsets.PreservesHoistedOffsetAcrossBitfieldSlices and SharedByteOffsets.ShiftedByteOffsetPreservesHoistedDef verified failing on 291dd57b86d8 and passing on 1407a4774d9c)
  • test-backend-ops test -b Vulkan0 -o MUL_MAT: 2883/2883 passed
  • dEQP-VK.api.smoke.*: 8/8 passed, 0 failed
  • dEQP-VK.compute.pipeline.basic.*: 80/81 passed, 0 failed, 1 not supported

agx_nir_opt_shared_byte_offsets() runs a second time on AGX3 after
agx_nir_opt_licm() and agx_nir_opt_bitfield_slices(). When
agx_nir_opt_bitfield_slices() fuses a disjoint ior into imadshl_agx,
term_upper_bound() can prove a loop-invariant shared-memory address
bounded that was previously opaque, causing fold_elem_offsets_impl()
to rewrite the already-hoisted offset.

Both fold_elem_offsets_impl() and fold_shifted_byte_offset() inserted
the rewritten address arithmetic and u2u16 at nir_before_instr(&intr->instr),
sinking loop-invariant shared-memory bases back into the loop body after
LICM had already finished. On Q4_K cooperative-matrix matmul shaders,
this rematerialized six hoisted shared-memory bases inside the inner K
loop (+11 ALU, +4 loop immediates, +2 loop uniform copies, +8 GPRs,
dropping occupancy from 576 to 512 threads).

Emit the rewritten expression and u2u16 at nir_before_instr(u2u16_instr)
instead. Every operand gathered by chasing ALU sources from u2u16_instr
dominates u2u16_instr by SSA construction, and u2u16_instr dominates
intr, so hoisted bases remain in their pre-loop block while keeping all
bitfield-slice fusions intact.

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj
satchlj requested review from a team and DjDeveloperr as code owners December 4, 2026 06:25
@github-actions github-actions Bot added area:compiler Area from CODEOWNERS (bot) blast:m3 Macs affected, from Merge risk (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 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 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Merge readiness at 1407a477: not ready yet

Check Detail
CI at head ok 6 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok in the PR body at 1407a477
Conflicts ok none
PR text ok Risk: two-way · AGX3 Macs (M3/M4/M5)

Areas: 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.

Approving at 1407a4774d9c. aurora-wip is at 5cdc12d19f13; the four commits it gained since your base 291dd57b don't touch either file.

The fix is right. Moving the cursor from the load/store to the original u2u16 is safe for SSA:

  • Every term the rewrite uses comes from gather_affine_sum() (or, in fold_shifted_byte_offset(), the non-constant iadd operand). Both only walk ALU sources down from that u2u16, so each term is defined before the u2u16 on every path. That includes the case where the u2u16 sits in a loop preheader and the access is inside the loop.
  • The u2u16 is found with nir_scalar_resolved() on the access's own offset, which follows only mov and vec, never a phi. So even when the offset reaches the access through a mov, the u2u16 comes before the access, and the new u2u16 placed in front of it does too.
  • Each candidate emits its own expression, and nothing is shared between candidates before nir_opt_cse. The only callers are the two agx_compile.c calls (is_agx3 && shared_size > 0) and the unit tests, and both enter through the access's own offset.

One thing to keep in mind, not a blocker: this second shared-offset pass runs after LICM's register budget check. When one hoisted u2u16 feeds several accesses that end up with different anchors, each of those values now stays live across the loop. On G16G this shows up in 4 fp8 cooperative-matrix multiply-add shaders: 102 to 104 half-registers at the same code size, still in the 1,024-thread tier.

What I ran, on a local merge of this head into aurora-wip (6281d5bc43cc, tree 5ee64ddcc59d):

  • x86 build with asserts on: meson test --suite asahi 410 OK / 0 fail / 1 skip; agx_tests 2,018 pass / 1 skip, SharedByteOffsets.* 48/48. Both new tests fail against aurora-wip's pass file, so they test the fix.
  • drm-shim compile sweep, base vs merge, with each Mac's exact feature word, on every chip the pass runs for: G16G on the 15,641-case wide slice, and G15S, G17P and G17S on its 4,558-case compute part. Refused and failed compiles were 0 on both arms on every chip, with no status changes. Code changed in 4 cases on G16G (the shaders above) and 3 on G17S (same size, same registers), and in none on G15S or G17P. The cases whose chunks dropped out were rerun in smaller chunks; the ones still left over are missing on both arms.
  • GTA V Fossilize replay on all four chips: the refused-shader messages are identical between arms, with 0 failed compiles and 0 crashes. Changed pipelines: G16G 1 (4 fewer registers), G15S 1 (4 fewer instructions), G17P 0, G17S 12 (+28 instructions out of 9.96M).
  • J516s (M3 Pro, G15S), CTS row base vs merge on one fresh boot (7.3.0-j516s-aes4103): PASS. 54,764 cases per arm, including the compute slice above with cooperative-matrix and workgroup-memory cases first. 0 differences, 0 fail or crash, refused 0/0, 0 GPU fault lines.

@DjDeveloperr
DjDeveloperr merged commit 09902a1 into aurora-wip Dec 4, 2026
8 checks passed
@DjDeveloperr
DjDeveloperr deleted the satchlj/agx-shared-offsets-preserve-hoist branch December 4, 2026 07:15
@DjDeveloperr

Copy link
Copy Markdown

Merged as 09902a1427c0 (aurora-wip 5cdc12d19f13 + this head 1407a4774d9c). Its tree 5ee64ddcc59d is the same merge tree the checks in the review above ran on.

J516s row receipt: kernel 7.3.0-j516s-aes4103, boot 3dd51365, base lib f49b0f8b… (aurora-wip), head lib 487af9c0… (merge). 54,764 cases per arm: 27,621 pass, 27,143 not supported, 0 fail, 0 crash, refused 0/0, 0 differences, 0 GPU fault lines. VERDICT PASS.

Still not run: a G16G (M4) or G17 (M5) hardware row. The shim sweep shows the change reaches those chips only in a handful of cooperative-matrix shaders.

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) blast:m3 Macs affected, from Merge risk (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants