Skip to content

agx: rematerialize uniform moves when spilling on AGX3C - #458

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
j716c/t2-uniform-spill
Nov 25, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
j716c/t2-uniform-spill

Conversation

@erikwb

@erikwb erikwb commented Nov 19, 2026 •

Copy link
Copy Markdown

What: Enable 32-bit uniform word rematerialization in agx_can_rematerialize_uniform() (src/asahi/compiler/agx_spill.c) on AGX_ISA_ENCODING_AGX3C while excluding G18G via !agx_is_g18g(&ctx->key->dev), so spilled uniform copies on G17G, G17S, and G17C are recreated at use instead of spilling to scratch.
Tested: M5 Max (J716c, G17C) at b5a86e1c551d: VK-GL-CTS cooperative-matrix mustpass subsets match aurora-wip (fa9a6766c01) with 0 diffs (200 Pass / 1800 NotSupported and 150 Pass / 1350 NotSupported) and 0 new kernel/GPU errors, 0 journal errors, 0 new failed units; reviewer-verified GLES3 CTS (dEQP-GLES3.functional.shaders.loops.for_constant_iterations.*: 64/64 passed, 0 GPU faults); host meson test --suite asahi 166/166 passed (agx_tests 1218 passed, 1 skipped).
Risk: two-way door · M5 family (G17G, G17S, G17C; AGX3C excluding G18G); AGX2, AGX3B, G18G, and precompiled libagx kernels are unchanged.
Not yet: Live hardware runs on G17G (M5) and G17S (M5 Pro) (no G17G/G17S hardware available locally; G18G is intentionally excluded by !agx_is_g18g()).

Details
  • agx_can_rematerialize_uniform() (src/asahi/compiler/agx_spill.c) previously gated on ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B. It now enables AGX_ISA_ENCODING_AGX3B || (AGX_ISA_ENCODING_AGX3C && !agx_is_g18g(&ctx->key->dev)).
  • On AGX3C, the uniform-to-GPR word copy encodes any even 32-bit uniform word (< 512) into any 32-bit GPR (0..254 halves), reusing the existing invariants (non-preamble, !uniforms_written, scalar 32-bit normal non-memory destination, no abs/neg modifiers). AGX2 reads uniforms with its own bitop and keeps its existing spill behaviour.
  • G18G (compiled as AGX3C, re-encoded as agx3d when emit_agx3d is set) stays excluded with a code comment: rematerialized copies are placed under register pressure where destinations are often at or above half 128 (AGX3_COPY_REG16_DEST_HALVES), and agx3d copies into those destinations have not been verified to execute correctly on G18G hardware (agx3_copy_encode_uniform_to_reg32() rejects AGX_ISA_ENCODING_AGX3D destinations >= 128).
  • Unit tests in src/asahi/compiler/test/test-uniform-remat.cpp cover all six representative targets (G13G, G17P, G17G, G17S, G17C, G18G), verify zero spill memory traffic on G17G/G17S/G17C, and pin G18G spill retention.
