Skip to content

agx: affine strided shared-offset decomposition and M5 multi-bank texture staging cache - #1524

Merged
DjDeveloperr merged 3 commits into
aurora-wipfrom
m5/agx-shared-affine-and-stage-cache
Dec 2, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-wipfrom
m5/agx-shared-affine-and-stage-cache

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Dec 2, 2026 •

Copy link
Copy Markdown

What

Ports two compiler optimizations cleanly onto aurora-wip (always-on, no new debug/env flags):

  1. Affine strided shared-memory offset decomposition and cross-block greedy anchoring (agx_nir_lower_shared_bitsize.c, shared across all AGX3B and AGX3C targets, including G18G and G17P):
    • Extends agx_nir_opt_shared_byte_offsets() to decompose affine shared-memory element-index expressions (iadd, isub, ishl, imul/amul, imadshl_agx, imsubshl_agx) into scaled terms plus a modular constant offset, rolling back decompositions that do not extract a constant or merge terms.
    • Greedily anchors bounded clusters spanning a 256-byte chunk boundary or multiple basic blocks at each 256-byte window's minimum element offset using a canonical imadshl_agx expression so NIR CSE shares one address calculation across taps and folds per-tap deltas into LDTG/STTG immediate byte offsets.
  2. M5 (AGX3C, non-G18G) multi-bank texture fetch and sample coordinate staging cache (agx3_lower_texture_publications.c, agx3_pack_binary.c, agx3_staging.h, agx_compiler.h, M5/AGX3C-only excluding emit_agx3d):
    • Adds a 4-entry per-block multi-bank coordinate staging cache covering plain 2D texel fetches and relocatable 2D texture samples (agx3_m5_next_fetch_xy(), agx3_m5_next_sample_xy()), scanning forward past register-only ops and up to 3 intervening disjoint-banked plain 2D fetches/samples while stopping on any coordinate register overwrite, non-banked texture op, barrier, or memory op.
    • Reserves live cached bank halves in agx3_staging_plan_above_reserved() and agx3_emit_texture_fetch() so intervening texture ops allocate disjoint banks without evicting cached coordinates, while preserving exact hardware .coord_kill liveness and accumulating every consumer's scoreboard slot in staging_banks.

Tested

  • Exact-head unit tests on dd115067502d (base 7fe256e03606):
    • Clean GCC and Clang builds (-Dbuild-tests=true), 0 new warnings.
    • meson test --suite asahi --suite vulkan --suite poly: 247/247 PASS (0 fail), including agx_tests (1924 passed, 1 skipped NativeFMA.ExactDyadicOracle, 0 failed), covering SharedByteOffsets.AffineStridedCrossBlockCluster, SharedByteOffsets.AffineStridedGuards, SharedByteOffsets.NestedImulIorImadshlBounds, AGX3PackBinary.M5MultiBank*, AGX3PackBinary.M5SampleXY*, AGX3PackBinary.ComputeShadow2DArrayRegisterDescRetiresLiveSlot6OnAGX3C, and AGX3FetchGroups.*.
  • Exact-head hardware verification on J775c (Apple M5 Max, G17C B1):
    • Independent exact-head correctness and ABBA row passed with 0 regressions and 0 GPU faults (exact-head J775c result).
  • Preliminary prior-stack measurements on J714s (Apple M5 Pro G17S B1, 7.3.0-m5-pr67, different rc14a development stack — NOT exact-head dd115067502d):
    • Correctness: 157/157 targeted CTS (cts-rq, cts-sparse-ssbo-residency) PASS, GPUBench RT (rt.scene, checksum b541b83e) PASS, 0 GPU faults.
    • GTA V Enhanced (2560x1440, Very High RT, FSR Quality, 5-pass headless, prior rc14a stack): no regression across the 5 passes (median +0.33 FPS, pass deltas [+0.388, +0.328, -0.050, +2.247, -0.103], 0 GPU faults, 0 clamp seconds).
    • Offline shader-db (GTA V): FSR2 -83 native (-11.6%), FSR1 -80 native (-10.2%) across 5.44M thr/fr; -1,834 staging copies / -1,958 native across 104 shaders (Denoise1 -25%, Denoise2 -23%, Lighting -10, 0 regressions).

Risk

Two-way door confined to src/asahi/compiler/. Commit 1 changes shared-memory offset codegen (including bounded cross-block anchoring) on all AGX3B and AGX3C targets, including G18G and G17P; G13X is untouched. Commit 2 is gated to AGX3C excluding emit_agx3d, preamble, and ray-traversal shaders.

