Skip to content

asahi/compiler: wire AGX3 matmad zero_accumulator/c_zero and enable M5 FTZ32 and graphics native subgroups - #1187

Merged
RyanTheTide merged 2 commits into
aurora-silicon:aurora-wipfrom
oreater:pr/agx3-matrix-zero-accum-m5-subgroups
Nov 28, 2026
Merged

RyanTheTide merged 2 commits into
aurora-silicon:aurora-wipfrom
oreater:pr/agx3-matrix-zero-accum-m5-subgroups

Conversation

@oreater

@oreater oreater commented Nov 27, 2026 •

Copy link
Copy Markdown

What: Wire existing AGX3 hardware zero_accumulator (fGeMM16x16x16) and c_zero (mma8) bits via AGX3_MATRIX_ZERO_ACCUM when a cooperative-matrix multiply-add starts from a +0.0 accumulator (eliminating dead zero-init moves and freeing 16 GPR halves on the first K-step), and enable M5 native shuffle_xor and simd_reduce float min/max across graphics stages and DenormFlushToZero 32 (keeping DenormPreserve 32 lowered).

Tested: J716c Apple M5 Max at 8928b33: Clang 22.1.8 debugoptimized meson test --suite asahi --suite poly 334/334 pass (0 fail, including updated M5FloatMinMaxReduce GTests); live 3-round ABBA vs 41fe336dffa shows FTZ32 subgroupMin/Max loop 392 -> 20 instrs (-94.9%), 3920 -> 268 B code, 35 -> 9 GPRs, wall-clock 20.650 -> 8.415 ms (-59.2%, 2.45x, bit-exact hash 33017f33e4cd2325 including 1e-40f subnormals), and 16x16x16 coopMatMulAdd with zero accumulator 73 -> 65 instrs, 63 -> 47 GPRs (-16 halves), wall-clock 0.174 -> 0.146 ms (-16.2%, 1.19x, exact 1024.0); Vulkan CTS subgroups.{arithmetic,shuffle}.{framebuffer,graphics,compute} 207/419 pass across two slices (0 fail, 0 status diffs).

Risk: two-way door · M5 (G17G / G17S / G17C; G18G excluded via !g18g) — AGX3_MATRIX_ZERO_ACCUM matches exact 0u bit patterns (+0.0, never -0.0); DenormPreserve 32 remains on the shuffle-tree fallback.

Not yet: Upstream CI run at 63a59152822, and a second M5 chip row (J714s / G17S) covering the graphics subgroup CTS slices and cooperative-matrix CTS.

Details & benchmark data

Scope

  • src/asahi/compiler/agx3_matrix_core.h, agx3_pack_binary.c, agx_compile.c, agx_register_allocate.c, agx_validate.c: detect +0.0 constant accumulators on cmat_muladd_agx, set AGX3_MATRIX_ZERO_ACCUM with agx_null() src[2], skip tied-accumulator copy/RA reads, and encode zero_accumulator / c_zero.
  • src/asahi/compiler/agx_nir_lower_subgroups.c, test/test-m5-float-minmax-reduce.cpp: enable native_shuffle_xor across graphics/compute and m5_float_minmax across graphics/compute and FTZ32 (!preserve32).

Live M5 Max 3-round ABBA (MESA_SHADER_CACHE_DISABLE=1, base 41fe336dffa SHA-256 0ea0ee4efd3b vs head SHA-256 ca8e22d277fc)

Workload Base median (6 runs) Head median (6 runs) Delta ShaderDB / Output
FTZ32 subgroupMin/Max loop (65536 threads x 4096 iters) 20.650 ms 8.415 ms -59.2% (2.45x) 392->20 instrs, 35->9 GPRs, bit-exact 33017f33e4cd2325
coopMatMulAdd 16x16x16 zero accumulator (256 WGs x 64 iters, 64x) 0.174 ms 0.146 ms -16.2% (1.19x) 73->65 instrs, 63->47 GPRs, exact 1024.0

