Skip to content

Translator: constants assemble asks for late are defined too - #3902

Open
fearkov wants to merge 2 commits into
ItsNotRexed:mainfrom
fearkov:fix/late-uint-constants
Open

fearkov wants to merge 2 commits into
ItsNotRexed:mainfrom
fearkov:fix/late-uint-constants

Conversation

@fearkov

@fearkov fearkov commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #3796 as:

#3796 (comment)

What and why

tests/test_u16_high_half.py::test_d16_buffer_store_no_redundant_extract_after_shr16 fails on main. The module
it translates doesn't validate: %fmt_size_arr = OpTypeArray %uint %c80, and %c80 is never defined.

assemble writes the uint constants (OpConstant %uint for everything in gen.consts) and only then builds the
format tables, which ask gen.cu for constants of their own:

  • fmt_size_arr, from aa2988c, needs its length 128 and its entries;
  • fmt_mask_arr needs 16 and its mask words;
  • several prologue and stage arrays further down ask for their own as well.

A constant first asked for there gets a name and no definition. The mask table only works because translation
already happens to ask for its constants. In a shader whose only typed access is a d16 store, nothing else uses
128, so fmt_size_arr broke.

The fix is generic rather than one more pre-registration. assemble remembers which constants it wrote, and
insert_late_constants adds any asked for later right after them, so before every type and instruction.

Commits:

  1. insert_late_constants in tools/rdna2spirv.py
  2. test_constants_asked_for_late_are_defined in tests/test_translate.py: that d16 store defines %c80 once,
    before %fmt_size_arr, and the module passes spirv-val

Tested

Linux, llvm-mc 23.1.5, SPIRV-Tools from the Vulkan SDK 1.4.363, on main aa2988c.

Check main this branch
tests/test_u16_high_half.py + tests/test_translate.py 1 failed (the d16 store) 614 passed, 4 skipped (+1 new)
the new test fails: no definition of %c80 passes
runtime/test/shaders/build.py 234 translated, 234 pass spirv-val the same 234 .spv, byte for byte

The 234 GPU fixtures don't change at all, so this only touches modules that were invalid.

python -m unittest discover -s tests/semantic: 7,456 tests, the same 7 failures and 2 errors as on main aa2988c (test_loop_ballot_barrier 3 + 2, test_cmp_class_f64 2 (TEST-853), test_mutations 2), none new.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.

assemble writes the uint constants before the format tables, and those ask gen.cu for constants of their own. A shader whose only typed access is a d16 store used no 128 anywhere else, so fmt_size_arr's length (aa2988c) was an id with no definition and the module failed spirv-val (tests/test_u16_high_half.py on main). Constants asked for after the others were written now go in right after them.
@Ingenioeren

Copy link
Copy Markdown

Independent native Windows check of c225f859 over aa2988cc, using AMDGPU llvm-mc 23.1.5 and SPIRV-Tools 2026.1. With only the test hunk applied, both targeted tests failed on main. The existing d16 store fixture assembled, then spirv-val rejected the array length because its ID lacked a previous definition. The new test independently found zero %c80 definitions. With the translator hunk applied, both passed and the new test verified the one definition precedes OpTypeArray.

Full tests/test_u16_high_half.py and tests/test_translate.py on Windows: 615 passed, four skipped. Synthetic inputs only. AI-assisted.

@throny

throny commented Nov 22, 2026

Copy link
Copy Markdown

Tested c225f85 on real corpus shaders (CPU only, Windows 11, SPIRV-Tools from Vulkan SDK 1.4.363.0): pass.

The 4 shaders of my own 1.0.2.1 copy that the TEST-305 watch found newly invalid at aa2988cc (out of a 3,000-shader coverage sample), translated with --wave64-emulate --no-asm:

Commit Translated spirv-val valid
4c52ce27 (before the regression) 4/4 4/4
aa2988cc (main) 4/4 0/4, all requires a previous definition
#3902 c225f859 4/4 4/4
#3921 c0130045 4/4 4/4

Both PRs fix all four. This one also covers later gen.cu() calls made after the constant block and comes with a test, so it is the one I would merge. Not run here: the semantic suite and the full 3,000 sample on this head. Counts only, no shader data. AI-assisted.

@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.

Tested c225f859 on Windows 11: tests/test_u16_high_half.py -> 33 passed (including the previously failing test_d16_buffer_store_no_redundant_extract_after_shr16); tests/test_d16_format_probe.py -> 21 passed, 2 skipped; compileall and git diff --check clean.

The generic late-constant insertion is the better fix than my closed #3921: it covers fmt_size_arr (128 and its entries), fmt_mask_arr, and any future prologue/stage array that first asks gen.cu() after the constant block. Inserting after the highest written constant keeps every use after its definition; the new test_translate.py case pins the d16-only shape. No blocking issue.

@Flyander Flyander 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 c225f85. The late constants are inserted before all type and instruction uses, and the regression covers the d16-only shape that previously emitted an undefined array-length ID. Local compileall and diff --check pass; pytest/llvm-mc/SPIRV-Tools are unavailable here, and independent Windows verification reports the focused suites passing. No blocking finding.

@throny

throny commented Nov 22, 2026

Copy link
Copy Markdown

Follow-up to my test above, same head c225f85: the 3,000-shader TEST-305 coverage sample of my own 1.0.2.1 copy (--wave64-emulate --no-asm, SPIRV-Tools 2026.4) is 3,000 / 3,000 translated and spirv-val-valid, no error classes. Main at aa2988cc gives 3,000 translated and 2,996 valid on the same sample, so this head fixes all 4 and breaks none in the sample. Counts only. AI-assisted.

@chpeu

chpeu commented Nov 22, 2026

Copy link
Copy Markdown

Tested c225f859 (main aa2988cc is an ancestor, no rebase needed) on Linux, no game files.

This fixes the one test that current main fails here. With the 28 pytest-marked files run the way CI's
translator-tests job does (python -m pytest <those files> -q):

  • main aa2988cc: 1592 passed, 1 failed — tests/test_u16_high_half.py::test_d16_buffer_store_no_redundant_extract_after_shr16,
    where spirv-val reports line 153: Operand '135[%135]' requires a previous definition for
    %_arr_uint_135 = OpTypeArray %uint %135.
  • your branch: 1594 passed, 0 failed (41 skipped, 97 subtests passed) — the failure is gone and your new case
    is in the count.

Where it came from, in case it helps: on this machine the file is green on the thirteen commits before aa2988cc
and red on aa2988cc itself, so the format-size table commit is what exposed it — consistent with your
description of a constant being assembled after the type that uses it.

GPU side (RTX 3080, driver 615.71.09):

  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-20/bin/llvm-mc
    → 195/210 passed, 15 skipped, 0 failed
  • without the variable → 164/210 passed, 46 skipped, 0 failed
  • exactly the numbers main gives on this machine, so nothing moves on the hardware side.

The RTX 5080 was not run in this pass: it is holding ~15.6 GB for another job on this box, and I would rather
say so than report an allocation failure as if it were your change. python -m compileall -q make_game.py tools tests: clean.

Machine: Linux Mint 22 (Ubuntu 24.04 base), llvm-mc 20.1.2, SPIRV-Tools 2025.1~rc1 (Ubuntu packages), GCC 13.3,
Python 3.12.3 with pytest 9.2.0 and numpy 2.5.5, built with cmake -S runtime -B runtime/build/linux -DLEONIDA_VULKAN_HEADERS_FETCH=ON. AI-assisted testing, counts read through by hand.

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head on Windows 11 / Python 3.13.0: tests/test_translate.py + tests/test_u16_high_half.py give 615 passed, 4 skipped; the targeted semantic files 88 passed; regen_fixtures --check and compileall clean; merge clean.

