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
4 changes: 2 additions & 2 deletions docs/design.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/shader_validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ the two wave sizes agree on every word. `tools/fma_mix_probe.py --oracle hw` (th
`tests/semantic/spirv_mini_interp.py`, then compared with the hardware under the MODE the translator assumes for the
game (IEEE 0, DX10_CLAMP 1, f32 denormals flushed, f16 denormals kept). It gives the same word for the float
constants in every `op_sel`/`op_sel_hi` combination, for the integer constants as f16 halves, for clamp on values that
are not NaN, and for the f16 denormals. It differs in five ways, none fixed here:
are not NaN, and for the f16 denormals. It differs in five ways; item 2 is fixed since 2026-11-24:

1. **f16 omod.** The translator applies `mul:2`/`mul:4`/`div:2` to every f16 result. The hardware ignores them in this
MODE (24 words: `v_add/mul/fma/fmac/exp/rcp_f16`). `v_cvt_f16_f32` drops its omod and clamp today, so its omod
Expand All @@ -2007,6 +2007,7 @@ are not NaN, and for the f16 denormals. It differs in five ways, none fixed here
when "output denormals" are off, the `SIFoldOperands.cpp` comment quoted above). That count needs the game files.
2. **clamp of a NaN.** The interpreter refuses `FClamp` with a NaN operand, which GLSL.std.450 leaves undefined. The
hardware gives +0 with DX10_CLAMP 1, for `v_fmac_f16`, `v_fma_mixlo_f16` and `v_fma_mix_f32` alike (SHADER-186).
**Fixed since 2026-11-24:** `Gen.clamp_mods` selects +0 for a NaN before `FClamp` (`tests/semantic/test_vop3_clamp_nan.py`).
3. **Mix-op rounding.** The hardware rounds once (the section above).
4. **f32 denormal operands of the mix ops.** They are not flushed with `denorm_mode_32 = 0`: `0x00000001`, `0x807fffff`
and the integer constants `1` and `64` used as f32 give the denormal, where the hardware gives a zero of its sign
Expand Down
6 changes: 3 additions & 3 deletions tests/semantic/f16_unary_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ def test_neg_and_abs_source_modifiers(self):
def test_output_modifiers_and_clamp(self):
for a in self.MOD_INPUTS:
base = self.ref(a)
# `clamp` of a NaN result is not modelled (the translator emits FClamp; docs/design.md, float min/max/med3 with
# NaN), so a NaN result is only checked with the output multipliers.
# `clamp` of a NaN result gives +0 (DX10_CLAMP 1 in the game's MODE; Gen.clamp_mods selects +0 for a NaN
# before FClamp, docs/design.md, float min/max/med3 with NaN).
# #4542, measured on gfx1036: an f16 destination ignores omod (mul:2/mul:4/div:2) under the game's MODE
# (IEEE=0, f16 denormals kept) and keeps clamp (docs/design.md, f16 output modifiers).
for suffix, want in ((" mul:2", base), (" mul:4", base),
(" div:2", base), (" clamp", clamp01(base)),
(" mul:2 clamp", clamp01(base))):
if is_nan(base) and "clamp" in suffix:
continue
want = 0
with self.subTest(a=hex(a), mod=suffix):
got = run(self.line("_e64") + suffix, {"v1": a})
self.assertEqual(got >> 16, OLD >> 16)
Expand Down
9 changes: 5 additions & 4 deletions tests/semantic/test_alu_f32_min_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,11 @@ def test_python_default_nan_divergence(self) -> None:
# The reference oracle correctly returns 2.0.
self.assertEqual(ref_min_f32(QNAN, TWO), TWO)

def test_clamp_on_all_nan_raises_unsupported(self) -> None:
# Clamping an all-NaN result is undefined in GLSL.std.450 / SPIR-V and raises UnsupportedConstruct.
with self.assertRaises(si.UnsupportedConstruct):
run_f32_2op("v_min_f32", QNAN, QNAN, e64=True, clamp=True)
def test_clamp_on_all_nan_gives_plus_zero(self) -> None:
# The all-NaN result is a NaN; the clamp bit turns it into +0 under the game's DX10_CLAMP 1
# (docs/shader_validation.md), selected before FClamp, which is undefined for a NaN operand.
self.assertEqual(run_f32_2op("v_min_f32", QNAN, QNAN, e64=True, clamp=True), POS_ZERO)
self.assertEqual(run_f32_2op("v_max_f32", NEG_QNAN, SNAN, e64=True, clamp=True), POS_ZERO)

