Skip to content
Open
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
3 changes: 2 additions & 1 deletion docs/design.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion runtime/test/shaders/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def not_found_message(build, missing):
("cs_w32_fcmpswap", [0, 0x3F800000, 0x7FC00000, 0x3F800000, 0x80000000, 0x40400000, 0x3F800000, 0x7FC00000]),
("cs_w32_new_ops", [0x55, 0x8001, 0xC0000000, 0x80014000, 0x4000FFFF, 0x77, 0x99, 0x1234]),
# v_dot2_f32_f16 / v_dot2c_f32_f16 (derivations in cs_w32_dot2_f16.asm)
("cs_w32_dot2_f16", [0x41280000, 0x41600000, 0xC1080000, 0x41480000, 0x3F800000, 0x00000000, 0x411C0000, 0x3EC00004]),
("cs_w32_dot2_f16", [0x41280000, 0x41600000, 0xC1080000, 0x41480000, 0x41280000, 0xC1080000, 0x411C0000, 0x3EC00004]),
("cs_w32_misc_ext", [0x0FFF0FFF, 0xE2C4A688, 0x0007FFFF, 0x7FFF8000, 0xFFFE0002, 0xFFFF0005, 0x509, 0xCAFEF00D]),
("cs_w32_salu64", [0x1E6A2C48, 0x1234, 0xFFFFFFFF, 0x100, 47, 48, 0xFFFF8000, 0xFFFFFFFE]),
# SHADER-85: 8-bit LDS reads into one half of a VGPR, the other half kept
Expand Down
9 changes: 5 additions & 4 deletions runtime/test/shaders/cs_w32_dot2_f16.asm
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
; v_dot2_f32_f16 and the old D for v_dot2c_f32_f16 (docs/design.md, "Packed integer dot products ... v_dot2_f32_f16").
; gfx1013 has neither instruction, so this file is assembled for gfx1030 (the "; mcpu:" line above).
; Every product and sum below is exact in f32, so the words do not depend on how a GPU rounds or fuses; what they
; check is which half each operand reads (op_sel / op_sel_hi), the negations, the accumulator and clamp.
; check is which half each operand reads (op_sel / op_sel_hi), the negations, the accumulator, and that clamp
; changes nothing: RDNA2 hardware ignores v_dot2_f32_f16's clamp bit (docs/design.md, "v_dot2_f32_f16 clamp").
; Halves: 2.0 = 0x4000, 3.0 = 0x4200, 4.0 = 0x4400, 0.5 = 0x3800, 1.5 = 0x3e00, 0.25 = 0x3400, 2^-24 = 0x0001.
; A = 0x42004000 (lo 2.0, hi 3.0) B = 0x38004400 (lo 4.0, hi 0.5) C = 1.0f
; out[0] v_dot2_f32_f16 A, B, C 2*4 + 3*0.5 + 1 = 10.5 0x41280000
; out[1] op_sel:[1,0,0] op_sel_hi:[0,1,1] (A's halves swapped) 3*4 + 2*0.5 + 1 = 14.0 0x41600000
; out[2] neg_lo:[1,0,0] neg_hi:[0,1,0] (-a0, -b1) -2*4 + 3*-0.5 + 1 = -8.5 0xc1080000
; out[3] op_sel:[1,0,0] op_sel_hi:[1,1,1] neg_lo:[0,0,1] (both from A.hi, -acc) 3*4 + 3*0.5 - 1 = 12.5 0x41480000
; out[4] clamp 10.5 -> 1.0 0x3f800000
; out[5] neg_lo:[1,0,0] neg_hi:[0,1,0] clamp -8.5 -> 0 0x00000000
; out[4] clamp (ignored) 10.5 = 10.5 0x41280000
; out[5] neg_lo:[1,0,0] neg_hi:[0,1,0] clamp (ignored) -8.5 = -8.5 0xc1080000
; out[6] v_dot2c_f32_f16 A, B with D = 0.25 8 + 1.5 + 0.25 = 9.75 0x411c0000
; out[7] v_dot2c_f32_f16 0x3e000001, 0x34004000 with D = 0 (a denormal half: 2^-24 * 2 + 1.5 * 0.25)
; 2^-23 + 0.375 0x3ec00004
; group: 32,1,1
; Run: vk_compute_test cs_w32_dot2_f16.spv 1 dump -> out[0..7] = 1093140480 1096810496 3238526976 1095237632
; 1065353216 0 1092354048 1052770308
; 1093140480 3238526976 1092354048 1052770308
; mattr: +wavefrontsize32,-wavefrontsize64
v_mov_b32_e32 v1, 0x42004000
v_mov_b32_e32 v2, 0x38004400
Expand Down
4 changes: 2 additions & 2 deletions scripts/run_gpu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,8 @@ def run_suite(a, work: str) -> int:
# ds_cmpst_f32 and buffer_atomic_fcmpswap: float compare (cs_w32_fcmpswap.asm)
"cs_w32_fcmpswap": [0, 0x3F800000, 0x7FC00000, 0x3F800000, 0x80000000, 0x40400000, 0x3F800000, 0x7FC00000],
"cs_w32_new_ops": [0x55, 0x8001, 0xC0000000, 0x80014000, 0x4000FFFF, 0x77, 0x99, 0x1234],
# v_dot2_f32_f16 / v_dot2c_f32_f16: half selects, negations, accumulator, clamp (cs_w32_dot2_f16.asm)
"cs_w32_dot2_f16": [0x41280000, 0x41600000, 0xC1080000, 0x41480000, 0x3F800000, 0x00000000, 0x411C0000, 0x3EC00004],
# v_dot2_f32_f16 / v_dot2c_f32_f16: half selects, negations, accumulator, ignored clamp (cs_w32_dot2_f16.asm)
"cs_w32_dot2_f16": [0x41280000, 0x41600000, 0xC1080000, 0x41480000, 0x41280000, 0xC1080000, 0x411C0000, 0x3EC00004],
"cs_w32_misc_ext": [0x0FFF0FFF, 0xE2C4A688, 0x0007FFFF, 0x7FFF8000, 0xFFFE0002, 0xFFFF0005, 0x509, 0xCAFEF00D],
"cs_w32_salu64": [0x1E6A2C48, 0x1234, 0xFFFFFFFF, 0x100, 47, 48, 0xFFFF8000, 0xFFFFFFFE],
# SHADER-85: ds_read_u8/i8_d16[_hi] (cs_w32_lds_u8_d16.asm header)
Expand Down
15 changes: 15 additions & 0 deletions tests/semantic/fixtures/dot2_f32_f16_clamp.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; v_dot2_f32_f16 with clamp: the hardware ignores the bit (gfx1031, tools/hw_oracle, wave32 and wave64).
; Expected MRT0 words: 41280000 c1080000 3fa00000 4fffc004 (10.5, -8.5, 1.25, 2 * 65504^2), all unclamped.
v_mov_b32 v1, 0x42004000
v_mov_b32 v2, 0x38004400
v_mov_b32 v3, 1.0
v_dot2_f32_f16 v4, v1, v2, v3 clamp
v_dot2_f32_f16 v5, v1, v2, v3 neg_lo:[1,0,0] neg_hi:[0,1,0] clamp
v_mov_b32 v1, 0x38003400
v_mov_b32 v2, 0x3c003c00
v_mov_b32 v3, 0.5
v_dot2_f32_f16 v6, v1, v2, v3 clamp
v_mov_b32 v1, 0x7bff7bff
v_dot2_f32_f16 v7, v1, v1, 0 clamp
exp mrt0 v4, v5, v6, v7 done vm
s_endpgm
35 changes: 12 additions & 23 deletions tests/semantic/test_alu_dot_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Covers the 7 remaining RDNA2 dot-product instructions:
- v_dot2c_i32_i16, v_dot2c_u32_u16, v_dot4c_u32_u8, v_dot8c_i32_i4, v_dot8c_u32_u4 (VOP2 accumulating)
- v_dot2_f32_f16 (VOP3P half-to-float dot product with op_sel, op_sel_hi, neg_lo, neg_hi, clamp)
- v_dot2c_f32_f16 (VOP2 accumulating half-to-float dot product with optional clamp)
- v_dot2_f32_f16 (VOP3P half-to-float dot product with op_sel, op_sel_hi, neg_lo, neg_hi; clamp is accepted
and, as on the hardware, has no effect)
- v_dot2c_f32_f16 (VOP2 accumulating half-to-float dot product, no modifiers)

Standard library only; no GPU, no LLVM, no SPIRV-Tools, no game files.
"""
Expand Down Expand Up @@ -110,7 +111,6 @@ def ref_dot2_f32_f16(
selhi=(1, 1, 1),
nlo=(0, 0, 0),
nhi=(0, 0, 0),
clamp: bool = False,
) -> int:
a0 = bits_f16((s0 >> (16 * sel[0])) & 0xFFFF)
a1 = bits_f16((s0 >> (16 * selhi[0])) & 0xFFFF)
Expand All @@ -131,8 +131,6 @@ def ref_dot2_f32_f16(
p1 = f32(a1 * b1)
s = f32(p0 + p1)
r = f32(s + acc)
if clamp:
r = max(0.0, min(1.0, r))
return f32_bits(r)


Expand Down Expand Up @@ -236,36 +234,26 @@ def test_v_dot2_f32_f16_and_v_dot2c_f32_f16_values(self):
self.assertEqual(got[0], want)
self.assertEqual(got[3], want)

def test_v_dot2_f32_f16_opsel_neg_and_clamp(self):
def test_v_dot2_f32_f16_opsel_neg_and_ignored_clamp(self):
s0 = pack_f16x2(1.5, -2.0)
s1 = pack_f16x2(4.0, 0.5)
acc = f32_bits(0.75)
cases = [
("op_sel:[1,0,0] op_sel_hi:[0,1,1]", (1, 0, 0), (0, 1, 1), (0, 0, 0), (0, 0, 0), False),
("neg_lo:[1,0,0] neg_hi:[0,1,0]", (0, 0, 0), (1, 1, 1), (1, 0, 0), (0, 1, 0), False),
("neg_lo:[0,0,1]", (0, 0, 0), (1, 1, 1), (0, 0, 1), (0, 0, 0), False),
("clamp", (0, 0, 0), (1, 1, 1), (0, 0, 0), (0, 0, 0), True),
("neg_lo:[1,1,1] clamp", (0, 0, 0), (1, 1, 1), (1, 1, 1), (0, 0, 0), True),
("op_sel:[1,0,0] op_sel_hi:[0,1,1]", (1, 0, 0), (0, 1, 1), (0, 0, 0), (0, 0, 0)),
("neg_lo:[1,0,0] neg_hi:[0,1,0]", (0, 0, 0), (1, 1, 1), (1, 0, 0), (0, 1, 0)),
("neg_lo:[0,0,1]", (0, 0, 0), (1, 1, 1), (0, 0, 1), (0, 0, 0)),
("clamp", (0, 0, 0), (1, 1, 1), (0, 0, 0), (0, 0, 0)),
("neg_lo:[1,1,1] clamp", (0, 0, 0), (1, 1, 1), (1, 1, 1), (0, 0, 0)),
]
for mods, sel, selhi, nlo, nhi, clamp in cases:
for mods, sel, selhi, nlo, nhi in cases:
with self.subTest(mods=mods):
got = run_ps([
f"v_mov_b32 v1, {s0:#x}",
f"v_mov_b32 v2, {s1:#x}",
f"v_mov_b32 v3, {acc:#x}",
f"v_dot2_f32_f16 v0, v1, v2, v3 {mods}",
])[0]
self.assertEqual(got, ref_dot2_f32_f16(s0, s1, acc, sel, selhi, nlo, nhi, clamp=clamp))

# v_dot2c_f32_f16 clamp
got_c = run_ps([
f"v_mov_b32 v0, {f32_bits(0.5):#x}",
f"v_mov_b32 v1, {pack_f16x2(2.0, 1.0):#x}",
f"v_mov_b32 v2, {pack_f16x2(1.0, 1.0):#x}",
"v_mov_b32 v3, 0",
"v_dot2c_f32_f16_e64 v0, v1, v2 clamp",
])[0]
self.assertEqual(got_c, f32_bits(1.0))
self.assertEqual(got, ref_dot2_f32_f16(s0, s1, acc, sel, selhi, nlo, nhi))

def test_subnormals_inf_and_nan(self):
# f16 subnormal input (2^-24) * 2.0 + 0.0 -> 2^-23 (exact normal f32, kept)
Expand Down Expand Up @@ -336,6 +324,7 @@ def test_rejects_unsupported_forms(self):
"v_dot2c_f32_f16 v0, v1, v2 neg_lo:[1,0,0]",
"v_dot2c_f32_f16_sdwa v0, v1, v2 dst_sel:DWORD",
"v_dot2c_f32_f16_e64 v0, |v1|, v2",
"v_dot2c_f32_f16_e64 v0, v1, v2 clamp",
"v_dot2c_f32_f16 s0, v1, v2",
]
for line in bad_lines:
Expand Down
66 changes: 66 additions & 0 deletions tests/semantic/test_dot2_f32_f16_clamp_hw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""v_dot2_f32_f16 `clamp` is a no-op on RDNA2: the translator gives the words measured on the hardware.

Measured on an AMD Radeon RX 6700 XT (gfx1031, RADV / amdgpu DRM, `tools/hw_oracle`) in wave32 and wave64, which
agree word for word. Over 2,560 random and special inputs per wave size the words with and without `clamp` were
identical, while `v_fma_f32 ... clamp` in the same kernels clamped. The console's gfx1013 has no `v_dot2_f32_f16`
(docs/design.md, GFX10.1 / GFX10.3 opcode differences), so desktop gfx103x is the only hardware for it.
"""

import math
from pathlib import Path
import struct
import unittest

from support import HERE, mrt0_words, translate_listing, translate_lines

# (S0, S1, S2, extra modifiers, measured v_dot2_f32_f16 ... clamp word)
HW_WORDS = [
(0x42004000, 0x38004400, 0x3f800000, "", 0x41280000),
(0x42004000, 0x38004400, 0x3f800000, "neg_lo:[1,0,0] neg_hi:[0,1,0]", 0xc1080000),
(0x38003400, 0x3c003c00, 0x3f000000, "", 0x3fa00000),
(0x38003400, 0x3c003c00, 0x3e800000, "", 0x3f800000),
(0x38003400, 0x3c003c00, 0xbe800000, "", 0x3f000000),
(0x3c00bc00, 0x3c003c00, 0x00000000, "", 0x00000000),
(0xbc00bc00, 0x3c003c00, 0x00000000, "", 0xc0000000),
(0x7bff7bff, 0x7bff7bff, 0x00000000, "", 0x4fffc004),
(0x42004000, 0x38004400, 0x7fc00000, "", 0x7fc00000),
(0x42004000, 0x38004400, 0x7f800000, "", 0x7f800000),
(0x42004000, 0x38004400, 0xff800000, "", 0xff800000),
(0x7c003c00, 0x00003c00, 0x00000000, "", 0xffc00000),
(0x40003c00, 0x44004200, 0x3f800000, "op_sel:[1,0,0] op_sel_hi:[0,1,1]", 0x41300000),
(0x40003c00, 0x44004200, 0xc1a00000, "neg_lo:[0,0,1]", 0x41f80000),
]


def is_nan(word):
return math.isnan(struct.unpack("<f", struct.pack("<I", word))[0])


class Dot2F32F16ClampHw(unittest.TestCase):
def test_fixture(self):
fixture = Path(HERE) / "fixtures" / "dot2_f32_f16_clamp.asm"
text = translate_listing("ps_dot2_f32_f16_clamp", fixture.read_bytes())
self.assertEqual(mrt0_words(text), [0x41280000, 0xC1080000, 0x3FA00000, 0x4FFFC004])
self.assertNotIn("FClamp", text)

def test_measured_words(self):
for s0, s1, s2, mods, want in HW_WORDS:
with self.subTest(s0=hex(s0), s1=hex(s1), s2=hex(s2), mods=mods):
text = translate_lines("ps_dot2_f32_f16_clamp_hw", [
f"v_mov_b32 v1, {s0:#x}",
f"v_mov_b32 v2, {s1:#x}",
f"v_mov_b32 v3, {s2:#x}",
f"v_dot2_f32_f16 v0, v1, v2, v3 {mods} clamp".replace(" ", " "),
"exp mrt0 v0, v0, v0, v0 done vm",
"s_endpgm",
])
got = mrt0_words(text)[0]
if is_nan(want):
# SPIR-V keeps neither the payload nor the sign of a NaN.
self.assertTrue(is_nan(got), hex(got))
else:
self.assertEqual(got, want)


if __name__ == "__main__":
unittest.main()
7 changes: 5 additions & 2 deletions tools/rdna2spirv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7148,7 +7148,9 @@ def bits(name, default):
self.emit(f"{t} = OpFNegate %float {acc}")
acc = t
else:
if any(m != "clamp" for m in mods):
# The VOP2 form has no clamp bit, and gfx103x has no VOP3 form (llvm-mc rejects
# v_dot2c_f32_f16_e64 and calls the bytes an invalid encoding), so a clamp here is unmeasurable.
if mods:
raise Unsupported(op + " modifiers")
a0 = self.f16_in(a[1], 0)
a1 = self.f16_in(a[1], 1)
Expand All @@ -7160,7 +7162,8 @@ def bits(name, default):
self.emit(f"{p1} = OpFMul %float {a1} {b1}")
self.emit(f"{s} = OpFAdd %float {p0} {p1}")
self.emit(f"{r} = OpFAdd %float {s} {acc}")
r = self.clamp_mods(r, ["clamp"] if "clamp" in mods else [])
# v_dot2_f32_f16 `clamp` is encoded but the hardware ignores it (measured on gfx1031, docs/design.md
# "v_dot2_f32_f16 clamp"), so it is accepted and not applied.
self.dst(a[0], self.tou(r))
elif op in ("v_pk_lshlrev_b16", "v_pk_lshrrev_b16", "v_pk_ashrrev_i16"):
# Packed 16-bit shifts (VOP3P): result half h = src1 half (op_sel / op_sel_hi) shifted by src0 half & 15.
Expand Down