Skip to content

fix(shader): translate f32 add and multiply in f32 denormal modes 1 a… - #6010

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
zmeizy:claude/add-mul-f32-denormal-modes
Oct 24, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
zmeizy:claude/add-mul-f32-denormal-modes

Conversation

@zmeizy

@zmeizy zmeizy commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

What

v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32 and v_mul_legacy_f32 threw f32 denormal mode N ... is not implemented in shaders whose FLOAT_MODE f32 denormal field (bits 5:4) is 1 or 2. On RDNA2, with f32 round to nearest even:

  • Mode 1 keeps denormal inputs and flushes a result below 2^-126 to a zero of its sign.
    • 0 + 2^-149 gives +0, and 0x00800001 - 0x00800000 gives +0.
    • As in mode 0, a product is flushed only when it is still below 2^-126 after rounding to 24 bits with an unbounded exponent.
  • Mode 2 flushes denormal inputs to zeros of their sign and keeps a denormal result: 0x00800001 - 0x00800000 gives 0x00000001.

Changes:

  • f32DenormalFlushFor returns the result flush alone for mode 1 and the input flush alone for mode 2, when the f32 round mode is to nearest even.
  • writeOperand applied the result flush only when the input flush bit was set too: flushF32Denormal returned early on bit 0. It now checks the result flush bit for the result.
    • Products did not need this, because the tiny-product check already reads the result bit. Sums with kept denormal inputs did.
    • On main nothing else uses a result-only flush, so modes 0 and 3 are unchanged.
  • With a directed f32 round mode, modes 1 and 2 keep throwing. The host rounds these sums and products to nearest even in every mode, and the measurement shows that this already differs from the hardware in modes 0 and 3 with a directed round mode. That is recorded in the new debt entry and left for a separate change.

Source of semantics: measured with the hardware oracle's kernel template on gfx1030 (AMD Radeon RX 6800, AMD Windows driver 32.0.21045.5002 through its HIP runtime amdhip64_6.dll):

  • Rows, set 1: 1,296 operand pairs over 36 values, both signs: zeros, 2^-149 to 3 * 2^-149, 0x00400000, 0x007fffff, 0x00800000 to 0x00800002, 0x00ffffff, 0x01000000, 0x01000001, 2^-24, 2^-23, 1, 0x7f7fffff, infinity and a quiet NaN. All five instructions.
  • Rows, set 2: 896 products around 2^-126, from 16 by 14 magnitudes with all four sign combinations. These include 0x3f7ffffe, 0x3f7fffff and 0x3f800001 times 0x00800000 to 0x00800002, 0x007fffff and 0x00400000. v_mul_f32 and v_mul_legacy_f32.
  • Float modes: ieee 0 and 1, each with denorm32 0 to 3 and round32 0 to 3 (32 modes); dx10_clamp 1, denorm16 3, round16 0, fp16_overflow 0.
  • Model: the exact result, rounded in the f32 round mode, with the input and result flushes of the denormal field, tininess after rounding for products, and a +0 product for a zero factor of v_mul_legacy_f32. It matches every row without a NaN or infinity input, in all 32 modes. Tininess before rounding misses 12 products in mode 1 (and 4 in mode 0) at round to nearest even.

A new entry in docs/dev/debt/shader.md records it. The general "f32 denormals" entry is left unchanged because #5782 edits that line.

Tested

