Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<mnemonic> 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.
Expand Down
51 changes: 51 additions & 0 deletions tests/startup_buffer_address_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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('<I', len(rts0)) + rts0
root = b'DXBC' + bytes(16) + struct.pack('<4I', 1, 36 + len(part), 1, 36) + part
load_descriptor = struct.pack('<3I', 0xf4080804, 0xfa000010, 0xbf8cc07f) # s_load_dwordx4; lgkmcnt(0)
export_position = struct.pack('<4I', 0xbf8c3f70, 0xf80008cf, 0x06060606, 0xbf810000) # vmcnt(0); exp; end
ps = struct.pack('<3I', 0xf800180f, 0, 0xbf810000)

with tempfile.TemporaryDirectory() as temp, patch.dict(os.environ, {'X2W_FXC': ''}):
folder = Path(temp)

def request(load):
vs = load_descriptor + load + export_position
key = G.pipeline_key(vs, ps, root)
for suffix, data in (('a', vs), ('b', ps), ('root', root)):
(folder / f'req_{key:016x}.{suffix}').write_bytes(data)
return G.translate_request(temp, key), folder / f'{key:016x}'

# With the same descriptor and address, adjacent raw and MTBUF loads can publish a translation.
for load, hlsl in (
(encode_mubuf(0x0f, vdata=6, srsrc=32), 'v8 = r0.Load(a + 8);'),
(encode_mtbuf(0x02, vdata=6, srsrc=32), 'v8 = U(((w >> 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
Expand Down
5 changes: 5 additions & 0 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tools/startup/triage_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading