Repository navigation
borealis: preserve shared masked byte-to-float shifts in bitfield_slices - #1629
Conversation
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>
|
Merge readiness at
Areas: 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>
|
Updated head to Verified at
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
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 asahi411 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 bootc538c8f8; 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. Base9b13a912lib085fbefc…vs head = mergeacd1d51clib45f1b1ea….
|
Hardware row receipt for the merge gate. It ran on J516s (M3 Pro, G15S, AGX3B), at head The run used stock |
What: Skip the
ushr(iand(x, mask), k) -> ubitfield_extractrewrite inagx_nir_opt_bitfield_slices()whenkis a byte offset (8,16, or24), theiandhas multiple uses, and the shift feeds a byte-to-float conversion recognized by AGX3 lowering. Preserves AGX3normfltbyte-pair fusion on quantized matmul weight/scale unpacks (-80 icand-164 Bonq4_Kmul_mat_vec), restoring+7.38 t/s(+2.89%) onQwen3.8-2B Q4_K_Mtg128on 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) at6e392b0ced69vs09902a1427c0:meson test --suite asahi255/255 OK (both new tests intest-shared-byte-offsets.cppverified failing on09902a1427c0and passing on6e392b0ced69),llama.cpptest-backend-ops -b Vulkan0 -o MUL_MAT2883/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=1tg128ABBA+7.38 t/s(+2.89%,254.98 -> 262.36 t/s, 4/4 non-overlapping) withpp51210961.2 -> 10939.7 t/s(-0.20%),Llama-2-7B Q4_0 fa=1tg128parity /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_Kmul_mat_vec,q4_Kmul_mm,q4_0mul_mm) mask a packed 32-bit word once (qm = q & 0x0f0f0f0forscales & 0x3f3f3f3f) and unpack all four byte lanes to float (u2f32): bytes 0..2 via mask/u2u8, and byte 3 viaushr(qm, 24).In
src/asahi/compiler/agx_nir_lower_shared_bitsize.c,opt_bitfield_slice_instr()(from #1495) rewroteushr(qm, 24)intoubitfield_extract(q, 24, 4)(or width6). Becauseqmhas other uses for bytes 0..2, the sharediandis not eliminated; meanwhile, bypassingqmchanges byte 3's source word fromqmtoqand narrows the extract width below 8. That preventsu2f32byte-unpack lowering (agx_compile.c) from extracting byte 3 fromqm(it only matchesubitfield_extractwith width 8) and preventsagx_opt_agx3_normflt_byte_pairs()from pairing bytes 2 and 3 ofqminto a zero-ICnormfltpair, leaving an IC-pipebfeilper word (+20 × 4 = +80 ic,+164 Binq4_Kmul_mat_vec;+16 icinq4_Kandq4_0mul_mm).Skipping the
ushr(iand(x, mask), k) -> ubitfield_extractrewrite only whenkis a byte offset (8,16, or24),src0has multiple uses, and the shift feeds a byte-to-float conversion (u2f32(ushr(y, 24)),u2f32(iand(ushr(y, k), 0xff)), oru2f32/i2f*(u2u8/i2i8(...))) restores allnormfltbyte 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
origin/aurora-wip):09902a1427c06e392b0ced697.3.0-1-54.20261204-ARCH(aurora-silicon/linux@main35ddf947b124), 0 failed units, 0 GPU faultsQwen3.8-2B Q4_K_M(-p 512 -n 128 -fa 1 -r 5) ABBApp512(t/s)tg128(t/s)09902a1427c010976.55 ± 21.83255.05 ± 0.396e392b0ced6910947.05 ± 15.73262.06 ± 0.496e392b0ced6910932.33 ± 11.40262.66 ± 0.2109902a1427c010945.78 ± 36.92254.91 ± 0.25-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 note09902a1427c0)6e392b0ced69)pp5123561.97 t/s3497.13 t/s-1.82%tg128122.53 t/s122.31 t/s-0.18%pp512 @ d20483002.46 t/s2952.25 t/s-1.67%tg128 @ d204898.47 t/s97.80 t/s-0.68%Investigation of the
Llama-2-7B Q4_0pp512-1.82%delta:pp512shader changes (q4_0mul_mm), and its stats improve strictly:836 -> 832 alu,460 -> 444 ic,11198 -> 11118 code size(-80 B), same167 gprs/576 threads.09902a1427c0and6e392b0ced69, shifted by-80 B(2723 -> 2643).5cdc12d19f13(before borealis: preserve hoisted shared-memory offsets across post-LICM folding #1626, where the identical-4 alu, -16 icunpack change shifted the same 8,347-byte body from2857 -> 2797), the same patch gained+21.2 t/sonLlama-2-7B pp512(3565.86 -> 3587.07 t/s,+0.59%, andpp512 @ d20483001.29 -> 3018.58 t/s).6e392b0ced69insertingNno-op identity instructions immediately before thescoped_barrier_agx3ahead 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/speriodic swing with identical IR and unpack code).Shader-DB diff (
09902a1427c0->6e392b0ced69)Qwen3.8-2B Q4_K_M:Llama-2-7B Q4_0:Unit tests & Conformance (
6e392b0ced69)meson test --suite asahi:255/255 OK, 0 Fail(both new testsSharedByteOffsets.BitfieldSlicesPreservesSharedByteToFloatandSharedByteOffsets.BitfieldSlicesPreservesNormfltBytePairverified failing on09902a1427c0and passing on6e392b0ced69)test-backend-ops test -b Vulkan0 -o MUL_MAT:2883/2883 passeddEQP-VK.api.smoke.*:8/8 passed, 0 faileddEQP-VK.compute.pipeline.basic.*:80/81 passed, 0 failed, 1 not supported