rdna2spirv: form MUBUF byte offsets in 64 bits (SHADER-MUBUF-OFFSET-WRAP) #1068

Closed
github-bot wants to merge 1 commit from gh-pr/1068/head into gh-pr/1068/base
Owner

@⁠Moriz82 on GitHub · 2026-11-24 17:40 UTC · original
split/shader-mubuf-offset-wrap → main · +212 −26 · 4 files · 1 commits · closed without merging

Claimed in the coordination issue as: SHADER-MUBUF-OFFSET-WRAP (claim).

Part of the split of #984 (first child). Depends on: none. Overlaps: none found (searched open and merged pull requests for buffer_address, stride, index and offset wrap; #513 changes the CBV's V# stride in the runtime, not the translator's address arithmetic; #1021 edits docs/design.md near the swizzled-buffer row).

What and why

Gen.buffer_address in tools/rdna2spirv.py forms the byte offset of every MUBUF/MTBUF access, off + soffset + index * stride (and the swizzled form), in 32 bits and adds it to the 48-bit V# base. The hardware does not wrap there: the RDNA2 ISA (8.1.5) gives the address as base + stride * index + offset with a 48-bit base, and only off (vaddr + inst_offset) wraps at 32 bits. So an index of 0xFFFFFFFF with a 20-byte stride addresses base + 0x13_FFFF_FFEC on the console, while the translated shader turns it into base − 20:

  • OOB_SELECT 3 (raw) checks offset + soffset against NUM_RECORDS and does not look at the index;
  • OOB_SELECT 2 (disabled) checks only that the buffer is not empty.

Both let the wrapped access through, so a store writes 20 bytes below the buffer and a load reads them. When the view is one slice of a larger pool that is mapped as a whole, that is a silent write into a neighbour's records.

The change forms the linear and the swizzled offset as a 64-bit value and adds that to the base. Nothing else changes: each mode checks exactly what it checked before, and a literal V# and a run-time V# take the same path.

What changed since the first version

The first version also treated every offset above 0xFFFFFFFF as out of range. The reviews showed two faults in that, and both are fixed by removing the bound instead of patching it:

  • Mode 2 has no such limit. An access 5 GiB past the base that its mode allows was dropped. It is now made at its 64-bit address, and that case is a test.
  • A literal mode 0 or 1 V# skipped the check that a run-time V# took, so folding the descriptor changed the result. There is no check left to skip, and the matched literal and run-time cases are a test.

What this does not do

  • It does not make a wild index safe. Index −1 in mode 2 or 3 is no longer the 20 bytes below the buffer, and it is not dropped either: it addresses tens of GiB past the base. What the console does with such an address when nothing is mapped there is not measured. On Vulkan an access outside the buffer is undefined. The runtime's GPU address arena is at most 4095 MiB (docs/d3d12_vulkan.md), so there an offset of 4 GiB or more always leaves the arena. Whether the runtime should refuse an address outside the arena is a separate decision, with the arena size as the bound and not 2^32. It is not part of this pull request.
  • The non-wrapping product is five-star-recomp's hardware observation (ThatRecompDev, GPL-3, commit b9b57e2), credited in the source. It was not measured by us on AMD hardware; the contract row and docs/buffer_bounds.md say so. Someone with the tools/hw_oracle setup could pin it with one probe (raw view, stride 20, index −1).

Cost: two or three 64-bit conversions and adds and one 64-bit multiply per access, now for every access including folded structured views (the first version left those alone). On a sample of 200 compute programs of a retail library (translated locally with --wave64-emulate, not included) 114 modules change and the 200 grow by 0.45 % together; all 200 pass spirv-val before and after.

Tested

Linux (CachyOS, kernel 7.3.5), Python 3.14, LLVM llvm-mc 23.1.5 and SPIRV-Tools v2026.4 from the distribution. GPU suite on an NVIDIA GeForce RTX 4070, driver 620.73.04.

  • python3 -I tests/semantic/test_buffer_offset_wrap.py: 8 tests pass. With the translator of the base commit the same file has 13 failing subtests: index −1 in raw and disabled mode with a literal and a run-time V# (4), the mapped 5 GiB offset in disabled mode (2), a structured index below NUM_RECORDS whose product passes 4 GiB in modes 0 and 1 (4), a swizzled offset past 4 GiB with a literal and a run-time V# (2), and off 0xFFFFFFF0 + soffset 0x20 (1). The swizzled control below 4 GiB passes on both. The swizzled cases were added after a review noted that the swizzled path had moved to 64 bits without a case above 4 GiB.
  • python3 -I scripts/run_semantic_tests.py tests/semantic: 471 files, 6,176 tests, 0 files failed.
  • The standard-library and pytest files under tests/ the way ci.yml runs them: 520 files, 519 pass. tests/test_imhex_patterns.py fails (6 of its 9 tests) and fails the same way on the base commit, so it is not from this change.
  • python3 -I scripts/check_no_game_data.py: passes.
  • python3 -I scripts/run_gpu_tests.py (run on the head before the swizzled tests were added; the translator has not changed since): 261 shaders translate and pass spirv-val; 202 of 252 tests pass, 49 are skipped and 1 fails (cs_imgstore_cube). The run with the base commit's translator gives the same 202, 49 and 1, test for test. The harness programs came from an older native build on this machine (2026-11-23) and were not rebuilt for this, which is the likely reason for that one failure; it is not a result of this change.

Not run by us: native Windows, macOS, an AMD GPU, the tools/hw_oracle probe. A reviewer ran the GPU suite on an AMD RX 9070 XT (236 of 252 pass, 16 skipped, 0 failed) on e01626be9, which has the same translator as this head.

GPUs tested: NVIDIA GeForce RTX 4070 (translation only; see above for what is not measured on hardware).

AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named.

Checklist

  • Based on upstream main 32950551e and merges cleanly into current main.
  • One topic.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • The rule is in the contract table of docs/design.md ("Buffer bounds checking"), with how it was confirmed and what is not measured.

