Skip to content

fix(shader): flush v_rcp_f32 denormal results to a signed zero - #22309

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
NWProgramming00:fix/shader-rcp-f32-denormal-result
Nov 26, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
NWProgramming00:fix/shader-rcp-f32-denormal-result

Conversation

@NWProgramming00

@NWProgramming00 NWProgramming00 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

On RDNA2, v_rcp_f32 and v_rcp_iflag_f32 return a zero of the result's sign whenever the reciprocal is below 2^-126, that is for every input above 2^126 in magnitude. This holds in both f32 denormal modes of FLOAT_MODE (flushed, 0xc0, and kept, 0xf0), at IEEE=0 and IEEE=1, and also through a negated VOP3 source. The recompiler kept the driver's denormal 1/x, so on lavapipe (and on any driver that keeps f32 denormals) v_rcp_f32(0x7f7fffff) gave 0x00200000 where the hardware gives 0x00000000.

unaryFloatSpecials already treats a denormal input of these ops as zero. This adds the matching result flush for FPRecip32 and FPRecipIFlag32: a result below 2^-126 in magnitude becomes the zero of its sign. It covers the default path and the APS5_EXACT_RCP path, which passes non-normal results to the driver's 1/x.

v_rcp_f16 already matches the hardware in both f16 denormal modes, and the extra check cannot fire for it: its f32 intermediate is never denormal. The pixel shader's position W reciprocal does not go through unaryFloatSpecials and is unchanged.

Measurement: hardware oracle on an AMD Radeon Graphics iGPU (Ryzen 7 9700X, gfx1036), AMD Windows driver 32.0.21042.62, HIP runtime. The same body and rows run through agc_oracle_runner (lavapipe) showed the difference. The PS5's gfx1013 is not measured.

input hardware v_rcp_f32 before after
0x7e800000 (2^126) 0x00800000 0x00800000 0x00800000
0x7e800001 0x00000000 0x007fffff 0x00000000
0x7f000000 0x00000000 0x00400000 0x00000000
0x7f7fffff 0x00000000 0x00200000 0x00000000
0xff7fffff 0x80000000 0x80200000 0x80000000

Before the fix the oracle shows the difference in all four combinations of f32 denormal field (0 and 3) and IEEE mode, so titles on the default flushed mode are affected too. The hardware flushes the result before the output modifier and clamp: with the fix, mul:2, mul:4, div:2, clamp and |x| forms over 32 inputs between 2^124 and 2^128 match the hardware in every mode, except the documented 1-ULP approximation rows and one case this change does not touch. With f32 denormals kept and IEEE mode off, gfx1036 applies the output modifier to v_rcp_f32 where the translation ignores it, before and after this change.

The new execution test RcpDenormalResult pins 32 measured rows for v_rcp_f32, v_rcp_iflag_f32 and v_rcp_f32_e64 with a negated source, under FLOAT_MODE 0xc0 and 0xf0. The rows are the denormal range plus the normal boundary (0x7e800000, 0x7e7fffff, 0x7e000001), zeros, infinities and denormal inputs. The default lowering is the driver's 1/x, which Vulkan allows 2.5 ulp of error. A result other than the RDNA 2 value is therefore accepted if it has the sign of 1/x and is either:

  • a normal value within 2.5 ulp of the exact reciprocal, the bound main's float probe uses for rcp; or
  • a signed zero, when that bound reaches below 2^-126.

A denormal result always fails. I checked the rule on the host against the table:

  • every normal cell moved by ±1 and ±2 ulp is accepted;
  • all 48 denormal results the unfixed lowering produces are rejected;
  • 2^-126 instead of zero is accepted only for ±0x7e800001, whose exact reciprocal is a fraction of an ulp below 2^-126.

I assembled the kernel words with AMD's comgr for gfx1036.

The debt fragment adds the measured behaviour. It also records that v_rcp_f64 still keeps a denormal result with f64 denormals flushed, where the same GPU returns a signed zero. That case is left for a separate change.

Tested

Linux (WSL2 Ubuntu 24.04), GCC, Release, lavapipe. Rebased on main 9b7afdd507, where the full ctest -j 14 gives 5997 of 6002. The failures are the same five tests as below; guest_audio_in_status_reroute_shared_bounds passes 3 of 3 runs on its own. Before the rebase, on main e51ae59fb4:

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: lavapipe (llvmpipe, LLVM 20.1.2, Mesa) on Linux (WSL2) for the GPU tests; the measurement is from an AMD Radeon Graphics iGPU (gfx1036, Ryzen 7 9700X), AMD Windows driver 32.0.21042.62, Windows 11
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

RDNA2 returns a zero of the result's sign from v_rcp_f32 and
v_rcp_iflag_f32 whenever the reciprocal is below 2^-126, in every
FLOAT_MODE f32 denormal mode and IEEE mode. The recompiler kept the
driver's denormal 1/x, so inputs above 2^126 in magnitude gave a
denormal where the hardware gives zero.

Measured on gfx1036 with the hardware oracle; the new execution test
pins those rows for FLOAT_MODE 0xc0 and 0xf0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The default v_rcp_f32 lowering is the driver's 1/x, which Vulkan allows
2.5 ulp of error. A result other than the RDNA 2 value now passes when
it is a normal value within that bound of the exact reciprocal, or a
signed zero when the bound reaches below 2^-126; a denormal result
still fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@domx4q