Ubuntu 24.04 (WSL2), GCC 13.3, lavapipe, on main 73efd940.

  • agc_driver_f32_add_mul_denormal_modes (new) pins measured rows in modes 1 and 2 at IEEE 0 and 1, round to nearest even, with one dispatch per mode. The rows are 40 pairs from set 1 for all five instructions, and 24 products from set 2, including the 12 that separate tininess before and after rounding.
    • On main it fails (mode 1 throws).
    • With only the f32DenormalFlushFor part it also fails: v_add_f32 of 0x00000000 and 0x00000001 gives 0x00000001 in mode 1 instead of 0x00000000.
    • With the whole change it passes in 0.1 s.
  • All measured rows of both sets match the recompiler output at round to nearest even, in all four denormal modes at IEEE 0 and 1.
  • Merged with fix(shader): translate v_mullit_f32 in f32 denormal modes 1 and 2 #5941 (v_mullit_f32 in the same modes, whose mode 1 also takes the result flush now): it merges cleanly, agc_driver_mullit_f32_denormal_modes passes, and all 110,592 measured v_mullit_f32 rows still match.
  • These existing tests pass: agc_driver_f32_arithmetic_nan, agc_driver_f32_constant_factor_tininess, agc_driver_f32_denormal_flush, agc_driver_f32_output_modifier, agc_driver_f32_result_clamp, agc_driver_f32_compare_constant, agc_driver_f32_min_max_nan, agc_driver_f32_pack_rounding, agc_driver_f32_dot2_rounding, agc_driver_dx10_clamp, agc_driver_division, agc_driver_division_output_modifiers, agc_driver_division_result_modifiers, agc_driver_fma_rounding, agc_driver_fma_legacy, agc_driver_integer_to_f32_rounding, agc_driver_nan_quieting, agc_driver_vcmp_f32_denormals and agc_driver_vop3_add_sub_clamp.
  • Measurement data attached:
    • anyps5-gpu-probes-v2-gfx1030.json.gz: probe add_f32_denormal_modes.
    • anyps5-gpu-probes-v3-gfx1030.json.gz: probe mul_tiny_denormal_modes.

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: lavapipe (Mesa 25.2.8, LLVM 20.1.2) on Ubuntu 24.04 under WSL2; semantics measured on AMD Radeon RX 6800 (gfx1030, Windows driver 32.0.21045.5002)
  • AI-assisted: yes

🤖 Generated with Claude Code
anyps5-gpu-probes-v3-gfx1030.json.gz
anyps5-gpu-probes-v2-gfx1030.json.gz

@github-actions github-actions Bot added shader core/shader docs agc libSceAgc* labels Oct 14, 2026

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

Run on an Apple GPU. The new test fails here, because the device keeps neither f32 denormals nor tiny products that round up to 2^-126 (shaderDenormPreserveFloat32 = false, shaderDenormFlushToZeroFloat32 = false).

GPUs tested: Apple M2 Max, MoltenVK 1.4.2 (Vulkan 1.4.357), macOS 27.2, x86-64 under Rosetta; also MoltenVK main with SPIRV-Cross #2704 (keeps the sign of -0)

Method: macos-port at b7eb6b6b (mugurc/AnyPS5) + ImportWatched + this PR's diff (merge-base..head, without docs/), not main, because main does not run the GPU tests on macOS yet. Release, LunarG Vulkan SDK 1.4.363.0, x86-64 under Rosetta. Suite: ctest -R "^agc" -E "agc_display_buffer_dcc|agc_driver_flip|agc_driver_lds_atomics64" -j1 --timeout 30; without the PR it has 13 known macOS failures with MoltenVK 1.4.2 and 7 with the #2704 build (#1). Head ebf9fcadc; the diff applies cleanly.

  • agc_driver_f32_add_mul_denormal_modes fails on both drivers. The first failure is v_sub_f32 f32 denormal mode 1, IEEE=0 of 0x00000000, 0x00000001 is 0x00000000, expected 0x80000000: the denormal input is flushed by the device, so the result is +0, not the flushed -2^-149.
  • With the Require turned into a print locally, there are 254 mismatches with the #2704 build. MoltenVK 1.4.2 has 378: its 124 extra rows differ from the expected value only in the sign of a zero.
    • 222 have a denormal operand or result: mode 1 sums and products with kept denormal inputs, and mode 2 denormal results.
    • The other 32 are products such as 0x3f7ffffe * 0x00800001, whose exact value is below 2^-126 and rounds to it. Mode 1 gives 0 here, where the rows expect 0x00800000. That is the same case that agc_driver_f32_constant_factor_tininess already fails on this device without the PR.
  • Only the test file on the unchanged branch: it fails with f32 denormal mode 1 at pc 20 is not implemented, so the test runs here.
  • Suite: the new test is the only added failure. No other test changes, on either driver.

None of the mismatches points at the change itself, but the test can't pass on a device without f32 DenormPreserve.

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