Not yet

  • Exact-head aurora-wip (7fe256e03606 vs dd115067502d) J714s (G17S) hardware A/B qualification row (paired bundle staged on Mini).
  • Second-generation exact-head hardware rows for shared commit 1: J700 (G17P) and J773g (G16G) (both requested in row-requests.tsv).

…-block clusters

Extend agx_nir_opt_shared_byte_offsets() to decompose affine shared-memory
element-index expressions (iadd, isub, ishl, imul/amul, imadshl_agx,
imsubshl_agx) into scaled terms and a modular constant offset, while
rolling back decompositions that do not extract a constant or merge terms
so fused multi-term address ops are never unfused.

For bounded clusters spanning a 256-byte chunk boundary or multiple blocks
(such as unrolled strided stencil taps around a nonzero base offset),
greedily anchor each 256-byte window at its minimum element offset using a
canonical imadshl_agx expression so NIR CSE shares one address calculation
across all taps and folds per-tap deltas into LDTG/STTG immediate byte
offsets.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Add a 4-entry per-block multi-bank coordinate staging cache for M5 (AGX3C,
non-G18G) plain 2D texel fetches and relocatable 2D texture samples:

- Add agx3_m5_next_fetch_xy() and agx3_m5_next_sample_xy() to scan forward
  within a basic block past register-only ops and up to 3 intervening
  disjoint-banked plain 2D fetches/samples (stopping on any coordinate
  register overwrite, non-banked texture op, barrier, or memory op).
- Reserve active cached bank halves in agx3_staging_plan_above_reserved()
  and agx3_emit_texture_fetch() so intervening ops allocate disjoint banks
  without evicting cached coordinates before their next consumer.
- Preserve exact hardware .coord_kill liveness (false while a verified
  later consumer remains in the block, true on the final consumer) and
  accumulate every consumer's scoreboard slot in staging_banks.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 2, 2026 08:19
@satchlj

satchlj commented Dec 2, 2026

Copy link
Copy Markdown
Member

Independent exact-head M5 Max (J775c, G17C B1) correctness + same-boot ABBA row at dd115067502d20aa226b45fc1b48dfa9f9362731 vs base 7fe256e036068b23aaa4e9ce21132ceddd06908c: PASS, 0 regressions, 0 GPU faults. (Covers unit tests, dEQP 2D texture, compute/render/RT benchmarks, and LLM inference; does not replace the staged J714s GTA V or peer-generation rows.)

Stack & build:

  • Linux 7.3.0-1-49.20261202-ARCH, GCC 16.1.1, Vulkan-only Asahi Borealis builds (-Dbuildtype=release -Db_ndebug=false -Dbuild-tests=true), switched via VK_DRIVER_FILES with isolated per-ICD shader cache directories (same boot, libvulkan_asahi.so sha256 0613ea83… base vs 030f4ef5… PR).
  • meson test --suite asahi --suite vulkan --suite poly on dd115067502d: 243/243 PASS (0 fail, including agx_tests).

Correctness (dd115067502d, MESA_SHADER_CACHE_DISABLE=1):

  • dEQP-VK.glsl.texture_functions.texelfetch.sampler2d_fixed_*: 3/3 PASS
  • dEQP-VK.texture.explicit_lod.2d.sizes.*: 288/288 PASS
  • dEQP-VK.texture.filtering.2d.formats.r8g8b8a8_unorm.*: 12/12 PASS (6 not supported, 0 failed)
  • llama.cpp (f0c9c91f) test-backend-ops (Vulkan0): FLASH_ATTN_EXT (hsk=128,hsv=128) 876/876, MUL_MAT q4_K 127/127, MUL_MAT q6_K 65/65
  • gpubench-rt-vk: rt.scene light CRC 20002598, medium CRC e91ea686 identical across all 4 ABBA passes
  • dmesg GPU fault / hang / quarantine delta: 0

