Skip to content

rdna2_decode: print a literal word in hex, so it stays apart from an inline constant - #5698

Open
0klavair wants to merge 1 commit into
ItsNotRexed:mainfrom
0klavair:tools/rdna2-decode-f64-literal-hex
Open

0klavair wants to merge 1 commit into
ItsNotRexed:mainfrom
0klavair:tools/rdna2-decode-f64-literal-hex

Conversation

@0klavair

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #5361 (comment) (from @tupacfan04's finding; offered to the finder first)

What and why

tools/rdna2_decode.py loses the literal marker of a source operand with code 255 (_src, line 223), and _format_operand prints any value below 100 in decimal. A small literal word, for example the f64 literal 1, is therefore printed exactly like the inline constant 1: v_add_f64 v[4:5], 1, 1 instead of v_add_f64 v[4:5], 0x1, 1. The disassembly then loses the literal/inline distinction that shader_ir and the translator read from it.

The change

  • _src: a code-255 operand keeps "literal": True.
  • _format_operand: an operand with that marker is printed in hex. Other immediates are unchanged.
  • tests/test_rdna2_decode.py:
    • new test_f64_literal_printed_as_hex, built from hand-written bytes 040064d5ff02010001000000;
    • test_sop1_sop2_sopc_sopk_with_literals now expects the marker on its literal operand ({"k": "imm", "v": 0x12345678, "literal": True}). That is the only change to the operand dict's shape.

Tested

Windows 11 23H2, Python 3.12, at main + this commit:

  • pytest tests/test_rdna2_decode.py: 14 passed, 1 skipped (llvm-mc parity, TEST-1010). With the tool change reverted and the tests kept, 2 fail (the new test and the updated dict), as expected.
  • pytest tests/test_shader_disasm.py tests/test_shader_ir.py (the decoder's consumers): 66 passed.
  • tests/semantic, the *decode* modules: 8 passed.

No game files. AI-assisted: written by an agent on this machine, and checked and re-run by me.

…inline constant

_src dropped the literal marker for source code 255, and _format_operand printed
any value under 100 in decimal, so a small f64 literal came out as an inline
constant would (`v_add_f64 v[4:5], 1, 1` instead of `0x1, 1`). The disassembly then
lost the literal/inline distinction shader_ir and the translator read from it.

The operand keeps "literal": True and is printed in hex. New test
test_f64_literal_printed_as_hex; test_sop1_sop2_sopc_sopk_with_literals now expects
the marker on its literal operand.

Found by @tupacfan04.
@kapitche

kapitche commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested 4c4c6ea merged with main 561b65a (PR already based on it) on Linux WSL2, Python 3.12.16: pass

tests/test_rdna2_decode.py: 15 run, 14 passed, 1 skipped, 0 failed. tests/test_shader_disasm.py (the other consumer of rdna2_decode): 29 run, 29 passed.

Negative control: with tools/rdna2_decode.py restored to upstream/main, tests/test_rdna2_decode.py runs 15 tests with 2 failures (the new hex test and the updated imm-operand expectation).

Read the diff: the new "literal": True key on a decoded 255-code operand matches what tools/rdna2spirv.py already reads via .get("literal"), so it is the existing convention. Note the new test only asserts the substring '0x1' in the first line. No failure found.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Related data, in case you want LLVM-exact output: llvm-mc 23.1.3 doesn't print every literal in hex — it prints lit(0x…) when the literal's value is also an inline constant for that operand type (AMDGPUDisassembler::decodeLiteralConstant / AMDGPUInstPrinter at llvmorg-23.1.3), and on f64 sources only for 0 and the inline doubles' high words (ff 5e 00 7e 01 00 00 00 → v_rcp_f64_e32 v[0:1], 0x1). Brute-forced against llvm-mc --disassemble over every literal-capable source in the decoder tables × 28 values: the lit()-or-hex choice can match on all 39,064 gfx1030 / 38,524 gfx1013 instructions. One deliberate deviation worth keeping: LLVM prints a non-lit literal on a 16-bit operand as its low half, which breaks v_fma_mix through the python backend, so keeping the full dword there is safer. #5832 (shader_ir reads lit(…)) is the parser side. AI-assisted (Claude Code).

@0klavair

Copy link
Copy Markdown
Contributor Author

@cynbun thanks, that's very useful data. I'll keep this PR to plain hex for now. It is unambiguous, shader_ir already reads it, and it fixes the finding (a small literal no longer prints like an inline constant). Printing lit(0x…) exactly where llvm-mc does belongs with #5832, since shader_ir must read it first. Once that lands, switching the code-255 case to your lit()-or-hex rule (and keeping the full dword on 16-bit operands, as you say) is a small follow-up. Happy to review it or to do it on top of #5832 if you'd rather not. @kapitche, thanks for the Linux run. AI-assisted.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Sounds right — plain hex here, and the LLVM-exact lit()-or-hex rule as a follow-up once #5832 lands. We have that change ready (rule from decodeLiteralConstant/AMDGPUInstPrinter at llvmorg-23.1.3, brute-forced against llvm-mc --disassemble, full dword kept on 16-bit operands), so I'll open it on top of this PR and #5832 after they merge, and tag you for review. 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