This was referenced Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 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/Recompiler.cpp
core/shader/recompiler/Translation/include/Translation/InstructionTranslator.hpp
core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
core/shader/recompiler/Translation/src/InstructionTranslator.cpp
PR 5021 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 5371 docs/dev/debt/shader.md
PR 5609 core/shader/recompiler/Recompiler.cpp
PR 6235 docs/dev/debt/shader.md
PR 6280 docs/dev/debt/shader.md
PR 6412 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 6488 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 6539 core/shader/recompiler/Recompiler.cpp
PR 6858 core/shader/recompiler/Recompiler.cpp
PR 6924 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 6959 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 7201 core/shader/recompiler/Recompiler.cpp
PR 7223 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 7410 core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Translation/include/Translation/InstructionTranslator.hpp
core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
core/shader/recompiler/Translation/src/InstructionTranslator.cpp
core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp
docs/dev/debt/shader.md
PR 7484 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 7642 core/shader/recompiler/Recompiler.cpp
PR 7684 docs/dev/debt/shader.md
PR 8004 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 8077 docs/dev/debt/shader.md
PR 9193 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 9356 core/shader/recompiler/Recompiler.cpp
PR 9387 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 9889 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
core/shader/recompiler/Translation/src/VectorInstructions.cpp
docs/dev/debt/shader.md
PR 9969 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 10193 core/shader/recompiler/Recompiler.cpp
PR 10233 docs/dev/debt/shader.md

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the measurements. I re-measured on RDNA2 (gfx1035, IEEE 0/1, f32 denormal modes 1/2, round to nearest even). All 64 rows pinned by the test match the hardware bit for bit. Your model (tininess after rounding, +0 for a zero factor of v_mul_legacy_f32) also matches the hardware on 6,892 more rows: a 38×38 sweep and products around 2^-126. The semantics are right. The problem is that the emitted code depends on the driver:

  • agc_driver_f32_add_mul_denormal_modes fails on RADV (Mesa 26.2.4, Radeon 660M), 3 runs out of 3. The first failure is v_sub_f32 f32 denormal mode 1, IEEE=0 of 0x00000000, 0x00000001 is 0x00000000, expected 0x80000000. With every row checked instead of stopping at the first one, 222 checks fail out of 4 × 320: 91 per IEEE mode in mode 1 and 20 per IEEE mode in mode 2. Mode 2 fails too, for example v_sub_f32 0x00800002, 0x00800000 gives 0x00000000 instead of 0x00000002. It passes on CI's lavapipe only. RADV flushes f32 denormals unless the shader requests DenormPreserve for 32-bit, and this PR doesn't. So mode 1 loses the kept inputs and mode 2 loses the kept results. @jotitadev's MoltenVK run hit the same problem. "Mode 1 depends on the driver keeping denormal inputs" in the debt entry is therefore not just a caveat: AMD's Linux driver doesn't keep them.
  • Suggested direction: when the shader's f32 denormal field keeps denormals (modes 1 and 2 here), emit OpCapability DenormPreserve plus OpExecutionMode DenormPreserve 32. Gate it on the device's shaderDenormPreserveFloat32, the same way f16 already does it (float16DenormPreserve in Recompiler.hpp, EmitF16DenormPreserve in SpirvModuleEmitter.cpp). Keep throwing on devices without it. Then the test should pass on RADV and lavapipe alike. The debt entry should name the requirement, and the test can skip (77) on a device without shaderDenormPreserveFloat32 rather than fail.

@MaxLikesCode

Copy link
Copy Markdown
Contributor

The new test fails on NVIDIA: GeForce RTX 4090, driver 617.42, Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON with every execution test built. This head (ebf9fca) merged into main ad502d7.

agc_driver_f32_add_mul_denormal_modes fails 3 of 3 runs at its first mismatch:

AGC graphics: v_sub_f32 f32 denormal mode 1, IEEE=0 of 0x00000000, 0x00000001 is 0x00000000, expected 0x80000000

flushF32Denormal keeps the sign bit, so the subtraction itself must have produced +0. That is what happens if the driver flushes the denormal input 2^-149 to zero before subtracting. The recompiler emits DenormPreserve only for 16 and 64 bits, and this driver cannot preserve f32 denormals on request anyway. vulkaninfo reports:

denormBehaviorIndependence             = SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL
shaderSignedZeroInfNanPreserveFloat32  = true
shaderDenormPreserveFloat32            = false
shaderDenormFlushToZeroFloat32         = false

So mode 1's kept denormal inputs cannot come from the native add here. Lavapipe keeps them, which is presumably why it passes there. The test would need a skip on devices without shaderDenormPreserveFloat32, or the translation would need to keep denormal inputs without the host's help.