Same-boot ABBA (base -> pr -> pr -> base):

  • gpubench-vk (--depths 4 --runs 5 --run-ms 400, lower is better):
    • compute.conv2d: 101.39 -> 101.30 µs (-0.09%, gpu_us -0.06%)
    • compute.edge: 60.61 -> 60.66 µs (+0.09%, gpu_us +0.10%)
    • compute.stereo: 736.65 -> 738.01 µs (+0.18%, gpu_us +0.01%)
    • compute.uber: 144.46 -> 144.74 µs (+0.20%, gpu_us +0.22%)
    • render.deferred: 45.81 -> 45.79 µs (-0.05%, gpu_us 0.00%)
    • render.postfx: 222.85 -> 221.61 µs (-0.56%, gpu_us +0.59%)
    • render.shadow: 107.84 -> 108.49 µs (+0.60%, gpu_us -0.05%)
    • mem.image: 110.44 -> 111.58 µs (+1.03%)
  • gpubench-rt-vk (trace=query, depth=2):
    • rt.scene light: 2,522.7 -> 2,527.6 fps (+0.20%, gpu_us 394.9 -> 394.2 µs)
    • rt.scene medium: 134.4 -> 134.2 fps (-0.11%, gpu_us 7,334.0 -> 7,336.6 µs)
  • llama-bench (master f0c9c91f, -fa 1):
    • Llama-2-7B Q4_0: pp512 3,535.15 -> 3,536.78 (+0.05%), tg128 121.56 -> 121.61 (+0.05%)
    • Qwen3.8-2B Q4_K_M: pp512 10,805.94 -> 10,803.51 (-0.02%), tg128 258.83 -> 256.76 (-0.80%; passes 257.48 / 260.18 vs 254.11 / 259.42, within run-to-run variance)

@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: FINISH, then merge at dd115067502d20aa226b45fc1b48dfa9f9362731
Blockers:

  1. Missing second-generation exact-head hardware row: commit 259474f2eb86 changes shared-memory offset codegen across both AGX3B and AGX3C targets, while the posted exact-head hardware row covers M5 Max (J775c, AGX3C) only. Once one AGX3B exact-head row (J700 G17P or J773g G16G, already queued in Not yet) passes and is posted, this review is ready to flip to APPROVE without a full re-review.

Checked: no Apple material, no inappropriate model gating, full diff of both commits (affine shared-offset decomposition and M5 multi-bank staging cache), unit tests, exact-head M5 Max hardware receipt

Evidence
  • Commit 1 (259474f2eb86, src/asahi/compiler/agx_nir_lower_shared_bitsize.c + test-shared-byte-offsets.cpp): verified gather_affine_sum() modular arithmetic and rollback on unmerged ops, analyze_elem_candidate() non-wrapping 16-bit bounds check (scale <= UINT16_MAX, ub <= UINT16_MAX, scale * ub <= UINT16_MAX, bound <= UINT16_MAX - elements), cross-block greedy min-anchor windowing (prefer_greedy), emit_affine_expr() / expr_matches_canonical() idempotency, and small_shared fallback.
  • Commit 2 (dd115067502d, agx3_lower_texture_publications.c, agx3_pack_binary.c, agx3_staging.h, test-agx3-pack-binary.cpp, test-agx3-uniform-alu.cpp):
    • Scoped to AGX_ISA_ENCODING_AGX3C && !emit_agx3d && !is_preamble && !has_ray_traversal on fragment/compute stages.
    • agx3_m5_next_fetch_xy() and agx3_m5_next_sample_xy() stop on any coordinate/packed-level destination overlap, non-plain fetch/sample, or non-preserves_publications() instruction, matching agx_foreach_instr_in_block() emission order.
    • Verified staged_sample already retires wait before the reuse_xy branch (assert(relocatable_coordinates && staged_sample && !wait)), agx3_staging_hold() still holds bank_base for every reusing slot, and stage_cache_halves excludes reserved banks in both agx3_allocate_staging() and agx3_emit_texture_fetch().
    • Non-blocking defensive nit: in agx3_emit_texture_fetch() (src/asahi/compiler/agx3_pack_binary.c), if !placed && !have_fallback (currently unreachable because ARRAY_SIZE(stage_cache) == 4 and plain_banks >= 6), returning false instead of falling through to staging_coord = fallback_coord would guard against overwriting a reserved bank if cache capacity increases later.
  • Exact-head M5 Max (J775c, G17C B1) verification at dd115067502d passed 243/243 unit tests, 2D texture dEQP, llama.cpp test-backend-ops, bit-exact gpubench-rt-vk CRCs, and same-boot ABBA with 0 GPU faults.

@DjDeveloperr

Copy link
Copy Markdown
Author

