Repository navigation
borealis: preserve hoisted shared-memory offsets across post-LICM folding - #1626
Conversation
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>
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
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, infold_shifted_byte_offset(), the non-constantiaddoperand). Both only walk ALU sources down from thatu2u16, so each term is defined before theu2u16on every path. That includes the case where theu2u16sits in a loop preheader and the access is inside the loop. - The
u2u16is found withnir_scalar_resolved()on the access's own offset, which follows onlymovandvec, never a phi. So even when the offset reaches the access through amov, theu2u16comes before the access, and the newu2u16placed 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 twoagx_compile.ccalls (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 asahi410 OK / 0 fail / 1 skip;agx_tests2,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.
|
Merged as J516s row receipt: kernel 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. |
What: Emit rewritten shared-memory offset expressions in
agx_nir_opt_shared_byte_offsets()at the originalu2u16instruction rather than before theload_shared/store_sharedintrinsic, so post-LICM runs afteragx_nir_opt_bitfield_slices()(#1559) never sink loop-invariant shared-memory bases back into loops. Restores+401.8 t/s(+3.80%) onQwen3.8-2B Q4_K_Mpp512on J775c while keeping all #1559 bitfield-slice fusions intact.Tested: J775c (M5 Max, G17C B1,
7.3.0-1-54.20261204-ARCH) at1407a4774d9c:meson test --suite asahi255/255 OK (including 2 new regression tests intest-shared-byte-offsets.cpp),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=1pp512ABBA+401.8 t/s(+3.80%,10563.4 -> 10965.1 t/s, 4/4 non-overlapping),Llama-2-7B Q4_0 fa=1parity, 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()runsagx_nir_opt_shared_byte_offsets()twice: once beforeagx_nir_opt_licm(), and again afteragx_nir_opt_licm()andagx_nir_opt_bitfield_slices().When #1559 (
agx_nir_opt_bitfield_slices()) fuses a disjointiorintoimadshl_agx,term_upper_bound()can now prove a loop-invariant shared-memory index expression bounded that was previously opaque. On the second pass ofagx_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) andfold_shifted_byte_offset()setb.cursor = nir_before_instr(&intr->instr). Whenintrlives inside a loop and itsu2u16offset had already been hoisted to the preheader byagx_nir_opt_licm(), this re-emitted the 32-bit address arithmetic andu2u16inside the loop body after LICM had already finished. OnQwen3.8-2B Q4_K_Mcooperative-matrix matmul kernels, six hoisted shared-memory bases were rematerialized inside the inner K loop (+11ALU,+4loop immediates,+2loop uniform copies,+8GPRs, dropping occupancy from 576 to 512 threads on the hottest kernel, and+18ALU on the secondq4_Kkernel).Emitting at
nir_before_instr(u2u16_instr)preserves SSA dominance by construction: every term gathered by chasing ALU sources fromu2u16_instrstrictly dominatesu2u16_instr, andu2u16_instrdominatesintr. Hoisted bases stay in their preheader block (and CSE merges them there) while keeping all #1559 fusions active.Evidence
Base vs PR head
origin/aurora-wip):291dd57b86d81407a4774d9c7.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)291dd57b86d810562.02 ± 45.49252.92 ± 0.511407a4774d9c10970.70 ± 41.61253.55 ± 0.181407a4774d9c10959.58 ± 12.18253.09 ± 0.18291dd57b86d810564.73 ± 14.84253.75 ± 0.14+401.76 (+3.80%)-0.02 (-0.01%)Llama-2-7B Q4_0(-p 512 -n 128 -fa 1 -r 3 -d 0,2048) ABBA291dd57b86d8)1407a4774d9c)pp5123568.26 t/s3566.29 t/s-0.06%tg128122.13 t/s122.40 t/s+0.22%pp512 @ d20483001.89 t/s2998.95 t/s-0.10%tg128 @ d204897.99 t/s97.54 t/s-0.46%Shader-DB diff (
Qwen3.8-2B,291dd57b86d8->1407a4774d9c)Unit tests & Conformance (
1407a4774d9c)meson test --suite asahi:255/255 OK, 0 Fail(both new testsSharedByteOffsets.PreservesHoistedOffsetAcrossBitfieldSlicesandSharedByteOffsets.ShiftedByteOffsetPreservesHoistedDefverified failing on291dd57b86d8and passing on1407a4774d9c)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