Skip to content

fix(shader): flush f64 floor, ceil, fract and frexp denormal inputs - #7036

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
thuanquang:fix/f64-rounding-denormal-flush
Oct 19, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
thuanquang:fix/f64-rounding-denormal-flush

Conversation

@thuanquang

Copy link
Copy Markdown
Contributor

What

v_floor_f64, v_ceil_f64, v_fract_f64, v_frexp_mant_f64 and v_frexp_exp_i32_f64 ignored the FLOAT_MODE f16/f64 denormal field (bits 7:6). On the GPU:

  • with bit 6 clear (modes 0 and 2) a denormal input is flushed to a zero of its sign first, so v_ceil_f64 2^-1074 is 0 where main gives 1.0, v_floor_f64 -2^-1074 is -0 where main gives -1.0, v_fract_f64 -2^-1074 is 0 where main gives 0x3fefffffffffffff, and both frexp forms give 0;
  • with bit 7 clear (modes 0 and 1) v_fract_f64 flushes the denormal result it gives for a positive denormal input to +0.

float64Unary now flushes the source of those four opcodes, and the v_fract_f64 result, through a new flushF64Bits (the f64 counterpart of flushF16Bits, by the same keep bits); float64Operation flushes the source of v_frexp_exp_i32_f64. v_trunc_f64 and v_rndne_f64 give the same results with or without the flush and are unchanged. Modes that keep denormals, and shaders without a known float mode, are translated as before. The other f64 arithmetic still ignores the field; min/max and the compares are covered by #6760 and #6850, which this PR does not touch.

Measured

The same instructions were run directly on the GPU with the tools/hw-oracle template, launched through the driver's HIP runtime, and through the recompiler on Vulkan, at IEEE 0 and 1, over 23 f64 values (±0, ±2^-1074, ±0x000fffffffffffff, 0x0008000000000000, ±2^-1022, 0x0010000000000001, ±0.5, ±1, 0x3feffffffffffffe, 0x3ff0000000000001, 2^-52, 2^-1021, 0x7fefffffffffffff, ±inf, a quiet and a signaling NaN):

f64 denormal mode main vs GPU (floor / ceil / fract / frexp_mant / frexp_exp) this change vs GPU
0 2 / 3 / 5 / 5 / 5 of 23 0 each
1 0 / 0 / 3 / 0 / 0 of 23 0 each
2 2 / 3 / 5 / 5 / 5 of 23 0 each
3 0 each 0 each

With this change every mode also matches for -v[4:5] (floor, frexp_mant, frexp_exp), |v[4:5]| (ceil) and -|v[4:5]| (fract) over 16 of those values, and v_trunc_f64 and v_rndne_f64 match before and after.

Tested