Exact-head M4 Mac mini (J773g, T8132, G16G B0) second-generation hardware verification at dd115067502d20aa226b45fc1b48dfa9f9362731 (tree d496c2b1dd90947236b4d8afcbba425e606548a6) vs base 7fe256e036068b23aaa4e9ce21132ceddd06908c: PASS, 0 status diffs, 0 invalid CRCs, 0 GPU faults.

  • Environment: Apple M4 Mac mini (J773g, T8132, G16G B0), stock kernel 7.3.0-ARCH-m4main-6c83a2a6 (boot_id=0592f16f-cada-4fd6-ba00-5568cf439988, aurora.fault_control=0, soft_faults=disabled), clean-builddir AArch64 release Borealis Vulkan ICDs (base sha256 4bee0a9296a4d559c7480991a36d80c7874f589696e2ed8261142a512edd879b, head sha256 d3e8a6dc099e2da7a6530de225cbdb998641352da5b33c483354dccb67a3b2e7), same-boot A/B.
  • Headless Vulkan shared-memory compute CTS (MESA_SHADER_CACHE_DISABLE=true, solo, 582 cases: compute.pipeline.basic, workgroup_memory_explicit_layout, zero_initialize_workgroup_memory, shader_object_{binary,spirv}.basic, all 210 memory_model.shared.* 8-bit/16-bit/arrays/structs/vectors, and all 85 spirv_assembly.instruction.compute.*workgroup*):
    • Base 7fe256e03606: 507 Passed / 0 Failed / 75 NotSupported / 0 Warnings.
    • Head dd115067502d: 507 Passed / 0 Failed / 75 NotSupported / 0 Warnings.
    • Per-case status diffs: 0/582.
  • gpubench-vk (7a0dfcf8, --verify, MESA_SHADER_CACHE_DISABLE=true, queue mode depths 1,4, 6/6 valid=true):
    • compute.heavy: d1 783.70 us (2740.19 GFLOP/s, crc=6d0d8609) -> 783.70 us (2740.19 GFLOP/s, crc=6d0d8609); d4 553.10 us (3882.65 GFLOP/s) -> 548.25 us (3917.01 GFLOP/s, crc=6d0d8609).
    • compute.light: d1 112.26 us -> 113.02 us (valid=true); d4 43.26 us -> 43.43 us (valid=true).
    • render.postfx: d1 791.14 us (1264 fps, crc=4707efa8) -> 793.65 us (1260 fps, crc=4707efa8); d4 632.91 us (1580 fps, crc=4707efa8) -> 637.76 us (1568 fps, crc=4707efa8).
  • GPU health / faults: health: failed 0 errno 0 degraded 0, recoveries 0, hang_kills 0, quarantined 0, 0 new GPU faults in dmesg.

@DjDeveloperr

Copy link
Copy Markdown
Author

J714s (M5 Pro, G17S B1) exact-head row at dd115067502d20aa226b45fc1b48dfa9f9362731: PASS

Base is the PR's merge base with aurora-wip, 7fe256e036068b23aaa4e9ce21132ceddd06908c.

MacBook Pro (M5 Pro, G17S B1), stock 7.3.0-m5-pr67, boot a7360295-c649-497d-8eaf-1fe72696a2a2, SLC on (16 planes). Both drivers were built natively on J714s from the exact commits and ran on the same boot, headless (no display), each selected per process via VK_DRIVER_FILES (verified live /proc mapping and gpubench git- stamp).

Driver Commit libvulkan_asahi.so SHA-256
base 7fe256e03606 2fb1769472edd3dfda94c1c8573d1b0699a1f4c090d2f89e68fea6279b4292cd
head dd115067502d 749a859dd877805a9a1d881f4f49602f77fad9aa582874698e49202261e203db
  • Vulkan CTS 1.4.7.1 cts-rq (107 cases): base 107 Pass; head 107 Pass (0 diffs).
  • Vulkan CTS 1.4.7.1 tiered shared-memory / texture list (20,000 cases): 20,000/20,000 ran on both drivers — base 15,188 Pass, 4,812 NotSupported; head 15,188 Pass, 4,812 NotSupported (0 diffs, 0 regressions).
  • GPUBench gpubench-vk suite (24 ABBA runs, 0 issues, median gpu_us):
    • compute.conv2d gaussian 1080p d1/d4: 193.4 -> 193.8 / 193.1 -> 193.5
    • compute.edge sobel 1080p d1/d4: 139.2 -> 140.8 / 138.6 -> 140.4
    • compute.heavy alu d1/d4: 269.9 -> 270.0 / 272.7 -> 273.4
    • compute.heavy bw d1/d4: 488.4 -> 488.8 / 484.0 -> 490.1
    • render.heavy fill 1080p d1/d4: 142.2 -> 142.2 / 123.8 -> 123.8
    • render.heavy texture 1080p d1/d4: 287.4 -> 287.4 / 268.5 -> 268.4
  • GPUBench gpubench-rt-vk rt.scene preset=medium (ABBA, depth 2, median of 2/2): Image CRC32 identical e91ea686 on base and head (all 4 rounds); fps: 72.88 -> 72.85, gpu_us: 13640 -> 13640.
  • GPU / kernel faults (base / head): 0 / 0. Clean stock PR67 handback (hw released ok).

