Skip to content

rdna2spirv: register the buffer-format table constants before the dump - #4466

Closed
FZNull wants to merge 1 commit into
ItsNotRexed:mainfrom
FZNull:fix/fmt-size-constants
Closed

FZNull wants to merge 1 commit into
ItsNotRexed:mainfrom
FZNull:fix/fmt-size-constants

Conversation

@FZNull

@FZNull FZNull commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Problem

aa2988cc ("typed buffer loads read the format's byte size from one table") builds the table's array type with an id from gen.cu(128):

types += [f"%fmt_size_arr = OpTypeArray %uint {gen.cu(128)}", ...]

but the module's gen.consts dump runs earlier in tools/rdna2spirv.py (:12185-12187). A cu() call after that dump returns %cXX without ever appending its OpConstant, so any shader that uses the new table emits an undefined id and spirv-as rejects the module with Operand 'N[%N]' requires a previous definition. The same class exists at :12239 (gen.cu(16) for the typed-format masks).

Reproduced at 03b09a08 with SPIRV-Tools present:

$ python -m pytest tests/test_u16_high_half.py
FAILED tests/test_u16_high_half.py::test_d16_buffer_store_no_redundant_extract_after_shr16
E  AssertionError: error: line 153: Operand '135[%135]' requires a previous definition
E      %_arr_uint_135 = OpTypeArray %uint %135

Without spirv-as/spirv-val the test's validation returns early, which is why CI does not show it; the affected shaders are the typed buffer loads and stores, so a corpus that uses the table would fail to compile.

Change

Register the constants the later type blocks ask for immediately before the dump, so cu() returns ids that are defined:

  • the table length (128) and every Gen.FORMATS byte size when gen.uses_fmt_size;
  • the mask length (16) and every word of Gen.int_format_masks() when the typed image aliases are used.

The later blocks keep their gen.cu(...) calls unchanged; those now return the registered ids. No semantics change, no new opcode, one file.

Verification

Environment: Windows 11 build 26300, Python 3.13.12 (out/test-env), llvm-mc 21.1.8 (AMDGPU), SPIRV-Tools v2026.4, at 03b09a08 + this commit.

$env:PATH = "<tools>/spirv-tools/bin;" + $env:PATH
$env:LLVM_MC = "<tools>/llvm-mc/llvm-mc.exe"
out\test-env\Scripts\python.exe -m pytest tests/test_u16_high_half.py -q
# before: 1 failed, 19 passed  (the error above)
# after:  33 passed

out\test-env\Scripts\python.exe -m pytest tests/test_u16_high_half.py tests/test_translate.py `
    tests/test_buffer_store_probe.py tests/test_image_load_typed.py `
    tests/test_image_load_d16_typed.py tests/test_rdna2spirv_cli.py -q
# 673 passed, 4 skipped, 82 subtests passed in 93.43s

The regression test already existed and now passes; no test was changed. This is translator evidence for the generated SPIR-V, not hardware or game evidence.

Claimed in the coordination issue (comment 7088927133). Refs #4257.

aa2988c builds %fmt_size_arr from gen.cu(128) while the module's uint constant dump has already run, so a shader that uses the table references a %cXX id that is never defined and spirv-as rejects the module. Register the constants the later type blocks ask for (the format-size table and the typed-format masks) before the dump. Same class at :12239 (gen.cu(16)).

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

Ran this at 99182890 on Windows 11: tests/semantic/test_buffer_format_narrow_int.py -> 3 tests, OK; test_buffer_tbuffer_format_d16.py -> 7 tests, OK; compileall and git diff --check clean.

The buffer-format table constants are now registered before the constant dump, so a cu() call in that later block cannot create an undefined id. The affected format tests pass. No blocking issue.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified 99182890 on Windows 11 25H2 + AMD Radeon RX 6700 XT (gfx1031, Adrenalin 26.11.2, Vulkan 1.4.315, SPIRV-Tools v2026.4, llvm-mc 21.1.8).

