Repository navigation
asahi: fold horizontal sums of packed bytes - #1548
Conversation
Add agx_nir_fold_w8_byte_sum() for AGX3B and AGX3C (!emit_agx3d): when an iadd reduction tree in a block sums all 3 or 4 u2u32 lanes of one or more packed R8_UINT device loads (with >=2 loads or at least one vec4 load), keep each load as a single packed 32-bit word (via AGX_NIR_MEMORY_PACKED_COUNT_SHIFT) and accumulate up to 32 words in two 16-bit SWAR lanes (max 32 * 510 = 16320 <= 65535, zero cross-lane carry) before one final (s & 0xffff) + (s >> 16) reduction. Preserves exact mod-2^32 semantics while cutting per-byte unpack ALU and improving load batching. Signed-off-by: Diljit Singh <hey@djdev.me> (cherry picked from commit 179980b) (cherry picked from commit 8bf65d33f3f9be031e4bb442423e31ff5da29bc6) (cherry picked from commit 54cd94a)
|
Exact-head M5 Max (
Note: standalone synthetic W8 byte-sum microbenchmark was not run on this machine; verification covers compiler tests, Vulkan CTS, ray-tracing scene, and LLM no-regression on |
|
The exact #1548 head |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Changes needed: the new byte-sum fold crashes the compiler on valid shaders when the same sum also adds one component of a vector value.
agx_nir_fold_w8_byte_sum() in src/asahi/compiler/agx_compile.c walks the iadd tree with stack[sp++] = ...src[i].src.ssa and rebuilds the sum from extras[]. It keeps each leaf's SSA def but drops src.swizzle[0], so a leaf such as w.y comes back as the whole vector w and the rebuilt sum becomes a vector iadd.
Failing compute shader (src is u8vec4[], words is uvec4[]):
uvec4 a = uvec4(src[i]);
uvec4 w = words[i];
dst[i] = w.x + w.y + w.z + w.w + a.x + a.y + a.z + a.w;Expected: it compiles and stores w.x + w.y + w.z + w.w plus the four bytes. At 0de2e7b the NIR after the pass has 32x4 %30 = iadd %6, %6 through 32x4 %34 = iadd %33, %29.xxxx, and validation fails on the store_agx (src->ssa->num_components == num_components). Base c94b25c compiles it, and the merge with aurora-wip c3adfe7 fails the same way. The compiler also crashes on G15G, G16G and G17P for (v.y + a.x + a.y + a.z + a.w) ^ v.x with a uvec2 v (assert in agx_emit_alu), and on G17S with two u8vec3 loads. G14G and G18G are unchanged.
Fix: keep each leaf as a nir_scalar (def plus component), the way agx_nir_opt_u8x4_hsum() does, and re-add it with nir_channel(). Or skip the tree when any leaf has more than one component. A test case with a uvec4 addend would cover it.
Small: the new code went in between fold_w8_word_unpack()'s comment and that function.
Everything else checked out on pc (x86_64, drm-shim, assert-enabled build):
- Meson asahi, poly and vulkan suites: 454 pass, 0 fail, 1 skip at base, at head and on the merge with c3adfe7.
- agx_tests: 1,959 pass at base, 1,960 at head and 1,961 on the merge.
- GTA V shader-db (12,868 executables per chip): 0 changes and 0 crashes on G17P, G16G, G15G, G17S and G14G.
- The PR's own shader drops from 57 to 48 instructions on G16G and G17P. M3 (G15G) is AGX3B too, so its code changes as well (67 to 58), but it has no hardware row yet.
An iadd source can select a single component of a vector SSA definition. Track both the definition and component when collecting and rebuilding a byte-sum tree so unrelated vector lanes never enter the scalar result. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
|
Thanks, Dj. Fixed in The exact J713 and J615 drivers verified 524,288 outputs and as many padding words per process. J713 improved the original two-vector case by 2.45% paired. The new vector-addend case was −0.13% paired on J713. The first J615 vector-addend pass was noisy (+4.55% paired, +0.20% pooled). In a 12-block interleaved repeat with two identical baseline copies, J615 was +0.16% paired versus baseline and −0.21% versus its identical copy. Both saved boots were unchanged and the kernel journals were empty. I’ve requested re-review on the exact head. |
Fixed the vector-addend compiler crash in f723a28. The regression test and J615/J713 checks pass on the new head. Re-review requested.
|
Exact-head M5 Max (
|
|
The assert-enabled host Asahi, Poly and Vulkan Meson suites at exact head |
DjDeveloperr
left a comment
There was a problem hiding this comment.
The fix is right at f723a281. The vector-addend crash is gone, and I found no other addend shape that breaks the fold. This is a comment, not an approval: the first commit here is under my account, so a reviewer with no commits in this PR still needs to approve.
I checked the merge of this head into aurora-wip c3adfe79 (tree 711b8afc) on pc, using an x86-64 assert-enabled build with drm-shim:
- Earlier failures: the three failing shaders from my earlier review and the PR's own shader now compile and pass NIR validation on G13X, G14G, G15G, G16G, G17P, G17S and G18G.
- Other addend shapes: these also compile cleanly on every chip:
- built-in vectors and push-constant
uvec4lanes as addends - an inner sum that has a second use
- the same vector lane added twice
- two sums in one block, one with a
u8vec4load and one with twou8vec3loads - 32 strided
u8vec4loads, the per-lane limit: all 32 pack, and G16G goes from 389 to 232 instructions - 33 loads, which bail out as intended
- built-in vectors and push-constant
- Bit-exact results: I evaluated the final NIR of every shape on base and on the merge, with 1,000 random and edge inputs per chip, including all bytes 0xFF and all zero. The stored values were identical in all 72 shape and chip pairs. A deliberately broken mask does get caught.
- Local CI on the merge:
- Meson asahi, poly and vulkan suites: 454 pass, 0 fail, 1 skip (
hk-g17p-cross-queue, exit 77). Base gives the same result. agx_tests: 1,962 pass and 1 skip, against 1,960 on base. The newWave8SwarW8ByteSumWithVectorAddendstest passes.
- Meson asahi, poly and vulkan suites: 454 pass, 0 fail, 1 skip (
- GTA V shader-db, base against merge: 0 changed executables on G14G, G15G, G16G, G17P, G17S and G18G. I ran both the combined GTA V Fossilize DB and the Steam DB, up to 26,437 executables per chip.
- Hardware at this head: the J775c (G17C) row above, plus the J713 (G16G) and J615 (G15G) results in your comment.
Not blocking: on G17S, u8vec3 sums come out a few instructions longer than on base, because AGX3C's own W8 load already widens each byte to a word.
vec3leafgoes from 21 to 24 instructions, and the two-sum shape from 31 to 33.u8vec4sums are unchanged there, becauseagx_nir_opt_u8x4_hsum()takes them first.- Nothing in GTA V hits this. If a real M5 shader ever does, skipping 3-byte loads on AGX3C would avoid it.
Still needed before merge: an approval from a reviewer with no commits in this PR.
DjDeveloperr
left a comment
There was a problem hiding this comment.
Approving at f723a281, following our review in #1548 (review): the fix is correct on G13X through G18G, the final NIR matches base, local CI on the merge with aurora-wip is clean, and there is no shader-db change on G14G to G18G. We are running one J714s row at this exact head before merge.
|
PASS J714s (M5 Pro, T6050/G17S B1, stock 7.3.0-m5-pr67) mesa#1548 head f723a28 merged onto aurora-wip 725f2ab (merge 03f6d18630c7, tree c611688f9e4c): store-aarch64 byte-sum 4x ABBA/BAAB (64/64 procs verified 524288 outputs + 524288 sentinels, MESA_SHADER_CACHE_DISABLE=1) mode=2 10708->10729.5ns (+0.48%), mode=3 1x 11792->11792ns (-0.26%), mode=4 10834->10792ns (-0.57%), mode=3 256-batch 1545187.5->1545125ns (-0.00%); G17S AGX3C main shader size 416->416B (agx_nir_opt_u8x4_hsum already handles M5 AGX3C with 0 regression, 46 instrs/13 GPRs/0 spills); 0 GPU faults. |
What
Carry the horizontal W8 byte sum from #804 directly on aurora-wip, preserving Dj’s sign-off. This supersedes stacked #1542. Complete reductions keep the load as a packed word and accumulate in two 16-bit lanes. Partial-vector consumers keep the original path.
Tested
Exact head
0de2e7b1b9bapassed AArch64 Vulkan build, 1,960 compiler tests with one skip, 103 focused checks and 44 policy checks. Baseline ICDe20bc7252c2eis tree-identical to aurora-wipc94b25ce4e2f. Same saved7.3.0-aurora-fleet94boot on each Mac, with per-armVK_DRIVER_FILESandMESA_SHADER_CACHE_DISABLE=1. Every process verified 524,288 CPU results and 524,288 padding words.Neo ran four balanced ABBA/BAAB blocks per mode with
./store-aarch64 1 2,1 3and1 4, 20 GPU timestamps after four warmup dispatches. J713 ran eight balanced blocks of./store-aarch64 1 3, with 256 dispatches per sample after 24 warmup batches. Two J713 blocks varied slightly above zero (+0.15%, +0.32%). The main shader shrank from 586 to 488 bytes on both Macs. Saved boot, Enforcing, zero failed units and no new kernel entries on both.Risk
Two-way · Asahi packed W8 sums on AGX3B and AGX3C.
Shader and individual GPU samples
Neo
J713