Skip to content
4 changes: 2 additions & 2 deletions docs/design.md

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion runtime/test/shaders/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ def heap_init_env():
"ps_vint_minmax": (0x7ffffffe, 0x80000001, 0x80000001, 0x7ffffffe),
"ps_vint_minmax3": (0xf0f0f0f0, 0xab800000, 0xf0f0f0f0, 0xab800000),
# v_readlane_b32 across both halves of a wave64 (the .asm header derives the words)
"ps_w64x_readlane": (0x40000079, 0x40000010, 0x40000064, 0x400000be)}
"ps_w64x_readlane": (0x40000079, 0x40000010, 0x40000064, 0x400000be),
# v_mad_legacy_f32 / v_mac_legacy_f32: legacy zero rule and denormal flush (the .asm header derives the words)
"ps_mad_legacy": (0x3f800000, 0xc0000000, 0x00800000, 0x40400000),
# v_mad_f32 / v_mac_f32 denormal flush: operand, product, result, after div:2 (.asm header)
"ps_mad_flush": (0x3f800000, 0x00800000, 0x80000000, 0x00000000)}
# Pixel shaders that are exact only when the pixel stage really ran 64 lanes wide. vk_harness says so when it chained
# a required subgroup size onto the fragment stage ("fragment subgroup size: N", the whole line; a device that could
# not take the size gets "fragment subgroup size: N not available" or no line). A device that cannot
Expand Down
27 changes: 27 additions & 0 deletions runtime/test/shaders/ps_mad_flush.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; v_mad_f32 and v_mac_f32 (D is the addend): D = S0 * S1 + S2 with the product rounded to f32 before the add (not
; fused). docs/design.md (SHADER-118, "Denormal flush in v_mad_f32 ..."): these opcodes have no f32 denormals, so a
; subnormal operand, the rounded product and the result (after omod) read as zero of their sign.
; v0 = 0x00000001 (the smallest denormal), v1 = 2^126 (0x7e800000), v2 = 2^-70 (0x1c800000),
; v3 = FLT_MIN = 2^-126 (0x00800000), v4 = -2^-125 (0x81000000), v5 = 1.5 * 2^-126 (0x00c00000), v8 = FLT_MIN.
; x: v_mad_f32 v6, v0, v1, 1.0: S0 flushed to +0, so 0 * 2^126 + 1.0 = 1.0 -> 0x3f800000
; (unflushed: 2^-149 * 2^126 = 2^-23, 1.0 + 2^-23 = 0x3f800001)
; y: v_mac_f32 v8, v2, v2: 2^-70 * 2^-70 = 2^-140, a denormal product, flushed to +0;
; D = +0 + 2^-126 = 0x00800000 (unflushed 2^-126 + 2^-140 = 0x00800200)
; z: v_mad_f32 v7, v4, 1.0, v5: -2^-125 + 1.5 * 2^-126 = -0.5 * 2^-126 = -2^-127, a denormal result,
; flushed to -0 -> 0x80000000 (unflushed 0x80400000)
; w: v_mad_f32 v9, v3, 1.0, 0 div:2: 2^-126 + 0 = 2^-126, div:2 gives 2^-127, then the flush: +0 -> 0x00000000
; (a flush before the omod would leave 0x00400000)
; Expected mrt0 bits: 0x3f800000, 0x00800000, 0x80000000, 0x00000000
v_mov_b32 v0, 1
v_mov_b32 v1, 0x7e800000
v_mov_b32 v2, 0x1c800000
v_mov_b32 v3, 0x00800000
v_mov_b32 v4, 0x81000000
v_mov_b32 v5, 0x00c00000
v_mov_b32 v8, 0x00800000
v_mad_f32 v6, v0, v1, 1.0
v_mac_f32 v8, v2, v2
v_mad_f32 v7, v4, 1.0, v5
v_mad_f32 v9, v3, 1.0, 0 div:2
exp mrt0 v6, v8, v7, v9 done vm
s_endpgm
30 changes: 30 additions & 0 deletions runtime/test/shaders/ps_mad_legacy.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
; v_mad_legacy_f32 (VOP3) and v_mac_legacy_f32 (VOP2, D is the addend), which had CPU checks only.
; RDNA ISA: D = S0 * S1 + S2 with the legacy multiply: if S0 == 0.0 or S1 == 0.0 the product is +0.0, whatever the
; other operand is (inf and NaN included); otherwise the product is an ordinary f32 multiply. docs/design.md
; (SHADER-118, "Denormal flush in v_mad_f32 ..."): these opcodes have no f32 denormals, so a subnormal operand, the
; rounded product and the result read as zero of their sign.
; v0 = +0, v1 = -0, v2 = +inf, v3 = qNaN, v7 = 2^-70 (0x1c800000), v9 = 0x80000001 (the smallest negative denormal),
; v10 = FLT_MIN = 2^-126 (0x00800000), v8 = 3.0.
; x: v_mad_legacy_f32 v4, v0, v2, 1.0: S0 is 0, so 0 * inf is +0 (an ordinary multiply gives NaN);
; +0 + 1.0 = 1.0 -> 0x3f800000
; y: v_mad_legacy_f32 v5, v3, v1, -2.0: S1 is -0, so NaN * -0 is +0; +0 + -2.0 = -2.0 -> 0xc0000000
; z: v_mad_legacy_f32 v6, v7, v7, v10: 2^-70 * 2^-70 = 2^-140, a denormal product, flushed to +0;
; +0 + 2^-126 = 0x00800000 (unflushed it would be 2^-126 + 2^-140 =
; 0x00800200, 2^-140 being 2^9 ulps of 2^-126)
; w: v_mac_legacy_f32 v8, v9, v2: S0 = -denormal flushed to -0, so the legacy product is +0 (unflushed
; it is -inf); D = +0 + 3.0 = 3.0 -> 0x40400000
; Expected mrt0 bits: 0x3f800000, 0xc0000000, 0x00800000, 0x40400000
v_mov_b32 v0, 0
v_mov_b32 v1, 0x80000000
v_mov_b32 v2, 0x7f800000
v_mov_b32 v3, 0x7fc00000
v_mov_b32 v7, 0x1c800000
v_mov_b32 v9, 0x80000001
v_mov_b32 v10, 0x00800000
v_mov_b32 v8, 0x40400000
v_mad_legacy_f32 v4, v0, v2, 1.0
v_mad_legacy_f32 v5, v3, v1, -2.0
v_mad_legacy_f32 v6, v7, v7, v10
v_mac_legacy_f32 v8, v9, v2
exp mrt0 v4, v5, v6, v8 done vm
s_endpgm
4 changes: 4 additions & 0 deletions scripts/run_gpu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,10 @@ def run_suite(a, work: str) -> int:
"ps_vint_minmax": (0x7FFFFFFE, 0x80000001, 0x80000001, 0x7FFFFFFE),
"ps_vint_minmax3": (0xF0F0F0F0, 0xAB800000, 0xF0F0F0F0, 0xAB800000),
"ps_w64x_readlane": (0x40000079, 0x40000010, 0x40000064, 0x400000BE), # same expectation as check.py
# v_mad_legacy_f32 / v_mac_legacy_f32: legacy zero rule and denormal flush (.asm header)
"ps_mad_legacy": (0x3F800000, 0xC0000000, 0x00800000, 0x40400000),
# v_mad_f32 / v_mac_f32 denormal flush: operand, product, result, after div:2 (.asm header)
"ps_mad_flush": (0x3F800000, 0x00800000, 0x80000000, 0x00000000),
}
if skip := wide_pixel_skip(name, msg): # a 64-wide pixel stage the device could not give: nothing to judge
results.append((name, None, str(skip)))
Expand Down
14 changes: 14 additions & 0 deletions tests/semantic/fixtures/disasm/ps_mad_flush.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; ps_mad_flush test fixture generated by tests/semantic/regen_fixtures.py from runtime/test/shaders/ps_mad_flush.asm
v_mov_b32 v0, 1
v_mov_b32 v1, 0x7e800000
v_mov_b32 v2, 0x1c800000
v_mov_b32 v3, 0x00800000
v_mov_b32 v4, 0x81000000
v_mov_b32 v5, 0x00c00000
v_mov_b32 v8, 0x00800000
v_mad_f32 v6, v0, v1, 1.0
v_mac_f32 v8, v2, v2
v_mad_f32 v7, v4, 1.0, v5
v_mad_f32 v9, v3, 1.0, 0 div:2
exp mrt0 v6, v8, v7, v9 done vm
s_endpgm
15 changes: 15 additions & 0 deletions tests/semantic/fixtures/disasm/ps_mad_legacy.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; ps_mad_legacy test fixture generated by tests/semantic/regen_fixtures.py from runtime/test/shaders/ps_mad_legacy.asm
v_mov_b32 v0, 0
v_mov_b32 v1, 0x80000000
v_mov_b32 v2, 0x7f800000
v_mov_b32 v3, 0x7fc00000
v_mov_b32 v7, 0x1c800000
v_mov_b32 v9, 0x80000001
v_mov_b32 v10, 0x00800000
v_mov_b32 v8, 0x40400000
v_mad_legacy_f32 v4, v0, v2, 1.0
v_mad_legacy_f32 v5, v3, v1, -2.0
v_mad_legacy_f32 v6, v7, v7, v10
v_mac_legacy_f32 v8, v9, v2
exp mrt0 v4, v5, v6, v8 done vm
s_endpgm
2 changes: 2 additions & 0 deletions tests/semantic/regen_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"ps_vint_mul24",
"ps_vint_minmax",
"ps_vint_minmax3",
"ps_mad_legacy", # v_mad_legacy_f32 / v_mac_legacy_f32: legacy zero rule and denormal flush
"ps_mad_flush",
"ps_vcmp_f32_nan", "ps_vcmp_64", "cs_w32_andn2_exec") # VOPC f32 NaN / 64-bit int compares; s_andn2_saveexec/wrexec_b32