Repro & Negative Control (spirv-as + spirv-val --target-env vulkan1.2)

  • Executed the exact buffer_store_format_d16_xy v1, v2, s[0:3], 0 offen fixture (test_d16_buffer_store_no_redundant_extract_after_shr16 logic via tests/listing.py):
    • Parent 03b09a08 (HEAD~1): spirv-as exits 0, then spirv-val fails with error: line 153: Operand '135[%135]' requires a previous definition (%fmt_size_arr = OpTypeArray %uint %c80 without %c80 = OpConstant %uint 128).
    • PR #4466 (99182890): emits %c80 = OpConstant %uint 128 before types, and passes both spirv-as (rc=0) and spirv-val (rc=0).
  • Minimal synthetic checks (minimal_vs, minimal_ps_pv, cs_lds_off84, cs_bar3, cs_emu_xsink) also pass spirv-as + spirv-val.

GPU & Unit Checks (LEONIDA_VK_VALIDATION=1)

  • scripts/run_gpu_tests.py --filter cs_fmt --require-pass: 5/5 translated & validated, 6/6 passed, 0 validation errors.
  • scripts/run_gpu_tests.py --filter d16 --require-pass: 6/6 translated & validated, 6/6 passed, 0 validation errors.
  • tests/test_buffer_store_probe.py: 7/7 OK (0.331 s).
  • tests/test_image_load_typed.py: 21/21 OK (1.100 s).
  • tests/test_rdna2spirv_cli.py: 25/25 OK (5.251 s).
  • tests/test_shader_checks.py: 75/75 OK (1.416 s).
  • py_compile tools/rdna2spirv.py: exit 0.

(Non-blocking note: other post-dump gen.cu(...) sites remain in assemble() after line 12199, e.g. lds_arr, x_arr, arr_clip/cull, ms epilogue; they currently avoid the failure because their constants are registered earlier, though dumping gen.consts after assembling types/fn would close the whole pattern cleanly as a follow-up.)

AI-assisted (Gemini, Antigravity).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 99182890 on Linux (Ubuntu), CPython 3.10.12, pytest 9.2.0, CPU only, no game files: pass.

  • python -m pytest -q tests/test_u16_high_half.py: 33 passed — same on main, because without SPIRV-Tools the test's spirv-as/spirv-val validation returns early, which is exactly the CI blind spot described. So this run confirms no regression, not the fix itself.
  • The change registers the table constants before the gen.consts dump so the later gen.cu() calls return defined ids; reads correctly.
  • git diff --check clean; compileall clean. Full verification needs a box with SPIRV-Tools on PATH — the PR's Windows run covers that.

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: this fixes every failure of this kind in the corpus.

  • Full corpus on main 03b09a08 (rdna2spirv.py --shard i/8, merged): 26,503 translated, 26,473 pass spirv-val. All 30 failures are the same error, Operand 'N[%N]' requires a previous definition (15 CS, 1 PS, 14 others).
  • Those 30 retranslated one at a time:
tree translated pass spirv-val
main 03b09a08 30 0 (30 × requires a previous definition)
this PR, 99182890 (contains main) 30 30
#3902 merged onto main 30 30

I did not retranslate the other 26,473 with this PR, so this doesn't show that nothing else changes. No game data in this comment beyond counts.

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

@dieguesmosken

Copy link
Copy Markdown

Tested 9918289 on NVIDIA GeForce RTX 5050 (Blackwell), NVIDIA 595.104.02, Pop!_OS 24.04 (Linux, spirv-as/spirv-val installed): pass.

