Skip to content

agx: put the inclusive ballot count's +1 at the count - #1549

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
fix/agx-inclusive-rank-cursor
Dec 2, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
fix/agx-inclusive-rank-cursor

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Dec 2, 2026 •

Copy link
Copy Markdown

What: Fixes a bug that #1540 added to the AGX compute ballot-count lowering. When ballot_bit_count_inclusive reuses the rank in front of its ballot, its +1 is emitted wherever the previous rewrite left the builder cursor. After a rewritten ballot(true) count inside an if, the +1 lands inside that if but is used after it, so the shader fails NIR validation when asserts are on and release builds compile the invalid SSA. This puts the +1 in front of the count it replaces and adds a regression test. Reported in #1540 (comment).

Tested: at c365c46d7d9f: J773g (M4 mini, G16G) and J700 (G17P) subgroup compute CTS A/B against base c94b25ce4e2f are both PASS with 0 new fails (3,776 cases each: 1,898 Pass and 1,878 NotSupported on both arms), 0 GPU faults or recoveries, receipts 7353790211 and 7353816751. Local CI: Meson asahi/poly/vulkan suites 454 OK, 0 fail, 1 skip; agx_tests 1,960 passed, 1 skipped; the repro shader now compiles on G14G, G16G and G17P; the GTA V shader-db is unchanged on G17P and G16G. Without the fix, the new test fails and the repro shader fails NIR validation on all three GPUs.

Risk: two-way · all Macs: compute shaders on every AGX generation that use subgroup ballot bit counts. The change only moves where one iadd is placed.

Not yet: rows on J414s (G14) and J714s (G17S). They don't gate the merge: two generations pass at this head, and the host test covers all three lowerings.

Details

active_rank_at_ballot() reuses the u2u32(load_active_subgroup_invocation_agx) that sits right before a ballot, and it only sets b->cursor when it has to insert a new rank. nir_shader_intrinsics_pass() does not reset the cursor between instructions. So on the reuse path the inclusive count's iadd went wherever the previous rewrite had left the cursor, in front of another ballot that can be inside an if. The +1 is now placed with b->cursor = nir_before_instr(&intr->instr). On the insert path it used to sit in front of the ballot and now sits in front of the count. Both positions dominate the use.

The exclusive count and the bit_count(ballot & lt_mask) form emit no instruction on the reuse path, so they were not affected.

New test AGX3VisionOpts.Wave8InclusiveCountReusedRankAfterIf, next to #1540's tests: an exclusive count of ballot A, then an exclusive count of ballot B inside an if, then an inclusive count of ballot A after the if. It runs the default, G17P and M5 subgroup lowering, checks that the stored value dominates its store (nir_validate_shader() is a no-op without asserts), validates the shader, and checks that there are two ranks, which shows the inclusive count took the reuse path.

The signed power-of-two idiv fold from #1540 is not touched here.

Evidence