def test_med3_third_operand_nan_is_min3(self) -> None:
# Since #2029 a NaN operand gives min3 of the operands (measured on gfx1030), not an eager FClamp on NaN.
Expand Down
15 changes: 7 additions & 8 deletions tests/semantic/test_alu_min3_max3.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
- Signed zeros are ordered with -0.0 below +0.0 (the hardware order measured on AMD RDNA2,
docs/design.md): min(+0.0, -0.0) = -0.0 and max(+0.0, -0.0) = +0.0 in either operand order.
- VOP3 input modifiers: absolute value (|src|), negation (-src), and neg-abs (-|src|).
- VOP3 output modifier: clamp to [0.0, 1.0]. If all operands are NaN, clamp raises
UnsupportedConstruct because GLSL.std.450 FClamp on NaN is undefined.
- VOP3 output modifier: clamp to [0.0, 1.0]. If all operands are NaN, the NaN result clamps to +0
(DX10_CLAMP 1 in the game's MODE; the translator selects +0 before GLSL.std.450 FClamp, which is
undefined for a NaN operand).

Integer min/max:
- v_min3_i32 and v_max3_i32 evaluate operands as signed 32-bit integers in two's complement.
Expand Down Expand Up @@ -142,7 +143,7 @@ def ref_float(op: str, a: float, b: float, c: float, *, clamp: bool = False) ->
r = ref_nmin_nmax(fn, ref_nmin_nmax(fn, a, b), c)
if clamp:
if math.isnan(r):
raise si.UnsupportedConstruct("clamp on NaN is undefined")
return 0.0 # DX10_CLAMP 1: the clamp bit turns a NaN into +0
r = max(0.0, min(1.0, r))
return r

Expand Down Expand Up @@ -330,8 +331,7 @@ class FloatNanSkippingTests(unittest.TestCase):
"""GLSL.std.450 NMin and NMax implement IEEE 754-2008 NaN-skipping:
- If one or two operands are NaN, the non-NaN operand is chosen.
- If all three operands are NaN, NaN is returned.
- If clamp modifier is specified on all-NaN, UnsupportedConstruct is raised because
FClamp on NaN is undefined.
- If clamp modifier is specified on all-NaN, the result is +0 (DX10_CLAMP 1).
"""

def test_single_nan_operand_skipped(self) -> None:
Expand Down Expand Up @@ -370,11 +370,10 @@ def test_all_three_nans_produce_nan(self) -> None:
res = run_alu(op, QNAN_BITS, SNAN_BITS, NEG_QNAN_BITS)
self.assertTrue(math.isnan(u32_to_f32(res)))

def test_clamp_on_all_nan_rejected(self) -> None:
def test_clamp_on_all_nan_gives_plus_zero(self) -> None:
for op in FLOAT_OPS:
with self.subTest(op=op):
with self.assertRaises(si.UnsupportedConstruct):
run_alu(op, QNAN_BITS, SNAN_BITS, NEG_QNAN_BITS, clamp=True)
self.assertEqual(run_alu(op, QNAN_BITS, SNAN_BITS, NEG_QNAN_BITS, clamp=True), 0)


