Files
mandy/runtime/test/shaders/cs_smem_soffset_imm.asm
shelbykb2 49dd92d41d translator: SMEM loads with soffset and imm add both (the imm was dropped)
What was wrong
An RDNA2 SMEM load can carry a register offset and an immediate at once.
llvm-mc prints that form as "s_buffer_load_dword s12, s[24:27], s40 offset:0x4".
shader_ir.parse_line puts "offset:0x4" in mods, and Gen.scalar_load only read
the third operand, so the immediate was dropped without a diagnostic: the line
translated to the same SPIR-V with and without the modifier, and spirv-val
accepted both.

Evidence
Measured on an AMD Radeon RX 6800 (gfx1030) by running the instructions as a
raw compute kernel: for s_buffer_load_dword/x2/x4/x8 the address is
base + soffset + imm + 4*i, e.g. soffset 8 + imm 4 reads the dword at +12.
s_load_dword* with both offsets was not measured; it shares the SMEM encoding
(soffset_en + imm), so giving it the same rule is an inference.

Change
- scalar_load adds the immediate when the third operand is a register and mods
  has offset:N. The no-modifier forms emit the same code as before.
- Forms llvm-mc does not accept (immediate operand plus offset:, two offset:
  modifiers, a fourth operand, an unparseable offset) raise Unsupported.
- Not changed, and listed in docs/design.md as known differences: the hardware
  truncates base, soffset and imm each to a multiple of 4 and s_buffer_load
  returns 0 past NUM_RECORDS; the translator does neither. A negative s_load
  immediate is still zero-extended from 32 bits, in both forms (not measured).

Tests
- tests/semantic/test_scalar_mem_smem.py: SmemSoffsetImmTranslatorTests runs
  the code scalar_load emits against concrete SGPRs and memory and asserts the
  addresses and values read (9 tests; 7 fail on main, where the loads read +8
  instead of +12, and the imm-only / soffset-only regression cases pass).
- runtime/test/shaders/cs_smem_soffset_imm.asm, wired into check.py and
  scripts/run_gpu_tests.py. Not run on a GPU in this change.
- unittest discover -s tests/semantic: 335 -> 344, all pass.
- pytest tests (--capture=sys, llvm-mc through WSL): 883 -> 893 passed, the
  same 6 test_rtti_scan failures before and after (pefile not installed).
- The 27 hand-written GPU shaders: 27 translated, 27 pass spirv-val before and
  after, output byte-identical.

AI-assisted.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 13:28:03 -07:00

62 lines
2.6 KiB
NASM

; SMEM loads that carry a register offset AND an immediate (llvm-mc: "sOFF offset:IMM"): address = base + soffset + imm.
; Measured on an AMD Radeon RX 6800 (gfx1030) by running s_buffer_load_dword/x2/x4/x8 as a raw compute kernel:
; soffset 8 + imm 4 reads the dword at +12. s_load_dword shares the SMEM encoding; applying the rule to it is an inference.
; Thread 0 seeds heap bytes 0x10..0x30 with 0x5eed0000 + byte offset, then loads with s8 = 0x10:
; s_buffer_load_dword s9, s[0:3], s8 offset:0x14 -> byte 0x24 (imm dropped: 0x10, s8 dropped: 0x14)
; s_load_dword s10, s[4:5], s8 offset:0x18 -> byte 0x28 (imm dropped: 0x10, s8 dropped: 0x18)
; s_buffer_load_dwordx2 s[12:13], s[0:3], s8 offset:0x1c -> bytes 0x2c, 0x30 (imm dropped: 0x10, 0x14; s8 dropped: 0x1c, 0x20)
; and writes s9, s10, s12, s13 to out[0..3]; out[4..7] keep the seeds of bytes 0x10..0x1c.
; All offsets are multiples of 4 and inside the buffer, so alignment and num_records play no part.
; Run: vk_compute_test cs_smem_soffset_imm.spv 1 dump -> out[0..7] = 0x5eed0024 0x5eed0028 0x5eed002c 0x5eed0030
; 0x5eed0010 0x5eed0014 0x5eed0018 0x5eed001c (1592590372 1592590376 1592590380 1592590384 1592590352 1592590356
; 1592590360 1592590364). Same FSSM header as cs_lds.
v_cmpx_eq_u32_e32 0, v0
v_mov_b32 v1, 0x5eed0010
v_mov_b32 v2, 0x10
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed0014
v_mov_b32 v2, 0x14
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed0018
v_mov_b32 v2, 0x18
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed001c
v_mov_b32 v2, 0x1c
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed0020
v_mov_b32 v2, 0x20
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed0024
v_mov_b32 v2, 0x24
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed0028
v_mov_b32 v2, 0x28
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed002c
v_mov_b32 v2, 0x2c
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, 0x5eed0030
v_mov_b32 v2, 0x30
buffer_store_dword v1, v2, s[0:3], 0 offen
s_waitcnt vmcnt(0)
s_mov_b32 s4, 0
s_mov_b32 s5, 0
s_mov_b32 s8, 0x10
s_buffer_load_dword s9, s[0:3], s8 offset:0x14
s_load_dword s10, s[4:5], s8 offset:0x18
s_buffer_load_dwordx2 s[12:13], s[0:3], s8 offset:0x1c
s_waitcnt lgkmcnt(0)
v_mov_b32 v1, s9
v_mov_b32 v2, 0
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, s10
v_mov_b32 v2, 4
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, s12
v_mov_b32 v2, 8
buffer_store_dword v1, v2, s[0:3], 0 offen
v_mov_b32 v1, s13
v_mov_b32 v2, 12
buffer_store_dword v1, v2, s[0:3], 0 offen
s_endpgm