Head c365c46d7d9fb2f336090e4c70b866440527fbb4, base aurora-wip at c94b25ce4e2fb5586e947554a694c48b2fc166d3 (the #1540 merge).

Local CI: x86-64 Fedora 44 container, debugoptimized with asserts, warm build directory and ccache.

meson setup --reconfigure build -Dgallium-drivers=asahi -Dvulkan-drivers=asahi \
  -Dplatforms=wayland,x11 -Dbuild-tests=true -Dtools=drm-shim
meson compile -C build; ninja -C build                    # second pass: nothing compiled or linked
ninja -C build <every test input from intro-tests.json>   # rebuilds the drm-shim test binaries
meson test -C build --suite asahi --suite poly --suite vulkan
build/src/asahi/compiler/agx_tests
base c94b25ce4e2f test only, no fix head c365c46d7d9f
Meson asahi/poly/vulkan 454 OK, 0 fail, 1 skip not run 454 OK, 0 fail, 1 skip
agx_tests 1,959 passed, 1 skipped new test fails for all three targets 1,960 passed, 1 skipped

Without the fix, the new test fails on each target (0 = default, 1 = G17P, 2 = M5 lowering):

[ RUN      ] AGX3VisionOpts.Wave8InclusiveCountReusedRankAfterIf
../src/asahi/compiler/test/test-agx3-vision-opts.cpp:2714: Failure
Value of: dominated
  Actual: false
Expected: true
Google Test trace:
../src/asahi/compiler/test/test-agx3-vision-opts.cpp:2678: 0
(the same failure for traces 1 and 2)
[  FAILED  ] AGX3VisionOpts.Wave8InclusiveCountReusedRankAfterIf (0 ms)

Repro shader through Borealis and the noop drm-shim (ASAHI_SHIM_CHIP=<chip> fossilize-replay --enable-pipeline-stats):

uvec4 m = subgroupBallot(true);
uint e = subgroupBallotExclusiveBitCount(m);
if ((gl_SubgroupInvocationID & 1u) != 0u) {
   uvec4 m2 = subgroupBallot(true);
   o[i + 64u] = subgroupBallotExclusiveBitCount(m2);
}
o[i] = e + 1000u * subgroupBallotInclusiveBitCount(m);
GPU base head
G14G NIR validation fails (nir_block_dominates, nir_validate.c:1879) compiles: 17 instructions, 7 GPRs, 1024 threads, 0 spills
G16G NIR validation fails compiles: 19 instructions, 7 GPRs, 1024 threads, 0 spills
G17P NIR validation fails compiles: 19 instructions, 7 GPRs, 1024 threads, 0 spills

GTA V shader-db, base to head (Steam Fossilize DB, fossilize-replay --master-process --num-threads 10 --enable-pipeline-stats, no crashed or hung pipelines in any run):

GPU Executables changed Instructions GPRs Threads Spills / fills
G17P 0 of 12,868 4,710,032, unchanged 882,865, unchanged unchanged 866 / 871, unchanged
G16G 0 of 12,868 4,841,221, unchanged 887,738, unchanged unchanged 747 / 792, unchanged

When ballot_bit_count_inclusive reuses the rank already in front of its
ballot, active_rank_at_ballot() leaves the builder cursor where the
previous rewrite put it, and the +1 is emitted there. If that rewrite
was a count of another ballot(true) inside an if, the iadd lands inside
the if while its use comes after it. Such shaders fail NIR validation in
builds with asserts, and release builds compile the invalid SSA.

Put the +1 in front of the count it replaces, and add a test of this
shape for the default, G17P and M5 subgroup lowering.

Fixes: e20bc72 ("agx: reuse adjacent rank reads for the same ballot")
Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 2, 2026 17:42

@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 at c365c46d7d9f (non-author, 12-01 bar).

active_rank_at_ballot() only moves b->cursor when inserting a new rank, so reusing a rank after an inner-branch rewrite left the inclusive-count +1 inside the preceding if block where it does not dominate the use. Setting b->cursor = nir_before_instr(&intr->instr) before nir_iadd_imm(b, active_id, 1) guarantees dominance on both reuse and insert paths. The new Wave8InclusiveCountReusedRankAfterIf test covers default, G17P, and M5 lowering with explicit dominance checks and nir_validate_shader().

Not merging from B2-MERGE while your hardware rows at c365c46d7d9f are pending.

@DjDeveloperr

Copy link
Copy Markdown
Author

J773g (M4 mini, T8132, G16G B0, OS FW 27.2 / 26B123) — PASS (exact-head A/B vs base c94b25ce4e2f)

Built both base (c94b25ce4e2fb5586e947554a694c48b2fc166d3) and exact PR head (c365c46d7d9fb2f336090e4c70b866440527fbb4) natively on J773g into side prefixes (VK_ICD_FILENAMES, system /usr/lib/libvulkan_asahi.so untouched, MESA_SHADER_CACHE_DISABLE=true).

  • Kernel: steady stock 7.3.0-ARCH-m4main-6c83a2a6, boot_id=54586579-9b48-493d-bd74-628ec82f4883 (unchanged across A/B)
  • Side ICD SHA-256:
    • Base c94b25ce4e2f: 3a3cdc17fe7bcc81d50ea2841c162034d299f4a36398cf478fd5c56f800f8c03
    • Head c365c46d7d9f: 47c4e92e39d3e79051aa67b8ea1729d4dba9995f0bedd75400745e5a91e51478
  • Compiler unit test (agx_tests at exact head c365c46d7d9f): 112/112 PASSED
  • Vulkan CTS subgroup compute A/B (deqp-vk, Apple M4 (G16G B0) Borealis 1.4.363):
Suite Base c94b25ce4e2f (Pass / Fail / NotSup) Head c365c46d7d9f (Pass / Fail / NotSup) New Fails
dEQP-VK.subgroups.ballot_other.compute.* 14 / 0 / 0 14 / 0 / 0 0
dEQP-VK.subgroups.ballot_broadcast.compute.* 264 / 0 / 1356 264 / 0 / 1356 0
dEQP-VK.subgroups.arithmetic.compute.* 1608 / 0 / 522 1608 / 0 / 522 0
dEQP-VK.subgroups.basic.compute.* 12 / 0 / 0 12 / 0 / 0 0
Total (3776 cases) 1898 / 0 / 1878 1898 / 0 / 1878 0
  • GPU & kernel health: 0 GPU recoveries, 0 new GPU kernel errors (dmesg clean), WARN_BUG_OOPS_SERROR=0.

@DjDeveloperr

Copy link
Copy Markdown
Author

J700 (MacBook Neo, A18 Pro, G17P B1): PASS. Exact-head A/B against base c94b25ce4e2f.

0 new fails, 0 GPU faults, and the kernel log stayed empty for the whole run.

  • Arms: base c94b25ce4e2fb5586e947554a694c48b2fc166d3 is aurora-wip with asahi: optimize integer and subgroup operations #1540 merged. It is this PR's merge base and its only parent. Head is c365c46d7d9fb2f336090e4c70b866440527fbb4. Both Borealis ICDs were cross-built warm for AArch64 with the same Meson options. They were loaded only through VK_DRIVER_FILES from a scratch directory, and the system ICD was not touched. Each arm loaded its own library and reported its own git- stamp on Apple A18 Pro (G17P B1).

    • libvulkan_asahi.so SHA-256, base: 9272024a3a0b89546b49bf67da29a4615dd361da33c3ac1abd588f23eb1616b1
    • head: 48ddf62019fc8576dd8b6a6c9eaad9aa2dfbea79d5a01816b7cfd4c4a2701003
  • Board: J700 on the stock Fedora 44 kernel 7.3.0-41.fc44.aurora.aarch64. Userspace only, in one boot (159dcbc0), with no reboot or install. SELinux was Enforcing. SLC was on: apple-mcc 220000000.memory-controller: system level cache allocation enabled on 4 planes.

  • CTS: vulkan-cts-1.4.7.1 (0d18b347, plus the Borealis driver-ID and teardown cherry-picks), deqp-vk SHA-256 d3ef839a…. Runs were serial with the shader cache off, base then head for each group. The case lists match this build's mustpass exactly.

    Group Cases Base: Pass / Not supported / Fail Head: Pass / Not supported / Fail New fails on head Fail on base, pass on head
    subgroups.ballot_other.compute.* 14 14 / 0 / 0 14 / 0 / 0 0 0
    subgroups.basic.compute.* 12 12 / 0 / 0 12 / 0 / 0 0 0
    subgroups.ballot_broadcast.compute.* 1,620 264 / 1,356 / 0 264 / 1,356 / 0 0 0
    subgroups.arithmetic.compute.* 2,130 1,608 / 522 / 0 1,608 / 522 / 0 0 0
    Total 3,776 1,898 / 1,878 / 0 1,898 / 1,878 / 0 0 0

    Every case had the same status on both arms. There were no crashes, timeouts or InternalErrors, and all 8 deqp-vk runs exited 0.

  • What this shows: the base passed every case too, including subgroupballotinclusivebitcount and its _requiredsubgroupsize variant. So on G17P these groups don't reach the shape asahi: optimize integer and subgroup operations #1540 miscompiled (a reused rank whose +1 lands at a stale cursor). That shape is covered by the PR's new unit test. This row shows the fix is safe on AGX3B for these groups. It is not a CTS reproduction of the bug.

  • Faults and kernel log: a scan after each step and a final scan found 0 GPU-side lines: no faults, timeouts, recoveries or lost devices. The kernel log over the run window had no new lines at all, so there were 0 WARN/BUG/Oops lines.

  • Stock handback: the boot ID, kernel, Mesa packages and library hashes, GRUB default entry and failed units (0) all matched before and after. One GRUB env flag changed, boot_success=0 to 1. Fedora's own grub-boot-success.timer set it at 12:53:49, two minutes after this boot started, while the row was running. I re-checked the board under the lease afterwards and it is in its stock state.

@DjDeveloperr
DjDeveloperr merged commit c3adfe7 into aurora-wip Dec 2, 2026
@DjDeveloperr
DjDeveloperr deleted the fix/agx-inclusive-rank-cursor branch December 2, 2026 18:00
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