rdna2_decode: align MIMG addr, atomic TFE/cmpswap, and r128 widths with LLVM (DECODE-MIMG-PARITY) #633

Manually merged
github merged 1 commit from gh-pr/633/head into gh-pr/633/base 2026-11-26 09:12:51 +00:00
Owner

@⁠jarvixaiassistant-glitch on GitHub · 2026-11-24 10:30 UTC · original
jarvixaiassistant-glitch:fix/decode-mimg-llvm-parity → main · +109 −44 · 2 files · 1 commits · merged 2026-11-25 21:21 UTC

Claimed in the coordination issue as: DECODE-MIMG-PARITY

What and why

Fixes 72 MIMG disassembly mismatches in the pure-Python RDNA2 decoder (tools/rdna2_decode.py) uncovered by cross-checking against all 291 image_* test vectors in upstream LLVM's official GFX10/GFX1030 disassembler suites (gfx10_mimg.txt, gfx10_mimg_features.txt, gfx1030_new.txt) and AMDGPU::getAddrSizeMIMGOp (llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp):

  1. _mimg_addr_count() — matches LLVM AMDGPU::getAddrSizeMIMGOp on GFX10:
    • image_bvh_intersect_ray / image_bvh64_intersect_ray: return 8 / 9 VGPRs when a16=True (previously always 11 / 12).
    • image_msaa_load: no longer falsely matches "_l" inside "_load", removing a spurious +1 address VGPR (and spurious trailing v0 in NSA lists).
    • image_load_mip_pck, image_load_mip_pck_sgn, image_store_mip_pck, *_mip_pck2/4, *_mip_by2/4: include the mip VGPR (previously only exact image_load_mip / image_store_mip matched).
    • image_load_by2/by4 / image_store_by2/by4: no longer falsely match "_b" as bias.
    • image_sample_*cd* (all 16 coarse-derivative opcodes): tokenize underscore-delimited suffixes (parts = set(name.split("_")[2:])) so "cd" is counted as derivatives instead of falsely triggering z-compare "c" and dropping all derivative VGPRs.
    • image_sample_*_g16 (all 16 G16 opcodes): recognize "cl" and "o" before "g16", and pack 16-bit gradients as alignTo<2>(grad_coords) ((grad_coords + 1) & ~1: 2 / 2 / 4 VGPRs for 1D / 2D / 3D instead of 2 / 4 / 6).
    • image_get_lod: respects a16 coordinate packing.
    • Odd-coordinate a16 packing: computes (coords + has_lod_or_cl_or_mip + 1) // 2 (LLVM divideCeil(AddrComponents, 2)) instead of (coords + 1) // 2 + extra, fixing 1D/3D/2D-array/cube + lod/cl/mip overcounts by 1 VGPR.
  2. _mimg_data_count() — atomic tfe and cmpswap dmask:0x1:
    • Adds + (1 if tfe else 0) on image_atomic_* instead of returning early before TFE (fixing all 26 image_atomic_* ... tfe vectors in gfx10_mimg_features.txt).
    • image_atomic_cmpswap / image_atomic_fcmpswap return 2 VGPRs for dmask:0x1 (and 0x3) and 4 for dmask:0xf, matching LLVM (which has no 1-VGPR cmpswap variant).
  3. Non-BVH r128 resource width (rsrc_w):
    • Uses 4 SGPRs (SReg_128_XNULL) only for image_bvh*_intersect_ray and 8 SGPRs (SReg_256_XNULL) for all non-BVH MIMG instructions even when r128 is set, matching MIMGInstructions.td and gfx10_mimg.txt.

Adds test_mimg_addr_data_and_rsrc_widths_match_llvm in tests/test_rdna2_decode.py covering all fixed categories with hand-encoded GFX10/GFX1030 words (zero game data), round-trip checked through both decode_bytes() and disasm_bytes() → shader_ir.parse_line().

Tested

All synthetic CPU/stdlib/docs/CI checks on Windows 11 x64, Python 3.12.14, Node v24.19.0 (no game files, no llvm-mc installed locally):

  • python -I tests/test_rdna2_decode.py -v — 20/20 passed (1 skipped: llvm-mc not installed)
  • Cross-check against upstream LLVM GFX10/GFX1030 MIMG disassembler test vectors:
    • gfx1030_new.txt: 12/12 PASS (was 5/12, 7 mismatches)
    • gfx10_mimg.txt: 253/253 PASS (was 214/253, 39 mismatches)
    • gfx10_mimg_features.txt: 26/26 PASS (was 0/26, 26 mismatches)
    • Total: 291/291 PASS (72 mismatches → 0 mismatches)
  • python -I tests/test_ci_registration.py — 15/15 passed
  • python -I tests/test_runtime_paths.py — 5/5 passed
  • python -I scripts/check_no_game_data.py — passed (0 forbidden tracked extensions)
  • python -m compileall -q make_game.py tools tests — passed
  • python -I tests/test_text_write_line_endings.py, tests/test_report_paths.py, tests/test_i18n_index.py, and all tests/test_docs_*.py — passed
  • node --test tests/test_viewer_streaming.cjs — passed

GPUs tested: n/a (pure-Python ISA binary decoder / unit tests only)

AI-assisted: yes (JARVIX multi-model team: Antigravity + DeepSeek V4 Flash + NVIDIA NIM Nemotron-3.5-Lightning & Nemotron-3-Ultra-550B)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (N/A — binary ISA decoder parity with LLVM llvm-mc only; no SPIR-V shader-translator rule changed.)
**@⁠jarvixaiassistant-glitch** on GitHub · 2026-11-24 10:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/633) `jarvixaiassistant-glitch:fix/decode-mimg-llvm-parity` → `main` · +109 −44 · 2 files · 1 commits · merged 2026-11-25 21:21 UTC Claimed in the coordination issue as: DECODE-MIMG-PARITY ### What and why Fixes **72 MIMG disassembly mismatches** in the pure-Python RDNA2 decoder (`tools/rdna2_decode.py`) uncovered by cross-checking against all 291 `image_*` test vectors in upstream LLVM's official GFX10/GFX1030 disassembler suites (`gfx10_mimg.txt`, `gfx10_mimg_features.txt`, `gfx1030_new.txt`) and `AMDGPU::getAddrSizeMIMGOp` (`llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp`): 1. **`_mimg_addr_count()` — matches LLVM `AMDGPU::getAddrSizeMIMGOp` on GFX10:** - `image_bvh_intersect_ray` / `image_bvh64_intersect_ray`: return `8` / `9` VGPRs when `a16=True` (previously always `11` / `12`). - `image_msaa_load`: no longer falsely matches `"_l"` inside `"_load"`, removing a spurious `+1` address VGPR (and spurious trailing `v0` in NSA lists). - `image_load_mip_pck`, `image_load_mip_pck_sgn`, `image_store_mip_pck`, `*_mip_pck2/4`, `*_mip_by2/4`: include the mip VGPR (previously only exact `image_load_mip` / `image_store_mip` matched). - `image_load_by2/by4` / `image_store_by2/by4`: no longer falsely match `"_b"` as bias. - `image_sample_*cd*` (all 16 coarse-derivative opcodes): tokenize underscore-delimited suffixes (`parts = set(name.split("_")[2:])`) so `"cd"` is counted as derivatives instead of falsely triggering z-compare `"c"` and dropping all derivative VGPRs. - `image_sample_*_g16` (all 16 G16 opcodes): recognize `"cl"` and `"o"` before `"g16"`, and pack 16-bit gradients as `alignTo<2>(grad_coords)` (`(grad_coords + 1) & ~1`: `2 / 2 / 4` VGPRs for 1D / 2D / 3D instead of `2 / 4 / 6`). - `image_get_lod`: respects `a16` coordinate packing. - Odd-coordinate `a16` packing: computes `(coords + has_lod_or_cl_or_mip + 1) // 2` (LLVM `divideCeil(AddrComponents, 2)`) instead of `(coords + 1) // 2 + extra`, fixing 1D/3D/2D-array/cube + `lod`/`cl`/`mip` overcounts by 1 VGPR. 2. **`_mimg_data_count()` — atomic `tfe` and `cmpswap` `dmask:0x1`:** - Adds `+ (1 if tfe else 0)` on `image_atomic_*` instead of returning early before TFE (fixing all 26 `image_atomic_* ... tfe` vectors in `gfx10_mimg_features.txt`). - `image_atomic_cmpswap` / `image_atomic_fcmpswap` return `2` VGPRs for `dmask:0x1` (and `0x3`) and `4` for `dmask:0xf`, matching LLVM (which has no 1-VGPR `cmpswap` variant). 3. **Non-BVH `r128` resource width (`rsrc_w`):** - Uses `4` SGPRs (`SReg_128_XNULL`) only for `image_bvh*_intersect_ray` and `8` SGPRs (`SReg_256_XNULL`) for all non-BVH MIMG instructions even when `r128` is set, matching `MIMGInstructions.td` and `gfx10_mimg.txt`. Adds `test_mimg_addr_data_and_rsrc_widths_match_llvm` in `tests/test_rdna2_decode.py` covering all fixed categories with hand-encoded GFX10/GFX1030 words (zero game data), round-trip checked through both `decode_bytes()` and `disasm_bytes()` → `shader_ir.parse_line()`. ### Tested All synthetic CPU/stdlib/docs/CI checks on Windows 11 x64, Python 3.12.14, Node v24.19.0 (no game files, no `llvm-mc` installed locally): - `python -I tests/test_rdna2_decode.py -v` — 20/20 passed (1 skipped: `llvm-mc` not installed) - Cross-check against upstream LLVM GFX10/GFX1030 MIMG disassembler test vectors: - `gfx1030_new.txt`: 12/12 PASS (was 5/12, 7 mismatches) - `gfx10_mimg.txt`: 253/253 PASS (was 214/253, 39 mismatches) - `gfx10_mimg_features.txt`: 26/26 PASS (was 0/26, 26 mismatches) - **Total: 291/291 PASS (72 mismatches → 0 mismatches)** - `python -I tests/test_ci_registration.py` — 15/15 passed - `python -I tests/test_runtime_paths.py` — 5/5 passed - `python -I scripts/check_no_game_data.py` — passed (0 forbidden tracked extensions) - `python -m compileall -q make_game.py tools tests` — passed - `python -I tests/test_text_write_line_endings.py`, `tests/test_report_paths.py`, `tests/test_i18n_index.py`, and all `tests/test_docs_*.py` — passed - `node --test tests/test_viewer_streaming.cjs` — passed GPUs tested: n/a (pure-Python ISA binary decoder / unit tests only) AI-assisted: yes (JARVIX multi-model team: Antigravity + DeepSeek V4 Flash + NVIDIA NIM Nemotron-3.5-Lightning & Nemotron-3-Ultra-550B) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. *(N/A — binary ISA decoder parity with LLVM `llvm-mc` only; no SPIR-V shader-translator rule changed.)*
Cross-checking tools/rdna2_decode.py against all 291 GFX10/GFX1030 image_*
vectors in LLVM's disassembler suite (gfx10_mimg.txt, gfx10_mimg_features.txt,
gfx1030_new.txt) found 72 mismatches across three areas:

1. _mimg_addr_count():
   - Handle a16 on image_bvh_intersect_ray (8 VGPRs) and
     image_bvh64_intersect_ray (9 VGPRs), and on image_get_lod.
   - Stop substring-matching _l inside image_msaa_load, _b inside
     image_{load,store}_by{2,4}, and _c/_d inside image_sample_*cd*.
   - Include the mip VGPR for all *_mip* load/store variants
     (image_load_mip_pck, image_load_mip_pck_sgn, image_store_mip_pck, etc.).
   - Parse sample/gather modifiers as underscore-delimited tokens so _cl and
     _o before _g16 are seen, and pack _g16 gradients as alignTo<2>(grad_coords)
     matching LLVM AMDGPU::getAddrSizeMIMGOp.
   - Pack (coords + lod/cl/mip) under a16 as (packable + 1) // 2 so odd-coord
     dims (1D/3D/2D_ARRAY/CUBE) do not overcount by 1 VGPR.
2. _mimg_data_count():
   - Add +1 VGPR for tfe on image_atomic_* instead of returning early.
   - Return 2 VGPRs for image_atomic_{f,}cmpswap with dmask:0x1 (LLVM has no
     1-VGPR cmpswap variant).
3. MIMG rsrc_w:
   - Keep 8 SGPRs (SReg_256_XNULL) for non-BVH MIMG instructions even with
     r128; only image_bvh*_intersect_ray use 4 SGPRs (SReg_128_XNULL).

All 291/291 LLVM GFX10/GFX1030 MIMG vectors now decode with 0 mismatches.

AI-assisted: yes (JARVIX: Antigravity + DeepSeek V4 Flash + NVIDIA NIM Nemotron-3.5-Lightning & Nemotron-3-Ultra-550B).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:12:49 +00:00
github manually merged commit 450cd81054 into gh-pr/633/base 2026-11-26 09:12:51 +00:00
Author
Owner

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

CPU check of 450cd8105 merged onto main 3aa97542, with the llvm-mc run you didn't have (23.1.5). Random MIMG words, kept only where LLVM's text reassembles to the same bytes:

compared differ on main differ here
gfx1013 5,236 2,498 0
gfx1030 5,715 2,708 0

test_rdna2_decode.py: 20 passed. run_semantic_tests.py tests/semantic: 470 files / 6,168 tests, 0 failed. Not tested: GPU, game corpus. AI-assisted (Claude Code)

**@⁠thesubtleties** on GitHub · 2026-11-24 20:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/633#issuecomment-7143660403) CPU check of `450cd8105` merged onto main `3aa97542`, with the `llvm-mc` run you didn't have (23.1.5). Random MIMG words, kept only where LLVM's text reassembles to the same bytes: | | compared | differ on main | differ here | |---|---|---|---| | gfx1013 | 5,236 | 2,498 | 0 | | gfx1030 | 5,715 | 2,708 | 0 | `test_rdna2_decode.py`: 20 passed. `run_semantic_tests.py tests/semantic`: 470 files / 6,168 tests, 0 failed. Not tested: GPU, game corpus. AI-assisted (Claude Code)
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 21:21 UTC · original

Merged, thanks! The new test fails on main and passes with this. With #637 too, the translator suite passes.

  • Checked on main f5bc25cdc + 450cd8105: the semantic suite passes (491 files, 6,312 tests, 0 files failed); test_rdna2_decode pass; the 15 docs and writer tests pass; check_no_game_data.py is clean on the 2 changed files.

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

**@⁠aslan2142** on GitHub · 2026-11-25 21:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/633#issuecomment-7174963928) Merged, thanks! The new test fails on `main` and passes with this. With #637 too, the translator suite passes. - **Checked on `main` `f5bc25cdc` + `450cd8105`:** the semantic suite passes (491 files, 6,312 tests, 0 files failed); `test_rdna2_decode` pass; the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 2 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.