Expand Down
39 changes: 39 additions & 0 deletions tests/semantic/test_value_semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
Measured on gfx1036 (ps_valu16_mad_sel.asm): op_sel bit 2 adds S2[31:16] zero-extended
(i16 too), bit 3 writes the result << 16, clamp saturates to u32 / i32 before that shift.
v_subrev_f16 D.lo = S1.lo - S0.lo; the high half of D is kept in the VOP2 and the VOP3 form.
v_mad_legacy_f32 / D = S0 * S1 + S2 (mac: + D), the product +0 when S0 or S1 is 0 (inf/NaN included);
v_mac_legacy_f32 a denormal operand, product or result reads as zero of its sign (SHADER-118).
v_mad_f32 / v_mac_f32 D = S0 * S1 + S2 (mac: + D), product rounded before the add; the same flush, the result's
after omod.
16-bit SDWA ops with dst_sel:DWORD the 16-bit result is written zero-extended: the high half of D is
(v_add/mul_f16, cleared whatever dst_unused says (measured on gfx1036, ps_sdwa_dword_hi.asm).
v_cvt_u16_f16,
Expand Down Expand Up @@ -115,6 +119,8 @@
VCMP_F32_HEAP = (0x7FC10001, 0x3F800000, 0x40000000) # NaN, 1.0, 2.0
# Four (A, B) pairs as A.lo A.hi B.lo B.hi (ps_vcmp_64.asm header).
VCMP_64_HEAP = (0x80000000, 0, 1, 0, 0xFFFFFFFF, 0xFFFFFFFF, 1, 0, 1, 0, 0x80000000, 0, 7, 5, 7, 5)
EXPECTED_MAD_LEGACY = [0x3F800000, 0xC0000000, 0x00800000, 0x40400000]
EXPECTED_MAD_FLUSH = [0x3F800000, 0x00800000, 0x80000000, 0x00000000]


def _f32_bits(x):
Expand Down Expand Up @@ -453,6 +459,29 @@ def ff1(m):
e1, e2 = vcc & ~0x0000FFFF & 0xFFFFFFFF, vcc & ~0xFFFFFFFF & 0xFFFFFFFF
out.append((5 if e1 else 6) << 8 | (7 if e2 else 8))
return out
def _from_bits(b):
return struct.unpack("<f", struct.pack("<I", b))[0]
def _flush(x):
"""No f32 denormals in the mad opcodes: a subnormal reads as zero of its sign (inf and NaN pass)."""
return math.copysign(0.0, x) if abs(x) < 2.0 ** -126 else x
def _mad_legacy(s0, s1, s2):
"""v_mad_legacy_f32 on bit patterns: flushed operands, legacy product rounded to f32 and flushed, then the add."""
x, y, z = (_flush(_from_bits(b)) for b in (s0, s1, s2))
prod = 0.0 if x == 0.0 or y == 0.0 else _flush(_from_bits(_f32_bits(x * y)))
return _f32_bits(_flush(_from_bits(_f32_bits(prod + z))))
def _mad(s0, s1, s2, omod=1.0):
"""v_mad_f32 on bit patterns: flushed operands, product rounded to f32 and flushed, the sum, omod, flush."""
x, y, z = (_flush(_from_bits(b)) for b in (s0, s1, s2))
prod = _flush(_from_bits(_f32_bits(x * y)))
return _f32_bits(_flush(_from_bits(_f32_bits(_from_bits(_f32_bits(prod + z)) * omod))))
def reference_mad_flush():
one = _f32_bits(1.0)
return [_mad(0x00000001, 0x7E800000, one), _mad(0x1C800000, 0x1C800000, 0x00800000),
_mad(0x81000000, one, 0x00C00000), _mad(0x00800000, one, 0, omod=0.5)]
def reference_mad_legacy():
zero, nzero, inf, nan = 0x00000000, 0x80000000, 0x7F800000, 0x7FC00000
return [_mad_legacy(zero, inf, _f32_bits(1.0)), _mad_legacy(nan, nzero, _f32_bits(-2.0)),
_mad_legacy(0x1C800000, 0x1C800000, 0x00800000), _mad_legacy(0x80000001, inf, _f32_bits(3.0))]
def _isg1_dxbc(elements):
"""A DXBC container with one ISG1 input signature. elements: (register, mask, system value, component type)."""
payload = bytearray(struct.pack("<II", len(elements), 8))
Expand Down Expand Up @@ -510,6 +539,8 @@ def test_reference_matches_constants(self):
self.assertEqual(reference_vcmp_f32_nan(), EXPECTED_VCMP_F32_NAN)
self.assertEqual(reference_vcmp_64(), EXPECTED_VCMP_64)
self.assertEqual(reference_andn2_exec(), EXPECTED_ANDN2_EXEC)
self.assertEqual(reference_mad_legacy(), EXPECTED_MAD_LEGACY)
self.assertEqual(reference_mad_flush(), EXPECTED_MAD_FLUSH)


class TranslatorValueSemantics(unittest.TestCase):
Expand Down Expand Up @@ -602,6 +633,14 @@ def flushing(h):
with mock.patch.object(si, "half_to_float", flushing):
self.check("ps_sdwa_f16_const", EXPECTED_SDWA_F16_CONST)

def test_mad_mac_legacy_zero_rule_and_denormal_flush(self):
# 0 * inf and NaN * -0 give +0 before the add; a denormal product (2^-140) and a denormal operand are
# flushed, so FLT_MIN stays FLT_MIN and -denormal * inf is +0; v_mac_legacy_f32 adds D.
self.check("ps_mad_legacy", EXPECTED_MAD_LEGACY)
def test_mad_mac_f32_denormal_flush(self):
# A denormal operand, a denormal product (2^-140) and a denormal sum read as zero of their sign; the
# result is flushed after div:2, so FLT_MIN div:2 is +0.
self.check("ps_mad_flush", EXPECTED_MAD_FLUSH)
def test_s_buffer_load_dwordx2_into_vcc(self):
# The V# in s[0:3] points at 0x1000; the two dwords must come back in vcc_lo and vcc_hi, not be lost.
push = [0x1000, 0, 16, 0] + [0] * 28
Expand Down
Loading