Forward-merge aurora-wip at e86174f (through #1497).

test-shared-byte-offsets.cpp: both sides appended new tests at the
end of the file (AffineStridedCrossBlockCluster/AffineStridedGuards
here, the SharedIndexReassoc tests from #1497). Keep both, this
branch's tests first.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Author

Forward-merged aurora-wip into this branch. New head: 91a7b69947a0d34f5ccaaae593073cf8e7dc34e3

This is a merge commit (dd115067502d + aurora-wip e86174f60fbc), not a rebase or force-push.

Conflict resolved: there was one conflict, in src/asahi/compiler/test/test-shared-byte-offsets.cpp. This branch and #1497 had each added tests at the end of the file. I kept both sets, with this branch's tests first. agx_nir_lower_shared_bitsize.c, which #1497 also changed, merged without conflicts.

The PR's diff is unchanged. I compared git diff 7fe256e03606 dd115067502d (old base to old head) with git diff e86174f60fbc 91a7b69947a0 (new base to new head):

So the J714s exact-head row (#issuecomment-7344582749) carries over to this head, and so do the J775c and J773g rows at dd115067502d.

Interaction with #1497: on AGX3, #1497's new agx_nir_opt_shared_index_reassoc runs right after agx_nir_opt_shared_byte_offsets, the pass this PR extends. I wrote a throwaway test, not committed, that ran this PR's AffineStridedCrossBlockCluster shader through both passes in agx_compile.c order. The reassoc pass made no changes in either call, and the result was the same as with this PR alone:

  • store/load bases stay at 96/0/192,
  • all three accesses share one imadshl_agx,
  • byte addresses match the original shader for x in {0, 1, 7, 8, 128, 255}.

Local CI on the new head: run on pc, because hosted CI is disabled. The tree is 1fb8620b03b8, the commit aurora-wip would get from merging this head. I used the same runner as the #1494/#1497 landing receipts, aurora-mesa-ci:fedora44-amd64, with warm build dirs:

  • .github/ci/mesa-arm64.sh (build + full meson test): 389 Ok, 0 Fail, 1 Skipped
  • the same script with -march=x86-64-v3 -funsigned-char (build-a64emu): 389 Ok, 0 Fail, 1 Skipped
  • meson test -C build --suite asahi --suite vulkan --suite poly: 447 Ok, 0 Fail, 1 Skipped
  • this PR's gtests in agx_tests (SharedByteOffsets.*, SharedVec2.*, GemmMatch.*, AGX3PackBinary.*, AGX3UniformALU.*, AGX3FetchBanks.*, AGX3FetchGroups.*, AGX3ArrayStaging.* and the other AGX3 suites in the two touched test files): 610/610 passed in both build dirs

These counts match the aurora-wip e86174f60fbc tree. This PR adds gtest cases inside agx_tests but no new meson tests.

Parked for TAG CUT. Merge command: gh pr merge 1524 -R aurora-silicon/mesa --merge --match-head-commit 91a7b69947a0d34f5ccaaae593073cf8e7dc34e3

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

MERGE at 91a7b69947a0.

Forward-merge on aurora-wip e86174f6 verified clean:

  • agx_nir_lower_shared_bitsize.c preserves #1497's known_zero_bits_u32 alongside #1524's unchanged hunks across all 7 source files.
  • test-shared-byte-offsets.cpp keeps both #1497's SharedIndexReassocDisjointOrAndNestedImadshl and #1524's AffineStridedGuards tests cleanly.
  • Exact-head/source-identical hardware verification now covers M4 (J773g, G16G B0: 582 shared-memory CTS 0 diffs, gpubench-vk 6/6 valid=true, 0 faults) and M5 Pro (J714s, G17S B1: 107/107 cts-rq, 20,000 tiered CTS 0 diffs, 24 ABBA gpubench-vk, 0 faults), plus exact-head local CI (389 Ok / 0 Fail, 610/610 gtests) and green GitHub CI.

@DjDeveloperr
DjDeveloperr merged commit 77fd993 into aurora-wip Dec 2, 2026
@DjDeveloperr
DjDeveloperr deleted the m5/agx-shared-affine-and-stage-cache branch December 2, 2026 12:36
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