tests/test_u16_high_half.py (pytest): on the parent 03b09a0 test_d16_buffer_store_no_redundant_extract_after_shr16 fails in spirv-val with "Operand '135[%135]' requires a previous definition" (OpTypeArray %uint %135); on this branch all 33 pass. This is the Linux + spirv-val check that was missing, since without SPIRV-Tools that test's validation is skipped.
GPU: run_gpu_tests.py --filter fmt: 7/7 pass (cs_buf_fmt8, cs_fmt_1010102, cs_fmt_d16_xyz x2, cs_fmt_int_narrow, cs_fmt_sint32, cs_fmt_xy_xyz); translation of 6 shaders, 6 pass spirv-val.

No game data. AI-assisted.

@mrbean56

Copy link
Copy Markdown

Read and translated on Linux (Debian 13), Python 3.13.5: no behaviour change where main already worked, and the reordering is correct by inspection.

  • Translated 6 synthetic fixtures (cs_fmt_1010102, cs_fmt_int_narrow, cs_fmt_sint32, cs_oob_tail, cs_fmt_d16_xyz, and the coherent read-modify-write PS from tests/semantic/test_image_coherent.py) on 03b09a08 and on this head: byte-identical SPIR-V in all six, and every %c… id used by %fmt_size_init / %fmt_uint_init / %fmt_sint_init is defined in both.
  • Checked the ordering claim against the source: on main the two gen.cu() blocks sit at 12228 and 12236, after the constant dump at 12186, so ids first created there would have no definition. The new block at 12188 runs before the dump and calls the same cu() values (128, the 128 format sizes, 16, the mask words), so the ids are pre-registered and the later blocks reuse them. Output order is unchanged because the dump is sorted by value.
  • What I could not do: build a module that is invalid on main today. In every fixture I tried, the body translation had already registered the table's constants (%cc, %c10, the mask words), so the missing-definition path never shows. That makes this a latent-bug fix rather than a reproduced one.
  • Suggestion, since the branch has no test: a cheap pin is a semantic test that translates an image_load ... glc read-modify-write (the rmw() helper in test_image_coherent.py is one line) and asserts that every %c… id appearing in %fmt_size_init/%fmt_uint_init/%fmt_sint_init has a definition. It passes today on this head and would catch a future path that misses the registration; it is honest that it does not fail on main.

No GPU, no game data. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Checked on a full re-translation of a real 1.0.2.1 dump with --wave64-emulate: on 03b09a0, 30 of 26,503 game shaders (all contain buffer_store_format_xyzw) fail with requires a previous definition, and 26,473 pass; with this PR's head, those 30 all translate and pass spirv-val (Windows, Python 3.13, spirv-val from the leonida-tools folder). Nothing else regressed against the build from this morning.

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

Overlap to resolve: #3902 (fearkov, c225f859) fixes the same failure — a late gen.cu() id with no OpConstant — with a general insertion of every constant assemble asks for after the dump, plus test_constants_asked_for_late_are_defined. This PR pre-registers the format-table constants instead.

Verified on Windows 11 against main d0835dd9 and both heads, with spirv-as/spirv-val from Vulkan SDK 1.4.363:

main d0835dd9  tests/test_u16_high_half.py::test_d16_buffer_store_no_redundant_extract_after_shr16  FAILED
#3902 c225f859  same test  passed
#4466 99182890  same test  passed

Both edits sit in assemble right around the constants dump, so whichever lands first the other will conflict. #3902 is the earlier claimant and the general shape (it also covers the mask table and future late constants) and carries a regression test; if the targeted shape is preferred instead, please port that test. Either way one of the two should close.

@0xmp3

0xmp3 commented Nov 23, 2026

Copy link
Copy Markdown

Tested on: pass — the whole affected set from a real corpus, not just the pytest repro.

OS:      Windows 11 Pro 26300; own 1.0.2.1 copy (108/108 files, SHA-256 verified)
Corpus:  shaders stage at aa2988cc, 26,503 DXBC listings
Tools:   llvm-mc 23.1.5 (gfx1013 disassembly), SPIRV-Tools v2026.4

