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
1 change: 1 addition & 0 deletions docs/design.md

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion runtime/test/shaders/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def not_found_message(build, missing):
("cs_dpp_ops", [100, 10, 11, 12, 13, 14, 15, 16]),
("cs_dpp_bound", [10, 10, 11, 12, 13, 14, 15, 16]),
("cs_dpp_bank", [0xffffffff, 0xfffffffe, 0xfffffffd, 0xfffffffc, 7, 7, 7, 7]),
("cs_dpp_fi", [1, 2, 3, 0x7777, 5, 6, 7, 0x7777])]
("cs_dpp_fi", [1, 2, 3, 0x7777, 5, 6, 7, 0x7777]),
# a VOPC compare with a DPP src0: 1 for the pair compare, 2 for the dpp8 one (cs_dpp_cmp.asm header)
("cs_dpp_cmp", [3, 2, 3, 2, 1, 0, 1, 0])]
WAVE32_DUMPS += tuple(n for n, _ in CS_DPP)
WAVE32_LANE_DUMPS += tuple(n for n, _ in CS_DPP)
CS_DUMPS = [*CS_DPP, ("cs_store", CS_STORE), ("cs_smem_soffset_imm", CS_SMEM),
Expand Down
22 changes: 22 additions & 0 deletions runtime/test/shaders/cs_dpp_cmp.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; DPP on a VOPC compare: src0 is read from the permuted lane, src1 from the lane itself, and the result goes to VCC.
; v1 = float(lane). The first compare takes src0 from the other lane of each pair (quad_perm [1,0,3,2]), so
; "neighbour > self" is true on even lanes. The second takes it from the other half of each 8 lanes (dpp8), so it
; is true on lanes 0-3. out = (first ? 1 : 0) + (second ? 2 : 0).
; Run: vk_compute_test cs_dpp_cmp.spv 1 dump -> out[0..7] = 3 2 3 2 1 0 1 0
; llvm-mc assembles neither compare for gfx1013 ("dpp variant of this instruction is not supported"), so the GPU
; test takes this file's text as its listing; tools/shader_disasm.py prints the same lines for the encoded words.
; run_gpu_tests: text only, llvm-mc has no gfx1013 encoding for an instruction here
; regen_fixtures: this source has a translator fixture in tests/semantic/fixtures/disasm
v_mbcnt_lo_u32_b32 v0, -1, 0
v_cvt_f32_u32 v1, v0
v_mov_b32 v3, 0
v_mov_b32 v4, 1
v_mov_b32 v5, 2
v_cmp_gt_f32_dpp vcc_lo, v1, v1 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1
v_cndmask_b32 v6, v3, v4, vcc_lo
v_cmp_gt_f32_dpp vcc_lo, v1, v1 dpp8:[4,5,6,7,0,1,2,3]
v_cndmask_b32 v7, v3, v5, vcc_lo
v_add_nc_u32 v6, v6, v7
v_lshlrev_b32 v2, 2, v0
buffer_store_dword v6, v2, s[0:3], 0 offen
s_endpgm
18 changes: 16 additions & 2 deletions scripts/run_gpu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,23 @@ def make_ms_psv0(threads_x: int, maxv: int, maxp: int, topology: int = 2, group_
return struct.pack("<I", len(r)) + bytes(r)


# A comment line a .asm carries when llvm-mc cannot assemble it (cs_dpp_cmp.asm: a VOPC compare in DPP form).
TEXT_ONLY_MARK = "; run_gpu_tests: text only, llvm-mc has no gfx1013 encoding for an instruction here"


def assemble_to_s(llvm_mc: str, asm_path: str, s_path: str, name: str) -> None:
"""Assemble raw RDNA2 .asm with llvm-mc -show-encoding into .s format for rdna2spirv.py."""
with open(asm_path, encoding="utf-8") as source:
raw = source.read()
if any(ln.strip() == TEXT_ONLY_MARK for ln in raw.splitlines()):
# The source holds an instruction llvm-mc has no gfx1013 encoding for. The translator reads straight-line
# code from its text alone (tests/semantic/regen_fixtures.py writes such listings), so the lines go through
# as they are.
lines = [f"; {name} code 0x0..0x0 (synthetic), gfx1013, 0 invalid words"]
lines += ["\t" + ln.strip() for ln in raw.splitlines() if ln.strip() and not ln.lstrip().startswith(";")]
with open(s_path, "w", encoding="utf-8", newline="\n") as f:
f.write("\n".join(lines) + "\n")
return
cmd = mc_command.mc_command(llvm_mc) + ["-triple=amdgcn--amdpal", "-mcpu=gfx1013"]
for ln in raw.splitlines():
s = ln.strip()
Expand Down Expand Up @@ -1162,7 +1175,7 @@ def run_suite(a, work: str) -> int:
results.append((name,) + wavemask_check(vk_compute, spv, cs_env, w64_emulated()))
elif name in ("cs_store", "cs_signed_zero_nan", "cs_smem_soffset_imm", "cs_w32_lane_wrap", "cs_movrelsd_2", "cs_w32_permute", "cs_w32_sincos", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_w32_permlane_fi", "cs_exec_const", "cs_w32_mask_imm", "cs_w64_mask_imm", "cs_w32_lds_u8_d16", "cs_w32_lds_subdword", "cs_w32_lds_wide", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_wave64_ballot", "cs_wm_half_ops", "cs_wm_half_cmp", "cs_wm_bfm64", "cs_wm_scc_rfl", "cs_wqm_w64", "cs_wqm_w32", "cs_wqm_w64_half",
"cs_dpp", "cs_dpp_row", "cs_dpp_mask", "cs_dpp8", "cs_dpp_ops", "cs_dpp_bank", "cs_dpp_fi",
"cs_dpp_bound", "cs_dpp_w64",
"cs_dpp_bound", "cs_dpp_cmp", "cs_dpp_w64",
"cs_w64_mask_detect",
"cs_w64x_readlane", "cs_w64x_writelane", "cs_w64x_readfirstlane", "cs_w64x_bpermute", "cs_w64x_permute",
"cs_w64x_branch", "cs_w64x_ballot", "cs_w64x_wg128_branches", "cs_w64x_lds_raw",
Expand Down Expand Up @@ -1223,6 +1236,7 @@ def run_suite(a, work: str) -> int:
"cs_dpp_bound": [10, 10, 11, 12, 13, 14, 15, 16],
"cs_dpp_bank": [0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFC, 7, 7, 7, 7],
"cs_dpp_fi": [1, 2, 3, 0x7777, 5, 6, 7, 0x7777],
"cs_dpp_cmp": [3, 2, 3, 2, 1, 0, 1, 0],
"cs_dpp_w64": [1, 0, 3, 2, 5, 4, 7, 6],
# SHADER-99: -0 and NaN survive (SignedZeroInfNanPreserve, cs_signed_zero_nan.asm); 0 in out[4..7]
# without it
Expand Down Expand Up @@ -1297,7 +1311,7 @@ def run_suite(a, work: str) -> int:
"cs_bpermute32", "cs_w32_partial_scmp", "cs_w32_partial_permute", "cs_w32_permlane_fi", "cs_w32_sincos", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap",
"cs_lds_b64_cmpst", "cs_lds_xchg64_ext", "cs_lds_atomics_ext", "cs_exec_const", "cs_w32_mask_imm", "cs_mask_vgpr_exec",
"cs_dpp", "cs_dpp_row", "cs_dpp_mask", "cs_dpp8",
"cs_dpp_ops", "cs_dpp_bound", "cs_dpp_bank", "cs_dpp_fi", "cs_w32_andn2_exec",
"cs_dpp_ops", "cs_dpp_bound", "cs_dpp_bank", "cs_dpp_fi", "cs_dpp_cmp", "cs_w32_andn2_exec",
"cs_w32_exec_computed") and not emulated and not partial
if partial and not emulated:
cs_env = dict(os.environ, VKC_SUBGROUP_SIZE="64")
Expand Down
14 changes: 14 additions & 0 deletions tests/semantic/fixtures/disasm/cs_dpp_cmp.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; cs_dpp_cmp test fixture generated by tests/semantic/regen_fixtures.py from runtime/test/shaders/cs_dpp_cmp.asm
v_mbcnt_lo_u32_b32 v0, -1, 0
v_cvt_f32_u32 v1, v0
v_mov_b32 v3, 0
v_mov_b32 v4, 1
v_mov_b32 v5, 2
v_cmp_gt_f32_dpp vcc_lo, v1, v1 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1
v_cndmask_b32 v6, v3, v4, vcc_lo
v_cmp_gt_f32_dpp vcc_lo, v1, v1 dpp8:[4,5,6,7,0,1,2,3]
v_cndmask_b32 v7, v3, v5, vcc_lo
v_add_nc_u32 v6, v6, v7
v_lshlrev_b32 v2, 2, v0
buffer_store_dword v6, v2, s[0:3], 0 offen
s_endpgm
230 changes: 230 additions & 0 deletions tests/semantic/test_dpp_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
"""A VOPC compare in DPP form reads src0 through the DPP lane selection (SHADER-VOPC-DPP).

Three pipeline-library compute programs of 1.0.2.1 hold `v_cmp_gt_f32` with a DPP16 or DPP8 src0 (7 words each),
beside `v_min/max_f32_dpp` and `v_mov_b32_dpp` in a lane-pair reduction. `Gen.dpp_insn` took only a VGPR
destination, so the programs were refused ("DPP destination").

The rule (docs/design.md, "DPP on a VOPC compare"): the bit a lane writes is compare(src0 of the selected lane, its
own src1), with 0 for src0 when the selected lane is out of its row or inactive. Only the forms where every lane
writes are translated: all rows and banks enabled and either `bound_ctrl:1` or DPP8. A form that leaves a lane
unwritten is refused, because what that lane's mask bit becomes is not measured.

The values come from running the translated module in tests/semantic/spirv_wave_interp.py.

Standard library only; no GPU, no LLVM, no SPIRV-Tools, no game files.
"""
import os
import sys
import unittest

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

import support # noqa: E402 (puts tools/ on the path)
import spirv_wave_interp as wi # noqa: E402
from rdna2spirv import Unsupported # noqa: E402

# Every lane writes with each of these: all rows and banks, and bound_ctrl:1 or DPP8.
WRITTEN_FORMS = [
"quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1", # the three forms the programs use
"quad_perm:[2,3,0,1] row_mask:0xf bank_mask:0xf bound_ctrl:1",
"row_ror:8 row_mask:0xf bank_mask:0xf bound_ctrl:1",
"dpp8:[4,5,6,7,0,1,2,3]",
"row_shr:1 row_mask:0xf bank_mask:0xf bound_ctrl:1", # lane 0 of a row has no source: reads 0
"row_shl:3 row_mask:0xf bank_mask:0xf bound_ctrl:1",
"row_mirror row_mask:0xf bank_mask:0xf bound_ctrl:1",
"dpp8:[7,6,5,4,3,2,1,0] fi:1", # fi:1 reads an inactive source lane too
]

# A lane that DPP does not write: a row or bank switched off, or an invalid source without bound_ctrl.
UNWRITTEN_FORMS = [
"quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf",
"quad_perm:[1,0,3,2] row_mask:0x5 bank_mask:0xf bound_ctrl:1",
"quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xa bound_ctrl:1",
"row_shr:1 row_mask:0xf bank_mask:0xf",
]

COMPARES = {
"v_cmp_gt_f32_dpp": ("v1", lambda a, b: a > b), # v1 = float(lane)
"v_cmp_lt_u32_dpp": ("v0", lambda a, b: a < b), # v0 = lane
"v_cmp_eq_u32_dpp": ("v0", lambda a, b: a == b),
}


def compare_vs(op, form, exec_off=None):
"""A vertex shader: VCC = `op`(DPP(src), src) with lane `exec_off` off in EXEC for the compare; param0.x
exports 1 where the lane's VCC bit is set and 0 where it is clear."""
src = COMPARES[op][0]
lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0", "v_cvt_f32_u32 v1, v0", "v_mov_b32 v3, 0", "v_mov_b32 v4, 1",
"s_mov_b32 s10, exec_lo"]
if exec_off is not None:
lines.append(f"v_cmpx_ne_u32_e32 {exec_off}, v0")
lines += [f"{op} vcc_lo, {src}, {src} {form}", "s_mov_b32 exec_lo, s10", "v_cndmask_b32 v6, v3, v4, vcc_lo",
"v_mov_b32 v5, 0", "exp pos0 v5, v5, v5, v5 done", "exp param0 v6, v5, v5, v5", "s_endpgm"]
return support.translate_lines("vs_dpp_compare", lines)


def compare_reference(op, form, exec_off=None, wave=32):
"""The VCC bit per lane after compare_vs: an inactive lane's bit is 0, as for a plain compare."""
mods = form.split()
fi = any(m == "fi:1" for m in mods)
relation = COMPARES[op][1]
active = [lane != exec_off for lane in range(wave)]
out = []
for lane in range(wave):
loc, ok = lane & 15, True
if mods[0].startswith("dpp8:"):
src = (lane & ~7) | [int(x) for x in mods[0][6:-1].split(",")][lane & 7]
elif mods[0].startswith("quad_perm:"):
src = (lane & ~3) | [int(x) for x in mods[0][11:-1].split(",")][lane & 3]
else:
kind, _, n = mods[0].partition(":")
n = int(n or 0)
if kind == "row_shr":
ok, s = loc >= n, loc - n
elif kind == "row_shl":
ok, s = loc + n < 16, loc + n
elif kind == "row_ror":
s = (loc - n) & 15
else: # row_mirror
s = 15 - loc
src = (lane & ~15) | (s & 15)
valid = ok and (active[src] or fi)
out.append(int(active[lane] and relation(src if valid else 0, lane)))
return out


def run(op, form, exec_off=None):
res = wi.run_wave(compare_vs(op, form, exec_off), wave=32,
builtins={"VertexIndex": lambda lane: lane, "InstanceIndex": lambda lane: 0})
return [o["%out_param0"][0] for o in res["outputs"]]


class DppCompare(unittest.TestCase):
def test_values(self):
for op in COMPARES:
for form in WRITTEN_FORMS:
for exec_off in (None, 5, 16):
with self.subTest(op=op, form=form, exec_off=exec_off):
self.assertEqual(run(op, form, exec_off), compare_reference(op, form, exec_off))

def test_reference_by_hand(self):
# Pairs swapped: an even lane compares its odd neighbour against itself, an odd lane the lane below it.
pair = "quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1"
self.assertEqual(compare_reference("v_cmp_gt_f32_dpp", pair)[:8], [1, 0, 1, 0, 1, 0, 1, 0])
# Halves of eight swapped: lanes 0-3 see lanes 4-7.
self.assertEqual(compare_reference("v_cmp_gt_f32_dpp", "dpp8:[4,5,6,7,0,1,2,3]")[:8], [1, 1, 1, 1, 0, 0, 0, 0])
# Lane 4's source, lane 5, is inactive and reads 0, so "0 > 4" is false; lane 5 is inactive and writes 0.
self.assertEqual(compare_reference("v_cmp_gt_f32_dpp", pair, exec_off=5)[:8], [1, 0, 1, 0, 0, 0, 1, 0])
# The game's reduction step compares against runtime/test/shaders/cs_dpp_cmp.asm's expected 3 2 3 2 1 0 1 0.
first = compare_reference("v_cmp_gt_f32_dpp", pair)[:8]
second = compare_reference("v_cmp_gt_f32_dpp", "dpp8:[4,5,6,7,0,1,2,3]")[:8]
self.assertEqual([a + 2 * b for a, b in zip(first, second)], [3, 2, 3, 2, 1, 0, 1, 0])

def test_a_form_that_leaves_a_lane_unwritten_is_refused(self):
for form in UNWRITTEN_FORMS:
with self.subTest(form=form):
with self.assertRaisesRegex(Unsupported, "DPP compare with lanes that keep their mask bit"):
compare_vs("v_cmp_gt_f32_dpp", form)

def test_cmpx_is_refused(self):
lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0",
"v_cmpx_gt_u32_dpp v0, v0 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1",
"v_mov_b32 v5, 0", "exp pos0 v5, v5, v5, v5 done", "s_endpgm"]
with self.assertRaises(Unsupported):
support.translate_lines("vs_dpp_cmpx", lines)