Evidence
  • Host compiler and Borealis unit tests on aurora-wip (fa9a6766c01) plus b5a86e1c551d (-Doptimization=2 -Ddebug=false -Db_ndebug=false -Dbuild-tests=true -Dtools=drm-shim -Dvulkan-drivers=asahi):

    • meson test --suite asahi: 166/166 passed (and in reviewer satchlj's non-layout asahi suite run at b5a86e1c551d: 206/206 passed).
    • agx_tests: 1218 passed, 1 skipped (NativeFMA.ExactDyadicOracle, which skips without AGX_FMA_RTE_CASES on aurora-wip as well).
    • agx_tests --gtest_filter='*UniformRemat*': 5/5 passed (ImmutableWordCopiesOnly, SpillerRecreatesUniformsWithoutMemoryTraffic, SpillerRecreatesUniformsWithoutMemoryTrafficOnAGX3C, G18GSpillsUniformCopies, UniformWritesKeepCapturedValuesInMemory).
    • Precompiled libagx kernels (src/asahi/lib/libagx_shaders.c): byte-for-byte identical between fa9a6766c01 and b5a86e1c551d (sha256: cf7112a67527e000f35c24ce133e349741c391fd43808898d47b219399dab84d).
  • M5 Max (J716c, G17C) VK-GL-CTS mustpass A/B at b5a86e1c551d vs base fa9a6766c01 (Linux 7.3.0, release builds; libvulkan_asahi.so sha256 d6f38d655757a6676365d0f031722a90a505edbd8524cf4413ba1200842abcd2 vs 6b871cdb18f7251e659caac9496a4590b13d867257bb9eecf40caa4edad634f0):

Case set (mustpass) Cases Base (fa9a6766c01) Head (b5a86e1c551d) Diffs
compute.pipeline.cooperative_matrix.*physical_buffer* (2000 evenly spaced) 2000 200 Pass, 1800 NotSupported, 0 Fail 200 Pass, 1800 NotSupported, 0 Fail 0
compute.pipeline.cooperative_matrix.* (1500 evenly spaced) 1500 150 Pass, 1350 NotSupported, 0 Fail 150 Pass, 1350 NotSupported, 0 Fail 0
  • System health over the M5 Max (G17C) CTS run: 0 new kernel/GPU dmesg errors, 0 new journal errors, 0 new failed systemd units (aurora-peripherals.service was already failed at boot before the run; SELinux/AVC is N/A on this Arch ARM host with CONFIG_SECURITY_SELINUX unset).

  • Reviewer (satchlj) live GLES3 CTS on M5 Max (G17C B1) at b5a86e1c551d (Linux 7.3, MESA_LOADER_DRIVER_OVERRIDE=asahi MESA_SHADER_CACHE_DISABLE=1):

    • dEQP-GLES3.functional.shaders.loops.for_constant_iterations.*: 64/64 passed (0 failed), dmesg GPU fault delta 0.
  • Shader statistics (AGX_MESA_DEBUG=shaderdb under libasahi_noop_drm_shim.so), aurora-wip 7ba29572 vs identical code before rebase (ASAHI_SHIM_CHIP profiles g17s, g17g-compile-only, g18g, g17p, g16g, g14g, g13g, plus a local compile-only g17c profile on 7ba29572 with gpu_generation=17, gpu_variant='C', gpu_revision=0, chip_id=0x6050, num_dies=1, num_clusters_total=4, num_cores_per_cluster=10, core_masks={0x3ff, 0x3ff, 0x3ff, 0x3ff}, usc_generation=3, gpu_hal_generation=DRM_ASAHI_GPU_HAL_300, features=SOFT_FAULTS|RENDER_RESOURCES|RENDER_RESOURCES_V2):

    • 617 compute shaders of Cyberpunk 2077: on G17C and G17S, 56 change (45 use fewer registers, 44 gain occupancy e.g. 111 -> 103 registers and 896 -> 1024 threads; total instructions 716,277 -> 717,806 on G17C, spills 2,054 -> 2,041, fills 2,047 -> 2,036 across 3 shaders). On G17G, 5 change (3 with fewer registers and more threads). No change on G18G (347 of 617 compile), G17P, or G13G.
    • 483 compute kernels of lily (all compile on G17C, G17S, G17G; 41 on G18G, G17P, G16G; 25 on G13G, G14G): no change on any chip.
  • Prior to rebase (on aurora-wip 7ba29572 with identical code), 12 of the 200 supported cases in the physical_buffer set compiled differently and passed on both; across 20,000 evenly spaced cases over the Vulkan mustpass, no case outside cooperative matrices compiled differently. Earlier on M5 Max (57df4db849a with #450, #451, #452), a 22-frame Cyberpunk 2077 2.5 capture (3840x2160 High, FSR2 Balanced, GFXReconstruct replay, median of 3 alternating runs) measured 12.207 -> 12.274 FPS (+0.55%, within the 11.99-12.43 FPS run spread; stage times changed by < 0.2%).

@RyanTheTide

Copy link
Copy Markdown

Thanks for extending rematerialization.

We select behaviour by a hardware property, here the ISA encoding, rather than by the chip it was tested on. The change adds a chip id next to an encoding test:

  • src/asahi/compiler/agx_spill.c:299-300 (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B || ctx->key->dev.chip == AGX_CHIP_G17C)

G17G, G17S and G18G use the same AGX3C encoding as G17C, so they would keep reading the move back from scratch for no stated reason. Could the test be encoding == AGX3B || encoding == AGX3C (or every encoding where a uniform move can be re-emitted), with a code comment naming the hardware reason if one encoding must be excluded?

If you make it the default more widely, we can supply rows on our M1 Pro, M2, M4 Pro and A18 Pro.

@DjDeveloperr

Copy link
Copy Markdown

Merge train: independent review (code only, Opus 5.5 xhigh)

Verdict: CLEAN WITH NITS. No blocker or major (G17P already had the same rematerialization). NIT: no new test covers the G17C path.

Reach: gated on chip == AGX_CHIP_G17C, and no libagx target compiles as G17C, so this changes no codegen, layouts or cache keys on G17S/G17G (M5 / M5 Pro) or G17P. The train holds it only on RyanTheTide's open property-gate request (owner decision pending on merging G17C-gated PRs as-is). On widening: keying on the AGX3C encoding would also sweep in G18G (encoding AGX3C, chip G17G, emit_agx3d), and any encoding-wide gate changes the precompiled g17g/g18g libagx kernels every M5 part loads. For this PR, G18G's uniform-to-register copy is only known to work for destinations below half 128.

A spilled copy of a 32-bit uniform word can be made again at its use
instead of being read back from scratch. AGX3B already does this; the
AGX3C copy form reads any even uniform pair into any 32-bit register the
same way, so G17G, G17S and G17C take it too. This covers the AGX3 word
copy; AGX2 reads uniforms with its own bitop and keeps its spill
behaviour.

G18G, which compiles as AGX3C and re-encodes as agx3d, is left out: a
rematerialized copy is placed under register pressure, where its
destination is often at or above half 128, and agx3d copies into those
destinations have not been verified to execute correctly.

The uniform rematerialization tests now cover every AGX3C part, check
that after spilling there at least sixteen uniform moves remain with no
memory operands, and that G18G keeps the copies in memory.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb force-pushed the j716c/t2-uniform-spill branch from 210b063 to b5a86e1 Compare November 21, 2026 16:52
@erikwb erikwb changed the title agx: rematerialize uniform moves when spilling on G17C agx: rematerialize uniform moves when spilling on AGX3C Nov 21, 2026
@erikwb

erikwb commented Nov 21, 2026

Copy link
Copy Markdown
Author

Note

🤖 Claude Opus 5.5 responding on behalf of erikwb

Thanks. Changes in this revision:

  • Gate: the test is now encoding == AGX3B || (encoding == AGX3C && !agx_is_g18g(...)). The chip id is gone, and G17G, G17S and G17C all rematerialize uniform moves. This is the AGX3 word-copy path. AGX2 reads uniforms with its own bitop and keeps its spill behaviour.
  • G18G is left out, and the comment says why. A rematerialized copy is placed where registers are tight, so its destination is often at or above half 128. Copies into those destinations haven't been verified to execute correctly on G18G hardware. They can be encoded: shaders pack as AGX3C and the agx3d re-encode accepts high destinations. Only the direct agx3d encoder (agx3_copy_encode_uniform_to_reg32()) refuses them. So the exclusion rests on missing execution evidence, not on an encoding failure.
  • Tests (Dj's nit):
    • UniformRemat.ImmutableWordCopiesOnly runs over G13G, G17P, G17G, G17S, G17C and G18G. It expects rematerialization on AGX3B and AGX3C but not on G18G.
    • UniformRemat.SpillerRecreatesUniformsWithoutMemoryTrafficOnAGX3C: on G17G, G17S and G17C, at least sixteen uniform moves remain after spilling, with no memory operands.
    • UniformRemat.G18GSpillsUniformCopies checks that G18G still has spill memory operands.
    • With G18G admitted, the first and last of these fail.
  • Rebased onto aurora-wip fa9a676. The rebase was clean.

Shader statistics, aurora-wip vs this change (host compiles under the noop drm-shim). "No change" means the tracked statistics (instructions, registers, threads, spills, fills, scratch, code size) match for the shaders that compiled on both trees:

  • 617 Cyberpunk 2077 compute shaders: on G17C and G17S, 56 change. 45 use fewer registers and 44 of them gain occupancy (for example 111 -> 103 registers, 896 -> 1024 threads). 44 get a few more instructions, from the copies made again at their uses: 716,277 -> 717,806 in total. Spills drop in 3 (2,054 -> 2,041 spills, 2,047 -> 2,036 fills). On G17G, 5 change (3 with fewer registers and more threads). No change on G17P or G13G, or on G18G, where 347 of the 617 compile.
  • 483 lily compute kernels: no change on any chip.
  • Precompiled libagx kernels: no kernel changes on any target.

On the M5 Max (G17C), the CTS sets in the description match aurora-wip case for case, including the cases where this change compiles differently. Rows on your M1 Pro, M2, M4 Pro and A18 Pro should show no change, since nothing outside AGX3C changes. A G17G or G17S row would cover the newly enabled parts.

@RyanTheTide

Copy link
Copy Markdown

No blockers. Re-read b5a86e1 against the reviewed 210b063.

  1. Selection: resolved. agx_can_rematerialize_uniform() now tests encoding == AGX3B || (encoding == AGX3C && !agx_is_g18g()), and agx_is_g18g() is the emit_agx3d property, so no chip id is left. G18G stays out, and the comment gives the hardware reason: agx3d copies into destinations at or above half 128 haven't been run on hardware. That's a fair reason.
  2. Dj's test nit is covered: ImmutableWordCopiesOnly covers all six targets, SpillerRecreatesUniformsWithoutMemoryTrafficOnAGX3C covers G17G, G17S and G17C, and G18GSpillsUniformCopies pins the exclusion.
  3. Evidence: the M5 Max (G17C) CTS sets, which include the 12 cases that now compile differently, match aurora-wip. The Cyberpunk statistics (fewer registers and more threads in 44 shaders, at a cost of +0.2% instructions) and the unchanged libagx kernels are good enough for G17C. As with agx: keep half register FFMA operands that several FFMAs read on AGX3C #386, only an M5 or M5 Pro row would add coverage for G17G and G17S. Our Macs (AGX3B and AGX2) don't change.

Assisted-by: LLM (Claude:Opus 5.5)

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

Review of b5a86e1c551d547d85fb0dbc516484c261cf9266

0 code/policy/attribution blockers. Holding APPROVE only for §5 hardware verification on newly enabled AGX3C parts.

Checklist summary

  • §1 No model gating: Pass. agx_can_rematerialize_uniform() (src/asahi/compiler/agx_spill.c:307-321) keys on encoding == AGX_ISA_ENCODING_AGX3B || (encoding == AGX_ISA_ENCODING_AGX3C && !agx_is_g18g(&ctx->key->dev)). agx_is_g18g() checks the emit_agx3d ISA property rather than a chip ID, and the G18G exclusion is backed by agx3_copy_encode_uniform_to_reg32() rejecting AGX3D destinations >= 128 (src/asahi/compiler/agx3_copy_core.c:286-288) until high-destination agx3d copies are hardware-verified.
  • §2 Hygiene & §3 Correctness: Pass. Reuses the existing AGX3B eligibility invariants (non-preamble, !uniforms_written, scalar 32-bit normal non-memory dest, aligned even 32-bit uniform < 512, no modifiers), which agx3_copy_encode_uniform_to_reg32() encodes across the full 0..254 half-register destination range on AGX3C. Unit tests in test-uniform-remat.cpp cover all 6 targets (G13G, G17P, G17G, G17S, G17C, G18G), verify zero spill memory traffic on AGX3C, and pin G18G's spill behavior.
  • §4 Attribution & clean-room: Pass. Single commit carries human author/committer and Signed-off-by only; PR body includes Assisted-by:.
  • §6 Naming: Pass. Compiler-only (agx), no Honeykrisp/hk_ naming issues.
  • CI / Mergeability: MERGEABLE / CLEAN against aurora-wip; no CI checks configured on this branch. Host meson test --suite asahi (166/166) and agx_tests (1218 pass, 1 skip) pass on b5a86e1c, and precompiled libagx arrays are unchanged across all targets.

Remaining items (§5 evidence)

  1. Newly enabled AGX3C hardware (G17G M5 / G17S M5 Pro): at least one hardware run covering the cooperative-matrix CTS subset (matching aurora-wip case-for-case) with regression checks (0 SELinux/AVC denials, 0 failed units, 0 new kernel errors).
  2. Rebased-head regression row on b5a86e1c: the M5 Max (G17C) CTS sets were run on identical code prior to rebase (7ba29572); please include the regression row (0 AVC / 0 failed units / 0 new kernel errors) on the rebased head when running hardware checks. (AGX2, AGX3B, and G18G codegen and libagx are unchanged, so no rows are needed there.)

Assisted-by: LLM (Gemini 4 Neon)

@erikwb
erikwb marked this pull request as draft November 22, 2026 19:26
@erikwb

erikwb commented Nov 22, 2026

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Hydrogen responding on behalf of erikwb

Returning this PR to draft pending G17G and G17S hardware coverage, and updated the PR body with the rebased-head M5 Max (G17C) CTS and system-health regression row on b5a86e1c551d plus the compile-only drm-shim profile details for the shaderdb run.

On b5a86e1c551d vs base fa9a6766c01 (M5 Max J716c, G17C, kernel 7.3.0-ewb270+ #1 SMP PREEMPT_DYNAMIC Fri Nov 20 23:30:16 EST 2026, libvulkan_asahi.so sha256 d6f38d655757a6676365d0f031722a90a505edbd8524cf4413ba1200842abcd2 vs 6b871cdb18f7251e659caac9496a4590b13d867257bb9eecf40caa4edad634f0):

  • compute.pipeline.cooperative_matrix.*physical_buffer* (2000 evenly spaced): 200 Pass, 1800 NotSupported, 0 Fail on both (0 diffs).
  • compute.pipeline.cooperative_matrix.* (1500 evenly spaced): 150 Pass, 1350 NotSupported, 0 Fail on both (0 diffs).
  • System health over the run window: 0 new kernel/GPU dmesg errors, 0 new journal errors, and 0 new failed systemd units (aurora-peripherals.service was already in failed state from boot 18h earlier; SELinux/AVC is N/A on this Arch ARM host with CONFIG_SECURITY_SELINUX unset).
  • Re-verified host checks on b5a86e1c551d: meson test --suite asahi passes 166/166, agx_tests passes 1218 with 1 skipped (NativeFMA.ExactDyadicOracle), and generated src/asahi/lib/libagx_shaders.c is byte-for-byte identical to fa9a6766c01 (sha256: cf7112a67527e000f35c24ce133e349741c391fd43808898d47b219399dab84d).
  • Clarified in the PR body that the G17C shaderdb rows used a locally extended libasahi_noop_drm_shim.so compile-only ASAHI_SHIM_CHIP=g17c profile (gpu_generation=17, gpu_variant='C', gpu_revision=0, chip_id=0x6050, 4 clusters x 10 cores, DRM_ASAHI_GPU_HAL_300), alongside the in-tree g17s, g17g-compile-only, g18g, g17p, g16g, g14g, and g13g profiles.

Still unrun (PR moving back to draft until covered): G17G (M5) and G17S (M5 Pro) cooperative-matrix CTS + regression checks, as no G17G or G17S hardware is available here.

@erikwb
erikwb marked this pull request as ready for review November 22, 2026 19:31

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

Approved.

Blockers

None.

Checked

  • Audited src/asahi/compiler/agx_spill.c and src/asahi/compiler/test/test-uniform-remat.cpp, plus callers in src/asahi/compiler/agx_register_allocate.c.
  • Verified agx_can_rematerialize_uniform() uses ISA encoding / backend capability gating (AGX_ISA_ENCODING_AGX3B or AGX_ISA_ENCODING_AGX3C excluding G18G pending verified high-destination >= 128 uniform copy encoding) and preserves the immutability, alignment, range (< 512), and non-modifier preconditions across both spilling and RA cost estimation.
  • Verified build, 206/206 non-layout asahi tests (including 5/5 UniformRemat.* unit tests covering AGX3B, AGX3C, and G18G spill retention), and live GLES3 CTS on Apple M5 Max (G17C B1), Linux 7.3 (MESA_SHADER_CACHE_DISABLE=1) with 0 GPU faults in dmesg.
  • Untested scope: Live execution on other AGX3C variants (G17G, G17S) and G18G.
Evidence
  • meson test --suite asahi (non-layout): 206/206 OK (agx_tests OK)
  • agx_tests --gtest_filter='*UniformRemat*': 5/5 passed (ImmutableWordCopiesOnly, SpillerRecreatesUniformsWithoutMemoryTraffic, SpillerRecreatesUniformsWithoutMemoryTrafficOnAGX3C, G18GSpillsUniformCopies, UniformWritesKeepCapturedValuesInMemory)
  • Live Apple M5 Max (G17C B1), Linux 7.3, MESA_LOADER_DRIVER_OVERRIDE=asahi MESA_SHADER_CACHE_DISABLE=1:
    • dEQP-GLES3.functional.shaders.loops.for_constant_iterations.*: 64/64 passed (0 failed)
    • dmesg GPU fault delta: 0

@DjDeveloperr

Copy link
Copy Markdown

J714s (Apple M5 Pro / T6050 / G17S B1) exact-head PASS at b5a86e1c551d vs base fa9a6766c012 (kernel 7.3.0-m5-cdirect14, headless):

  • Unit tests (head b5a86e1c551d): UniformRemat.* 5/5 PASS; agx_tests 1,212/1,212 PASS (1 skipped: NativeFMA.ExactDyadicOracle); meson test --suite asahi 147/147 PASS (0 fail).
  • Vulkan CTS dEQP-VK.compute.pipeline.cooperative_matrix.* (17,924 mustpass cases, A/B):
    • Head b5a86e1c551d: 1,480 Pass, 16,444 Skip, 0 Fail (48 s)
    • Base fa9a6766c012: 1,480 Pass, 16,444 Skip, 0 Fail (49 s)
    • Case-for-case diff: 17,924/17,924 identical, 0 regressions.
  • Extra Vulkan CTS (workgroup_memory_explicit_layout.* full + subgroups 1/10, 8,689 cases): Head b5a86e1c551d 4,943 Pass, 3,746 Skip, 0 Fail (7 s).
  • Health: 0 GPU faults, 0 kernel/journal errors, 0 new failed units, post-run GPU liveness OK (vulkaninfo=1 dispatch=1 kernel=ok). (GLES3 mustpass was not exercised in this slot.)

@DjDeveloperr
DjDeveloperr merged commit 2b8c4ed into aurora-wip Nov 25, 2026
@RyanTheTide
RyanTheTide deleted the j716c/t2-uniform-spill 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.

4 participants