Skip to content

asahi/lib,borealis: add and optimize subgroup sample-equivalence fast path for G17P MSAA local-read - #892

Merged
DjDeveloperr merged 5 commits into
aurora-wipfrom
djdev/asahi-lib-g17p-msaa-equiv-pack
Nov 24, 2026
Merged

DjDeveloperr merged 5 commits into
aurora-wipfrom
djdev/asahi-lib-g17p-msaa-equiv-pack

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

Adds and optimizes the subgroup sample-equivalence fast path for monolithic MSAA local-read shaders on G17P/G16G:

  • Probes input attachment samples across the subgroup and shades once per pixel when all samples in every subgroup lane match.
  • Widens equivalence tracking to 16 dwords without cross-branch v0 reuse and fixes the 4-MRT unit test narrowing initializer.
  • Compares raw tilebuffer lanes directly instead of decoding and re-encoding each sample channel.
  • Packs 16-bit probes into 32-bit comparisons, shares SR39 coverage across probes, and folds the coverage mismatch into the subgroup ballot.

Verification

  • git diff --check: clean across all 5 commits.
  • Live Apple M4 (G16G B0, 26B123) silicon:
    • gpubench: vk-full render.deferred tile m4 d=2 +10.4%; vk-full 202/202 PASS (1.094× vs Metal 4; Wave-2 1.001×); full suite 440/440 PASS (0 faults, 0 bad CRCs).
    • Vulkan local-read + MSAA CTS: dEQP-VK.renderpasses.dynamic_rendering.*.local_read.* + MSAA 148/148 PASS (4/4 feedback_loop_msaa).

@DjDeveloperr
DjDeveloperr force-pushed the perf/borealis-local-read-tilebuffer branch from 986f71b to 9ffd7a2 Compare November 24, 2026 08:10
@DjDeveloperr
DjDeveloperr force-pushed the djdev/asahi-lib-g17p-msaa-equiv-pack branch from c6c7ffd to 1c5f1c9 Compare November 24, 2026 08:13
@DjDeveloperr
DjDeveloperr force-pushed the perf/borealis-local-read-tilebuffer branch from 9ffd7a2 to 4708ad9 Compare November 24, 2026 10:09
@DjDeveloperr
DjDeveloperr force-pushed the djdev/asahi-lib-g17p-msaa-equiv-pack branch from 1c5f1c9 to 3e214d5 Compare November 24, 2026 10:21
@DjDeveloperr
DjDeveloperr force-pushed the perf/borealis-local-read-tilebuffer branch from 4708ad9 to 60974fa Compare November 24, 2026 12:04
@DjDeveloperr
DjDeveloperr force-pushed the djdev/asahi-lib-g17p-msaa-equiv-pack branch from 3e214d5 to 38e672c Compare November 24, 2026 12:15
…hic MSAA local-read

In hk_g17p_local_read_inline_epilog_part(), monolithic MSAA local-read
shaders previously unrolled the full shading body once per sample even when
every sample of an interior pixel had full coverage and identical tilebuffer
inputs. Add agx_nir_unroll_g17p_samples_equiv() to probe eligible top-level
local-read tile loads (up to 8 dwords), subgroup-ballot full coverage plus
bitwise sample equivalence, and run the body once with a whole-pixel tile
store on the fast path while keeping only sample 0 live into the branches.
Add a unit test compiling for both G16G and G17P with zero spills.

Signed-off-by: Diljit Singh <hey@djdev.me>
…cross-branch v0 reuse

Fix a latent bug in struct g17p_equiv_probe where nir_intrinsic_target()
was omitted when matching and emitting load_local_pixel_agx probes.

Lift G17P_EQUIV_MAX_DWORDS from 8 to 16 while keeping cross-branch v0
reuse capped at G17P_EQUIV_REUSE_DWORDS (8). When a shader probes more
than 8 dwords (such as 4-MRT deferred MSAA local-read lighting passes),
drop probe values after the subgroup ballot and reload sample 0 in the
fast path (writing all samples via store_samples = 0xFF), narrowing
32-bit R8_UNORM/R8_SNORM probe loads to f16 for the equality check while
keeping exact f32 reloads in the shading body. Run nir_opt_dce in the
inline epilog algebraic loop and add a 4-MRT 4x MSAA deferred unit test.

Signed-off-by: Diljit Singh <hey@djdev.me>
clang rejects passing the table's unsigned base to the int base index of
nir_load_local_pixel_agx() in C++ (-Wc++11-narrowing), so
libasahi_tests did not build and none of its tests ran, including the
MSAA local-read sample-equivalence tests in this file. Declare the
table's base as int.

Signed-off-by: Diljit Singh <hey@djdev.me>
…e probe