The constant scan is the strong part: over 229 translated runtime/test/shaders/*.asm (uint and %cl), 0 undefined, duplicate, name/value-mismatched or use-before-definition ids. The new test goes through conftest.make_shader -> rdna2spirv.translate and spirv-val, and reverting only tools/rdna2spirv.py fails it at tests/test_translate.py:246 (%c80 undefined).

Minor, latent only: insert_late_constants covers gen.consts but not gen.lconsts (tools/rdna2spirv.py:11845-11858), and there is no late cl() caller today.

Approve. AI-assisted review, no game files, no GPU.

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown

Tested c225f859 (base aa2988cc) on Linux (Ubuntu 26.04 container, Python 3.14), NVIDIA RTX 3060 Ti, driver 580.173.02: pass, and the new test is red on the base.

  • tests/test_translate.py: 584 passed, 4 skipped. With tools/ from aa2988cc, 1 failed: test_constants_asked_for_late_are_defined (assert len(defs) == 1, which is 0 on the base).
  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped. That matches main aa2988cc test for test on this card. ctest: 47/47 passed.

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent compatibility test at c225f859: the fix applies cleanly on top of the verified semantic recovery tree (#3995, #3948 and #3883 on main aa2988cc), producing tree 6e891c70.

On Windows 11 with uv CPython 3.12.13, pytest 9.2.0, Vulkan SDK 1.4.363.0, and WSL LLVM 21 for assembly, the combined targeted run gave 709 passed, 99 subtests passed, and 4 skipped. It included test_translate.py, test_u16_high_half.py, the typed buffer load/store suites, and both ballot and class-f64 regression suites. The previously failing d16 store validation check passed in the combined run. This confirms composition with those recovery PRs. CPU-only synthetic inputs. AI-assisted (Codex).

@tunczzz

tunczzz commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested c225f859 (base: main aa2988cc) in WSL Ubuntu, Python 3.14, llvm-mc 21, SPIRV-Tools v2026.1: pass.

  • pytest tests --ignore=tests/semantic -n 3: 6416 passed, 0 failed. On plain aa2988cc the same run fails test_u16_high_half.py::test_d16_buffer_store_no_redundant_extract_after_shr16 (spirv-as: %c80, the length of %fmt_size_arr, is used but never defined). With this PR it passes.
  • No change where nothing is late: translated 240 FUZZ-8 programs (cf_diff seed 100, 60 each for wave32/wave64 × structured/dispatch-loop) with aa2988cc and with this PR. All 240 modules are byte-identical.
  • Code read: insert_late_constants puts the late constants right after the last one assemble wrote, so before every OpTypeArray/OpConstantComposite that uses them, and keeps the sorted order. I found two other kinds of late request it also covers: the %fmt_mask_arr length (gen.cu(16)) and its entries, and setdefault(1)/(2) in the prologue. Those only worked because the constants were almost always used elsewhere already.

AI-assisted (Claude Code). CPU only, no game files.

@Ayinaki

Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Store-side coverage for this fix, if you want it in the branch.

The regression only shows on a format store. A buffer_load_format_* also decodes the format, and that path asks for 128 through 1 << (w-1), so every load test still produces a valid module on aa2988cc; a buffer_store_format_* at a run-time V# decodes nothing, so nothing but the table asks for the array length and %c80 is left undefined. insert_late_constants fixes both halves; a hand-written store fixture pins the store half so a later change cannot quietly reintroduce it.

This is the test from my #4071, which I have just closed in favour of yours. It passes unchanged on your head c225f859 (40 of 40 in tests/semantic/test_buffer_store_format.py with it appended) and is red on aa2988cc. Append to that file, after adding import re to its imports:

class TestRuntimeFormatSizeTable(unittest.TestCase):
    """`%fmt_size_tab` holds one byte size per V# FORMAT code (a buffer_load/store_format at a run-time format), so it
    declares a constant for each size plus the array length. Those have to be emitted with the rest of the constant
    section: the table block asked for them after that section was written, so a module that needed the length (128)
    nowhere else referenced an id it never declared, and spirv-val rejects it with
    `Operand '<id>' requires a previous definition`. A format load also decodes the format, which registers 128 on
    its own; a format store does not, which is the case pinned here."""

    def test_runtime_format_store_declares_every_table_constant(self) -> None:
        spv = support.translate_lines("ps_fmt_store_runtime", [
            "s_mov_b32 s4, 0x1000",
            "s_mov_b32 s5, 0",
            "s_mov_b32 s6, 0x100",
            "v_mov_b32 v1, 0",
            "v_mov_b32 v2, 0x11223344",
            # s7 is never written, so the V# FORMAT field is run-time data and the element size comes from the table
            "buffer_store_format_xyzw v[2:5], v1, s[4:7], 0 offen",
            "s_endpgm",
        ])
        self.assertIn("%fmt_size_arr", spv)
        self.assertIn("%c80 = OpConstant %uint 128", spv)          # the array length nothing else asks for
        declared = {line.split()[0] for line in spv.splitlines() if " = OpConstant " in line}
        referenced = set(re.findall(r"%c[0-9a-f]+\b", spv))
        self.assertEqual(sorted(referenced - declared), [], "the module references a constant it never declares")

Only the %c80 = OpConstant %uint 128 line is specific to the format-size table; the rest asserts that no %c... id in the module is used without an OpConstant line, which is the shape of the failure rather than this one constant. Take it or leave it, no other change is needed either way.

@lunarminus

Copy link
Copy Markdown

Tested c225f859 (base aa2988cc) over the whole corpus of my own 1.0.2.1 copy on Linux (CachyOS, llvm-mc 23.1.5, system SPIRV-Tools; CPU only): pass.

rdna2spirv.py <work>/shaders/dxbc/disasm --shard i/8 ×8, then --merge, with the same flags as make_game.py's shaders stage on this machine (wave64 emulation off; the GPU's smallest maximum subgroup size is 64):

translated spirv-val-valid
main aa2988cc (the make_game.py shaders stage) 26,503 26,422
this PR 26,503 26,503
  • On main all 81 failures are the same class: Operand '<id>' requires a previous definition. All 81 are valid with this PR.
  • The 26,422 shaders that were already valid come out byte-identical (26,422 of 26,422 .spv files equal), so the fix changes nothing else.

AI-assisted (Claude Code).

@traeb0n

traeb0n commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested c225f85 on RTX 5070, driver 621.01, Windows 11 Pro 26300, MSVC 14.44 (build_runtime.ps1), Vulkan SDK 1.4.363, Python 3.11, llvm-mc 23.1.6 (conda-forge llvm-tools; it assembles the v_lshlrev_b16_e64 … op_sel:[0,1,1] line from the docs): pass.

  • pytest tests/test_translate.py: 582 passed, 4 skipped (main: 581 passed, 4 skipped, so the one new test passes)
  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed, the same per-test results as main. All 214 shaders translate and pass spirv-val.

Baseline on main aa2988c, same machine: full ctest 149/149 (llvm-mc build), pytest tests/test_translate.py 581 passed, 4 skipped, scripts/run_gpu_tests.py 164/210 passed, 46 skipped, 0 failed. Of the 46 skips, 31 need a 64-wide subgroup (this card is 32-only), 1 needs a required 64-wide fragment subgroup, and 14 are run by their own ctest target.

No game files. AI-assisted (Claude Code).

@byPreaZy

Copy link
Copy Markdown

Corpus validation on the game shader set (26,503 disassembled programs, FSSM wave sizes):

The 81 shaders aa2988c broke

On aa2988cc (your base) the corpus run drops 103 shaders: 81 fail spirv-val with "Operand requires a previous definition" (the %c80-style late constants) and 22 hit "no instructions". Running the same 103 through this head's translator:

  • 81/81 now translate and pass spirv-val (the 22 "no instructions" are unchanged, a separate stable case)

So the fix restores every shader the HEAD commit regressed, on real corpus input, not only the synthetic d16-store case.

No change to already-valid modules

A random sample of 400 shaders that were valid on aa2988cc, retranslated here: 400/400 produce byte-identical .spv. insert_late_constants only adds definitions that were missing, so valid modules are untouched, matching your 234-fixture result.

Notes

  • Method: tools/rdna2spirv.py <disasm> --fssm-dir <fssm> on both heads, spirv-val from Vulkan SDK 1.4.363.0, counts and hashes only, no shader data.
  • A full-corpus run on this head is in progress for the exact translated/valid totals; happy to post them as a follow-up.

First corpus-scale check of this fix. AI-assisted (Devin).

@theswiss24

Copy link
Copy Markdown

Corpus check on my own copy (Xbox Series 1.0.2.1), Windows 11, Python 3.14.2, llvm-mc 21.1.8, --wave64-emulate. Counts only.

On main 03b09a08 the full corpus gives 26,503 translated, 26,473 pass spirv-val. All 30 failures are Operand 'N[%N]' requires a previous definition. Retranslating those 30 with c225f859 merged onto main 03b09a08 (clean, 86e4942c): 30 translated, 30 pass (main: 0 pass). #4466 also fixes the same 30, so the two overlap. I did not retranslate the other 26,473 with this PR. No game data in this comment beyond counts.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@ramenbellic

Copy link
Copy Markdown

Tested c225f85 on NVIDIA GeForce RTX 3070, driver 620.70.06, Linux (Bazzite, Fedora 45 base, kernel 7.3.3): pass

  • GPU suite (scripts/run_gpu_tests.py): 164/210 passed, 46 skipped (wave64 tests on 32-wide subgroups), 0 failed native; 195/210 passed, 15 skipped, 0 failed with LEONIDA_W64_EMULATE=1
  • The PR's tests: tests/test_translate.py: 582 passed, 4 skipped. tests/semantic as a whole: 7 failures, 2 errors, the same as main aa2988c (this PR's base) alone on this machine, so not from this PR
  • Whole shader corpus (26,503 game shaders, --wave64-emulate), this PR merged onto main f847748 (which brings in main up to aa2988c): 26,503/26,503 translated, 26,503 pass spirv-val. No per-shader diff count: the PR is based on a main 1,307 commits newer than that baseline

AI-assisted run (Claude Code); counts only, no game data.

@byPreaZy

Copy link
Copy Markdown

Follow-up: the full corpus run on this head has finished.

  • 26,482 shaders translated and pass spirv-val, out of 26,503 disassembled programs (aa2988c: 26,400)
  • +82 index entries vs main, 0 removed, 0 wave64-flag changes across the 26,400 common shaders: the 81 wave64 shaders the fmt-table constants broke, plus one listing re-disassembled locally while the run was in flight
  • The 21 remaining failures are all "no instructions" cases whose listing files hold a captured WSL error instead of code (a local disassembly artifact, since re-disassembled; unrelated to this change)

Counts only, no shader data. AI-assisted (Devin).

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.