Skip to content

agx: trim unused trailing vector memory load channels after address lowering - #877

Merged
DjDeveloperr merged 7 commits into
aurora-wipfrom
j716c/agx-shrink-vectors-pre-address
Nov 26, 2026
Merged

DjDeveloperr merged 7 commits into
aurora-wipfrom
j716c/agx-shrink-vectors-pre-address

Conversation

@erikwb

@erikwb erikwb commented Nov 24, 2026 •

Copy link
Copy Markdown

What: Trim unused trailing vector channels from scalar-consumed memory loads after address lowering, agx_nir_opt_select_load(), and CSE in the Asahi compiler, preserving shared-address load widths and skipping zero-gain divergent power-of-two trims, and merging aurora-wip.

Tested: Host x86_64 (-Doptimization=2 -Ddebug=false -Db_ndebug=false) and three Macs at 8d56adc5738 vs 262403b7bce1 (aurora-wip):

  • Host x86_64: meson test --suite asahi (207/207 passed, 0 failed) and agx_tests (1,619 passed, 1 pre-existing skip at test-rte-fma.cpp:245, 0 failed; 7/7 targeted ShrinkMemoryLoadsTest.* and AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack across AGX2/G13G, AGX3B/G17P, AGX3C/G17S, and G18G/AGX3D for load_agx and load_constant_agx).
  • J713 (M4 Air / G16G): 139,608 Vulkan CTS cases (ssbo, ubo, memory_model, robustness: 61,308 Pass / 78,300 NotSupported / 0 Fail), 0 diffs; separate uncached AGX_MESA_DEBUG=shaderdb across 119,023 compiled shaders: 754 VS shaders reduced GPRs (-6,388 total, -0.31%), 0 shaders increased GPRs, 0 spills, 0 fills, 0 occupancy changes (54 VS shaders +1 to +2 instrs); 0 GPU faults, 0 failed systemd units, 0 AppArmor denials (SELinux N/A). ray_query = false in hk_physical_device.c at 262403b7bce1, so the prior 39 -> 47 RT fragment shader GPR check cannot be re-verified on this base.
  • J716c (M5 Max / G17C B1): 139,608 Vulkan CTS cases (ssbo, ubo, memory_model, robustness: 77,094 Pass / 62,514 NotSupported / 0 Fail), 0 diffs, 0 GPU-error lines, 0 AVC (SELinux N/A); ray_query = false, so rt.scene skips.
  • J313 (M1 Air / G13G B1): 139,608 Vulkan CTS cases (ssbo, ubo, memory_model, robustness: 47,540 Pass / 91,564 NotSupported / 504 pre-existing DeviceLost / 0 Fail), 0 diffs, 0 GPU fault/recovery lines, 0 AppArmor denials (SELinux N/A).
  • Prior superseded runs (regression still open for retest at 8d56adc5738): at d14a9373101d, external MacBook Neo (G17P B1) testing measured a regression in rt.scene (trace=pipeline -2.75% FPS, 183 -> 191 GPRs, 576 -> 512 threads, 4:14 spills:fills; trace=query -1.48% FPS, 0:0 spills:fills); at 6220042c7bce, J716c (G17C) Cyberpunk 2077 T2 engine times (-0.47 ms compute, -0.15 ms fragment).

Risk: two-way door · affects AGX2 (G13G), AGX3B (G16G/G17P), AGX3C (G17S/G17C), and AGX3D (G18G) shaders compiling through Borealis and Gallium Asahi.

Not yet: External MacBook Neo (G17P B1) retest of 8d56adc5738 vs 262403b7bce1 to confirm whether the d14a9373101d rt.scene regression (4:14 spills:fills, -2.75% trace=pipeline FPS, -1.48% trace=query FPS) is resolved (collecting AGX_MESA_DEBUG=shaderdb in a separate uncached run on both arms and timing both rt.scene modes with diagnostics disabled); J713 (G16G) ray-tracing fragment shader GPR check (39 -> 47 at 6220042c7bce) on an RT-enabled base (ray_query = false at 262403b7bce1); J716c (G17C) Cyberpunk 2077 T2 A/B engine times at 8d56adc5738; external J714s (M5 Pro / G17S) coverage.

Details

Proposed updated PR title for post-address lowering: agx: trim unused trailing vector memory load channels after address lowering.

agx_nir_shrink_memory_loads() runs in agx_optimize_nir() (src/asahi/compiler/agx_compile.c) after agx_nir_lower_address(), agx_nir_opt_uniform_stride_chains(), agx_nir_opt_uniform_stride(), agx_nir_opt_select_load(), and an unconditional nir_opt_cse() / nir_opt_dce() pass, before subword load narrowing and preamble formation.