The sample-equivalence fast path of a monolithic MSAA local-read program
first probes every sample of each input attachment and only shades once
when all samples of every lane in the subgroup match. The probe read each
input in the body's own format, so an 8-bit normalized input was decoded
(normflt) for all four samples before the comparison, then narrowed from
f32 to f16 when the probe was wide, and every sample and channel got its
own compare and select.

An AGX3 tile load names a lane width, not a format, and equal bytes decode
to equal values in every format. Probe the raw lanes instead: four or two
8-bit lanes as one 32-bit or 16-bit word at the same byte offset, 16-bit
lanes as 16-bit integers, packed 32-bit formats as their word. OR the
differing bits per bit size and test them once. A probe that is the body's
own read (16- and 32-bit integer lanes) is still reused for sample 0 when
the footprint is small; a raw probe of a decoded format is not, and the
body decodes its own sample-0 reload. Bytes that differ but decode alike
(snorm -128 and -127, NaN payloads) take the per-sample path, which is
always correct.

gpubench render.deferred mode=tile msaa=4 lighting pass (RGBA8, RGBA16F,
RGBA8 and R32F inputs, per-sample shading), compiled for G16G and G17P
under the asahi drm-shim:

                     probe    fast path   whole program
  before              172        301           802
  after                77        204           703

The fast path is what interior pixels run; the 1x lighting program is 125
instructions.

Update the two equivalence unit tests for the raw probes.

Signed-off-by: Diljit Singh <hey@djdev.me>
…n G17P MSAA equiv

In agx_nir_unroll_g17p_samples_equiv(), monolithic MSAA local-read shaders
(such as 4x MSAA deferred lighting with an RGBA16F normal buffer and three
RGBA8 G-buffers) spent ~25 extra instructions on the whole-pixel fast path:

1. Four- and two-channel 16-bit targets at 4-byte-aligned tile offsets were
   probed as four 16-bit lanes, doubling probe XOR/OR traffic and forcing a
   separate 16-bit difference accumulator. Probe them as 2x32-bit or
   1x32-bit raw R32_UINT words (witnessed on AGX3B/AGX3C at AGX3_TILE_W32
   with chan_mask=3/1 at the same tilebuffer partition offset), fold their
   differences directly into diff32, and unpack sample 0's 32-bit words via
   nir_unpack_32_2x16_split_x/y (zero-cost agx_subdivide_to) so R16_UINT
   and R16_FLOAT bodies still reuse sample 0 without reloading the tile.

2. Coverage was read repeatedly from SR39: once for the equivalence check
   and again in every basic block by agx_nir_lower_g17p_tile_samples().
   Because eligible shaders reject discard, demote, zs_emit, and all other
   sample-mask mutations, hardware coverage is invariant after force_early.
   Read native W16 SR39 once via nir_load_active_samples_agx(), derive
   cov_low and the per-sample masks once in block 0, and pass them to the
   probes, fast path, and slow path.

3. Fold the coverage mismatch (cov_low ^ full_mask) and probe diff bits
   into a single 32-bit mismatch word and ballot once with
   nir_ballot(mismatch != 0), removing the 3x icmpsel + invert chain while
   emitting a single witnessed 32-bit AGX3B icmp_ballot.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the djdev/asahi-lib-g17p-msaa-equiv-pack branch from 38e672c to 6aeaa4e Compare November 24, 2026 14:36
