Files
mandy/runtime/test/shaders/cs_smem_stride_align.asm
shelbykb2 87d7bf7c43 Translator: SMEM alignment, s_buffer_load NUM_RECORDS bound, signed s_load imm, raw MUBUF per-dword tail
What was wrong (tools/rdna2spirv.py):
- s_load* / s_buffer_load* added base + soffset + imm unaligned; hardware truncates each of the three to a
  multiple of 4 before adding them.
- s_buffer_load* had no bound at all and read past NUM_RECORDS.
- s_load* zero-extended a negative immediate from 32 bits (soffset 8 + imm -4 read ptr + 4 GiB + 4).
- raw buffer_load_dword* checked only the start (offset < num_records), so a dword at NR-1..NR-3 and the tail
  of a multi-dword load returned data where hardware returns 0.

Hardware evidence: AMD Radeon RX 6800 (gfx1030, driver 32.0.21045.29003), raw compute kernels, wave32 and
wave64, 155 runs, every result 0 or the stored data, wave32 = wave64 in every dword. Decisive rows:
s_buffer_load imm 0x10 with NR 18 -> 0; imm 0x13 with NR 21 -> data@0x10; soffset 0xe + imm 2 with NR 16 ->
data@0xc; x4 at 0xc with NR 21 -> data, data, 0, 0; stride 4 with NR 5: 0x10 data, 0x14 0; stride 1 with NR 17:
0x10 -> 0; base & 3 = 1 with imm 3 -> data@0. s_load soffset 8 + imm -4 -> @4, soffset 0xb + imm -1 -> @4.
buffer_load_dword at 0xf with NR 18 -> 0; x4 at 3 with NR 18 -> d0..d2 data, d3 0.

Now: off = (soff & ~3) + (imm & ~3), dword i at (base & ~3) + off + 4i. s_buffer_load dword i is 0 and not
loaded (its own selection construct + OpPhi) when off + 4i + 4 > limit, limit = NR * stride or NR (stride 0).
s_load: imm sign-extended from 21 bits, 64-bit sum, no bound. Raw (stride 0, no idxen) buffer_load: dword i
loaded iff O + 4i + 4 <= NR. Structured, idxen, stores, atomics and format loads keep their old first-byte
check (not measured).

Inferred, not measured: s_load with soffset + imm < 0 (64-bit signed sum), s_buffer_load x16 and buffer_load
x3 (same rule), an s_buffer_load immediate with bit 20 set or negative. OOB_SELECT (V# word3[29:28]) changes
vector-load results on hardware (2 probes) and is recorded in docs/design.md, not modelled.

Tests: tests/semantic SmemHardwareRuleTests (evaluator follows branches/phis), RawBufferTailBounds and
MeasuredBoundsShaders assert values and the exact addresses read; 19 fail with main's translator. New GPU
tests cs_smem_bounds, cs_smem_stride_align, cs_smem_sload_neg, cs_buf_tail (expected words derived by hand
from the model) in check.py and scripts/run_gpu_tests.py. test_heap_base_is_added_once now gives its V# a
NUM_RECORDS (it had 0, which the measured bound turns into a load of nothing).

Tested on AMD Radeon RX 6800 / Windows / driver 26.11.2 (Vulkan), on main d937b7f: run_gpu_tests 62/62 passed;
check.py 56 of 56; unittest tests/semantic 460 OK; pytest 1796 passed, 7 skipped, 1 xfailed. The three new tests
that are safe to run with main's translator fail with it on the GPU; cs_smem_sload_neg was not run with it (it
would read 4 GiB past the heap). Indexed (idxen) buffer loads keep their single selection construct.

AI-assisted.

Rebased onto main 8df277f: since 2e8101b the semantic mini interpreter raises on a PhysicalStorageBuffer access that
is not 4-byte aligned, so RawBufferTailBounds rows that load at an unaligned O now set base_low so that base + O is
aligned (the bound sees only O, so the measured O / NR pairs are kept); the base & 3 = 3 row uses offset 5 as a
stand-in for the measured offset 3. Byte-exact data at an unaligned address is no longer asserted there.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 22:49:26 -07:00

67 lines
2.7 KiB
NASM

