rdna2spirv: translate FLAT loads and stores as global accesses #65

Open
github-bot wants to merge 2 commits from github-bot/gh-65 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 02:11 UTC · original
ubiquitarianism:old-pr/9790 → main · +214 −3 · 4 files · 2 commits · draft

Carried over from old PR 9790 by tiagonogueiraa-bit in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9790, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
tiagonogueiraa-bit: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Resubmission of #8823, unchanged (head fbe718a46). #8823 was closed at 22:39 UTC during the move to ProjectLeonida, with no comment and no triage: line, and the API refuses to reopen it. It had about ten independent passes (CPU and GPU on Linux, Windows and macOS; see #8823's comments). On today's main 2d53a8055 the branch merges without conflicts, and its 12 test_flat_mem tests pass. Originally claimed in #8683: https://github.com/ProjectLeonida/Leonida/issues/8683#issuecomment-7111644742

What

flat_load_* and flat_store_* used to reach the translator's final Unsupported(op). tools/isa_coverage.py listed all 54 FLAT opcodes as unknown, and no open PR translates them. Gen.flat_mem now emits the same code as the GLOBAL access with off, through Gen.global_mem / Gen.global_sub. That covers:

  • every dword width (dword, x2, x3, x4);
  • the 8/16-bit loads and stores;
  • the d16 / d16_hi forms.

Inactive lanes don't access memory, as with global_*.

Why global

  • RDNA2 rule: a FLAT address outside the LDS and private apertures reads and writes global memory, and that is the GLOBAL access with off.
  • Aperture addresses: a program reads the aperture bases as src_shared_base / src_private_base. The translator refuses those operands, so a program that builds an aperture address from them is refused as a whole. A test covers this.
  • Unchecked case: an aperture address loaded from memory or a constant would be treated as global memory. I haven't seen one, and the translator can't detect it. The docs/design.md row says so.

Refused (Unsupported)

  • A non-zero instruction offset. Its behaviour on the console GPU isn't measured. LLVM marks GFX10.1 with FeatureFlatSegmentOffsetBug ("inst_offset is ignored when flat instructions access global memory") and folds no offset into a FLAT access there.
  • flat_atomic_*.
  • A 32-bit address.

Also changed

  • First-read analysis (read_before_write_vgprs): it counts the vdst of a FLAT d16 load as read, the same as for global_load_*_d16.
  • Interp copy analysis: it skips flat_store like the other stores.
  • tools/ab_control_flow.py: it lists flat_store among the memory writes the harness can't set up, next to global_store.
  • Contract row: a new "Flat memory" row in docs/design.md.

Tests

tests/semantic/test_flat_mem.py is new. Each program is translated, run with spirv_mini_interp, checked against words worked out by hand, and checked to leave the same memory as its global_* ..., off spelling. It covers:

  • loads and stores of every width;
  • byte offsets 0 to 11, including a 16-bit access that crosses a dword;
  • d16 loads keeping the other half;
  • a non-zero high address dword;
  • 32 lanes with 8 active through v_cmpx;
  • the refused forms (offset, atomics, 32-bit address) and both aperture operands;
  • the first-read analysis.

What I ran, on macOS 26 with an Apple M4 (CPU only) and Python 3.12.13:

  • python -I scripts/run_semantic_tests.py -j 4 tests/semantic: 448 files, 5,969 tests; 446 files pass. The two that fail, test_image_atomics_guard (47) and test_image_store_d16 (1), fail the same way on main 867d922f.
  • tests/test_isa_coverage.py, tests/test_semantic_translator_use.py, tests/test_ab_control_flow.py, tests/test_design_contract_rows.py, tests/test_docs_repo_paths.py: all pass.
  • tests/test_docs_design_rows.py and tests/test_docs_links.py fail the same way on main (on docs/README.md and docs/dev-tools.md, which this PR doesn't touch).
  • scripts/check_no_game_data.py passes on the changed files.

Not GPU-verified. No game files were used.

AI-assisted (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

**@⁠Andretuta** on GitHub · 2026-11-24 02:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/65) `ubiquitarianism:old-pr/9790` → `main` · +214 −3 · 4 files · 2 commits · draft > Carried over from **old PR 9790** by `tiagonogueiraa-bit` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9790`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `tiagonogueiraa-bit`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): **Resubmission of `#8823`**, unchanged (head `fbe718a46`). `#8823` was closed at 22:39 UTC during the move to ProjectLeonida, with no comment and no `triage:` line, and the API refuses to reopen it. It had about ten independent passes (CPU and GPU on Linux, Windows and macOS; see `#8823`'s comments). On today's `main` `2d53a8055` the branch merges without conflicts, and its 12 `test_flat_mem` tests pass. Originally claimed in `#8683`: https://github.com/ProjectLeonida/Leonida/issues/8683#issuecomment-7111644742 ## What `flat_load_*` and `flat_store_*` used to reach the translator's final `Unsupported(op)`. `tools/isa_coverage.py` listed all 54 FLAT opcodes as unknown, and no open PR translates them. `Gen.flat_mem` now emits the same code as the GLOBAL access with `off`, through `Gen.global_mem` / `Gen.global_sub`. That covers: - every dword width (`dword`, `x2`, `x3`, `x4`); - the 8/16-bit loads and stores; - the d16 / d16_hi forms. Inactive lanes don't access memory, as with `global_*`. ## Why global - **RDNA2 rule:** a FLAT address outside the LDS and private apertures reads and writes global memory, and that is the GLOBAL access with `off`. - **Aperture addresses:** a program reads the aperture bases as `src_shared_base` / `src_private_base`. The translator refuses those operands, so a program that builds an aperture address from them is refused as a whole. A test covers this. - **Unchecked case:** an aperture address loaded from memory or a constant would be treated as global memory. I haven't seen one, and the translator can't detect it. The `docs/design.md` row says so. ## Refused (`Unsupported`) - **A non-zero instruction offset.** Its behaviour on the console GPU isn't measured. LLVM marks GFX10.1 with `FeatureFlatSegmentOffsetBug` ("inst_offset is ignored when flat instructions access global memory") and folds no offset into a FLAT access there. - **`flat_atomic_*`.** - **A 32-bit address.** ## Also changed - **First-read analysis** (`read_before_write_vgprs`): it counts the `vdst` of a FLAT d16 load as read, the same as for `global_load_*_d16`. - **Interp copy analysis:** it skips `flat_store` like the other stores. - **`tools/ab_control_flow.py`:** it lists `flat_store` among the memory writes the harness can't set up, next to `global_store`. - **Contract row:** a new "Flat memory" row in `docs/design.md`. ## Tests `tests/semantic/test_flat_mem.py` is new. Each program is translated, run with `spirv_mini_interp`, checked against words worked out by hand, and checked to leave the same memory as its `global_* ..., off` spelling. It covers: - loads and stores of every width; - byte offsets 0 to 11, including a 16-bit access that crosses a dword; - d16 loads keeping the other half; - a non-zero high address dword; - 32 lanes with 8 active through `v_cmpx`; - the refused forms (offset, atomics, 32-bit address) and both aperture operands; - the first-read analysis. What I ran, on macOS 26 with an Apple M4 (CPU only) and Python 3.12.13: - `python -I scripts/run_semantic_tests.py -j 4 tests/semantic`: 448 files, 5,969 tests; 446 files pass. The two that fail, `test_image_atomics_guard` (47) and `test_image_store_d16` (1), fail the same way on `main` `867d922f`. - `tests/test_isa_coverage.py`, `tests/test_semantic_translator_use.py`, `tests/test_ab_control_flow.py`, `tests/test_design_contract_rows.py`, `tests/test_docs_repo_paths.py`: all pass. - `tests/test_docs_design_rows.py` and `tests/test_docs_links.py` fail the same way on `main` (on `docs/README.md` and `docs/dev-tools.md`, which this PR doesn't touch). - `scripts/check_no_game_data.py` passes on the changed files. **Not GPU-verified.** No game files were used. AI-assisted (Claude Code, Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
flat_load_* / flat_store_* with a 64-bit VGPR address emit the global_*
code with `off` (Gen.global_mem / Gen.global_sub). An address outside the
LDS and private apertures is a global access on RDNA2; the aperture bases
(src_shared_base / src_private_base) are refused, so no translated program
builds an aperture address from them. A non-zero instruction offset, the
FLAT atomics and a 32-bit address stay Unsupported. The first-read analysis
counts a FLAT d16 load's vdst, the interp copy analysis and ab_control_flow
treat flat_store like global_store.

CPU-checked by tests/semantic/test_flat_mem.py; contract row in
docs/design.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review on #8823 (wavehs): NEEDS listed flat_store but not flat_atomic, which
writes memory as well.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:42:51 +00:00
Author
Owner

@⁠wavehs on GitHub · 2026-11-24 02:19 UTC · original

macOS arm64 verification (Apple M3 Pro) — the FLAT→GLOBAL mapping is right, the new test bites, and the merged tree is identical to main row for row on both drivers

Head fbe718a46, branch point 867d922f0; I merged it onto current main (9ec34cdff) as d238c4e80 with no conflicts, and everything below is that merged tree. 4 files +214/−3. Host: macOS 27.2.1, Apple M3 Pro (arm64), CPython 3.15.2, llvm-mc 23.1.5 (AMDGPU), Apple clang 21.0.0, MoltenVK 1.4.0 and KosmicKrisp (Mesa 26.3.0).

Check Merged d238c4e80 main 9ec34cdff
tests/semantic/test_flat_mem.py 12/12 OK file absent; the same file on the base translator: 2 failures + 36 errors
scripts/run_semantic_tests.py tests/semantic 464 files, 6,087 tests, 2 failed 463 files, 6,075 tests, 2 failed
the two failing files test_exec_computed, test_image_atomics_guard the same two
run_gpu_tests.py (all 248 cases), MoltenVK 182/248 passed, 61 skipped 182/248, 61 skipped
run_gpu_tests.py (all 248), KosmicKrisp 177/248 passed, 64 skipped 177/248, 64 skipped
test_isa_coverage, test_semantic_translator_use, test_ab_control_flow, test_design_contract_rows, test_docs_repo_paths, test_docs_design_rows, test_docs_links all OK —

The GPU rows are identical line for line on both drivers after normalising the temporary work paths: no fixture changed verdict, value or skip reason. No fixture uses FLAT yet (0 .asm files match flat_), so that is a regression check on everything else, not a hardware test of this rule — which is why the design row's "Not GPU-verified" is the honest label.

The negative control is the base translator: 36 of the 12 tests' subtests raise Unsupported there and 2 assert failures remain, against 0 at this head. And the new test is not only "it translates": every program is also run as its global_* ..., off spelling and the memory must match, so the assertion is equivalence between the two forms, not a golden number.

The reuse is the right shape. flat_mem maps flat_<x> to global_<x> and hands {"k":"special","t":"off"} as the SADDR (tools/rdna2spirv.py:3558-3595), which lands in global_address's off branch — the 64-bit VGPR pair path (:3365-3368) that the GPU fixtures already exercise for global_*. So the heap-base translation, the inactive-lane gate and the sub-dword byte merging are the same code, not a parallel implementation. Three things I checked by translating, beyond the test file:

  • an odd register pair (flat_load_dword v5, v[3:4]) translates — the code uses vaddr["r"] and r + 1, and every test case uses v[2:3];
  • glc slc on a load and glc on flat_store_dwordx4 translate, i.e. the cache modifiers reach the GLOBAL path instead of being refused;
  • lds cannot appear through the assembler at all (llvm-mc rejects it as an operand for flat_load_dword), and global_mem refuses the modifier anyway (:3475), so the one FLAT bit that would make an access LDS-relative is unreachable.

The d16 forms go through GLOBAL_SUB, the first-read analysis counts the destination half (:15871-15873), and flat_atomic_* and a 32-bit address fall through to Unsupported — all covered by the test.

Two small suggestions, neither blocking.

  • Add one case with an odd pair (v[3:4]) and one with glc/slc, since both translate and neither is pinned; the current test file only ever spells v[2:3] and no cache modifier.
  • The first hardware check for this rule should be a compute fixture that reads and writes through a FLAT address using the existing cs_global_* pattern, because the CPU test proves FLAT ≡ GLOBAL in the translator — not that the console GPU's global path is what the console's FLAT does. The design row already says this; a fixture would turn it into a GPU-checked row.

Also worth saying for the carried-over context: the original PR's macOS pass was CPU-only, so this is the first time the rule has been run against a merged tree on an Apple GPU here, and it costs nothing: both suites are unchanged.

No game files. AI-assisted.

**@⁠wavehs** on GitHub · 2026-11-24 02:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/65#issuecomment-7120017403) **macOS arm64 verification (Apple M3 Pro) — the FLAT→GLOBAL mapping is right, the new test bites, and the merged tree is identical to `main` row for row on both drivers** Head `fbe718a46`, branch point `867d922f0`; I merged it onto current `main` (`9ec34cdff`) as `d238c4e80` with no conflicts, and everything below is that merged tree. 4 files +214/−3. Host: macOS 27.2.1, Apple M3 Pro (arm64), CPython 3.15.2, llvm-mc 23.1.5 (AMDGPU), Apple clang 21.0.0, MoltenVK 1.4.0 and KosmicKrisp (Mesa 26.3.0). | Check | Merged `d238c4e80` | `main` `9ec34cdff` | |---|---|---| | `tests/semantic/test_flat_mem.py` | **12/12 OK** | file absent; the same file on the base translator: **2 failures + 36 errors** | | `scripts/run_semantic_tests.py tests/semantic` | **464 files, 6,087 tests, 2 failed** | 463 files, 6,075 tests, 2 failed | | the two failing files | `test_exec_computed`, `test_image_atomics_guard` | the same two | | `run_gpu_tests.py` (all 248 cases), MoltenVK | **182/248 passed, 61 skipped** | 182/248, 61 skipped | | `run_gpu_tests.py` (all 248), KosmicKrisp | **177/248 passed, 64 skipped** | 177/248, 64 skipped | | `test_isa_coverage`, `test_semantic_translator_use`, `test_ab_control_flow`, `test_design_contract_rows`, `test_docs_repo_paths`, `test_docs_design_rows`, `test_docs_links` | all **OK** | — | The GPU rows are **identical line for line** on both drivers after normalising the temporary work paths: no fixture changed verdict, value or skip reason. No fixture uses FLAT yet (0 `.asm` files match `flat_`), so that is a regression check on everything else, not a hardware test of this rule — which is why the design row's "Not GPU-verified" is the honest label. **The negative control is the base translator**: 36 of the 12 tests' subtests raise `Unsupported` there and 2 assert failures remain, against 0 at this head. And the new test is not only "it translates": every program is also run as its `global_* ..., off` spelling and the memory must match, so the assertion is equivalence between the two forms, not a golden number. **The reuse is the right shape.** `flat_mem` maps `flat_<x>` to `global_<x>` and hands `{"k":"special","t":"off"}` as the SADDR (`tools/rdna2spirv.py:3558-3595`), which lands in `global_address`'s `off` branch — the 64-bit VGPR pair path (`:3365-3368`) that the GPU fixtures already exercise for `global_*`. So the heap-base translation, the inactive-lane gate and the sub-dword byte merging are the same code, not a parallel implementation. Three things I checked by translating, beyond the test file: - an **odd register pair** (`flat_load_dword v5, v[3:4]`) translates — the code uses `vaddr["r"]` and `r + 1`, and every test case uses `v[2:3]`; - `glc slc` on a load and `glc` on `flat_store_dwordx4` translate, i.e. the cache modifiers reach the GLOBAL path instead of being refused; - `lds` cannot appear through the assembler at all (llvm-mc rejects it as an operand for `flat_load_dword`), and `global_mem` refuses the modifier anyway (`:3475`), so the one FLAT bit that would make an access LDS-relative is unreachable. The d16 forms go through `GLOBAL_SUB`, the first-read analysis counts the destination half (`:15871-15873`), and `flat_atomic_*` and a 32-bit address fall through to `Unsupported` — all covered by the test. **Two small suggestions, neither blocking.** - Add one case with an odd pair (`v[3:4]`) and one with `glc`/`slc`, since both translate and neither is pinned; the current test file only ever spells `v[2:3]` and no cache modifier. - The first hardware check for this rule should be a compute fixture that reads and writes through a FLAT address using the existing `cs_global_*` pattern, because the CPU test proves FLAT ≡ GLOBAL in the translator — not that the console GPU's global path is what the console's FLAT does. The design row already says this; a fixture would turn it into a GPU-checked row. Also worth saying for the carried-over context: the original PR's macOS pass was CPU-only, so this is the first time the rule has been run against a merged tree on an Apple GPU here, and it costs nothing: both suites are unchanged. No game files. AI-assisted.
This pull request has changes conflicting with the target branch.
  • docs/design.md
  • tools/rdna2spirv.py
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/65/head:github-bot/gh-65'
git switch 'github-bot/gh-65'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-65'
git switch 'github-bot/gh-65'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-65'
git switch 'github-bot/gh-65'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-65'
git switch 'main'
git merge --squash 'github-bot/gh-65'
git switch 'main'
git merge --ff-only 'github-bot/gh-65'
git switch 'main'
git merge 'github-bot/gh-65'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.