diff --git a/docs/design.md b/docs/design.md index daeb4eb425..986860fc0e 100644 --- a/docs/design.md +++ b/docs/design.md @@ -402,7 +402,7 @@ How the generated SPIR-V expects the runtime to work: | Floating-point class compare | `v_cmp_class_f32` writes the selected class to a lane mask. `v_cmpx_class_f32` intersects the existing EXEC with that same predicate and preserves VCC, as specified in [AMD's RDNA2 ISA §12.9](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf). VOP3 `abs` and `neg` on `src0` are applied as raw sign-bit moves on the 32-bit integer bits (`OpBitwiseAnd 0x7FFFFFFF`, then `OpBitwiseXor 0x80000000`) rather than `FAbs`/`OpFNegate`, so signalling NaNs stay signalling (class bit 0) and subnormals are never flushed. **Hardware-measured natively on RDNA2 (`gfx1036`)** via `tools/hw_oracle` (Ryzen 7 9800X3D iGPU, Linux AMDGPU DRM): 672/672 exact across 21 representative IEEE-754 values (including 5 sNaNs) × 4 modifier forms (`v`, `-v`, ` `cs_cmpx_class_f32` checks both encodings with quiet NaN, positive infinity and signed zero, and verifies that a previously inactive lane stays inactive. GPU-checked on an RTX 4070 SUPER (driver 616.92). \| v \| `, `- \| v \| `) × 10 class bits for both `v_cmp_class_f32_e64` and `v_cmpx_class_f32_e64` in wave32 and wave64 across all four `(IEEE=0/1, denorm32=0/3)` modes (until 2026-11-22 `Gen.cmp_class` used `src_f` → `FAbs`/`OpFNegate`, which quieted modified sNaNs in `spirv_mini_interp.py`). 672/672 exact across 21 representative IEEE-754 values (including 5 sNaNs) × 4 modifier forms (`v`, `-v`, `\|v\|`, `-\|v\|`) × 10 class bits for both `v_cmp_class_f32_e64` and `v_cmpx_class_f32_e64` in wave32 and wave64 across all four `(IEEE=0/1, denorm32=0/3)` modes (until 2026-11-22 `Gen.cmp_class` used `src_f` → `FAbs`/`OpFNegate`, which quieted modified sNaNs in `spirv_mini_interp.py`). Corpus count (26,503 shaders): `v_cmp_class_f32` appears 37,026 times in 7,101 shaders (0 with VOP3 `abs`/`neg` on `src0`). Checked on CPU + `spirv-val` by `tests/semantic/test_cmp_class_f32_mods.py` and `tests/semantic/test_valu_f32_class_rndne.py`; `cs_cmpx_class_f32` checks both encodings with quiet NaN, positive infinity and signed zero, and verifies that a previously inactive lane stays inactive (GPU-checked on RTX 4070 SUPER driver 616.92 and AMD RX 9070 XT RADV Mesa 26.2.6, plus direct Vulkan compute verification of modified sNaNs on RX 9070 XT and Radeon 610M `gfx1036`). | | Float controls (NaN, ±inf, −0) (SHADER-99) | RDNA2 float ops return NaN, ±inf and −0 as computed, and Vulkan lets the driver assume none of them occur unless the module says otherwise. Every module therefore declares `OpExecutionMode %main SignedZeroInfNanPreserve 32` (capability `SignedZeroInfNanPreserve`, core since SPIR-V 1.4). Its index entry lists `"float_controls": ["shaderSignedZeroInfNanPreserveFloat32"]`, the device property it needs; `vk_pipeline_check` skips the shader where that property is false, and `--no-float-controls` leaves the mode out. Measured on AMD Radeon RX 9070 XT (driver 26.11.2). Without the mode, `runtime/test/shaders/cs_signed_zero_nan.asm` gets 0 in out[4..7]: the `x != x` NaN checks fold to false and a NaN `v_cvt_f16_f32` is lost. The f16 fuzzer (`tools/valu_fuzz.py --family f16`) also saw `v_pk_mul_f16` −0 products come back +0, and, with integer f16 narrowing (#575), NaN results that were never stored. With the mode, the test and those programs match. Other drivers, from testers in #664: RADV (RX 7800 XT) folds the f16 NaN check (out[6]) without the mode; NVIDIA and Intel Arc keep all three either way. Apple GPUs (MoltenVK) report the property, and the mode fixes their NaN checks, but −0 products (out[0], out[2], out[3]) still come back +0 with or without it (#1203, SHADER-182). `check.py` and `scripts/run_gpu_tests.py` don't judge those three words on an Apple device. Mesa's llvmpipe, where the CI runs these tests, returns 0 for the `0 × inf` NaN check (out[5]) with or without the mode, while its other NaN checks give 1 (#664), so out[5] isn't judged there (`UNJUDGED_WORDS` in `check.py`). f64 has its own row below (SHADER-597); its `SignedZeroInfNanPreserve 64` isn't declared yet. | | Float controls (f16 denormals) (SHADER-526) | RDNA2 keeps f16 denormals, so every module also declares `OpExecutionMode %main DenormPreserve 16` (capability `DenormPreserve`, core since SPIR-V 1.4 like `SignedZeroInfNanPreserve`, so no extension at `--target-env vulkan1.2`). The translator models f16 arithmetic in f32 with integer narrowing (`Gen.f16_bits`), so an f16 denormal is an ordinary f32 value in the module and no f32 denormal is involved: the 16-bit mode is what governs the f16→f32 conversion the driver emits for an f16 source. NVIDIA flushed that conversion without it: in `runtime/test/shaders/ps_sdwa_f16_const.asm` the y lane (`src0_sel:BYTE_1` of `2.0` is the denormal 0x0040 = 2^−18, × 1024.0 = 2^−8 = 0x1c00) read 0x0000, 4096/4096 pixels wrong, while x, z and w matched; with the mode the test is 4096/4096 exact (RTX 4070 SUPER, driver 610.88, Windows 11, validation layers on). The same lane read 0x0000 on an RTX 3090 (driver 610.88, found by @Rainmaker33). Assembling the same module with only the two `DenormPreserve` lines removed reads 0x0000 again, so it is the mode the driver honours, not a change in the translated code. Not every NVIDIA build flushes it: on `main` without the mode an RTX 4060 Ti (621.24), an RTX 5070 Ti (620.73.04, Linux) and an RTX 5080 (616.92) pass the test. The index entry lists `"shaderDenormPreserveFloat16"` after `"shaderSignedZeroInfNanPreserveFloat32"`; `vk_pipeline_check` skips the shader where the device reports that property false, and `--no-float-controls` leaves both modes out. Where the property is false the module has no f16 denormal guarantee, so an f16 denormal read can come back +0 exactly as in that lane above (NVIDIA's default without the mode); that is a value difference in the shader, and the GPU test's y lane is then expected to fail. The AMD Radeon iGPU (RDNA2, driver 24.10.24, Vulkan 1.3.280) reports `shaderDenormPreserveFloat16` true with `denormBehaviorIndependence` `32_BIT_ONLY`, and `vk_pipeline_check` builds the module and its pipeline there (`ps_sdwa_f16_const`: 1 module ok, 1 pipeline ok, wave32 and wave64); the GPU test itself skips on that device, which has 128 push-constant bytes where the test needs 144. `tests/semantic/test_f16_denorm_preserve.py` pins the mode, the index property and the four words. | -| `v_mul_legacy_f32` default-mode f32 denormal flushing | Under the inferred game `MODE` (`IEEE=0`, `denorm_mode_32=0`), the DX9 legacy multiplication reads f32 denormal operands as signed zeros, then its zero-operand override produces **+0** even when an operand was negative or the other is infinity/NaN. A nonzero subnormal product flushes to a zero retaining its sign, *before* VOP3 `mul:2`/`mul:4`/`div:2`; with IEEE off an output multiplier converts a resulting -0 to +0 on native RDNA2. The translated result is flushed after the modifier. This uses software f32 bit tests (`Gen.flush_f32`) rather than driver-specific denormal controls. Native gfx1031 (39,101 programs) and gfx1036 (38,858 programs) measured the MODE and DX9 mul rule (see the output-modifier rows below); the previous translator allowed f32 denormal operands and products through raw `OpFMul`. Synthetic regression: `tests/semantic/test_valu_mul_legacy_denorm.py`, `ps_mul_legacy_denorm.asm` and `ps_mul_legacy_denorm_mods.asm` (CPU, spirv-val and Vulkan GPU). This models the inferred game mode, not all possible dynamically changed MODE configurations. | +| `v_mul_legacy_f32` default-mode f32 denormal flushing | Under the inferred game `MODE` (`IEEE=0`, `denorm_mode_32=0`), the DX9 legacy multiplication reads f32 denormal operands as signed zeros, then its zero-operand override produces **+0** even when an operand was negative or the other is infinity/NaN. A nonzero subnormal product flushes to a zero retaining its sign, *before* VOP3 `mul:2`/`mul:4`/`div:2`; with IEEE off an output multiplier converts a resulting -0 to +0 on native RDNA2. The translated result is flushed after the modifier. This uses software f32 tests rather than driver-specific denormal controls: one ` \| x \| < FLT_MIN` test per operand, which is both its input flush and the zero override (the product of the unflushed operands is discarded when either holds), and `Gen.flush_f32` on the product and on a scaled result. `test_each_operand_is_tested_once` pins that count. Native gfx1031 (39,101 programs) and gfx1036 (38,858 programs) measured the MODE and DX9 mul rule (see the output-modifier rows below); the previous translator allowed f32 denormal operands and products through raw `OpFMul`. Synthetic regression: `tests/semantic/test_valu_mul_legacy_denorm.py`, `ps_mul_legacy_denorm.asm` and `ps_mul_legacy_denorm_mods.asm` (CPU, spirv-val and Vulkan GPU). This models the inferred game mode, not all possible dynamically changed MODE configurations. | | Float controls for f64 (SHADER-597) | A module that declares `Float64` (the translator's native f64 ops) also declares `OpExecutionMode %main SignedZeroInfNanPreserve 64`, since Vulkan does not guarantee −0, NaN or ±inf for 64-bit float ops without it (e.g. `v_add_f64` of −0 and −0 = −0). Same capability as the 32-bit mode, no extension; modules without `Float64` don't get it. The index entry then lists `"float_controls": ["shaderSignedZeroInfNanPreserveFloat32", "shaderSignedZeroInfNanPreserveFloat64", "shaderDenormPreserveFloat16"]`, `vk_pipeline_check` skips the shader where any of them is false, and `--no-float-controls` leaves all three modes out. Found by @Jolymmiles (issue #3047). `tests/test_f64_float_controls.py` checks the module and the index entry; **not yet GPU-verified** (`ps_f64_mods.asm` and the f64 fixtures need a run on a device with `shaderFloat64`). | | Multiply-add with a literal | `v_fmamk_f32`/`v_madmk_f32` are D = S0 × K + S1 and `v_fmaak_f32`/`v_madak_f32` are D = S0 × S1 + K, K being the 32-bit literal. LLVM prints them `vdst, src0, K, vsrc1` and `vdst, src0, vsrc1, K`, so in print order all four are a[1] × a[2] + a[3] (fused for fmamk/fmaak). **Until 2026-11-21 fmamk and madmk computed S0 × S1 + K.** Source: RDNA2 ISA (V_FMAMK_F32, V_MADMK_F32) and the operand fields of the llvm-mc encoding. Checked by `tests/semantic` and GPU-checked with `runtime/test/shaders/ps_fma_literal.asm` on an NVIDIA GeForce RTX 2060 SUPER (Linux, driver 620.70.06): 4096/4096 pixels exact, and 4096/4096 differ with the old translator. **Literal MAD denormal flushing (SHADER-LITERAL-MAD-DENORM):** `v_madak_f32` and `v_madmk_f32` flush all three operands, the separately rounded product and the final sum to signed zero when subnormal, matching `v_mad_f32`, independently of the host driver's denormal mode. Source: Mesa ACO [`aco_ir.h`, `fp_denorm`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/mesa-25.0.0/src/amd/compiler/aco_ir.h), which explicitly includes both literal MAD instructions in the always-flush family. CPU-checked by `tests/semantic/test_literal_mad_denorm.py` and the hand-written `tests/semantic/fixtures/literal_mad_denorm.asm`: both source signs, the smallest normal boundary, subnormal operands/product/sum, ordinary values, separate rounding, infinity and NaN. This denormal rule has not been GPU-checked or run on the game shader corpus. | | Multiply-add and output modifiers | `v_mac_f32` is the VOP2 form of `v_mad_f32`, **not** of `v_fma_f32`: the product is rounded before the add (`OpFMul` + `OpFAdd`, both `NoContraction`). It used to emit `Fma`, one ULP off on some inputs. Source: LLVM `SIInstrInfo.cpp` `getNewFMAInst` (`V_MAC_F32` -> `V_MAD_F32_e64`, `V_FMAC_F32` -> `V_FMA_F32_e64`). VOP3 `clamp` and `mul:2`/`mul:4`/`div:2` now apply to `v_cvt_f32_i32`/`u32`/`ubyte0..3`, `v_cvt_f32_f16` and `v_rcp_iflag_f32` (they were dropped). Source: LLVM `VOPProfileI2F` (`Ins64 = $src0, $clamp, $omod`) and the float-source profiles. Found by `tools/f32_fuzz.py` (FUZZ-3: an exact-arithmetic model against the translated SPIR-V; 0 mismatches over 10,000 seeded programs after the fix) and pinned by `runtime/test/shaders/ps_f32_mods.asm` (`tests/semantic/test_f32_valu.py`); **GPU-checked** (TEST-298): `ps_f32_mods` gives the expected four words (`0xc0256a01 0x3f800000 0x41e00000 0x3fc00000`, so the `v_mac_f32` product is not fused and the `clamp`, `mul:4` and `div:2` on `v_cvt_f32_ubyte1`/`u32`/`f16` and `v_rcp_iflag_f32` apply) on an NVIDIA GeForce RTX 2060 SUPER (Linux, driver 615.78.08); `mul:2` on the conversions is not in that fixture. **not yet GPU-verified**. Still open: on some inputs the ISA defines a result but the emitted SPIR-V is undefined: `v_rcp_f32(±0)` (`OpFDiv` by zero), `v_rsq_f32`/`v_log_f32` of ±0 or a negative, `v_sqrt_f32` of a negative. `v_cvt_pk_u8_f32(NaN)` now selects +0 before `FClamp` (see "Float-to-byte pack"). `v_rcp_f32(±0)` (`OpFDiv` by zero), `v_rsq_f32`/`v_log_f32` of ±0 or a negative, `v_sqrt_f32` of a negative (`v_cvt_pk_u8_f32(NaN)` now selects +0 before `FClamp`, see "Float-to-byte pack"). | diff --git a/tests/semantic/test_valu_mul_legacy_denorm.py b/tests/semantic/test_valu_mul_legacy_denorm.py index 47bcc1683a..f02eec4c5f 100644 --- a/tests/semantic/test_valu_mul_legacy_denorm.py +++ b/tests/semantic/test_valu_mul_legacy_denorm.py @@ -74,6 +74,29 @@ def test_product_flush_precedes_modifiers(self): got, _ = run_mul(src0, src1, mods) self.assertEqual(got, expected) + def test_each_operand_is_tested_once(self): + # The operand's magnitude test is the zero override itself: no flushed copy of the operand and no + # second compare with zero. The product is flushed once, and again only after output scaling. + frame = support.translate_lines("ps_mul_legacy_denorm_semantic", [ + "v_mov_b32 v0, 0x800000", + "v_mov_b32 v1, 0x3f000000", + "v_mov_b32 v2, v0", + "exp mrt0 v2, v2, v2, v2 done vm", + "s_endpgm", + ]) + for mods, magnitude_tests, zero_compares in [ + ("", 3, 0), + ("clamp", 3, 0), + ("mul:2", 4, 1), + ("div:2 clamp", 4, 1), + ]: + with self.subTest(mods=mods): + _, spv = run_mul(0x00800000, 0x3F000000, mods) + added = lambda op: spv.count(op) - frame.count(op) # noqa: E731 + self.assertEqual(added("OpFOrdLessThan %bool"), magnitude_tests) + self.assertEqual(added("OpFOrdEqual %bool"), zero_compares) + self.assertEqual(added("OpFMul %float"), 2 if zero_compares else 1) + def test_emits_explicit_f32_flushes_instead_of_a_driver_mode(self): _, spv = run_mul(0x00800000, 0x3F000000, "mul:2") # One check for each input, plus the product and final result. diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 936de535a9..0fe58b5b60 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -10085,6 +10085,9 @@ def halves(s, is_full): # The inferred game's f32 denorm mode is flush-in/flush-out. Unlike an IEEE multiply, the legacy # zero override gives +0 even when a negative subnormal operand becomes -0 after input flushing. # Flush the product before omod: otherwise FLT_MIN * 0.5 mul:2 becomes FLT_MIN, not zero. + # A subnormal operand reads as zero, so one magnitude test per operand is both its input flush and + # the zero override; the product of the unflushed operands is discarded whenever either test holds. + # Without omod the result is +0, the flushed product or a clamp of those, never a new subnormal. x, y = (self.src_f(t) for t in a[1:3]) m, ax, ay, zx, zy, either, r = (self.tmp() for _ in range(7)) self.emit(f"{m} = OpFMul %float {x} {y}")