Key behaviors at 8d56adc5738:

  • Handles both unlowered (load_ubo, load_ssbo, load_global, load_global_constant, load_constant, load_push_constant) and lowered (load_agx, load_constant_agx) multi-component loads, while skipping load_scratch, ACCESS_BUFFER_DESCRIPTOR_AGX, ACCESS_VOLATILE, AGX_NIR_MEMORY_PACKED_COUNT_MASK, AGX_NIR_MEMORY_UNPACK_W8_WORD, and non-PIPE_FORMAT_R{8,16,32}_UINT formats.
  • Uses agx_def_only_scalar_alu_uses() (checking nir_foreach_use_including_if and scalar nir_op_mov chains up to depth 4) to leave non-ALU uses and vector-constructing / nir_op_pack_* ops untouched so vector register affinity stays intact.
  • Computes the live trailing component count via util_last_bit(nir_def_components_read(&intr->def)) (rounded up to a power of two above 4).
  • Calls nir_divergence_analysis() and skips divergent trims where util_next_power_of_two(rounded) >= util_next_power_of_two(orig) (e.g. divergent vec4 -> vec3), avoiding zero-GPR-gain narrowing while keeping uniform vec4 -> vec3 uniform-slot trimming and divergent vec4 -> vec1/vec2 trimming.
  • Matches sibling memory loads in the same nir_function_impl (agx_memory_loads_match(), including equal scalar constant sources) and keeps matching loads at their shared maximum target component count so downstream post-LICM CSE and load merging continue to see uniform widths across blocks.
  • AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack (src/asahi/compiler/test/test-agx3-pack-binary.cpp) verifies both load_agx and load_constant_agx shrink to 16-bit vec3 in NIR and emit a 3-component W16 load (mask == 0x7, with zero 0xf loads) in the packed binary across AGX2 (G13G), AGX3B (G17P), AGX3C (G17S), and G18G (AGX3D).
Evidence
  • Commits on j716c/agx-shrink-vectors-pre-address:
    • 6220042c7bc: agx: trim unused trailing vector memory load channels before address lowering
    • 2e451a8c2f6: Merge branch 'aurora-wip' into j716c/agx-shrink-vectors-pre-address
    • f743472aa1e: agx: leave vector-feeding and non-ALU memory loads intact
    • d14a9373101: agx/tests: derive G16G test ISA encoding from agx_chip_isa_encoding()
    • 68022021488: Merge remote-tracking branch 'origin/aurora-wip' into HEAD (merges 262403b7bce, resolving src/asahi/compiler/agx_compile.c, src/asahi/compiler/agx_nir.h, and src/asahi/compiler/test/test-agx3-pack-binary.cpp)
    • c5f074c59ef: agx: shrink memory loads after CSE and skip zero-gain divergent trims
    • 8d56adc5738: agx/tests: verify packed 3-component W16 loads across AGX2, AGX3B, AGX3C, and G18G
  • Host checks at 8d56adc5738 (meson setup -Doptimization=2 -Ddebug=false -Db_ndebug=false -Dvulkan-drivers=asahi -Dtools=asahi -Dbuild-tests=true -Dllvm=enabled -Dmesa-clc=enabled -Dprecomp-compiler=enabled):
    • meson test --suite asahi: 207/207 passed (0 failed).
    • agx_tests --gtest_brief=1: 1,619 passed, 1 skipped (test-rte-fma.cpp:245), 0 failed (1,620 tests across 144 suites).
    • agx_tests --gtest_filter="ShrinkMemoryLoadsTest.*:AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack": 7/7 passed (AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack across AGX2/G13G, AGX3B/G17P, AGX3C/G17S, and G18G/AGX3D for both load_agx and load_constant_agx, plus all 6 ShrinkMemoryLoadsTest.* tests).
    • git merge-tree --write-tree origin/aurora-wip HEAD: d019eeba7c0c (clean).
  • Hardware A/B checks at 8d56adc5738 vs 262403b7bce1 (aurora-wip, aarch64 release builds with -Doptimization=2 -Ddebug=false -Db_ndebug=false -Dvulkan-drivers=asahi, selected via VK_DRIVER_FILES + LD_LIBRARY_PATH):
    • J713 (M4 Air / G16G, 8 cores, Linux 7.3.0):
      • Vulkan CTS (dEQP-VK.ssbo.* 12,225 + ubo.* 13,240 + memory_model.* 17,300 + robustness.* 96,843 = 139,608 cases, shader cache enabled, 6 deqp-vk jobs): 61,308 Pass / 78,300 NotSupported / 0 Fail / 0 Crash on both arms, 0 per-case diffs.
      • Separate uncached AGX_MESA_DEBUG=shaderdb (MESA_SHADER_CACHE_DISABLE=true, dEQP-VK.glsl.*, ssbo.layout*, ubo.*, fragment_shader_interlock.*, compute.pipeline.shader_object*: 45,854 cases, 33,985 cases emitting stats = 119,023 compiled shaders per arm, paired shader-by-shader):
        Stage Shaders GPRs base GPRs PR Fewer GPRs More GPRs Occupancy changed Spills:fills base → PR
        FS 73,594 1,033,880 1,033,880 0 0 0 0:0 → 0:0
        VS 37,494 689,294 682,906 754 0 0 0:0 → 0:0
        CS 7,378 328,421 328,421 0 0 0 0:0 → 0:0
        TCS 366 7,686 7,686 0 0 0 0:0 → 0:0
        GS 191 655 655 0 0 0 0:0 → 0:0
        all 119,023 2,059,936 2,053,548 (−6,388, −0.31%) 754 0 0 0:0 → 0:0
      • VS GPR drops: -8 in 673 shaders, -16 in 30, -12 in 22, -4 in 21, -20 in 4, -24 in 4; 54 VS shaders +1 to +2 instructions (44 x +1, 10 x +2); totals 4,148,528 -> 4,148,592 instructions, 42,674,450 -> 42,674,284 code bytes, 3,984,882 -> 3,984,148 uniforms; 0 per-case diffs (33,775 Pass / 11,700 NotSupported / 210 pre-existing DeviceLost / 169 pre-existing Fail).
      • Ray tracing: dEQP-VK.ray_query.* + dEQP-VK.ray_tracing_pipeline.* (57,190 cases) are 57,190/57,190 NotSupported on both arms because src/asahi/vulkan/hk_physical_device.c (hk_get_device_extensions()) hard-codes ray_query = false at 262403b7bce1, so the prior 6220042c7bce J713 RT fragment shader 39 -> 47 GPR check cannot be re-verified on this base.
      • System regression: 0 GPU fault/recovery/timeout/MMU fault/OOM lines, 0 WARNING/BUG/Oops/Call trace, 0 failed systemd units, 0 AppArmor denials, 0 AVC (SELinux not installed).
    • J716c (M5 Max / G17C B1, Linux 7.3.0):
      • Vulkan CTS (139,608 cases, shader cache enabled, 6 shards): ssbo.* 12,225 (12,162 Pass / 63 NotSupported), ubo.* 13,240 (13,240 Pass), memory_model.* 17,300 (3,796 Pass / 13,504 NotSupported), robustness.* 96,843 (47,896 Pass / 48,947 NotSupported) = 77,094 Pass / 62,514 NotSupported / 0 Fail / 0 Crash on both arms, 0 per-case diffs.
      • System regression: 0 GPU-error lines, 0 AVC (SELinux not installed). gpubench-rt-vk --info reports ray_query: false (rt.scene skips).
    • J313 (M1 Air / G13G B1, Linux 7.3.0):
      • Vulkan CTS (ssbo.* + ubo.* + memory_model.* + robustness.* = 139,608 cases, shader cache enabled, 6 shards): 47,540 Pass / 91,564 NotSupported / 504 pre-existing DeviceLost (0 Fail / 0 Crash) on both arms, 0 per-case diffs.
      • System regression: 0 GPU fault/timeout/recovery/lost-device/EIO lines, 0 Oops/BUG/WARNING/Call trace, 0 OOM kills, 0 AppArmor denials, 0 AVC (SELinux not installed); the 504 DeviceLost cases correspond 1:1 to pre-existing M3 compute/render rejected + ENOTSUPP rejections identical on both arms.
  • Superseded prior-head measurements:
    • d14a9373101d on J713 (G16G, Vulkan CTS 7ba03cdac7ae): dEQP-VK.ssbo.* stride 3 4,052 Pass / 23 NotSupported / 0 Fail, dEQP-VK.compute.pipeline.* + dEQP-VK.memory_model.* stride 10 793 Pass / 2,967 NotSupported / 0 Fail (0 diffs vs 6eb4883958d5); shaderdb 1,415 shaders identical aggregate totals, uniforms changed in 12/1,415.
    • d14a9373101d on MacBook Neo (G17P B1, external): rt.scene trace=pipeline -2.75% (183 -> 191 GPRs, 576 -> 512 threads, 0:0 -> 4:14 spills:fills) and trace=query -1.48% (0:0 -> 0:0 spills:fills).
    • 6220042c7bce on J716c (G17C): Cyberpunk 2077 T2 compute -0.47 ms, fragment -0.15 ms.

