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
15 changes: 13 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ def mesh_psv0_chunk(threads=(128, 1, 1), max_vertices=64, max_primitives=64, top
# 0x8088 = 4 user SGPRs, TGID_X, 512 B of LDS; group 64x1x1 (the header the cs_* samples in runtime/test/shaders use).
DEFAULT_CS_HEADER = (0x8088, (64, 1, 1))

# The comment line a sample carries when llvm-mc cannot assemble it (cs_dpp_cmp.asm: a VOPC compare in DPP form).
# scripts/run_gpu_tests.py has the same line; tests/test_translate.py checks that the two agree.
TEXT_ONLY_MARK = "; run_gpu_tests: text only, llvm-mc has no gfx1013 encoding for an instruction here"


class Shader:
def __init__(self, workdir, name, asm, cs_header=DEFAULT_CS_HEADER, dxbc=EMPTY_DXBC):
Expand All @@ -167,6 +171,15 @@ def __init__(self, workdir, name, asm, cs_header=DEFAULT_CS_HEADER, dxbc=EMPTY_D
rsrc2, (lx, ly, lz) = cs_header
(workdir / "fssm").mkdir(exist_ok=True)
(workdir / "fssm" / f"{name}.fssm").write_bytes(bytes(0x138) + struct.pack("<4I", rsrc2, lx, ly, lz))
self.path = disasm / f"{name}.s"
self.workdir = workdir
if any(ln.strip() == TEXT_ONLY_MARK for ln in asm.splitlines()):
# llvm-mc has no gfx1013 encoding for an instruction of this sample, so its text is the listing, as in
# assemble_to_s of scripts/run_gpu_tests.py: the translator reads straight-line code from the text alone.
lines = [f"; {name} code 0x0..0x0 (synthetic), gfx1013, 0 invalid words"]
lines += ["\t" + ln.strip() for ln in asm.splitlines() if ln.strip() and not ln.lstrip().startswith(";")]
self.path.write_text("\n".join(lines) + "\n", encoding="utf-8", newline="\n")
return
cmd = LLVM_MC + ["-triple=amdgcn--amdpal", "-mcpu=gfx1013"]
for ln in asm.splitlines():
s = ln.strip()
Expand All @@ -181,9 +194,7 @@ def __init__(self, workdir, name, asm, cs_header=DEFAULT_CS_HEADER, dxbc=EMPTY_D
cmd.append("-show-encoding")
r = subprocess.run(cmd, input=asm, capture_output=True, text=True)
assert r.returncode == 0, r.stderr
self.path = disasm / f"{name}.s"
self.path.write_text(f"; {name} synthetic\n{r.stdout}")
self.workdir = workdir

def translate(self):
return rdna2spirv.translate(str(self.path))
Expand Down
22 changes: 20 additions & 2 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import pytest

import rdna2spirv
from conftest import (EMPTY_DXBC, KNOWN_VAL_ISSUES, ROOT, SAMPLE_DXBC, Shader, make_dxbc, mesh_psv0_chunk,
requires_tools, signature_chunk)
from conftest import (EMPTY_DXBC, KNOWN_VAL_ISSUES, ROOT, SAMPLE_DXBC, TEXT_ONLY_MARK, Shader, make_dxbc,
mesh_psv0_chunk, requires_tools, signature_chunk)

pytestmark = requires_tools

Expand Down Expand Up @@ -64,6 +64,24 @@ def test_gpu_samples_translate_and_validate(path, make_shader):
shader.validate(shader.translate(), KNOWN_VAL_ISSUES.get(stem))


def test_text_only_mark_is_spelled_as_in_the_gpu_runner():
"""conftest.Shader and scripts/run_gpu_tests.py have to agree on the comment line that marks a sample llvm-mc
cannot assemble: with two spellings, a marked sample is handed to llvm-mc here and fails."""
runner = Path(ROOT, "scripts", "run_gpu_tests.py").read_text(encoding="utf-8")
assert f'\nTEXT_ONLY_MARK = "{TEXT_ONLY_MARK}"\n' in runner


def test_only_a_marked_sample_skips_llvm_mc(make_shader):
"""A sample with the text-only line becomes its own listing, comment lines dropped, whatever llvm-mc makes of
its instructions; the same lines without it still go to llvm-mc and fail there."""
body = "v_mov_b32 v1, 1.0\n v_no_such_instruction v0, v1\n\ns_endpgm\n"
shader = make_shader(f"; a sample llvm-mc cannot assemble\n{TEXT_ONLY_MARK}\n{body}", "cs_text_only")
assert shader.path.read_text(encoding="utf-8").splitlines()[1:] == [
"\tv_mov_b32 v1, 1.0", "\tv_no_such_instruction v0, v1", "\ts_endpgm"]
with pytest.raises(AssertionError, match="v_no_such_instruction"):
make_shader(body, "cs_not_marked")


@pytest.mark.parametrize("case", sorted(SUPPORTED))
def test_supported_instruction(case, make_shader):
shader = make_shader(program(SUPPORTED[case]))
Expand Down
Loading