mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 22:58:37 +00:00
Measure MUBUF/MTBUF swizzled buffer addressing (V# SWIZZLE_ENABLE, ADD_TID_ENABLE, INDEX_STRIDE, OOB_SELECT, multi-dword and typed accesses, atomics, 32-bit offset wrap) natively on RDNA2 (gfx1036, wave32 and wave64) via tools/buffer_swizzle_probe.py, and fix all six observed silicon divergences in tools/rdna2spirv.py: - idxen=1 ignores ADD_TID_ENABLE (both address and bounds). - SWIZZLE_ENABLE=1 with idxen=0 and ADD_TID_ENABLE=0 stays linear and OOB_SELECT 3 remains RAW. - Multi-dword raw accesses step by +4*IS only in scratch-swizzle mode (SWIZZLE_ENABLE=1, idxen=0, ADD_TID_ENABLE=1); indexed swizzle (idxen=1) steps linearly by +4 bytes even across IS boundaries. - Scratch-swizzle mode suppresses typed formats > 4 bytes (MUBUF/MTBUF) and 8-byte buffer_atomic_*_x2 even under OOB_SELECT 2 (formats <= 4B execute normally); indexed swizzle executes them linearly. - OOB_SELECT 3 with active swizzle and stride != 0 checks (index + soffset) >= num_records or off + payload > stride. - off = uint32(voff + inst_offset) wraps at 32 bits before bounds checking, while RAW mode 3 uint64(off) + uint64(soffset) does not. Update semantic tests, cs_buf_swizzle fixture expectation (word 4: 0x5eed0028 -> 0x5eed000c), unit tests, and docs/design.md / docs/shader_validation.md.
129 lines
5.7 KiB
Python
129 lines
5.7 KiB
Python
"""Unit tests for tools/buffer_swizzle_probe.py (SHADER-343). Standard library only; no GPU or game files needed."""
|
|
import io
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import unittest.mock
|
|
from contextlib import redirect_stderr, redirect_stdout
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
TOOLS = ROOT / "tools"
|
|
sys.dont_write_bytecode = True
|
|
if str(TOOLS) not in sys.path:
|
|
sys.path.insert(0, str(TOOLS))
|
|
|
|
import buffer_swizzle_probe as probe # noqa: E402
|
|
from hw_oracle import runner as hw_runner # noqa: E402
|
|
|
|
|
|
class BufferSwizzleProbeTests(unittest.TestCase):
|
|
def test_cases_unique_and_nonempty(self):
|
|
cases = probe.build_cases()
|
|
self.assertGreaterEqual(len(cases), 20)
|
|
names = [c.name for c in cases]
|
|
self.assertEqual(len(names), len(set(names)))
|
|
groups = {c.group for c in cases}
|
|
self.assertEqual(groups, {
|
|
"swizzle_basic", "idxen_ignores_tid", "swizzle_activation",
|
|
"multi_dword_stepping", "typed_and_atomic_x2", "oob_mode3_soffset",
|
|
"offset_wrap", "add_tid_wave_lane",
|
|
})
|
|
for key in probe.KNOWN_DIVERGENCES:
|
|
self.assertIn(key, names)
|
|
|
|
def test_hw_kernel_lines_wave32_and_wave64(self):
|
|
by_name = {c.name: c for c in probe.build_cases()}
|
|
c = by_name["tid_wave32_vs_wave64"]
|
|
w32 = "\n".join(probe._hw_kernel_lines(c, 0, wave=32))
|
|
w64 = "\n".join(probe._hw_kernel_lines(c, 1, wave=64))
|
|
self.assertIn("s_mov_b32 s28, exec_lo", w32)
|
|
self.assertIn("s_mov_b64 s[28:29], exec", w64)
|
|
self.assertIn("s_barrier", w32)
|
|
self.assertIn(".Lseed_0:", w32)
|
|
self.assertIn(".Lseed_1:", w64)
|
|
|
|
def test_self_test_and_translator_groups(self):
|
|
buf = io.StringIO()
|
|
with redirect_stdout(buf):
|
|
rc = probe.main(["--self-test"])
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("self-test:", buf.getvalue())
|
|
|
|
def test_list_groups_and_unknown_group(self):
|
|
out, err = io.StringIO(), io.StringIO()
|
|
with redirect_stdout(out):
|
|
rc = probe.main(["--list-groups"])
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("oob_mode3_soffset", out.getvalue())
|
|
|
|
with redirect_stderr(err):
|
|
rc = probe.main(["--group", "does_not_exist"])
|
|
self.assertEqual(rc, 2)
|
|
self.assertIn("unknown group", err.getvalue())
|
|
|
|
def test_hw_unavailable_returns_77(self):
|
|
err = io.StringIO()
|
|
with unittest.mock.patch.object(hw_runner, "open_oracle", side_effect=hw_runner.Unavailable("no GPU")):
|
|
with redirect_stderr(err):
|
|
rc = probe.main(["--oracle", "hw", "--group", "offset_wrap"])
|
|
self.assertEqual(rc, 77)
|
|
self.assertIn("no GPU", err.getvalue())
|
|
|
|
def test_both_match_known_divergence_strict_and_json(self):
|
|
cases = [c for c in probe.build_cases() if c.group == "oob_mode3_soffset"]
|
|
tr = probe.run_translator(cases, waves=(32, 64))
|
|
fake_hw = ("device=mock arch=gfx1036", {32: dict(tr[32]), 64: dict(tr[64])})
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
jpath = os.path.join(tmp, "out.json")
|
|
out = io.StringIO()
|
|
with unittest.mock.patch.object(probe, "run_hw", return_value=fake_hw):
|
|
with redirect_stdout(out):
|
|
rc = probe.main(["--oracle", "both", "--group", "oob_mode3_soffset", "--json", jpath])
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("2 match, 0 known divergence, 0 unexpected mismatch, 0 wave32/64 diff", out.getvalue())
|
|
data = json.loads(Path(jpath).read_text(encoding="utf-8"))
|
|
self.assertEqual(data["device"], "device=mock arch=gfx1036")
|
|
self.assertEqual(len(data["cases"]), 2)
|
|
|
|
# Synthetic known divergence vs --strict
|
|
fake_hw[1][32]["indexed_mode3_soff_edge"] = fake_hw[1][64]["indexed_mode3_soff_edge"] = [1, 2, 3, 4]
|
|
known = {"indexed_mode3_soff_edge": ("synthetic", [1, 2, 3, 4], "test-only divergence")}
|
|
with unittest.mock.patch.dict(probe.KNOWN_DIVERGENCES, known):
|
|
out2 = io.StringIO()
|
|
with unittest.mock.patch.object(probe, "run_hw", return_value=fake_hw):
|
|
with redirect_stdout(out2):
|
|
rc_ok = probe.main(["--oracle", "both", "--group", "oob_mode3_soffset"])
|
|
self.assertEqual(rc_ok, 0)
|
|
self.assertIn("1 match, 1 known divergence, 0 unexpected mismatch", out2.getvalue())
|
|
|
|
out3 = io.StringIO()
|
|
with unittest.mock.patch.object(probe, "run_hw", return_value=fake_hw):
|
|
with redirect_stdout(out3):
|
|
rc_strict = probe.main(["--oracle", "both", "--group", "oob_mode3_soffset", "--strict"])
|
|
self.assertEqual(rc_strict, 1)
|
|
|
|
err = io.StringIO()
|
|
with unittest.mock.patch.object(probe, "run_hw", return_value=fake_hw):
|
|
with redirect_stdout(io.StringIO()), redirect_stderr(err):
|
|
rc_dir = probe.main(["--oracle", "both", "--group", "oob_mode3_soffset", "--json", tmp])
|
|
self.assertEqual(rc_dir, 2)
|
|
|
|
def test_unexpected_mismatch_and_wave_diff_fail(self):
|
|
bad_hw = ("device=mock arch=gfx1036", {
|
|
32: {"voff_inst_wrap_and_soff_nowrap": [1, 2, 3, 4]},
|
|
64: {"voff_inst_wrap_and_soff_nowrap": [1, 2, 3, 5]},
|
|
})
|
|
out = io.StringIO()
|
|
with unittest.mock.patch.object(probe, "run_hw", return_value=bad_hw):
|
|
with redirect_stdout(out):
|
|
rc = probe.main(["--oracle", "both", "--group", "offset_wrap"])
|
|
self.assertEqual(rc, 1)
|
|
self.assertIn("WAVE_DIFF", out.getvalue())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|