…lowering

Add a targeted agx_nir_shrink_memory_loads() pass in agx_optimize_nir()
before addressing lowering to drop unused trailing channels from
non-volatile UBO/SSBO/global/constant/push-constant vector loads without
introducing address-offset arithmetic. Preserves scratch load widths
(required for AGX3 16-bit dynamic stack loads) and 4-component
ACCESS_BUFFER_DESCRIPTOR_AGX loads (required for preamble speculation).

Signed-off-by: Erik Bourget <erik@ebourget.net>

@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 merge at 8d56adc5738a. Thanks, Erik.
Blockers none.
Checked

  • c5f074c59ef4 moves the trim after address lowering, agx_nir_opt_select_load() and CSE. Loads that still match (same intrinsic, sources, base, format, sign extension and range) keep the widest width any of them needs, so CSE and select_load can still merge them.
  • On load_agx and load_constant_agx, only plain R8/R16/R32 formats are trimmed; packed counts and W8-word unpacks keep their width. Divergent loads are trimmed only when that lowers their power-of-two register width. Descriptor and volatile loads are still left alone.
  • The J700 re-test at this head clears the earlier RT spill and FPS drop, and RT CTS is 675/0/14. The J713, J716c and J313 rows at this head show no CTS differences.
  • Not a blocker: the sibling search compares every pair of loads in a function, which costs compile time only on very large shaders.

@DjDeveloperr

Copy link
Copy Markdown

G17P B1 verification: base 9322511c94e vs head 6220042c7bce1fab088986b8d97364905a223af7

Summary: a G17P ray-tracing regression. The ray-tracing pipeline kernel starts spilling and rt.scene with pipeline tracing drops 2.27% in frame rate. Conformance and the other workloads are clean.

Static corpus

Shader statistics, all corpora (shader-db, Minecraft packs, GB6 compute, gpubench, vk-synth), 10.9k–11.1k shaders per chip.

G17P: gpubench native +450 (+1.25%), 18 rows better and 12 worse. Spills go 0 → 24 and fills 0 → 64, all in ray-tracing kernels that already use the full 191-GPR file:

kernel native spills / fills
ray-query traversal CS e9a829388a690843 (10 rows, also b3a34fd16285dce7) 1,966 → 1,974 0 → 2 / 0 → 5
ray-tracing pipeline 60f3947b463257df (2 rows) 2,449 → 2,654 (+8.4%) 0 → 2 / 0 → 7
  • Elsewhere on G17P: shader-db −80 GPRs, native +10 (1 row better, 10 worse); GB6 and vk-synth +1 each.

G16G:

  • gpubench native −54 (9 rows better).
  • One ray-tracing fragment shader (9 rows) goes 39 → 47 GPRs at the same 1,024 threads.
  • shader-db −80 GPRs.

