Skip to content

agx: keep range bounds per implementation in two late passes - #1567

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
fix/agx-range-bounds-per-impl
Dec 3, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
fix/agx-range-bounds-per-impl

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Dec 3, 2026 •

Copy link
Copy Markdown

What: agx_nir_opt_bitfield_slices() and agx_nir_opt_redundant_shift_masks() each used one range table for the main shader and the preamble. nir_unsigned_upper_bound() caches by SSA index and every implementation numbers from zero, so a bound found in the main shader could answer for an unrelated value of the preamble and give a wrong result. Each implementation now starts with an empty table, as in agx_nir_lower_address() and agx_nir_lower_bitfield_u2f().

Examples on aurora-wip: (p.v[6] & 31u) of a push constant loses its mask in the G16G constant program because the main shader shifts by gl_LocalInvocationIndex & 15u; ushr(ishl(x, 12), 4) of an unbounded x becomes x << 8.

Tested: Local only at b7fe9e7c2486 (base 80a0ac5d0cdd), no Mac yet. Assert-enabled x86-64: asahi, poly and vulkan suites 465 ok, 0 failed, 1 skipped, before and after; agx_tests 1971 passed before, 1973 after (the two new tests), 1 skipped. Both new tests fail on the base.

Risk: two-way door · M1, M2, M4, A18 Pro, M5 (and M3 where the kernel lets a compute shader form a constant program). A shader can only change where the two tables disagree. Borealis forms no preamble on M6, so it is not affected there.

Not yet: hardware rows on M4 (J773g), A18 Pro (J700), M5 Pro (J714s) and M2 Pro (J414s): the self-checking shaders below, base and head, plus dEQP-VK.glsl.operator.binary_operator.* shifts and masks.

Details
Evidence

Compiled with each driver's noop shim. A probe build runs both tables side by side, compares every decision, and hashes every compiled binary; the arms differ only in which table is used.

set chip compiles changed binaries preamble decisions compared
GTA V pipelines G16G 86,829 0 7,650
GTA V pipelines G17P 86,874 0 7,947
GTA V pipelines G17S 32,343 0 2,823
GTA V pipelines G13S 64,712 0 6,155
GTA V pipelines G18G 11,734 0 none (no preamble)
llama.cpp MUL_MAT q4_K/q6_K G16G 38 0 0
llama.cpp MUL_MAT q4_K/q6_K G17P 39 0 0
llama.cpp MUL_MAT q4_K/q6_K G13S 31 0 0
llama.cpp backend op tests, as far as they run on the shim G16G / G17P / G17S 302 / 303 / 247 0 3 / 0 / 0
600 generated compute shaders (uniform field decode next to per-invocation shifts) G16G, G16S, G16C, G17P 600 each 97 (43 mask, 54 slice) all
same G17S, G17D 351 of 600 69 (15 mask, 54 slice) all
same G13G, G13S, G14G 600 each 51 (mask) all
same G18G, G15S 600 each 0 0

The unmodified base and head drivers agree as well: 0 of 26,447 GTA V executables change on G17S and 0 of 22,208 on G16G. No compiled GTA V or llama.cpp binary changes; the generated shaders show that ordinary code reaches both bugs. The other 249 of 600 do not compile on G17S and G17D, before or after this change, for another reason: the AGX3C packer refuses a right shift by a 16-bit count.

Failing on 80a0ac5d0cdd, passing at b7fe9e7c2486:

  • SharedByteOffsets.BitfieldSlicesRangesStayInTheirImpl (both the ishl and the imadshl_agx form): 0xffffff00 instead of 0x0fffff00, 0xfffffffc instead of 0x3ffffffc.
  • AGX3SubgroupShuffleXor.ShiftMaskRangesStayInTheirImpl: the preamble's iand(x, 31) is gone.

Smallest whole-pipeline example, G16G constant program before and after (NIR_DEBUG=print_cs):

%10 = imul %7, 0x9e3779b1                 %8 = iand %7, 0x1f
                                          %10 = imul %8, 0x9e3779b1

nir_unsigned_upper_bound() caches by SSA index, and every implementation
numbers its defs from zero. agx_nir_opt_bitfield_slices() used one table
for the whole shader, so a bound found in the main shader could be used
for an unrelated value of the preamble: ushr(ishl(x, a), c), and the
imadshl_agx form of the shift, lost the truncation of x << a, which gives
a wrong value.

Start every implementation with an empty table, like
agx_nir_lower_address() and agx_nir_lower_bitfield_u2f().

Fixes: 408af60 ("hk,agx: prove uniform compute loop bounds and fold AGX3 bitfield slices")
Signed-off-by: DjDeveloperr <hey@djdev.me>
agx_nir_opt_redundant_shift_masks() used one range table for the main
shader and the preamble, but nir_unsigned_upper_bound() caches by SSA index
and every implementation numbers its defs from zero. A bound found in the
main shader, such as that of a local invocation index, could answer for an
unrelated value of the preamble. Its & 15 or & 31 was then removed, whether
it masked a shift count or decoded a field of a uniform.