Vulkan CTS (dEQP-VK.subgroups.{arithmetic,shuffle}.{framebuffer,graphics,compute}.*, MESA_SHADER_CACHE_DISABLE=1)

  • Slice 1 (272 cases): Base 120/272 Pass, 0 Fail; Head 120/272 Pass, 0 Fail, 0 diffs

  • Slice 2 (147 targeted framebuffer/graphics/compute min/max/shuffle_xor): Base 87/147 Pass, 0 Fail; Head 87/147 Pass, 0 Fail, 0 diffs

  • Review follow-up at 63a59152822:

    • Narrowed the RA early-kill exclusion in agx_ra_assign_local() to AGX3_MATRIX_ZERO_ACCUM only so AGX3B (M4) and non-zero-accum matmad are untouched.
    • Renamed FlushToZeroKeepsTheFlushedTree -> FlushToZeroFlushesInputsAndKeepsNativeReduce.
    • Added end-to-end binary-decoding compiler tests in test-agx3-cmat-mixed-mma8.c: gemm16 +0.0 (15 instrs, zero_accumulator=1) vs -0.0 (23 instrs, zero_accumulator=0), mma8 +0.0 (12 instrs, c_zero=1) vs -0.0 (14 instrs, c_zero=0), and mma8 +0.0 untouched on AGX3B (c_zero=0).
    • Ran full dEQP-VK.compute.pipeline.cooperative_matrix.khr_r.*matrixmuladd* (f16/f32) + op_constant_null.* A/B on J716c: 154/1064 PASS, 910 NotSupported, 0 FAIL, 0 diffs.
    • Live J716c signed-zero probe (A=-1.0, B=+0.0, C=+0.0 and A=-0.0, B=+0.0, C=+0.0): f32matmad8x8 (c_zero=1) returns 0x00000000 (+0.0, identical to explicit C=+0.0); fGeMM16x16x16 (zero_accumulator=1) returns 0x80000000 (-0.0) vs 0x00000000 (+0.0) for explicit C=+0.0 (permitted under Vulkan / VK_KHR_cooperative_matrix default float semantics).

@oreater
oreater requested review from a team and DjDeveloperr as code owners November 27, 2026 01:04

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

Verdict at 8928b337d5fc: no code blockers found. Not approving yet: CI hasn't run, the cooperative-matrix CTS is missing, and the hardware rows below are still owed.

Blockers: none.

Checked:

  • Zero-accumulator detection (agx_emit_intrinsic()).
    • Only on AGX3C, not AGX3D (G18G), and only for 32-bit accumulators, so the AGX3_MATRIX_ACCUM_F16 matmad8 path is unaffected.
    • Every component must be a bit-exact 0u, either directly constant or through a vec of constants. -0.0 (0x80000000), undefs and loop-carried zeros keep the normal path.
    • Bit 9 doesn't overlap the input-type nibbles (bits 0–7) or AGX3_MATRIX_ACCUM_F16 (bit 8).
  • Encoding (agx3_pack_binary()).
    • Both packers refuse a ZERO_ACCUM instruction whose src[2] isn't null, check only src[0..1], and set zero_accumulator (gemm16) or c_zero with c = 0 (mma8).
    • The gemm16 dest==acc tie is required only without the bit, and its existing refusals (compute only, AGX3C, eight 32-bit dest channels and so on) still apply.
    • agx_read_registers() reports 0 registers for a null accumulator, and agx_lower_agx3_matrix_sources() skips the tied copy only for ZERO_ACCUM gemm16.
  • Register allocation.
    • ZERO_ACCUM gemm16 no longer takes the tied-accumulator special case, so its destination is allocated normally.
    • The new generic early-kill exclusion keeps the destination off A/B, which is always legal.
  • FTZ32 native min/max reduce.
    • agx_nir_flush_subgroup_minmax() runs first in agx_nir_lower_subgroups_internal() and rewrites every 32-bit fmin/fmax reduce and scan source to x + (-0.0) under nir_fp_no_fast_math.
    • The native reduce therefore only sees flushed inputs, and min/max returns one of them, so no denormal comes out.
    • DenormPreserve 32 still takes the shuffle tree, and G18G keeps both options off (!g18g).
  • Graphics stages. native_shuffle_xor and m5_float_minmax now also apply outside compute. optimize_ballot_count still excludes fragment shaders.
    • The PR's subgroup CTS (arithmetic and shuffle, framebuffer/graphics/compute) has 0 failures and 0 status changes against base on J716c.
  • Build and tests. With the CI job's options (-Dgallium-drivers=asahi -Dvulkan-drivers=asahi -Dplatforms=wayland,x11 -Dbuild-tests=true -Dtools=drm-shim, ccache clang), meson test --suite asahi --suite poly passes 337/337. That includes the three M5FloatMinMaxReduce GTests and agx3_g17p_producers.
    • No new warnings in the changed files. The ones shown, agx3_pack_binary.c:10435 and lines 36–40/159 of the GTest, are on lines this PR doesn't touch.
  • Merge and text. The PR merges into current aurora-wip (8ac326f31d4f) without conflicts, and no compiler commit has landed since its base. Signed-off-by, with no attribution lines.