def vopc(opcode, src0, vsrc1):
return 0x3E << 25 | opcode << 17 | vsrc1 << 9 | src0


def dpp16_word(src0_reg, ctrl, bound=0, fi=0, bank_mask=0xF, row_mask=0xF):
return row_mask << 28 | bank_mask << 24 | bound << 19 | fi << 18 | ctrl << 8 | src0_reg


def dpp8_word(src0_reg, lanes):
return sum(lane << (8 + 3 * i) for i, lane in enumerate(lanes)) | src0_reg


GT_F32, LT_U32 = 0x04, 0xC1 # VOPC opcodes (tools/rdna2_tables.py)
S_ENDPGM = 0xBF810000
# (first word, second word, the line both printers give)
PAIRS = [
(vopc(GT_F32, 250, 2), dpp16_word(1, 0xB1, bound=1),
"v_cmp_gt_f32_dpp vcc_lo, v1, v2 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf bound_ctrl:1"),
(vopc(GT_F32, 250, 9), dpp16_word(9, 0x128, bound=1),
"v_cmp_gt_f32_dpp vcc_lo, v9, v9 row_ror:8 row_mask:0xf bank_mask:0xf bound_ctrl:1"),
(vopc(LT_U32, 250, 3), dpp16_word(4, 0x111, bank_mask=0xA, row_mask=0x5),
"v_cmp_lt_u32_dpp vcc_lo, v4, v3 row_shr:1 row_mask:0x5 bank_mask:0xa"),
(vopc(GT_F32, 233, 20), dpp8_word(20, [4, 5, 6, 7, 0, 1, 2, 3]),
"v_cmp_gt_f32_dpp vcc_lo, v20, v20 dpp8:[4,5,6,7,0,1,2,3]"),
(vopc(GT_F32, 234, 6), dpp8_word(5, [7, 6, 5, 4, 3, 2, 1, 0]),
"v_cmp_gt_f32_dpp vcc_lo, v5, v6 dpp8:[7,6,5,4,3,2,1,0] fi:1"),
]