Give every implementation a table of its own, and declare the pass in
agx_nir.h so it can be tested with two implementations.

Fixes: fc22daf ("agx: fold constant-XOR shuffles, enable 16-bit shuffle_xor, and strip redundant late shift masks")
Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 3, 2026 02:15

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

Reviewed b7fe9e7. Both passes isolate range entries by implementation, so matching SSA indices in the main shader and preamble cannot share a cached bound. The reported two regressions fail against base, and the rest of the host suites retain their outcomes. Hardware coverage is pending.

@DjDeveloperr

Copy link
Copy Markdown
Author

J773g (Apple M4 Mac mini, Mac16,10, T8132, G16G B0 / AGX3B) zero-reboot validation at head b7fe9e7c248604e162c0ba4e2a08acbb05d19c31 (git-b7fe9e7c24) vs merge-base 80a0ac5d0cdd997706b16dabb0149c6eede32a95 (git-80a0ac5d0c), swapped via VK_DRIVER_FILES on stock 7.3.0-ARCH-m4main-6c83a2a6 (boot_id=bc9fa24e-37f4-4fa0-b91d-31ed138b3356, MESA_SHADER_CACHE_DISABLE=true, VK_LOADER_LAYERS_DISABLE='~implicit~').

Verdict: PASS.

  • base ICD SHA-256: 0979719ac761cfa33c97407abfd0b8286543e8a3cf1993a7917c0f34bd289565 (git-80a0ac5d0c)
  • head ICD SHA-256: c1be116eb12a17ac28971ea03847aef4b87153cc7695de66217c18c4ca2a3d22 (git-b7fe9e7c24)
  • Kit: rangecheck-kit.tar.zst (cdcc08d150d8754d1f4244b54e337e8e94e6f2e361a7c5a948d4315a04f022a9), caselist.txt (9365d76b4188af2fa82f4f8f81f69094d64148e280edc9a023b06720b02c7472, 384 cases).

1. rangecheck (600 self-checking compute shaders, base head head base)

  • Slot 1 (base): DEVICE Apple M4 (G16G B0) — TOTAL 600 WRONG 140 NOPIPE 0
  • Slot 2 (head): DEVICE Apple M4 (G16G B0) — TOTAL 600 WRONG 0 NOPIPE 0
  • Slot 3 (head): DEVICE Apple M4 (G16G B0) — TOTAL 600 WRONG 0 NOPIPE 0
  • Slot 4 (base): DEVICE Apple M4 (G16G B0) — TOTAL 600 WRONG 140 NOPIPE 0
  • diff slot 1 vs slot 4 (base): 0 lines (identical 140 wrong shaders both times). diff slot 2 vs slot 3 (head): 0 lines.

2. dEQP-VK CTS & Kernel Faults

  • base: 384/384 Passed, 0 Failed, 0 NotSupported, 0 Warnings, 0 Waived
  • head: 384/384 Passed, 0 Failed, 0 NotSupported, 0 Warnings, 0 Waived
  • Status diffs: 0. Kernel fault/warning delta: 0 (1 -> 1).

@DjDeveloperr

Copy link
Copy Markdown
Author

J700 (MacBook Neo, A18 Pro, G17P B1): PASS at b7fe9e7c2486

Stock Fedora 44 kernel 7.3.0-41 (feature word 0x0000007a2001fc06), zero reboot, userspace only. Base is the merge base 80a0ac5d0cdd. Both arms are cross-builds with identical options, loaded per process; nothing was installed. Shader cache off.

Run Shaders Wrong No pipeline
base (1st) 600 140 0
head (1st) 600 0 0
head (2nd) 600 0 0
base (2nd) 600 140 0
  • The row reaches the bug: the base got the same 140 of the 600 self-checking shaders wrong both times, and the head got all 600 right both times. That is the same base count that the J773g reports.
  • CTS: 384 cases on each arm (the recipe's list), 384 Pass on both, 0 differences.
  • Health: 0 GPU-side kernel lines, 0 WARN or BUG lines.

Not covered: no performance row (no GTA V or llama binary changes, per the recipe), and the run-time internal shaders.

@DjDeveloperr

Copy link
Copy Markdown
Author

J714s (M5 Pro, T6050, G17S B1, AGX3C, stock 7.3.0-m5-pr67, boot eca1eecc, SLC ON 16 planes, headless) #1567 b7fe9e7c248604e162c0ba4e2a08acbb05d19c31 vs base 80a0ac5d0cdd997706b16dabb0149c6eede32a95: PASS (rangecheck 600 shaders ABBA base/head/head/base: base TOTAL 600 WRONG 140 NOPIPE 0 twice 0 diffs, head TOTAL 600 WRONG 0 NOPIPE 0 twice 0 diffs; dEQP-VK 384-case caselist.txt 9365d76b4188: 384/384 Pass on both arms, 0 diffs; 0 new GPU/kernel faults; DSOs base 0979719ac761, head c1be116eb12a).

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