Files
mandy/tests/semantic/test_wave32_lane_select.py
Jan Trencansky 314b4e73dd Merge upstream ProjectLeonida/mandy main (b589e0fe0) into round10 base
# Conflicts:
#	docs/design.md
#	runtime/shims/d3d12_vulkan_device.cpp
#	runtime/src/queue_holds.h
#	runtime/src/shader_library.h
#	runtime/src/vulkan_backend.h
#	runtime/src/vulkan_replay.h
#	runtime/test/vk_harness.cpp
#	tests/semantic/test_wave32_lane_select.py
#	tools/lane_masks.py
#	tools/rdna2spirv.py
2026-11-29 06:33:39 -05:00

74 lines
3.4 KiB
Python

"""Wave32 lane select of v_readlane_b32 / v_writelane_b32 (SHADER-52), executed across the 32 lanes of the
translated SPIR-V.
The lane is S1[4:0] in wave32 and S1[5:0] in wave64. Until #488 the translator masked with 63 in wave32 too, so
a lane select of 32 or more wrote no lane, and a read shuffled from a lane past the end of a 32-wide subgroup
(undefined in SPIR-V; NVIDIA happens to wrap it). Found by tools/crosslane_fuzz.py (FUZZ-7).
tests/test_translate.py checks which masks appear in the SPIR-V text; this runs the result. The input is the GPU
test runtime/test/shaders/cs_w32_lane_wrap.asm (hand-written, no game data; fixture from regen_fixtures.py),
run by spirv_mini_interp.run_workgroup as one 32-thread workgroup on one 32-wide subgroup. A shuffle from lane
32 or more raises UndefinedResult there, so a translation that masks with 63 fails loudly instead of passing on
a driver's wrap.
"""
import re
import struct
import unittest
import support
import spirv_mini_interp as si
# 4 user SGPRs (the output V#), workgroup id in s4, 512 bytes of LDS, 32x1x1 threads (the .asm's "; group:").
FSSM = bytearray(0x148)
struct.pack_into("<4I", FSSM, 0x138, 0x8088, 32, 1, 1)
FSSM = bytes(FSSM)
VSHARP = [0, 0, 0x1000, 0x31016FAC]
# The .asm header (and check.py): readlane(40) = lane 8 = 25, readlane(5) = 16, writelane(40) lands on lane 8,
# so readlane(8) and readlane(40) both give 777. Lane 0 stores the four words.
EXPECTED = [25, 16, 777, 777]
def translate():
return support.translate_fixture("cs_w32_lane_wrap", fssm=FSSM)
def run(spv):
memory = si.run_workgroup(
spv, 32, 32,
inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i},
push_constants=VSHARP + [0] * 28, memory={})
stray = sorted(a for a in memory if not 0 <= a < 16 or a % 4)
if stray:
raise AssertionError(f"stores outside out[0..3]: {stray}")
return [memory.get(4 * k) for k in range(4)]
class Wave32LaneSelect(unittest.TestCase):
def test_lane_select_uses_bits_4_0(self):
self.assertEqual(run(translate()), EXPECTED)
def test_mask_63_breaks_lane_40_wrap(self):
# Negative control: put the wave64 mask back on the lane selects; lane 40 no longer wraps to lane 8
# (writelane(40) misses lane 8 and present-lane guarded readlane(40) redirects to lane 0 instead of lane 8).
spv = translate()
# every constant 31 (lower_addr64 declares its own for its shift masks, which stay as they are)
c31s = re.findall(r"^(%\S+) = OpConstant %uint 31$", spv, re.M)
lines = spv.splitlines()
sels = [i for i, ln in enumerate(lines)
if any(re.search(r"= OpBitwiseAnd %uint \S+ " + re.escape(c31) + "$", ln) for c31 in c31s)
and not ln.lstrip().startswith("%a64_")]
# Two non-spill reads on v2 and the write on v10 select a lane (plus wave_lane & 31).
self.assertGreaterEqual(len(sels), 3)
for i in sels:
lines[i] = re.sub(r"%\S+$", "%neg_c63", lines[i])
k = next(i for i, ln in enumerate(lines) if ln.startswith(c31s[0] + " = OpConstant"))
lines.insert(k + 1, "%neg_c63 = OpConstant %uint 63")
got = run("\n".join(lines) + "\n")
self.assertNotEqual(got, EXPECTED)
# v2 readlane(40) sees lane 40 as absent and redirects to lane 0 (= 1) instead of wrapping to lane 8 (= 25).
self.assertEqual(got[0], 1)
if __name__ == "__main__":
unittest.main()