; s_buffer_load with a stride (limit = NUM_RECORDS * stride bytes) and SMEM address truncation: base, soffset and
; imm are each truncated to a multiple of 4 before they are added. Rules measured on an AMD Radeon RX 6800 (gfx1030)
; as a raw compute kernel (docs/design.md, Scalar/buffer memory).
; Thread 0 seeds heap bytes 0x40..0x5f (region offset X = byte 0x40 + X) with 0x5eed0000 + X, then:
; s_buffer_load_dword s24, stride 4, NR 5, 0x10 -> limit 20, 0x14 <= 20: 0x5eed0010 (NR as bytes would give 0)
; s_buffer_load_dword s25, stride 4, NR 5, 0x14 -> 0x18 > 20: 0
; s_buffer_load_dword s26, stride 1, NR 17, 0x10 -> limit 17, 0x14 > 17: 0 (index 16 < 17 would read)
; s_buffer_load_dword s27, base 0x41, 0x3 -> (0x41 & ~3) + 0: 0x5eed0000 (unaligned sum 0x44: 0x5eed0004)
; s_load_dword s28, ptr 0x41, 0x1 -> 0x40: 0x5eed0000
; s_buffer_load_dword s29, base 0x41, s20=3 offset:0x3 -> 0 + 0: 0x5eed0000 (sum 6 truncated would be 4)
; s_load_dword s30, ptr 0x40, s20=3 offset:0x3 -> 0x5eed0000
; s_buffer_load_dword s31, base 0x41, s21=2 offset:0x6 -> 0 + 4: 0x5eed0004 (sum 8 would be 8)
; (V#s with base 0x41 have NR 0x100, word3 0x31016fac = OOB_SELECT 3 everywhere) and writes s24..s31 to out[0..7].
; Run: vk_compute_test cs_smem_stride_align.spv 1 dump -> out[0..7] = 0x5eed0010 0x00000000 0x00000000 0x5eed0000
; 0x5eed0000 0x5eed0000 0x5eed0000 0x5eed0004 (1592590352 0 0 1592590336 1592590336 1592590336 1592590336
; 1592590340).
v_cmpx_eq_u32_e32 0, v0
v_mov_b32 v1, 0x5eed0000
v_mov_b32 v2, 0x5eed0004
v_mov_b32 v3, 0x5eed0008
v_mov_b32 v4, 0x5eed000c
buffer_store_dwordx4 v[1:4], off, s[0:3], 0 offset:64
v_mov_b32 v1, 0x5eed0010
v_mov_b32 v2, 0x5eed0014
v_mov_b32 v3, 0x5eed0018
v_mov_b32 v4, 0x5eed001c
buffer_store_dwordx4 v[1:4], off, s[0:3], 0 offset:80
s_waitcnt vmcnt(0)
s_mov_b32 s4, 0x40
s_mov_b32 s5, 0x40000
s_mov_b32 s6, 5
s_mov_b32 s7, 0x31016fac
s_mov_b32 s8, 0x40
s_mov_b32 s9, 0x10000
s_mov_b32 s10, 17
s_mov_b32 s11, 0x31016fac
s_mov_b32 s12, 0x41
s_mov_b32 s13, 0
s_mov_b32 s14, 0x100
s_mov_b32 s15, 0x31016fac
s_mov_b32 s16, 0x41
s_mov_b32 s17, 0
s_mov_b32 s18, 0x40
s_mov_b32 s19, 0
s_mov_b32 s20, 3
s_mov_b32 s21, 2
s_buffer_load_dword s24, s[4:7], 0x10
s_buffer_load_dword s25, s[4:7], 0x14
s_buffer_load_dword s26, s[8:11], 0x10
s_buffer_load_dword s27, s[12:15], 0x3
s_load_dword s28, s[16:17], 0x1
s_buffer_load_dword s29, s[12:15], s20 offset:0x3
s_load_dword s30, s[18:19], s20 offset:0x3
s_buffer_load_dword s31, s[12:15], s21 offset:0x6
s_waitcnt lgkmcnt(0)
v_mov_b32 v1, s24
v_mov_b32 v2, s25
v_mov_b32 v3, s26
v_mov_b32 v4, s27
buffer_store_dwordx4 v[1:4], off, s[0:3], 0
v_mov_b32 v1, s28
v_mov_b32 v2, s29
v_mov_b32 v3, s30
v_mov_b32 v4, s31
buffer_store_dwordx4 v[1:4], off, s[0:3], 0 offset:16
s_endpgm