tests/semantic: PSB loads and stores in the mini interpreter are one aligned 32-bit word

spirv_mini_interp keeps PhysicalStorageBuffer memory as a flat {byte address: 32-bit word} map, but OpLoad and
OpStore on a PSB pointer used it for any type and address: an OpLoad %ulong gave only the low word, a float or
64-bit store wrote one non-word entry, and a load at an address that is not 4-byte aligned (undefined under the
translator's Aligned 4) read 0, or whatever a test seeded at that byte key. They now raise UnsupportedConstruct,
as the atomics on the same memory already did for non-32-bit types. The translator emits only %uint PSB accesses
on dword-aligned pointers, so no current result changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
VictorJulianiR
2026-11-21 22:41:59 -03:00
parent c432cdce12
commit 2e8101baa5
2 changed files with 64 additions and 4 deletions

View File

@ -13,7 +13,8 @@ Design rules (see tests/semantic/README.md):
branches the translator emits: OpBranch / OpBranchConditional to a later label plus OpPhi at the merge block.
Backward branches, loops, calls and switches are rejected. Function/Output/Input variables live in Python
boxes; PushConstant reads can be seeded with a word list; physical-storage-buffer loads/stores (and 32-bit
atomics, which with one invocation are read-combine-write) use a flat address -> word dictionary. Workgroup
atomics, which with one invocation are read-combine-write) use a flat address -> word dictionary, so they
must be 32-bit integers at 4-byte-aligned addresses (anything else raises). Workgroup
storage and images are rejected.
* A default run sees zero push constants, no inputs and empty memory, so tests that do not care about them
keep the old behaviour.
@ -396,6 +397,13 @@ def _invocation(text, inputs, push_constants, memory, step=False):
box, i = p
return box[i]
def psb_word(addr):
"""The flat memory holds one 32-bit word per 4-byte-aligned byte address. Another address is undefined
under the translator's `Aligned 4` and would read a key nobody wrote, so it raises."""
if addr % 4:
raise UnsupportedConstruct(f"PhysicalStorageBuffer access at {addr:#x}, not 4-byte aligned")
return addr
def wmask(type_id):
return (1 << types.bits(type_id)) - 1
@ -498,7 +506,9 @@ def _invocation(text, inputs, push_constants, memory, step=False):
w = p[1] // 4
values[res] = push_constants[w] if 0 <= w < len(push_constants) else 0
elif p[0] == "psb":
values[res] = memory.get(p[1], 0)
if types.t[a[0]][:2] != ("int", 32):
raise UnsupportedConstruct("PhysicalStorageBuffer load of a type that is not a 32-bit integer")
values[res] = memory.get(psb_word(p[1]), 0)
else:
v = deref(p)
values[res] = list(v) if isinstance(v, list) else v
@ -507,7 +517,11 @@ def _invocation(text, inputs, push_constants, memory, step=False):
if p[0] == "pc":
raise UnsupportedConstruct("store to PushConstant")
if p[0] == "psb":
memory[p[1]] = val(a[1])
v, t = val(a[1]), types.t.get(id_type.get(a[1]))
if isinstance(v, bool) or not isinstance(v, int) or v > MASK \
or (t is not None and t[:2] != ("int", 32)):
raise UnsupportedConstruct("PhysicalStorageBuffer store of a value that is not a 32-bit integer")
memory[psb_word(p[1])] = v
else:
p[0][p[1]] = val(a[1])
elif op in _ATOMICS:
@ -518,7 +532,7 @@ def _invocation(text, inputs, push_constants, memory, step=False):
raise UnsupportedConstruct(f"{op} outside PhysicalStorageBuffer")
if types.bits(a[0]) != 32:
raise UnsupportedConstruct(f"{op} on a non-32-bit integer")
old = memory.get(p[1], 0)
old = memory.get(psb_word(p[1]), 0)
if op == "OpAtomicCompareExchange":
new = val(a[5]) if old == val(a[6]) & MASK else old
else:

View File

@ -216,6 +216,52 @@ class InterpreterTests(unittest.TestCase):
"OpStore %out %res\n" + FOOTER)
self.assertEqual(si.run_fragment(text, memory={4100: 0x12345678})["%out"][0], 0x12345678)
def test_physical_storage_buffer_access_is_one_aligned_32bit_word(self):
"""The flat memory holds one 32-bit word per 4-byte-aligned address, so a PSB load or store of another
type, or at an address that is not 4-byte aligned (undefined under `Aligned 4`), must raise instead of
reading or writing one dictionary entry (a 64-bit load would give only its low word, a load at 4098 the
0 of a key nobody wrote)."""
text = HEADER.replace("OpMemoryModel Logical GLSL450",
'OpCapability PhysicalStorageBufferAddresses\n'
'OpExtension "SPV_KHR_physical_storage_buffer"\n'
"OpMemoryModel PhysicalStorageBuffer64 GLSL450")
decls = ("%ulong = OpTypeInt 64 0\n"
"%_ptr_PSB_uint = OpTypePointer PhysicalStorageBuffer %uint\n"
"%_ptr_PSB_ulong = OpTypePointer PhysicalStorageBuffer %ulong\n"
"%_ptr_PSB_float = OpTypePointer PhysicalStorageBuffer %float\n"
"%a4096 = OpConstant %ulong 4096\n%a4098 = OpConstant %ulong 4098\n"
"%cl_big = OpConstant %ulong 4294967296\n")
text = text.replace("%main = OpFunction", decls + "%main = OpFunction")
memory = {4096: 0x11223344, 4100: 0x55667788}
def run(body):
return si.run_fragment(text + body + "OpStore %out %res\n" + FOOTER, memory=dict(memory))
# Control: an aligned 32-bit load and store work.
self.assertEqual(run("%p = OpConvertUToPtr %_ptr_PSB_uint %a4096\n"
"%res = OpLoad %uint %p Aligned 4\n")["%out"][0], 0x11223344)
mem = dict(memory)
si.run_fragment(text + "%p = OpConvertUToPtr %_ptr_PSB_uint %a4096\nOpStore %p %c8 Aligned 4\n"
"%res = OpLoad %uint %p Aligned 4\nOpStore %out %res\n" + FOOTER, memory=mem)
self.assertEqual(mem, {4096: 8, 4100: 0x55667788})
cases = {
"unaligned load": "%p = OpConvertUToPtr %_ptr_PSB_uint %a4098\n%res = OpLoad %uint %p Aligned 4\n",
"unaligned store": "%p = OpConvertUToPtr %_ptr_PSB_uint %a4098\nOpStore %p %c8 Aligned 4\n"
"%res = OpCopyObject %uint %c0\n",
"64-bit load": "%p = OpConvertUToPtr %_ptr_PSB_ulong %a4096\n%w = OpLoad %ulong %p Aligned 8\n"
"%res = OpUConvert %uint %w\n",
"64-bit store": "%p = OpConvertUToPtr %_ptr_PSB_ulong %a4096\nOpStore %p %cl_big Aligned 8\n"
"%res = OpCopyObject %uint %c0\n",
"float load": "%p = OpConvertUToPtr %_ptr_PSB_float %a4096\n%x = OpLoad %float %p Aligned 4\n"
"%res = OpBitcast %uint %x\n",
"float store": "%p = OpConvertUToPtr %_ptr_PSB_float %a4096\nOpStore %p %f1 Aligned 4\n"
"%res = OpCopyObject %uint %c0\n",
}
for name, body in cases.items():
with self.subTest(name):
with self.assertRaises(si.UnsupportedConstruct):
run(body)
def test_sconvert_sign_extends_and_truncates(self):
"""OpSConvert: sign-extend from the operand's width, wrap to the result's (the global_* offset path)."""
text = HEADER.replace("OpCapability Shader", "OpCapability Shader\nOpCapability Int64")