Files
mandy/docs/buffer_bounds.md
AlxcBTW 14887e6828 Translator: honour V# OOB_SELECT in MUBUF/MTBUF bounds checks
buffer_address() picked the bounds check from the stride alone and never read
word3. It now reads OOB_SELECT (bits 29:28) and applies the rule measured
natively on RDNA2 (gfx1036, docs/buffer_bounds.md; Mesa's GFX11 column): a
dword is out of range in mode 0 at idx >= NR or off + 4 > stride, mode 1 at
idx >= NR, mode 2 at NR == 0, mode 3 (RAW) at offs + 4 > NR, where off is
inst_offset + vaddr and offs adds soffset. A literal word3 folds the mode, a
run-time one selects with OpSelect.

Builds on the raw tail rule of #443 (per-dword loads): x2..x4 loads AND stores
are per dword in modes 0 and 3 (a store that straddled the edge used to write
past it), modes 1 and 2 take the whole access, a typed element
(buffer_load/store_format) is all or nothing at its format's size, an atomic
checks its dword's end. Measured on a Radeon 880M (gfx1150), a valid proxy for
RDNA2: 504 buffer_load_dword cases over idxen/offen/inst_offset/soffset at the
stride and record edges agree with the rules, plus the x2/x4, typed and atomic
edges.

Swizzle (V# word1 bit 31 SWIZZLE_ENABLE, word3 bit 23 ADD_TID_ENABLE; Mesa
gfx10-rsrc.json lines 326 and 337): a literal V# with either is Unsupported in
every mode; a run-time one is translated unswizzled (documented known gap).

A V# with word3 = 0 is mode 0 / stride 0 and so always out of range, as on
hardware: the harness output V# (vk_compute_test, interpreter push constants),
cs_bounds, cs_fmt_1010102, cs_store, mem_diff and the format tests now say
OOB_SELECT 3 (or 1 with a stride) explicitly.

Tests: tests/semantic/test_buffer_oob_select.py (33 hand-tabulated cases plus
multi-dword stores, typed elements and the swizzle gate, literal and run-time
V#), GPU tests cs_oob_select.asm and cs_oob_tail.asm (the second tells the end
check from Mesa's GFX10 start check). docs/design.md, Buffer bounds checking,
has the per-mode rule; docs/buffer_bounds.md notes the translator reads it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 07:40:20 +01:00

52 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Buffer bounds: V# `OOB_SELECT`, measured on hardware
Which buffer accesses the hardware treats as out of range, per the V# `OOB_SELECT` field (word 3 bits [29:28]),
measured natively on AMD GPUs (SHADER-116). The translator reads this field and picks the bounds check by mode
(SHADER-110); the contract row is "Buffer bounds checking" in
[design.md](design.md). Out of range, a load returns 0 and a store or atomic does not touch memory.
## Measured rules
`N` is `NUM_RECORDS` (V# word 2), `index` the VGPR index (`idxen`), `offset` the VGPR byte offset (`offen`) and
`soffset` the scalar offset. Every access below is a 4-byte `buffer_load_dword` / `buffer_store_dword`.
| `OOB_SELECT` | Out of range on RDNA2 (gfx1036) when | Mesa GFX10 column | Mesa GFX11 column |
|---|---|---|---|
| 0 structured with offset | `index >= N` or `offset + 4 > stride` | `offset >= stride`: differs | same |
| 1 structured | `index >= N` | same | same |
| 2 disabled | `N == 0` | same | same |
| 3 raw | `offset + soffset + 4 > N`; `index * stride` moves the address but is not checked | `offset >= N`: differs | `offset + 4 > N`, without `soffset` |
- **RDNA2 follows Mesa's GFX11 column**, not the GFX10 one: of 732 cases, 729 match GFX11 and 696 match GFX10.
Every GFX10 miss is an access that only partly fits, for example a raw buffer with `N = 3` read at offset 0.
- **In raw mode `soffset` is part of the checked offset.** The 3 cases that match neither column are raw reads at
offset 8 with `soffset` 8 and `N = 18`: 8 + 8 + 4 > 18, out of range on hardware.
- Loads and stores agree in every case.
- In-range accesses at a dword-aligned address read and wrote the expected dword (0 exceptions). An address that is
not a multiple of 4 (stride 18 with `index > 0`) is rounded by the hardware; where such an access lands was not
part of this measurement.
- @AlxcBTW measured RDNA3.5 (Radeon 880M, gfx1150) through the HIP runtime and found it matches the GFX11 column
([finding](https://github.com/ItsNotRexed/Leonida/issues/374#issuecomment-7072793165)). The RDNA2 result here
agrees with it, plus the `soffset` rule.
**Open:** the console target is gfx1013 (GFX10.1); gfx1036 is GFX10.3. Mesa uses one GFX10 column for both, and
this measurement shows that column is wrong for GFX10.3. Whether GFX10.1 matches GFX10.3 here needs an RDNA1 card
(gfx1010–1012, RX 5000 series).
## How it was measured
Ryzen 7 9800X3D integrated GPU (gfx1036, RDNA2), AMD driver 3679.0 (PAL, LC), Windows 11. A standalone OpenCL
host (not in this repository) loads the driver's `OpenCL.dll`, and each case is a one-lane kernel whose inline
assembly runs a single `buffer_load_dword` or `buffer_store_dword` with a V# built by hand:
- word 0–1: the buffer's address, `STRIDE` in word 1 [29:16];
- word 2: `NUM_RECORDS`;
- word 3: `DST_SEL` x/y/z/w, `FORMAT` = GFX10 `32_FLOAT` (22), `RESOURCE_LEVEL` = 1, `OOB_SELECT` = 0–3.
The buffer holds a pattern (dword `i` = `0xA0000000 | i`), so a non-zero load names the dword it read, and a store
writes a marker whose position is found by comparing the whole buffer afterwards. The cases are modes 0–3 × stride
0, 16, 18 × `N` 0, 3, 18, 48 × `offen` (offsets 0, 12, 16, 44, 48), `idxen` (indices 0, 2, 3, 4) and
`idxen offen` (six index/offset pairs), plus raw cases with `soffset` 8: 732 in all, each a load and a store.
Three runs gave identical results. Each case was compared with both Mesa columns as listed in @AlxcBTW's finding
(Mesa `src/amd/common/ac_descriptors.c`).