Owed before merge:

  • The RA change reaches M4 too, so narrow it or test it there.
    • The early-kill exclusion in agx_ra_assign_local() (I->op != MATMAD16_AGX3 && I->op != MATMAD8_AGX3) applies to every matmad, not only ZERO_ACCUM ones. That includes AGX3B (M4) matmad8 whenever the matmad8 in-place special case doesn't fire (accumulator not killed, misaligned, or of a different width).
    • That conflicts with "M4/AGX3B and AGX2 are untouched".
    • Either restrict it to I->matrix_type & AGX3_MATRIX_ZERO_ACCUM (if the concern is the destination landing on A/B once it is no longer tied to C), or add a J773g row with the cooperative-matrix CTS and say so in the PR.
  • Cooperative-matrix CTS on M5. VK_KHR_cooperative_matrix is exposed (hk_physical_device.c), and this is the first use of the zero_accumulator/c_zero encodings from the compiler. Please run dEQP-VK.compute.pipeline.cooperative_matrix.* (both shapes, f16 and f32 inputs, and the zero and non-zero accumulator cases) against base. The one 16×16×16 sample doesn't cover them.
    • It would also show whether a hardware zero accumulator can return -0.0 where products + (+0.0) gives +0.0.
  • A compiler test for the new path. The encoder fields have tests, but nothing checks that a constant +0.0 accumulator emits ZERO_ACCUM with a null src[2], no tied move, and the bit in the encoding, or that -0.0 doesn't.
  • A second M5 chip. The subgroup change reaches graphics stages on every M5 part, but the rows are J716c (G17C) only. A J714s (G17S) row with the same subgroup CTS slices is needed.
  • CI at the exact head. GitHub reports no checks on this branch.
  • Nit: FlushToZeroKeepsTheFlushedTree now asserts that the native reduce is kept; a name like FlushToZeroFlushesInputsAndKeepsNativeReduce would match.
Evidence

Head 8928b337d5fcfdde02e4f67648d9954121fdaaf3, base aurora-wip 41fe336dffae. Reviewed in a scratch repository (alternates only). Linux aarch64 host, clang + ccache, Meson 1.12.1.

Check Result
meson test --suite asahi --suite poly Ok: 337, Fail: 0; M5FloatMinMaxReduce 3/3 OK
warnings in changed files none new
git merge-tree origin/aurora-wip (8ac326f31d4f) clean
gh pr checks no checks reported

Code read at this head: agx_emit_intrinsic() (cooperative-matrix multiply-add), agx_lower_agx3_matrix_sources(), agx3_pack_binary() (MATMAD16_AGX3/MATMAD8_AGX3), agx_ra_assign_local() (the matmad8 in-place case and the generic early-kill block), agx_read_registers(), agx_subgroup_options_for_target(), agx_nir_flush_subgroup_minmax() / flush_reduced_value(), and agx_nir_lower_subgroups_internal().

…5 FTZ32 and graphics native subgroups

Two AGX3C (M5) codegen improvements using existing hardware encodings:

1. Add AGX3_MATRIX_ZERO_ACCUM (1u << 9) for MATMAD16_AGX3 (fGeMM16x16x16
   zero_accumulator) and MATMAD8_AGX3 (mma8 c_zero) when the NIR
   cooperative-matrix multiply-add accumulator is a +0.0 constant vector.
   Pass agx_null() as src[2], skip tied-accumulator moves and accumulator
   register reads in lowering/RA/validation, and encode zero_accumulator /
   c_zero directly in agx3_pack_binary(). Also avoid in-place RA source
   aliasing on MATMAD16_AGX3 / MATMAD8_AGX3.
2. Enable native_shuffle_xor and m5_float_minmax across M5 graphics stages
   (VS/FS) and under DenormFlushToZero 32 (where
   agx_nir_flush_subgroup_minmax() flushes lane inputs before reduction;
   DenormPreserve 32 stays lowered).

Update M5FloatMinMaxReduce GTests for FTZ32, DenormPreserve 32, and
graphics stages.

Signed-off-by: oreater <145754695+oreater@users.noreply.github.com>
@oreater
oreater force-pushed the pr/agx3-matrix-zero-accum-m5-subgroups branch from 8928b33 to 63a5915 Compare November 27, 2026 02:44
@oreater

oreater commented Nov 27, 2026

Copy link
Copy Markdown
Author

Thanks for the thorough review! Updated at 63a59152822:

  1. Narrowed RA early-kill exclusion to AGX3_MATRIX_ZERO_ACCUM only: agx_ra_assign_local() now excludes only (I->op == AGX_OPCODE_MATMAD16_AGX3 || I->op == AGX_OPCODE_MATMAD8_AGX3) && (I->matrix_type & AGX3_MATRIX_ZERO_ACCUM), leaving M4/AGX3B and non-zero-accum matmad completely untouched.
  2. Renamed test: FlushToZeroKeepsTheFlushedTree -> FlushToZeroFlushesInputsAndKeepsNativeReduce.
  3. Added end-to-end compiler + binary-decode tests in test-agx3-cmat-mixed-mma8.c: compiles nir_cmat_muladd_agx and decodes the emitted binary with agx3_decode():
    • gemm16 +0.0 on AGX3C: 15 instrs (126 B), zero_accumulator=1 vs -0.0: 23 instrs (218 B), zero_accumulator=0.
    • mma8 +0.0 on AGX3C: 12 instrs (102 B), c_zero=1, c=0 vs -0.0: 14 instrs (122 B), c_zero=0.
    • mma8 +0.0 on AGX3B: 14 instrs (122 B), c_zero=0 (untouched).
    • Full local Clang debug CI (--suite asahi --suite poly): 334/334 PASS, 0 FAIL.
  4. Cooperative-matrix CTS A/B on J716c: ran all 1,064 dEQP-VK.compute.pipeline.cooperative_matrix.khr_r.*matrixmuladd* (f16/f32) + op_constant_null.* cases (which iterate both 8x8x8 and 16x16x16 shapes): 154/1064 PASS, 910 NotSupported, 0 FAIL, 0 status diffs vs base (including 4/4 PASS on op_constant_null.{null_a,null_b,null_c,null_r}).
  5. Signed-zero hardware behavior probed directly on J716c silicon ((-1.0) * (+0.0) + (+0.0) and (-0.0) * (+0.0) + (+0.0)):
    • f32matmad8x8 (c_zero=1): returns 0x00000000 (+0.0), bit-identical to explicit C = +0.0.
    • fGeMM16x16x16 (zero_accumulator=1): returns 0x80000000 (-0.0), whereas explicit C = +0.0 returns 0x00000000 (+0.0). This matches hardware skipping the + C add rather than adding +0.0; Vulkan / VK_KHR_cooperative_matrix does not require signed-zero preservation on OpCooperativeMatrixMulAddKHR, and if you prefer gating gemm16 zero_accumulator off under any strict mode or leaving it for mma8 only, happy to adjust!
  6. Updated **Not yet:** to list the owed upstream CI run at 63a59152822 and the J714s (G17S) row (I only have J716c locally).

This was referenced Nov 27, 2026

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

Verdict: FINISH, then merge at 63a591528222

