Skip to content

shader_ir: read LLVM 23's lit(V) operand as the constant V - #6808

Merged
shayderrr merged 2 commits into
ItsNotRexed:mainfrom
FIocker:fix/llvm23-lit-operand
Nov 23, 2026
Merged

shayderrr merged 2 commits into
ItsNotRexed:mainfrom
FIocker:fix/llvm23-lit-operand

Conversation

@FIocker

@FIocker FIocker commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #5781 (comment).

What

llvm-mc 23.1.5 prints some literals as lit(V), for example v_fma_mix_f32 v139, lit(0x42200000), s104, neg(0) op_sel_hi:[0,1,0]. It does this when the literal could also be shown as an inline constant: an f32 v_fma_mix* source, or a literal whose low half is an inline f16 constant. shader_ir.operand() returned {"k": "raw"} for that form, so translate() refused the program with "operand lit(...)".

lit(V) now parses as V. A - in front of it is the VOP3 neg modifier, so -lit(V) reads as neg(V), not integer negation. Nothing else changes: LLVM 23 still re-encodes only the literal's low half, and mix_literal() already restores the full literal from the blob in that case.

LLVM 21.1.8 is worse on the same bytes. It prints the inline constant of the low half and consumes 8 bytes instead of 12, dropping the literal word:

bytes 8b 00 20 cc ff d0 00 92 00 00 20 42
21.1.8  v_fma_mix_f32 v139, 0, s104, neg(0) op_sel_hi:[0,1,0]                 ; encoding: [0x8b,0x00,0x20,0xcc,0x80,0xd0,0x00,0x92]
23.1.5  v_fma_mix_f32 v139, lit(0x42200000), s104, neg(0) op_sel_hi:[0,1,0]   ; encoding: [...,0xff,0xd0,0x00,0x92,0x00,0x00,0x00,0x00]

With LLVM 21 the printed-encoding check refuses such programs; with LLVM 23 they now translate.

Where it matters

The pipeline library in shaders.rpf (docs/shader_library.md) has 5,733 such operands in 2,809 programs. None appear in the carved FSSM corpus, whose LLVM 23 runs are already at 26,503/26,503.

On the 2,809 programs, disassembled with llvm-mc 23.1.5 for gfx1013 (counts only):

main 561b65a8 this PR
translate and pass spirv-val 0 1,496
refused with "operand lit(...)" 1,520 0

The rest stop at other refusals, mostly the FSPX wave32 check (#6809, s_bfm_b64 in wave32 code) and undecodable words.

Tests (CPU, no game data)

  • tests/semantic/test_fma_mix_literal.py:

    • llvm-mc 23.1.5's own text and encoding for an f32 source, in a clean and a re-anchored listing;
    • the case the file had avoided, lit(0x3c000001) next to an inline constant;
    • a mismatched literal, which is still refused.

    The first two fail without the change.

  • tests/test_shader_ir.py: the lit forms parse as the plain constant.

  • With the three PRs merged locally (rdna2spirv: write the compute thread-group SGPR (TG_SIZE_EN) #6446, this one, and rdna2spirv: s_bfm_b64 in wave32 code is 64-bit data, not a lane mask #6809): tests/semantic, tests/test_lane_masks.py and tests/test_shader_ir.py give 6,210 passed, 1 skipped, 1 xfailed, no failures.

  • tests/test_rdna2_decode.py::test_parity_with_llvm_mc_on_shader_fixtures fails the same way on main with llvm-mc 21.1.8: v_permlane16_b32 prints op_sel:[0,1]. That is unrelated to this change.

docs/design.md: one sentence in the "Printed encodings" row.

AI-assisted (Claude Code). Windows 11, Python 3.12, llvm-mc 21.1.8 and 23.1.5 (built with scripts/build_llvm_mc.ps1), no GPU used.

🤖 Generated with Claude Code

llvm-mc 23.1.5 prints a literal it could also show as an inline constant as
lit(V). In the pipeline library (docs/shader_library.md) that is 5,733 v_fma_mix*
sources, e.g. `v_fma_mix_f32 v139, lit(0x42200000), s104, neg(0)`. operand()
returned {"k": "raw"} for it, so translate() refused 1,520 programs with
"operand lit(...)". LLVM 21 is worse on the same bytes: it prints the inline
constant of the low half and drops the literal word.

lit(V) now parses as V; a '-' in front is the VOP3 neg modifier, as neg(V).
LLVM 23 still re-encodes only the literal's low half, which is the case
mix_literal() already repairs from the blob, so nothing else changes.

tests/semantic/test_fma_mix_literal.py: llvm-mc 23.1.5's own text and encoding
for an f32 source (clean and re-anchored listings) and for the case the file had
avoided (lit next to an inline constant), plus a refused mismatch. The first two
fail without the change. tests/test_shader_ir.py: lit forms parse as the plain
constant.

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

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The new parser accepts a register inside a literal wrapper. A synthetic listing with lit(v2) now executes as if it contained plain v2. The parent rejects this operand. This affects text translation without a companion blob.

Run this script from the repository root with py -3.14 -I probe.py:

import runpy
t = runpy.run_path("tests/semantic/test_fma_mix_literal.py")
program = t["program"](0, words="01 00 20 cc ff 04 02 02",
                       text="v_fma_mix_f32 v1, lit(v2), v2, 0")
words = t["FmaMixLiteralTest"]().run_ps(t["listing"](program), None)
print([hex(word) for word in words])

Expected: reject lit(v2) because a register is not a literal value. Actual at 8d09da06: the CPU interpreter produces 0x40800000 (4.0) in all four output components. With the parent parser from 561b65a8, the same input raises Unsupported: operand lit(v2). Restrict the unwrap to supported numeric literal tokens. Add a case for a register inside the wrapper.

On Windows, all 38 shader IR tests and all 10 FMA mix literal tests passed with CPython 3.14.7.

AI-assisted.

Review on ItsNotRexed#6808 (@shdann): lit(v2) parsed as v2, so a synthetic listing with a register in the
wrapper ran as if the register were there. Only a number is unwrapped now (hex, decimal or float,
optionally negative); lit(v2) keeps its text, which translate() refuses, as on main. The pipeline
library's 5,733 lit operands are all hex constants.

Tests: test_fma_mix_literal.py runs the review's listing and expects Unsupported;
test_shader_ir.py checks lit(v2), lit(s4), -lit(v2) and lit(vcc_lo) are not unwrapped.

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

FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@shdann right, thanks. Fixed in the new head commit: _LIT now unwraps only a numeric constant (hex, decimal or float, optionally negative).

  • lit(v2) keeps its text and translate() refuses it, as on main.
  • Your listing is now test_lit_around_a_register_is_refused in tests/semantic/test_fma_mix_literal.py, and it expects Unsupported.
  • tests/test_shader_ir.py checks that lit(v2), lit(s4), -lit(v2) and lit(vcc_lo) are not unwrapped.

The library's 5,733 lit operands are all hex constants, so the counts in the description are unchanged. Locally, test_shader_ir.py and test_fma_mix_literal.py give 50 passed.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

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 operand() now unwraps LLVM 23's lit(V) to the numeric constant (with -lit(V) read as neg(V)) while leaving lit(v2) raw so the translator still refuses it. docs/design.md notes the form in the printed-encodings row. tests/semantic/test_fma_mix_literal.py and tests/test_shader_ir.py add lit-form, register-in-lit, and blob-mismatch cases.

Why merge

LLVM 23 prints 5,733 pipeline-library v_fma_mix* literals as lit(...), which were previously refused as "operand lit(...)", blocking 1,520 programs. With the fix 1,496 translate and pass spirv-val. This is essential for recompiling the shader library with current LLVM.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

tools/shader_ir.py operand() now unwraps LLVM 23's lit(V) to the numeric constant (with -lit(V) read as neg(V)) while leaving lit(v2) raw so the translator still refuses it. docs/design.md notes the form in the printed-encodings row. tests/semantic/test_fma_mix_literal.py and tests/test_shader_ir.py add lit-form, register-in-lit, and blob-mismatch cases.

Why merge

LLVM 23 prints 5,733 pipeline-library v_fma_mix* literals as lit(...), which were previously refused as "operand lit(...)", blocking 1,520 programs. With the fix 1,496 translate and pass spirv-val. This is essential for recompiling the shader library with current LLVM.

Verdict: Approve

@shayderrr
shayderrr merged commit 513294b into ItsNotRexed:main Nov 23, 2026
0 of 5 checks passed
cynbun added a commit to cynbun/Leonida that referenced this pull request Nov 23, 2026
…s none of

ItsNotRexed#6808 reads LLVM's lit(V) as the constant V. llvm-mc's assembler encodes lit(V) as
the literal dword V even when V is an inline constant, but the 64-bit float
readers tell a literal from an inline constant by value (v_cmp_*_f64, v_ldexp_f64)
or by a 0x spelling (parse_line's f64_literal), so a decimal lit(1) on v_add_f64,
or lit(0x1) on v_cmp_*_f64 / v_ldexp_f64, read as the inline integer 1 in bits
31:0 instead of the literal in bits 63:32.

operand() now marks the imm "literal", parse_line and v_ldexp_f64 honour the mark
(read_f64_words and cmp_class_f64 already did), and lit(-1), lit(1.0), a value
past one dword and lit64() stay "special" (Unsupported) instead of reading as an
imm/fimm whose meaning depends on the operand type. LLVM's disassembler prints
lit() only in hex and only where the literal and the inline constant give the
same value, so no translation of its listings changes.

tests/semantic/test_lit_literal.py (new fixture lit_f64_inline_range.asm) fails
on main and passes here; ItsNotRexed#6808's operand test now expects the mark.

AI-assisted (Claude Code).

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.

3 participants