@DjDeveloperr
DjDeveloperr changed the base branch from perf/borealis-local-read-tilebuffer to aurora-wip November 24, 2026 14:36
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 24, 2026 14:36
@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased onto aurora-wip d40ba9f9ea21 (after #703 and #944) and retargeted to aurora-wip. New head 6aeaa4e17372, 5 commits.

  • The only conflict was in src/asahi/lib/tests/test-g17p-msaa-epilog.cpp, where borealis: enable read-only early-Z rejection ahead of discard on M5 #791 and this PR each appended a test at the end; both tests are kept. Commits 2-5 are unchanged.
  • Fork CI on the exact head: asahi run 45414797388 (build + meson test --suite asahi, 276/276) and aurora-agent-policy run 45414797568 pass.

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict at 6aeaa4e17372

APPROVED

Blockers

None

Checked

  • Audited agx_nir_unroll_g17p_samples_equiv() in src/asahi/lib/agx_nir_lower_msaa.c, agx_nir_unroll_g17p_samples_equiv wiring + nir_opt_dce cleanup in src/asahi/vulkan/hk_cmd_draw.c, and the new tests in src/asahi/lib/tests/test-msaa.cpp.
  • Verified enum ail_isa_format values alias enum pipe_format in src/asahi/layout/layout.h, so g17p_equiv_set_raw_form() safely probes raw undecoded tile words (R32_UINT/R16_UINT), shares one load_active_samples_agx (SR39) read, folds coverage mismatch (cov_low ^ full_mask) and inter-sample XOR diffs into one subgroup-uniform nir_ballot(mismatch != 0) == 0 guard, unpacks 2x/4x 16-bit probes via nir_unpack_32_2x16_split_{x,y}, and falls back cleanly to agx_nir_unroll_g17p_samples_layout() when ineligible.
  • Built and ran the non-layout asahi test suite (261/261 PASS, including all 3 new G17PMSAAEpilog/G17PMsaaEpilog tests) plus Borealis Vulkan compute smoke on Apple M5 Max (G17C B1), Linux 7.3 with MESA_SHADER_CACHE_DISABLE=1 and 0 GPU faults in dmesg.
  • Untested scope: Live G17P/G16G hardware execution of the new agx_nir_unroll_g17p_samples_equiv fast path (verified via G17P/G16G compiler and NIR lowering unit tests; live hardware check ran on Apple M5 Max (G17C B1)), and asahi_layout_tests (pre-existing GCC C++20 designator-order error in src/asahi/layout/tests/test-layout.cpp:374).
Evidence
  • meson test --suite asahi (non-layout): 261/261 PASS (0 FAIL)
  • libasahi_tests --gtest_filter="*Equiv*": 3/3 PASS
    • G17PMSAAEpilog.MonolithicLocalReadSampleEquivFastPathCompiles
    • G17PMsaaEpilog.MonolithicLocalReadDeferred4MRTMsaa4EquivCompiles
    • G17PMsaaEpilog.MonolithicLocalReadDeferred4MRTMsaa4Half16EquivCompiles
  • Live Borealis Vulkan CTS on Apple M5 Max (G17C B1), Linux 7.3 (MESA_SHADER_CACHE_DISABLE=1, 0 GPU faults in dmesg):
    • dEQP-VK.compute.pipeline.basic.*: 80/81 PASS, 0 FAIL, 1 NotSupported

@DjDeveloperr

Copy link
Copy Markdown
Author

6aeaa4e J773g M4 mini (Apple M4, G16G B0) PASS (413 Pass / 165 NotSupported / 0 Fail across Vulkan dynamic-rendering local-read & MSAA sample-shading CTS; 6/6 gpubench render.deferred CRCs exact; 184/184 asahi unit tests; 0 GPU faults/recoveries/dmesg).

Exact-head G16G verification details
  • Commit: 6aeaa4e17372d4bf3529aba332f72c7dca797302 (Mesa 26.3.0-devel (git-6aeaa4e173), Borealis Vulkan 1.4.363 + Zink GL 4.6)
  • Host/Target: Apple M4 Mac mini (J773g, Apple M4 (G16G B0)), Linux 7.3
  • Unit suite: meson test --suite asahi — 184/184 PASS (0 Fail, including libasahi_tests / test-g17p-msaa-epilog.cpp)
  • Vulkan CTS (jobs=1, MESA_SHADER_CACHE_DISABLE=1, ICD probe verified git-6aeaa4e173):
    • dynamic_rendering local_read slice (172 cases): 148 Pass / 24 NotSupported / 0 Fail
    • dEQP-VK.renderpasses.dynamic_rendering.primary_cmd_buff.basic.* (240 cases): 240 Pass / 0 Fail
    • Multisample sample_id + sample_mask + sample_shading slice (166 cases): 25 Pass / 141 NotSupported / 0 Fail
  • gpubench Vulkan render.deferred (1080p, d=1 and d=2, 6/6 status=ok, 6/6 CRCs exact):
    • mode=tile msaa=1: crc=2cfe9a54 exact (759.88 us d=1, 458.72 us d=2)
    • mode=tile msaa=4: crc=5e4130ce exact (5555.56 us d=1, 5102.04 us d=2)
    • mode=dram msaa=4: crc=7c09507f exact (3731.34 us d=1, 3378.38 us d=2)
  • Kernel health: 0 GPU faults, 0 recoveries, 0 dmesg errors, clean idle post-run.

@DjDeveloperr
DjDeveloperr merged commit 9f24931 into aurora-wip Nov 24, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merged into aurora-wip as 9f24931002d0 (head 6aeaa4e17372, verified tree 172054686c58). Verified: exact-head peer approval (261/261 asahi unit suite, live M5 Max Borealis Vulkan CTS), exact-head M4 (G16G B0) Vulkan dynamic-rendering/MSAA CTS (413/0) + render.deferred CRC parity, and fork CI asahi (45414797388) + aurora-agent-policy (45414797568).

@RyanTheTide
RyanTheTide deleted the djdev/asahi-lib-g17p-msaa-equiv-pack branch November 27, 2026 13:20
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.

2 participants