def text_of(line):
return " ".join(line.split(";", 1)[0].split())


class DppCompareDecode(unittest.TestCase):
"""The two printers of the pair: tools/rdna2_decode.py, and tools/shader_disasm.py over llvm-mc."""

def test_opcodes_are_the_tables(self):
import rdna2_decode
self.assertEqual(rdna2_decode.T.VOPC[GT_F32][0], "v_cmp_gt_f32")
self.assertEqual(rdna2_decode.T.VOPC[LT_U32][0], "v_cmp_lt_u32")

def test_python_decoder(self):
import rdna2_decode
import struct
for w0, w1, want in PAIRS:
with self.subTest(want=want):
asm, bad = rdna2_decode.disasm_bytes(struct.pack("<3I", w0, w1, S_ENDPGM), mcpu="gfx1013", wave32=True)
lines = [text_of(line) for line in asm.splitlines() if "encoding" in line]
self.assertEqual((lines, bad), ([want, "s_endpgm"], 0))

def test_a_decoded_line_translates(self):
# What the disassembler prints is what the translator takes: the two forms it accepts, from PAIRS.
for _, _, line in (PAIRS[0], PAIRS[3]):
with self.subTest(line=line):
lines = ["v_mbcnt_lo_u32_b32 v0, -1, 0", line, "v_mov_b32 v5, 0", "exp pos0 v5, v5, v5, v5 done",
"s_endpgm"]
self.assertIn("OpGroupNonUniformShuffle", support.translate_lines("vs_dpp_compare_line", lines))

