Skip to content

asahi: optimize integer and subgroup operations - #1540

Merged
RyanTheTide merged 3 commits into
aurora-wipfrom
perf/closeout-801-integer-subgroups
Dec 2, 2026
Merged

RyanTheTide merged 3 commits into
aurora-wipfrom
perf/closeout-801-integer-subgroups

Conversation

@RyanTheTide

Copy link
Copy Markdown

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 e20bc7252c2e passed 1,959 compiler tests with one skip, including 56 focused subgroup and integer checks. The AArch64 Vulkan ICD is byte-identical to the tested b787d0692cbc6 ICD. The only source change after the lab runs removed one trailing blank line in the host test source. Baseline 6f4be3f6537d has the same tree as aurora-wip fb0f929ad544.

J615 M3 Air (G15G, saved 7.3.0-aurora-edge) exercised divergent consumers of a ballot with three producer masks. MacBook Neo J700 (G17P, saved 7.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_FILES manifest and MESA_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, and 1 7 on J615, and ./integer-aarch64 1 2, 1 3, and 1 4 on Neo.

Mac and case Base pooled median (ns) Head pooled median (ns) Median paired change
J615 ballot divisor 3 32,958 32,500 −1.11%
J615 ballot divisor 5 32,958 32,500 −1.39%
J615 ballot divisor 7 32,958 32,459 −1.39%
Neo highest bit 40,688 39,417 −3.35%
Neo signed division 39,583 39,584 +0.00%
Neo divergent first/vote 71,916 41,333 −42.57%

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
#version 450
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_ballot : require
layout(local_size_x = 64) in;
layout(push_constant) uniform Params { int rows; int start; int end; };
layout(set = 0, binding = 2, std430) writeonly buffer Output { uint dst[]; };

void main()
{
    uint lane = gl_SubgroupInvocationID;
    uint divisor = uint(end - start);
    uint result = 0u;
    if (lane % divisor != 0u) {
        uvec4 mask = subgroupBallot(true);
        if ((lane & 1u) != 0u) {
            uint exclusive = subgroupBallotExclusiveBitCount(mask);
            uint inclusive = subgroupBallotInclusiveBitCount(mask);
            uint lower = bitCount(mask.x & gl_SubgroupLtMask.x);
            uint upper = subgroupBallotExclusiveBitCount(uvec4(mask.y, 0, 0, 0));
            result = 1u | (exclusive << 1) | (inclusive << 7) |
                     (lower << 13) | (upper << 19);
        }
    }
    dst[gl_GlobalInvocationID.x] = result;
}
shape	process	arm	samples (ns)
1x3	1	base	32959,32958,32792,33000,32875,32958,32833,32833,32958,32750,32834,32750,32750,32750,32750,32791,32750,32750,32791,32750
1x3	2	head	32917,32583,32625,32583,32584,32625,32542,32584,32542,32542,32625,32625,32583,32542,32542,32625,32542,32584,32625,32708
1x3	3	head	32750,32750,32667,32542,32667,32500,32750,32708,32666,32500,32500,32542,32583,32542,32625,32583,32541,32500,32542,32542
1x3	4	base	33042,32916,32875,32958,32916,32834,32916,32791,32875,32834,32959,32834,32833,32792,33000,32833,32958,32792,32917,32792
1x3	5	head	32667,32542,32583,32459,32541,32459,32500,32500,32542,32500,32541,32458,32500,32500,32583,32500,32500,32541,32500,32458
1x3	6	base	33083,32958,33125,32709,32875,32791,32833,32750,32667,32792,32834,32708,32750,32875,32834,32750,32792,32709,32792,32750
1x3	7	base	33083,33084,32958,32875,32958,32916,33042,32916,32958,32875,33042,33042,32959,32875,33000,32916,32917,32916,32917,33000
1x3	8	head	32625,32667,32541,32417,32417,32542,32459,32375,32417,32417,32500,32333,32500,32417,32458,32333,32459,32333,32667,32416
1x3	9	base	33125,33084,33000,33000,33000,32917,32916,32958,32875,32958,32917,32917,33000,32958,32917,32916,32916,32959,32916,32917
1x3	10	head	32875,32708,32709,32875,32709,32833,32625,32625,32792,32750,32667,32625,32708,32875,32708,32583,32625,32667,32750,32625
1x3	11	head	32584,32583,32500,32375,32459,32333,32417,32375,32334,32333,32333,32333,32333,32375,32334,32333,32375,32458,32333,32500
1x3	12	base	33042,33000,32875,32958,32958,33041,32916,32875,32958,32875,32916,32916,32917,32834,33000,32875,32875,33041,32959,32834
1x3	13	head	32583,32375,32375,32417,32459,32542,32375,32584,32417,32375,32459,32333,32417,32333,32375,32375,32459,32333,32417,32333
1x3	14	base	33291,33166,33000,33042,33125,33125,33084,33083,33042,33083,33041,33042,33125,33042,33041,33084,33042,33042,33125,33000
1x3	15	base	33166,33042,33083,32959,33125,33042,33000,33042,33125,33209,33042,33083,33041,33042,32959,32958,33000,33167,33000,33042
1x3	16	head	32583,32458,32417,32458,32459,32500,32417,32292,32417,32375,32417,32375,32416,32292,32458,32334,32417,32292,32458,32375
1x5	1	base	33083,33042,32917,32917,32917,33000,32958,33042,32958,32958,32959,32875,32916,32917,32917,32916,32917,32959,32917,32959
1x5	2	head	32708,32792,32541,32708,32625,32583,32583,32625,32583,32583,32625,32542,32542,32500,32625,32542,32583,32583,32625,32500
1x5	3	head	32583,32417,32500,32458,32459,32458,32459,32459,32334,32458,32292,32417,32333,32333,32333,32334,32333,32375,32375,32292
1x5	4	base	33125,32917,32958,32958,32916,32875,32958,32959,33083,32958,32958,32958,32916,32917,32875,33000,32958,33042,32917,33000
1x5	5	head	32584,32458,32458,32542,32417,32417,32500,32375,32416,32375,32417,32375,32458,32542,32750,32375,32500,32417,32459,32333
1x5	6	base	33209,33167,33000,32959,33084,32875,33000,33000,33083,32958,32959,32916,33083,33000,33042,33083,33208,32875,33083,32959
1x5	7	base	33042,32917,32875,32916,32792,32833,32792,32792,32875,32834,32834,32750,32791,32791,32792,32792,32750,32792,32792,32750
1x5	8	head	32666,32667,32541,32459,32500,32500,32458,32541,32667,32458,32542,32458,32584,32458,32459,32542,32500,32500,32500,32459
1x5	9	base	33125,32917,32875,32750,32833,32833,32875,32833,32917,32875,32958,32833,32916,32833,32833,32791,32958,32917,32958,32834
1x5	10	head	32667,32500,32542,32583,32708,32500,32583,32541,32458,32584,32542,32584,32500,32542,32542,32541,32500,32500,32500,32833
1x5	11	head	32500,32750,32375,32333,32500,32416,32500,32416,32542,32458,32417,32375,32333,32375,32292,32375,32375,32292,32292,32333
1x5	12	base	33209,33000,32959,33042,32917,32834,32917,32958,32916,32958,32875,32917,32916,32917,33042,33125,32917,32958,32875,33125
1x5	13	head	32709,32500,32500,32541,32625,32500,32542,32542,32500,32500,32542,32500,32583,32583,32500,32583,32542,32459,32541,32500
1x5	14	base	33208,33000,33042,33167,33208,33208,33209,33167,33084,33000,33041,33000,33041,33083,33000,33125,33083,33000,33000,33000
1x5	15	base	33083,32917,32917,33083,33125,32917,32958,32875,32916,33041,33000,32958,32916,32958,32917,32875,32833,33000,32959,32959
1x5	16	head	32625,32500,32583,32500,32417,32375,32458,32417,32416,32375,32375,32500,32333,32333,32500,32417,32417,32334,32458,32417
1x7	1	base	33042,32833,32875,32875,33000,32916,32875,32875,32875,33042,33000,32875,32917,32833,32875,32875,32916,32875,32917,32875
1x7	2	head	33042,32750,32667,32625,32708,32583,32583,32500,32500,32583,32584,32458,32625,32542,32458,32500,32542,32584,19666,19583
1x7	3	head	32750,32625,32417,32375,32417,32333,32417,32500,32334,32375,32417,32583,32375,32458,32541,32416,32500,32333,32375,32375
1x7	4	base	33208,32958,33000,32875,32958,32875,32959,33041,32917,32875,32916,32916,32916,32958,33000,32917,32959,32917,32917,33041
1x7	5	head	32583,32500,32500,32333,32458,32292,32416,32334,32250,32417,32333,32334,32334,32333,32333,32333,32334,32375,32333,32291
1x7	6	base	33166,33083,33125,32958,32958,33042,32917,32875,33042,32958,32917,32834,32958,32916,32959,32917,32958,32959,33000,32958
1x7	7	base	33167,33083,33083,33042,33083,33000,33000,32959,33041,33000,33083,33000,33000,33042,33041,32958,33000,33083,33042,33042
1x7	8	head	32584,32500,32500,32333,32458,32375,32333,32250,32458,32334,32292,32333,32375,32333,32375,32375,32375,32333,32291,32459
1x7	9	base	33084,32958,32958,32959,32958,32958,33042,32916,32959,32917,32958,32958,32917,32917,32959,32833,32917,32917,32875,19833
1x7	10	head	32667,32583,32459,32500,32375,32333,32334,32458,32334,32333,32375,32333,32333,32334,32334,32375,32333,32334,32334,32334
1x7	11	head	32709,32625,32542,32625,32708,32584,32542,32750,32541,32750,32708,32541,32542,32584,32542,32500,32666,32625,32625,32584
1x7	12	base	33125,32917,32959,32875,32958,32958,33042,32834,32916,32875,33000,32875,32959,33000,32875,32875,33000,32875,32917,32917
1x7	13	head	32708,32417,32666,32500,32334,32500,32459,32459,32375,32584,32417,32459,32458,32542,32375,32417,32500,32625,32375,32667
1x7	14	base	33167,33083,33041,33084,33042,33000,33083,33041,33041,33166,33042,33000,33084,33000,33083,33041,33041,33042,33000,33125
1x7	15	base	33125,33208,32875,32833,32791,32750,32750,32792,32791,32750,32792,32792,32750,32792,32833,32750,32792,32750,32792,32834
1x7	16	head	32667,32500,32500,32625,32625,32500,32541,32458,32458,32541,32500,32500,32500,32500,32541,32542,32542,32542,32459,32500
Neo integer and subgroup shader and individual GPU samples
#version 450
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_ballot : require
#extension GL_KHR_shader_subgroup_vote : require
layout(local_size_x = 64) in;
layout(push_constant) uniform Params { int unused; int start; int end; };
layout(set = 0, binding = 1, std430) readonly buffer Input { uint src[]; };
layout(set = 0, binding = 2, std430) writeonly buffer Output { uint dst[]; };

void main()
{
    uint i = gl_GlobalInvocationID.x;
    uint value = src[i];
    uint answer = 0u;
    int mode = end - start;
    if (mode == 2) {
        answer = uint(findMSB(value));
    } else if (mode == 3) {
        int x = int(value);
        answer = uint(x / 2) ^ (uint(x / 8) * 17u) ^ (uint(x / 32768) * 257u);
    } else {
        uint lane = gl_SubgroupInvocationID;
        if ((lane & 1u) != 0u) {
            answer = subgroupBroadcastFirst(lane);
            answer |= subgroupAllEqual(value & 1u) ? 32u : 0u;
            answer |= subgroupAllEqual(lane) ? 64u : 0u;
        }
    }
    dst[i] = answer;
}
shape	process	arm	samples (ns)
1x2	1	base	40333,40833,40375,41125,40125,40584,40125,40916,39667,40375,40584,40125,40458,40292,40292,40041,40416,40792,40709,40167
1x2	2	head	38958,39625,39750,40083,39208,39625,39625,39125,39292,39666,39791,39667,39125,38834,39791,39334,39250,39583,39625,39834
1x2	3	head	39166,40375,39541,39625,39584,39625,39208,39250,39500,39458,39500,39708,39792,40125,39542,39250,39708,39791,39458,39458
1x2	4	base	41125,41917,40709,40709,40750,41334,40541,40416,40292,40916,40459,41125,40333,40541,40375,41167,40833,41000,40167,40542
1x2	5	head	39333,40041,39292,39292,39917,39458,39167,39459,39125,39292,39791,39667,39167,39792,38958,39625,39125,39666,39583,39334
1x2	6	base	40500,41583,41417,40792,40125,40667,40584,40667,40291,40917,41083,41083,40375,40500,40583,40708,40166,40083,40041,40958
1x2	7	base	41000,41041,41125,41333,41083,41125,40417,40750,40209,40666,40667,41375,41083,40625,41000,40334,40791,41375,41083,41375
1x2	8	head	39417,39542,39208,39959,39208,39167,38750,39167,39209,39333,39917,39291,38958,39083,38833,38750,38417,39708,39375,39125
1x2	9	base	40875,40875,41416,40833,40500,40333,40583,41000,41000,41334,40875,41000,40875,40792,40875,40500,40583,40958,40417,41167
1x2	10	head	39292,40000,39417,39417,38916,39291,39042,38917,38875,39625,39042,39208,39875,39459,39083,38917,39167,39625,39000,39500
1x2	11	head	39167,39958,39708,39334,39000,39791,39583,39459,39292,39833,39375,39625,39417,39500,39083,39000,39000,39625,39292,39334
1x2	12	base	40125,41000,40542,40125,40625,40334,41000,40792,40959,41292,41000,41084,40209,40292,40584,40625,40208,40667,40542,41000
1x2	13	head	39209,39709,39125,39500,39167,39167,40291,39958,39708,39625,39250,39833,39416,40084,38958,39209,39375,39750,39917,39416
1x2	14	base	40583,41541,40541,40458,40167,41417,40875,40083,40250,40875,40750,40959,40292,40541,40417,40625,41292,41625,41083,40458
1x2	15	base	40875,41334,41209,40625,40875,40791,40417,40542,40875,41000,41458,40708,39958,40375,40250,40541,40334,41667,41125,40500
1x2	16	head	39000,39958,39417,39584,38417,38916,39041,39417,39542,39750,39625,39792,39083,39208,39083,38917,39458,39417,39583,39291
1x3	1	base	39334,39333,39834,39708,38916,39208,39500,39375,39208,38959,39208,39334,39625,39291,39166,39750,39917,39709,40083,39375
1x3	2	head	39041,39083,39208,39833,39958,39709,39417,39292,39667,39791,39541,39542,39375,39541,39375,39833,39750,39500,39458,39667
1x3	3	head	39334,40292,39834,41041,39792,39750,39417,40042,39458,39417,39458,39166,39375,38958,40042,39291,38750,39917,40042,40042
1x3	4	base	39125,39625,39875,40042,39667,39958,39333,40292,39667,39708,39542,39792,39667,39833,38917,39500,39667,40334,40042,39750
1x3	5	head	39042,39833,39667,39833,39250,39833,39583,38958,39250,39792,39250,39791,39459,39208,39209,39292,39416,40291,39792,39167
1x3	6	base	39584,39625,39750,40083,39500,39417,39584,39334,38916,39458,39625,39917,39333,38792,40250,39209,39833,39875,39375,39500
1x3	7	base	39625,39542,39875,39834,39541,39584,38750,39083,39583,39667,39917,39500,39708,40042,39333,39208,39833,39583,39500,39417
1x3	8	head	40083,40667,39792,39458,39958,39750,39959,39416,40083,39750,39542,39916,39583,39917,39333,39834,39917,40292,39458,39458
1x3	9	base	39666,40084,39500,39583,40000,39750,39542,39250,39458,39458,39916,39709,39375,39500,39583,39791,40375,39791,39333,39833
1x3	10	head	38792,40250,39709,39542,39500,39666,39167,39125,39166,39958,39625,39333,39041,39291,39750,38917,38917,39875,39334,39167
1x3	11	head	40042,40167,40000,39708,39584,39375,39583,39208,39291,39542,39458,40209,39833,39708,40000,39750,39792,39583,39958,39917
1x3	12	base	39375,39708,39584,40000,39209,39500,39250,39458,39542,39500,39875,39500,39375,39375,39375,39375,39375,39458,39667,40083
1x3	13	head	39334,40041,39708,40042,39417,39333,39458,39375,39792,39875,39375,39583,39209,39583,39500,39458,39375,39083,39542,40000
1x3	14	base	39291,39709,39750,39542,39500,39667,39209,39708,39208,40084,39709,39625,40542,39917,39250,39416,39209,39583,39542,39750
1x3	15	base	39667,40625,39750,39917,39292,39792,39416,39333,39542,39750,39583,39875,39792,39917,39292,39708,39834,39792,39417,39375
1x3	16	head	39416,39958,39666,39750,39750,39958,39625,39375,39750,40625,40000,39417,39667,39875,39542,39584,39666,39625,39417,39875
1x4	1	base	72167,72250,71791,71958,71708,72041,71750,72208,72041,72083,72291,72084,72000,72000,71750,71625,71792,71833,72084,72084
1x4	2	head	41500,42417,41333,41417,41250,41583,41709,41584,41500,41792,41584,41750,41084,41167,41250,41542,42000,42000,41667,41792
1x4	3	head	40833,41334,41209,41083,41459,41375,41084,41250,41375,41334,41709,41208,40958,40958,40625,40791,40833,41166,41250,41291
1x4	4	base	71584,72125,71792,71708,71417,71541,71500,71708,71958,71958,71959,71875,71834,71792,71667,71667,71791,72083,72208,71958
1x4	5	head	41250,41375,41958,41541,41333,41250,41834,40917,41458,41667,41334,41292,41084,41375,41291,41292,41708,41834,41750,41583
1x4	6	base	71500,71791,71500,71916,71917,72083,71708,71500,71875,71333,71417,71667,71416,71541,71583,71875,71833,72000,71917,71917
1x4	7	base	71709,72542,72083,72416,72000,72042,71792,72083,71792,72250,71750,71667,71958,72000,72250,72125,72083,72333,71959,72125
1x4	8	head	41167,41625,41291,41250,41000,41291,41125,41500,41042,41333,41125,41167,41208,41541,41250,41083,41208,41542,41750,41250
1x4	9	base	71917,72708,72083,71917,71458,72334,71583,71833,71792,72167,71916,71834,71875,71958,71875,71708,71667,72500,71833,71792
1x4	10	head	41125,41417,41583,41958,41333,41125,41625,41000,41125,41292,41458,41709,41125,41333,41541,41084,41333,41500,41292,41125
1x4	11	head	40917,41500,41375,41625,41584,41292,41334,41083,41041,41250,41459,41458,41334,41417,41125,41167,41583,41458,41125,41250
1x4	12	base	72250,72500,72208,72208,72167,72167,72291,71625,71792,72042,71958,72583,71583,71792,71750,72333,72166,72500,71958,72000
1x4	13	head	41083,41708,41333,41250,41750,41542,41167,40875,41125,41125,41334,41334,41000,41208,41084,41333,40917,41458,41167,41125
1x4	14	base	71291,72459,72291,72000,71792,72250,71500,71875,71583,72208,71750,71916,72084,72041,71833,71625,71750,71875,71875,71875
1x4	15	base	71958,72291,72334,72333,71417,71584,71750,71666,71625,71750,71875,72292,71541,71875,72042,71792,71750,71625,72083,72250
1x4	16	head	41208,42000,41458,42000,41042,41208,41125,41166,41500,41500,41542,41625,41375,41542,41125,40917,41125,41458,41542,41208

DjDeveloperr and others added 3 commits December 3, 2026 03:14
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>

@aurora-silicon-review aurora-silicon-review Bot 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.

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 against imul(isign(a), ushr(iabs(a), k)) at INT32_MIN: iabs wraps to INT32_MIN, so ushr gives 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, so bit_count(x-1) is its index. vote_ieq now 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)) and bit_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.