class FloatModifiersTests(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion tests/semantic/test_alu_vop3_mods_hw.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
sample, up to three per form with distinct results. The measured cases not listed match too, except for differences
already on record: an omod turning a -0 result into +0 (docs/design.md, Output modifiers rows; #1281), f32 denormal
operands or intermediate results (SHADER-377), NaN sign/payload, 1-ulp differences in v_rcp/v_log/v_sin, and cases
whose translation is undefined in SPIR-V (FClamp of a NaN, the root or log of a negative or zero).
whose translation is undefined in SPIR-V (FClamp of a NaN, the root or log of a negative or zero). The f32/f16 clamp of a
NaN gives +0 since 2026-11-24 (test_vop3_clamp_nan.py); those rows are not replayed here.
"""
import os
import struct
Expand Down
4 changes: 2 additions & 2 deletions tests/semantic/test_valu_f32_min3_max3_mad.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
Observed translator / interpreter findings (kept out of failing assertions and documented here):
1. v_min3_f32 / v_max3_f32 with NaN operands: tools/rdna2spirv.py emits GLSL.std.450 NMin / NMax, which give the
other operand for a NaN, as RDNA2 hardware does. Those rows are asserted in tests/semantic/
test_float_nan_fract.py; NaN inputs stay out of the value assertions here (clamp-on-NaN is still FClamp, which
spirv_mini_interp.py rejects). NaN semantics are tested on v_mad_f32, v_fmac_f32, and v_mad_legacy_f32.
test_float_nan_fract.py; NaN inputs stay out of the value assertions here (clamp-on-NaN, which gives +0, is
checked in test_vop3_clamp_nan.py). NaN semantics are tested on v_mad_f32, v_fmac_f32, and v_mad_legacy_f32.
2. v_min3_f32 / v_max3_f32 signed-zero ties: RDNA2 hardware orders -0.0 below +0.0 (measured on an AMD Radeon
RX 6800: min(+0, -0) = -0 and max(+0, -0) = +0 in either operand order), and spirv_mini_interp.py's NMin / NMax
do the same, so every zero triple asserts exact bit patterns.
Expand Down
137 changes: 137 additions & 0 deletions tests/semantic/test_vop3_clamp_nan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""VOP3 `clamp` of a NaN result gives +0 (Gen.clamp_mods).

The game's MODE has DX10_CLAMP 1, and with it the clamp bit turns a NaN result into +0: measured on RDNA2 (gfx1036)
for v_rcp_iflag_f32 (docs/shader_validation.md, "32-bit float VALU on the GPU": a NaN result +0 except with DX10_CLAMP
off, 169 programs) and for v_fmac_f16, v_fma_mixlo_f16 and v_fma_mix_f32 (SHADER-186, "f16 output modifiers, clamp and
v_fma_mix* operands on RDNA2").
GLSL.std.450 leaves FClamp undefined for a NaN operand, so the translator selects +0 for a NaN before FClamp, as the
div_fixup and cvt_pknorm clamps already did. Before that the shared helper emitted a bare FClamp, which the CPU
interpreter refuses (UnsupportedConstruct) and which a driver may return as the NaN.

Each program computes one value in v4 and exports it. The NaN comes from a NaN operand (quiet, negative, signalling)
or from an invalid operation (inf - inf, -inf * 0); controls without a NaN check that the clamp itself is unchanged.
Standard library only; no game data.
"""
import os
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from support import mrt0_words, translate_lines # noqa: E402

QNAN = 0x7FC00000
NEG_QNAN = 0xFFC00000
SNAN = 0x7F800001
POS_INF = 0x7F800000
NEG_INF = 0xFF800000
ONE = 0x3F800000
TWO = 0x40000000
QUARTER = 0x3E800000
NEG_ONE = 0xBF800000
F16_QNAN = 0x7E00
F16_NEG_QNAN = 0xFE00
F16_ONE = 0x3C00
F16_HALF = 0x3800


def run(op_line, v1, v2, v3=0):
"""mrt0.x after `op_line` (which writes v4) with v1..v3 set to the given words."""
spv = translate_lines("ps_clamp_nan", [
f"v_mov_b32 v1, {v1:#x}",
f"v_mov_b32 v2, {v2:#x}",
f"v_mov_b32 v3, {v3:#x}",
"v_mov_b32 v4, 0xdeadbeef",
op_line,
"exp mrt0 v4, v4, v4, v4 done vm",
"s_endpgm",
])
return mrt0_words(spv)[0]


class ClampNanF32Tests(unittest.TestCase):
def test_nan_operand_clamps_to_plus_zero(self):
for nan in (QNAN, NEG_QNAN, SNAN):
for line in ("v_add_f32_e64 v4, v1, v2 clamp",
"v_sub_f32_e64 v4, v1, v2 clamp",
"v_mul_f32_e64 v4, v1, v2 clamp",
"v_fma_f32 v4, v1, v2, v2 clamp",
"v_mul_f32_e64 v4, v1, v2 mul:2 clamp"):
with self.subTest(op=line, nan=hex(nan)):
self.assertEqual(run(line, nan, ONE), 0)

def test_invalid_operation_clamps_to_plus_zero(self):
self.assertEqual(run("v_sub_f32_e64 v4, v1, v2 clamp", POS_INF, POS_INF), 0) # inf - inf
self.assertEqual(run("v_add_f32_e64 v4, v1, v2 clamp", POS_INF, NEG_INF), 0) # inf + -inf
self.assertEqual(run("v_mul_f32_e64 v4, v1, v2 clamp", NEG_INF, 0), 0) # -inf * 0
self.assertEqual(run("v_fma_f32 v4, v1, v2, v3 clamp", POS_INF, 0, ONE), 0) # inf * 0 + 1

def test_all_nan_min_max_clamps_to_plus_zero(self):
# Without clamp an all-NaN min/max keeps src0's NaN (docs/design.md); the clamp then gives +0.
for op in ("v_min_f32_e64", "v_max_f32_e64"):
with self.subTest(op=op):
self.assertEqual(run(f"{op} v4, v1, v2 clamp", QNAN, NEG_QNAN), 0)
for op in ("v_min3_f32", "v_max3_f32"):
with self.subTest(op=op):
self.assertEqual(run(f"{op} v4, v1, v2, v3 clamp", QNAN, SNAN, NEG_QNAN), 0)

def test_nan_without_clamp_is_still_nan(self):
# The select is only on the clamp path: without clamp a NaN result stays a NaN.
w = run("v_add_f32_e64 v4, v1, v2", QNAN, ONE)
self.assertEqual(w & 0x7F800000, 0x7F800000)
self.assertNotEqual(w & 0x007FFFFF, 0)

def test_clamp_controls_unchanged(self):
self.assertEqual(run("v_add_f32_e64 v4, v1, v2 clamp", TWO, TWO), ONE)
self.assertEqual(run("v_add_f32_e64 v4, v1, v2 clamp", NEG_ONE, 0), 0)
self.assertEqual(run("v_add_f32_e64 v4, v1, v2 clamp", QUARTER, 0), QUARTER)
self.assertEqual(run("v_mul_f32_e64 v4, v1, v2 clamp", POS_INF, ONE), ONE)
self.assertEqual(run("v_mul_f32_e64 v4, v1, v2 clamp", NEG_INF, ONE), 0)


def test_legacy_ops_nan_clamps_to_plus_zero(self):
# v_mul_legacy_f32 ... clamp is the most common clamp form in the game's listings (docs/design.md).
self.assertEqual(run("v_mul_legacy_f32_e64 v4, v1, v2 clamp", QNAN, ONE), 0)
self.assertEqual(run("v_mad_legacy_f32 v4, v1, v2, v3 clamp", QNAN, ONE, 0), 0)


# An f16 result written to the low half keeps the high half of v4 (0xdead); op_sel:[0,0,1] and v_fma_mixhi_f16 write
# the high half and keep the low one (0xbeef).
LO_ZERO = 0xDEAD0000
HI_ZERO = 0x0000BEEF


class ClampNanF16Tests(unittest.TestCase):
def test_f16_nan_clamps_to_plus_zero(self):
for nan in (F16_QNAN, F16_NEG_QNAN):
for line in ("v_add_f16_e64 v4, v1, v2 clamp",
"v_mul_f16_e64 v4, v1, v2 clamp",
"v_fmac_f16_e64 v4, v1, v2 clamp",
"v_fma_f16 v4, v1, v2, v3 clamp"):
with self.subTest(op=line, nan=hex(nan)):
self.assertEqual(run(line, nan, F16_ONE), LO_ZERO)

def test_f16_high_half_nan_clamps_to_plus_zero(self):
self.assertEqual(run("v_add_f16_e64 v4, v1, v2 op_sel:[0,0,1] clamp", F16_QNAN, F16_ONE), HI_ZERO)
self.assertEqual(run("v_fma_mixhi_f16 v4, v1, v2, v3 clamp", QNAN, ONE, 0), HI_ZERO)

def test_f16_all_nan_med3_clamps_to_plus_zero(self):
self.assertEqual(run("v_med3_f16 v4, v1, v2, v3 clamp", F16_QNAN, F16_NEG_QNAN, 0x7E01), LO_ZERO)

def test_conversions_nan_clamp_to_plus_zero(self):
self.assertEqual(run("v_cvt_f32_f16_e64 v4, v1 clamp", F16_QNAN, 0), 0)
self.assertEqual(run("v_cvt_f16_f32_e64 v4, v1 clamp", QNAN, 0), LO_ZERO)
self.assertEqual(run("v_cvt_f16_f32_e64 v4, v1 mul:2 clamp", NEG_QNAN, 0), LO_ZERO)

def test_mix_ops_nan_clamp_to_plus_zero(self):
# SHADER-186 measured +0 on gfx1036 for these (docs/shader_validation.md).
self.assertEqual(run("v_fma_mix_f32 v4, v1, v2, v3 op_sel_hi:[1,1,1] clamp", F16_QNAN, F16_ONE, 0), 0)
self.assertEqual(run("v_fma_mixlo_f16 v4, v1, v2, v3 clamp", QNAN, ONE, 0), LO_ZERO)

def test_f16_clamp_controls_unchanged(self):
self.assertEqual(run("v_add_f16_e64 v4, v1, v2 clamp", F16_ONE, F16_ONE), 0xDEAD0000 | F16_ONE)
self.assertEqual(run("v_add_f16_e64 v4, v1, v2 clamp", F16_HALF, 0), 0xDEAD0000 | F16_HALF)
self.assertEqual(run("v_fma_mixlo_f16 v4, v1, v2, v3 clamp", TWO, ONE, 0), 0xDEAD0000 | F16_ONE)


if __name__ == "__main__":
unittest.main()
3 changes: 3 additions & 0 deletions tests/test_f64_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def result(instruction):
scale = result(rf"OpBitcast %float {constant}")
value = result(rf"OpFMul %float {value} {scale}")
if clamp:
# DX10_CLAMP: a NaN is replaced by +0 before FClamp (Gen.clamp_mods).
nan = result(rf"OpIsNan %bool {value}")
value = result(rf"OpSelect %float {nan} %f0 {value}")
value = result(rf"OpExtInst %float %glsl FClamp {value} %f0 %f1")
word = result(rf"OpBitcast %uint {value}")
dest = result(r"OpAccessChain %_ptr_Function_uint %vgpr %c3")
Expand Down
3 changes: 3 additions & 0 deletions tests/test_rcp_iflag_mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def match_result(pattern):
self.assertNotRegex(spv, r"\bOpFMul %float\b")

if clamp:
# DX10_CLAMP: a NaN is replaced by +0 before FClamp (Gen.clamp_mods).
nan = match_result(rf"OpIsNan %bool {value}")
value = match_result(rf"OpSelect %float {nan} %f0 {value}")
value = match_result(rf"OpExtInst %float %glsl FClamp {value} %f0 %f1")
else:
self.assertNotIn("FClamp", spv)
Expand Down
9 changes: 7 additions & 2 deletions tools/rdna2spirv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6593,8 +6593,13 @@ def clamp_mods(self, v, mods, allow_omod=True):
self.emit(f"{g} = OpFMul %float {v} {c}")
v = g
if "clamp" in mods:
g = self.tmp()
self.emit(f"{g} = OpExtInst %float %glsl FClamp {v} %f0 %f1")
# The game's MODE has DX10_CLAMP=1, so the clamp bit turns a NaN result into +0
# (docs/shader_validation.md). GLSL.std.450 FClamp leaves a NaN operand undefined,
# so select +0 for a NaN first, as the div_fixup and cvt_pknorm clamps do.
nan, safe, g = self.tmp(), self.tmp(), self.tmp()
self.emit(f"{nan} = OpIsNan %bool {v}")
self.emit(f"{safe} = OpSelect %float {nan} %f0 {v}")
self.emit(f"{g} = OpExtInst %float %glsl FClamp {safe} %f0 %f1")
return g
return v

Expand Down