diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 06:08:10 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 06:08:10 -0500 |
| commit | 481a5ee6ce615ae3f0a400a04529fb9c20ac5a42 (patch) | |
| tree | 455b0a51b310ca64dc1ed87281a638c6e1ed9831 | |
| parent | 016d64254fc8b326f9a58876fad3f122b6b8ea09 (diff) | |
| parent | f4edee1530a8243fbcb11346fe8d1662c001c07b (diff) | |
Merge pull request #1390 from daveleone
| -rw-r--r-- | tests/semantic/test_or_saveexec.py | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/tests/semantic/test_or_saveexec.py b/tests/semantic/test_or_saveexec.py new file mode 100644 index 000000000..7b9d317a7 --- /dev/null +++ b/tests/semantic/test_or_saveexec.py | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | """Semantic interpreter test for s_or_saveexec_b32 (and its wave64 form, s_or_saveexec_b64). | ||
| 2 | |||
| 3 | AMD "RDNA 2" Instruction Set Architecture, section 12.3 SOP1 Instructions (opcodes 37 and 61), with S0 the source | ||
| 4 | mask and EXEC the mask before the instruction: | ||
| 5 | |||
| 6 | D = EXEC; EXEC = S0 | EXEC; SCC = (EXEC != 0) | ||
| 7 | |||
| 8 | Each case translates a synthetic compute shader with tools/rdna2spirv.py and runs every lane of the wave in lockstep | ||
| 9 | with spirv_mini_interp.run_workgroup, in wave32 (_b32) and native wave64 (_b64); --wave64-emulate is checked to | ||
| 10 | translate only, since the interpreter does not run its workgroup barriers. Per lane the shader stores whether the | ||
| 11 | lane is on in the new EXEC, its bit of D, and SCC; the expected values come from a plain-Python model of the | ||
| 12 | pseudocode above. Before the op, D is set to all ones and SCC to the opposite of the expected value, so a result the | ||
| 13 | translator never writes cannot pass. The cases: S0 and EXEC overlapping in part; EXEC empty, so the op turns S0's | ||
| 14 | lanes back on and D = 0; and both empty, so EXEC stays 0 and SCC = 0. | ||
| 15 | """ | ||
| 16 | import os | ||
| 17 | import struct | ||
| 18 | import sys | ||
| 19 | import unittest | ||
| 20 | |||
| 21 | HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| 22 | if HERE not in sys.path: | ||
| 23 | sys.path.insert(0, HERE) # python -I leaves the script's folder off sys.path | ||
| 24 | |||
| 25 | import support # noqa: E402 (support puts tools/ on sys.path, honouring LEONIDA_TOOLS_DIR) | ||
| 26 | import spirv_mini_interp as si # noqa: E402 | ||
| 27 | |||
| 28 | rdna2spirv = support.rdna2spirv | ||
| 29 | VSHARP = [0, 0, 0xFFFFFFFF, 0x31016FAC] # raw buffer, every store in range (as in test_carry_exec_mask.py) | ||
| 30 | |||
| 31 | |||
| 32 | def ref(s0, exec_): | ||
| 33 | """(new EXEC, D) from the ISA pseudocode; masks are Python sets of lanes.""" | ||
| 34 | return s0 | exec_, exec_ | ||
| 35 | |||
| 36 | |||
| 37 | # (S0 lanes, EXEC lanes) per wave size: partial overlap; EXEC empty; both empty. The wave64 masks have lanes in both | ||
| 38 | # halves, so a result taken from one half only shows. | ||
| 39 | MASKS = { | ||
| 40 | 32: [({1, 3, 5, 20, 31}, {3, 4, 5, 17, 31}), ({2, 30}, set()), (set(), set())], | ||
| 41 | 64: [({1, 3, 33, 40, 63}, {3, 4, 33, 50, 63}), ({2, 45}, set()), (set(), set())], | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | def _regs(wave_size): | ||
| 46 | if wave_size == 64: | ||
| 47 | return {"op": "s_or_saveexec_b64", "exec": "exec", "mov": "s_mov_b64", "or": "s_or_b64", | ||
| 48 | "s0": "s[4:5]", "tmp": "s[6:7]", "old": "s[8:9]", "d": "s[10:11]"} | ||
| 49 | return {"op": "s_or_saveexec_b32", "exec": "exec_lo", "mov": "s_mov_b32", "or": "s_or_b32", | ||
| 50 | "s0": "s4", "tmp": "s6", "old": "s8", "d": "s10"} | ||
| 51 | |||
| 52 | |||
| 53 | def _mask_lines(lanes, dst, r): | ||
| 54 | """Build a lane mask in `dst` that is 1 for `lanes` and 0 elsewhere (v0 = lane index), under a full EXEC. | ||
| 55 | |||
| 56 | Masks come from v_cmp; the only constants moved into a mask anywhere in this file are 0 and -1, the same for | ||
| 57 | every lane, so the test does not depend on how the translator splits a constant mask into lane bits (SHADER-198). | ||
| 58 | """ | ||
| 59 | if not lanes: | ||
| 60 | return [f"{r['mov']} {dst}, 0"] | ||
| 61 | lanes = sorted(lanes) | ||
| 62 | lines = [f"v_cmp_eq_u32_e64 {dst}, v0, {lanes[0]}"] | ||
| 63 | for lane in lanes[1:]: | ||
| 64 | lines.append(f"v_cmp_eq_u32_e64 {r['tmp']}, v0, {lane}") | ||
| 65 | lines.append(f"{r['or']} {dst}, {dst}, {r['tmp']}") | ||
| 66 | return lines | ||
| 67 | |||
| 68 | |||
| 69 | def _program(s0_lanes, exec_lanes, wave_size, scc_before): | ||
| 70 | """Lines that run the op with S0 = s0_lanes and EXEC = exec_lanes, then store per lane (12 bytes): the new EXEC | ||
| 71 | bit, the D bit and SCC. D starts as all ones and SCC as `scc_before`, so a value the op does not write shows.""" | ||
| 72 | r = _regs(wave_size) | ||
| 73 | lines = [f"{r['mov']} {r['exec']}, -1"] | ||
| 74 | lines += _mask_lines(s0_lanes, r["s0"], r) | ||
| 75 | lines += _mask_lines(exec_lanes, r["old"], r) | ||
| 76 | lines += [ | ||
| 77 | "v_mov_b32 v6, 0", # 1 below only on lanes the new EXEC keeps on | ||
| 78 | f"{r['mov']} {r['d']}, -1", | ||
| 79 | f"{r['mov']} {r['exec']}, {r['old']}", # EXEC before the op (s_mov does not write SCC) | ||
| 80 | "s_cmp_eq_u32 0, 0" if scc_before else "s_cmp_lg_u32 0, 0", | ||
| 81 | f"{r['op']} {r['d']}, {r['s0']}", | ||
| 82 | "v_mov_b32 v6, 1", | ||
| 83 | "s_cselect_b32 s14, 1, 0", # SCC of the op | ||
| 84 | f"{r['mov']} {r['exec']}, -1", | ||
| 85 | f"v_cndmask_b32_e64 v7, 0, 1, {r['d']}", | ||
| 86 | "v_mov_b32 v8, s14", | ||
| 87 | "v_mul_lo_u32 v11, v0, 12", | ||
| 88 | "buffer_store_dword v6, v11, s[0:3], 0 offen", | ||
| 89 | "v_add_nc_u32 v12, 4, v11", | ||
| 90 | "buffer_store_dword v7, v12, s[0:3], 0 offen", | ||
| 91 | "v_add_nc_u32 v12, 8, v11", | ||
| 92 | "buffer_store_dword v8, v12, s[0:3], 0 offen", | ||
| 93 | ] | ||
| 94 | return lines | ||
| 95 | |||
| 96 | |||
| 97 | def _fssm(wave_size): | ||
| 98 | """Compute-shader header: COMPUTE_PGM_RSRC2 0x8088 (4 user SGPRs, s[0:3] = output V#; TGID_X), then the | ||
| 99 | thread-group size: one wave of `wave_size` threads.""" | ||
| 100 | return bytes(0x138) + struct.pack("<4I", 0x8088, wave_size, 1, 1) | ||
| 101 | |||
| 102 | |||
| 103 | def _translate_cs(name, body_lines, wave_size, emulate): | ||
| 104 | """Translate a synthetic compute shader of one wave; returns the SPIR-V text.""" | ||
| 105 | prev_emu = rdna2spirv.WAVE64_EMULATE | ||
| 106 | rdna2spirv.WAVE64_EMULATE = emulate | ||
| 107 | try: | ||
| 108 | spv = support.translate_lines(name, [*body_lines, "s_endpgm"], fssm=_fssm(wave_size)) | ||
| 109 | finally: | ||
| 110 | rdna2spirv.WAVE64_EMULATE = prev_emu | ||
| 111 | got64 = rdna2spirv.translate.last_gen.wave64 | ||
| 112 | if got64 != (wave_size == 64): | ||
| 113 | raise AssertionError(f"{name}: expected wave64={wave_size == 64}, got {got64}") | ||
| 114 | return spv | ||
| 115 | |||
| 116 | |||
| 117 | def _run_cs(name, body_lines, wave_size): | ||
| 118 | """Translate a synthetic compute shader and run all lanes of one wave on one subgroup; returns the memory.""" | ||
| 119 | spv = _translate_cs(name, body_lines, wave_size, emulate=False) | ||
| 120 | return si.run_workgroup( | ||
| 121 | spv, wave_size, wave_size, | ||
| 122 | inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i}, | ||
| 123 | push_constants=VSHARP + [0] * 28, memory={}, | ||
| 124 | ) | ||
| 125 | |||
| 126 | |||
| 127 | class ReferenceTests(unittest.TestCase): | ||
| 128 | def test_reference(self): | ||
| 129 | # The model itself, on hand-checked pairs. | ||
| 130 | self.assertEqual(ref({1, 3}, {3, 4}), ({1, 3, 4}, {3, 4})) | ||
| 131 | self.assertEqual(ref({2}, set()), ({2}, set())) | ||
| 132 | self.assertEqual(ref(set(), set()), (set(), set())) | ||
| 133 | |||
| 134 | |||
| 135 | class _OrSaveExecMixin: | ||
| 136 | wave_size = 32 | ||
| 137 | |||
| 138 | def test_exec_d_and_scc(self): | ||
| 139 | for case, (s0, exec_) in enumerate(MASKS[self.wave_size]): | ||
| 140 | with self.subTest(case=case, wave=self.wave_size): | ||
| 141 | want_exec, want_d = ref(s0, exec_) | ||
| 142 | want_scc = 1 if want_exec else 0 | ||
| 143 | lines = _program(s0, exec_, self.wave_size, scc_before=1 - want_scc) | ||
| 144 | mem = _run_cs(f"cs_or_saveexec_w{self.wave_size}_{case}", lines, self.wave_size) | ||
| 145 | for lane in range(self.wave_size): | ||
| 146 | base = lane * 12 | ||
| 147 | self.assertEqual(mem[base], int(lane in want_exec), f"lane {lane}: new EXEC bit") | ||
| 148 | self.assertEqual(mem[base + 4], int(lane in want_d), f"lane {lane}: D bit") | ||
| 149 | self.assertEqual(mem[base + 8], want_scc, f"lane {lane}: SCC") | ||
| 150 | |||
| 151 | |||
| 152 | class Wave32Tests(_OrSaveExecMixin, unittest.TestCase): | ||
| 153 | wave_size = 32 | ||
| 154 | |||
| 155 | |||
| 156 | class Wave64Tests(_OrSaveExecMixin, unittest.TestCase): | ||
| 157 | wave_size = 64 | ||
| 158 | |||
| 159 | |||
| 160 | class Wave64EmulateTranslationTests(unittest.TestCase): | ||
| 161 | """--wave64-emulate: the emulated wave synchronises through workgroup barriers, which spirv_mini_interp does not | ||
| 162 | run (as in test_carry_exec_mask.py and test_salu64_bitwise_data.py), so only the translation is checked here.""" | ||
| 163 | |||
| 164 | def test_translates(self): | ||
| 165 | s0, exec_ = MASKS[64][0] | ||
| 166 | spv = _translate_cs("cs_emu_or_saveexec", _program(s0, exec_, 64, scc_before=0), 64, emulate=True) | ||
| 167 | self.assertIn("OpStore %x_it", spv) # the exchange-round counter, declared only by the emulated translation | ||
| 168 | |||
| 169 | |||
| 170 | if __name__ == "__main__": | ||
| 171 | unittest.main() | ||