fix(shader): translate f32 min and max in f32 denormal modes 1 and 2 #6009
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6009"
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:12:51Z.
What
v_min_f32,v_max_f32,v_min3_f32,v_max3_f32andv_med3_f32threwf32 denormal mode N ... is not implementedin shaders whoseFLOAT_MODEf32 denormal field (bits 5:4) is 1 or 2.On RDNA2 they follow only bit 0 of the field (input denormals):
v_min_f32 0x807fffff, 0x3f800000gives0x807fffff.f32DenormalFlushFornow returns the mode-3 flush (none) for them when bit 0 is set, and the mode-0 flush (inputs and result) otherwise. 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_min_f32andv_max_f32on the first two operands;v_min3_f32,v_max3_f32andv_med3_f32on all three.A new entry in
docs/dev/debt/shader.mdrecords it. As with mode 3, mode 1 relies on the driver keeping denormals. 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_min_max_denormal_modes(new) pins 56 measured non-NaN triples where modes 1 and 2 differ, for all five instructions in modes 1 and 2 at IEEE 0 and 1. It compiles one program per mode.mainit fails (mode 1 throws). With the change it passes in 0.1 s.agc_driver_f32_min_max_nan,agc_driver_f32_denormal_flush,agc_driver_dx10_clamp,agc_driver_f32_output_modifierandagc_driver_f32_result_clamppass.anyps5-gpu-probes-v2-gfx1030.json.gz(probeminmax_f32_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-v2-gfx1030.json.gz
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T20:33:22Z.
Review: COMMENTED
Run on an Apple GPU. The new test fails here because the device doesn't keep f32 denormals (
shaderDenormPreserveFloat32 = false), and mode 1 keeps them.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). Headc3d5a43fd; the diff applies cleanly.agc_driver_f32_min_max_denormal_modesfails on both drivers. The first failure isv_min3_f32 f32 denormal mode 1, IEEE=0 of 0x00000000, 0x00000000, 0x00000001 is 0x00000001, expected 0x00000000: the denormal compares as zero and the tie picks it.Requireturned into a print locally, there are 104 mismatches, the same on both drivers:f32 denormal mode 1 at pc 20 is not implemented, so the test runs here.On devices without DenormPreserve for f32 the mode 1 rows can't match. Skipping them there would keep the mode 2 coverage on such devices.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T01:02:54Z.
Ran this on PR head c3d5a43fd (merge base
73efd9407, 290 commits behind mainab9a47684). This confirms jotitadev's MoltenVK result above on a second driver.Setup: PR head checked out as-is (not rebased), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), Linux, ANYPS5_NO_SHADER_CACHE=1, GPU test run on lavapipe (Mesa 26.2.4 llvmpipe, LLVM 23.1.1).
"AGC graphics: v_min3_f32 f32 denormal mode 1, IEEE=0 of 0x00000000, 0x00000000, 0x00000001 is 0x00000001, expected 0x00000000"
The test stops at the first mismatch and mode 1 runs before mode 2, so mode 2 never ran here.
lavapipe reports shaderDenormPreserveFloat32 = false and shaderDenormFlushToZeroFloat32 = false. On main I find denormal-preserve handling only for f16 and f64 (VulkanDevice.cpp lines 1212-1213, 1222 and 1410), and no f32 DenormPreserve execution mode emitted, by reading. So the kept-denormal result depends on the device, which fits this failure, though I have not proven that lavapipe flushes this input. The test could skip the kept cases when the device reports no f32 denormal preservation, or the translation could produce the kept result without relying on the device. I have not tried either.
Overlap: #6007, #6010, #5782 and #5941 also edit f32DenormalFlushFor.
Not checked: NVIDIA, AMD, Windows, or any title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T20:58:23Z.
Review: CHANGES_REQUESTED
Thanks for the measurement: I reran it on gfx1035 (all 2,744 triples, five instructions, IEEE 0/1, f32 denormal modes 0 to 3). Mode 1 matches mode 3 and mode 2 matches mode 0 on every row, and the 56 rows in the test match bit for bit. So the semantics are right. The problem is the translation of mode 1:
agc_driver_f32_min_max_denormal_modesalso fails on RADV (Mesa 26.2.4, Radeon 660M, gfx1035), with the same first mismatch that jotitadev (MoltenVK) and Dextroid17 (lavapipe 26.2.4) reported:v_min3_f32 f32 denormal mode 1, IEEE=0 of 0x00000000, 0x00000000, 0x00000001 is 0x00000001, expected 0x00000000. It passes only on CI's lavapipe 25.2.8. RADV reportsshaderDenormPreserveFloat32 = true, but the module never declaresDenormPreservefor width 32 (only the f16/f64 paths emit it,SpirvModuleEmitter.cpp:1068). The driver therefore uses its default f32 denormal mode and flushes. Wheremainthrew, mode 1 now returns a wrong result on AMD hardware, which is the case "throw if it can't be exact" exists for.shaderDenormPreserveFloat32likefloat16DenormPreserve(cache key and request serializer included). When a shader uses these instructions with the input-keep bit set, emitDenormPreserve 32if the device has it, and throw otherwise. The other option is to do the selection on the bits so that it doesn't depend on the device. In the test, skip the mode 1 rows on devices without f32 denormal preservation and keep mode 2, as jotitadev suggested.mainhas the same gap for these instructions with denormal operands (no row there catches it). If your change covers the mode-3 path too, add a mode 3 row to the test. Otherwise, a sentence in the newdocs/dev/debt/shader.mdline saying that kept denormals needDenormPreserve 32would make the dependency on the driver explicit.Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-21T17:23:17Z.
Thanks for re-measuring. Updated in b3fe41a7d2, rebased onto main
cecb540a.Gating. Mode 1 of v_min/max/min3/max3/med3_f32 now translates only when the module keeps f32 denormals: target.float32DenormPreserve set and the hardware f32 flush not in use. Otherwise it throws f32 denormal mode 1 at pc X needs DenormPreserve 32, which the device does not offer for this shader. Mode 2 flushes the inputs, never depends on the driver, and runs everywhere.
Stacked on #6010. The branch sits on #6010's commit, which adds TranslateOptions::float32DenormPreserve (and the result-only flush fix). So Depends on: #6010, and this PR's own change is the second commit. I'll rebase it to a single commit once #6010 is in.
RADV. On main the hardware f32 flush is declared in every module, so RADV throws in mode 1. claude/f32-hardware-flush-float-mode (a separate PR) limits that flush to shaders that flush f32 denormals. With it, RADV gets DenormPreserve 32 here.
Test. agc_driver_f32_min_max_denormal_modes checks the measured rows in each mode the recompiler translates. Where it throws, it requires that the device doesn't keep f32 denormals, that the mode is 1, and that the error names DenormPreserve 32, so main still fails it. On CI's lavapipe 25.2.8 (no shaderDenormPreserveFloat32) it checks the values in mode 2 at IEEE 0 and 1 and the throw in mode 1.
Mode 3. As you said, mode 3 on main has the same driver dependence. This change doesn't cover it, and the new debt entry says that mode 3 keeps depending on the driver without DenormPreserve 32. The entry is rewritten gap-first and is under 1,000 characters.
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 2,744 triples × 5 instructions match in modes 0–3 at IEEE 0/1. Without the flag, only mode 1 throws.
Negative control: the new test fails on main (f32 denormal mode 1 at pc 20 is not implemented) and passes with the change.
Focused tests: agc_driver_f32_min_max_denormal_modes, f32_min_max_nan, f32_add_mul_denormal_modes, f32_denormal_flush, dx10_clamp, f32_output_modifier and f32_result_clamp 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.