G17S:

  • gpubench native −49 and preamble −20 (20 rows better, 0 worse).
  • shader-db −64 GPRs.

No new failures on any chip.

Hardware: MacBook Neo (Apple A18 Pro, G17P B1)

  • Kernel: 7.3.0-41.fc44.aurora.aarch64, Mesa cross-built for both arms:
    • base 9322511c94e+0, tarball sha256 d582dadb3c6433eabfb5d2d7ac45fd7fef7b907ce375e459e0c94d56b19b5397
    • head 6220042c7bc+0, tarball sha256 603ce3022971d18e594eaa437fa2c2a77439adf1f352aa44bb591f1147ca832d
  • Setup: GPU held exclusively for the run, MESA_SHADER_CACHE_DISABLE=true.
  • gpubench (suite b701a2b): run base, head, head, base. Every run passed its correctness check.
    • Ray-tracing tests: --warmup-ms 500 --runs 3 --run-ms 500.
    • Other tests: --depths 1 --warmup-ms 100 --runs 3 --run-ms 400.
test base head change
rt.scene preset=medium trace=pipeline (fps) 56.03 / 55.97 54.74 / 54.72 −2.27%
rt.scene preset=medium trace=query (fps) 65.87 / 65.65 65.75 / 65.79 +0.02%
rt.rays preset=medium kind=coherent (Mrays/s) 406.3 / 404.5 407.8 / 406.1 +0.38%
rt.rays preset=medium kind=incoherent (Mrays/s) 284.1 / 284.0 284.9 / 284.5 +0.23%
compute.heavy kind=alu (µs/submit) 1388.9 / 1388.9 1388.9 / 1393.7 +0.17%
compute.heavy kind=bw (µs/submit) 2857.1 / 2857.1 2857.1 / 2836.9 −0.35%
compute.uber kind=divergent (µs/submit) 1246.1 / 1250.0 1250.0 / 1250.0 +0.16%
render.heavy kind=fill res=1080p (µs/submit) 688.5 / 684.9 684.9 / 683.8 −0.34%
  • Pipeline-trace scene: frame time goes 17.86 → 18.27 ms (+2.3%), and the base and head runs don't overlap. That is the pipeline kernel that grows and spills in the corpus.
  • Query-trace scene: flat despite its traversal kernel's two new spills.
  • rt.rays incoherent: its +0.23% doesn't overlap but is within the noise of this short run.
  • Everything else: overlaps.

Conformance (head):

  • Vulkan CTS: ssbo.layout.single_basic_type, ubo.single_basic_type, spirv_assembly.instruction.compute.{8bit,16bit}_storage. 4,857 cases: 3,980 pass, 0 fail, 877 not supported.
  • GLES3 functional.{ubo.single_basic_type,shaders.indexing}: 1,696 / 1,696.
  • Kernel log over both runs: 0 new GPU faults, 0 fault-pattern lines.

Likely cause: trimming a ray-tracing kernel's trailing load channels changes how its values are packed into vector registers, at a point where the kernel already uses the whole 191-GPR file. A fix should keep the ray-tracing pipeline kernel at 0 spills on G17P. One option is to trim only where the narrowed load doesn't raise register demand; another is to leave loads that feed vector values alone.

Signed-off-by: Erik Bourget <erik@ebourget.net>
Require every use (including if-uses and scalar mov chains) of a candidate vector load to terminate in scalar non-packing ALU ops, matching the pass invariant and preserving vector register packing/coalescing affinity. Also add unit tests covering scalar trimming, vector/pack/non-ALU skips, trimmed f16 vec3 packing on AGX2/AGX3B/AGX3C, and zero-spill G16G/G17P vector-load compilation.

Signed-off-by: Erik Bourget <erik@ebourget.net>
Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb requested review from a team as code owners November 24, 2026 21:29
@erikwb
erikwb requested a review from LiamRay11 November 24, 2026 21:29
@erikwb

erikwb commented Nov 24, 2026

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Hydrogen responding on behalf of erikwb

Updated the PR branch against aurora-wip and added a proposed guard for the vector/non-ALU register-pressure regression (hardware re-verification pending at d14a9373101):

  • Merge conflict resolved (2e451a8c2f6): Merged origin/aurora-wip (6eb4883958d) into j716c/agx-shrink-vectors-pre-address, keeping both agx_nir_shrink_memory_loads() and the new agx_preamble_allowed() / agx_nir_uniform_unit_global_id() helpers in src/asahi/compiler/agx_compile.c.
  • Proposed guard for ray-tracing register demand & non-ALU/vector uses (f743472aa1e):
    • Replaced the nir_instr_type_intrinsic-only filter with agx_def_only_scalar_alu_uses(), checking nir_foreach_use_including_if and chasing scalar nir_op_mov chains (depth < 4).
    • Loads with any non-ALU use (if, phi, tex, intrinsic, jump), multi-component ALU use (nir_op_vec*, vector mov, etc.), or lane-packing ALU op (nir_op_pack_*) are left untouched so COLLECT/SPLIT vector register affinity stays intact.
    • Because synthetic unit tests do not exercise the full reported ray-tracing shaders, this guard is a proposed fix until the named G17P shaders (ray-query traversal CS e9a829388a690843, ray-tracing pipeline 60f3947b463257df), rt.scene (trace=pipeline), and the J713 (G16G) ray-tracing fragment shader are rechecked on hardware.
  • Odd 16-bit vector load packing check (f743472aa1e): Added AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack in src/asahi/compiler/test/test-agx3-pack-binary.cpp, confirming 16-bit vec3 (6-byte) load_global and load_global_constant pack cleanly on AGX2 (G13G), AGX3B (G17P), and AGX3C (G17S) (while agx_nir_shrink_memory_load() continues skipping load_scratch for agx3_translate_dynamic_stack_load()).
  • Host compiler tests & PR body updates (d14a9373101): Added ShrinkMemoryLoadsTest (src/asahi/compiler/test/test-shrink-memory-loads.cpp) covering scalar trimming, vector/pack/non-ALU skips, and synthetic zero-spill compilation on G16G (deriving AGX3B via agx_chip_isa_encoding()) and G17P (AGX3B). Updated the PR body to attribute the superseded -0.47 ms compute and -0.15 ms fragment measurements at 6220042c7bce to Cyberpunk 2077 T2 on J716c (G17C), use J716c/G17C and J713/G16G consistently, and keep both the measured RT regressions and hardware CTS / Cyberpunk 2077 T2 / RT re-verification visible under **Not yet:** at d14a9373101.

