Repository navigation
asahi: optimize integer and subgroup operations - #1540
Conversation
1. AGX3B integer bit ops:
- Use immediate shift counts in agx_g17p_ushr_imm() instead of
materializing shift boundaries in registers.
- Replace the 16-op SWAR popcount sequence in agx_emit_g17p_ufind_msb()
with native agx_popcount_to() after bit smearing.
2. Subgroup lowering:
- On AGX3B (emulates_ufind_msb), lower first_invocation and vote_ieq
using popcount(first_bit - 1) on the single-bit first-active ballot
instead of emitting emulated ufind_msb.
- In compute shaders (no helper invocations), lower ballot_bit_count
exclusive/inclusive of ballot(true) and popcount(ballot(true) &
subgroup_lt_mask) directly to active_subgroup_invocation_agx (+1 for
inclusive).
3. Algebraic late pass:
- Fold nir_opt_idiv_const's 9-op signed power-of-two idiv pattern
imul(clamp(a, -1, 1), ushr(iabs(a), k)) into the bit-exact 3/4-op
sign-bias arithmetic shift (a + bias) >> k for k in 1..30 (exact for
all int32_t including INT32_MIN).
Signed-off-by: Diljit Singh <hey@djdev.me>
(cherry picked from commit 382d594)
Match the actual low ballot word and retain scalar component indices when matching the mask. Read the active rank where the ballot was taken so a later divergent consumer cannot change the result. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org> (cherry picked from commit 5f925b0)
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
There was a problem hiding this comment.
Verdict: MERGE at e20bc72
Checked: an exact-head x86-64 assert-enabled build with drm-shim, asahi Gallium + Vulkan, and meson test --suite asahi --suite poly gave 393 OK, 0 fail, 1 skip. Mesa has no Actions CI, so this local run is the CI record. Code points:
fold_signed_pow2_idiv:(a + (ishr(a,31) >>> (32-k))) >> k(k=1:a + (a >>> 31)) is round-toward-zero division by 2^k. I checked it againstimul(isign(a), ushr(iabs(a), k))at INT32_MIN: iabs wraps to INT32_MIN, soushrgives 2^(31-k) and the sign makes it -2^(31-k), which matches the fold. It also holds at -1, 0 and positive inputs. k=31 is not matched, so it is left alone. Both isign spellings (imin∘imax, imax∘imin) are covered.- G17P
ufind_msb: the smeared value is 2^(n+1)-1, so popcount-1 = n, and 0 gives -1. Shifts are now plain immediates 1..31 (asserted). first_invocation:ballot(active_id==0)is a single set bit, sobit_count(x-1)is its index.vote_ieqnow compares against the first active lane instead of the highest one. Any active lane works for an all-equal vote.ballot_bit_count_{ex,in}clusive(ballot(true))andbit_count(ballot(true) & lt_mask)become the active rank only in compute stages (no helper lanes). The rank is materialized immediately before the ballot, or reused only when rank -> u2u32 -> ballot are adjacent, so a consumer inside narrower divergent flow still sees the ballot's mask. Non-zero high ballot words are rejected unless they come from the same ballot.
Follow-ups: none blocking.
|
Tested exact head
|
satchlj
left a comment
There was a problem hiding this comment.
Reviewed e20bc7252c2e (asahi: optimize integer and subgroup operations):
- AGX3B integer & subgroup ops (
agx_compile.c,agx_nir_lower_subgroups.c):agx_g17p_ushr_imm()uses immediate shifts (1..31),agx_emit_g17p_ufind_msb()replaces SWAR popcount with nativeagx_popcount_to(), andemulates_ufind_msbcleanly lowersread_first_invocation/vote_ieqviabit_count(first_bit - 1). - Active-rank lowering (
5959c022a7cc,e20bc7252c2e): Both earlier concerns from#801are cleanly addressed —ballot_true_low_word()enforcess.comp == 0whileopt_compute_ballot_bit_count()checks upper words,active_rank_at_ballot()placesload_active_subgroup_invocation_agxright before the producerballot(preserving its execution mask under divergent consumers) and only reuses an immediately adjacentu2u32(load_active_subgroup_invocation_agx), and the rewrite stays compute-only. - Signed pow2
idiv(agx_nir_algebraic.py):(a + bias) >> kfork in 1..30is bit-exact across allint32_tincludingINT32_MIN(withk = 31excluded and covered by a negative test). - Hygiene & verification: All 3 commits carry DCO (preserving Dj's authorship and
Signed-off-by:),## Riskis present, and exact-head verification landed across three generations (J615 M3 Air, J700 G17P-42.57%divergent first/vote, and J775c M5 MaxG17C B1343/343Meson,81/81targetedagx_tests, Vulkan subgroup/div CTS + RT + ABBA parity, 0 GPU faults).
LGTM.
|
Post-merge check of Bug: 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);Through Borealis and the noop drm-shim (assert-enabled build), base Smallest fix, in if (intr->intrinsic == nir_intrinsic_ballot_bit_count_inclusive) {
b->cursor = nir_before_instr(&intr->instr);
active_id = nir_iadd_imm(b, active_id, 1);
}With it, the shader compiles on all three with fewer instructions than base (G14G 23 → 17, G16G/G17P 29 → 19). A unit test of the same shape passes for the default and G17P targets, and The signed pow2 idiv fold never fires in a real compile. Checked and fine: immediate SRL plus native popcount for G17P Local CI on pc (x86-64 Fedora 44 container, debugoptimized with asserts, warm build dir, base
Shader-db, base → head. GTA V Fossilize DB replayed with
The occupancy losses are compute pipelines |
What
Carry #801 forward onto current aurora-wip. The three commits preserve Dj’s original integer, subgroup and signed division work and sign-off, keep the ballot’s execution mask at its producer, and avoid rereading its rank when the immediately preceding conversion already has it. The target-option reconciliation keeps the newer relative shuffles and late algebraic passes.
Tested
The exact head
e20bc7252c2epassed 1,959 compiler tests with one skip, including 56 focused subgroup and integer checks. The AArch64 Vulkan ICD is byte-identical to the testedb787d0692cbc6ICD. The only source change after the lab runs removed one trailing blank line in the host test source. Baseline6f4be3f6537dhas the same tree as aurora-wipfb0f929ad544.J615 M3 Air (G15G, saved
7.3.0-aurora-edge) exercised divergent consumers of a ballot with three producer masks. MacBook Neo J700 (G17P, saved7.3.0-aurora-fleet94) exercised unsigned highest-bit lookup, signed division by 2/8/32,768, and first-invocation/vote under odd-lane divergence. Each process matched 524,288 CPU reference outputs and preserved 524,288 padding words. Both boots stayed Enforcing with zero failed units or kernel entries.Each arm used its own
VK_DRIVER_FILESmanifest andMESA_SHADER_CACHE_DISABLE=1. Four alternating ABBA and BAAB blocks per row, 20 GPU timestamp samples per process after four warmups. Commands were./ballot-aarch64 1 3,1 5, and1 7on J615, and./integer-aarch64 1 2,1 3, and1 4on Neo.The J615 ballot cases improved in all 12 blocks. Neo signed-division blocks ranged −0.26% to +0.32%. On G17P, the emitted shader uses native popcount where the baseline used the SWAR sequence. On J615, the ballot shader dropped from 436 to 390 bytes.
Risk
Two-way · Asahi compute shaders and G17P subgroup lowering. The rank reuse stays adjacent to the ballot producer and preserves its mask under divergence.
J615 ballot shader and individual GPU samples
Neo integer and subgroup shader and individual GPU samples