Skip to content

borealis: preserve shared masked byte-to-float shifts in bitfield_slices - #1629

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
satchlj/agx-bitfield-slices-preserve-byte-to-float
Dec 4, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
satchlj/agx-bitfield-slices-preserve-byte-to-float

Conversation

@satchlj

@satchlj satchlj commented Dec 4, 2026

Copy link
Copy Markdown
Member

What: Skip the ushr(iand(x, mask), k) -> ubitfield_extract rewrite in agx_nir_opt_bitfield_slices() when k is a byte offset (8, 16, or 24), the iand has multiple uses, and the shift feeds a byte-to-float conversion recognized by AGX3 lowering. Preserves AGX3 normflt byte-pair fusion on quantized matmul weight/scale unpacks (-80 ic and -164 B on q4_K mul_mat_vec), restoring +7.38 t/s (+2.89%) on Qwen3.8-2B Q4_K_M tg128 on J775c while keeping single-use masks and integer consumers folding as before.

Tested: J775c (M5 Max, G17C B1, 7.3.0-1-54.20261204-ARCH) at 6e392b0ced69 vs 09902a1427c0: meson test --suite asahi 255/255 OK (both new tests in test-shared-byte-offsets.cpp verified failing on 09902a1427c0 and passing on 6e392b0ced69), 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 tg128 ABBA +7.38 t/s (+2.89%, 254.98 -> 262.36 t/s, 4/4 non-overlapping) with pp512 10961.2 -> 10939.7 t/s (-0.20%), Llama-2-7B Q4_0 fa=1 tg128 parity / pp512 -1.82% (3562.0 -> 3497.1 t/s, traced to instruction-cache alignment of the unchanged 8,347-byte coopmat body — details below), 0 GPU faults, 0 failed units.

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

Not yet: M3/M4 (G15/G16/G17P) hardware run; change is confined to the multi-use byte-to-float guard in opt_bitfield_slice_instr() and verified by 255/255 asahi host tests.

Details

Quantized matmul compute shaders (q4_K mul_mat_vec, q4_K mul_mm, q4_0 mul_mm) mask a packed 32-bit word once (qm = q & 0x0f0f0f0f or scales & 0x3f3f3f3f) and unpack all four byte lanes to float (u2f32): bytes 0..2 via mask/u2u8, and byte 3 via ushr(qm, 24).

