Skip to content

agx: limit fragment loop and branch expansion on M5 - #459

Closed
erikwb wants to merge 1 commit into
aurora-wipfrom
j716c/t2-fragment-flow
Closed

erikwb wants to merge 1 commit into
aurora-wipfrom
j716c/t2-fragment-flow

Conversation

@erikwb

@erikwb erikwb commented Nov 19, 2026 •

Copy link
Copy Markdown

Large unrolled loops and flattened conditionals can expand M5 fragment programs. Limit loop unrolling to eight iterations and the peephole-select budget to twelve instructions in that stage.

Tested on an M5 Max (G17C). 54 focused shader and buffer CTS cases passed on its standalone head (5 unsupported).

Hardware benchmark

Cyberpunk 2077 2.5 on a 40-core Apple M5 Max (J716c, 128 GB), Linux 7.3.0-ewb234+ on AC. Replayed the same 22 measured frames of a 3840×2160 High, FSR2 Balanced Vulkan capture with GFXReconstruct 1.0.5-dev (ccbbb1c). The game capture needs #450, #451 and #452 on 57df4db849a; the unmodified public base stops compiling its MRT fragment epilog. Only the feature differs within each pair.

PR parent 57df4db849aa, proposed feature acf07800a9a4. Tested integration commits 92c9f438a005 and 9600db5b94b2. Build the control by cherry-picking the game prerequisites in order onto 57df4db849a, then cherry-pick the feature for the candidate. Both builds used AArch64 release Honeykrisp.

Median of three runs Control Proposed Change
Replay FPS 12.586 12.604 +0.15%
Vertex ms/frame 4.389 4.388 -0.04%
Fragment ms/frame 12.673 12.662 -0.09%
Compute ms/frame 25.317 25.209 -0.43%

Run order was control/proposed, proposed/control, control/proposed. Replay throughput varied from 12.12 to 13.23 FPS across these short runs. Stage times use the replay process's DRM engine nanoseconds divided by the number of frames. Each engine is reported separately. The game’s full native-resolution T2 benchmark measures a different workload.

The comparison measures this capture. The aggregate T2 result cannot be attributed to this PR alone.

Reproduction and raw output

Use the GFXReconstruct replay tool with an optimized 24-frame capture of the benchmark. The private game asset hash was 2c74dea133fc0067d4a51ef3dafa699388ea0b8d132ee86300cca38b6842600f. For each build, run the same command under a 4 KiB user page personality:

VK_DRIVER_FILES="$icd" DRIRC_CONFIGDIR="$drirc" MESA_SHADER_CACHE_DIR="$cache" \
userpages 4k gfxrecon-replay-gputime -m rebind --log-level error \
  --measurement-frame-range 2-24 --flush-measurement-range \
  --measurement-file frames.json --omit-pipeline-cache-data scene-optimized.gfxr
round,build,frames,fps,vertex_ms_per_frame,fragment_ms_per_frame,compute_ms_per_frame
1,control,22,12.12476020,4.39680314,12.69313195,25.34562855
1,proposed,22,12.57272282,4.38754805,12.66165945,25.20092950
2,control,22,12.58560248,4.38727032,12.67315109,25.31669805
2,proposed,22,12.60447249,4.39838555,12.66717332,25.31672818
3,control,22,12.62144006,4.38932605,12.66322223,25.22491373
3,proposed,22,13.22761239,4.38699168,12.63799750,25.20856973

Reviewed with sol-loop: 1 round with GPT-6.2 Sol. Sol verdict: approved.

Created with GPT-6.2 Astra in Codex.

Developed with Claude Code.
Signed-off-by: Erik Bourget <erik@ebourget.net>
@RyanTheTide

Copy link
Copy Markdown

Thanks for the patch.

We keep compiler limits the same on every generation unless a hardware reason says otherwise, rather than scoping them to the GPU they were measured on. Both new limits are M5 fragment only:

  • src/asahi/compiler/agx_compile.c:9659 options.max_unroll_iterations = nir->info.stage == MESA_SHADER_FRAGMENT ? 8 : 64; (inside if (m5))
  • src/asahi/compiler/agx_compile.c:9674 .limit = m5 && nir->info.stage == MESA_SHADER_FRAGMENT ? 12 : 64,

Limiting unroll and flatten expansion in fragment programs is a code-size or instruction-cache trade-off that other generations share. Could these limits apply to fragment shaders on every generation, or follow a hardware property such as instruction-cache size, with a code comment naming the reason? The +0.15 % in the short replay is within run-to-run noise, so a workload that shows the benefit would help too.

If you make it the default, we can supply rows on our M1 Pro, M2, M4 Pro and A18 Pro.

@erikwb

erikwb commented Nov 19, 2026

Copy link
Copy Markdown
Author

Note

🤖 GPT-6.2 Astra responding on behalf of erikwb

The longer game test leaves the benefit of the fragment unroll and flatten limits unresolved. Including #459 changed T2 by −0.02 and −0.05 FPS in the two blocks.

We used two blocks of full Cyberpunk T2 runs in opposite orders on the M5 Max, native 2560×1600 High, with the fans fixed. The control was Mesa e59727e7a4fc with #460, #461, #466 and #468 removed. Each delta uses the mean of two control runs in its block. For this PR we also built the control without #459 to isolate its effect.

Limiting expansion can prevent oversized shaders. In this game only 3 of 723 captured fragment binaries changed. The one affected draw we matched in the trace took a median 0.049 ms of fragment time across 15 GPU samples. That gives this workload little leverage on the heuristic. A shader that actually expands substantially would be a better test for choosing limits across GPUs.

In the separate eight-run test, #460, #461, #466 and #468 together gained 1.35 FPS (29.99 to 31.33). #459 was present on both sides of that comparison.

@RyanTheTide

Copy link
Copy Markdown

Thanks for running the longer test and for reporting it plainly. With only 3 of 723 fragment binaries changing and no measurable effect in either block, there is no measured benefit to justify M5-only limits. Would you be open to closing this one, or dropping it until a shader that really expands shows the gain? If such a case turns up, we would want the limits to apply to every generation (or follow an instruction-cache property) with a code comment, and we can supply rows on our M1 Pro, M2, M4 Pro and A18 Pro.

@erikwb

erikwb commented Nov 21, 2026

Copy link
Copy Markdown
Author

Note

🤖 Neon responding on behalf of erikwb

Closing this PR. With only 3 of 723 captured fragment shaders affected and −0.02 / −0.05 FPS in the isolated T2 blocks, there is no measured benefit to justify M5-only unroll/flatten limits. If a workload with substantial fragment expansion turns up later, we can revisit it as a cross-generation limit.

@erikwb erikwb closed this Nov 21, 2026
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