Windows 11 26200, AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, ANYPS5_REQUIRE_VULKAN=1:

  • New agc_driver_f64_rounding_denormal_modes: the five instructions over 18 finite values (zeros, denormals, minimum normals, 0.5, -1.5, 2.75, values next to ±1, ±0x7fefffffffffffff and 2^52 + 1) in f64 denormal modes 0 to 3 at IEEE 0 and 1 and without a known float mode, against std::floor, std::ceil, std::frexp and min(x - floor(x), 0x3fefffffffffffff) of the flushed input, with the fract result flushed when bit 7 is clear. That model matched the GPU on those 18 values in every mode. On main it fails: v_ceil_f64 f64 denormal mode 0, IEEE mode on of 0x0000000000000001 is 0x3ff0000000000000, expected 0x0000000000000000. With this change it passes.
  • Full ctest --parallel 4 --timeout 120 on main 6e7ba928a with this change: 1368 tests, 2 fail, both failing on main on this GPU too: agc_driver_lds_mixed_width_atomics (agc_* tests: 3 of 483 fail on AMD Radeon RX 6400, pass on NVIDIA RTX 4080 SUPER #5271) and agc_driver_layered_volume_vertex_no_gpl (AMD Windows: GPL-mode cache failures (alpha/wave32 pass after local driver update) #4768). agc_driver_f64_arithmetic, agc_driver_f64_conversions and the other f64 tests pass.
  • python tools/check_conventions.py --base origin/main with this title and body passes.
  • Not tested: the PS5's gfx1013, other AMD drivers, NVIDIA, Intel, and titles.

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 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
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Full CI on 5bc68b1 (run 39012021072): Linux fails the test this PR adds, agc_driver_f64_rounding_denormal_modes (llvmpipe, LLVM 20.1.2):

AGC graphics: v_frexp_mant_f64 without a known float mode of 0x0000000000000001 is 0x3fe0000000000001, expected 0x3fe0000000000000

Windows shows success only because its runner has no Vulkan device, so the test is skipped there; Linux is the only CI job that runs it. Could you look at the frexp path when the float mode isn't known?

@github-actions

github-actions Bot commented Oct 17, 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 4072 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
PR 5021 docs/dev/debt/shader.md
PR 5371 docs/dev/debt/shader.md
PR 5426 docs/dev/debt/shader.md
PR 5500 docs/dev/debt/shader.md
PR 5941 docs/dev/debt/shader.md
PR 6009 docs/dev/debt/shader.md
PR 6235 docs/dev/debt/shader.md
PR 6280 docs/dev/debt/shader.md
PR 6412 docs/dev/debt/shader.md
PR 6488 docs/dev/debt/shader.md
PR 6604 docs/dev/debt/shader.md
PR 6924 docs/dev/debt/shader.md
PR 6977 docs/dev/debt/shader.md
PR 7223 docs/dev/debt/shader.md
PR 7332 docs/dev/debt/shader.md
PR 7392 docs/dev/debt/shader.md
PR 7395 docs/dev/debt/shader.md
PR 7484 docs/dev/debt/shader.md
PR 7684 docs/dev/debt/shader.md

@LeandroLP23

Copy link
Copy Markdown
Contributor

Another AMD driver, since the description lists other AMD drivers as untested.

Windows 11 (26300), Radeon 860M (RDNA 3.5, 1002:1114), driver 32.0.13058.8001, WinLibs GCC 15.2.0, Release, ANYPS5_REQUIRE_VULKAN=1. Tree: main 17cfb1f4c with #7018, #7034, #7036, #7038, #7040, #7041 and #7042 merged together (only docs/dev/debt/shader.md conflicted, for #7040); "main" is the same tree with the six recompiler files reverted to main and the tests kept.

AI-assisted: yes (Claude Code).

@jotitadev jotitadev 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.

An NVIDIA run, and the full list of rows that fail on lavapipe, for the CI failure @oneandonlydean reported.

Machine: Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON. NVIDIA GeForce RTX 3070 (driver 595.99.02) and lavapipe (Mesa 26.0.3, LLVM 21.1.8) on the same host. PR merged onto main 12a71f3f5 (clean). ANYPS5_NO_SHADER_CACHE=1, -j1 --timeout 30.

Run RTX 3070 lavapipe
^agc_driver_f64 (10 tests) pass 3/3 9/10; agc_driver_f64_rounding_denormal_modes fails with the CI message: v_frexp_mant_f64 without a known float mode of 0x0000000000000001 is 0x3fe0000000000001, expected 0x3fe0000000000000
new test, Float64Instructions.cpp/SubwordPacking.cpp/TranslationContext.hpp from main fails: v_ceil_f64 f64 denormal mode 0, IEEE mode on of 0x0000000000000001 is 0x3ff0000000000000, expected 0x0000000000000000 fails

To see every failing row and not only the first one, I changed the test's Require locally to log and count instead of throwing:

  • RTX 3070: 0 mismatches in all 9 modes.
  • lavapipe: 40 mismatches. All are v_frexp_mant_f64 and v_frexp_exp_i32_f64 of a denormal input, in the modes that keep f64 denormals (1, 3 and no known float mode), 8 per mode. floor, ceil and fract match in every mode, and in modes 0 and 2 (input flushed first) frexp matches too. The rows for no known float mode:
input frexp_mant lavapipe / expected frexp_exp lavapipe / expected
0x0000000000000001 0x3fe0000000000001 / 0x3fe0000000000000 -1022 / -1073
0x000fffffffffffff 0x3fefffffffffffff / 0x3feffffffffffffe match
0x0008000000000000 0x3fe8000000000000 / 0x3fe0000000000000 match

(plus the negative inputs, same pattern). These rows translate as on main, so this is lavapipe's f64 frexp not normalizing a denormal input (it reads the mantissa bits as if the exponent were -1022), not this PR's flush.

One note on gating these rows on shaderDenormPreserveFloat64: the 3070 reports it false, the same as this lavapipe, and passes all of them. A gate on that feature would skip the kept-denormal rows on NVIDIA too, and the floor/ceil/fract rows that lavapipe gets right. On this lavapipe only the frexp rows with a denormal input need to be skipped.

Not run: Windows, AMD, any title.

GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02) and lavapipe (Mesa 26.0.3), Ubuntu 26.04

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@Dextroid17