@satchlj

satchlj commented Dec 2, 2026

Copy link
Copy Markdown
Member

Tested exact head e20bc7252c2e against aurora-wip base fb0f929ad544 on Apple M5 Max (J775c, G17C B1, kernel 7.3.0-1-54.20261202-ARCH, default command line):

  • Native AArch64 assert-enabled build & compiler tests (buildtype=debugoptimized, b_ndebug=false, gallium-drivers=asahi, vulkan-drivers=asahi):
    • Full Meson test suite: 343/343 OK, 0 Fail.
    • Targeted agx_tests (*Wave8*:*Subgroup*:*Ballot*:*Idiv*): 81/81 PASS, including AGX3VisionOpts.Wave8BallotRankUsesProducerMaskAndLowWord, AGX3VisionOpts.Wave8Agx3bSubgroupBallotLtMaskAndFirstInv, and AGX3VisionOpts.Wave8FoldSignedPow2Idiv.
  • Live G17C B1 Borealis Vulkan CTS & RT (isolated release ICD at e20bc7252c2e, MESA_SHADER_CACHE_DISABLE=true):
    • dEQP-VK.api.smoke.*: 8/8 PASS (0 fail).
    • dEQP-VK.subgroups.ballot.compute.*: 2/2 PASS (0 fail).
    • dEQP-VK.subgroups.vote.compute.*: 92/92 supported PASS (92/124, 32 not supported, 0 fail).
    • dEQP-VK.subgroups.basic.compute.*: 12/12 PASS (0 fail).
    • dEQP-VK.glsl.operator.binary_operator.div.mediump_int*: 8/8 PASS (0 fail).
    • dEQP-VK.glsl.operator.binary_operator.div.highp_int*: 8/8 PASS (0 fail).
    • gpubench-rt-vk: raster median 2,257.68 fps (crc32=cca69c73), medium median 138.9 fps (336.5 Mray/s, crc32=e91ea686).
  • Same-boot isolated release ABBA llama-bench (llama-2-7b.Q4_0, -fa 1, -r 5) vs fb0f929ad544 at parity:
    • Default NV_coopmat2: pp128 791.29 → 791.55 t/s (+0.03%), pp512 876.36 → 876.15 t/s (-0.02%), tg32 122.38 → 122.22 t/s (-0.13%).
    • GGML_VK_DISABLE_COOPMAT=1: pp128 373.79 → 373.86 t/s (+0.02%), pp512 775.48 → 775.90 t/s (+0.05%), tg32 122.42 → 122.20 t/s (-0.18%).
  • Faults / stability: 0 GPU faults and 0 kernel oops/WARN in dmesg across the entire run.

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

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 native agx_popcount_to(), and emulates_ufind_msb cleanly lowers read_first_invocation / vote_ieq via bit_count(first_bit - 1).
  • Active-rank lowering (5959c022a7cc, e20bc7252c2e): Both earlier concerns from #801 are cleanly addressed — ballot_true_low_word() enforces s.comp == 0 while opt_compute_ballot_bit_count() checks upper words, active_rank_at_ballot() places load_active_subgroup_invocation_agx right before the producer ballot (preserving its execution mask under divergent consumers) and only reuses an immediately adjacent u2u32(load_active_subgroup_invocation_agx), and the rewrite stays compute-only.
  • Signed pow2 idiv (agx_nir_algebraic.py): (a + bias) >> k for k in 1..30 is bit-exact across all int32_t including INT32_MIN (with k = 31 excluded and covered by a negative test).
  • Hygiene & verification: All 3 commits carry DCO (preserving Dj's authorship and Signed-off-by:), ## Risk is present, and exact-head verification landed across three generations (J615 M3 Air, J700 G17P -42.57% divergent first/vote, and J775c M5 Max G17C B1 343/343 Meson, 81/81 targeted agx_tests, Vulkan subgroup/div CTS + RT + ABBA parity, 0 GPU faults).

LGTM.

@RyanTheTide
RyanTheTide merged commit c94b25c into aurora-wip Dec 2, 2026
@RyanTheTide
RyanTheTide deleted the perf/closeout-801-integer-subgroups branch December 2, 2026 16:41
@DjDeveloperr

Copy link
Copy Markdown

Post-merge check of e20bc7252c2e (merged as c94b25ce4e2f): one real bug in the new rank rewrite and one pass that never fires. The rest is correct.

Bug: ballot_bit_count_inclusive can put its +1 in the wrong block (every generation, compute shaders).
active_rank_at_ballot() sets b->cursor only when it inserts a new rank. When it reuses the rank already in front of the ballot, the cursor stays where the previous rewrite left it, and nir_shader_intrinsics_pass() never resets it. If an inclusive count reuses ballot A's rank after the pass rewrote a count of a second ballot B inside an if, the iadd ..., 1 is emitted in front of B, inside the if, while its use is after the if:

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 fb0f929ad544 compiles this on G14G, G16G and G17P. Head fails NIR validation on all three (nir_block_dominates(...), nir_validate.c:1879). Release builds skip that check and compile the broken SSA.

Smallest fix, in opt_compute_ballot_bit_count():

   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 agx_tests stays at 1,960 passed, 1 skipped.

The signed pow2 idiv fold never fires in a real compile. agx_nir_options sets lower_iabs, so by the time agx_nir_fuse_algebraic_late runs, iabs(a) is already imax(a, ineg(a)) and the pattern can't match. v[i] = v[i] / 8 still ends as imul(imin(imax(a, -1), 1), ushr(imax(a, -a), 3)) on head, and GTA V on G17P is identical with the fold removed. The new unit test passes only because it builds iabs directly. Adding ('imax', 'a@32', ('ineg', 'a')) as a second magnitude form makes it fire: the single divide drops from 12 to 9 instructions on G16G/G17P (12 to 10 on G14G). But the PR's three-divide test shader then gets 1 instruction and 2 GPRs worse on G16G/G17P, so it needs a look before it's enabled. The rewrite itself is exact for every int32, including INT32_MIN.

Checked and fine: immediate SRL plus native popcount for G17P ufind_msb (the same forms the existing G17P ushr and bit_count paths emit), bit_count(first_bit - 1) for first-invocation and vote_ieq, the low-word and upper-word checks, and the rank keeping the ballot's own mask. Note that the rank rewrite runs on every generation's compute shaders, not only G17P.

Local CI on pc (x86-64 Fedora 44 container, debugoptimized with asserts, warm build dir, base fb0f929ad544 and head e20bc7252c2e):

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>  # drm-shim test binaries rebuilt
meson test -C build --suite asahi --suite poly --suite vulkan
build/src/asahi/compiler/agx_tests
  • meson test: base 454 OK, 0 fail, 1 skip. Head 454 OK, 0 fail, 1 skip.
  • agx_tests: base 1,956 passed, 1 skipped. Head 1,959 passed, 1 skipped, including the three new Wave8 tests.

Shader-db, base → head. GTA V Fossilize DB replayed with LD_PRELOAD=libasahi_noop_drm_shim.so ASAHI_SHIM_CHIP=<chip> fossilize-replay --master-process --enable-pipeline-stats:

Chip Executables changed Instructions GPRs Occupancy Spills/fills
G17P 48 of 9,759 −2,093 (32 better, 0 worse) +42 (5 worse) 4 compute shaders 704 → 640 threads unchanged
G17S 38 of 26,447 −428 (38 better, 0 worse) 0 unchanged unchanged
G16G 47 of 9,759 −2,115 (31 better, 0 worse) +18 (3 worse) 2 compute shaders 576 → 512 threads unchanged
G14G 12 of 9,741 −88 (12 better, 0 worse) 0 unchanged unchanged

The occupancy losses are compute pipelines 370656e36682132c, 412cb7e7ae44d488, 8af2029385e47910 and af5d7f2b16f2db49 on G17P (143 → 149 or 153 GPRs), and the first two on G16G (183 → 191 GPRs). Their instruction counts drop by 12 to 67, so whether the trade pays off needs a hardware check. They don't come from the idiv fold, which changes nothing. The PR's own benchmark shaders: ballot 33 → 30 instructions on G16G/G17P (26 → 25 on G14G); integer 157 → 80 on G16G/G17P (G14G unchanged at 55). With asserts on, the same 33 GTA V pipelines crash the replayer on base and on head, so those crashes are pre-existing.

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.

3 participants