From 5a77e32c143b928bd51b0bd897aac82c9dc2f990 Mon Sep 17 00:00:00 2001 From: avt0m4t <56218626+avt0m4t48@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:15:11 +0300 Subject: [PATCH] startup: explain formatted MUBUF descriptor refusals --- docs/experiments.md | 29 +++++++++++++++ tests/startup_buffer_address_check.py | 51 +++++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 5 +++ tools/startup/triage_run.py | 2 ++ 4 files changed, 87 insertions(+) diff --git a/docs/experiments.md b/docs/experiments.md index f3bb185135..151c3bc6d1 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -133,6 +133,35 @@ Four wrong references (omitting `voffset` or `soffset`) and four translation mut ctest --preset dev --no-tests=error -R '^startup_buffer_address_(controls|warp|hardware)$' -V ``` +### Descriptor format for startup MUBUF formatted loads + +For `buffer_load_format_{x,xy,xyz,xyzw}`, the RDNA 2 buffer resource descriptor +supplies both `FORMAT` (bits 18:12 of word 3) and `DST_SEL` (bits 11:0 of word 3). +The opcode determines how many components the instruction returns. An MTBUF +`tbuffer_load_format_*` encodes the format in the instruction and uses identity +selectors. [AMD RDNA 2 Shader ISA, §8.1.4 and §8.1.8](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) + +The startup graphics request contains the two shaders, the root signature and +an optional input layout. `gfx2hlsl` records a scalar load from a descriptor +table as a table index, byte offset and dword index. The descriptor words are +available to `draw_translated()` when it reads the bound table, after it has +loaded a desktop translation. The raw buffer binder uses the base address, +stride and record count from a native descriptor and passes no format or +selectors to the translated shader. A wrapped view token follows a separate +binding path. A formatted lowering needs the actual bound descriptor state +and a verified channel mapping, alignment and bounds policy for the forms it +accepts. + +The translator reports ` requires descriptor FORMAT and DST_SEL` +in the request's `.fail` file. The source-built control submits all four +`off` forms with a scalar descriptor-table load and checks the named refusal +and absence of a translation. The same table entry and address produce HLSL +for `buffer_load_dwordx3` and `tbuffer_load_format_xyz` as positive controls. + +```sh +python3 -B tests/startup_buffer_address_check.py controls +``` + ## Startup translator: SMEM combined register and immediate offsets In RDNA 2 SMEM instructions (`s_load_dword*` and `s_buffer_load_dword*`), a scalar register `SOFFSET` (`s0`..`s105` or `m0`) and a non-zero 21-bit signed immediate `OFFSET` (`w1[20:0]`) can be active at the same time: [`tools/disasm_rdna2.py`](../tools/disasm_rdna2.py) prints the register in `ops[2]` and `offset:0x...` in `mods`. Previously, `Stage.op_s_buffer_load_dword()` and `Stage.op_s_load_dwordx4()` in [`tools/startup/gfx2hlsl.py`](../tools/startup/gfx2hlsl.py) ignored `mods`, silently dropping `offset:0x...` on register-addressed buffer and dynamic sampler-heap loads, accepting unknown modifiers, reading negative 21-bit signed encodings (`0x100000`..`0x1fffff`) as `+1`..`2` MiB, and overwriting a runtime SGPR `soffset` mid-load when a multi-dword `s_buffer_load_dwordx*` wrote into that register first. diff --git a/tests/startup_buffer_address_check.py b/tests/startup_buffer_address_check.py index 9970604b16..ba5842f76c 100644 --- a/tests/startup_buffer_address_check.py +++ b/tests/startup_buffer_address_check.py @@ -22,11 +22,13 @@ import subprocess import sys import tempfile +from unittest.mock import patch HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE.parent / 'tools')) sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) import gfx2hlsl as G # noqa: E402 +from triage_run import classify_shader_reason # noqa: E402 SENTINEL = 0xc5c5c5c5 # offscreen_check --compute-store-case fills the raw t0 buffer with this 320-dword pattern. @@ -177,7 +179,56 @@ def old_buffer_address(stage, ops, mods): return buf, address +def formatted_mubuf_request_controls(): + # One SRV descriptor table at s[8:9]. The shader loads its second 16-byte entry. + rts0 = b''.join(( + struct.pack('<6I', 2, 1, 24, 0, 0, 0), # Root signature header + struct.pack('<3I', 0, 0, 36), # Descriptor-table parameter + struct.pack('<2I', 1, 44), # Descriptor table + struct.pack('<6I', 0, 2, 0, 0, 0, 0), # SRV range with two entries + )) + part = b'RTS0' + struct.pack('> 16) & 255) / 255.0);'), + ): + result, name = request(load) + assert result == 'ok', result + assert 'srv 0 buf table 0 16\n' in name.with_suffix('.txt').read_text() + assert hlsl in name.with_suffix('.vs.hlsl').read_text(), hlsl + + formats = (('x', 'v6'), ('xy', 'v[6:7]'), ('xyz', 'v[6:8]'), ('xyzw', 'v[6:9]')) + for op, (suffix, dest) in enumerate(formats): + load = encode_mubuf(op, vdata=6, srsrc=32) + mnemonic = 'buffer_load_format_' + suffix + assert G.decode(load) == [(0, 8, mnemonic, f'{mnemonic} {dest}, off, s[32:35], 0')] + result, name = request(load) + reason = name.with_suffix('.fail').read_text().strip() + assert reason == f'{mnemonic} requires descriptor FORMAT and DST_SEL', reason + assert result == f'unsupported: {reason}', result + assert classify_shader_reason(reason) == 'buffer_* instruction', reason + assert set(folder.glob(name.name + '.*')) == {name.with_suffix('.fail')} + + def controls(): + formatted_mubuf_request_controls() + # 1. Existing single-mode forms keep their exact HLSL output. asm, st = translate_bytes(encode_mubuf(imm=12, soffset=128 + 16)) assert asm == ['buffer_load_dword v4, off, s[16:19], 16 offset:12'], asm diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 8695b6c1f7..aad10dc600 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -583,6 +583,11 @@ def one(self, name, asm): return self.set_v(reg_range(ops[0])[1], text.format(*[self.u(o) for o in ops[1:1 + arity]])) if base in SCA: return self.scalar(base, ops) + if base in ('buffer_load_format_x', 'buffer_load_format_xy', + 'buffer_load_format_xyz', 'buffer_load_format_xyzw'): + # MUBUF formatted loads take both fields from V# word 3. The startup request carries + # the root signature layout, while the descriptor table is supplied at draw time. + raise Unsupported('%s requires descriptor FORMAT and DST_SEL' % base) raise Unsupported(base) # VOP1/VOP2 with sub-dword addressing: select the source fields, run the same operation, then place diff --git a/tools/startup/triage_run.py b/tools/startup/triage_run.py index ba6b9d4236..2f74d30ab4 100644 --- a/tools/startup/triage_run.py +++ b/tools/startup/triage_run.py @@ -65,6 +65,8 @@ MNEMONIC_FAMILY = r"(v|s|ds|buffer|tbuffer|image|flat|global|scratch|exp|lds)_[a-z0-9_]{1,40}" SHADER_REASONS: list[tuple[re.Pattern[str], str]] = [ (re.compile(r"(image_load_mip) (tfe|a16)\Z"), r"\1 \2"), + (re.compile(r"buffer_load_format_(?:x|xy|xyz|xyzw) requires descriptor FORMAT and DST_SEL\Z"), + "buffer_* instruction"), (re.compile(r"SDWA UNUSED_SEXT\Z"), "SDWA UNUSED_SEXT"), (re.compile(r"SDWA dst_unused\b"), "SDWA dst_unused"), (re.compile(r"undecodable word\b"), "undecodable word"),