Files
mandy/tests/semantic/test_export_valid_mask.py
StebanSchrader 5f50d57f47 Translator: a PS discards by EXEC at its last vm export
The epilogue killed by EXEC at the end of the program, so an EXEC write
after the `exp ... vm` changed which pixels survive. Keep EXEC at that
export, only where a path can write EXEC afterwards: other programs
translate to the same text as before.

The rule is from the RDNA2 ISA reference guide (30-November-2020),
11.1 and 11.2.1. Not measured on hardware.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 12:43:03 +00:00

150 lines
7.0 KiB
Python

"""The PS valid mask: a pixel is discarded by EXEC at its last executed `exp ... vm`, not by EXEC at the end.
RDNA2 ISA reference guide (30-November-2020), 11.1 and 11.2.1: with VM set EXEC is the valid mask, the final one
sent is used, and an export to NULL may carry only the mask. Not measured on hardware; docs/design.md, EXEC.
Hand-written programs, no tools needed.
"""
import unittest
import support
import rdna2spirv
import shader_ir
from spirv_mini_interp import run_fragment
from spirv_wave_interp import run_wave
ONE = 0x3F800000
SEED = [f"v_mov_b32_e32 v2, 0x{ONE:x}", "v_mov_b32_e32 v1, 1"]
OFF = ["v_cmpx_eq_u32_e32 0, v1"]
ON = ["s_mov_b64 exec, -1"]
EXP = ["exp mrt0 v2, v2, v2, v2 done vm"]
EXP_NO_VM = ["exp mrt0 v2, v2, v2, v2"]
EXP_MRT1 = ["exp mrt1 v2, v2, v2, v2"]
NULL = ["exp null off, off, off, off done vm"]
END = ["s_endpgm"]
KILLED = {}
MRT0 = {"%out_mrt0": (ONE,) * 4}
def sized(lines):
"""The lines with a placeholder encoding of the right size: branch offsets are counted in dwords of it."""
out = []
for line in lines:
size = 8 if line.startswith("exp ") or "0x3f800000" in line else 4
out.append(f"{line} ; encoding: [" + ",".join(["0x00"] * size) + "]")
return out
def translate(lines):
return support.translate_lines("ps_valid_mask", sized(lines))
def dwords(lines):
return sum(2 if line.startswith("exp ") or "0x3f800000" in line else 1 for line in lines)
class StraightLine(unittest.TestCase):
def check(self, lines, want):
self.assertEqual(run_fragment(translate(SEED + lines + END)), want)
def test_exec_unchanged_after_the_export(self):
self.check(EXP, MRT0)
self.check(OFF + EXP, KILLED)
def test_off_at_the_export_and_back_on_is_killed(self):
self.check(OFF + EXP + ON, KILLED)
def test_on_at_the_export_and_off_afterwards_is_kept(self):
self.check(EXP + OFF, MRT0)
self.check(EXP + ["s_mov_b64 exec, 0"], MRT0)
def test_null_export_carries_the_mask_too(self):
self.check(EXP_NO_VM + OFF + NULL + ON, KILLED)
self.check(EXP_NO_VM + NULL + OFF, MRT0)
def test_compressed_export_carries_the_mask_too(self):
compr = ["exp mrt0 v2, v2, v2, v2 done compr vm"]
self.check(OFF + compr + ON, KILLED)
self.assertEqual(list(run_fragment(translate(SEED + compr + OFF + END))), ["%out_mrt0"])
def test_depth_export_carries_the_mask_too(self):
depth = ["exp mrtz v2, off, off, off done vm"]
self.check(EXP_NO_VM + OFF + depth + ON, KILLED)
self.assertEqual(run_fragment(translate(SEED + EXP_NO_VM + depth + OFF + END))["%out_mrt0"], (ONE,) * 4)
def test_the_last_vm_export_wins(self):
first = ["exp mrt0 v2, v2, v2, v2 vm"]
self.check(first + OFF + NULL, KILLED)
self.check(first + OFF + ON + NULL + OFF, MRT0)
def test_a_later_export_without_vm_does_not_move_the_mask(self):
self.check(EXP + OFF + EXP_MRT1, {**MRT0, "%out_mrt1": (0,) * 4}) # 0: the interpreter's unwritten output
def test_without_a_vm_export_exec_at_the_end_decides(self):
self.check(EXP_NO_VM + OFF, KILLED)
self.check(EXP_NO_VM + OFF + ON, MRT0)
class Branches(unittest.TestCase):
def skip_over(self, taken, skipped):
"""SCC = `taken`, then a branch over `skipped`."""
return [f"s_cmp_eq_u32 0, {0 if taken else 1}", f"s_cbranch_scc1 {dwords(skipped)}"] + skipped
def test_exec_write_in_a_later_block(self):
for taken in (False, True):
body = EXP + self.skip_over(taken, OFF)
self.assertEqual(run_fragment(translate(SEED + body + END)), MRT0, taken)
body = OFF + EXP + self.skip_over(taken, ON)
self.assertEqual(run_fragment(translate(SEED + body + END)), KILLED, taken)
def test_exec_write_several_blocks_later(self):
far = self.skip_over(False, ["v_mov_b32_e32 v3, 0"])
body = EXP + far + self.skip_over(False, OFF)
self.assertEqual(run_fragment(translate(SEED + body + END)), MRT0)
body = OFF + EXP + far + self.skip_over(False, ON)
self.assertEqual(run_fragment(translate(SEED + body + END)), KILLED)
def test_a_path_around_the_vm_export_uses_exec_at_the_end(self):
# The branch skips the export: that path has no valid mask, so EXEC at the end decides, as before.
body = EXP_NO_VM + self.skip_over(True, NULL) + OFF
self.assertEqual(run_fragment(translate(SEED + body + END)), KILLED)
body = EXP_NO_VM + self.skip_over(False, NULL) + OFF
self.assertEqual(run_fragment(translate(SEED + body + END)), MRT0)
def test_exec_write_earlier_in_a_loop(self):
# Two rounds. EXEC is written above the export, so only the back edge puts that write after it.
head = SEED + EXP_NO_VM + ["s_mov_b32 s4, 0"]
loop = ON + ["s_cmp_eq_u32 s4, 0", "s_cbranch_scc1 1"] + OFF + NULL + ["s_add_u32 s4, s4, 1",
"s_cmp_lt_u32 s4, 2"]
back = [f"s_cbranch_scc1 {65536 - dwords(loop) - 1}"]
text = translate(head + loop + back + ON + END)
self.assertEqual(run_wave(text, wave=32)["outputs"], [None] * 32) # off at the second round's export
loop = ON + ["s_cmp_eq_u32 s4, 1", "s_cbranch_scc1 1"] + OFF + NULL + ["s_add_u32 s4, s4, 1",
"s_cmp_lt_u32 s4, 2"]
text = translate(head + loop + back + OFF + END)
self.assertNotIn(None, run_wave(text, wave=32)["outputs"]) # on at the second round's export
class OnlyWhereNeeded(unittest.TestCase):
def test_no_latch_when_exec_cannot_change_after_the_export(self):
for body in (EXP, OFF + EXP, OFF + ON + EXP, EXP_NO_VM + OFF, OFF + EXP + ["s_cmp_eq_u64 exec, 0"],
OFF + EXP + ["v_mov_b32_e32 v3, 0"]):
self.assertNotIn("%vm_mask", translate(SEED + body + END), body)
def test_code_after_endpgm_does_not_count(self):
self.assertNotIn("%vm_mask", translate(SEED + OFF + EXP + END + ON))
def test_every_form_that_writes_exec_is_seen(self):
for line in ("v_cmpx_eq_u32_e32 0, v1", "v_cmpx_lt_f32_e64 v1, v2", "v_cmpx_class_f32_e32 v1, v2",
"v_cmpx_eq_u64 v[2:3], v[4:5]", "s_and_saveexec_b64 s[4:5], vcc",
"s_or_saveexec_b32 s4, vcc_lo", "s_andn2_wrexec_b64 s[4:5], s[6:7]", "s_mov_b64 exec, -1",
"s_mov_b32 exec_lo, -1", "s_mov_b32 exec_hi, 0", "s_wqm_b64 exec, exec",
"s_and_b64 exec, exec, s[4:5]", "v_cmp_eq_u32_e64 exec, 0, v1"):
self.assertTrue(rdna2spirv.writes_exec(shader_ir.parse_line("\t" + line)), line)
for line in ("s_cmp_eq_u64 exec, 0", "s_bitcmp1_b32 exec_lo, 3", "s_mov_b64 s[4:5], exec",
"exp mrt0 v0, v1, v2, v3 done vm", "v_cndmask_b32_e64 v0, v1, v2, exec"):
self.assertFalse(rdna2spirv.writes_exec(shader_ir.parse_line("\t" + line)), line)
if __name__ == "__main__":
unittest.main()