Blockers:

  1. src/asahi/compiler/test/test-agx3-cmat-mixed-mma8.c:444: nir_const_value cv[8]; is not zeroed, and only .u32 is set before nir_build_imm(), so the upper bytes of each constant are stack garbage. With asserts on, nir_validate rejects the load_const (nir_validate.c:1277, "NIR validation failed after nir_opt_copy_prop", shader "gemm16 +0.0 on AGX3C") and agx3_cmat_mixed_mma8 aborts. Reproduced on this head merged alone into aurora-wip 90211df6816 (329/330, only this test fails) and in a batch build. Fix: nir_const_value cv[8] = {0}; (or memset). With that one-line change the test passes locally (1/1 OK).

Checked: Read the whole diff. The zero-accumulator detection only matches exact 0u bit patterns (so -0.0 keeps the accumulator), the tied-accumulator copy is skipped only when the flag is set, and the M5 subgroup changes keep DenormPreserve 32 on the shuffle tree; under FTZ32 the native reduce relies on agx_nir_flush_subgroup_minmax() flushing the inputs first. M5-only (!g18g, AGX3C). Also: the Tested line is at 8928b337d5f, not this head; please re-run at the fixed head.

Evidence

Local Mesa gate (stands in for CI; buildbox x86 docker aurora/hk6356-mesa-x86-tests:local, clang release with asserts, -Dbuild-tests=true -Dtools=asahi,drm-shim, meson test --suite asahi --suite poly):

  • base aurora-wip 90211df6816: 335/335 OK.
  • batch build: 90211df6816 with 22 open PR heads merged on top (#1133 #1137 #1155 #1136 #1140 #1157 #1158 #1160 #1164 #1165 #1167 #1168 #1170 #1171 #1172 #1175 #1182 #1183 #1185 #1187 #1190 #1191; each also merges cleanly alone, and into current aurora-wip d2e883ab2e3): 335/336 OK. The one failure, agx3_cmat_mixed_mma8, reproduces with #1187 alone on the base (329/330) and is caused by that PR's new test; nothing else in the batch fails.
  • #1187 alone on 90211df6816: meson test --suite asahi Ok 329 / Fail 1 (agx3_cmat_mixed_mma8, SIGABRT from nir_validate). Same build with cv[8] = {0}: agx3_cmat_mixed_mma8 OK.

…at test

nir_build_imm() validates all 64 bits of each nir_const_value union entry
when asserts are enabled, so leaving cv[8] uninitialized before writing
only cv[i].u32 triggered nir_validate failures depending on stack contents
(and deterministically under -ftrivial-auto-var-init=pattern).

Zero-initialize cv[8] = {0} before filling .u32.

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

satchlj commented Nov 27, 2026

Copy link
Copy Markdown
Member

Pushed the 1-line uninitialized-constant fix at 429037e5e783 (src/asahi/compiler/test/test-agx3-cmat-mixed-mma8.c:444: nir_const_value cv[8] = {0};, addressing @RyanTheTide's blocker).

Exact-head verification at 429037e5e783 on Mac Studio M5 Max (J775c, G17C B1, Borealis overlay ICD, MESA_SHADER_CACHE_DISABLE=1)

  • Repro before fix (63a591528222): Building with -Dbuildtype=release -Db_ndebug=false -Dc_args=-ftrivial-auto-var-init=pattern deterministically reproduces agx3_cmat_mixed_mma8_test SIGABRT (RC=134) in nir_validate.
  • Unit tests after fix (429037e5e783): Native GCC 16 release + asserts (-Db_ndebug=false -Dc_args=-ftrivial-auto-var-init=pattern -Dbuild-tests=true -Dtools=asahi,drm-shim), MALLOC_PERTURB_=42 meson test -C build --suite asahi --suite poly --print-errorlogs: 334/334 OK, 0 FAIL (including agx3_cmat_mixed_mma8 and M5FloatMinMaxReduce).
  • Live Vulkan CTS (GPU_FAULT_DELTA=0):
    • dEQP-VK.compute.pipeline.cooperative_matrix.khr_r.*matrixmuladd*: 302/3,292 PASS, 0 FAIL, 2,990 NotSupported.
    • dEQP-VK.compute.pipeline.cooperative_matrix.*op_constant_null*: 4/4 PASS, 0 FAIL.
    • dEQP-VK.subgroups.arithmetic.{framebuffer,graphics,compute}.*: 5,628/7,455 PASS, 0 FAIL (3,216 framebuffer + 804 graphics + 1,608 compute).
    • dEQP-VK.subgroups.shuffle.{framebuffer,graphics,compute}.*: 4,312/5,880 PASS, 0 FAIL (2,464 framebuffer + 616 graphics + 1,232 compute).
  • Total live CTS: 10,246 PASS / 0 FAIL / 0 GPU faults.
  • Clean merge check against current aurora-wip: git merge-tree --write-tree origin/aurora-wip HEAD clean (no conflicts).

Ready for re-review at 429037e5e783.

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: HOLD for applicable hosted checks, final hardware gates and the outside-contributor landing policy. The test initialization finding is resolved.

Checked: The sole new diff zero-initializes the NIR constant array before filling its 32-bit lanes, so upper bytes consumed by validation are defined. The author reply reports 334/334 native GCC assert tests at this head with stack pattern initialization enabled, and a J775c cooperative-matrix row with zero failures among supported cases. This App approval covers the revised head. Please have the owner of the earlier personal change request resolve that review before landing.

@RyanTheTide
RyanTheTide dismissed their stale review November 28, 2026 04:54

Resolved at 429037e: the one blocker (uninitialized nir_const_value in test-agx3-cmat-mixed-mma8.c:444) is fixed by the zero-initializer; the aurora-silicon-review App approval covers that head. Dismissing my earlier change request; merge still waits on the lab regression rows.

@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr, code-owner heads-up: this PR has an aurora-silicon-review App approval at 429037e5e783 from a different lane plus the author's exact-head hardware row. I plan to land it no earlier than 06:29Z, after a combined A/B regression row on two lab GPU families (M1 Pro G13S + M4 G16G). The App approval doesn't count toward the ruleset, so that merge uses --admin under the 6-point rule. Please post a review here if you want it held.

@RyanTheTide

Copy link
Copy Markdown

Merge-train landing at 429037e5e783.

  • Review: aurora-silicon-review App APPROVE at this head (different lane), DjDeveloperr no-blocker review at 8928b33; the only later change is the zero-initialised test constant (non-author).
  • Feature row: the author's exact-head hardware row on this PR.
  • Regression: combined A/B overlay of aurora-wip plus this PR and the other queued PRs, user space, default kernel command line. M1 Pro (G13S, fleet89): 0 status changes. M4 Pro (G16S, fleet89): 0 regressions. The only flips there are 5 synchronization multi_queue timeouts under 4-job load; all 5 pass 3/3 alone on both arms, and the base arm shows the same flake. Each Mac ran 26,595 dEQP-VK + 8,153 dEQP-GLES cases, with 0 GPU firmware crashes, Enforcing, 0 failed units. Evidence traces/gpu/20261128-mergetrain-mesa.
  • --admin under the 6-point rule: the App approval does not count toward the ruleset. Dj was pinged here more than 1 h ago, and no blocking review is open.

Revert: git revert -m 1 <merge commit> on aurora-wip. A regression gets reverted first and fixed after.

@RyanTheTide
RyanTheTide merged commit 17570e2 into aurora-silicon:aurora-wip Nov 28, 2026
satchlj added a commit that referenced this pull request Nov 28, 2026
Resolve conflicts with merged #1187 (AGX3_MATRIX_ZERO_ACCUM / FP16xFP16->FP16
coopmat) and #1278 (cooperativeMatrixWorkgroupScope):
- Move AGX3_MATRIX_SATURATE to (1u << 10) alongside AGX3_MATRIX_ZERO_ACCUM (1u << 9)
- Combine zero_accum and is_int8 operand checks in agx3_pack_binary.c and agx_validate.c
- Skip INT8/UINT8 in 8x8-dependent workgroup wg_shapes (32x16@128, 32x32@256) in
  hk_physical_device.c and guard needs_16x16_pieces()/agx_nir_lower_cmat_workgroup()
  against falling back to 8x8 tiles for INT8/UINT8 (iGeMM16x16x16 is 16x16 only)

Signed-off-by: Satya Benson <satya@benson.earth>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants