Skip to content

perf(shader): emit the f32 NaN repair without its own selection - #28820

Open
oneandonlydean wants to merge 1 commit into
boykopovar:mainfrom
oneandonlydean:perf2/nan-repair-without-selection
Open

oneandonlydean wants to merge 1 commit into
boykopovar:mainfrom
oneandonlydean:perf2/nan-repair-without-selection

Conversation

@oneandonlydean

@oneandonlydean oneandonlydean commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

What

EmitNanPropagateF32 opened one DontFlatten selection per exact f32 result: branch on "the host result is a NaN", pick the source NaN inside, phi after. In large compute programs that is hundreds to thousands of small selections in one function, and the GPU vendor's pipeline compiler pays for each of them.

Now the repair is emitted without its own selection: the same selects pick the source NaN, they always run, and one more OpSelect on the same NaN test chooses between the repaired bits and the host result. The value is the same in every case: the instructions that moved out of the branch are bit casts, integer compares, selects and one OR, none with a side effect. The form without possible NaN sources was already a single select and is unchanged, and so is the grouped region path (EmitNanRegion), which keeps its one entry or exit test for a whole region.

Why: on #27914 a reporter's boots spend about a minute of vendor compile per new compute program, with one function holding 90% or more of each module. I measured where that time goes on a title I hold and found this branch per result to be the largest single part I could remove without changing results.

The debt note of #17733 said the source repair branches on a NaN host result; its sentence is corrected in the pending fragment that adds the entry.

Tested

Linux (CachyOS, GCC 16, Release), NVIDIA GeForce RTX 4090, driver 610.57.04.

On this head:

  • GPU execution tests agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast and agc_driver_fma_mix_f32_nan_sources (they run v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32, v_fma_f32 and v_fma_mix_f32 forms with NaN sources on the GPU and compare the result bits, payloads included): pass.
  • agc_nan_payload_elimination, agc_shader_memory, agc_spirv_branch_targets: pass.

Measured with a build of main 7502170 that carries this same change behind a local switch (one binary, switch off and on; the switched-on path emits exactly what this PR emits):

Vendor pipeline creation time, offline. The 13 compute programs over 100000 SPIR-V words that PPSA21564 creates in one level, captured once per arm and replayed with agc_compute_pipeline_replay, one process per replay, fresh application cache, driver disk cache off, two passes. Times are the tool's fresh pipeline creation time in ms, on this host's E cores.

program words before selections before ms pass 1 ms pass 2 words after selections after ms pass 1 ms pass 2 after / before
0x50051e600 207446 751 4664 4836 201065 327 2700 2579 55.6%
0x500525200 138459 473 1001 1010 132041 52 718 733 72.2%
0x500571000 269476 1194 6891 7248 261184 639 4137 4020 57.7%
0x500586800 333603 1461 8740 9335 321251 642 5768 5933 64.7%
0x50059cd00 292684 1310 7946 7619 282906 650 4772 4599 60.2%
0x5005b3f00 358136 1604 10395 10474 344196 675 6638 6591 63.4%
0x5005cc100 307663 1321 7887 8296 297851 654 5129 4545 59.8%
0x5005e4a00 372913 1619 10422 10823 358883 681 6415 7040 63.3%
0x5005fdb00 327956 1435 9537 9871 316606 659 5576 5611 57.6%
0x500617500 393268 1733 11430 12221 377712 686 7019 8933 67.4%
0x500666700 149521 691 1062 1002 141361 167 612 618 59.6%
0x555f41f00 555668 2436 9691 9793 536622 1095 7534 7256 75.9%
0x555f4f500 312386 1292 3983 4293 306732 899 3081 2933 72.7%

All 13 together: pass 1 93648 ms to 60100 ms (64.2%), pass 2 96821 ms to 61391 ms (63.4%). Words 96.5%, selections 45.2%. No program got slower. The two passes of one capture differ by 3.8% in the median, 24% at most.

Frame time, PPSA21564, same scene, one perf window, warm shader caches (so this pair shows the run-time cost of always running the selects, not the compile gain): p50 over 180 to 330 s is 205.8 ms before and 208.6 ms after (+2.8 ms, +1.4%); the noise floor of this scene is 2 to 4 ms, so neutral. Presents per 10 s 49 and 48, GPU busy per present 81.8 ms and 80.8 ms. No crash, the level loaded and the frames show the same pictures in both arms.