In src/asahi/compiler/agx_nir_lower_shared_bitsize.c, opt_bitfield_slice_instr() (from #1495) rewrote ushr(qm, 24) into ubitfield_extract(q, 24, 4) (or width 6). Because qm has other uses for bytes 0..2, the shared iand is not eliminated; meanwhile, bypassing qm changes byte 3's source word from qm to q and narrows the extract width below 8. That prevents u2f32 byte-unpack lowering (agx_compile.c) from extracting byte 3 from qm (it only matches ubitfield_extract with width 8) and prevents agx_opt_agx3_normflt_byte_pairs() from pairing bytes 2 and 3 of qm into a zero-IC normflt pair, leaving an IC-pipe bfeil per word (+20 × 4 = +80 ic, +164 B in q4_K mul_mat_vec; +16 ic in q4_K and q4_0 mul_mm).

Skipping the ushr(iand(x, mask), k) -> ubitfield_extract rewrite only when k is a byte offset (8, 16, or 24), src0 has multiple uses, and the shift feeds a byte-to-float conversion (u2f32(ushr(y, 24)), u2f32(iand(ushr(y, k), 0xff)), or u2f32/i2f*(u2u8/i2i8(...))) restores all normflt byte pairs without affecting single-use masks or multi-use integer consumers (such as thread-ID bit unpacks in matmul preheaders).

Evidence

Base vs PR head

  • Base (origin/aurora-wip): 09902a1427c0
  • PR head: 6e392b0ced69
  • 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 09902a1427c0 10976.55 ± 21.83 255.05 ± 0.39
B1 6e392b0ced69 10947.05 ± 15.73 262.06 ± 0.49
B2 6e392b0ced69 10932.33 ± 11.40 262.66 ± 0.21
A2 09902a1427c0 10945.78 ± 36.92 254.91 ± 0.25
Mean B vs A -21.48 (-0.20%) +7.38 (+2.89%)

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

Metric Mean A (09902a1427c0) Mean B (6e392b0ced69) Delta
pp512 3561.97 t/s 3497.13 t/s -1.82%
tg128 122.53 t/s 122.31 t/s -0.18%
pp512 @ d2048 3002.46 t/s 2952.25 t/s -1.67%
tg128 @ d2048 98.47 t/s 97.80 t/s -0.68%

Investigation of the Llama-2-7B Q4_0 pp512 -1.82% delta:

  • Only one pp512 shader changes (q4_0 mul_mm), and its stats improve strictly: 836 -> 832 alu, 460 -> 444 ic, 11198 -> 11118 code size (-80 B), same 167 gprs / 576 threads.
  • The entire 8,347-byte coopmat body + epilog following the unpack block is bit-for-bit identical between 09902a1427c0 and 6e392b0ced69, shifted by -80 B (2723 -> 2643).
  • On the immediately preceding base 5cdc12d19f13 (before borealis: preserve hoisted shared-memory offsets across post-LICM folding #1626, where the identical -4 alu, -16 ic unpack change shifted the same 8,347-byte body from 2857 -> 2797), the same patch gained +21.2 t/s on Llama-2-7B pp512 (3565.86 -> 3587.07 t/s, +0.59%, and pp512 @ d2048 3001.29 -> 3018.58 t/s).
  • Diagnostic padding sweep on 6e392b0ced69 inserting N no-op identity instructions immediately before the scoped_barrier_agx3 ahead of the unchanged coopmat body confirmed pure fetch/I-cache alignment sensitivity (N=0: 3494.0, N=3: 3545.3, N=6: 3527.3, N=12: 3445.6, N=16: 3436.2 t/s — a ~100 t/s periodic swing with identical IR and unpack code).

Shader-DB diff (09902a1427c0 -> 6e392b0ced69)

Qwen3.8-2B Q4_K_M:

-CS shader: 889 instrs, 767 alu, 767 fscib, 336 ic, 7734 code size, 99 gprs, 138 uniforms, 0 scratch, 1024 threads, 5 loops, 0:0 spills:fills, 104 preamble inst, 937 native, 17 native ldimm, 1 carriers, 1 cf drains, 0 loop cf drains, 0 staging copies, 13 loop immediates, 1 loop uniform copies
+CS shader: 905 instrs, 763 alu, 763 fscib, 256 ic, 7570 code size, 111 gprs, 138 uniforms, 0 scratch, 896 threads, 5 loops, 0:0 spills:fills, 104 preamble inst, 953 native, 19 native ldimm, 1 carriers, 1 cf drains, 0 loop cf drains, 0 staging copies, 17 loop immediates, 21 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
+CS shader: 1142 instrs, 858 alu, 858 fscib, 484 ic, 11464 code size, 175 gprs, 146 uniforms, 0 scratch, 576 threads, 2 loops, 0:0 spills:fills, 54 preamble inst, 1217 native, 13 native ldimm, 2 carriers, 1 cf drains, 1 loop cf drains, 0 staging copies, 1 loop immediates, 2 loop uniform copies

Llama-2-7B Q4_0:

-CS shader: 1119 instrs, 836 alu, 836 fscib, 460 ic, 11198 code size, 167 gprs, 146 uniforms, 0 scratch, 576 threads, 2 loops, 0:0 spills:fills, 54 preamble inst, 1194 native, 14 native ldimm, 2 carriers, 1 cf drains, 1 loop cf drains, 0 staging copies, 2 loop immediates, 2 loop uniform copies
+CS shader: 1119 instrs, 832 alu, 832 fscib, 444 ic, 11118 code size, 167 gprs, 146 uniforms, 0 scratch, 576 threads, 2 loops, 0:0 spills:fills, 54 preamble inst, 1194 native, 14 native ldimm, 2 carriers, 1 cf drains, 1 loop cf drains, 0 staging copies, 2 loop immediates, 2 loop uniform copies

Unit tests & Conformance (6e392b0ced69)

  • meson test --suite asahi: 255/255 OK, 0 Fail (both new tests SharedByteOffsets.BitfieldSlicesPreservesSharedByteToFloat and SharedByteOffsets.BitfieldSlicesPreservesNormfltBytePair verified failing on 09902a1427c0 and passing on 6e392b0ced69)
  • 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

When a compute shader masks a packed 32-bit word once and extracts all
four byte lanes to float (such as quantized matmul weight/scale unpack:
qm = q & 0x0f0f0f0f, then bytes 0..2 via mask/u2u8 and byte 3 via
qm >> 24 -> u2f32), opt_bitfield_slice_instr() rewrote ushr(qm, 24)
into ubitfield_extract(q, 24, 4).

Because qm has other uses for the remaining byte lanes, the shared iand
stays alive, while bypassing qm changes byte 3's source word from qm to
q and narrows its extract width below 8. That prevents u2f32 byte-unpack
lowering from extracting the high half of qm and prevents
agx_opt_agx3_normflt_byte_pairs() from pairing byte 2 and byte 3 of qm
into a zero-IC normflt pair, leaving an IC-pipe bfeil per word.

Skip the ushr(iand(x, mask), k) -> ubitfield_extract rewrite when k is a
byte offset (8, 16, or 24), the iand has multiple uses, and the shift
feeds a byte-to-float conversion pattern recognized by AGX3 lowering.
Single-use masks and integer consumers still fold as before.

Add unit tests covering the NIR guard cases and an end-to-end AGX3C
normflt byte-pair compile check.

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj
satchlj requested review from a team and DjDeveloperr as code owners December 4, 2026 07:33
@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 b6d90ae5: 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 no no line with the head SHA b6d90ae5, a Mac and a result in the PR body or a comment
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.

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj

satchlj commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Updated head to b6d90ae5dca7 (test-only 1-line fix: call agx_preprocess_nir_m5_chip(b.shader, AGX_CHIP_G17C) before agx_compile_shader_nir() in SharedByteOffsets.BitfieldSlicesPreservesNormfltBytePair so nir->info.io_lowered is set under asserts-on builds).

Verified at b6d90ae5dca7 with -Db_ndebug=false on J775c (M5 Max, G17C B1, 7.3.0-1-54.20261204-ARCH):

  • agx_tests: 2020 passed, 1 skipped, 0 failed (SharedByteOffsets.* 50/50)
  • Both new tests verified failing cleanly (without aborting) against 09902a1427c0's agx_nir_lower_shared_bitsize.c (op == ubitfield_extract, ic == 4)
  • src/asahi/compiler/agx_nir_lower_shared_bitsize.c is unchanged from 6e392b0ced69, so the J775c hardware ABBA and conformance rows above apply unchanged.

@github-actions github-actions Bot added the needs-hw-row No hardware row at the PR head (bot) label Dec 4, 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.

Approved at head b6d90ae5dca7. Merge gate run by DjDeveloperr (frontier child c1617).

What it does. Before this PR, agx_nir_opt_bitfield_slices() turned ushr(iand(q, 0x0f0f0f0f), 24) into ubitfield_extract(q, 24, 4), even when the masked word qm was still needed for bytes 0 to 2. That gave byte 3 a different source word and a width under 8, so neither the u2f32 byte unpack nor agx_opt_agx3_normflt_byte_pairs() could use it. The guard skips that one rewrite only when the shift is 8, 16 or 24, the iand has other users, and the shift feeds a byte-to-float conversion.

Correctness. The guard can only skip a rewrite and leave valid NIR, so it cannot miscompile on any chip. The pass runs under if (is_agx3) in agx_compile.c, for every stage. Per agx_chip.h, AGX3B is G15G/S/C/D, G16P/G/S/C, G17P and G17A, and AGX3C is G17G/S/C/D. G18G also runs it, because it compiles with the AGX3C key plus emit_agx3d.
On AGX3C every byte takes the direct unpack. On AGX3B only bytes 0 and 2 do (!high_byte || u2f32_byte_agx3c), so a guarded odd byte costs one shift instead of one bfeil, the same count. G18G gets neither the byte path nor normflt, so the count is the same there too.

Can a narrow extract still appear another way? I found none. Rule 1b takes its width from the shader's own mask and keeps the source word, so a guarded iand(ushr(qm, 16), 0xff) becomes ubfx(qm, 16, 8), which the unpack accepts. Slice merging and disjoint-ior fusion only rewrite iadd/ior/imadshl roots. agx_nir_opt_pack_byte_lut_select extracts at width 8. No nir_opt_algebraic runs after this pass.

Cost where the old code was already right. Single-use masks and integer consumers still fold (your cases 2 and 3). With 0x0f0f0f0f-style masks only k=24 was ever rewritten, so the main cost is that qm stays live until byte 3. That is the 99 to 111 GPR (1024 to 896 threads) Qwen shader in your stats, and the J775c ABBA still nets +2.89 % tg128.

Llama-2 pp512 -1.82 %. I read it as code layout, not a cost of this change. The one shader that changes (q4_0 mul_mm) gets strictly smaller (-4 ALU, -16 IC, -80 B, same GPRs and threads), and the 8,347-byte body after it is byte-identical, only moved. With identical IR, padding alone moves pp512 across 3,436 to 3,545 t/s, a 3.1 % band that contains the -65 t/s. On the previous base the same change gained +0.59 %. One caveat: the best padded point is still 0.5 % under base, and padding adds instructions of its own, so the sweep cannot show the base layout fully recovered.

Test fix. On 6e392b0c the new end-to-end test aborted on the io_lowered assert in builds with asserts on, so hosted build-test failed. b6d90ae5 fixes it; I had made the same one-line change locally (same blob, 697ebe96). Both new tests pass here and fail on aurora-wip's pass (ic 4, want 0).

Tested on a local merge into aurora-wip 9b13a91203e3 (acd1d51ca870, tree ae83bd920f18):

  • pc (asserts on): meson test --suite asahi 411 OK / 0 fail / 1 skip; agx_tests 2,020 pass / 1 skip.
  • x86 drm-shim with each Mac's exact feature word, base vs head: G16G wide 15,299 cases, and G15S/G17P/G17S compute slice 4,313 each, plus missing-case reruns. 0 changed shaders, refused 0/0, compile-failed 0/0, 0 status changes.
  • GTA V foz on all four chips: 0 changed pipelines. Refusals are identical, 0 failed, 0 crashed. The one unmatched shader hash also appears in a base-vs-base control.
  • J516s (G15S, AGX3B) wide CTS row: PASS. Stock 7.3.0-f3822, fresh unslept boot c538c8f8; 55,702 cases per arm (28,777 pass, 26,925 not supported). 0 differences, 0 fails, 0 crashes, refused 0 on base and 0 on head, 0 GPU fault lines, every process mapped the right library. Base 9b13a912 lib 085fbefc… vs head = merge acd1d51c lib 45f1b1ea….

@DjDeveloperr
DjDeveloperr merged commit c3fd4e7 into aurora-wip Dec 4, 2026
8 checks passed
@DjDeveloperr
DjDeveloperr deleted the satchlj/agx-bitfield-slices-preserve-byte-to-float branch December 4, 2026 08:47
@DjDeveloperr

Copy link
Copy Markdown

Hardware row receipt for the merge gate. It ran on J516s (M3 Pro, G15S, AGX3B), at head b6d90ae5dca7, as a wide CTS row comparing base and head. The result is PASS.

--- RECEIPT mesa#1629 ---
pr head b6d90ae5dca7f5d38dbe48740da43c94fffc6792
arm A base aurora-wip 9b13a91203e3dd1a6e31fc685b9086e7922b361f tree c7eb4f3aeedca7eb930ba2730ec12d9bb682fc8c lib 085fbefc6fdb7f866b61033c19716df798955d265eec98ce9ddce2a41dc5a776
arm B head merge acd1d51ca870f91d2c38eb57d89fe2918f30a8f6 tree ae83bd920f18c904be56918d2e7174c487dc25a4 lib 45f1b1eac26e216b3c2c8f1c9cff1ba13e42a898325e6bfa15adafb599a3f9cc
mac Apple MacBook Pro (16-inch, M3 Pro, Nov 2023) [apple,j516s apple,t6030 apple,arm-platform] kernel 7.3.0-f3822 boot_id c538c8f8-a60a-4a3d-a1c1-6ffdfab7c419
base: cases 55702 pass 28777 notsupported 26925 warn 0 fail 0 crash 0 refused 0 (all chunks 0)
head: cases 55702 pass 28777 notsupported 26925 warn 0 fail 0 crash 0 refused 0 (all chunks 0)
new fails/refusals on head: 0; cases with a different result: 0
gpu fault/reset/timeout lines in the kernel log during the run: 0
--- END RECEIPT ---
VERDICT PASS mesa#1629 b6d90ae5dca7 on Apple MacBook Pro (16-inch, M3 Pro, Nov 2023): 55702 cases/arm, 0 differ, 0 new fails, refused base 0 head 0, GPU fault lines 0

The run used stock 7.3.0-f3822 on a fresh boot that had not slept, with private ICDs and the Mac's own deqp-vk. It took 332 s of wall time over 4 processes.
Merged as c3fd4e7df6c4 with --admin --match-head-commit b6d90ae5…. Its tree ae83bd920f18 is the tree that was tested.

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

2 participants