On aa2988cc the full run gives 26,422 / 26,503 passing spirv-val; the 81 failures are exactly this class. I isolated them (filter fssm_index.json to the names with no .spv, shader_disasm.py --unpack-index, then rdna2spirv.py on that folder) and ran the same 81 through this PR head:

aa2988cc this PR
--wave64-emulate off (native 64-wide, RDNA4) 0 / 81 81 / 81
--wave64-emulate on 51 / 81 81 / 81

Both modes report 81 shaders: 81 translated, 81 pass spirv-val.

The mechanism checks out on the modules themselves: the undefined id is %c80 in all 81 — the hex name of 128, i.e. the gen.cu(128) array length — and 79 of the 81 also carry %fmt_size_tab, so the other two are the uses_store_views / uses_int_fetch mask-table case you fix as well.

No game data; counts and error classes only. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Ran a stratified sample of 3100 of my own dumped 1.0.2.1 shader inputs (rare opcodes, random fill, buffer_store_format; --wave64-emulate, Windows Python 3.13.1, SPIRV-Tools spirv-val). At d0835dd (main): 19 fail spirv-as/val with Operand requires a previous definition. At this PR's head (9918289): 0 fail, 19 fixed, 0 of the other 3081 changed in output SPIR-V. Counts only; will re-check on main after it merges.

@abilliontokens

Copy link
Copy Markdown
Contributor

Tested 9918289 CPU-only (Windows 11, Python 3.12.10 python -I, spirv-as/spirv-val v2026.4, Vulkan SDK 1.4.363.0): pass, full corpus.

Reproduction on main 03b09a08 first, whole corpus (26,503 listings, default flags, --no-asm, 6 shards + --merge, spirv-val on every module): 26,503 translate, but only 26,422 pass spirv-val — 81 modules invalid, all one class:

INVALID: error: line N: Operand 'X[%X]' requires a previous definition

The first offender in each is the %fmt_size_arr = OpTypeArray %uint %c80 line: %c80 is used and never defined. assemble asks gen.cu(128) (and the table-entry constants) in the uses_fmt_size block after the constants dump loop (for v, name in sorted(gen.consts.items())) has already written the definitions, so an id that nothing else in the shader had asked for is emitted with no definition. The 81 are exactly the uses_fmt_size shaders whose bodies never pull one of the table-only constants in by other means; every other module is unaffected.

With this PR at 9918289, same corpus and flags: 26,503 translated, 26,503 pass spirv-val (merged index 26,503 entries). Byte comparison of the two full output sets: 26,422 modules byte-identical, 0 changed, 81 present that main could not emit — the fix adds only the missing definitions and touches nothing any valid module contained.

Corpus provenance: the run read a complete 26,503-listing carve of the same package; its modules are byte-identical to a second, independently carved copy I ran 03b09a08 on (26,422/26,422 identical between the two base runs), so the before/above pair compares identical inputs.

For the maintainers' choice between the two open fixes: #3902 (@fearkov, opened earlier) fixes the same class generally (insert_late_constants defining anything assemble asks for after the dump) and carries a translator-driven test with a hand-written listing; this PR pre-registers exactly the table constants at the source. This comment is the corpus-scale check for this PR, posted as data — I hold no claim on either.

AI-assisted (omp orchestrator).

@kapitche

Copy link
Copy Markdown
Contributor

Re-ran the same 3100-shader sample (my own dump, --wave64-emulate, Windows Python 3.13.1) at main 7b7d73f (after the round-43 merge): 0 of 3100 fail. The 19 that failed at d0835dd with Operand requires a previous definition pass now; main's 06b47de registers the %fmt_size_tab constants in the typed-load path, the same defect class this PR fixes at assembly. Output SPIR-V changed for 2891 of the 3100 (908 commits, 105 of them touching translator files, so no per-commit split). The gen.cu(16) mask-length case this PR also covers did not fail on any shader in the sample.

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 99182890 merged with main d0835dd (clean merge) on NVIDIA GeForce RTX 3060 Ti, driver 580.173.02, Linux (kernel 6.18), with SPIRV-Tools v2026.1: pass.

  • pytest tests/test_translate.py tests/test_u16_high_half.py plus a def-before-use test over the 9 typed buffer_load/store_format_* forms: 624 passed, 4 skipped. That test fails 7 of 9 cases on d0835dd alone (the stores).
  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed.

