Files
mandy/tests/semantic/test_lds_unconditional_bounds.py
Jan Trencansky cd5ee11079 rdna2spirv(r9b): unconditional CS/MS LDS bounds, PS EarlyFragmentTests, mesh epilogue & PS quad swizzle
Round 9b translator fixes for rendering correctness and Mandy d3d12/ parity:

1. Unconditional CS & MS LDS OOB bounds (Mandy 21633b328 parity):
   - Record g.lds_declared from PSV0.shared for MS and RSRC2.LDS_SIZE for CS.
   - Whenever g.lds_declared > 0 (opt-out --no-lds-bounds), set
     g.lds_dwords = g.lds_bound = min(g.lds_declared, 16384) so every LDS
     read, write, sub-dword, wide, and atomic op goes through lds_access()
     (OOB reads return 0, OOB writes/atomics dropped instead of wrapping).

2. PS DB_SHADER_CONTROL bit 12 EarlyFragmentTests (Mandy f33940415/bf479a3cd):
   - Add fssm_header.db_shader_control() reading u32 at E + 0x138 (0x24C for
     rec = 0xB0; 3,023/12,101 corpus PS have bit 12 DEPTH_BEFORE_SHADER set).
   - Emit OpExecutionMode %main EarlyFragmentTests (opt-out --no-early-depth)
     and record early_depth in shader_meta when bit 12 is set and the PS does
     not export depth/stencil (no exp mrtz, DB_SHADER_CONTROL & 0x7 == 0, no
     OSG1 SV_Depth*/SV_StencilRef).

3. Defensive mesh epilogue & PS helper-lane quad swizzle:
   - MeshEXT and --mesh-compute epilogues explicitly write CullPrimitiveEXT =
     true (1) and (0, 0, 0) when %mli < %msp and !%ms_has_prim.
   - Lower quad-local ds_swizzle_b32 in PS via OpGroupNonUniformQuadBroadcast /
     OpGroupNonUniformQuadSwap (GroupNonUniformQuad capability) so Vulkan 2x2
     helper invocations participate on AMD RADV while EXEC-masked lanes still
     read 0.

Verified: compileall, check_no_game_data.py, 6,674/6,674 semantic tests (0
failures), 200/200 corpus slice (200/200 spirv-val), plus targeted LDS MS
(5/5), LDS CS (60/60), early-depth PS (120/120), and PS quad-swizzle (50/50)
corpus slices (100% spirv-val). AI-assisted.
2026-11-29 09:25:40 -05:00

159 lines
7.2 KiB
Python

"""FIX 9b-1: unconditional RDNA2 LDS OOB bounds on CS and MS (Mandy 21633b328 parity).
Both compute and mesh shaders with a declared LDS allocation (> 0 dwords) size %lds_arr to the exact
declared allocation (even when non-power-of-two) and bounds-check every LDS read, write, and atomic
against lds_bound (OOB reads return 0, OOB writes/atomics are dropped and never wrap onto slot 0).
All fixtures are hand-written; no game data.
"""
import os
import re
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_mini_interp as si # noqa: E402
rdna2spirv = support.rdna2spirv
FIXTURES = os.path.join(HERE, "fixtures")
VSHARP = [0, 0, 0x1000, 0x31016FAC]
def _load_asm(filename):
with open(os.path.join(FIXTURES, filename), encoding="utf-8") as f:
return [ln.split(";")[0].strip() for ln in f if ln.split(";")[0].strip()]
def _cs_fssm(lds_units, threads=32):
return bytes(0x138) + struct.pack("<4I", 0x88 | (lds_units << 15), threads, 1, 1)
def _ms_dxbc(group_shared, threads=64, maxv=64, maxp=64):
r = bytearray(48)
struct.pack_into("<I", r, 0, group_shared)
struct.pack_into("<HH", r, 12, maxv, maxp)
r[24], r[27] = 13, 2
struct.pack_into("<III", r, 36, threads, 1, 1)
part = b"PSV0" + struct.pack("<I", 4 + len(r)) + struct.pack("<I", len(r)) + bytes(r)
head = 36
return b"DXBC" + bytes(16) + struct.pack("<IIII", 1, head + len(part), 1, head) + part
def _validate_if_available(testcase, spv_text):
if not (shutil.which("spirv-as") and shutil.which("spirv-val")):
return
with tempfile.TemporaryDirectory() as tmp:
sa = os.path.join(tmp, "t.spvasm")
sv = os.path.join(tmp, "t.spv")
with open(sa, "w", encoding="utf-8") as f:
f.write(spv_text)
r = subprocess.run(["spirv-as", "--target-env", "vulkan1.2", sa, "-o", sv], capture_output=True, text=True)
testcase.assertEqual(r.returncode, 0, f"spirv-as failed:\n{r.stderr}")
v = subprocess.run(["spirv-val", "--target-env", "vulkan1.2", sv], capture_output=True, text=True)
testcase.assertEqual(v.returncode, 0, f"spirv-val failed:\n{v.stderr}")
class UnconditionalLdsBoundsTests(unittest.TestCase):
def test_cs_dynamic_oob_npot_drops_writes_and_reads_zero(self):
lines = _load_asm("cs_lds_dynamic_oob.asm")
spv = support.translate_lines("cs_lds_dynamic_oob", lines, fssm=_cs_fssm(3, threads=32))
gen = rdna2spirv.translate.last_gen
self.assertEqual((gen.lds_declared, gen.lds_dwords, gen.lds_bound), (384, 384, 384))
self.assertIsNone(re.search(r"= OpBitwiseAnd %uint %\w+ %\w+\n\s*%\w+ = OpAccessChain %_ptr_WG_uint\w* %lds ", spv))
_validate_if_available(self, spv)
mem = si.run_workgroup(
spv, 1, 32,
inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i},
push_constants=VSHARP + [0] * 28, memory={},
)
# Slot 0 and slot 383 keep their sentinels; dynamic OOB reads and atomic return 0.
self.assertEqual(
(mem.get(0), mem.get(4), mem.get(8), mem.get(12), mem.get(16)),
(0x11223344, 0x55667788, 0, 0, 0),
)
def test_ms_dynamic_oob_npot_drops_writes_and_reads_zero(self):
lines = _load_asm("ms_lds_dynamic_oob.asm")
spv = support.translate_lines("ms_lds_dynamic_oob", lines, dxbc=_ms_dxbc(3072, threads=64))
gen = rdna2spirv.translate.last_gen
self.assertEqual((gen.lds_declared, gen.lds_dwords, gen.lds_bound), (768, 768, 768))
self.assertIsNone(re.search(r"= OpBitwiseAnd %uint %\w+ %\w+\n\s*%\w+ = OpAccessChain %_ptr_WG_uint\w* %lds ", spv))
_validate_if_available(self, spv)
_, outs = si.run_workgroup(
spv, 64, 64,
inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%num_wg": (1, 1, 1), "%local_index": i, "%sg_lane": i},
push_constants=[0] * 36, memory={}, return_outputs=True,
)
pos0 = outs[0]["%ms_out_pos"][:4]
self.assertEqual(pos0, (0x42000000, 0x42800000, 0, 0))
def test_cs_wide_and_subdword_boundary_straddle_validates_and_bounds_each_dword(self):
# 3 units = 384 dwords = 1536 bytes. Write b128 starting at byte 1528 (dwords 382, 383 in-bounds;
# dwords 384, 385 OOB), write2_b32 with offset0=0 (in-bounds) offset1=1 (OOB) from base 1532,
# and sub-dword b8/b16 at OOB byte 1536 and straddling byte 1535.
lines = [
"v_mov_b32_e32 v0, 0",
"v_mov_b32_e32 v1, 0xaaaaaaaa",
"ds_write_b32 v0, v1", # sentinel at slot 0
"v_mov_b32_e32 v2, 1528",
"v_mov_b32_e32 v4, 0x11111111",
"v_mov_b32_e32 v5, 0x22222222",
"v_mov_b32_e32 v6, 0x33333333",
"v_mov_b32_e32 v7, 0x44444444",
"ds_write_b128 v2, v[4:7]", # dwords 382, 383 written; 384, 385 dropped
"v_mov_b32_e32 v8, 1536",
"v_mov_b32_e32 v9, 0xff",
"ds_write_b8 v8, v9", # OOB byte dropped
"v_mov_b32_e32 v10, 1535",
"v_mov_b32_e32 v11, 0xbeef",
"ds_write_b16 v10, v11", # crosses past allocation: whole write dropped
"v_mov_b32_e32 v12, 2044",
"v_mov_b32_e32 v13, 0x77777777",
"v_mov_b32_e32 v14, 0x88888888",
"ds_write2_b32 v12, v13, v14 offset0:0 offset1:1", # both OOB (2044, 2048); must not clobber slot 0
"s_waitcnt lgkmcnt(0)",
"ds_read_b128 v[16:19], v2", # expects (0x11111111, 0x22222222, 0, 0)
"ds_read2_b32 v[20:21], v0 offset0:0 offset1:255", # slot 0 = 0xaaaaaaaa, slot 255 = 0
"ds_read_u16 v22, v10", # crossing past end -> 0
"ds_read_u8 v23, v8", # OOB -> 0
"s_waitcnt lgkmcnt(0)",
"v_mov_b32_e32 v30, 0",
"buffer_store_dword v16, v30, s[0:3], 0 offen",
"v_mov_b32_e32 v30, 4",
"buffer_store_dword v17, v30, s[0:3], 0 offen",
"v_mov_b32_e32 v30, 8",
"buffer_store_dword v18, v30, s[0:3], 0 offen",
"v_mov_b32_e32 v30, 12",
"buffer_store_dword v19, v30, s[0:3], 0 offen",
"v_mov_b32_e32 v30, 16",
"buffer_store_dword v20, v30, s[0:3], 0 offen",
"v_mov_b32_e32 v30, 20",
"buffer_store_dword v22, v30, s[0:3], 0 offen",
"v_mov_b32_e32 v30, 24",
"buffer_store_dword v23, v30, s[0:3], 0 offen",
"s_endpgm",
]
spv = support.translate_lines("cs_lds_wide_straddle", lines, fssm=_cs_fssm(3, threads=32))
_validate_if_available(self, spv)
mem = si.run_workgroup(
spv, 1, 32,
inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i},
push_constants=VSHARP + [0] * 28, memory={},
)
self.assertEqual(
tuple(mem.get(4 * k) for k in range(7)),
(0x11111111, 0x22222222, 0, 0, 0xAAAAAAAA, 0, 0),
)
if __name__ == "__main__":
unittest.main()