fix(shader): translate f32 add and multiply in f32 denormal modes 1 a… #6010
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6010"
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 zmeizy on 2026-10-14T19:13:50Z.
What
v_add_f32,v_sub_f32,v_subrev_f32,v_mul_f32andv_mul_legacy_f32threwf32 denormal mode N ... is not implementedin shaders whoseFLOAT_MODEf32 denormal field (bits 5:4) is 1 or 2. On RDNA2, with f32 round to nearest even:0 + 2^-149gives+0, and0x00800001 - 0x00800000gives+0.0x00800001 - 0x00800000gives0x00000001.Changes:
f32DenormalFlushForreturns the result flush alone for mode 1 and the input flush alone for mode 2, when the f32 round mode is to nearest even.writeOperandapplied the result flush only when the input flush bit was set too:flushF32Denormalreturned early on bit 0. It now checks the result flush bit for the result.mainnothing else uses a result-only flush, so modes 0 and 3 are unchanged.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):v_mul_f32andv_mul_legacy_f32.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.mdrecords it. The general "f32 denormals" entry is left unchanged because #5782 edits that line.Tested
Ubuntu 24.04 (WSL2), GCC 13.3, lavapipe, on
main73efd940.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.mainit fails (mode 1 throws).f32DenormalFlushForpart it also fails:v_add_f32of0x00000000and0x00000001gives0x00000001in mode 1 instead of0x00000000.v_mullit_f32in the same modes, whose mode 1 also takes the result flush now): it merges cleanly,agc_driver_mullit_f32_denormal_modespasses, and all 110,592 measuredv_mullit_f32rows still match.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_denormalsandagc_driver_vop3_add_sub_clamp.anyps5-gpu-probes-v2-gfx1030.json.gz: probeadd_f32_denormal_modes.anyps5-gpu-probes-v3-gfx1030.json.gz: probemul_tiny_denormal_modes.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
anyps5-gpu-probes-v3-gfx1030.json.gz
anyps5-gpu-probes-v2-gfx1030.json.gz
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T20:33:23Z.
Review: COMMENTED
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-portatb7eb6b6b(mugurc/AnyPS5) + ImportWatched + this PR's diff (merge-base..head, withoutdocs/), notmain, becausemaindoes 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). Headebf9fcadc; the diff applies cleanly.agc_driver_f32_add_mul_denormal_modesfails on both drivers. The first failure isv_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.Requireturned 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.0x3f7ffffe * 0x00800001, whose exact value is below 2^-126 and rounds to it. Mode 1 gives 0 here, where the rows expect0x00800000. That is the same case thatagc_driver_f32_constant_factor_tininessalready fails on this device without the PR.f32 denormal mode 1 at pc 20 is not implemented, so the test runs here.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.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T21:05:03Z.
Review: CHANGES_REQUESTED
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_modesfails on RADV (Mesa 26.2.4, Radeon 660M), 3 runs out of 3. The first failure isv_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 examplev_sub_f32 0x00800002, 0x00800000gives0x00000000instead of0x00000002. It passes on CI's lavapipe only. RADV flushes f32 denormals unless the shader requestsDenormPreservefor 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.OpCapability DenormPreserveplusOpExecutionMode DenormPreserve 32. Gate it on the device'sshaderDenormPreserveFloat32, the same way f16 already does it (float16DenormPreserveinRecompiler.hpp,EmitF16DenormPreserveinSpirvModuleEmitter.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 withoutshaderDenormPreserveFloat32rather than fail.Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-19T03:08:11Z.
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=ONwith every execution test built. This head (ebf9fcadc) merged intomainad502d717.agc_driver_f32_add_mul_denormal_modesfails 3 of 3 runs at its first mismatch:flushF32Denormalkeeps 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 emitsDenormPreserveonly for 16 and 64 bits, and this driver cannot preserve f32 denormals on request anyway.vulkaninforeports: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.pyselects behave as onmainon this GPU: the other failures areagc_driver_bda_device(#7584),agc_driver_image_table_reuse(#7710),agc_driver_pixel_lds(#7708) and a load-dependentagc_driver_storage_file_writethat passes when rerun alone.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-21T17:19:26Z.
Thanks for the measurements and the direction. Updated in 4418689176, rebased onto main
cecb540a, 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.
Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-21T17:19:43Z.
anyps5-gpu-probes-v4-gfx1030.json.gz