🤖 Generated with Claude Code

**@⁠Moriz82** on GitHub · 2026-11-24 17:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068) `split/shader-mubuf-offset-wrap` → `main` · +212 −26 · 4 files · 1 commits · closed without merging Claimed in the coordination issue as: SHADER-MUBUF-OFFSET-WRAP ([claim](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14)). Part of the split of #984 (first child). Depends on: none. Overlaps: none found (searched open and merged pull requests for `buffer_address`, stride, index and offset wrap; #513 changes the CBV's V# stride in the runtime, not the translator's address arithmetic; #1021 edits `docs/design.md` near the swizzled-buffer row). ### What and why `Gen.buffer_address` in `tools/rdna2spirv.py` forms the byte offset of every MUBUF/MTBUF access, `off + soffset + index * stride` (and the swizzled form), in 32 bits and adds it to the 48-bit V# base. The hardware does not wrap there: the RDNA2 ISA (8.1.5) gives the address as base + stride * index + offset with a 48-bit base, and only `off` (vaddr + inst_offset) wraps at 32 bits. So an index of `0xFFFFFFFF` with a 20-byte stride addresses base + 0x13_FFFF_FFEC on the console, while the translated shader turns it into **base − 20**: - `OOB_SELECT 3` (raw) checks `offset + soffset` against `NUM_RECORDS` and does not look at the index; - `OOB_SELECT 2` (disabled) checks only that the buffer is not empty. Both let the wrapped access through, so a store writes 20 bytes below the buffer and a load reads them. When the view is one slice of a larger pool that is mapped as a whole, that is a silent write into a neighbour's records. The change forms the linear and the swizzled offset as a 64-bit value and adds that to the base. Nothing else changes: each mode checks exactly what it checked before, and a literal V# and a run-time V# take the same path. ### What changed since the first version The first version also treated every offset above `0xFFFFFFFF` as out of range. The reviews showed two faults in that, and both are fixed by removing the bound instead of patching it: - Mode 2 has no such limit. An access 5 GiB past the base that its mode allows was dropped. It is now made at its 64-bit address, and that case is a test. - A literal mode 0 or 1 V# skipped the check that a run-time V# took, so folding the descriptor changed the result. There is no check left to skip, and the matched literal and run-time cases are a test. ### What this does not do - It does not make a wild index safe. Index −1 in mode 2 or 3 is no longer the 20 bytes below the buffer, and it is not dropped either: it addresses tens of GiB past the base. What the console does with such an address when nothing is mapped there is **not measured**. On Vulkan an access outside the buffer is undefined. The runtime's GPU address arena is at most 4095 MiB (`docs/d3d12_vulkan.md`), so there an offset of 4 GiB or more always leaves the arena. Whether the runtime should refuse an address outside the arena is a separate decision, with the arena size as the bound and not 2^32. It is not part of this pull request. - The non-wrapping product is five-star-recomp's hardware observation (ThatRecompDev, GPL-3, commit `b9b57e2`), credited in the source. It was **not** measured by us on AMD hardware; the contract row and `docs/buffer_bounds.md` say so. Someone with the `tools/hw_oracle` setup could pin it with one probe (raw view, stride 20, index −1). Cost: two or three 64-bit conversions and adds and one 64-bit multiply per access, now for every access including folded structured views (the first version left those alone). On a sample of 200 compute programs of a retail library (translated locally with `--wave64-emulate`, not included) 114 modules change and the 200 grow by 0.45 % together; all 200 pass `spirv-val` before and after. ### Tested Linux (CachyOS, kernel 7.3.5), Python 3.14, LLVM `llvm-mc` 23.1.5 and SPIRV-Tools v2026.4 from the distribution. GPU suite on an NVIDIA GeForce RTX 4070, driver 620.73.04. - `python3 -I tests/semantic/test_buffer_offset_wrap.py`: 8 tests pass. With the translator of the base commit the same file has **13 failing subtests**: index −1 in raw and disabled mode with a literal and a run-time V# (4), the mapped 5 GiB offset in disabled mode (2), a structured index below `NUM_RECORDS` whose product passes 4 GiB in modes 0 and 1 (4), a swizzled offset past 4 GiB with a literal and a run-time V# (2), and `off 0xFFFFFFF0 + soffset 0x20` (1). The swizzled control below 4 GiB passes on both. The swizzled cases were added after a review noted that the swizzled path had moved to 64 bits without a case above 4 GiB. - `python3 -I scripts/run_semantic_tests.py tests/semantic`: 471 files, 6,176 tests, 0 files failed. - The standard-library and pytest files under `tests/` the way `ci.yml` runs them: 520 files, 519 pass. `tests/test_imhex_patterns.py` fails (6 of its 9 tests) and fails the same way on the base commit, so it is not from this change. - `python3 -I scripts/check_no_game_data.py`: passes. - `python3 -I scripts/run_gpu_tests.py` (run on the head before the swizzled tests were added; the translator has not changed since): 261 shaders translate and pass `spirv-val`; 202 of 252 tests pass, 49 are skipped and 1 fails (`cs_imgstore_cube`). The run with the base commit's translator gives the same 202, 49 and 1, test for test. The harness programs came from an older native build on this machine (2026-11-23) and were not rebuilt for this, which is the likely reason for that one failure; it is not a result of this change. Not run by us: native Windows, macOS, an AMD GPU, the `tools/hw_oracle` probe. A reviewer ran the GPU suite on an AMD RX 9070 XT (236 of 252 pass, 16 skipped, 0 failed) on `e01626be9`, which has the same translator as this head. GPUs tested: NVIDIA GeForce RTX 4070 (translation only; see above for what is not measured on hardware). AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named. ### Checklist - [x] Based on upstream main `32950551e` and merges cleanly into current `main`. - [x] One topic. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [x] The rule is in the contract table of `docs/design.md` ("Buffer bounds checking"), with how it was confirmed and what is not measured. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The hardware adds index * stride (a full product) and soffset to the 48-bit
V# base without wrapping at 32 bits (RDNA2 ISA 8.1.5: base + stride * index
+ offset). Only off = vaddr + inst_offset wraps. buffer_address formed the
whole offset in 32 bits, so index 0xFFFFFFFF with a 20-byte stride became
base - 20: OOB_SELECT 3 does not look at the index and 2 checks nothing, so
a store wrote 20 bytes below the buffer and a load read them.

The linear and the swizzled offset are now 64-bit values added to the base.
No bound is added. Each mode checks what it checked before, for a literal
and a run-time V# alike, and an access above 4 GiB that its mode allows is
made at its 64-bit address. What such an access does when nothing is mapped
there is not measured on the console and is undefined on Vulkan.

The non-wrapping product was observed on hardware by five-star-recomp
(ThatRecompDev, GPL-3, commit b9b57e2); the code and the test are new.
tests/semantic/test_buffer_offset_wrap.py fails 13 ways with 32-bit
arithmetic (linear and swizzled offsets). Contract row "Buffer bounds checking" in docs/design.md and
docs/buffer_bounds.md say what is measured and what is taken over.

AI-assisted (Claude Code).

Co-authored-by: Plaaasma <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:36:28 +00:00
github-bot closed this pull request 2026-11-26 09:36:28 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-24 18:10 UTC · original · requested changes

The new fits <= 0xFFFFFFFF predicate is applied in every OOB mode, which appears to reject valid mapped addresses above 4 GiB. The AMD RDNA2 ISA describes a 48-bit resource base and linear addressing as stride * index + offset; for OOB_SELECT=2 it specifies only NumRecords == 0 as the bounds check (“do not check bounds”). It does not define a 32-bit ceiling for the computed byte offset: https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf (section 8.1.5, pp. 59–62).

I reproduced the regression with this PR’s translator and interpreter: mode 2, NumRecords=1, stride 20, index 0x10000000 gives byte offset 0x1_40000000 (5 GiB), but the store is suppressed. If that address is backed by the allocation, mode 2’s documented check should not reject it. The external observation cited here establishes that one much larger offset does not wrap back into the pool; it does not establish that every offset above 0xFFFFFFFF is out of range.

Please add a >32-bit but mapped-address case and limit the guard to the actual hardware address/range condition established by evidence. The new tests in this PR pass (4/4); existing test_buffer_oob_select.py (36/36) and test_vector_mem_mubuf.py (53/53) also pass locally, so this is a missing semantic boundary case rather than a test failure.

**@⁠KernelSmoker** on GitHub · 2026-11-24 18:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#pullrequestreview-6044791421) · **requested changes** The new `fits <= 0xFFFFFFFF` predicate is applied in every OOB mode, which appears to reject valid mapped addresses above 4 GiB. The AMD RDNA2 ISA describes a 48-bit resource base and linear addressing as `stride * index + offset`; for `OOB_SELECT=2` it specifies only `NumRecords == 0` as the bounds check (“do not check bounds”). It does not define a 32-bit ceiling for the computed byte offset: https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf (section 8.1.5, pp. 59–62). I reproduced the regression with this PR’s translator and interpreter: mode 2, `NumRecords=1`, stride 20, index `0x10000000` gives byte offset `0x1_40000000` (5 GiB), but the store is suppressed. If that address is backed by the allocation, mode 2’s documented check should not reject it. The external observation cited here establishes that one much larger offset does not wrap back into the pool; it does not establish that every offset above `0xFFFFFFFF` is out of range. Please add a >32-bit but mapped-address case and limit the guard to the actual hardware address/range condition established by evidence. The new tests in this PR pass (4/4); existing `test_buffer_oob_select.py` (36/36) and `test_vector_mem_mubuf.py` (53/53) also pass locally, so this is a missing semantic boundary case rather than a test failure.
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-24 18:21 UTC · original

Review of #1068 (SHADER-MUBUF-OFFSET-WRAP)

Head: ff94bdd72ec46c6009129c5c832c8767e180aeae (one commit on 32950551e). Claim: #14 comment 7139398473 by the author, 17:34 UTC, six minutes before the PR was opened; it matches the scope.

Environment: Windows 11, native Python 3 (-I), LLVM llvm-mc (Windows build), no GPU used (the change has no GPU test, and no AMD GPU is available here).

