Skip to content

rdna2_decode: a MUBUF load or store with tfe has one more data VGPR, the status dword - #7260

Open
hmodymac8-droid wants to merge 2 commits into
ItsNotRexed:mainfrom
hmodymac8-droid:fix/rdna2-decode-buffer-tfe
Open

hmodymac8-droid wants to merge 2 commits into
ItsNotRexed:mainfrom
hmodymac8-droid:fix/rdna2-decode-buffer-tfe

Conversation

@hmodymac8-droid

Copy link
Copy Markdown

What changed and why

TFE makes a buffer access return a status dword after the data, so llvm-mc prints the data range one VGPR longer.
tools/rdna2_decode.py printed it without that dword, so its listing disagreed with llvm-mc's, and anything that reads register ranges from the listing saw one VGPR fewer:

bytes llvm-mc 23.1.6 rdna2_decode on main
c0000ce00000803f buffer_load_format_xyzw v[0:4], off, s[0:3], s63 offset:192 tfe v[0:3]
000038e000018080 buffer_load_dwordx4 v[1:5], off, s[0:3], 0 tfe v[1:4]
00000ce200018080 buffer_load_format_d16_xyzw v[1:3], off, s[0:3], 0 tfe v[1:2]
000090e000018080 buffer_load_short_d16 v[1:2], off, s[0:3], 0 tfe v1
000070e000018080 buffer_store_dword v[1:2], off, s[0:3], 0 tfe v1

MIMG already counts the status dword (_mimg_data_count). The MUBUF path now adds it too.
An atomic with tfe is an invalid encoding to llvm-mc, so it is left as it was. MTBUF with tfe is likewise invalid there and is unchanged.
The translator refuses tfe anyway, so translation is not affected; this is about the listing matching llvm-mc.

Found by decoding random MUBUF words with both rdna2_decode and llvm-mc (-mcpu=gfx1013) and comparing.

Claim: #7231 (new item).

Test

python -I -m unittest tests.test_rdna2_decode: OK (1 skipped without LLVM_MC).
The new test_buffer_tfe_counts_the_status_dword checks the five rows above plus a load and a store without tfe, which are unchanged. Five of its cases fail on main and all pass with this change.

GPU and driver

No GPU needed.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…the status dword

TFE makes a buffer access return a status dword after the data, so llvm-mc
prints the data range one VGPR longer: `buffer_load_format_xyzw v[0:4] ...
tfe` (bytes c0000ce00000803f), `buffer_load_dwordx4 v[1:5] ... tfe`,
`buffer_store_dword v[1:2] ... tfe`. rdna2_decode printed the range without
it (v[0:3], v[1:4], v1), so its listing disagreed with llvm-mc's and anything
reading register ranges from it saw one VGPR fewer. MIMG already counts it
(_mimg_data_count); the MUBUF path now adds it too. An atomic with tfe is an
invalid encoding to llvm-mc and is left as it was; MTBUF with tfe is
likewise invalid there and unchanged.

Found by decoding random MUBUF words with both rdna2_decode and llvm-mc
23.1.6 (-mcpu=gfx1013) and comparing. All seven rows of the new test are
what llvm-mc prints for those bytes.

tests/test_rdna2_decode.py: test_buffer_tfe_counts_the_status_dword (five
of its cases fail on main, all pass with this change); the file: OK.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tikD1337

Copy link
Copy Markdown
Contributor

Review of #7260: rdna2_decode: a MUBUF load or store with tfe has one more data VGPR, the status dword

Environment: Windows 11, CPython 3.13. The checks ran without game files.

What changed

tools/rdna2_decode.py, MUBUF branch (hi6 == 0x38): the tfe bit is read as (w1 >> 23) & 1, and it already added the tfe modifier. It did not widen the data operand. Now, when tfe is set and the opcode is not buffer_atomic*, the VDATA range gets one more VGPR. The decoder then prints what llvm-mc prints: v[0:4] for an xyzw load, v[1:2] for a dword store. This also fixes a real disagreement between the two input paths. Text from llvm-mc already carried the wider range, but the binary decoder gave n = 4. Any range-based analysis therefore saw different registers depending on where the disassembly came from.

