Skip to content

perf(shader): drop flushes of f32 results that cannot be denormal - #4278

Closed
Friesmaniac wants to merge 1 commit into
boykopovar:mainfrom
Friesmaniac:perf/shader-denormal-free-results
Closed

Friesmaniac wants to merge 1 commit into
boykopovar:mainfrom
Friesmaniac:perf/shader-denormal-free-results

Conversation

@Friesmaniac

Copy link
Copy Markdown
Contributor

What

DenormalFlushEliminator removes the f32 denormal flush (and, iequal, and, select, 4 IR instructions) on values it can prove are not denormal. It now also recognises:

  • FPFloor32, FPCeil32, FPTrunc32, FPRoundEven32: the result is integral, ±0, ±inf or NaN.
  • FPSqrt, FPRecipSqrt32: the backend flushes the input first (EmitFlushF32DenormToSignedZero), and the result of a non-zero normal input stays at or above 2^-75 (rsq of the largest finite value is about 2^-64).
  • FPMin32, FPMax32, FPMinTri32, FPMaxTri32, FPMedTri32 of denormal-free operands: EmitMinMaxF32Value and EmitFPMedTri32 select operand bits, or the AND/OR of two zeros.
  • BitwiseAnd32 with 0x80000000 (always ±0), and BitwiseOr32 with a constant that has an exponent bit set (the exponent stays non-zero). This covers the sign and signed-infinity arms that unaryFloatSpecials builds, so the flushes after v_sqrt_f32, v_rsq_f32 and v_floor_f32 go away as well.

FPFract32, FPRecip32 and an OR with a mantissa-only constant (the IEEE-mode NaN quieting, | 0x00400000) are not treated as denormal-free; the tests pin that.

Part of the compile-cost side of #2682: the issue lists the denormal-flush selects as one of the remaining large costs in float-heavy pixel shaders.

Tested

Windows 11 Pro, WinLibs GCC 16.1.0 (x86_64-ucrt-posix-seh r4; not the supported 15.2.0 r7), Release with BUILD_TESTING=ON, NVIDIA GeForce RTX 4070 SUPER (driver 32.0.16.1714).

  • agc_denormal_flush_elimination has three new cases. All three fail against main's DenormalFlushEliminator.cpp and pass with this change.
  • Full ctest: 918 of 923 passed. The 5 failures are the Python tool tests (pr_overlap_encoding, progress_encoding, conventions_function_bodies, conventions_wrapped_declarations, check_skips), which failed to launch subprocesses with the interpreter available here (LibreOffice's bundled Python 3.12); no C++ code is involved.
  • The 440 tests matching agc|shader|vector|recompil|scalar|pixel|interp, run on the RTX 4070 SUPER, all pass, including agc_driver_f32_denormal_flush.
  • With a local counter (not part of this PR) after the eliminator, over the shaders those 440 tests compile: removed flushes 2229 → 2345, IR instructions after the pass 292,777 → 292,402 (−375). These are small test shaders; no title was run, and pipeline compile time was not measured.
  • python3 tools/check_conventions.py --base origin/main and git diff --check pass.

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
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • 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
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

The denormal flush eliminator now treats these values as denormal-free:
- floor, ceil, trunc and round-to-even results, which are integral, zero,
  infinite or NaN;
- square roots and reciprocal square roots, whose inputs the backend flushes
  first and whose results stay out of the denormal range;
- min, max, min3, max3 and med3 of denormal-free operands, which the backend
  lowers to a selection of operand bits;
- a value masked to its sign bit, and a value ORed with a constant that has an
  exponent bit set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Oct 12, 2026
@github-actions

Copy link
Copy Markdown

Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

@github-actions github-actions Bot closed this Oct 12, 2026
@oneandonlydean oneandonlydean removed the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Oct 17, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 17, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed shader core/shader agc libSceAgc* labels Oct 17, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for reopening this. It no longer merges cleanly into current main. Could you rebase onto current main? Note that main already skips the denormal flush for floor/ceil/min/max, so after the rebase this PR only needs the cases main doesn't cover yet. We'll review and run full CI on the new head.

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 18, 2026
@xXPBSXx

xXPBSXx commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Rebase of this PR onto current main, offered because it has been quiet since the 17.10. rebase request (happy to drop it if the author updates the PR): branch xXPBSXx:fix/denormal-flush-more-cases (da67f279f1, one commit, authorship kept, Friesmaniac and Claude as co-authors).

As the review noted, main already skips the flush for floor/ceil/min/max/med, the sign-bit masks (clearsSignificand) and phis. The rebase keeps only what main does not cover yet: FPTrunc32, FPRoundEven32, FPSqrt, FPRecipSqrt32 (their results cannot be denormal) and a BitwiseOr32 with an exponent bit set. FPFract32 and FPRecip32 still keep their flush, and the tests say so.

Windows 10 19045, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, merged on main 339217ebc9 (clean merge):

  • Re-run on main 339217ebc9: agc_denormal_flush_elimination passes 3 of 3 with the change and fails 3 of 3 with main's eliminator. The wider f32 test family was not re-run.
  • tools/check_conventions.py clean; merge-tree clean; pr_overlap only compares against this PR itself.
  • Not tested: GPU runs, any title.

AI-assisted (Claude Code).

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

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 8958 core/libs/prx/libSceAgcDriver/tests/DenormalFlushElimination.cpp
core/shader/recompiler/Optimization/src/DenormalFlushEliminator.cpp
PR 12548 core/shader/recompiler/Optimization/src/DenormalFlushEliminator.cpp

@fogydes

fogydes commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Fresh numbers on this PR (head 9989fd08e8, main 1e20650e84), and a correction relevant to how it's framed:

  1. What this is relative to its base: patch-id 00c31881…, one commit, +74 lines across DenormalFlushEliminator.cpp + the test file, fork base d146ec1b66. All added lines of perf(shader): skip the f32 flush after results that cannot be denormal #8958 (39) appear among this PR's added (70) — perf(shader): skip the f32 flush after results that cannot be denormal #8958 is a strict content subset of this change; the extra here is the min/max/min3/max3/med3 all-operands rule plus a third test. [verified by patch-id + line-set comparison]
  2. Main meanwhile: 69d0dc22 already merged the floor/ceil/min/max/med3 denormal-free piece (main DenormalFlushEliminator.cpp:106–137), so after a rebase the extra-but-over-main delta of this PR shrinks to roughly what perf(shader): skip the f32 flush after results that cannot be denormal #8958 carries plus the third test. Main still lacks setsExponentBit, the FPTrunc32/FPRoundEven32/FPSqrt/FPRecipSqrt32 rules and the BitwiseOr32 exponent rule — those are the live contribution of both open PRs. [verified by reading main's file + grep]
  3. Stale/colliding: 9,868 commits behind base; git merge-tree origin/main pr-4278 conflicts in both of its changed files, while perf(shader): skip the f32 flush after results that cannot be denormal #8958 merges cleanly into current main; git merge-tree pr-4278 pr-8958 conflicts in both shared files (mutually unmergeable). The rule-checking itself re-verifies sound on the branch source: sqrt ≥2^-63 and reciprocal-sqrt of the max normal ≈2^-64 cannot be denormal; and 0x80000000 → ±0; or with exponent-bit constant can't sub-normal. [verified on the branch text]

So this needs an explicit maintainer pick between the two open branches — rebase this one (superset + third test) and close #8958, or merge clean-merging #8958 now and close this one. Last branch commit here is dated 2026-10-11 with nothing pushed since, which keeps this the stale side of the pair. Pull any updated version forward if you intend to keep this route.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@Friesmaniac This PR now conflicts with main in core/libs/prx/libSceAgcDriver/tests/DenormalFlushElimination.cpp, core/shader/recompiler/Optimization/src/DenormalFlushEliminator.cpp. Main’s denormal-flush analysis and regression cases overlap this PR’s opcode and exponent-bit reasoning. Could you reconcile the non-denormal proofs and their tests with the analysis now on main?

oneandonlydean pushed a commit that referenced this pull request Nov 1, 2026
Rounding to an integral value, a square root, a reciprocal square root
and an OR with an exponent bit cannot produce a denormal, so the flush
after them is redundant. Rebased onto current main from #4278, keeping
only the cases main does not cover yet.

Co-Authored-By: Friesmaniac <181011464+Friesmaniac@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* needs rebase Conflicts with main, rebase needed 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.

4 participants