@erikwb
erikwb requested a review from malik-na November 24, 2026 21:29

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

APPROVE. The pass safely trims unused trailing components from multi-component NIR memory loads when all uses are scalar ALU chains (avoiding vector-constructing, packing, non-ALU, scratch, descriptor, and volatile loads), builds cleanly with assertions enabled, and passes the full Meson test suite plus targeted live Vulkan CTS on Apple M5 Max with zero failures or GPU faults.

Blockers

None.

Checked

  • Audited all 5 changed files (+353/-0) at exact head d14a9373101d across all 4 commits (src/asahi/compiler/agx_compile.c, src/asahi/compiler/agx_nir.h, src/asahi/compiler/meson.build, src/asahi/compiler/test/test-agx3-pack-binary.cpp, src/asahi/compiler/test/test-shrink-memory-loads.cpp):
    • Pass placement (agx_nir_shrink_memory_loads right before agx_nir_lower_address) preserves invariants: nir_lower_mem_access_bit_sizes has already run, agx_nir_lower_address does not depend on divergence metadata, and subsequent passes that need divergence (agx_nir_opt_uniform_stride_chains, nir_opt_uniform_subgroup) recompute nir_divergence_analysis.
    • Narrowing predicate only touches load_ubo, load_ssbo, load_global, load_global_constant, load_constant, and load_push_constant with num_components > 1, skipping load_scratch, ACCESS_BUFFER_DESCRIPTOR_AGX, and ACCESS_VOLATILE.
    • agx_def_only_scalar_alu_uses() rejects if-uses, non-ALU uses, multi-component ALU destinations, and nir_op_pack_* ops while chasing scalar nir_op_mov chains up to depth 4; util_last_bit(nir_def_components_read(&intr->def)) (rounded up to power-of-two above 4) keeps intermediate channels intact for sparse read masks so downstream swizzles remain valid.
    • Verified agx_emit_load handles 1–4 component loads across 8-, 16-, and 32-bit element sizes.
    • Verified clean merge onto current aurora-wip (876b22c82bd8) via git merge-tree --write-tree.
  • Built exact head d14a9373101d on Linux 7.3 (-Dbuildtype=release -Db_ndebug=false -Dbuild-tests=true) and ran the full Meson test suite: 224/224 passed (0 failed), including all 4 new/updated unit tests (ShrinkMemoryLoadsTest.* and AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack).
  • Ran live Vulkan CTS (MESA_SHADER_CACHE_DISABLE=1) on Apple M5 Max against the built Borealis ICD across SSBO/UBO vec3/vec4, 8-bit and 16-bit storage, push-constant, buffer-access robustness, and memory-model suites: 1009 passed, 0 failed, 107 not supported (1116 total), with 0 GPU faults in dmesg.
  • Untested scope: Live ray-tracing spill/performance re-measurement on G16G/G17P hardware (noted in the PR description) was not run in this verification pass.
Evidence
  • Build (d14a9373101d, -Dbuildtype=release -Db_ndebug=false -Dbuild-tests=true): clean build (423/424 incremental targets linked).
  • Meson test suite (meson test --no-rebuild --print-errorlogs): Ok: 224, Fail: 0.
  • Targeted GoogleTest filter (agx_tests --gtest_filter="ShrinkMemoryLoadsTest.*:AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack"): 4/4 passed (AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack, ShrinkMemoryLoadsTest.TrimsTrailingUnusedScalarAluChannels, ShrinkMemoryLoadsTest.SkipsVectorPackingAndNonAluUses, ShrinkMemoryLoadsTest.CompilesVectorConstructingLoadWithoutSpillingOnG16GAndG17P).
  • Live Apple M5 Max Vulkan CTS (MESA_SHADER_CACHE_DISABLE=1, apiVersion = 1.4.363, Mesa 26.3.0-devel (git-d14a937310)):
    • dEQP-VK.ssbo.layout.single_basic_type.std430.*vec3*: 15/15 passed, 0 failed
    • dEQP-VK.ssbo.layout.single_basic_type.std430.*vec4*: 15/15 passed, 0 failed
    • dEQP-VK.ubo.single_basic_type.std140.*vec3*: 120/120 passed, 0 failed
    • dEQP-VK.ubo.single_basic_type.std140.*vec4*: 120/120 passed, 0 failed
    • dEQP-VK.spirv_assembly.instruction.compute.8bit_storage.*: 29/29 passed, 0 failed
    • dEQP-VK.spirv_assembly.instruction.compute.16bit_storage.*: 504/535 passed, 0 failed, 31 not supported
    • dEQP-VK.pipeline.monolithic.push_constant.*: 61/65 passed, 0 failed, 4 not supported
    • dEQP-VK.robustness.buffer_access.compute.*: 138/204 passed, 0 failed, 66 not supported
    • dEQP-VK.memory_model.message_passing.core11.u32.coherent.fence_fence.atomicwrite.device.payload_local.buffer.*: 7/13 passed, 0 failed, 6 not supported
  • dmesg GPU fault count before and after CTS: 0 -> 0.