Copy link
Copy Markdown
Contributor

@jotitadev's review found only the frexp rows failing on lavapipe with Mesa 26.0.3, and suggested skipping just those because the RTX 3070 reports shaderDenormPreserveFloat64 false and still passes every row. On a newer lavapipe that narrower skip is not enough.

Linux, lavapipe (Mesa 26.2.4, LLVM 23.1.1, GALLIUM_OVERRIDE_CPU_CAPS=avx as in CI), Release, PR head 5bc68b1cc. With the test's Require changed locally to log and count instead of throwing, there are 114 mismatches. All are in the modes that keep f64 denormals (no known float mode, 1 and 3), none in modes 0 and 2. Besides frexp, floor, ceil and fract of a denormal input fail here too, for example v_ceil_f64 without a known float mode of 0x0000000000000001 is 0x0000000000000000, expected 0x3ff0000000000000. With Float64Instructions.cpp, SubwordPacking.cpp and TranslationContext.hpp from main and the test kept, the same 114 kept-mode rows fail, row for row. So these rows depend on the device, not on this PR's change.

Lavapipe hard-codes shaderDenormPreserveFloat64 = false (lvp_device.c, line 1043 at mesa-25.2.8, CI's version, and line 1122 at mesa-26.2.4), so these shaders get no DenormPreserve 64, and Vulkan then allows the driver to flush f64 denormals. Which rows fail changes with the Mesa version, so a list of rows to skip would need to follow it.

This is the gate @oneandonlydean suggested on #7042, applied per mode with the same float64DenormPreserve check F64FmaSubnormals.cpp uses:

+        const bool canPreserveDenormals = device->Target().float64DenormPreserve;
         for (const Mode& mode : modes) {
+            const bool keepsDenormals = !mode.value.has_value() || ((mode.value->floatMode >> 6u) & 1u) != 0u;
+            if (keepsDenormals && !canPreserveDenormals) {
+                std::cout << "skipped " << mode.name << ", the device does not report f64 DenormPreserve\n";
+                continue;
+            }
             Run(*device, mode);

ctest -R '^agc_driver_f64_rounding_denormal_modes$', 3 runs each, lavapipe:

                         this PR + gate    the three files from main + gate
result                   3/3 pass          3/3 fail

The failure in the second column is v_frexp_mant_f64 f64 denormal mode 0, IEEE mode on of 0x0000000000000001 is 0x3fe0000000000001, expected 0x0000000000000000, so with the gate the test still fails without the fix, in modes 0 and 2.

The cost is the one @jotitadev named: on a device that reports the feature false but keeps denormals, like the RTX 3070, the kept-mode rows are skipped too. Keeping them there would need the recompiler to compute these results from the bits when DenormPreserve is not available, or the rows to report a mismatch as a skip instead of a failure on such devices. I have not tried either.

Not tested: Windows, NVIDIA, or any AMD GPU.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

thuanquang and others added 2 commits October 18, 2026 01:54
v_floor_f64, v_ceil_f64, v_fract_f64, v_frexp_mant_f64 and
v_frexp_exp_i32_f64 ignored the FLOAT_MODE f16/f64 denormal field. On
RDNA2 (gfx1031) bit 6 clear flushes a denormal input to a zero of its
sign, so v_ceil_f64 of 2^-1074 is 0 instead of 1, and bit 7 clear
flushes the denormal result v_fract_f64 gives for a positive denormal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lavapipe reports no f64 DenormPreserve, so the shaders get no
DenormPreserve 64 and it may flush f64 denormals; the rows of modes that
keep them then fail with or without the recompiler change (frexp, floor,
ceil and fract of 2^-1074 on Mesa 25 and 26). As in F64FmaSubnormals,
a mismatch in those modes is reported as skipped on such a device. The
modes that flush f64 denormals are still checked on every device.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thuanquang
thuanquang force-pushed the fix/f64-rounding-denormal-flush branch from 5bc68b1 to b55d411 Compare October 17, 2026 18:58
@thuanquang

Copy link
Copy Markdown
Contributor Author

Thanks for the lavapipe runs. b55d411 adds the gate, with the rule F64FmaSubnormals.cpp uses: on a device whose target reports no f64 DenormPreserve, a mismatch in a mode that keeps f64 denormals (1, 3 and no known float mode) is printed as skipped instead of failing; modes 0 and 2, the ones this change fixes, are checked on every device. Rows that pass are still checked, so a device like the RTX 3070 keeps covering the kept modes. The branch is rebased onto main f219248f0.

RX 6700 XT, Windows: the test passes with no skipped mode, and with core/shader/recompiler from main it still fails: v_ceil_f64 f64 denormal mode 0, IEEE mode on of 0x0000000000000001 is 0x3ff0000000000000, expected 0x0000000000000000. I have not run lavapipe here.

@jotitadev jotitadev 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.

A rerun of the gate in b55d41151 on the two devices from my earlier run. It does what the commit says on both: the RTX 3070 still checks every kept-denormal row, and lavapipe skips only the rows it gets wrong.

Machine: Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON. NVIDIA GeForce RTX 3070 (driver 595.99.02) and lavapipe (Mesa 26.0.3, LLVM 21.1.8) on the same host. #7042 at 3221feb5a, which carries this PR's head b55d41151, merged onto main 5de7a46e0 (clean). ANYPS5_NO_SHADER_CACHE=1, -j1 --timeout 30.

RTX 3070 lavapipe
^agc_driver_f64 (11 tests) 11/11, 3 runs 11/11, 2 runs
skipped lines in agc_driver_f64_rounding_denormal_modes none 5: no known float mode and modes 1 and 3 (IEEE on/off), all on v_frexp_mant_f64 of 0x0000000000000001
same, with Float64Instructions.cpp, SubwordPacking.cpp and TranslationContext.hpp from main fails: v_ceil_f64 f64 denormal mode 0, IEEE mode on of 0x0000000000000001 is 0x3ff0000000000000, expected 0x0000000000000000 fails with the same message, after the same skip for no known float mode

Both devices report shaderDenormPreserveFloat64 = false. On the 3070 none of the kept-mode rows mismatch, so nothing is reported as skipped and those rows are still checked there. That answers the point from my earlier comment. Modes 0 and 2 are checked on both devices and still catch the missing flush.

Not run: Windows, AMD, a newer Mesa.

GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02) and lavapipe (Mesa 26.0.3), Ubuntu 26.04

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

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

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants