mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 15:28:37 +00:00
158 lines
8.3 KiB
Python
158 lines
8.3 KiB
Python
"""Fix 4C: `s_mov_b64 vcc, s[a:b]` preserves both 32-bit scalar words for `[..., vcc_lo, vcc_hi]` tuples and wave32.
|
|
|
|
In wave32 shaders, lane masks are 32-bit (`vcc_lo`), so `s_mov_b64 vcc, s[a:b]` is a 64-bit scalar data pair move
|
|
(commonly spilling words 2-3 of a V#/T# into `vcc_lo:vcc_hi`, even across basic blocks or from SGPRs that held a
|
|
lane mask elsewhere). Previously `vcc_scalar_pointer` / `wave32_vcc_pointer` only checked `s_load_* vcc`, causing
|
|
`Gen.mov64` to collapse `%vcc` to a per-lane 1-bit mask and leave `%vcc_hi` unwritten (0).
|
|
|
|
Hand-written listings, no game data.
|
|
|
|
python -I tests/semantic/test_vcc_tuple_mov64.py -v
|
|
"""
|
|
import os
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
if HERE not in sys.path:
|
|
sys.path.insert(0, HERE)
|
|
|
|
import support # noqa: E402
|
|
import spirv_asm # noqa: E402
|
|
import spirv_mini_interp as si # noqa: E402
|
|
from test_fspx_wave_size import fssm_header, translate as translate_fspx # noqa: E402
|
|
|
|
|
|
def translate_fspx_listing(listing_lines, wave32=True):
|
|
"""Translate raw instruction lines (with optional `; encoding:` comments) as an FSPX wave32/wave64 CS."""
|
|
return translate_fspx(listing_lines, wave32=wave32)
|
|
|
|
|
|
def validate_v12(testcase, asm, label):
|
|
if not shutil.which("spirv-val"):
|
|
return
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
spv = os.path.join(tmp, f"{label}.spv")
|
|
with open(spv, "wb") as f:
|
|
f.write(spirv_asm.assemble(asm, target_env="vulkan1.2"))
|
|
r = subprocess.run(["spirv-val", "--target-env", "vulkan1.2", spv], capture_output=True, text=True)
|
|
testcase.assertEqual(r.returncode, 0, f"{label} spirv-val failed:\n{r.stderr}\n{r.stdout}")
|
|
|
|
|
|
class VccTupleMov64Tests(unittest.TestCase):
|
|
def test_wave32_s_mov_b64_vcc_into_buffer_tuple_across_branch_and_ever_mask(self):
|
|
"""Wave32 s_mov_b64 vcc, s[50:51] preserves both words into [s104,s105,vcc_lo,vcc_hi] across branches
|
|
even when s50 was previously used as a lane mask."""
|
|
base_in = 0x2000
|
|
base_out = 0x4000
|
|
# 1) s50 holds a lane mask first (putting 50 in ever_mask!)
|
|
# 2) s[50:51] is overwritten with V# words 2..3 (num_records=0x100, rsrc_word3=0x30016fac)
|
|
# 3) s_mov_b64 vcc, s[50:51] before a branch; [s104, s105, vcc_lo, vcc_hi] used in successor block!
|
|
lines = [
|
|
"v_cmp_eq_u32_e64 s50, 0, v0\t; encoding: [0x32,0x00,0x12,0xd4,0x80,0x00,0x02,0x00]",
|
|
"s_mov_b32 s104, 0x2000\t; encoding: [0xff,0x03,0xe8,0xbe,0x00,0x20,0x00,0x00]",
|
|
"s_mov_b32 s105, 0\t; encoding: [0x80,0x03,0xe9,0xbe]",
|
|
"s_mov_b32 s50, 0x100\t; encoding: [0xff,0x03,0xb2,0xbe,0x00,0x01,0x00,0x00]",
|
|
"s_mov_b32 s51, 0x30016fac\t; encoding: [0xff,0x03,0xb3,0xbe,0xac,0x6f,0x01,0x30]",
|
|
"s_mov_b64 vcc, s[50:51]\t; encoding: [0x32,0x04,0xea,0xbe]",
|
|
"s_cmp_eq_u32 s0, 0\t; encoding: [0x00,0x80,0x06,0xbf]",
|
|
"s_cbranch_scc0 1\t; encoding: [0x01,0x00,0x84,0xbf]",
|
|
"s_nop 0\t; encoding: [0x00,0x00,0x80,0xbf]",
|
|
"v_mov_b32_e32 v4, 0\t; encoding: [0x80,0x02,0x08,0x7e]",
|
|
"buffer_load_dwordx3 v[2:4], v4, [s104, s105, vcc_lo, vcc_hi], 0 offen\t; encoding: [0x00,0x10,0x38,0xe0,0x04,0x02,0x1a,0x80]",
|
|
"s_waitcnt vmcnt(0)\t; encoding: [0x70,0x0f,0x8c,0xbf]",
|
|
"s_mov_b32 s4, 0x4000\t; encoding: [0xff,0x03,0x84,0xbe,0x00,0x40,0x00,0x00]",
|
|
"s_mov_b32 s5, 0\t; encoding: [0x80,0x03,0x85,0xbe]",
|
|
"s_mov_b32 s6, 0x400\t; encoding: [0xff,0x03,0x86,0xbe,0x00,0x04,0x00,0x00]",
|
|
"s_mov_b32 s7, 0x30016fac\t; encoding: [0xff,0x03,0x87,0xbe,0xac,0x6f,0x01,0x30]",
|
|
"v_mul_lo_u32 v5, v0, 12\t; encoding: [0x05,0x00,0x69,0xd5,0x00,0x19,0x01,0x00]",
|
|
"buffer_store_dwordx3 v[2:4], v5, s[4:7], 0 offen\t; encoding: [0x00,0x10,0x78,0xe0,0x05,0x02,0x01,0x80]",
|
|
]
|
|
asm, wave64 = translate_fspx_listing(lines, wave32=True)
|
|
self.assertFalse(wave64)
|
|
validate_v12(self, asm, "cs_w32_vcc_tuple_branch")
|
|
mem = {base_in: 0x11223344, base_in + 4: 0x55667788, base_in + 8: 0x99AABBCC}
|
|
si.run_workgroup(
|
|
asm, 32, 32,
|
|
inputs_for=lambda lane: {"%wg_id": (0, 0, 0), "%local_id": (lane, 0, 0), "%sg_lane": lane},
|
|
push_constants=[0] * 32, memory=mem,
|
|
)
|
|
for lane in range(32):
|
|
got = [mem.get(base_out + 12 * lane + 4 * j) for j in range(3)]
|
|
self.assertEqual(got, [0x11223344, 0x55667788, 0x99AABBCC], f"lane {lane} mismatch")
|
|
|
|
def test_wave32_s_mov_b64_vcc_plain_data_without_slist(self):
|
|
"""Wave32 s_mov_b64 vcc, s[4:5] stores both 32-bit scalar words even with no s_load or slist."""
|
|
base_out = 0x5000
|
|
lines = [
|
|
"s_mov_b32 s4, 0xdeadbeef",
|
|
"s_mov_b32 s5, 0xcafebabe",
|
|
"s_mov_b64 vcc, s[4:5]",
|
|
"v_mov_b32_e32 v2, vcc_lo",
|
|
"v_mov_b32_e32 v3, vcc_hi",
|
|
f"s_mov_b32 s8, {base_out:#x}",
|
|
"s_mov_b32 s9, 0",
|
|
"s_mov_b32 s10, 0x400",
|
|
"s_mov_b32 s11, 0x30016fac",
|
|
"v_lshlrev_b32_e32 v4, 3, v0",
|
|
"buffer_store_dwordx2 v[2:3], v4, s[8:11], 0 offen",
|
|
]
|
|
asm, wave64 = translate_fspx_listing(lines, wave32=True)
|
|
self.assertFalse(wave64)
|
|
validate_v12(self, asm, "cs_w32_vcc_plain")
|
|
mem = {}
|
|
si.run_workgroup(
|
|
asm, 32, 32,
|
|
inputs_for=lambda lane: {"%wg_id": (0, 0, 0), "%local_id": (lane, 0, 0), "%sg_lane": lane},
|
|
push_constants=[0] * 32, memory=mem,
|
|
)
|
|
for lane in range(32):
|
|
self.assertEqual((mem.get(base_out + 8 * lane), mem.get(base_out + 8 * lane + 4)),
|
|
(0xDEADBEEF, 0xCAFEBABE), f"lane {lane} mismatch")
|
|
|
|
def test_wave64_vcc_tuple_across_branch_with_ever_mask(self):
|
|
"""Wave64 s_mov_b64 vcc, s[50:51] with [s104,s105,vcc_lo,vcc_hi] preserves both words across branches
|
|
even when s[50:51] held a lane mask earlier."""
|
|
base_in = 0x2000
|
|
base_out = 0x4000
|
|
listing = (
|
|
"; cs_w64_vcc_tuple_branch hand-written test program\n"
|
|
"\tv_cmp_eq_u32_e64 s[50:51], 0, v0\t; encoding: [0x32,0x00,0x12,0xd4,0x80,0x00,0x02,0x00]\n"
|
|
"\ts_mov_b32 s104, 0x2000\t; encoding: [0xff,0x03,0xe8,0xbe,0x00,0x20,0x00,0x00]\n"
|
|
"\ts_mov_b32 s105, 0\t; encoding: [0x80,0x03,0xe9,0xbe]\n"
|
|
"\ts_mov_b32 s50, 0x100\t; encoding: [0xff,0x03,0xb2,0xbe,0x00,0x01,0x00,0x00]\n"
|
|
"\ts_mov_b32 s51, 0x30016fac\t; encoding: [0xff,0x03,0xb3,0xbe,0xac,0x6f,0x01,0x30]\n"
|
|
"\ts_mov_b64 vcc, s[50:51]\t; encoding: [0x32,0x04,0xea,0xbe]\n"
|
|
"\ts_cmp_eq_u32 s0, 0\t; encoding: [0x00,0x80,0x06,0xbf]\n"
|
|
"\ts_cbranch_scc0 1\t; encoding: [0x01,0x00,0x84,0xbf]\n"
|
|
"\ts_nop 0\t; encoding: [0x00,0x00,0x80,0xbf]\n"
|
|
"\tv_mov_b32_e32 v4, 0\t; encoding: [0x80,0x02,0x08,0x7e]\n"
|
|
"\tbuffer_load_dword v2, v4, [s104, s105, vcc_lo, vcc_hi], 0 offen\t; encoding: [0x00,0x10,0x30,0xe0,0x04,0x02,0x1a,0x80]\n"
|
|
"\ts_waitcnt vmcnt(0)\t; encoding: [0x70,0x0f,0x8c,0xbf]\n"
|
|
"\ts_mov_b32 s4, 0x4000\t; encoding: [0xff,0x03,0x84,0xbe,0x00,0x40,0x00,0x00]\n"
|
|
"\ts_mov_b32 s5, 0\t; encoding: [0x80,0x03,0x85,0xbe]\n"
|
|
"\ts_mov_b32 s6, 0x400\t; encoding: [0xff,0x03,0x86,0xbe,0x00,0x04,0x00,0x00]\n"
|
|
"\ts_mov_b32 s7, 0x30016fac\t; encoding: [0xff,0x03,0x87,0xbe,0xac,0x6f,0x01,0x30]\n"
|
|
"\tv_lshlrev_b32_e32 v5, 2, v0\t; encoding: [0x82,0x00,0x0a,0x04]\n"
|
|
"\tbuffer_store_dword v2, v5, s[4:7], 0 offen\t; encoding: [0x00,0x10,0x70,0xe0,0x05,0x02,0x01,0x80]\n"
|
|
"\ts_endpgm\t; encoding: [0x00,0x00,0x81,0xbf]\n"
|
|
).encode("utf-8")
|
|
asm = support.translate_listing("cs_w64_vcc_tuple_branch", listing, fssm=fssm_header(False))
|
|
validate_v12(self, asm, "cs_w64_vcc_tuple_branch")
|
|
mem = {base_in: 0xA1B2C3D4}
|
|
si.run_workgroup(
|
|
asm, 64, 64,
|
|
inputs_for=lambda lane: {"%wg_id": (0, 0, 0), "%local_id": (lane, 0, 0), "%sg_lane": lane},
|
|
push_constants=[0] * 32, memory=mem,
|
|
)
|
|
for lane in range(64):
|
|
self.assertEqual(mem.get(base_out + 4 * lane), 0xA1B2C3D4, f"lane {lane} mismatch")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|