@erikwb

erikwb commented Nov 24, 2026

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Neon responding on behalf of erikwb

Added exact-head J713 (M4 Air, G16G) sampled Borealis Vulkan CTS and shaderdb A/B results at d14a9373101d vs merge-base 6eb4883958d5 (Linux 7.3.0, Vulkan CTS 7ba03cdac7ae, same boot back-to-back):

  • dEQP-VK.ssbo.* (stride 3, 4,075 cases): 4,052 Pass / 23 NotSupported / 0 Fail, identical to base (0 diffs, 0 crashes).
  • dEQP-VK.compute.pipeline.* + dEQP-VK.memory_model.* (stride 10, 3,760 cases): 793 Pass / 2,967 NotSupported / 0 Fail, identical to base (0 diffs, 0 crashes).
  • AGX_MESA_DEBUG=shaderdb (MESA_SHADER_CACHE_DISABLE=true, 1,827 cases: ssbo.layout.* stride 4 + compute.pipeline.* stride 40): aggregate totals identical across 1,415 shaders (451,230 instrs, 388,559 alu, 14,800 ic, 3,989,046 bytes code, 89,236 gprs, 0 spills); only the uniforms field differs in 12/1,415 shaders (consistent with trimmed vector-load channels).

Updated the PR body's Tested, Not yet, and Evidence sections (remaining ubo.*/robustness.*/RT-shader recheck on J713, plus J716c, J313, and external Macs remain under Not yet). Head d14a9373101d is unchanged.

@DjDeveloperr

DjDeveloperr commented Nov 24, 2026 •

Copy link
Copy Markdown

MacBook Neo (J700, G17P B1) re-check at 8d56adc5738a0c0e88e937963308d5e0526669fd vs merge-base 262403b7bce1

The RT spill regression from this PR is gone. At this head the ray tracing pipeline kernel compiles exactly as it does at the merge-base: 191 GPRs, 512 threads and 4:14 spills:fills on both sides. rt.scene trace=pipeline moves +0.37%, which is within noise. All RT rows overlap.

The 4:14 spill itself is still there, but it now comes from aurora-wip, not from this PR. The merge-base 262403b7bce1 without this PR already compiles that kernel at 2,213 instrs, 191 GPRs, 512 threads and 4:14 spills:fills with 16 scratch. On 11-24 the older base 6eb4883958d5 compiled it at 2,192 instrs, 183 GPRs, 576 threads and 0:0, using the same gpubench binary. So it came in with something merged into aurora-wip between those two commits.

Setup

  • Linux 7.3.0-41.fc44.aurora.aarch64, on AC, Neo held for the run. Same boot for every arm. Per-arm VK_DRIVER_FILES kits; system Mesa untouched.
  • Release cross-builds (-Db_ndebug=true, empty diff), with MESA_SHADER_CACHE_DISABLE=true:
    • Base 262403b7bce1 (git-262403b7bc): libvulkan_asahi.so sha256 d1ecf7a100d4dccc67ccb401d11ab0d0b175d7437c33eba6a529605ad335450c
    • Head 8d56adc5738a (git-8d56adc573): libvulkan_asahi.so sha256 74197b493c68a31ea75ce18a3c8776ce83780cbfe04dca6551d2bf91309d19e3
  • gpubench b701a2b, the same binary as the 11-24 rows.

shaderdb (AGX_MESA_DEBUG=shaderdb, a separate uncached run on both arms)

Kernel Base 262403b7bce1 Head 8d56adc5738a
rt.scene trace=pipeline RT kernel 2,213 instrs, 191 GPRs, 512 threads, 4:14 2,214 instrs, 191 GPRs, 512 threads, 4:14
Ray-query traversal kernel 1,778 instrs, 191 GPRs, 512 threads, 0:0 same

The four RT rows compile 110 shaders in all (35 + 33 + 21 + 21). Across them, no GPR, thread or spill count changes. The head drops 2 to 8 uniforms from most shaders. Instruction totals move by −7 for trace=pipeline, −8 for trace=query, and 0 for both rt.rays rows.

Same-boot ABBA + BAAB (diagnostics off, all status=ok, identical CRCs)

Each arm has 4 samples per row, and each sample is the median of 3 × 500 ms runs.

Test Unit Base Head Pooled Δ ABBA / BAAB
rt.scene preset=medium trace=pipeline fps 64.28 64.52 +0.37% +0.40% / +0.34%
rt.scene preset=medium trace=query fps 69.87 69.88 +0.02% −0.01% / +0.06%
rt.rays preset=medium kind=coherent Mray/s 390.80 390.68 −0.03% −0.19% / +0.13%
rt.rays preset=medium kind=incoherent Mray/s 262.80 262.95 +0.06% +0.17% / −0.06%

On rt.scene trace=pipeline, gpu_rt_us is 8,505–8,543 µs at base and 8,465–8,503 µs at head.

Conformance and health (head)

  • RT CTS subset at head: 689 cases, 675 Pass / 14 NotSupported / 0 Fail, 0 recoveries. The subset is ray_tracing_pipeline.shader_binding_table.* (268), ray_tracing_pipeline.pipeline_library.* (301), ray_tracing_pipeline.misc.* (113) and the 7 RT pipeline_binary cases.
  • 0 GPU faults or recoveries, 0 AVCs, 0 failed units. The Neo's state was the same before and after.