Not claimed: that this unlocks PPSA38500 (#27914). It shortens the compile of each new large compute program there by an amount I measured only on PPSA21564's programs; whether that is enough for the reporter's boots is for a run on this head to show.

Windows: built and tested by CI only. No Windows host here.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4090, driver 610.57.04, Linux
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Updated shader handling to select the repaired floating-point result when the host calculation produces NaN. Existing fallback behavior for cases without a possible NaN source remains unchanged.
  • Documentation
    • Clarified how floating-point result repair and selection work, including existing fallback behavior and compatibility caveats.

EmitNanPropagateF32 opened one DontFlatten selection per exact f32
result: branch on a NaN host result, select the source NaN inside, phi
after. A large compute program carries hundreds to thousands of these in
one function, and the vendor's pipeline compiler pays for each.

The repair now runs unconditionally and one more select on the same NaN
test picks between the repaired bits and the host result. The value is
unchanged: the instructions that left the branch are bit casts, integer
compares, selects and one OR. The form without NaN sources and the
grouped region path are untouched.

Offline creation of the 13 compute programs over 100000 words of
PPSA21564 takes 64% of the time it took (56% to 76% per program, none
slower), with 96.5% of the words and 45% of the selections. A warm
paired run of PPSA21564 is neutral.
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: F32 NaN propagation

Layer / File(s) Summary
Emit and document the NaN repair select
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp, docs/dev/debt/changes/pr17733-shader.md
The emitter selects propagated bits when resultNan is true and original result bits otherwise. The documentation describes computing repair for every exact result without branching.

Possibly related issues

  • Issue 4042 discusses recompiler costs from NaN and special-value repair rules, including f32 NaN rules. This change updates the f32 NaN propagation path, but does not implement the issue’s proposed guard-and-branch approach.

Possibly related PRs

Suggested reviewers: filip722, mihiii1337, marczx10

Change: Refactor

Merge Risk: Minimal · up to f844e

The shader emitter now selects the repaired NaN result directly instead of branching. Results are intended to be unchanged, and reported GPU tests and offline pipeline measurements are consistent with that. No merge-blocking issue was found.

Security architecture risk: Minimal · up to f844e

The SPIR-V backend removes a branch from f32 NaN repair while preserving which result bits it returns. The change does not add a caller, grant access, or change how the shader recompiler is deployed.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3e783fc2-7100-436a-b29f-47df2c6695cd
Commits

Reviewing files that changed from the base of the PR and between 5ff6ec4 and f844ec4.

Files selected for processing: 2

  • core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
  • docs/dev/debt/changes/pr17733-shader.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Dec 4, 2026
@JJenkx

JJenkx commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Paired Linux check on AMD Radeon RX 6650 XT (RADV), comparing the PR base 5ff6ec436580 with f844ec4a1767. The three Vulkan fixtures agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast, and agc_driver_fma_mix_f32_nan_sources passed on both revisions. I verified which build of the driver library each executable loaded. Khronos validation was active in all six runs, with zero VUIDs or synchronization hazards reported. The CPU tests agc_nan_payload_elimination and agc_spirv_branch_targets also passed on both.

I gave the existing CPU fixture's four shaders distinct dump addresses and validated the eight modules with spirv-val --target-env vulkan1.3. In the raw multiply case, one DontFlatten selection and its phi were replaced by a select, reducing the module from 653 to 639 words. The raw add and both clamped modules were byte-identical across builds.

AI-assisted: yes.

@github-actions github-actions Bot added shader core/shader docs labels Dec 4, 2026
@jassycliq

Copy link
Copy Markdown

Data for the same transform from Windows and NVIDIA, on larger modules.

My local build has carried this change since Nov 29 behind an opt-in switch, APS5_BRANCHLESS_NAN_REPAIR (94bf347bb7 on jassycliq/AnyPS5 wip/compile-wall). It emits the same form as this head. The source checks always run, and select(resultNan, propagated, bits) replaces the DontFlatten selection and its phi.

Host: Windows 11, RTX 3090 24 GB, NVIDIA 617.14.

Offline pipeline creation. I captured six compute modules of 0.94 to 1.67 million SPIR-V words from a title and replayed them with agc_compute_pipeline_replay.

  • Each replay ran in a fresh process.
  • Each replay used a unique copy of the module, so no cache could serve it. As a control, three identical replays took 50.5, 50.0 and 49.4 s.
  • Each module ran two passes per arm.
  • For the "after" arm, I rewrote each captured module offline into the select form, and two independent checkers found it equal to the original. The switch emits the same form.
  • Times are the tool's pipeline creation figure.
words before → after before, s (pass 1 / 2) after, s (pass 1 / 2) change peak commit, MiB
996,842 → 963,101 50.79 / 51.38 35.66 / 36.46 −29% not logged
1,329,216 → 1,292,829 76.49 / 76.04 62.46 / 62.76 −18% not logged
1,665,000 → 1,597,995 91.49 / 92.50 63.57 / 63.68 −31% not logged
1,077,743 → 1,054,138 40.36 / 40.41 30.89 / 34.89 −19% 1,534 → 1,169 (−24%)
990,512 → 968,783 36.06 / 35.53 29.61 / 29.56 −17% 1,408 → 1,070 (−24%)
940,912 → 920,303 26.55 / 26.23 20.63 / 20.35 −22% 1,208 → 926 (−23%)

All six together went from 321.9 s to 245.3 s (−24%), with 96 to 98% of the words. No module got slower.

Two forms that didn't help. Moving the repair into DontInline helper functions made creation 1.8 to 7 times slower. A Flatten hint on the existing selection stayed within 1% of the base time.

Run time. On an open-source homebrew title at its 60 Hz cap, GPU busy per present was 3.82 and 3.46 ms with the branch, and 3.71 and 3.42 ms with the select form.

Exactness on this host. I ran agc_driver_f32_arithmetic_nan_tests with the switch and ANYPS5_NO_SHADER_CACHE=1. Its inputs include signalling and quiet NaN payloads, and it compares result bits. It passes, together with the _fast arm and the select/branch shape checks. I have not run agc_driver_fma_mix_f32_nan_sources with the switch.

AI-assisted: yes.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 28638 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp L1494-1501, L1518-1523

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 7392 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 7642 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 9508 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 10193 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 13928 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 13929 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 20518 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22034 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22424 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22439 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22446 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22628 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 23811 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 26101 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 26686 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28470 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28487 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28586 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28590 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28668 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28673 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 29089 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 29106 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 29107 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 29119 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 29120 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 29121 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 29123 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

The reviewed change removes the NaN repair branch while preserving the result selection, and I found no defect in the changed code.

What I ran

Reviewed head f844ec4a1767f2742a44223390eaf8babdd66f76 using the prepared diff and manifest, the changed function’s source range, and the complete changed debt note. No tests or builds were run.

Results

Check Result Note
Changed code review passed Repair selection remains gated by the NaN test
Tests not run No runtime checks performed

Negative control

Not run; no test was run to invert the expected behavior.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

GPU execution, console compatibility, and performance claims in the description were not independently verified. No changed files were excluded by the manifest.

To get this to a good state

Nothing is required.

@videogamer10293

Copy link
Copy Markdown
Contributor

Windows: built and tested by CI only. No Windows host here.

(PR description)

jassycliq's Windows data covers six larger modules from a title it does not name. Here are your 13 PPSA21564 programs, replayed on Windows with NVIDIA (RTX 5070 Ti, driver 621.24), plus this head in the title and its GPU tests.

Compile time. The 13 programs together take about 72 to 74 s per pass on main and about 59 s with this head: 80% to 81%, against 64% in your table. OS, driver and GPU differ from your setup, and probably the CPU (your E cores), so this does not show which one makes the difference. The cut is in the range of jassycliq's 17% to 31%. All 13 medians are lower with this head.

The largest program gains least. It took 8.0 to 8.4 s on main and 7.9 to 8.1 s with this head over 3 passes, a median of 97.9% (yours: 75.9%). A second session with 5 passes gave 8.13 to 8.27 s against 7.79 to 8.09 s (96.6%). In that session the 393268-word program went to 74.4%, close to its 76.8% in the first. This head removes all 1341 DontFlatten selections of the largest program, as it removes all 1047 of the 393268-word one.

words before ms before ms after after / before yours
555668 8122 7952 97.9% 75.9%
393268 9079 6970 76.8% 67.4%
372913 8357 6422 76.8% 63.3%
358136 7925 6252 78.9% 63.4%
333603 6533 5015 76.8% 64.7%
327956 7065 5464 77.3% 57.6%
312386 2962 2818 95.1% 72.7%
307663 6338 5043 79.6% 59.8%
292684 5907 4677 79.2% 60.2%
269476 5173 4074 78.8% 57.7%
207446 3187 2487 78.0% 55.6%
149521 897 816 91.0% 59.6%
138459 937 837 89.3% 72.2%

Crash Site also creates 3 programs that are not in your table, and 5 of yours a second time under other identities. All 21 pipelines together: 104.1 to 105.9 s per pass on main, 84.2 to 84.9 s with this head (80.2% to 81.0%).

How. I captured at Crash Site with APS5_DUMP_COMPUTE_PIPELINES and APS5_DUMP_COMPUTE_MIN_WORDS=100000, on main fb41bd7198 and on a local merge of f844ec4a17 into it (6ef51b82cb). Each side captured the same 21 identities in all 4 of its runs. The word and selection counts of your 13 programs match your before and after columns exactly. I replayed them with agc_compute_pipeline_replay from fb41bd7198, built with -DANYPS5_ENABLE_SPIRV_TOOLS=ON; all 42 modules passed its SPIR-V validation. One process per replay, a fresh application cache, a new empty __GL_SHADER_DISK_CACHE_PATH folder for each replay, the two sides interleaved, 3 passes. A cell is the median of 3 passes, or of 6 values for the 5 programs captured twice. The totals of the 13 vary by under 1 s with which copy is counted. The passes of one program differ by 2.7% of its median in the median, 11.4% at most. The CPU is a Ryzen 5 7600X with no pinning, and your times are on E cores, so only the ratios compare, not the ms.

Frame time. 3 pairs (A, B, B, A, A, B), 300 s each, medians over the summary intervals that start in the last 120 s of each run's log. Frame p50 is 86.5, 88.1 and 88.0 ms on main, and 86.1, 91.4 and 87.6 ms with this head. GPU busy per present is 52.73, 53.59 and 52.58 ms, against 52.16, 52.20 and 53.08 ms. I see no run-time cost beyond the run-to-run spread. Shader cache misses were 7, 0 and 0 on main, and 10, 5 and 89 with this head. Capture was on in every run on both sides.

Tests. On this GPU under VK_LAYER_KHRONOS_validation (implicit layers off, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1), on main fb41bd7198 and a local merge of this head (f85e63283d): agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast, agc_driver_fma_mix_f32_nan_sources, agc_nan_payload_elimination, agc_shader_memory, agc_shader_memory_coherent_atomic_loads and agc_spirv_branch_targets pass on both sides. The three GPU tests (the first three) got the layer, 3 inserts per side; the other four create no Vulkan device. 0 VUID- or SYNC-HAZARD lines on each side. The tests install no debug messenger and I ran no positive control, so 0 lines means only that none reached the log.

Setup, the driver cache and the cold runs

Ryzen 5 7600X, Windows 10 19045. -lvl hub_crashsite, no input. The tool's usual environment on both sides (summary every 10 s, pad input off, pad recording, screenshots before the measured window), plus the two capture variables. The log is redirected to a file on both sides. Other programs used a median of 2.76 to 2.88 cores in each measured title run, and 2.80 and 2.53 in the two warm-up runs. I did not measure the load during the replays.

The driver wrote a GLCache folder in each new __GL_SHADER_DISK_CACHE_PATH folder. For the largest main program, a second replay with the same driver cache was no faster (9.0 and 8.9 s). A new folder each time, or __GL_SHADER_DISK_CACHE=0, gave 8.7 to 8.9 s. That check ran in another session, and the main replay set gave 8.0 to 8.4 s for the same program.

Both sides started on a new, empty shader cache folder, so each got one cold warm-up run (A first, then B). Over the logged part of those runs (to 253 s and 261 s), the Vulkan.ComputePipeline phase, summed over threads and over all compute pipelines, is 90.6 s for main and 69.4 s for this head. That is one run per side, not a measurement.

Not covered: one GPU and one machine, NVIDIA only. Captures from one level. No check of the picture beyond every run reaching and holding the level. Not tested: whether this shortens the PPSA38500 boots in #27914. main is now 0b0c56dec56. Nothing under core/shader changed since fb41bd7198; five libSceAgcDriver files outside the tests did, and I did not test them with this head.

AI-assisted: yes.

@Plyxen Plyxen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Windows 11 (GCC 15.2, Release) on main 484b036, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and the Khronos validation layer 1.4.363. Every run log shows the loader inserting the layer and none shows "was not found". It ran on an RTX 5080 (621.24) and an AMD Radeon iGPU (26.11.2), together with #28802, #28780, #28646 and #28670.

  • The f32 NaN group passed on main and on this change on both GPUs, with no VUID- messages in any log: agc_driver_f32_arithmetic_nan, agc_driver_f32_arithmetic_nan_fast, agc_driver_fma_mix_f32_nan_sources, agc_nan_payload_elimination, agc_shader_memory and agc_spirv_branch_targets.
  • Read the emitter change. The moved instructions are bitcasts, integer compares, selects and one OR, all side-effect free. The final OpSelect on resultNan gives what the old phi gave on both edges.

Not run: the pipeline creation timings and the PPSA21564 frame-time pair, and Linux.

AI-assisted: yes (Claude Code)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs perf numbers missing perf pull request without a before and after summary line perf Performance change shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants