mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 12:08:36 +00:00
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.
192 lines
10 KiB
Python
192 lines
10 KiB
Python
"""How big the translator makes a compute shader's LDS, when its accesses are bounds-checked, and the wrap.
|
|
|
|
Standard library only; no GPU, no LLVM, no game files.
|
|
|
|
COMPUTE_PGM_RSRC2 bits [23:15] (LDS_SIZE) declare the group-shared size in 512-byte units (128 dwords). Where the
|
|
translator puts the Workgroup array `%lds_arr`:
|
|
* every constant `offset:` (bytes) or offset0/offset1 (4-byte units, x64 for st64, 8-byte units for b64) reaches
|
|
some number of dwords from address 0 (offset / 4 + 1 for a dword access); while all of them stay inside the
|
|
declared size, the array is a power of two of at least 16 dwords covering the declared size, capped at 16384
|
|
(64 KiB), and a dynamic address wraps at its size (dword (A / 4) mod size);
|
|
* a constant offset past the declared size (SHADER-309; RDNA2 LDS does not wrap, ISA 3.6.1/3.6.6) makes the array
|
|
exactly the declared size and every access bounds-checked: out of range reads 0 and writes are dropped;
|
|
* a declared size over 64 KiB and a thread group of more than 1024 invocations are refused; TG_SIZE_EN (RSRC2
|
|
bit 10) is accepted (test_cs_tg_size.py).
|
|
test_cs_shared_memory_limit.py runs the bounds-checked reads, writes and atomics; this file pins the array size and
|
|
which of the two forms is chosen.
|
|
|
|
Each case translates a hand-written listing with tools/rdna2spirv.py (support.translate_lines) and reads the array
|
|
length from the SPIR-V assembly; the wrap cases also run the shader in tests/semantic/spirv_mini_interp.py. The sizes
|
|
are worked out by hand from the rules above. Until this file, the LDS tests of this name ran a self-built
|
|
interpreter that copied the older rule (offsets grow the array) instead of calling the translator.
|
|
"""
|
|
import os
|
|
import re
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import lds_run as L # noqa: E402 (also puts tools/ on sys.path)
|
|
import rdna2spirv # noqa: E402
|
|
|
|
LDS_SIZE_SHIFT = 15
|
|
|
|
|
|
def rsrc2(units, extra=0):
|
|
"""4 user SGPRs, workgroup id in s4 (0x88) with `units` x 512 bytes of LDS."""
|
|
return 0x88 | (units << LDS_SIZE_SHIFT) | extra
|
|
|
|
|
|
def translate(line, units, size=(32, 1, 1)):
|
|
return L.translate(["v_mov_b32_e32 v1, 0", line], rsrc2(units), size)
|
|
|
|
|
|
def lds_dwords(line, units=0, size=(32, 1, 1)):
|
|
spv = translate(line, units, size)
|
|
m = re.search(r"%lds_arr = OpTypeArray %uint (%\S+)", spv)
|
|
assert m, "no LDS array declared"
|
|
c = re.search(re.escape(m.group(1)) + r" = OpConstant %uint (\d+)", spv)
|
|
return int(c.group(1))
|
|
|
|
|
|
def bounds_checked(line, units=0):
|
|
"""True when an `OpULessThan %bool %idx <limit>` guards an access chain into %lds at the same %idx. An unrelated
|
|
OpULessThan (a v_cmp_lt_u32, an EXEC path) does not satisfy it. (With nothing declared the array is padded to
|
|
one dword and the limit is 0, so the limit is not compared with the array length.)"""
|
|
spv = translate(line, units)
|
|
for c in re.finditer(r"OpULessThan %bool (%\S+) %\S+\s", spv):
|
|
if re.search(r"OpAccessChain %_ptr_WG_uint %lds " + re.escape(c.group(1)) + r"\s", spv):
|
|
return True
|
|
return False
|
|
|
|
|
|
READ = "ds_read_b32 v2, v1"
|
|
|
|
|
|
class TestAllocationSize(unittest.TestCase):
|
|
def test_declared_size_by_hand(self):
|
|
# With unconditional LDS_BOUNDS (default ON): units > 0 allocates exactly units * 128 dwords and
|
|
# bounds-checks every access; units == 0 with no constant offset falls back to the 16-dword minimum.
|
|
for units, want, checked in ((0, 16, False), (1, 128, True), (2, 256, True), (3, 384, True),
|
|
(5, 640, True), (8, 1024, True), (9, 1152, True), (32, 4096, True),
|
|
(33, 4224, True), (64, 8192, True), (65, 8320, True), (128, 16384, True)):
|
|
with self.subTest(units=units):
|
|
self.assertEqual(lds_dwords(READ, units), want)
|
|
self.assertEqual(bounds_checked(READ, units), checked)
|
|
|
|
def test_declared_size_unbounded_opt_out(self):
|
|
# With --no-lds-bounds (LDS_BOUNDS=False): 128 per unit rounded up to a power of two, 16 .. 16384, no check.
|
|
prev = rdna2spirv.LDS_BOUNDS
|
|
rdna2spirv.LDS_BOUNDS = False
|
|
try:
|
|
for units, want in ((0, 16), (1, 128), (2, 256), (3, 512), (5, 1024), (8, 1024), (9, 2048), (32, 4096),
|
|
(33, 8192), (64, 8192), (65, 16384), (128, 16384)):
|
|
with self.subTest(units=units):
|
|
self.assertEqual(lds_dwords(READ, units), want)
|
|
self.assertFalse(bounds_checked(READ, units))
|
|
finally:
|
|
rdna2spirv.LDS_BOUNDS = prev
|
|
|
|
def test_offset_inside_the_declaration_changes_nothing(self):
|
|
# 1 unit = 128 dwords = 512 bytes: the last dword is at byte 0x1FC; array stays 128 dwords, bounds-checked.
|
|
for off in (0, 0x40, 0x1FC):
|
|
with self.subTest(offset=hex(off)):
|
|
self.assertEqual(lds_dwords(f"ds_read_b32 v2, v1 offset:{off:#x}", 1), 128)
|
|
self.assertTrue(bounds_checked(f"ds_read_b32 v2, v1 offset:{off:#x}", 1))
|
|
|
|
def test_offset_past_the_declaration_bounds_checks_at_exactly_the_declared_size(self):
|
|
# byte 0x200 is dword 128: one past the end of a 1-unit allocation. A 3-unit one is 384 dwords (not a power of
|
|
# two): the array is 384 then, not rounded up to 512.
|
|
for units, off, want in ((1, 0x200, 128), (1, 0x1000, 128), (3, 0x600, 384), (2, 0x400, 256)):
|
|
line = f"ds_read_b32 v2, v1 offset:{off:#x}"
|
|
with self.subTest(units=units, offset=hex(off)):
|
|
self.assertEqual(lds_dwords(line, units), want)
|
|
self.assertTrue(bounds_checked(line, units))
|
|
|
|
def test_nothing_declared_and_an_offset_gives_an_empty_checked_array(self):
|
|
line = "ds_read_b32 v2, v1 offset:0x100"
|
|
self.assertTrue(bounds_checked(line, 0))
|
|
self.assertEqual(lds_dwords(line, 0), 1) # a 0-length array is not legal SPIR-V; every access fails the check
|
|
|
|
def test_two_address_and_wide_offsets_count_their_width(self):
|
|
# 1 unit = 128 dwords. Verify ds_const_extent tracks exact dword need (checked via --no-lds-bounds too).
|
|
prev = rdna2spirv.LDS_BOUNDS
|
|
rdna2spirv.LDS_BOUNDS = False
|
|
try:
|
|
for line, over in (("ds_read2_b32 v[2:3], v1 offset1:127", False), ("ds_read2_b32 v[2:3], v1 offset1:128", True),
|
|
("ds_read2st64_b32 v[2:3], v1 offset1:1", False),
|
|
("ds_read2st64_b32 v[2:3], v1 offset1:2", True),
|
|
("ds_read2_b64 v[2:5], v1 offset1:63", False), ("ds_read2_b64 v[2:5], v1 offset1:64", True),
|
|
("ds_read_b128 v[2:5], v1 offset:0x1F0", False), ("ds_read_b128 v[2:5], v1 offset:0x1F4", True),
|
|
("ds_read_u8 v2, v1 offset:0x1FF", False), ("ds_read_u8 v2, v1 offset:0x200", True)):
|
|
with self.subTest(line=line):
|
|
self.assertEqual(bounds_checked(line, 1), over)
|
|
finally:
|
|
rdna2spirv.LDS_BOUNDS = prev
|
|
|
|
def test_non_ds_listing_declares_none(self):
|
|
self.assertNotIn("%lds_arr", L.translate(["v_mov_b32_e32 v1, 0"], rsrc2(8)))
|
|
|
|
def test_declared_size_over_64k_is_refused(self):
|
|
with self.assertRaises(rdna2spirv.Unsupported):
|
|
translate(READ, 129) # 129 x 512 B = 66048 B
|
|
self.assertEqual(lds_dwords(READ, 128), 16384)
|
|
|
|
|
|
class TestRefusals(unittest.TestCase):
|
|
def test_tg_size_en_accepted(self):
|
|
self.assertIn("%lds_arr", L.translate([READ], rsrc2(8, extra=1 << 10)))
|
|
|
|
def test_group_over_1024_refused(self):
|
|
for size in ((1025, 1, 1), (32, 33, 1), (16, 16, 5)):
|
|
with self.subTest(size=size), self.assertRaises(rdna2spirv.Unsupported):
|
|
L.translate([READ], rsrc2(8), size)
|
|
|
|
def test_group_of_1024_accepted(self):
|
|
for size in ((1024, 1, 1), (32, 32, 1), (8, 8, 16)):
|
|
with self.subTest(size=size):
|
|
self.assertIn("%lds_arr", L.translate([READ], rsrc2(8), size))
|
|
|
|
|
|
class TestWrap(unittest.TestCase):
|
|
# Real RDNA2 LDS does not wrap (ISA 3.6.1/3.6.6): with unconditional LDS_BOUNDS (default ON), dynamic OOB
|
|
# writes are dropped (never clobbering slot 0) and OOB reads return 0. With --no-lds-bounds (LDS_BOUNDS=False),
|
|
# dynamic addresses still wrap at the power-of-two array size.
|
|
def wrap_case(self, units, write_addr, read_addr):
|
|
"""Lane i writes 0x900 + i at write_addr + 4 i, then reads read_addr + 4 i (addresses held in VGPRs)."""
|
|
body = ["v_lshlrev_b32_e32 v3, 2, v0", "v_add_nc_u32_e32 v1, %#x, v3" % write_addr,
|
|
"v_add_nc_u32_e32 v2, 0x900, v0", "ds_write_b32 v1, v2", "s_waitcnt lgkmcnt(0)",
|
|
"v_add_nc_u32_e32 v4, %#x, v3" % read_addr, "ds_read_b32 v5, v4", "s_waitcnt lgkmcnt(0)"] \
|
|
+ L.store("v5", 0)
|
|
mem = L.run(body, rsrc2=rsrc2(units))
|
|
return [L.out(mem, i, 0) for i in range(32)]
|
|
|
|
def test_dynamic_oob_access_does_not_wrap(self):
|
|
# 1 unit = 128 dwords = 512 bytes: OOB write at 0x200 is dropped (slot 0 stays 0); OOB read at 0x400 is 0.
|
|
self.assertEqual(self.wrap_case(1, 0x200, 0), [0] * 32)
|
|
self.assertEqual(self.wrap_case(1, 0, 0x400), [0] * 32)
|
|
self.assertEqual(self.wrap_case(1, 0x100, 0x100), [0x900 + i for i in range(32)]) # in-bounds: exact
|
|
self.assertEqual(self.wrap_case(1, 0x80, 0x100), [0] * 32) # a different word
|
|
|
|
def test_npot_allocation_drops_oob_past_declaration(self):
|
|
# 3 units = 384 dwords = 1536 bytes (0x600): writes at 0x600 and 0x800 are both OOB and dropped.
|
|
self.assertEqual(self.wrap_case(3, 0x800, 0), [0] * 32)
|
|
self.assertEqual(self.wrap_case(3, 0x600, 0), [0] * 32)
|
|
self.assertEqual(self.wrap_case(3, 0x600, 0x600), [0] * 32)
|
|
|
|
def test_oob_past_cap_does_not_wrap(self):
|
|
self.assertEqual(self.wrap_case(128, 0x10000, 0), [0] * 32)
|
|
|
|
def test_unbounded_opt_out_wraps_at_power_of_two(self):
|
|
prev = rdna2spirv.LDS_BOUNDS
|
|
rdna2spirv.LDS_BOUNDS = False
|
|
try:
|
|
self.assertEqual(self.wrap_case(1, 0x200, 0), [0x900 + i for i in range(32)])
|
|
self.assertEqual(self.wrap_case(3, 0x800, 0), [0x900 + i for i in range(32)])
|
|
finally:
|
|
rdna2spirv.LDS_BOUNDS = prev
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|