Earlier re-check: d14a9373101d vs 6eb4883958d5 (11-24)

Exact-head MacBook Neo (J700, G17P B1) re-check — d14a9373101d vs 6eb4883958d5

Verdict: G17P ray-tracing regression still present. The proposed guard in f743472aa1e clears the earlier trace=query traversal kernel spills (0:0), but the rt.scene trace=pipeline kernel still loses an occupancy tier (576 -> 512 threads) and introduces new spills (0:0 -> 4:14), dropping rt.scene trace=pipeline by -2.75% across both interleaved run orders (and rt.scene trace=query by -1.48%). Vulkan CTS and health are clean.

Build & Setup
  • Hardware / Kernel: Apple MacBook Neo (J700, G17P B1), 7.3.0-41.fc44.aurora.aarch64, on AC, exclusive GPU lock, MESA_SHADER_CACHE_DISABLE=true, release cross-builds (-Db_ndebug=true, empty BUILD-DIFF.patch):
    • Base 6eb4883958d5 (git-6eb4883958): libvulkan_asahi.so sha256 f52974d1a77b0a0e8ee1689ecdf09af7187dd9c397d0d7c06bf7afb98dc60b0e.
    • Head d14a9373101d (git-d14a937310): libvulkan_asahi.so sha256 9d03d0cf252d4bce57509ec670e599391ad62e5c674cbd80b70b8a006bc1c600.
Hardware AGX_MESA_DEBUG=shaderdb (rt.scene trace=pipeline, G17P B1)
  • Pipeline RT kernel ([10]): 2,192 -> 2,219 instrs (2,376 -> 2,397 native), 183 -> 191 GPRs, 576 -> 512 threads, 0:0 -> 4:14 spills:fills.
  • Query traversal kernel ([2]): 1,777 -> 1,777 instrs (1,934 -> 1,928 native), 191 GPRs, 512 threads, 0:0 -> 0:0 spills:fills.
Interleaved ABBA + BAAB (gpubench-vk b701a2b, 4 arms/side, all status=ok, identical CRCs)
Test Unit Base (6eb4883958d5) Head (d14a9373101d) Pooled Δ Pass 1 (ABBA) / Pass 2 (BAAB)
rt.scene preset=medium trace=pipeline fps 64.97 63.19 -2.75% -3.45% / -2.02%
rt.scene preset=medium trace=query fps 68.90 67.88 -1.48% -1.65% / -1.31%
rt.rays preset=medium kind=coherent Mray/s 378.73 378.90 +0.05% -0.45% / +0.56%
rt.rays preset=medium kind=incoherent Mray/s 255.88 256.03 +0.06% +0.25% / -0.14%
compute.heavy kind=alu µs 1462.55 1462.57 +0.00% -0.18% / +0.18%
compute.heavy kind=bw µs 2787.49 2782.63 -0.17% -0.35% / +0.00%
render.heavy kind=fill res=1080p µs 781.27 780.73 -0.07% +2.74% / -2.78%

On rt.scene trace=pipeline, gpu_rt_us rises from 8,196 / 8,189 µs -> 8,698 / 8,701 µs in Pass 1 and 8,448 / 8,453 µs -> 8,791 / 8,934 µs in Pass 2 (~+5.5% RT pass time), with zero overlap between base and head in either pass.

Conformance & Health (d14a9373101d)
  • dEQP-VK (ssbo.layout.single_basic_type.*, ubo.single_basic_type.*, spirv_assembly.instruction.compute.{8bit,16bit}_storage.*): 3,980 Pass / 877 NotSupported / 0 Fail (4,857 cases).
  • Health: 0 GPU faults/timeouts/recoveries, 0 AVCs, 0 failed units.

Signed-off-by: Erik Bourget <erik@ebourget.net>

# Conflicts:
#	src/asahi/compiler/agx_compile.c
#	src/asahi/compiler/agx_nir.h
#	src/asahi/compiler/test/test-agx3-pack-binary.cpp
Running agx_nir_shrink_memory_loads() before agx_nir_lower_address(),
agx_nir_opt_select_load(), and nir_opt_cse() narrowed loads that shared
an address with wider sibling loads to mismatched component counts,
preventing CSE and select_load from merging them and adding instructions
and register pressure. Narrowing divergent vec4 loads to vec3 also left
power-of-two register allocation unchanged while breaking downstream
load matching.

Support lowered load_agx and load_constant_agx intrinsics (keeping
packed, W8-word, and typed formats intact) so shrinking runs after
address lowering, opt_select_load, and CSE, preserve matching widths
across sibling loads in the same function, and skip divergent trims that
do not reduce the power-of-two component count. Update unit tests and run
TrimmedF16Vec3MemoryLoadsPack with a uniform workgroup ID address so it
continues covering trimmed 16-bit vec3 packing.

Signed-off-by: Erik Bourget <erik@ebourget.net>
…X3C, and G18G

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb requested a review from a team as a code owner November 26, 2026 03:57
@erikwb erikwb changed the title agx: trim unused trailing vector memory load channels before address lowering agx: trim unused trailing vector memory load channels after address lowering Nov 26, 2026
@erikwb

erikwb commented Nov 26, 2026

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Hydrogen responding on behalf of erikwb

