perf(shader): drop f32 NaN-result selects whose payload no sink observes #17227
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17227"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by talha-devp on 2026-11-08T15:27:57Z.
What
Part of #11183 (the f32 NaN rule on devices without an f32 denormal mode, step 5), by payload liveness rather than a known-class proof: discussion and numbers.
A select
isNaN(t) ? a : b, whereais NaN whenever it is taken andbis NaN whenevertis, differs frombonly in the NaN payload. The new IR passNanPayloadEliminatorreplaces such selects bybwhen no sink can observe that payload. It runs afterDenormalFlushEliminator, only with exact f32 arithmetic (APS5_FAST_F32unset).Referencethat keeps a branch condition), instructions with side effects and every opcode outside a list of pure ALU operations. A sink that receives a difference keeps the candidates behind it, and the propagation runs again (at most 16 rounds; a budget overrun keeps every select).FPIsNan32,FPSaturate32(every NaN becomes 0), ordered and unordered compares, the exponent mask and unsigned magnitude compares whose result is the same for every NaN magnitude. Other bit work on a NaN-only difference makes it arbitrary.exp == 0 ? sign : x), the output-modifier scaling and NaN-guarded emulations such asv_exp_f32(isNaN(x) ? x : ...).APS5_NO_NAN_PAYLOAD_ELIMINATIONkeeps every select; the recompiler switch list puts it in the shader cache key. The pass relies on the host returning a NaN for a NaN input to GLSL.std.450Floor,Ceil,Trunc,RoundEven,Fract,SqrtandExp2underSignedZeroInfNanPreserve;docs/dev/debt/shader.mdrecords that only NVIDIA is measured.Tested
Windows 10 22H2 (19045), RTX 4060 (NVIDIA 621.14), Ryzen 5 5600, MSYS2 UCRT64 GCC 16.2.0, Release.
mainf5fe807353against this branch:832c2d6126adds the pass,abff150ae3makes its analysis faster without changing its output.Rules. New
agc_nan_payload_elimination, 13 IR cases and one throughRecompile.ISub32. A NaN shared by an observed and an unobserved rule stays. Four of the cases were added by oneandonlydean in8c8015f6e6. With the observability check skipped (every candidate replaced), 5 of the first 9 fail.Recompile(54a560b95f, from adam25136's review). Two compute programs addv0to itself twice and store the result. With the secondv_add_f32clamped, no f32 NaN test is left in the SPIR-V; without the clamp, all four stay. With thePrepareResourceProgramcall removed, orAPS5_NO_NAN_PAYLOAD_ELIMINATIONset, both programs keep four and the case fails.GPU tests. On
mainand on this branch (at832c2d6126and again atabff150ae3), each executable run directly withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(MSYS2vulkan-validation-layers1.4.363,VK_ADD_LAYER_PATH;VK_LOADER_DEBUG=layershowsInsert instance layer "VK_LAYER_KHRONOS_validation"). Neither log reportswas not found but was requested by env var VK_INSTANCE_LAYERS.mainVUID-lines (main / PR)agc_driver_f32_arithmetic_nanagc_driver_f32_min_max_nanagc_driver_f32_output_modifieragc_driver_f32_result_clampagc_driver_f32_denormal_flushagc_driver_f32_add_mul_denormal_modesagc_driver_f32_fract_floor_denormal_modesagc_driver_f32_compare_constantagc_driver_f32_constant_factor_tininessagc_driver_sdwa_sin_cos_f32agc_f32_nan_result_selectsagc_denormal_flush_eliminationagc_nan_payload_eliminationIn a prototype of the same analysis, the pass removed selects inside
f32_arithmetic_nan(66 of 1,361),f32_result_clamp(17 of 380) andf32_output_modifier(45 of 542), and those tests kept passing. The full suite was not run.Homebrew titles on the same renderer. Catalog builds, SHA-256 checked.
tools/title_run.pyonmainf5fe807353and on this PR's head8c8015f6e6. A cold run starts with empty AnyPS5 shader and driver caches; a warm run follows it.main[ps5-agc] submission retirement failed; terminating application before cleanupindownload0/hui/dev/app.logThis is the first-run stop LeandroLP23 measured in #11183. On this host the PR clears it for Surround Sound Studio but not for Homebrew UI Lab.
Performance. ProsperoLight (PPSA99002 01.000.110, free homebrew), idle PCs screen, 3840x2160. Most of its GPU time is one fragment program (
0x217e15000, the same program as0x21ebf9000in #11183) with 9,403 NaN-test selects.tools/title_run.py --runs 3 --seconds 40withAPS5_TRACE_SECONDS=15:10, rounds alternated main / PR / main / PR, run 1 of each round (cold caches) dropped: 4 warm windows per arm.title_run.pyalso setsAPS5_DUMP_FRAMES_EVERY=30andAPS5_TRACE_SUMMARY_EVERY=10in both arms, and the title writes stderr to its own log.mainf5fe807353832c2d61260x217e15000per draw, separate pair withAPS5_PROFILE_GPU=1 APS5_PROFILE_GPU_DRAWS=10x217e15000SPIR-V words (agc_shader_replayon its request)agc_shader_replaywall time for that request, best of 3832c2d6126)APS5_TRACE_DRAWS=1 APS5_PROFILE_DRAW=1)One of the PR's four windows reports
gpu_busy_ms_per_present0.00 while presenting 598 frames at 16.66 ms p50; the GPU range above uses the other three. The same zero showed up once before in an unrelated arm of an earlier session, so it looks like a trace recording issue, not this change; its cause was not investigated.The GPU numbers were measured at
832c2d6126. Atabff150ae3the program's SPIR-V is byte-identical (agc_shader_replay --spv), so they apply to both commits. The added recompile time is CPU work on the first use of a program; the driver's pipeline creation for the smaller module drops by more than that here.Window summary lines, one per arm:
Digests (
tools/trace_digest.py):The traces can be shared on request; they could not be attached from the command line.
Not measured: AMD and Intel GPUs, Linux, large compute programs (the analysis cost scales with the program; a budget overrun keeps every select). Runs on other vendors would help.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code