mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 15:38:38 +00:00
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 maind937b7f: 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 main8df277f: since2e8101bthe 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>
46 lines
2.1 KiB
NASM
46 lines
2.1 KiB
NASM
; Raw MUBUF loads (stride 0) check every dword against NUM_RECORDS on its own: dword i of a load at byte offset
|
|
; O = inst_offset + soffset + vaddr is data when O + 4*i + 4 <= NUM_RECORDS, else 0. Measured on an AMD Radeon
|
|
; RX 6800 (gfx1030) as a raw compute kernel with V# word3 OOB_SELECT 3 (docs/design.md, Buffer bounds checking).
|
|
; Only dword-aligned offsets here: the unaligned rows of the measurement are in tests/semantic.
|
|
; Thread 0 seeds heap bytes 0x40..0x5f (region offset X = byte 0x40 + X) with 0x5eed0000 + X, then loads through
|
|
; V#s it builds itself (base 0x40, stride 0, word3 0x31016fac):
|
|
; buffer_load_dword v10, v5=0x10 offen, NR 0x12 -> 0x14 > 0x12: 0 (start-only check: 0x5eed0010)
|
|
; buffer_load_dword v11, v6=0xc offen, NR 0x12 -> 0x10 <= 0x12: 0x5eed000c
|
|
; buffer_load_dwordx4 v[12:15], v7=8 offen, NR 0x12 -> 8, 0xc in; 0x10, 0x14 out: data, data, 0, 0
|
|
; buffer_load_dwordx2 v[16:17], off, s20=4 offset:0xc, NR 0x17 -> O 0x10: 0x14 <= 0x17 data, 0x18 > 0x17: 0
|
|
; and writes v10..v17 to out[0..7].
|
|
; Run: vk_compute_test cs_buf_tail.spv 1 dump -> out[0..7] = 0x00000000 0x5eed000c 0x5eed0008 0x5eed000c
|
|
; 0x00000000 0x00000000 0x5eed0010 0x00000000 (0 1592590348 1592590344 1592590348 0 0 1592590352 0).
|
|
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, 0
|
|
s_mov_b32 s6, 0x12
|
|
s_mov_b32 s7, 0x31016fac
|
|
s_mov_b32 s8, 0x40
|
|
s_mov_b32 s9, 0
|
|
s_mov_b32 s10, 0x17
|
|
s_mov_b32 s11, 0x31016fac
|
|
s_mov_b32 s20, 4
|
|
v_mov_b32 v5, 0x10
|
|
v_mov_b32 v6, 0xc
|
|
v_mov_b32 v7, 8
|
|
buffer_load_dword v10, v5, s[4:7], 0 offen
|
|
buffer_load_dword v11, v6, s[4:7], 0 offen
|
|
buffer_load_dwordx4 v[12:15], v7, s[4:7], 0 offen
|
|
buffer_load_dwordx2 v[16:17], off, s[8:11], s20 offset:12
|
|
s_waitcnt vmcnt(0)
|
|
buffer_store_dwordx4 v[10:13], off, s[0:3], 0
|
|
buffer_store_dwordx4 v[14:17], off, s[0:3], 0 offset:16
|
|
s_endpgm
|