Commands and results

  • python -I tests/semantic/test_buffer_offset_wrap.py: 4 tests, OK.
  • Related semantic files (every tests/semantic/test_* matching buffer / tbuffer / mubuf / bounds / oob / swizzle, 26 files) run one by one; test_buffer_formats_all.py and test_buffer_swizzle.py through scripts/run_semantic_tests.py tests/semantic -p <file> -j 1 because they import support: all pass (about 300 tests). The full suite was not run.
  • Mutation: git restore --source=HEAD~1 --worktree -- tools/rdna2spirv.py (the pre-PR translator), new test: FAILED (failures=5), the same five subtests the description names (index -1 in raw, raw literal, disabled, disabled literal; off 0xFFFFFFF0 + soffset 0x20). Restored with git checkout -- tools/rdna2spirv.py, tree clean.
  • git merge-tree --write-tree origin/main rv/1068 (origin/main 3aa975421): clean. No commit on main since the base touches tools/rdna2spirv.py or docs/buffer_bounds.md.
  • Open PRs that touch tools/rdna2spirv.py (16 of them): none edits Gen.buffer_address or docs/buffer_bounds.md. #1021 edits docs/design.md near the swizzled-buffer row; a pairwise merge-tree of #1021 and #1068 is clean.
  • Corpus (count only, game listings read locally, nothing copied): of 26,503 listings, 18,871 have a MUBUF/MTBUF instruction and 17,256 have at least one idxen access (about 448k idxen lines). About 955 also have a non-zero soffset (a rough text match). 22 files with [ in their names were skipped by the search, so these counts are slightly low. Most idxen accesses through a run-time V# get the new check, so most translated compute and graphics modules change (the 2 % size estimate in the description is plausible). I did not translate the corpus to compare SPIR-V.

Findings

  1. The skip for literal modes 0 and 1 is not exact (tools/rdna2spirv.py:4148, docs/design.md:501, comment at tools/rdna2spirv.py:4133-4134). The skip assumes that idx < NUM_RECORDS bounds idx * stride to 32 bits. That holds only when NUM_RECORDS * stride <= 2^32, and NUM_RECORDS is a run-time word even when word3 is literal. A probe (not part of the PR; F:\recomp\tmp\rv1068-probe.py, using the PR's own helpers): mode 1, stride 20, NUM_RECORDS 0xFFFFFFFF, index 0x0CCCCCCD, so idx * stride = 0x1_0000_0004:

    • run-time V#: nothing written, load reads 0 (the new check fires);
    • the same V# from s_mov_b32 literals: 77 is stored at base + 4 and read back.
      So the same descriptor gives different results depending on whether the translator folded word3. That needs a very large NUM_RECORDS (0xFFFFFFFF "unbounded" descriptors do occur), so it is a corner case, but the docs state the opposite. Either emit fits for literal modes 0/1 too, skip it only when a literal word2 and stride prove NUM_RECORDS * stride <= 2^32, or say in the row and the comment that the folded path does not check it. A literal structured case belongs in the test as well: test_structured_modes_already_refuse_the_index (tests/semantic/test_buffer_offset_wrap.py:96) covers only the run-time V#, which always takes the new check.
  2. How the rule was confirmed (docs/design.md:501, docs/buffer_bounds.md:22-26). AGENTS.md:33-35 asks for a GPU test, a Mesa/LLVM reference, or a count over the corpus. The row cites another project's commit and says it was "not measured by us". It is honest, but it is none of the three. Part of the rule is already backed here: the RX 6800 and gfx1036 measurements in the same row show that soffset is added before the mode 3 check without truncation. The new parts are not backed: the full 64-bit index * stride product, the "no effect" outcome in mode 2, and the swizzled offset in 64 bits. Also, "lands tens of GiB past the base and has no effect" is an inference. On hardware an address 80 GiB past the base is either a page fault or another mapping, not a guaranteed no-op. Dropping the access is a reasonable choice for a translator, but the docs and test name present it as hardware behaviour. Suggest saying "treated as out of range (the hardware address is far outside the buffer; what the console does there is not measured)", and asking someone with tools/hw_oracle for the one probe the description proposes before merging, or recording it as a follow-up.

  3. AGENTS.md:21 asks for a hand-written .asm fixture in tests/semantic/. The test writes its assembly inline in Python (tests/semantic/test_buffer_offset_wrap.py:36-65). This is common in the suite (29 .asm fixtures against 471 test files), so it is a nit.

  4. Minor cost: with a literal mode 3, linear addressing, no index and non-zero soffset, fits is emitted although the raw check (off + soffset + end <= NUM_RECORDS, 64-bit, at most 0xFFFFFFFF) already implies it (tools/rdna2spirv.py:4148). It is redundant, not wrong.

What I checked and found correct: the 64-bit linear form off + soffset + idx * stride (tools/rdna2spirv.py:4149-4163), where off (vaddr + inst_offset) still wraps at 32 bits, as the existing measured docstring says; the 64-bit swizzled form (4151-4157), which matches the 32-bit formula term by term and with L = 0 reduces to idx * stride + off; fits is ANDed into every per-dword, typed and atomic check through check_end (4256-4264), and it is computed before any branch, so it dominates its uses; and by reading the code, the folded literal-structured path without soffset emits the same SPIR-V as before (nothing new is emitted, and the mode line only moved).

Verdict: the main change is correct for the case it targets (raw and disabled, run-time and literal), the test is real (5 failures without the change), and the PR merges cleanly with no overlapping open PR. Request changes on two points before merging: fix or document finding 1, and reword the "hardware observation / no effect" claim in finding 2, or measure it. Findings 3 and 4 are optional.

AI-assisted.

**@⁠kawaii-shakal** on GitHub · 2026-11-24 18:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#issuecomment-7140537451) Review of #1068 (SHADER-MUBUF-OFFSET-WRAP) Head: ff94bdd72ec46c6009129c5c832c8767e180aeae (one commit on 32950551e). Claim: #14 comment 7139398473 by the author, 17:34 UTC, six minutes before the PR was opened; it matches the scope. Environment: Windows 11, native Python 3 (`-I`), LLVM llvm-mc (Windows build), no GPU used (the change has no GPU test, and no AMD GPU is available here). Commands and results - `python -I tests/semantic/test_buffer_offset_wrap.py`: 4 tests, OK. - Related semantic files (every `tests/semantic/test_*` matching buffer / tbuffer / mubuf / bounds / oob / swizzle, 26 files) run one by one; `test_buffer_formats_all.py` and `test_buffer_swizzle.py` through `scripts/run_semantic_tests.py tests/semantic -p <file> -j 1` because they import `support`: all pass (about 300 tests). The full suite was not run. - Mutation: `git restore --source=HEAD~1 --worktree -- tools/rdna2spirv.py` (the pre-PR translator), new test: FAILED (failures=5), the same five subtests the description names (index -1 in raw, raw literal, disabled, disabled literal; `off 0xFFFFFFF0 + soffset 0x20`). Restored with `git checkout -- tools/rdna2spirv.py`, tree clean. - `git merge-tree --write-tree origin/main rv/1068` (origin/main 3aa975421): clean. No commit on main since the base touches `tools/rdna2spirv.py` or `docs/buffer_bounds.md`. - Open PRs that touch `tools/rdna2spirv.py` (16 of them): none edits `Gen.buffer_address` or `docs/buffer_bounds.md`. #1021 edits `docs/design.md` near the swizzled-buffer row; a pairwise `merge-tree` of #1021 and #1068 is clean. - Corpus (count only, game listings read locally, nothing copied): of 26,503 listings, 18,871 have a MUBUF/MTBUF instruction and 17,256 have at least one `idxen` access (about 448k `idxen` lines). About 955 also have a non-zero `soffset` (a rough text match). 22 files with `[` in their names were skipped by the search, so these counts are slightly low. Most `idxen` accesses through a run-time V# get the new check, so most translated compute and graphics modules change (the 2 % size estimate in the description is plausible). I did not translate the corpus to compare SPIR-V. Findings 1. The skip for literal modes 0 and 1 is not exact (tools/rdna2spirv.py:4148, docs/design.md:501, comment at tools/rdna2spirv.py:4133-4134). The skip assumes that `idx < NUM_RECORDS` bounds `idx * stride` to 32 bits. That holds only when `NUM_RECORDS * stride <= 2^32`, and NUM_RECORDS is a run-time word even when word3 is literal. A probe (not part of the PR; F:\recomp\tmp\rv1068-probe.py, using the PR's own helpers): mode 1, stride 20, NUM_RECORDS 0xFFFFFFFF, index 0x0CCCCCCD, so `idx * stride` = 0x1_0000_0004: - run-time V#: nothing written, load reads 0 (the new check fires); - the same V# from `s_mov_b32` literals: 77 is stored at base + 4 and read back. So the same descriptor gives different results depending on whether the translator folded word3. That needs a very large NUM_RECORDS (0xFFFFFFFF "unbounded" descriptors do occur), so it is a corner case, but the docs state the opposite. Either emit `fits` for literal modes 0/1 too, skip it only when a literal word2 and stride prove `NUM_RECORDS * stride <= 2^32`, or say in the row and the comment that the folded path does not check it. A literal structured case belongs in the test as well: `test_structured_modes_already_refuse_the_index` (tests/semantic/test_buffer_offset_wrap.py:96) covers only the run-time V#, which always takes the new check. 2. How the rule was confirmed (docs/design.md:501, docs/buffer_bounds.md:22-26). AGENTS.md:33-35 asks for a GPU test, a Mesa/LLVM reference, or a count over the corpus. The row cites another project's commit and says it was "not measured by us". It is honest, but it is none of the three. Part of the rule is already backed here: the RX 6800 and gfx1036 measurements in the same row show that `soffset` is added before the mode 3 check without truncation. The new parts are not backed: the full 64-bit `index * stride` product, the "no effect" outcome in mode 2, and the swizzled offset in 64 bits. Also, "lands tens of GiB past the base and has no effect" is an inference. On hardware an address 80 GiB past the base is either a page fault or another mapping, not a guaranteed no-op. Dropping the access is a reasonable choice for a translator, but the docs and test name present it as hardware behaviour. Suggest saying "treated as out of range (the hardware address is far outside the buffer; what the console does there is not measured)", and asking someone with `tools/hw_oracle` for the one probe the description proposes before merging, or recording it as a follow-up. 3. AGENTS.md:21 asks for a hand-written `.asm` fixture in `tests/semantic/`. The test writes its assembly inline in Python (tests/semantic/test_buffer_offset_wrap.py:36-65). This is common in the suite (29 `.asm` fixtures against 471 test files), so it is a nit. 4. Minor cost: with a literal mode 3, linear addressing, no index and non-zero soffset, `fits` is emitted although the raw check (`off + soffset + end <= NUM_RECORDS`, 64-bit, at most 0xFFFFFFFF) already implies it (tools/rdna2spirv.py:4148). It is redundant, not wrong. What I checked and found correct: the 64-bit linear form `off + soffset + idx * stride` (tools/rdna2spirv.py:4149-4163), where `off` (vaddr + inst_offset) still wraps at 32 bits, as the existing measured docstring says; the 64-bit swizzled form (4151-4157), which matches the 32-bit formula term by term and with L = 0 reduces to `idx * stride + off`; `fits` is ANDed into every per-dword, typed and atomic check through `check_end` (4256-4264), and it is computed before any branch, so it dominates its uses; and by reading the code, the folded literal-structured path without soffset emits the same SPIR-V as before (nothing new is emitted, and the `mode` line only moved). Verdict: the main change is correct for the case it targets (raw and disabled, run-time and literal), the test is real (5 failures without the change), and the PR merges cleanly with no overlapping open PR. Request changes on two points before merging: fix or document finding 1, and reword the "hardware observation / no effect" claim in finding 2, or measure it. Findings 3 and 4 are optional. AI-assisted.
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-24 18:27 UTC · original · requested changes

Follow-up on the updated head 0c3c8d166dcd1319f90598054f5cb26916eb1baf: I reran the added test file (4/4 pass) and independently reproduced the literal structured-mode boundary issue.

With a literal OOB_SELECT=1 V#, NUM_RECORDS=0xFFFFFFFF, stride 20, and index 0x0CCCCCCD, the index is in range but index * stride = 0x1_0000_0004. The translator's literal-mode shortcut omits fits; the local interpreter writes and reads 77 at wrapped base+4 (0x4004) instead of addressing base+0x1_0000_0004 (0x100004004). The same runtime V# takes the fits path, so descriptor-folding changes the result. This is a valid structured index by the documented mode-1 check, not an out-of-range index.

Please fix the literal mode-0/1 shortcut and add matched literal/runtime tests for a >32-bit product with index < NUM_RECORDS. My earlier concern also remains: the guard suppresses all runtime mode-2 offsets above 4 GiB even though that mode performs no bounds check. Tests passing on the current head do not cover either mapped-address case; no AMD hardware result is claimed here.

**@⁠KernelSmoker** on GitHub · 2026-11-24 18:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#pullrequestreview-6045042926) · **requested changes** Follow-up on the updated head `0c3c8d166dcd1319f90598054f5cb26916eb1baf`: I reran the added test file (4/4 pass) and independently reproduced the literal structured-mode boundary issue. With a literal `OOB_SELECT=1` V#, `NUM_RECORDS=0xFFFFFFFF`, stride 20, and index `0x0CCCCCCD`, the index is in range but `index * stride = 0x1_0000_0004`. The translator's literal-mode shortcut omits `fits`; the local interpreter writes and reads 77 at wrapped `base+4` (`0x4004`) instead of addressing `base+0x1_0000_0004` (`0x100004004`). The same runtime V# takes the `fits` path, so descriptor-folding changes the result. This is a valid structured index by the documented mode-1 check, not an out-of-range index. Please fix the literal mode-0/1 shortcut and add matched literal/runtime tests for a >32-bit product with `index < NUM_RECORDS`. My earlier concern also remains: the guard suppresses all runtime mode-2 offsets above 4 GiB even though that mode performs no bounds check. Tests passing on the current head do not cover either mapped-address case; no AMD hardware result is claimed here.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-24 18:57 UTC · original

AI-assisted independent GPU verification (overnight shift, worker w4).

Environment: Windows, Git Bash; runtime built RelWithDebInfo (Ninja, VS2022 BuildTools, SDK 10.0.26100, Vulkan SDK 1.4.363.0). llvm-mc via WSL Ubuntu llvm-21; spirv-val on PATH.

AMD Radeon iGPU (RDNA2 gfx1035, driver 24.10.24, Vulkan 1.3.280, 128B push constants):
VK_LOADER_DRIVERS_SELECT=*amd-vulkan* LEONIDA_VK_DEVICE=radeon VK_LOADER_LAYERS_DISABLE=~implicit~ LEONIDA_VK_VALIDATION=1 LEONIDA_PC_TAIL_UBO=1 py -3.14 -I scripts/run_gpu_tests.py --work <dir>

  • 229/252 passed, 20 skipped, 3 failed — all pre-existing/environmental, none related to this change:
    • ms_clip, ms_wave_info_s3: known AMD Windows LLPC 0xC0000005 mesh-shader crashes (SHADER-167; fixes in open PRs #802/#808, not in this base).
    • ms_v5_workgroup: first run lacked spirv-val on PATH; re-run with VulkanSDK/1.4.363.0/Bin on PATH → PASS (24/24 workgroup coordinates exact).
  • 0 Vulkan validation errors.

NVIDIA RTX 4070 SUPER (driver 610.88):
VK_LOADER_DRIVERS_SELECT=*nv-vk* LEONIDA_VK_DEVICE=4070 VK_LOADER_LAYERS_DISABLE=~implicit~ py -3.14 -I scripts/run_gpu_tests.py --work <dir>

  • 203/252 passed, 49 skipped, 0 failed. 0 validation errors.

Translator tests:

  • py -3.14 -I -m unittest discover -s tests/semantic -p test_buffer_offset_wrap.py → 4/4 OK.
  • Full suite py -3.14 -I scripts/run_semantic_tests.py tests/semantic → 471 files, 6172 tests, 0 failed.

Verdict: verified. The 32-bit buffer-offset wrap change breaks nothing on either GPU; the only AMD failures are the known mesh-shader driver crashes unrelated to this PR.

**@⁠whoalin1** on GitHub · 2026-11-24 18:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#issuecomment-7141380919) AI-assisted independent GPU verification (overnight shift, worker w4). **Environment:** Windows, Git Bash; runtime built RelWithDebInfo (Ninja, VS2022 BuildTools, SDK 10.0.26100, Vulkan SDK 1.4.363.0). `llvm-mc` via WSL Ubuntu llvm-21; spirv-val on PATH. **AMD Radeon iGPU (RDNA2 gfx1035, driver 24.10.24, Vulkan 1.3.280, 128B push constants):** `VK_LOADER_DRIVERS_SELECT=*amd-vulkan* LEONIDA_VK_DEVICE=radeon VK_LOADER_LAYERS_DISABLE=~implicit~ LEONIDA_VK_VALIDATION=1 LEONIDA_PC_TAIL_UBO=1 py -3.14 -I scripts/run_gpu_tests.py --work <dir>` - 229/252 passed, 20 skipped, **3 failed — all pre-existing/environmental, none related to this change:** - `ms_clip`, `ms_wave_info_s3`: known AMD Windows LLPC 0xC0000005 mesh-shader crashes (SHADER-167; fixes in open PRs #802/#808, not in this base). - `ms_v5_workgroup`: first run lacked spirv-val on PATH; re-run with `VulkanSDK/1.4.363.0/Bin` on PATH → **PASS** (24/24 workgroup coordinates exact). - **0 Vulkan validation errors.** **NVIDIA RTX 4070 SUPER (driver 610.88):** `VK_LOADER_DRIVERS_SELECT=*nv-vk* LEONIDA_VK_DEVICE=4070 VK_LOADER_LAYERS_DISABLE=~implicit~ py -3.14 -I scripts/run_gpu_tests.py --work <dir>` - 203/252 passed, 49 skipped, **0 failed**. 0 validation errors. **Translator tests:** - `py -3.14 -I -m unittest discover -s tests/semantic -p test_buffer_offset_wrap.py` → 4/4 OK. - Full suite `py -3.14 -I scripts/run_semantic_tests.py tests/semantic` → 471 files, 6172 tests, 0 failed. **Verdict: verified.** The 32-bit buffer-offset wrap change breaks nothing on either GPU; the only AMD failures are the known mesh-shader driver crashes unrelated to this PR.
Author
Owner

@⁠Moriz82 on GitHub · 2026-11-24 19:26 UTC · original

Thank you both for the reviews. You were right on the main point, so the branch is reworked instead of patched: the fits <= 0xFFFFFFFF bound is gone. The byte offset is now a 64-bit value added to the 48-bit base, in every mode and for a literal and a run-time V# alike, and each mode checks exactly what it checked before. The title and the description are rewritten to match.

@⁠KernelSmoker

  1. Mode 2 offsets above 4 GiB are no longer suppressed. Your case (mode 2, NumRecords 1, stride 20, index 0x10000000) is a test with a literal and a run-time V#: the store lands 5 GiB past the base and the load reads it back.
  2. The literal mode 0/1 shortcut is gone, because there is no check left to skip. Your second case (NUM_RECORDS 0xFFFFFFFF, stride 20, index 0x0CCCCCCD) is a test for modes 0 and 1, literal and run-time: all four store and read at base + 0x1_0000_0004.

@⁠kawaii-shakal

  1. Same fix as point 2 above, with the literal structured cases you asked for in the test.
  2. Reworded. The docs, the source comment and the test no longer present "no effect" as hardware behaviour. They now say: the formula is the ISA's (8.1.5), the non-wrapping product is five-star-recomp's observation and not measured by us, and what the console does with an address where nothing is mapped is not measured. The tools/hw_oracle probe is still wanted and is not done.
  3. The assembly stays inline in the Python file, as in most of the suite.
  4. Gone with the bound.

One consequence is the opposite of the first version, so I want it to be explicit. Index −1 in mode 2 or 3 is now neither base − 20 nor dropped: it is an access tens of GiB past the base. The runtime's arena is at most 4095 MiB, so that address is always outside it, and on Vulkan that is undefined. If the project wants such an access refused, I think the bound should be the arena size and it should live where the arena is known, as a follow-up. If you would rather have it in this pull request, say so.

Run on the new head (CachyOS, Python 3.14, llvm-mc 23.1.5, SPIRV-Tools v2026.4): tests/semantic/test_buffer_offset_wrap.py passes 6 tests, and 11 subtests fail with the base commit's translator and with the previous head's. Full semantic suite: 471 files, 6,174 tests, 0 files failed. scripts/run_gpu_tests.py on an RTX 4070 (620.73.04): 202 of 252 pass, 49 skipped, 1 fails (cs_imgstore_cube), test for test the same as with the base commit's translator; the harness programs were an older local build, not rebuilt. Not run here: native Windows and AMD. Your runs on the previous head do not carry over, since every indexed access now changes its SPIR-V.

🤖 Generated with Claude Code

**@⁠Moriz82** on GitHub · 2026-11-24 19:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#issuecomment-7142101167) Thank you both for the reviews. You were right on the main point, so the branch is reworked instead of patched: the `fits <= 0xFFFFFFFF` bound is gone. The byte offset is now a 64-bit value added to the 48-bit base, in every mode and for a literal and a run-time V# alike, and each mode checks exactly what it checked before. The title and the description are rewritten to match. @⁠KernelSmoker 1. Mode 2 offsets above 4 GiB are no longer suppressed. Your case (mode 2, `NumRecords` 1, stride 20, index `0x10000000`) is a test with a literal and a run-time V#: the store lands 5 GiB past the base and the load reads it back. 2. The literal mode 0/1 shortcut is gone, because there is no check left to skip. Your second case (`NUM_RECORDS` `0xFFFFFFFF`, stride 20, index `0x0CCCCCCD`) is a test for modes 0 and 1, literal and run-time: all four store and read at base + `0x1_0000_0004`. @⁠kawaii-shakal 1. Same fix as point 2 above, with the literal structured cases you asked for in the test. 2. Reworded. The docs, the source comment and the test no longer present "no effect" as hardware behaviour. They now say: the formula is the ISA's (8.1.5), the non-wrapping product is five-star-recomp's observation and not measured by us, and what the console does with an address where nothing is mapped is not measured. The `tools/hw_oracle` probe is still wanted and is not done. 3. The assembly stays inline in the Python file, as in most of the suite. 4. Gone with the bound. One consequence is the opposite of the first version, so I want it to be explicit. Index −1 in mode 2 or 3 is now neither base − 20 nor dropped: it is an access tens of GiB past the base. The runtime's arena is at most 4095 MiB, so that address is always outside it, and on Vulkan that is undefined. If the project wants such an access refused, I think the bound should be the arena size and it should live where the arena is known, as a follow-up. If you would rather have it in this pull request, say so. Run on the new head (CachyOS, Python 3.14, llvm-mc 23.1.5, SPIRV-Tools v2026.4): `tests/semantic/test_buffer_offset_wrap.py` passes 6 tests, and 11 subtests fail with the base commit's translator and with the previous head's. Full semantic suite: 471 files, 6,174 tests, 0 files failed. `scripts/run_gpu_tests.py` on an RTX 4070 (620.73.04): 202 of 252 pass, 49 skipped, 1 fails (`cs_imgstore_cube`), test for test the same as with the base commit's translator; the harness programs were an older local build, not rebuilt. Not run here: native Windows and AMD. Your runs on the previous head do not carry over, since every indexed access now changes its SPIR-V. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠iFeyz on GitHub · 2026-11-24 19:51 UTC · original

AMD run (the gap the description lists): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, MSVC 2022 RelWithDebInfo.

Head e01626be9:

  • scripts/run_gpu_tests.py with this translator: 261 of 261 shaders translated and spirv-val-clean, 236 of 252 pass, 16 skipped, 0 failed (native, subgroup 64).
  • tests/semantic/test_buffer_offset_wrap.py (6): OK.
**@⁠iFeyz** on GitHub · 2026-11-24 19:51 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#issuecomment-7142677997) AMD run (the gap the description lists): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, MSVC 2022 RelWithDebInfo. Head `e01626be9`: - `scripts/run_gpu_tests.py` with this translator: 261 of 261 shaders translated and `spirv-val`-clean, **236 of 252 pass, 16 skipped, 0 failed** (native, subgroup 64). - `tests/semantic/test_buffer_offset_wrap.py` (6): OK.
Author
Owner

@⁠thesubtleties on GitHub · 2026-11-24 20:07 UTC · original

CPU check of e01626be9 merged onto main 3aa97542 (CachyOS, Python 3.14).

  • test_buffer_offset_wrap.py: 6 pass; 11 failures with main's rdna2spirv.py.
  • run_semantic_tests.py tests/semantic: 471 files / 6,174 tests, 0 failed.
  • mem_diff.py (2,000 programs, seed 661001) is clean on both, so it doesn't reach this case.
  • I also read the bounds check: the mode 0 and 3 end checks were already 64-bit, so they agree with the new address.

One gap: the new tests only use the linear path. The swizzled path also moved to 64 bits and has no case above 4 GiB.

Not tested here: GPU, game corpus, what hardware does at such an address. AI-assisted (Claude Code)

**@⁠thesubtleties** on GitHub · 2026-11-24 20:07 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#issuecomment-7143075121) CPU check of `e01626be9` merged onto main `3aa97542` (CachyOS, Python 3.14). - `test_buffer_offset_wrap.py`: 6 pass; 11 failures with main's `rdna2spirv.py`. - `run_semantic_tests.py tests/semantic`: 471 files / 6,174 tests, 0 failed. - `mem_diff.py` (2,000 programs, seed 661001) is clean on both, so it doesn't reach this case. - I also read the bounds check: the mode 0 and 3 end checks were already 64-bit, so they agree with the new address. One gap: the new tests only use the linear path. The swizzled path also moved to 64 bits and has no case above 4 GiB. Not tested here: GPU, game corpus, what hardware does at such an address. AI-assisted (Claude Code)
Author
Owner

@⁠Moriz82 on GitHub · 2026-11-24 21:08 UTC · original

@⁠thesubtleties thank you, that gap was real: the swizzled path moved to 64 bits and had no case above 4 GiB. Added to tests/semantic/test_buffer_offset_wrap.py, with a literal and a run-time V# each:

  • swizzled, IS 8, stride 20, index 0x0CCCCCD5 (group 0x0199999A, element 5): the store and the load are at base + 0x1_0000_0054; modulo 2^32 that would be base + 0x54;
  • the same formula below 4 GiB as a control (index 13: base + 180).

The file now has 8 tests. With the base commit's translator 13 subtests fail (the two swizzled cases above 4 GiB are new among them; the control passes on both). Full semantic suite on the new head: 471 files, 6,176 tests, 0 files failed. The translator itself is unchanged since e01626be9; only the test and the count in the docs/design.md row changed.

@⁠iFeyz thank you for the RX 9070 XT run; the description now cites it.

🤖 Generated with Claude Code

**@⁠Moriz82** on GitHub · 2026-11-24 21:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#issuecomment-7144536407) @⁠thesubtleties thank you, that gap was real: the swizzled path moved to 64 bits and had no case above 4 GiB. Added to `tests/semantic/test_buffer_offset_wrap.py`, with a literal and a run-time V# each: - swizzled, `IS` 8, stride 20, index `0x0CCCCCD5` (group `0x0199999A`, element 5): the store and the load are at base + `0x1_0000_0054`; modulo 2^32 that would be base + `0x54`; - the same formula below 4 GiB as a control (index 13: base + 180). The file now has 8 tests. With the base commit's translator 13 subtests fail (the two swizzled cases above 4 GiB are new among them; the control passes on both). Full semantic suite on the new head: 471 files, 6,176 tests, 0 files failed. The translator itself is unchanged since `e01626be9`; only the test and the count in the `docs/design.md` row changed. @⁠iFeyz thank you for the RX 9070 XT run; the description now cites it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠Moriz82 on GitHub · 2026-11-25 03:41 UTC · original

@⁠KernelSmoker I rebased this onto main at bfef489c and reran the full semantic suite on a656d4f0. All 6,287 tests passed across 488 files. The eight focused cases cover mapped offsets above 4 GiB in mode 2, structured modes 0 and 1, and swizzled addressing, with both literal and runtime descriptors. Could you re-review the current head? The console behavior for an unmapped address remains unmeasured.

AI-assisted.

**@⁠Moriz82** on GitHub · 2026-11-25 03:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#issuecomment-7152146994) @⁠KernelSmoker I rebased this onto main at `bfef489c` and reran the full semantic suite on `a656d4f0`. All 6,287 tests passed across 488 files. The eight focused cases cover mapped offsets above 4 GiB in mode 2, structured modes 0 and 1, and swizzled addressing, with both literal and runtime descriptors. Could you re-review the current head? The console behavior for an unmapped address remains unmeasured. AI-assisted.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 19:01 UTC · original · approved these changes

Approving. Merged into current main (9f9a7c5) with no conflicts. test_buffer_offset_wrap.py passes, and the full translator suite gives 491 files / 6,300 tests with only test_int32_to_f64 failing, the same baseline failure as main here (Linux, Python 3.13).

I checked that no 32-bit offset survives in the address path. Both the linear and the swizzled forms now build wide as a %ulong. That covers index·stride as a full 64-bit product, soffset added in 64 bits, and the swizzled msb·stride + om4 shifted in 64 bits. The final base + wide takes it directly. Only vaddr + inst_offset still wraps at 32 bits, as the comment says, and the bound checks are untouched.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 19:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1068#pullrequestreview-6063332886) · **approved these changes** Approving. Merged into current main (9f9a7c5) with no conflicts. `test_buffer_offset_wrap.py` passes, and the full translator suite gives 491 files / 6,300 tests with only `test_int32_to_f64` failing, the same baseline failure as main here (Linux, Python 3.13). I checked that no 32-bit offset survives in the address path. Both the linear and the swizzled forms now build `wide` as a `%ulong`. That covers index·stride as a full 64-bit product, soffset added in 64 bits, and the swizzled `msb·stride + om4` shifted in 64 bits. The final `base + wide` takes it directly. Only `vaddr + inst_offset` still wraps at 32 bits, as the comment says, and the bound checks are untouched. AI-assisted (Claude).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.