shader: v_div_fmas_f32 flushes denormal inputs, misses the result flush in denormal mode 1 and rounds a scaled denormal result twice #10922

Closed
opened 2026-10-25 10:19:14 +00:00 by TechLevelZero · 0 comments
TechLevelZero commented 2026-10-25 10:19:14 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by TechLevelZero on 2026-10-25T10:19:14Z.


What happens
On RDNA2 v_div_fmas_f32 follows one rule in every f32 denormal mode:

  • It never flushes its inputs.
  • It rounds src0 * src1 + src2 once, after scaling by 2^64 (VCC set, src2 exponent 128 or more) or 2^-64 (VCC set otherwise).
  • It flushes a denormal result to a zero of its sign when bit 1 of the FLOAT_MODE f32 denormal field is clear (modes 0 and 1).

The translation (TranslationContext::vDivFmasF32, with no VDivFmasF32 entry in f32DenormalFlushFor) differs in three ways:

f32 denormal mode Translation Rows that differ, VCC clear / set (of 728)
0 With the hardware f32 flush (DenormFlushToZero 32), the driver also flushes the inputs, and a flushed result can lose its sign 55 / 46
1 A denormal result is never flushed 121 / 98
2, 3 With VCC set, the f32 fma result is rounded, then rounded again by the multiply with 2^-64, so a denormal result can be one unit in the last place off 0 / 4

Examples (hardware, translation):

src0, src1, src2 VCC mode hardware translation
0x807fffff, 0xff7fffff, 0x3f000000 0 0 0x408fffff 0x3f000000 (src0 flushed)
0xff800000, 0x00000001, 0x1e8b9c7b 0 0 0xff800000 0xffc00000 (inf * flushed 0)
0x0becd7b0, 0x2535a659, 0x80000001 0 0 0x80000000 0x00000000
0x0becd7b0, 0x2535a659, 0x80000001 0 1 0x80000000 0x80000001
0x32d351d2, 0x1f800000, 0x9f800000 1 1 0x80000000 0x80200000
0xbec6b7ec, 0x20000000, 0x00800000 1 3 0x8018d6fd 0x8018d6fe

LLVM wraps v_div_scale/v_div_fmas in s_denorm_mode to keep f32 denormals when a function flushes them, so the one-ulp error in modes 2 and 3 is the case compiled divisions would hit.

Title, test or build step
Found by differential testing of generated shaders against the hardware (tools/hw-oracle), then measured with a directed sweep.

AnyPS5 commit
Measured with a build of d01070d6c3. vDivFmasF32 and its denormal handling are unchanged on main da6c581a02.

System

  • Hardware: AMD Ryzen 7 9700X integrated graphics (gfx1036, RDNA2), ROCr through tools/hw-oracle.
  • Translation: RX 9070 XT (RADV GFX1201), Mesa 26.0.8, Fedora 44, Ubuntu 26.04 container.
  • IEEE=0, DX10_CLAMP=1, f32 round to nearest even, f16 denormals kept.

Steps to reproduce
Run v_div_fmas_f32 v10, v4, v5, v6, with vcc_lo set to 0 or -1, through tools/hw-oracle and through the recompiler. Use 728 operand triples: 512 random (with denormals, small exponents, zeros, infinities and the largest finite value) and 216 combinations of denormals, powers of two and ones. Run each f32 denormal mode, 0 to 3.

Log
One rule reproduces every finite hardware result in all four modes with VCC clear and set: 5,536 of 5,536 rows. The rule is the exact src0 * src1 + src2, times 2^±64 when VCC is set, rounded once to f32 with denormals kept, then flushed to a signed zero in modes 0 and 1. On the hardware, modes 0 and 1 give identical results, and so do modes 2 and 3.

Fix
A fix is on the fork branch fix/div-fmas-f32-single-rounding (04d5c84bf3, one commit on main da6c581a02). On devices with the Float64 capability, v_div_fmas_f32 is computed in f64:

  • The inputs are widened exactly. Denormal inputs are built from their integer bits, so the hardware f32 flush can't reach them.
  • The product is exact, and the sum with src2 is rounded to odd from its TwoSum error.
  • The 2^±64 scaling is exact, and the result is rounded to f32 once. A denormal result is rounded through integers.
  • f32DenormalFlushFor now flushes only the result in modes 0 and 1.

Devices without Float64 keep the previous lowering, and a debt entry in docs/dev/debt/shader.md records what that misses. agc_driver_division gains a check with 26 vectors from the sweep above, in each f32 denormal mode. It runs where Float64 is present.

The branch was run against main with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, on the 1,401 tests tools/select_tests.py selects:

GPU Failing only on the branch New VUID- errors
NVIDIA A800 (610.57.04) none after reruns: 4 timing tests failed while another build loaded the machine, then passed 3 of 3 serial runs on both sides none
AMD RX 9070 XT (RADV GFX1201, Mesa 26.0.8) none none
AMD gfx1036 (RADV RAPHAEL_MENDOCINO, Mesa 26.0.8) none none

I'll open it as a pull request once my first one is merged (open-PR limit).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6023357025 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/10922). Originally posted by [TechLevelZero](https://github.com/TechLevelZero) on 2026-10-25T10:19:14Z. --- **What happens** On RDNA2 `v_div_fmas_f32` follows one rule in every f32 denormal mode: - It never flushes its inputs. - It rounds `src0 * src1 + src2` once, after scaling by 2^64 (VCC set, src2 exponent 128 or more) or 2^-64 (VCC set otherwise). - It flushes a denormal result to a zero of its sign when bit 1 of the `FLOAT_MODE` f32 denormal field is clear (modes 0 and 1). The translation (`TranslationContext::vDivFmasF32`, with no `VDivFmasF32` entry in `f32DenormalFlushFor`) differs in three ways: | f32 denormal mode | Translation | Rows that differ, VCC clear / set (of 728) | | --- | --- | --- | | 0 | With the hardware f32 flush (`DenormFlushToZero 32`), the driver also flushes the inputs, and a flushed result can lose its sign | 55 / 46 | | 1 | A denormal result is never flushed | 121 / 98 | | 2, 3 | With VCC set, the f32 fma result is rounded, then rounded again by the multiply with 2^-64, so a denormal result can be one unit in the last place off | 0 / 4 | Examples (hardware, translation): | src0, src1, src2 | VCC | mode | hardware | translation | | --- | --- | --- | --- | --- | | `0x807fffff, 0xff7fffff, 0x3f000000` | 0 | 0 | `0x408fffff` | `0x3f000000` (src0 flushed) | | `0xff800000, 0x00000001, 0x1e8b9c7b` | 0 | 0 | `0xff800000` | `0xffc00000` (inf * flushed 0) | | `0x0becd7b0, 0x2535a659, 0x80000001` | 0 | 0 | `0x80000000` | `0x00000000` | | `0x0becd7b0, 0x2535a659, 0x80000001` | 0 | 1 | `0x80000000` | `0x80000001` | | `0x32d351d2, 0x1f800000, 0x9f800000` | 1 | 1 | `0x80000000` | `0x80200000` | | `0xbec6b7ec, 0x20000000, 0x00800000` | 1 | 3 | `0x8018d6fd` | `0x8018d6fe` | LLVM wraps `v_div_scale`/`v_div_fmas` in `s_denorm_mode` to keep f32 denormals when a function flushes them, so the one-ulp error in modes 2 and 3 is the case compiled divisions would hit. **Title, test or build step** Found by differential testing of generated shaders against the hardware (`tools/hw-oracle`), then measured with a directed sweep. **AnyPS5 commit** Measured with a build of `d01070d6c3`. `vDivFmasF32` and its denormal handling are unchanged on `main` `da6c581a02`. **System** - Hardware: AMD Ryzen 7 9700X integrated graphics (gfx1036, RDNA2), ROCr through `tools/hw-oracle`. - Translation: RX 9070 XT (RADV GFX1201), Mesa 26.0.8, Fedora 44, Ubuntu 26.04 container. - IEEE=0, DX10_CLAMP=1, f32 round to nearest even, f16 denormals kept. **Steps to reproduce** Run `v_div_fmas_f32 v10, v4, v5, v6`, with `vcc_lo` set to 0 or -1, through `tools/hw-oracle` and through the recompiler. Use 728 operand triples: 512 random (with denormals, small exponents, zeros, infinities and the largest finite value) and 216 combinations of denormals, powers of two and ones. Run each f32 denormal mode, 0 to 3. **Log** One rule reproduces every finite hardware result in all four modes with VCC clear and set: 5,536 of 5,536 rows. The rule is the exact `src0 * src1 + src2`, times 2^±64 when VCC is set, rounded once to f32 with denormals kept, then flushed to a signed zero in modes 0 and 1. On the hardware, modes 0 and 1 give identical results, and so do modes 2 and 3. **Fix** A fix is on the fork branch [`fix/div-fmas-f32-single-rounding`](https://github.com/TechLevelZero/AnyPS5/tree/fix/div-fmas-f32-single-rounding) (`04d5c84bf3`, one commit on `main` `da6c581a02`). On devices with the Float64 capability, `v_div_fmas_f32` is computed in f64: - The inputs are widened exactly. Denormal inputs are built from their integer bits, so the hardware f32 flush can't reach them. - The product is exact, and the sum with src2 is rounded to odd from its TwoSum error. - The 2^±64 scaling is exact, and the result is rounded to f32 once. A denormal result is rounded through integers. - `f32DenormalFlushFor` now flushes only the result in modes 0 and 1. Devices without Float64 keep the previous lowering, and a debt entry in `docs/dev/debt/shader.md` records what that misses. `agc_driver_division` gains a check with 26 vectors from the sweep above, in each f32 denormal mode. It runs where Float64 is present. The branch was run against `main` with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, on the 1,401 tests `tools/select_tests.py` selects: | GPU | Failing only on the branch | New `VUID-` errors | | --- | --- | --- | | NVIDIA A800 (610.57.04) | none after reruns: 4 timing tests failed while another build loaded the machine, then passed 3 of 3 serial runs on both sides | none | | AMD RX 9070 XT (RADV GFX1201, Mesa 26.0.8) | none | none | | AMD gfx1036 (RADV RAPHAEL_MENDOCINO, Mesa 26.0.8) | none | none | I'll open it as a pull request once my first one is merged (open-PR limit). AI-assisted: yes (Claude Code)
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#10922
No description provided.