main has fixed this since then. 06b47de (round 43, now in main 7b7d73f) registers the same constants as this PR does: Gen.fmt_size_of calls cu() for 128, 0 and every FORMATS size, and the image ops call it for 16 and the int-format masks (rdna2spirv.py:11134 and :11348). The def-before-use test passes on main 7b7d73f alone, so this PR looks superseded. 06b47de has no regression test; the test is in my comment on #3902 if anyone wants it.

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

@FZNull

FZNull commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Closing this in favour of the upstream fix, which already covers the same defect class. Thanks to everyone who verified it — the corpus numbers are what made the duplicate obvious.

What I measured just now on current main (98fa503f): tests/test_u16_high_half.py passes (33 passed), including the store-only case that failed on 03b09a08. The translator now registers the format-table constants when the typed load/store is decoded (fmt_int_flags() and the typed-load path; 06b47de1 is in main), so the later gen.cu() calls in assemble() return defined ids and this branch's pre-registration is redundant.

This PR changed no behaviour beyond that pre-registration, so there is nothing left for it to merge. The useful remainder is the regression guard, because the CI hole that let the bug through is still there: tests/test_u16_high_half.py returns early when spirv-as/spirv-val are absent, and on 03b09a08 a tool-less machine reported 33 passed on an invalid module.

I opened #4710 with only that guard: it checks the module text rdna2spirv.translate() already returns, needs no external tools, fails on 03b09a08 with the module references ids it never defines: ['%c80'], and passes on current main. Please review that instead.

Closing this branch rather than leaving an unmergeable duplicate open. Refs #4710.

@kapitche

Copy link
Copy Markdown
Contributor

Real-corpus data point (counts only; owner's own 1.0.2.1 dump, Windows Python 3.13.1): in a stratified 3,000-shader sample on current main (d0835dd), exactly 5 shaders fail spirv-as with Operand '..[%..]' requires a previous definition, and the same 5 fail in every translator flag configuration I tried (11 configurations: native vs --wave64-emulate, each fallback flag, and the Intel/Arc/all-flags combinations). With this PR's head the same 5 translate and pass spirv-val (5/5, --wave64-emulate).

@BeegSchmooze

Copy link
Copy Markdown

Tested this against a full shader corpus from the Xbox 1.0.2.1 package (make_game.py shaders stage, --wave64-emulate, gfx1013 disassembly with LLVM 21.1.8 llvm-mc, SPIRV-Tools v2026.4 spirv-val, Windows 11).

On main @ 03b09a0

  • 26,503 shaders translated, 26,473 pass spirv-val.
  • All 30 failures are the same error: Operand ID requires a previous definition, the late gen.cu() constants this PR moves ahead of the dump.
  • shader_library_corpus_test (with LEONIDA_SHADERS): 0 failures, and the only gap is those 30 untranslated shaders.

With this PR (9918289)

main this PR
The 30 failing shaders 30 translated, 0 pass 30 translated, 30 pass
600 random passing shaders (fixed seed) 600/600 pass 600/600 pass, output folders byte-identical to main (diff -rq clean)

So it fixes the whole failure class and leaves untouched output alone in the sample. I didn't rerun the full 26,503 with the PR; the sample plus the identical output is the basis for expecting 26,503/26,503 valid.

Not covered here: the 7 FSPX programs that fail with "reachable instruction with normalized encoding" (22/29 translate). That's a separate issue and this PR doesn't touch it.

Counts only. No shader names or game data are included.

This comment is AI-assisted (Claude Code); the runs above were done locally and their results checked.

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
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.