Repository navigation
fix(shader): translate f32 add and multiply in f32 denormal modes 1 a… - #6010
Conversation
jotitadev
left a comment
There was a problem hiding this comment.
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_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.- With the
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.- 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 expect0x00800000. That is the same case thatagc_driver_f32_constant_factor_tininessalready 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.
|
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.
|
adriadam10
left a comment
There was a problem hiding this comment.
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.- Suggested direction: when the shader's f32 denormal field keeps denormals (modes 1 and 2 here), emit
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.
|
The new test fails on NVIDIA: GeForce RTX 4090, driver 617.42, Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release,
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 The rest of the 976 tests AI-assisted: yes (Claude Code) |
1d5868b to
4418689
Compare
|
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. 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. |
…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>
4418689 to
6a26140
Compare
|
Re-checked against current |
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