Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ How the generated SPIR-V expects the runtime to work:
| `NoContraction` multiply on MoltenVK (SHADER-182) | On MoltenVK, a multiply whose exact product is a negative zero (`+0 × -1.5`, `-0 × 1.0`) returns +0 in f32, f16 and packed f16 (#1203). MoltenVK's SPIRV-Cross turns every `NoContraction` `OpFMul` into its `spvFMul` helper, which returns `fma(l, r, T(0))`. A -0 product plus +0 is +0. The f16 multiplies are `OpFMul` on `float` or `v2float`, so they take the same path. So does the product inside `fma_f16_odd`: `v_fma_f16`, `v_fmac_f16` and `v_pk_fma_f16` return +0 for a -0 product plus a -0 addend. SPIRV-Cross [300681bb](https://github.com/KhronosGroup/SPIRV-Cross/commit/300681bbcb8365a8d1eacdacdbdd0249bbf1d6b1) changes the addend to `T(-0.0)`. MoltenVK 1.4.2 has the old helper, and so does MoltenVK `main` at `5189f8de` (2026-10-13), which pins the parent commit `aa217aeb`. The translator does not change for this. `MVK_CONFIG_FAST_MATH_ENABLED=0` does not help, because the helper itself adds +0, and per #1203 neither does `SignedZeroInfNanPreserve` (#664). Measured on an Apple M3 Max (macOS 27.3): Metal keeps the sign for `a * b` and for `fma(a, b, -0.0)` with `MTLMathModeFast` and with `MTLMathModeSafe`, and loses it only with the +0 addend. With `valu_fuzz.py --family f16`, MoltenVK `5189f8de` as pinned matches 49/50 programs with `--seed 1 --wave 32`, 98/100 with `--seed 1 --emulate` and 98/100 with `--seed 4 --emulate`, and Homebrew MoltenVK 1.4.2 gives the same counts. Each mismatch is a -0 product read as +0. The same build with SPIRV-Cross `300681bb` matches 50/50, 100/100 and 100/100, as does KosmicKrisp (Mesa 26.3.1). For the FMA path, `--seed 330 --wave 32` with `--skip-op v_mul_f16 --skip-op v_pk_mul_f16` matches 49/50 with `aa217aeb` (a `v_fmac_f16` result) and 50/50 with `300681bb` and on KosmicKrisp. |
| MoltenVK fast-math (SHADER-234, NaN compares) | MoltenVK compiles every shader's MSL with Metal's fast-math on by default (`MVK_CONFIG_FAST_MATH_ENABLED`, default 1), and with it Metal folds an f32 compare of a NaN with itself as if the NaN were an ordinary number: `v_cmpx_eq_f32` and `v_cmpx_le_f32` on (NaN, NaN) give true where the ISA, AMD and NVIDIA give false (`ps_vcmpx_forms`, #1374: word 1 `0xf05e8e16`, want `0xf05e8a12`, bits 2 and 10; found by @zneel and @DomasUrbonas). The translation is not involved: the compare is `OpFOrdEqual` / `OpFOrdLessThanEqual`, and the MSL MoltenVK generates (`MVK_CONFIG_SHADER_DUMP_DIR`) is byte-identical with fast-math on and off, so it is Metal's compile option. `MVK_CONFIG_FAST_MATH_ENABLED=2` (on demand) does not help, because translated modules don't declare an execution mode that asks for IEEE behaviour; 0 does. The test tools (`runtime/test/vulkan_loader.h`, which every Vulkan test program uses to load MoltenVK) set `MVK_CONFIG_FAST_MATH_ENABLED=0` before MoltenVK reads its configuration, unless the variable is already set, so a user's own value still wins; the variable also reaches MoltenVK through the Vulkan loader. A runtime that loads MoltenVK on a Mac must do the same. Fast-math off does **not** remove the `[[clang::optnone]]` `spvFAdd`/`spvFSub`/`spvFMul` helpers SPIRV-Cross emits for `NoContraction` (11 calls in `ps_ops`'s MSL either way: their cost is the other part of SHADER-234), and it does not fix the row above (SHADER-182): `valu_fuzz.py --family f16 --seed 1` gives the same `v_mul_f16` +0 mismatch with fast-math on and off. Confirmed on an Apple A18 Pro (macOS 26.6.2, MoltenVK 1.4.2): `ps_vcmpx_forms` fails on all 4,096 pixels with fast-math on and matches all 4,096 with it off, natively and with wave64 emulation; no other `run_gpu_tests.py` result and none of the 25 `ctest` tests changes. |
| `v_cmp(x)_lg_f32/f16` on MoltenVK (ordered not-equal) | LG is less-or-greater, false when either source is a NaN. The translator emits it as the `OpLogicalOr` of `OpFOrdLessThan` and `OpFOrdGreaterThan`, not as `OpFOrdNotEqual`. MoltenVK's SPIRV-Cross writes `OpFOrdNotEqual` as a plain `!=`, the same text it gives `OpFUnordNotEqual`, and Metal's `!=` is true on a NaN. That happens with fast-math off too, so it is not the row above: `ps_vcmp_f32_nan`'s SPIR-V has 8 `OpFOrdNotEqual` and 8 `OpFUnordNotEqual`, and the MSL MoltenVK dumps (`MVK_CONFIG_SHADER_DUMP_DIR`) has 16 identical `as_type<float>(a) != as_type<float>(b)`. Confirmed on an Apple M4 (MoltenVK 1.4.2, fast-math off, MoltenVK logs "MathMode Safe"). `ps_vcmp_f32_nan`, `ps_vcmp_f16`, `ps_vcmp_f16_e64`, `ps_vcmpx_f16` and `ps_vcmpx_f16_e64` failed only in the LG bits of their NaN pairs (f16 word 1 `0x4ce9bda2`, want `0x4ce9b1a2`; f32 `0x377c`, want `0x376c`). With this form all five pass, and no other `run_gpu_tests.py` result changes. f64 LG compares integer words and is not affected. `v_cmp(x)_lg_f32/f16` is in 13,795 of the 25,734 listings. The form is pinned on the CPU by `tests/semantic/test_vcmp_f32_predicates.py` (`test_lg_is_ordered_without_ord_not_equal`). |
| Denormal flush and special results in `v_rcp_f32`, `v_rsq_f32`, `v_sqrt_f32`, `v_log_f32`, `v_exp_f32`, `v_rcp_iflag_f32` | Measured on an RX 6800 (gfx1030, not the console's gfx1013) with raw kernels in both wave sizes and all four IEEE/DX10_CLAMP combinations: all six read a denormal input as a zero of its sign and flush a denormal result, even with MODE's denormal field set to keep them. `v_rcp_f32(±0)` = ±inf, `v_rsq_f32(-0)` = -inf, `v_sqrt_f32(-0)` = -0, `v_log_f32(±0)` = -inf, `v_rcp_f32(FLT_MAX)` = +0, `v_exp_f32(-127)` = +0, `v_exp_f32(128)` = +inf, and every invalid result (`v_sqrt_f32`/`v_rsq_f32`/`v_log_f32` of a negative, …) is `0xffc00000`. Each is within 1 ulp of correctly rounded on the values tried, and `v_rcp_iflag_f32` is bit-identical to `v_rcp_f32`. **Not modelled yet:** the translator leaves all six on Vulkan's default denormal handling, and the ±0 and negative inputs above stay undefined in the emitted SPIR-V (`OpFDiv` by zero, `Sqrt`/`Log2` of a negative), as listed in the multiply-add and output modifiers row; the `clamp`/`omod` bits on `v_cvt_f32_*` and `v_rcp_iflag_f32` are modelled there. Source: [@shelbykb2's hardware words](https://github.com/ItsNotRexed/Leonida/issues/634#issuecomment-7074348963), not the ISA text. The corpus only ever writes MODE's FP_ROUND field and carries `mul:2`/`mul:4`/`div:2` on 102,365 float instructions (a compiler does not emit one that is ignored), so the inferred f32 state, IEEE off with denormals flushed, is the one to model. Corpus count (`tools/shader_profile.py`, 26,503 shaders; the shader count of each op deduplicates its `_e32`/`_e64` forms): `v_rcp_f32` 17,393 shaders / 424,863 occurrences, `v_rsq_f32` 17,221 / 269,310, `v_sqrt_f32` 14,511 / 201,422, `v_log_f32` 8,454 / 90,199, `v_exp_f32` 8,603 / 148,846, `v_rcp_iflag_f32` 7,395 / 34,852; 20,193 shaders (76.2%) use at least one, 1,169,492 occurrences (2.6% of the corpus's 44,406,870 instructions). |
| Denormal flush and special results in `v_rcp_f32`, `v_rsq_f32`, `v_sqrt_f32`, `v_log_f32`, `v_exp_f32`, `v_rcp_iflag_f32` | Measured on an RX 6800 (gfx1030, not the console's gfx1013) with raw kernels in both wave sizes and all four IEEE/DX10_CLAMP combinations: all six read a denormal input as a zero of its sign and flush a denormal result, even with MODE's denormal field set to keep them. `v_rcp_f32(±0)` = ±inf, `v_rsq_f32(-0)` = -inf, `v_sqrt_f32(-0)` = -0, `v_log_f32(±0)` = -inf, `v_rcp_f32(FLT_MAX)` = +0, `v_exp_f32(-127)` = +0, `v_exp_f32(128)` = +inf, and every invalid result (`v_sqrt_f32`/`v_rsq_f32`/`v_log_f32` of a negative, …) is `0xffc00000`. Each is within 1 ulp of correctly rounded on the values tried, and `v_rcp_iflag_f32` is bit-identical to `v_rcp_f32`. **Modelled for v_exp_f32:** the translator explicitly flushes the raw Exp2 result before output modifiers using Gen.flush_f32. The semantic regression in tests/semantic/test_alu_exp_f32.py detects three incorrect subnormal results before the fix and passes afterwards. The synthetic ps_exp_f32_denorm and ps_exp_f32_denorm_rt GPU tests both match all 4096 pixels on an RTX 4070 SUPER (Windows, driver 591.86), with and without the fix. The GPU tests therefore confirm compatibility, not a hardware-visible regression on this device. No new native RDNA2 measurement or corpus scan was performed. Denormal handling after output scaling is not established. **Remaining limitations:** the translator leaves the other five on Vulkan's default denormal handling, and the ±0 and negative inputs above stay undefined in the emitted SPIR-V (`OpFDiv` by zero, `Sqrt`/`Log2` of a negative), as listed in the multiply-add and output modifiers row; the `clamp`/`omod` bits on `v_cvt_f32_*` and `v_rcp_iflag_f32` are modelled there. Source: [@shelbykb2's hardware words](https://github.com/ItsNotRexed/Leonida/issues/634#issuecomment-7074348963), not the ISA text. The corpus only ever writes MODE's FP_ROUND field and carries `mul:2`/`mul:4`/`div:2` on 102,365 float instructions (a compiler does not emit one that is ignored), so the inferred f32 state, IEEE off with denormals flushed, is the one to model. Corpus count (`tools/shader_profile.py`, 26,503 shaders; the shader count of each op deduplicates its `_e32`/`_e64` forms): `v_rcp_f32` 17,393 shaders / 424,863 occurrences, `v_rsq_f32` 17,221 / 269,310, `v_sqrt_f32` 14,511 / 201,422, `v_log_f32` 8,454 / 90,199, `v_exp_f32` 8,603 / 148,846, `v_rcp_iflag_f32` 7,395 / 34,852; 20,193 shaders (76.2%) use at least one, 1,169,492 occurrences (2.6% of the corpus's 44,406,870 instructions). |
| f16 transcendentals (`v_exp_f16`, `v_log_f16`, `v_rcp_f16`, `v_rsq_f16`, `v_sqrt_f16`, `v_sin_f16`, `v_cos_f16`) | Measured on an RX 6700 XT (gfx1031, not the console's gfx1013) with `tools/f16_trans_sweep.py`: all 65,536 inputs of each op, wave32 and wave64 (identical on every input), five MODEs (FUZZ-29). With f16 denormals kept (`denorm_mode_16_64 = 3`, the assembler default and what the translator assumes), `v_exp/log/rcp/rsq/sqrt_f16` are **correctly rounded on every input that is not a NaN** (0 ulp). `v_sin/v_cos_f16` take turns and reduce them exactly over the whole f16 range (no ±256 limit): compared with sin/cos(2πx), every result for \|x\| > 1 is bit-exact, and for \|x\| ≤ 1 only 14 (sin) and 4 (cos) of 30,722 are 1 ulp off. So the translator's rule (widen to f32, the GLSL.std.450 function, round to nearest even back to f16) matches the hardware as far as the driver's f32 function is accurate. For sin/cos that holds only once the angle is reduced in turns first (#594): with the f32 product 6.2831855 · x, 16,366 sin and 2,036 cos inputs are more than 1 ulp off, up to 9,944 ulp (sin of 64,640 turns: 0.0267 instead of 0). With `denorm_mode_16_64 = 0`, a denormal operand reads as a zero of its sign, and a denormal result is written as a zero of its sign. That differs from the f32 ops in the row above, which flush in both modes. The f16 field of FP_ROUND is ignored: round toward zero gives the same words as round to nearest on every input. IEEE changes only signalling NaNs. With IEEE off they pass through unchanged; with IEEE on they are quieted (bit 9 set). Payload and sign are kept either way, and quiet NaNs pass unchanged in every mode. Where SPIR-V leaves the result undefined, the hardware gives: log(±0) = -inf, rcp(±0) = ±inf, rsq(+0) = +inf, rsq(-0) = -inf, sqrt(-0) = -0, and `0xfe00` for log/rsq/sqrt of a negative (-inf included) and for sin/cos(±inf). `v_exp_f16`/`v_log_f16` give the same counts on gfx1036 (RDNA2, AMD Windows driver; SHADER-135). Since 2026-11-22 the translator selects those hardware words for `v_rcp_f16`, `v_rsq_f16` and `v_sqrt_f16` itself (the operand is replaced by 1.0 for the f32 `OpFDiv`/`InverseSqrt`/`Sqrt`, which SPIR-V leaves undefined at 0 or below 0) and `0xfe00` is the negative quiet NaN; checked bit for bit through the interpreter by `tests/semantic/test_valu_rcp_f16.py`, `test_valu_rsq_f16.py` and `test_valu_sqrt_f16.py` against exactly rounded references. The undefined cases of `v_exp_f16`/`v_log_f16`/`v_sin_f16`/`v_cos_f16` are still left to the driver. **Not modelled yet:** `Gen.f16_bits` quiets every NaN (the hardware keeps a signalling NaN with IEEE off), and the undefined cases above are left to the driver. |
| f64 literal sources | For `v_cvt_f32_f64` and either source of `v_add_f64`, a literal dword supplies bits 63:32 and bits 31:0 are zero. For `v_cvt_f32_f64`, `v_rcp_f64`, `v_rsq_f64`, `v_sqrt_f64`, `v_frexp_mant_f64`, `v_frexp_exp_i32_f64` and either source of `v_add_f64`, a literal dword supplies bits 63:32 and bits 31:0 are zero. Inline integers are sign-extended and inline floats use their binary64 value. For example, gfx1030 `llvm-mc` encodes 1.5 as the literal word `0x3ff80000`. The synthetic `tests/semantic/fixtures/f64_literals.asm` checks both source words in the translated SPIR-V, including the distinction between the printed literal `0x1` and inline `1`. [LLVM AMDGPU operand conversions](https://releases.llvm.org/15.0.0/docs/AMDGPUOperandSyntax.html#integer-literals) In the synthetic `unary_f64_literals.asm`, LLVM gfx1030 encodes the five unary literal sources in trailing dwords. The regression checks the emitted high and low operand words for reciprocal, reciprocal square root and square root. It executes the integer-only frexp paths: for 6.0 the mantissa words are (0, 0x3fe80000) and the exponent is 3. The full fixture passes Vulkan 1.2 validation; the semantic interpreter cannot execute its Float64 instructions. The rule keys on the literal's printed width, not only its value: LLVM pads nothing, so a value that fits one literal dword prints in at most 8 hex digits, and a spelling of more than 8 is the whole binary64 constant (the full-width form `tools/f64_diff.py` writes: `0x0000000012345678` is the low word `0x12345678` and a zero high word, while `0x12345678` stays the high word). gfx1013 `llvm-mc` refuses a literal wider than 32 bits for these ops ("invalid operand"), so a listing from the game carries only the one-dword form and its words do not change. The wider form used to be refused by the four rounding ops; they now read the whole constant, which is what the differential check's generated programs needed. `tests/semantic/test_f64_literal_whole.py` checks those emitted words for `v_sqrt_f64`, `v_rsq_f64`, `v_rcp_f64`, the four rounding ops and the frexp exponent path, next to the one-dword, padded-width and inline-integer controls; `tests/test_f64_diff.py` checks that the CPU reference reads a literal the same way; `tools/f64_diff.py`'s CPU reference judged the whole-literal `v_add_f64`/`v_rsq_f64`/`v_sqrt_f64` programs as mismatches before the change. |
| `v_cvt_f32_f64` output modifiers | Convert to f32, apply `mul:2`, `mul:4`, or `div:2` if present, then apply `clamp` to [0, 1] if requested. [LLVM GFX10 assembler tests](https://github.com/llvm/llvm-project/blob/main/llvm/test/MC/AMDGPU/gfx10_asm_vop1.s#L3044-L3054) confirm the modifier encodings. `tests/test_f64_conversion.py` checks the resulting SPIR-V data flow. |
Expand Down
17 changes: 17 additions & 0 deletions runtime/test/shaders/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ def salu_ext_test(tools, spirv, env=None):
"ps_ops7": (0x00ff8077, 0xff0000ff, 0x89abcdef, 0x00070003),
"ps_ops8": (0x80000000, 0xbfffffff, 0x000f100e, 0x0f0f0f1f),
"ps_f32_mods": (0xc0256a01, 0x3f800000, 0x41e00000, 0x3fc00000),
"ps_exp_f32_denorm": (0x01000000, 0x00800000, 0x00000000, 0x00000000),
"ps_scc": (1, 1, 3, 0x80000000), "ps_sdwa": (0x000000f1, 0xbffefff2, 0x3c003344, 0xbff70000),
# 16-bit SDWA results with dst_sel:DWORD zero the high half (.asm header, measured on gfx1036)
"ps_sdwa_dword_hi": (0x00004000, 0x00000003, 0x00004700, 0x0000c000),
Expand Down Expand Up @@ -2038,6 +2039,20 @@ def mandelbrot_counts(w):
return counts


def exp_f32_denorm_problem(px):
if len(px) != 4096:
return f"expected 4096 pixels, got {len(px)}"
for i, pixel in enumerate(px):
x = i % 64
result = (0x01000000, 0x00800000)[x] if x < 2 else 0
exponent = struct.unpack('<I', struct.pack('<f', -125.0 - x))[0]
column = struct.unpack('<I', struct.pack('<f', float(x)))[0]
expected = (result, exponent, column, result)
if tuple(pixel) != expected:
return f"pixel {i}: got {pixel}, expected {expected}"
return ''


def mandelbrot_problem(px):
"""ps_mandelbrot: channel 3 is the lane's iteration count. A lane that leaves EXEC too early or late, or a loop
that ends on the wrong branch, changes counts; the colour channels are only for looking at the frame."""
Expand Down Expand Up @@ -2151,6 +2166,8 @@ def pixel_test(tools, spirv, name):
c = collections.Counter((a, b) for a, b, _, _ in px)
fine = c[(8, 1)] and c[(8, 0)] and c[(7, 0)] and set(c) <= {(8, 1), (8, 0), (7, 0)}
return "" if fine else f"(s5, bit) counts {dict(c)}"
if name == "ps_exp_f32_denorm_rt":
return exp_f32_denorm_problem(px)
if name == "ps_mandelbrot":
return mandelbrot_problem(px)
return "no check written for this shader"
Expand Down
12 changes: 12 additions & 0 deletions runtime/test/shaders/ps_exp_f32_denorm.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; v_exp_f32 flushes subnormal results to positive zero.
; Expected mrt0 bits: 0x01000000, 0x00800000, 0x00000000, 0x00000000
v_mov_b32_e32 v0, 0xc2fa0000
v_mov_b32_e32 v1, 0xc2fc0000
v_mov_b32_e32 v2, 0xc2fe0000
v_mov_b32_e32 v3, 0xc3150000
v_exp_f32_e32 v4, v0
v_exp_f32_e32 v5, v1
v_exp_f32_e32 v6, v2
v_exp_f32_e32 v7, v3
exp mrt0 v4, v5, v6, v7 done vm
s_endpgm
10 changes: 10 additions & 0 deletions runtime/test/shaders/ps_exp_f32_denorm_rt.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; Runtime-dependent v_exp_f32 inputs from interpolated pixel coordinates.
; x = floor(u * 64), exponent = -125 - x.
v_interp_p1_f32_e32 v2, v0, attr0.x
v_interp_p2_f32_e32 v2, v1, attr0.x
v_mul_f32_e32 v3, 0x42800000, v2
v_floor_f32_e32 v3, v3
v_sub_f32_e32 v4, 0xc2fa0000, v3
v_exp_f32_e32 v5, v4
exp mrt0 v5, v4, v3, v5 done vm
s_endpgm
6 changes: 5 additions & 1 deletion scripts/run_gpu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from check import MESH_COMPUTE, mesh_env # LEONIDA_MESH_COMPUTE=1: mesh shaders as compute + a generated VS
from check import (WAVE32_LANE_DUMPS, WAVE32_LANE_ENV, bitmask_perm_narrow_skip, narrow_subgroup_skip,
unpinned_subgroup_skip, vcmpx_forms_narrow_skip, wave32_lane_skip) # as in check.py
from check import PIXEL_VERTEX, VINT_ENV, interp_f16_high_problem, ij_computed_problem, mandelbrot_problem # pixel producers and checks
from check import PIXEL_VERTEX, VINT_ENV, interp_f16_high_problem, ij_computed_problem, mandelbrot_problem, exp_f32_denorm_problem # pixel producers and checks
from check import barycentric_problem
from check import NAN_PAYLOAD_TEST, nan_payload_problem
from check import GET_LOD_TESTS, STORE_WORDS, get_lod_test, store_words_problem # TEST-184: image atomics and queries
Expand Down Expand Up @@ -1503,6 +1503,7 @@ def run_suite(a, work: str) -> int:
"ps_ops7": (0x00FF8077, 0xFF0000FF, 0x89ABCDEF, 0x00070003),
"ps_ops8": (0x80000000, 0xBFFFFFFF, 0x000F100E, 0x0F0F0F1F),
"ps_f32_mods": (0xC0256A01, 0x3F800000, 0x41E00000, 0x3FC00000),
"ps_exp_f32_denorm": (0x01000000, 0x00800000, 0x00000000, 0x00000000),
"ps_vcc_x2": (0x11223344, 0x55667788, 0, 0),
"ps_vcmpx_forms": (0xF05B2A48, 0xF05E8A12, 0xB174BE18, 0x15550000), # 14 v_cmpx forms (.asm header)
"ps_vcmp_int32": (0x963E52E8, 0x984DCEE8, 0xB9458EE8, 0xB73612E8), # u32/i32 compares (.asm header)
Expand Down Expand Up @@ -1594,6 +1595,9 @@ def run_suite(a, work: str) -> int:
elif name == "ps_ij_computed":
problem = ij_computed_problem(read_raw_u32(raw) if os.path.isfile(raw) else [])
ok, detail = not problem, problem or "computed I/J pair words and v/2 within 1e-6"
elif name == "ps_exp_f32_denorm_rt":
problem = exp_f32_denorm_problem(read_raw_u32(raw) if os.path.isfile(raw) else [])
ok, detail = not problem, problem or "4096 pixels exact across 64 runtime-dependent inputs"
elif name == "ps_mandelbrot":
px = read_raw_u32(raw) if os.path.isfile(raw) else []
problem = frame_problem(px) or mandelbrot_problem(px)
Expand Down
Loading