Files
mandy/tests/listing.py
Ewann 439887f04f Tests: check computed values with a small SPIR-V interpreter
tests/spirv_interp.py runs the straight-line, single-lane SPIR-V the translator
emits for branch-free shaders (binary32 rounding after every op, GLSL.std.450
subset, Function/Output variables). Unknown opcodes raise NotImplementedError.

- test_values.py: 49 value checks over the supported instruction families, with
  references written in plain Python from the ISA definitions.
- test_f16_op_sel_values.py: 84 value checks for op_sel on f16 VOP3 ops (all
  select combinations, neg/abs/clamp, fmac).
- listing.py: hand-written listing helper. shader_ir.parse_file treats the first
  line of a listing as its header, so the helper writes one; test_f16_op_sel.py
  used to lose its first instruction and now uses the helper.

None of these need llvm-mc or spirv-tools. Mutation-checked: breaking the
op_sel source index or the destination merge makes them fail.
2026-11-21 17:46:28 +01:00

22 lines
1.0 KiB
Python

"""Hand-written ``.s`` listings for translator tests that need neither llvm-mc nor spirv-tools."""
import struct
import rdna2spirv
EMPTY_DXBC = b"DXBC" + bytes(16) + struct.pack("<III", 1, 32, 0)
EPILOGUE = "v_mov_b32 v8, v3\nexp mrt0 v8, v8, v8, v8 done vm\ns_endpgm"
def translate_listing(tmp_path, body, name="ps_listing"):
"""Translate pixel-shader ``body`` (assembly text, one instruction per line) and return the SPIR-V assembly.
The first line of a listing is its header (shader_ir.parse_file skips it), so one is written here. Pixel shaders
must not read v0/v1 (barycentrics) before writing them. v3 is the register the epilogue exports.
"""
(tmp_path / "disasm").mkdir(exist_ok=True)
(tmp_path / f"{name}.dxbc").write_bytes(EMPTY_DXBC)
lines = [f"; {name} code 0x0..0x100"] + [ln.strip() for ln in (body + "\n" + EPILOGUE).splitlines() if ln.strip()]
path = tmp_path / "disasm" / f"{name}.s"
path.write_text(lines[0] + "\n" + "".join("\t" + ln + "\n" for ln in lines[1:]))
return rdna2spirv.translate(str(path))