domx4q commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Ran the new test and the existing rcp execution tests with the Khronos validation layer on Linux and an NVIDIA GPU; the posted runs are lavapipe only.

Setup: Arch Linux (kernel 7.3.4), GCC 16.2.1, Release, NVIDIA GeForce RTX 4090 (vendor 0x10de, device 0x2684, Ada, subgroup 32), proprietary driver 620.70.06, Vulkan 1.4.357, Khronos validation layer 1.4.363.1. Head a1efda1d9f merged onto main f0c01bc69d with --no-ff, clean merge. Main baseline is a Release build of f0c01bc69d on the same machine.

ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer \
ctest --test-dir build -R '^agc_driver_(.*rcp.*)$' -V
main f0c01bc69d this PR
Result 6 of 6 passed 7 of 7 passed
Insert instance layer "VK_LAYER_KHRONOS_validation" 6 7
was not found but was requested by env var VK_INSTANCE_LAYERS 0 0
VUID- none none

agc_driver_rcp_denormal_result is new and does not exist on main; it passes (rcp denormal result tests passed). The driver's probe reports f32 denormal flush: hardware on this device. The six existing tests (agc_driver_rcp_hardware_exact and the five agc_driver_sdwa_*rcp* tests) pass on both sides, and every test selected the RTX 4090.

No new VUID- errors; all focused tests pass on Ada.

AI-assisted: yes (Claude Code)

@Colewiz7

Copy link
Copy Markdown
Contributor

Head a1efda1d9f merged onto main e825fa1492 as d4d79a10ea, no conflict. Linux 7.3.4, GCC 16.2.1 Release, Mesa 26.2.6 RADV, RX 7900 XT (gfx1100) and 7800X3D iGPU (gfx1036).

No new failures and no new validation messages on either GPU.

RX 7900 XT iGPU
tests run (6356 on the 7900 XT, the 2037 GPU ones on the iGPU), new failures vs main none none
-R '^agc' ( tests) with the layer, new failures vs main none none
VUID- / SYNC-HAZARD lines 0 / 0 0 / 0

Layer runs used VK_LAYER_KHRONOS_validation 1.4.363.1 with VK_LOADER_DEBUG=layer and ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1; the loader inserted the layer in every run, and the set of distinct validation messages matches main.

AI-assisted: yes

@AbuMshMsh24

Copy link
Copy Markdown
Contributor

AMD driver data for this, on RDNA4 with AMD's Windows driver: the new test passes with and without the fix here, so this driver already returns the RDNA 2 values.

Setup: Windows 10 22H2, AMD Radeon RX 9070 XT (RDNA4, 0x1002:0x7550), AMD Software 26.11.2 (32.0.32029.2010), WinLibs GCC 15.2.0 r7, Release, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, Khronos validation layer 1.4.363.1 inserted. Head a1efda1d9f merged onto main 42f6354612 with --no-ff (clean).

agc_driver_rcp_denormal_result agc_driver_rcp_hardware_exact
this PR 3 of 3 pass pass
this PR with FloatInstructions.cpp from main (no fix) 3 of 3 pass not run
main (test not present) pass
  • Without the fix, every case already matches the measured value exactly or within the test's 1/x error, so the driver's native 1/x flushes these results to a zero of the result's sign, as RDNA 2 does. lavapipe's 0x007fffff in the description does not happen here.
  • So on this driver the added select changes no result; it costs one compare and select per v_rcp.
  • 0 VUID- lines in any log.

AI-assisted: yes (Claude Code)

@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 reciprocal result flush and its execution coverage are consistent with the reported RDNA2 behavior; no blocking defect was verified in the changed code.

What I ran

Reviewed head a1efda1d9f0f1f9f1fba176718b7024e47dbeb63. No build or tests were run. I read description.md, commits.md, manifest.json, diff.patch, the full text of all three changed files, and targeted surrounding code in FloatInstructions.cpp. All files marked reviewed in the manifest were read; there were no excluded changed files.

Results

Check Result Note
Diff and changed files passed All three changed files read
Targeted caller context passed Unary lowering path checked
Tests not run No commands executed
PS5 hardware behavior not run Description says gfx1013 was not measured

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build/test execution and behavior on PS5 gfx1013 were not verified. The hardware measurements and test runs in the description are author-reported, not runs performed in this review.

To get this to a good state

Nothing is required.

@Script-hpp

Copy link
Copy Markdown
Contributor

Tested on Intel Arc B390 (Mesa 26.2.7 ANV, Linux 7.2.10), PR head a1efda1d9f merged into main 8c254da48b, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (layer 1.4.341, the loader log shows it inserted) and VK_LOADER_DEBUG=layer:

  • ctest -R '^agc_driver_rcp_denormal_result$' -V: pass, no VUID- in the log.
  • Control: the test alone on main fails on this device with v_rcp_f32 of 0x7e800001 with FLOAT_MODE 0x000000f0 is 0x007fffff, expected the RDNA 2 value 0x00000000 or a normal result within the device's 1/x error. So the test catches the bug on this device too.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit 7967159 into boykopovar:main Nov 26, 2026
16 of 18 checks passed
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.

7 participants