Skip to content

shader_ir: read LLVM's lit(…) operand as the literal it encodes - #5832

Open
cynbun wants to merge 3 commits into
ItsNotRexed:mainfrom
cynbun:shader-ir-lit-literal
Open

cynbun wants to merge 3 commits into
ItsNotRexed:mainfrom
cynbun:shader-ir-lit-literal

Conversation

@cynbun

@cynbun cynbun commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Item: lit() operand gap in tools/shader_ir.py (claimed in #5581: #5581 (comment)).

Cause

LLVM's disassembler prints a 32-bit literal dword as lit(0x...) when its value would also be an inline constant for that operand, so the text reassembles to the same bytes. tools/shader_ir.py operand() (origin/main 561b65a, the "(" in core fallback at line ~139) turned that into {'k': 'special', 't': 'lit(0x40100000)'}, and the translator rejected the instruction (Unsupported operand lit(0x3f800000), Unsupported v_rcp_f64 operand).

Retail reachability

None found. @Kev-Ball counted all 26,503 gfx1013 disassembly listings of the Xbox 1.0.2.1 shader archive and found no lit( or lit64( spelling. Those listings were written by llvm-mc 23.1.5, which prints lit(0x3f800000) and lit(0x40100000) for the two example byte sequences below. So the zero means no decoded retail instruction carries a literal that shadows an inline constant. It covers decoded instructions only: 1,473 listings have at least one invalid word. This PR changes no translation of those listings. It fixes hand-written text (the fixtures below, or a test program disassembled with llvm-mc), not a retail shader the translator rejected. The docs/design.md row says so.

Where LLVM prints lit()

Measured with llvm-mc 23.1.3, -mcpu=gfx1030. I disassembled bytes with the source field 0xff and a chosen literal dword, for 26 instruction forms × 16 values (VOP1/VOP2/VOP3/VOPC/VOP3P/SOP1/SOP2/SOPC, f16/f32/f64/u16-packed/32- and 64-bit integer):

operand lit() for example (bytes → text)
32-bit (f32 or integer) 0..64, 0xfffffff0..0xffffffff, the f32 inline floats ff 02 00 06 00 00 80 3f → v_add_f32_e32 v0, lit(0x3f800000), v1
f16 / packed 16-bit the same values plus the f16 inline floats ff 16 06 7e 00 38 00 00 → v_cvt_f32_f16_e32 v3, lit(0x3800)
64-bit integer 0..64 only ff 10 c2 7d 40 00 00 00 → v_cmp_lt_u64_e32 vcc_lo, lit(0x40), v[8:9]
f64 0, and the high words of the inline doubles ff 5e 00 7e 00 00 10 40 → v_rcp_f64_e32 v[0:1], lit(0x40100000)
K of v_fmamk/fmaak_f32 never
  • VOP3 modifiers print as -lit(..), |lit(..)| and -|lit(..)|, for example v_fma_f32 v1, -lit(0x40000000), v5, -|lit(0x40000000)|.
  • The disassembler never printed lit64( for gfx10.
  • The value inside is always hex. The assembler does accept lit(4) and lit(1.0).
  • llvm-mc 23.1.3 cannot reassemble -lit(..) or |lit(..)|. It also turns |lit(0x40000000)| into the inline |2.0|. Because of this, the fixtures take their text from disassembly, not from assembly.

Fix

operand() now reads lit(<hex|decimal>) as the same imm a plain 0x... literal gives, with "literal": True added.

  • f64 sources: parse_line treats that flag like a hex spelling when it marks f64_literal (the "f64 literal sources" row). This means lit(0x40100000) on v_rcp_f64 / v_add_f64 / v_cvt_f32_f64 is bits 63:32 with a zero low word.
  • Existing hooks: compare_f64_cond and cmp_class_f64 already checked sv.get("literal"), but nothing set it until now.
  • Two more range-based f64 readers: cmp_class (the f64 branch) and v_ldexp_f64 now check the flag too.
  • Modifiers: -, |..|, -|..| and neg(lit(..)) set the sign-bit neg/abs flags, as neg(0x..) already does. They are not integer negation.
  • Left unparsed: lit64(..), a float inside lit() and a value past 32 bits stay special, so they remain Unsupported.

On every operand above, the literal gives the same value as the inline constant it shadows, with one exception: an f32 bit pattern on an f16 operand. There, lit(0x3f800000) reads its low half, +0.0, not 1.0. The translator already reads an imm as raw bits, so this falls out of the fix.

Tests

  • tests/semantic/test_lit_literal.py with two fixtures. Every line in them is llvm-mc 23.1.3 disassembly, and each was checked to round-trip from the bytes in its comment.
    • tests/semantic/fixtures/lit_literals.asm, executed in the interpreter: f32 VOP2, f32 VOP3 with -lit/-|lit|, SOP1/SOP2, 64-bit SOP2 and VOPC, integer VOP3 with the literal in two sources, and f16. Expected words: 0x40800000 0xc1000000 0x64 0x3f000000.
    • tests/semantic/fixtures/lit_f64_literals.asm: v_rcp_f64, v_add_f64 with -lit, v_cvt_f32_f64 with lit(0x0) and -|lit|, and v_cmp_gt_f64. The test checks the source words in the SPIR-V. The interpreter cannot run Float64.
    • The test also covers the operand dicts, including lit64, oversized and float spellings staying special.
  • On main: 3 failures and 2 errors (Unsupported operand lit(0x3f800000), Unsupported v_rcp_f64 operand). With this PR it passes.
  • spirv-val: both translated fixtures pass (translator CLI: 2 translated, 2 pass).
  • GPU: NVIDIA GeForce RTX 5080, driver 615.78.08, Linux, wave32, vk_harness with vs_ids. Both fixtures gave the expected words on all 4096 pixels: 0x40800000 0xc1000000 0x64 0x3f000000 and 0x0 0xbf800000 0x3e800000 0xbf400000.
  • python -m compileall -q make_game.py tools tests is clean. These pass: tests/test_shader_ir.py, tests/test_shader_meta.py, tests/test_fma_mix_literal_probe.py, tests/test_semantic_translator_use.py, tests/semantic/test_vop3_const_mods.py, tests/semantic/test_fma_mix_literal.py.
  • Full python -m unittest discover -s tests/semantic: 6156 tests OK (skipped=1, expected failures=1).
  • tests/test_rdna2_decode.py test_parity_with_llvm_mc_on_shader_fixtures fails the same way on main and on this branch: llvm-mc 23 prints op_sel:[0,1] and exp null differently. Its norm_arg compares imm by value, so lit() operands now match the decoder's imm.
  • docs/design.md has a new contract row, "lit() literals", placed after "f64 literal sources".

Seen, not changed here

  • tools/rdna2_decode.py _format_operand, the --backend python disassembly, prints a literal below 100 in decimal. For example, ff 5e 00 7e 01 00 00 00 becomes v_rcp_f64_e32 v[0:1], 1, which reads back as the inline 1 (LLVM prints 0x1). So that backend loses the literal/inline difference on f64 sources.
  • parse_line marks f64_literal only for the f64 ops it lists. A plain 0x1 literal on v_cmp_*_f64 or v_cmp_class_f64 (v_cmp_lt_f64_e32 vcc_lo, 0x1, v[0:1]) is still read as the inline 1, because those readers decide by value. Both cases are small values that are unlikely in compiled code. The same count found no v_cmp*_f64 instruction at all, class compares included, in the retail listings.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@Kev-Ball

Copy link
Copy Markdown

Checked the two retail reachability questions against all 26,503 stored gfx1013 disassembly listings from my Xbox 1.0.2.1 main shader archive. I found zero lit(...) or lit64(...) spellings. I also counted decoded v_cmp*_f64 instructions (including class compares) and found zero, so this corpus supplies no example of the small f64 compare literal mentioned under “Seen, not changed here.”

25,030 listings reported a clean disassembly and 1,473 reported at least one invalid word. These are counts over emitted listing text for the project's gfx1013 path, not a conclusion about undecoded words or other LLVM targets. CPU-only on CachyOS, AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 7c0aca1 on Intel UHD Graphics 630 (CFL GT2), Mesa ANV, subgroup 8..32: I translated the lit_literals and lit_f64_literals fixtures with the PR's tools, validated them with spirv-val (rc 0) and rendered them with vk_harness at 64x64. All 4096 pixels of ps_lit_literals are 0x40800000 0xc1000000 0x64 0x3f000000 and all 4096 pixels of ps_lit_f64_literals are 0x0 0xbf800000 0x3e800000 0xbf400000. test_shader_ir (37 tests), test_shader_meta (27), test_fma_mix_literal_probe (8), test_semantic_translator_use (3), test_vop3_const_mods (12), test_fma_mix_literal (7) and test_lit_literal (6) pass. I did not reproduce the failures on main or run the full tests/semantic suite.

AI-assisted.

@DenRakEiw

DenRakEiw commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Reviewed commit 7c0aca1cec28 (trial-merged cleanly onto origin/main at 561b65a83, git diff --check clean) on Windows 11 x64 (Python 3.12.10, Vulkan SDK 1.4.363.0):

  • Affected semantic and unit tests (python -m unittest tests/semantic/test_lit_literal.py tests/semantic/test_fma_mix_literal.py tests/semantic/test_valu_fma_mix.py tests/semantic/test_vop3_const_mods.py tests/test_shader_ir.py tests/test_shader_meta.py tests/test_shader_meta_operands.py tests/test_fma_mix_literal_probe.py tests/test_semantic_translator_use.py -v): 120/120 passed (including all 6 new cases in test_lit_literal.py).
  • SPIR-V assembly and validation (spirv-as --target-env vulkan1.2 + spirv-val --target-env vulkan1.2) on the translated ps_lit_literals (lit_literals.asm) and ps_lit_f64_literals (lit_f64_literals.asm) fixtures: both exited 0 with no warnings or errors.
  • Code inspection: shader_ir.operand() parses lit(0x...) / lit(...) up to 32-bit unsigned with VOP3 -/| sign-bit modifiers and marks literal: True, leaving lit64(...), floats, and >32-bit values as special; parse_line() and both 64-bit constant sites in rdna2spirv.py propagate literal so inline-range literal dwords (e.g. lit(0x0), lit(0x40100000)) remain high-word f64 literals rather than being treated as 64-bit inline integers.

AI-assisted (Gemini 4 Neon).

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @Kev-Ball, @gertify and @DenRakEiw.

@Kev-Ball's count. No retail listing carries lit(, so this PR fixes hand-written text, not a retail rejection. The PR didn't say otherwise, but it also didn't say so. New head 6d2754c52 adds one sentence to the docs/design.md "lit() literals" row, linking your count: on those listings the rule changes no translation, and it covers hand-written text such as the fixtures. The PR body has a new "Retail reachability" section. Its "Seen, not changed here" note now also says your count found no v_cmp*_f64 at all. No code changed.

One question about the count: which llvm-mc version wrote those listings? The build records it. I checked that llvm-mc 23.1.3 prints lit() with -mcpu=gfx1013 as well:

  • ff 02 00 06 00 00 80 3f → v_add_f32_e32 v0, lit(0x3f800000), v1
  • ff 5e 00 7e 00 00 10 40 → v_rcp_f64_e32 v[0:1], lit(0x40100000)

So if your listings come from a version that prints lit(), zero means the retail bytes have no literal that shadows an inline constant. If they come from an older version that printed these as plain 0x..., the zero only describes that text. I'd then reword the row to give the version.

@gertify (Intel UHD 630, ANV) and @DenRakEiw (Windows 11). Thanks. ANV matches the RTX 5080 words on all 4096 pixels for both fixtures, and your Windows unit run and spirv-val are clean.

Tests after the docs edit: python -I tests/test_docs_*.py (all 11) and tests/semantic/test_lit_literal.py pass.

AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

@cynbun The listings were produced with llvm-mc 23.1.5, target gfx1013 on CachyOS. They're the same 26,503 listings as the #4686 census, whose run record includes the LLVM version. I also ran your two synthetic byte sequences through /usr/bin/llvm-mc --disassemble -triple=amdgcn-amd-amdhsa -mcpu=gfx1013 -show-encoding here. Version 23.1.5 prints lit(0x3f800000) for the f32 add and lit(0x40100000) for the f64 reciprocal.

That confirms the disassembler used for the retail count emits the spelling on both positive controls. The zero applies to decoded instructions; 1,473 listings had at least one invalid word. AI-assisted.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @Kev-Ball. That settles it: llvm-mc 23.1.5 prints lit() on both of these byte sequences, so your zero is a real zero for decoded instructions.

New head 59330bf86 makes the docs/design.md row and the PR body's "Retail reachability" section say exactly that, citing your comment, including the caveat that 1,473 listings have at least one invalid word. Docs only; python -I tests/test_docs_*.py passes.

AI-assisted (Claude Code).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 7c0aca1ce on Windows 11 / Python 3.13.

  • python tests/semantic/test_lit_literal.py: 6/6 OK.
  • Negative control: the same test copied onto main 561b65a83 gives 3 failures + 2 errors (Unsupported operand lit(0x3f800000), Unsupported v_rcp_f64 operand), as described.
  • pytest tests/test_shader_ir.py tests/test_shader_meta.py tests/test_fma_mix_literal_probe.py tests/test_semantic_translator_use.py -q: 75 passed, 79 subtests passed.
  • Checked operand parsing: lit(hex|decimal) becomes the same imm as a plain literal with literal: true; -lit, |lit|, -|lit| and neg(lit(...)) set the existing sign-bit flags; lit64, float spellings and values past 32 bits stay special. The f64 readers use the literal high word and the interpreter checks the expected words.

Verdict: LGTM. The GPU claim is external to the PR (no GPU fixture is added); the CPU/IR evidence is solid.

@larpinguser larpinguser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

tools/shader_ir.py now parses LLVM's lit(0x...) operand as the literal imm (tagged literal so 64-bit sources distinguish it from an inline constant), with -lit, |lit|, -|lit|, neg(lit) as sign-bit modifiers; lit64(), floats and >32-bit values stay Unsupported. tools/rdna2spirv.py and tools/fma_mix_literal_probe.py are adjusted. Adds tests/semantic/test_lit_literal.py plus two llvm-mc fixtures and a docs/design.md row.

Why merge

Stops the translator rejecting hand-written/disassembled lit() operands (Unsupported operand lit(...)) that LLVM emits when a literal shadows an inline constant, without changing any retail translation.

Verdict: Approve

@larpinguser

Copy link
Copy Markdown

Summary

tools/shader_ir.py now parses LLVM's lit(0x...) operand as the literal imm (tagged literal so 64-bit sources distinguish it from an inline constant), with -lit, |lit|, -|lit|, neg(lit) as sign-bit modifiers; lit64(), floats and >32-bit values stay Unsupported. tools/rdna2spirv.py and tools/fma_mix_literal_probe.py are adjusted. Adds tests/semantic/test_lit_literal.py plus two llvm-mc fixtures and a docs/design.md row.

Why merge

Stops the translator rejecting hand-written/disassembled lit() operands (Unsupported operand lit(...)) that LLVM emits when a literal shadows an inline constant, without changing any retail translation.

Verdict: Approve

cynbun and others added 3 commits November 23, 2026 15:49
llvm-mc prints a 32-bit literal dword whose value is also an inline constant
of the operand as lit(0x...) (e.g. ff 5e 00 7e 00 00 10 40 is
v_rcp_f64_e32 v[0:1], lit(0x40100000)). operand() fell through to a
"special" operand and the translator rejected the instruction.

Read lit(<hex|decimal>) as the imm a plain literal gives, marked "literal":
parse_line marks it f64_literal (bits 63:32 of an f64), the f64 compare and
class readers already honour the flag, and cmp_class / v_ldexp_f64 now do
too. -lit(..), |lit(..)|, -|lit(..)| and neg(lit(..)) are sign-bit modifiers.
lit64(), a float or a value past one dword stay unsupported.

Fixtures are llvm-mc 23.1.3 gfx1030 disassembly; the f32/int/f16 one is
executed, the f64 one checked on its SPIR-V source words. Both also gave the
expected words on an RTX 5080 (NVIDIA 615.78.08, Linux). docs/design.md
gets a "lit() literals" contract row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kev-Ball counted all 26,503 gfx1013 disassembly listings of the Xbox
1.0.2.1 shader archive and found no lit( or lit64( spelling, so the
row now says the rule changes no translation of those listings and is
there for hand-written text such as its fixtures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kev-Ball's listings were written by llvm-mc 23.1.5, which prints lit()
for the row's two example byte sequences, so their zero means no
decoded retail instruction carries a literal shadowing an inline
constant. The zero covers decoded instructions only: 1,473 listings
have at least one invalid word.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

6 participants