def test_llvm_mc_listing(self):
import struct
import shader_disasm
probe = struct.pack("<I", S_ENDPGM)
asm, _, _ = shader_disasm.run_mc(probe, "gfx1013")
if "s_endpgm" not in asm:
self.skipTest("llvm-mc with the AMDGPU target is not available (LLVM_MC)")
for w0, w1, want in PAIRS:
with self.subTest(want=want):
code = struct.pack("<3I", w0, w1, S_ENDPGM)
plain, bad, _ = shader_disasm.run_mc(code, "gfx1013")
self.assertGreater(bad, 0, "llvm-mc decodes the pair now: the stand-in pass is no longer needed")
self.assertNotIn("v_cmp", plain)
asm, bad, _ = shader_disasm.disasm(code, "gfx1013")
lines = [line for line in asm.splitlines() if "encoding" in line]
self.assertEqual(([text_of(line) for line in lines], bad), ([want, "s_endpgm"], 0))
encoding = ",".join(f"0x{b:02x}" for b in struct.pack("<2I", w0, w1))
self.assertIn(f"; encoding: [{encoding}]", lines[0])

def test_llvm_mc_leaves_other_rejected_words(self):
import struct
import shader_disasm
asm, _, _ = shader_disasm.run_mc(struct.pack("<I", S_ENDPGM), "gfx1013")
if "s_endpgm" not in asm:
self.skipTest("llvm-mc with the AMDGPU target is not available (LLVM_MC)")
# A VOPC word with a DPP src0 whose opcode names no compare is not an instruction this pass knows.
unknown = next(op for op in range(0xFF, 0, -1) if op not in __import__("rdna2_decode").T.VOPC)
code = struct.pack("<3I", vopc(unknown, 250, 2), dpp16_word(1, 0xB1, bound=1), S_ENDPGM)
plain = shader_disasm.run_mc(code, "gfx1013")
self.assertEqual(shader_disasm.disasm(code, "gfx1013"), plain)
self.assertIsNone(shader_disasm.with_vopc_dpp(code, "gfx1013", plain[2]))


if __name__ == "__main__":
unittest.main()
Loading
Loading