Repository navigation
agx: flatten single-arm divergent short-circuit uniform global loads - #681
Conversation
|
Read through 2edd38c. I could not find a correctness problem: the arm can hold only redirected or speculatable loads and pure ALU, subgroup/quad ops are rejected through
Assisted-by: LLM (Claude:Opus 5.5) |
|
Clean-room runtime verification on Apple M5 Pro ( Unit Tests
M5 Pro (
|
2edd38c to
8212b1c
Compare
|
Follow-up confirmation for R2 head
|
8212b1c to
705d539
Compare
|
Rebased onto |
The second operand of a short-circuit && or || that reads memory becomes
a divergent if whose one non-empty arm loads from global memory:
if (c) { v = load_global(addr) < k; } r = phi(v, false)
agx_nir_opt_flatten_loads() keeps divergent ifs, and the if costs every
lane a control-flow boundary that waits for all loads in flight. Chained
tensor bounds checks reading uniform padding descriptors have this shape.
Add agx_nir_opt_flatten_short_circuit_uniform_loads() for it, on every
AGX3 encoding except G18G. It takes an arm of at most two loads and
eight counted instructions, whose memory reads are global loads at a
subgroup-uniform address, and whose other instructions may be
speculated. Each load that is not already speculatable is redirected to
the zero page unless some active lane takes the arm: the address is the
same for every lane, so if one lane reads it, every lane may, and the
test keeps the load uniform. The phis become selects.
In the flattening loop, peephole select now reruns only after something
changed, instead of repeating the run just before the loop.
Signed-off-by: Diljit Singh <hey@djdev.me>
|
Pushed R3 (
Three-chip shader-corpus impact (fires on 1 shader across the corpus, a Geekbench 6 kernel):
On G17P and G17S the load is already speculatable, so the any-lane guard folds away. On G16G that shader has no constant program, so the guard adds 4 instructions and raises GPRs from 23 to 27. This still needs M3, M4 (G16G timing to check whether removing the divergent branch outweighs the +10 instr / +4 GPR cost), and G17P before/after rows before merge. |
705d539 to
b1e52fe
Compare
satchlj
left a comment
There was a problem hiding this comment.
Verdict at b1e52fe13895
APPROVE
Blockers
None.
Checked
- Code audit (
src/asahi/compiler/agx_nir_opt_flatten_loads.c,agx_compile.c):- Verified the pass gate in
agx_compile.c((byte_indices || m5) && !g18_scans) runs beforenir_lower_subgroupsandagx_nir_lower_subgroups_*, and thatnir_intrinsic_ballotlowers directly to nativeagx_ballot_to()across stages without interfering withagx_nir_lower_subgroups.c'spopcount(ballot(true))optimization. - Verified fault/bounds safety in
flatten_short_circuit_uniform_load_block(): requires single-block arms with exactly one constant-only arm, divergentnif->condition, non-empty scalar 1-bit phis with constant empty-arm sources,arm_is_flattenable()within budget (1..2loads,<= 8counted instructions, rejecting stores/atomics/barriers/subgroup ops/jumps), and every memory read beingload_global/load_global_constantwithACCESS_NON_WRITEABLE, noVOLATILE|ATOMIC|COHERENT, and subgroup-uniform address (!intr->src[0].ssa->divergent). - Verified
any_taken = nir_ine_imm(b, nir_ballot(b, 1, 32, taken), 0)is emitted at the end ofprev_block(dominatingarmunder the active entry execution mask), preserving subgroup-uniformity of redirected load addresses and redirecting non-speculatable loads toAGX_ZERO_PAGE_ADDRESSwhenever no active lane takesarm.
- Verified the pass gate in
- Unit tests: Full non-layout
asahisuite249/249PASS (agx_testsOK;170/170*Flatten*tests PASS). - Live hardware (Apple M5 Max (G17C B1), Linux 7.3,
MESA_SHADER_CACHE_DISABLE=1): Targeted Borealis Vulkan compute, subgroup ballot, and SSBO CTS groups pass with0failures and0GPU faults indmesg. - Untested scope: Non-M5 AGX3B (
G16G/G17P) live hardware execution (covered by unit tests and pass gating) and full Vulkan/GLES3 CTS regression runs.
Evidence
meson test --suite asahi(non-layout):249/249OK (agx_testsOK;agx_tests --gtest_filter='*Flatten*':170/170PASSED)- Borealis Vulkan CTS (
Apple M5 Max (G17C B1),Linux 7.3,MESA_SHADER_CACHE_DISABLE=1):dEQP-VK.compute.pipeline.basic.*:80/81Passed,0Failed,1Not supporteddEQP-VK.subgroups.ballot.compute.*:2/2Passed,0FaileddEQP-VK.ssbo.layout.single_basic_type.std430.*:303/303Passed,0Failed
dmesgGPU fault delta:0
…-loads Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
…-loads Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
Post-merge bug report at
|
Summary
Adds
agx_nir_opt_flatten_short_circuit_uniform_loads()alongsideagx_nir_opt_flatten_loads()insrc/asahi/compiler/agx_nir_opt_flatten_loads.c, enabled for AGX3B and AGX3C ((byte_indices || m5) && !g18_scans) inside the existing flattening fixed-point loop inagx_optimize_nir().Problem
Compute shaders frequently emit chained short-circuit bounds checks whose guarded arms load a uniform descriptor value from a read-only SSBO (e.g., tensor padding/shape descriptors):
Because the condition is divergent (
coorddepends ongl_GlobalInvocationID),agx_nir_opt_flatten_loads()leaves theifintact (as intended for general divergent branches), whilenir_opt_peephole_select()cannot flattenload_globalintrinsics. On AGX3 this forces every lane through serialized control-flow barriers (CNDST/CNDEF/pop + load waits) for every chained check.Fix
flatten_short_circuit_uniform_load_block()matches only narrow, strictly bounded single-arm divergent short-circuitifs:thenandelse, where one arm is empty (or contains onlyload_const/undef), andnif->control != nir_selection_control_dont_flatten.arm_is_flattenable(), with<= 8instructions,1..2loads, and<= 2redirects.load_global/load_global_constantwithACCESS_NON_WRITEABLE, none ofACCESS_VOLATILE | ACCESS_ATOMIC | ACCESS_COHERENT, and a uniform address (!addr->divergent).redirect_loads()helper toAGX_ZERO_PAGE_ADDRESSwhen untaken; loads already markedACCESS_CAN_SPECULATEretain their uniform address.Verification
FEDORA-CI(asahisuite,AURORA_WITH="") at2edd38cdbdc:156/0PASS (1227/1227agx_teststests including new unit tests intest-flatten-loads.cpp:ShortCircuitUniformLoadFlattensAndRedirects,ShortCircuitSpeculatableUniformLoadKeepsAddress, and 9 negative safety cases inShortCircuitUniformLoadRejectsUnsafeShapes).DEFAULT_BRANCH=aurora-wip .github/aurora/agent-policy.sh pr HEAD->PR head is clean.86f85be1664) with0regressions across all 8 Geekbench 6.7.1 Vulkan workloads,13/13matrixpair0 bit diffs, and27,740/0targeted Vulkan CTS (spirv_idiv,robustness,glsl.loops,subgroups.arithmetic.compute,ssbo,compute).