The rest of the 976 tests tools/select_tests.py selects behave as on main on this GPU: the other failures are agc_driver_bda_device (#7584), agc_driver_image_table_reuse (#7710), agc_driver_pixel_lds (#7708) and a load-dependent agc_driver_storage_file_write that passes when rerun alone.

AI-assisted: yes (Claude Code)

@zmeizy

zmeizy commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the measurements and the direction. Updated in 4418689, rebased onto main cecb540, so it now sits on #8697's DenormPreserve 32.

Gating. Modes 1 and 2 of these five instructions now translate only when the module keeps the f32 denormals the shader keeps: target.float32DenormPreserve set and the hardware f32 flush not in use. Otherwise they throw f32 denormal mode N at pc X needs DenormPreserve 32, which the device does not offer for this shader.
The flag reaches the translator through TranslateOptions::float32DenormPreserve. It was already part of the cache key and the request serialization since #8697.
Directed rounding in these modes still throws as before.
RADV. RADV passes the startup probe and takes the hardware f32 flush, and on main that flush is declared in every module, including shaders that keep f32 denormals. So on RADV these modes throw until that is fixed. claude/f32-hardware-flush-float-mode (a separate PR I'm opening) takes the hardware flush only for shaders that flush f32 denormals. With it, RADV gets DenormPreserve 32 for these shaders and runs them. This PR doesn't depend on it: without it, RADV throws instead of returning a wrong result.
Test. agc_driver_f32_add_mul_denormal_modes checks the measured values in each mode the recompiler translates. Where it throws, the test requires the device not to keep f32 denormals and the error to name DenormPreserve 32, so main's "is not implemented" still fails it. On CI's lavapipe 25.2.8, which reports no shaderDenormPreserveFloat32, it checks the throw in all four runs.
Constant factors. adriadam10's #5941 finding applies here too: flushTinyProduct skipped the tiny-product check when a factor is a constant that keeps a product of normal numbers out of the tiny range, which only holds when the inputs are flushed. It now does that only when bit 0 of the flush is set. Measured on gfx1030 with v_mul_f32 and v_mul_legacy_f32 taking 1.0, 2.0, 0.5, 1.5 (literal) and 0x3f800001 (literal) against kept denormals (1,632 rows per mode). Before the fix, mode 1 differed from the hardware on 96 rows each for the 1.5 literal; for example 0x00555555 * 1.5 gave 0x00800000 instead of 0x00000000. After it, 0 rows differ. The test pins 32 of those rows for both instructions.
Debt entry. Rewritten gap-first and under 1,000 characters. It names the DenormPreserve 32 requirement and the directed-rounding gap, which modes 0 and 3 share.
Checked on Ubuntu 24.04 (WSL2), GCC 13.3, lavapipe (Mesa 25.2.8):

Values, with preserve forced on lavapipe in the local harness: all 1,296 pairs × 5 instructions, all 896 products and all 1,632 constant-factor rows match the hardware in modes 0–3 at IEEE 0/1, round to nearest even. Without the flag, modes 1 and 2 throw the new error.
Negative control: the new test fails on main and passes with the change.
Focused tests: agc_driver_f32_add_mul_denormal_modes, f32_arithmetic_nan, f32_constant_factor_tininess, f32_denormal_flush, f32_output_modifier, f32_result_clamp, fma_rounding, nan_quieting, vop3_add_sub_clamp, request_serializer and agc_shader_memory pass with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, with no validation messages beyond main's (VUID-VkDeviceCreateInfo-pNext-pNext only).
A run on RADV (with and without the hardware-flush PR) or MoltenVK would confirm the other two paths.

@zmeizy

zmeizy commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 22, 2026
…nd 2

v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32 and v_mul_legacy_f32
follow the FLOAT_MODE f32 denormal field with f32 round to nearest
even, as measured on RDNA2 (gfx1030): mode 1 keeps denormal inputs and
flushes a denormal result, and mode 2 flushes denormal inputs and keeps
a denormal result. Both need the device to keep the f32 denormals the
shader keeps, so they translate only when the module declares
DenormPreserve 32 and throw otherwise. The result flush now applies
when only the result flush bit is set; it was skipped unless the input
flush bit was set too. A constant factor no longer skips the tiny
product check while denormal inputs are kept, as a kept denormal times
such a constant can still be tiny. Directed rounding in these modes
still throws.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zmeizy
zmeizy force-pushed the claude/add-mul-f32-denormal-modes branch from 4418689 to 6a26140 Compare October 22, 2026 20:02
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 22, 2026
@adriadam10
adriadam10 merged commit e7e99ad into boykopovar:main Oct 24, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Re-checked against current main: the head is unchanged and merges cleanly; the only change main made to Recompiler.cpp since the last review (disk-cache resource plan sharing) doesn't touch the denormal-mode hunks. Modes 1 and 2 of v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32 and v_mul_legacy_f32 translate only with DenormPreserve 32 and otherwise throw, the test pins the measured rows, and the debt entry names the requirement; merged.

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.

4 participants