Repository navigation
agx: optimize AGX3B ufind_msb/subgroup lane ops and signed pow2 idiv - #801
DjDeveloperr wants to merge 2 commits into
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>
RyanTheTide
left a comment
There was a problem hiding this comment.
CHANGES · source review of 382d594c3af6
The active-lane rank rewrites in agx_nir_lower_subgroups.c replace a previously computed ballot(true) with an active-lane read emitted at the consumer. The matcher follows SSA through control flow without proving the execution mask is the same. A ballot taken before a divergent branch still includes lanes from outside that branch. Computing the rank in the branch changes the result. Emit the rank under the ballot's execution mask or restrict the rewrite to a region where the mask is proven unchanged. Please add a divergent consumer regression for both the intrinsic and the lowered bit-count pattern.
Verification here was by source inspection.
M4 Hardware Verification (
|
G17S B1 (
|
satchlj
left a comment
There was a problem hiding this comment.
Verdict: FINISH, then merge
src/asahi/compiler/agx_nir_lower_subgroups.c:is_ballot_true_def()(L95-114) resolves component0ofdefvianir_scalar s = nir_scalar_resolved(def, 0)and checks thats.defisnir_intrinsic_ballot(true), but never checkss.comp == 0. Becauseopt_compute_ballot_bit_count()runs beforenir_lower_subgroups()whennir_intrinsic_ballotis still a 4-componentuvec4(ballot.x= lanes 0..31,ballot.y/z/w= 0), a compute shader passing a swizzled upper ballot component such assubgroupBallotExclusiveBitCount(uvec4(b.y, 0u, 0u, 0u))(whereb = subgroupBallot(true)) resolves tos = { .def = b, .comp = 1 }, passesis_ballot_true_def(), and gets miscompiled toload_active_subgroup_invocation_agx(0..31) instead of0(similarly1..32instead of0forballot_bit_count_inclusive). In addition,opt_compute_ballot_lt_mask_count()(L189-192) andis_subgroup_lt_mask()(L149,L164) pass.definstead of the resolvednir_scalarintois_ballot_true_def(),is_subgroup_lt_mask(), andis_subgroup_invocation_mod32(), dropping.compwhenever an ALU operand extracts a non-zero component of a vector. Requires.comp == 0inis_ballot_true_def()and propagatenir_scalar(preserving.comp) acrossis_ballot_true_*,is_subgroup_lt_mask, andis_subgroup_invocation_mod32, with a negative unit test foruvec4(b.y, 0, 0, 0).
Checked: Code review of src/asahi/compiler/agx_compile.c, src/asahi/compiler/agx_nir_algebraic.py, src/asahi/compiler/agx_nir_lower_subgroups.c, and src/asahi/compiler/test/test-agx3-vision-opts.cpp at 382d594c3af6.
Evidence
agx_g17p_ushr_imm(),agx_emit_g17p_ufind_msb(),fold_signed_pow2_idiv(k in 1..30, bit-exact includingINT32_MIN), and theemulates_ufind_msbfirst_invocation/vote_ieq/vote_feqlowerings look sound.- In
src/asahi/compiler/agx_nir_lower_subgroups.c:97-113,is_ballot_true_def()only validatesballot->intrinsic == nir_intrinsic_ballotand constanttruepredicate ons.def, omittings.comp == 0;opt_compute_ballot_bit_count()executes atL511beforenir_lower_subgroups()(L515), whileballotis stilluvec4. - In
src/asahi/compiler/agx_nir_lower_subgroups.c:189-192,opt_compute_ballot_lt_mask_count()obtainsnir_scalar aandnir_scalar b_svianir_scalar_chase_alu_src()but passesa.defandb_s.defto helpers that always callnir_scalar_resolved(def, 0).
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>
|
I pushed 5f925b0 on top of your branch. The rank lowering matches the low ballot word and retains the selected component through the mask matcher. It reads the active rank at the ballot producer so a later divergent consumer uses the original mask. The regression covers inclusive, exclusive, and already-lowered counts, including an upper-word swizzle. All 216 Clang host tests passed. The parent stack, exact-head CI and hardware evidence still gate landing. |
There was a problem hiding this comment.
Verdict: MERGE at 5f925b0 after existing gates
The matcher retains the resolved component and only admits the low ballot word. Rank is computed under the original ballot mask, so a divergent consumer no longer changes it. The fixture covers inclusive, exclusive and lowered forms plus upper-word rejection. Keep #762 ordering, exact-head CI and hardware.
The matcher now carries the resolved component, rejects the upper ballot word, and computes rank at the producer. Negative and divergent-consumer tests passed. 5f925b0dc132
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
|
Thanks, Dj. I carried your integer, subgroup and division changes into #1540 on current aurora-wip with your sign-off preserved. I kept active rank at the ballot producer and reused the adjacent read where the same producer permits it. The live J615 cases improved 1.11–1.39%. On Neo, the highest-bit and divergent first/vote cases improved and signed division was at parity. Both machines matched all CPU readbacks. The paired results and raw GPU samples are in #1540. I’ll keep this original open until the replacement lands. |
|
Thanks, Dj. The integer, subgroup and signed-division work landed in #1540 as |
Stacked on #762 (
8f7cee6d4c11,perf/agx3b-interior-bounds-stencil-peel).Summary
src/asahi/compiler/agx_compile.c):agx_g17p_ushr_imm()instead of materializing shift boundaries in registers.agx_emit_g17p_ufind_msb()with nativeagx_popcount_to()after bit smearing.src/asahi/compiler/agx_nir_lower_subgroups.c):emulates_ufind_msb), lowerfirst_invocationandvote_iequsingpopcount(first_bit - 1)on the single-bit first-active ballot instead of invoking emulatedufind_msb.ballot_bit_count_exclusive/inclusive(ballot(true))andpopcount(ballot(true) & subgroup_lt_mask)directly toactive_subgroup_invocation_agx(+ 1for inclusive).src/asahi/compiler/agx_nir_algebraic.py):nir_opt_idiv_const's 9-opimul(clamp(a, -1, 1), ushr(iabs(a), k))sequence into the bit-exact 3/4-op sign-bias arithmetic shift(a + bias) >> kforkin1..30(exact for allint32_t, includingINT32_MIN).Testing & Verification
fedora-mesaci(382d594c3af68e9e87fc428a28c6f6df5b8ec9a2):146/0 PASS(wall 87 s, clang Fedora 44,-Dbuild-tests=true), including new unit testsAGX3VisionOpts.Wave8Agx3bSubgroupBallotLtMaskAndFirstInvandAGX3VisionOpts.Wave8FoldSignedPow2Idiv.-Db_ndebug=false, vs#762R38f7cee6d4c11):G15G17210 -> 16715(-495,-2.88%)165394 -> 160734(-4660,-2.82%)33 / 00G16G17210 -> 16715(-495,-2.88%)165394 -> 160734(-4660,-2.82%)33 / 00G17P17389 -> 16928(-461,-2.65%)178250 -> 173736(-4514,-2.53%)33 / 00G17S(M5)14411 -> 14411(+0,+0.00%)136814 -> 136814(+0,+0.00%)0 / 00Key
G17Pper-shader reductions:24eb36d4(-44i / -538B),323926dd(-51i / -4g / -448B),8b798f62(-41i / -10g / -494B),9020690a/a13778e1(-44i / -400Beach),e8b3e690(-40i / -364B),50e0fed8(-17i / -176B).G17P) hardware ABBA + CTS (combined Wave 8becdef64vs#762R38f7cee6d, ``):35.61 -> 34.01 ms(1.047x,24eb36d41.94 -> 1.67 ms/+16.2%); Horizon Detection26.71 -> 26.55 ms(50e0fed84.74 -> 4.58 ms); 8/8 Geekbench Vulkan workloads faster, 0 regressions.gpubench-vk100%CRC_OK; bounded Vulkan CTS1155/1155PASS (glsl_int_bitops288/288,subgroups_compute102/102,glsl_loops624/624,ssbo_std4304/4,compute_basic79/79),FAULTS_DELTA=0,AVC_DELTA=0.