Files
mandy/tests/semantic/test_tbuffer_store.py
AlxcBTW 4945add25f rdna2spirv: typed stores follow the hardware (f16 toward zero, one NORM rounding, two-component routing), all 77 codes
@AwkGrep256 ran tools/buffer_format_fuzz.py on a real RDNA2 (gfx1036, #2022): the same totals as the
Radeon 880M, 0 mismatches, and 199,144 elements of the 14 GFX10-only codes, also 0. This follows both.

- 16_FLOAT, 16_16_FLOAT and 16_16_16_16_FLOAT format stores round toward zero, saturate at +-65504 and
  keep a NaN a NaN (Gen.ufloat_encode_bits, now with an optional sign). d16 data rounding (#1759) is
  not touched.
- Every UNORM, SNORM and SCALED store rounds the exact product once (Gen.norm_encode_exact): 0.3 *
  65535 stores 19661, not 19660.
- A two-component format stored through a V# takes channel y as its second component when DST_SEL_Y is
  Y and DST_SEL_Z a constant (the runtime's X,Y,0,1: gfx1036 narrow-integer fixtures, gfx1150), and
  channel w otherwise (identity XYZW, @AwkGrep256 and gfx1150); _xy then writes (x, 0). tbuffer keeps
  (x, y). A probe with XYZW alone would have shown (x, w) for every V# and broken the runtime's
  selectors, which the oracle now also covers (XY01 runs for the two-component formats).
- Gen.FORMATS has all 77 codes: 30-35 and 37-42 (10_11_11 and 11_11_10 with UNORM..SINT) join.
- buffer_format_fuzz: the model has one store route per path, select_formats runs all 77 codes by
  default on a GFX10 device, the known-difference count is gone (0 on the 880M).

The three expectedFailure tests pass; the fixtures that baked the old behaviour (cs_fmt_sint32, test_translate
f16 and int32 store tests, test_value_semantics) take the measured words. Sizes: cs_store 206 KB on
main, 125 KB here.

Co-Authored-By: AwkGrep256 <AwkGrep256@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-22 17:55:37 +01:00

88 lines
4.2 KiB
Python

"""tbuffer_store_format_* (MTBUF stores with the instruction's own format) through the translator and the interpreter.
cs_tbuffer_store.asm stores into 13 sentinel-filled regions through V#s whose FORMAT differs from the instruction's,
then reads every region back into out[0..51]. check.py's TBUFFER_STORE_WORDS holds the words a Radeon 680M (gfx1035)
gives natively; the rules are in the .asm header. Here the translated fixture runs on the CPU and must give the same
words, so the GPU test, the hardware and the translation are kept in step.
"""
import os
import struct
import sys
import unittest
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path.insert(0, HERE)
sys.path.insert(0, os.path.join(ROOT, "runtime", "test", "shaders"))
sys.path.insert(0, os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools"))
import check # noqa: E402 (runtime/test/shaders/check.py; importing it runs nothing)
import rdna2spirv # noqa: E402
import support # noqa: E402
def fssm(rsrc2, group):
"""The compute-shader header words the translator reads: COMPUTE_PGM_RSRC2 and the thread-group size."""
header = bytearray(0x148)
struct.pack_into("<4I", header, 0x138, rsrc2, *group)
return bytes(header)
# 4 user SGPRs (the output V#), workgroup id in s4, 64x1x1 threads: the header build.py gives the GPU tests.
CS_FSSM = fssm(0x8088, (64, 1, 1))
# The output V# vk_compute_test passes in s[0:3]: base 0, num_records 0xffffffff, 32_FLOAT, OOB_SELECT 3.
OUT_VSHARP = [0, 0, 0xFFFFFFFF, 0x31016FAC]
def run(spv):
memory = {}
outputs = support.run_fragment(spv, inputs={"%wg_id": (0, 0, 0), "%local_id": (0, 0, 0)},
push_constants=OUT_VSHARP + [0] * 28, memory=memory)
return outputs, memory
class TbufferStoreFixture(unittest.TestCase):
def test_words_match_the_native_measurement(self):
outputs, memory = run(support.translate_fixture("cs_tbuffer_store", fssm=CS_FSSM))
self.assertEqual(outputs, {})
want = check.TBUFFER_STORE_WORDS
got = [memory.get(4 * i, 0) for i in range(len(want))]
for i, (g, w) in enumerate(zip(got, want)):
with self.subTest(out=i, region=hex(0x200 + 16 * (i // 4))):
self.assertEqual(f"{g:#010x}", f"{w:#010x}")
# The regions themselves hold the same words, and nothing lands between out[] and the regions.
self.assertEqual([memory.get(0x200 + 4 * i, 0) for i in range(len(want))], want)
self.assertEqual(sorted(a for a in memory if len(want) * 4 <= a < 0x200), [])
class TbufferStoreTranslation(unittest.TestCase):
def store(self, fmt_mod):
return support.translate_lines("cs_tbuf_one", [
"v_mov_b32_e32 v20, 1.0", "v_mov_b32_e32 v5, 0x200",
f"tbuffer_store_format_x v20, v5, s[0:3], 0{fmt_mod} offen", "s_endpgm"], fssm=CS_FSSM)
def test_format_outside_formats_is_unsupported(self):
# BUF_FMT_INVALID (code 0) has no encoder: reported, never guessed. Every code 1-77 has one now (BUF_FMT_8_UINT
# was the example until #1871 added the 8/16-bit UINT/SINT formats, BUF_FMT_8_USCALED and BUF_FMT_10_11_11_UNORM
# until the SCALED and 10_11_11 formats).
with self.assertRaises(rdna2spirv.Unsupported):
self.store(" format:[BUF_FMT_INVALID]")
def test_default_format_is_one_unorm_byte(self):
# No modifier is GFX10's default, BUF_FMT_8_UNORM: 1.0 is 0xff in the low byte, the other three kept.
spv = self.store("")
memory = {0x200: 0x11223344}
support.run_fragment(spv, inputs={"%wg_id": (0, 0, 0), "%local_id": (0, 0, 0)},
push_constants=OUT_VSHARP + [0] * 28, memory=memory)
self.assertEqual(f"{memory[0x200]:#010x}", "0x112233ff")
def test_data_vgprs_are_read(self):
# read_before_write_vgprs: a store's vdata is a source, as for buffer_store.
import shader_ir
ins = shader_ir.parse_line("\ttbuffer_store_format_xy v[7:8], v5, s[4:7], 0 format:[BUF_FMT_32_32_FLOAT] offen")
self.assertEqual(sorted(rdna2spirv.read_before_write_vgprs([ins])), [5, 7, 8])
if __name__ == "__main__":
unittest.main()