Thanks for the exact-head MacBook Neo (G17P B1) check at d14a9373101d (IC_kwDOU3NZwM8AAAABqg0dAA, measuring 4:14 spills:fills, 183 -> 191 GPRs, 576 -> 512 threads, and -2.75% FPS on rt.scene trace=pipeline, plus -1.48% on trace=query). Prepared a proposed update locally at 8d56adc5738 (with c5f074c59ef on top of the aurora-wip merge 68022021488; not yet pushed while d14a9373101d remains on GitHub) targeting the two causes of extra register pressure and lost merges seen on d14a9373101d — whether this resolves the Neo spill and throughput regression still needs to be confirmed by re-measuring on hardware:

  • Moved load shrinking after address lowering, agx_nir_opt_select_load(), and CSE (agx_optimize_nir() in src/asahi/compiler/agx_compile.c, c5f074c59ef): Running agx_nir_shrink_memory_loads() before agx_nir_lower_address(), opt_select_load, and nir_opt_cse() narrowed scalar-consumed loads that shared an address with wider sibling loads to mismatched component counts, preventing CSE and load-select merging. The pass now handles load_agx and load_constant_agx (leaving AGX_NIR_MEMORY_PACKED_COUNT_MASK, AGX_NIR_MEMORY_UNPACK_W8_WORD, and non-R{8,16,32}_UINT formats untouched) and preserves equal target widths across matching sibling loads in the same function (agx_memory_loads_match(), including equal scalar constant sources) so post-LICM CSE continues to merge them.
  • Skipped zero-gain divergent power-of-two trims (c5f074c59ef): Calling nir_divergence_analysis() keeps divergent loads whose util_next_power_of_two() register width would stay unchanged (such as divergent vec4 -> vec3) at their original width instead of preventing downstream merges without saving GPRs, while keeping uniform vec4 -> vec3 uniform-slot trimming and divergent vec4 -> vec1/vec2 trimming.
  • Host verification at 8d56adc5738: meson test --suite asahi passes (207/207, 0 failed) and agx_tests passes 1,619/1,620 (1 pre-existing skip at test-rte-fma.cpp:245, 0 failed). Targeted ShrinkMemoryLoadsTest.*:AGX3PackBinary.TrimmedF16Vec3MemoryLoadsPack passes (7/7): TrimmedF16Vec3MemoryLoadsPack (8d56adc5738) now verifies both load_agx and load_constant_agx shrink to 16-bit vec3 in NIR and emit a 3-component W16 load (mask == 0x7, zero 0xf loads) in the packed binary across AGX2 (G13G), AGX3B (G17P), AGX3C (G17S), and G18G (AGX3D).
  • Verified hardware runs at 8d56adc5738 vs 262403b7bce1 (aurora-wip, Borealis aarch64 release builds with asserts enabled):
    • J713 (M4 Air / G16G): 139,608 Vulkan CTS cases (dEQP-VK.ssbo.*, ubo.*, memory_model.*, robustness.*: 61,308 Pass / 78,300 NotSupported / 0 Fail), 0 diffs. In a separate uncached AGX_MESA_DEBUG=shaderdb run across 45,854 CTS cases (119,023 compiled shaders per arm, paired shader-by-shader): 754 VS shaders reduced GPRs (-6,388 total, -0.31%), 0 shaders increased GPRs, 0 spills/fills in either arm, and 0 occupancy changes (54 VS shaders +1 to +2 instructions; 0 per-case test diffs across 33,775 Pass / 11,700 NotSupported / 210 pre-existing DeviceLost / 169 pre-existing Fail). System regression: 0 GPU faults, 0 failed systemd units, 0 AppArmor denials (SELinux N/A). Note: src/asahi/vulkan/hk_physical_device.c hard-codes ray_query = false at 262403b7bce1 (57,190/57,190 dEQP-VK.ray_query.* + ray_tracing_pipeline.* NotSupported), so the prior 6220042c7bce J713 RT fragment shader 39 -> 47 GPR check cannot be re-verified on this base.
    • J716c (M5 Max / G17C B1): 139,608 Vulkan CTS cases (ssbo: 12,162 Pass / 63 NotSupported, ubo: 13,240 Pass, memory_model: 3,796 Pass / 13,504 NotSupported, robustness: 47,896 Pass / 48,947 NotSupported; 77,094 Pass / 62,514 NotSupported / 0 Fail total), 0 diffs, 0 GPU-error lines, 0 AVC (SELinux N/A); gpubench-rt-vk --info reports ray_query: false, so rt.scene skips.
    • J313 (M1 Air / G13G B1): 139,608 Vulkan CTS cases (ssbo, ubo, memory_model, robustness: 47,540 Pass / 91,564 NotSupported / 504 pre-existing DeviceLost / 0 Fail), 0 diffs, 0 GPU fault/recovery lines, 0 AppArmor denials (SELinux N/A).
  • Proposed PR title & remaining external/RT checks: Proposed updated PR title for post-address lowering: agx: trim unused trailing vector memory load channels after address lowering. Once 8d56adc5738 is pushed, external re-verification on MacBook Neo (G17P B1) needs AGX_MESA_DEBUG=shaderdb collected in a separate uncached run on both A/B arms (for GPRs, occupancy, spills, and fills) and both rt.scene modes (trace=pipeline and trace=query) timed with diagnostics disabled (so shaderdb serialization/logging does not skew FPS), alongside an RT-enabled base check for the prior J713 39 -> 47 fragment shader GPR regression, J716c (G17C) Cyberpunk 2077 T2 A/B timing, and external J714s (M5 Pro / G17S) coverage.

@DjDeveloperr
DjDeveloperr merged commit babd36d into aurora-wip Nov 26, 2026
@RyanTheTide
RyanTheTide deleted the j716c/agx-shrink-vectors-pre-address 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.

3 participants