The change matches what the MIMG path already does: _mimg_data_count (tools/rdna2_decode.py:394-412) returns n + (1 if tfe else 0). The two paths now count the status dword the same way.

Checking the expected widths by hand:

  • buffer_load_format_xyzw tfe: 4 + 1 = v[0:4] ✔
  • buffer_load_dwordx4 tfe: 4 + 1 = v[1:5] ✔
  • buffer_load_format_d16_xyzw tfe: 2 packed + 1 = v[1:3] ✔ (the +1 comes after the d16 width from the table, which is right)
  • buffer_load_short_d16 tfe: 1 + 1 = v[1:2] ✔
  • buffer_store_dword tfe: 1 + 1 = v[1:2] ✔
  • Without tfe, the store and load rows keep v1 ✔

Atomics are left out because llvm-mc treats atomic + tfe as an invalid encoding, so there is no reference output to match. That is a sensible choice.

Downstream effect

The translator lowers no MUBUF op with tfe: rdna2spirv.py:2094, 2171, 2223, 3107, 3452 and 3992 all raise Unsupported on tfe. So this does not change any SPIR-V. It only changes the register-range analyses (vgpr_dest_registers, read_before_write_vgprs), which now agree between the binary path and the llvm-mc text path.

Points for the author

  1. MTBUF has the same bit and is not touched. The MTBUF branch (rdna2_decode.py:~1153) decodes tfe and appends the tfe modifier, but still emits _vreg(vdata, w) without the extra register. Please run llvm-mc on one tbuffer_load_format_xyzw … tfe encoding:

    • If it prints v[0:4], the same +1 belongs in that branch.
    • If it prints v[0:3], add that row to the test so the difference is pinned down on purpose.

    Either way, a tbuffer row in test_buffer_tfe_counts_the_status_dword would close this gap. This is a hypothesis: I did not check how LLVM handles TFE variants for MTBUF.

  2. Pre-existing, but it now affects the binary path too: interp_copy_sources (rdna2spirv.py:16838) calls vgpr_dest_registers(ins, extra=int("tfe" in ins["mods"])). The data range already includes the status dword: on the llvm-mc text path, on MIMG through _mimg_data_count, and after this PR on binary MUBUF. So that extra counts one register too many. The error is on the safe side: an extra recorded write can only reject a v0/v1 copy chain, never accept a wrong one. It could still be dropped, or limited to forms whose range leaves out the status dword. This is a possible follow-up finding:, not a blocker.

  3. Edge case, no action needed: lds + tfe together is not covered by a test. No compiler is known to emit it, so this is only for completeness.

Test results

These were already run by the harness; I did not re-run them:

  • git diff --check, compileall, check_no_game_data.py and make_game.py --help all pass.
  • python -I tests/test_rdna2_decode.py: 17 tests, OK, 1 skipped.
  • The new test_buffer_tfe_counts_the_status_dword passes all 7 subtests. The expected text is parsed with shader_ir.parse_line, so the comparison is on structure (op/args/mods), not on strings.
  • test_parity_with_llvm_mc_on_shader_fixtures was skipped because llvm-mc is not installed on this machine. The expected rows are stated to be llvm-mc 23.1.6 output. That is the right kind of reference, but CI with llvm-mc available should confirm it.

Project rules

  • No game data: the fixtures are seven hand-built 8-byte MUBUF encodings with synthetic registers. Nothing is carved from the corpus.
  • No keys or decryption, nothing online: not applicable to this change.
  • Fix, don't disable: this corrects decoder parity with llvm-mc and adds no skip list. It adds no translator lowering rule, so no new row in the docs/design.md contract table is needed. The llvm-mc version given as the reference is enough confirmation.
  • Conventions: standard library only, and the comment in the code says why the change is there.

Verdict: Approve. The change is correct for MUBUF and well tested. I'd like the MTBUF question (point 1) settled before merge, either with the same fix or with a test row showing that MTBUF differs.

AI-assisted.

…sts, apart from the other open rdna2_decode PRs

Each of the open rdna2_decode PRs added its test at the same spot, so they
conflicted with each other. No change to the test itself.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AsgerCode

Copy link
Copy Markdown

Tested 007d10f on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files, no llvm-mc): pass

The earlier review checked the five rows by hand and did not run the tests. Here I ran them, did a negative control, swept every MUBUF opcode with hand-assembled words, and checked the overlap with the other open decoder PRs.

What I ran:

  • python -I tests/test_rdna2_decode.py: 17 tests OK, 1 skipped (LLVM_MC).
  • Tests that use the decoder or MUBUF tfe (python -I -m unittest discover -s tests/semantic -p <file>): test_image_sample_bias_nonps 7 OK (2 skipped), test_buffer_load_sub 9 OK (this includes the translator's Unsupported for tfe), test_vector_mem_mubuf 53 OK, test_mem_crash_forms 1 OK, test_printed_encoding 7 OK.
  • Negative control (main's tools/rdna2_decode.py): the five tfe subtests fail, and the two rows without tfe pass.
  • Merge with current main f25ba44: clean. The branch is based on that commit.
  • Hand-assembled sweep: I wrote a separate MUBUF encoder from the RDNA2 field layout (OP in w0[24:18] plus bit 25, VDATA in w1[15:8], SLC in w1[22], TFE in w1[23]). I wrote down each op's data dword count from its name, not from rdna2_isa_tables. Coverage: 40 load/store ops (dword, byte/short, format, _d16, _d16_hi, packed format_d16_*) and 7 atomics, each with 3 address forms (off; offen; idxen offen glc slc offset:4095) and tfe 0 and 1. The rule in the PR is "a status dword after the data". With the PR, the VDATA range is data + 1 for every non-atomic with tfe, and atomics do not change. The other operands do not change, mods gains only tfe, and format_insn text parses back to the same op/args/mods with shader_ir.parse_line. main gives 120 mismatches: every non-atomic tfe case.
bytes tfe PR prints
00000ce0030a8280 1 buffer_load_format_xyzw v[10:14], off, s[8:11], 0 tfe
00000ce2030a8280 1 buffer_load_format_d16_xyzw v[10:12], off, s[8:11], 0 tfe
000070e0030a0280 / 000070e0030a8280 0 / 1 buffer_store_dword v10, ... / v[10:11], ... tfe

Not cross-checked without llvm-mc:

  • The store rows. The "status dword after the data" rule covers loads. For a store, the extra VGPR is supported only by the llvm-mc 23.1.6 rows in the PR.
  • lds with tfe: the PR prints buffer_load_dword v[10:11], off, s[8:11], 0 lds tfe. main prints v10 there.
  • That MTBUF and atomics with tfe are invalid to llvm-mc, as the PR body says.
  • test_parity_with_llvm_mc_on_shader_fixtures, which was skipped.

Overlap with other open PRs:

AI-assisted (Claude Code).

@hmodymac8-droid

Copy link
Copy Markdown
Author

Thanks @tikD1337 and @AsgerCode for the reviews.

MTBUF + tfe (point 1): llvm-mc 23.1.6 (conda-forge llvm-tools, Windows) has no reference output for it on either target. I took the encodings of three tbuffer ops and set w1 bit 23:

word gfx1013 gfx1030
tbuffer_load_format_xyzw + tfe 00006bea00008080 invalid instruction encoding invalid instruction encoding
tbuffer_load_format_x + tfe 0000b0e800018080 invalid instruction encoding invalid instruction encoding
tbuffer_store_format_x + tfe 0000b4e800018080 invalid instruction encoding invalid instruction encoding

Assembling tbuffer_load_format_xyzw v[0:4], off, s[0:3], 0 format:[...] tfe is also refused. So there is no llvm-mc width to match, and MTBUF stays as it was, as the description says. rdna2_decode still accepts those words where llvm-mc refuses them; that's a separate parity point and outside this PR.

Stores (AsgerCode's note): llvm-mc 23.1.6 prints buffer_store_dword v[1:2], off, s[0:3], 0 tfe for 000070e000018080, which is the store row in the test.

#7044 digests: agreed. The output change is intended, so whichever of #7044 and this PR lands second regenerates them with --print-digests.

interp_copy_sources extra (point 2): good catch. It's on the safe side as you say, so I've left it out of this PR.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants