Skip to content

rdna2spirv: declare the format-size table's constants with the rest of them - #4071

Closed
Ayinaki wants to merge 1 commit into
ItsNotRexed:mainfrom
Ayinaki:fix/fmt-size-tab-order
Closed

Ayinaki wants to merge 1 commit into
ItsNotRexed:mainfrom
Ayinaki:fix/fmt-size-tab-order

Conversation

@Ayinaki

@Ayinaki Ayinaki commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Superseded by #3902, which fixes the same regression generically and was claimed before this one; closed as the duplicate. The comparison and the verification of #3902 are in the closing comment.

Claimed in the coordination issue as: the aa2988cc format-table regression reported by @throny (finding).

What and why

aa2988cc made buffer_load_format read its element size from %fmt_size_tab instead of a select chain. The table is a Private array of 128 sizes, so it needs a constant for its length and one for each size it holds — and those were asked for after assemble() had already written the OpConstant section (the single loop over gen.consts). Any value not already registered during translation was therefore referenced by %fmt_size_arr/%fmt_size_init and never declared, and spirv-val rejects such a module with Operand '<id>' requires a previous definition — the error class @throny measured on 4 of 3,000 corpus shaders.

Which value is missing depends on the shader:

  • A buffer_load_format_* at a run-time V# also decodes the format, and that path asks for 128 (1 << (w - 1) for an 8-bit format), so it happens to declare the array length. 400 of 400 typed-load shaders from my own work folder translate to spirv-val-valid modules at aa2988cc.
  • A buffer_store_format_* at a run-time V# reads the element size from the same table but decodes no format, so nothing else in the module asks for 128 and the length is the undeclared id. That is the case the new test pins.

The fix registers the table's constants where assemble() already pre-registers the ones its later blocks ask for (the same convention as the --wave64-emulate and barrier-round blocks, which is commented "before any constant is emitted"), so the later cu() calls reuse them. Gen.fmt_size_table() is now the one place the size list is built, used by both the registration and the OpConstantComposite initializer.

No behaviour change to the translated code: the same ids, sizes and initializer, only declared before they are referenced.

Tested

CPU only, no GPU; no game files in the PR.

  • New test TestRuntimeFormatSizeTable.test_runtime_format_store_declares_every_table_constant (tests/semantic/test_buffer_store_format.py): a buffer_store_format_xyzw whose V# FORMAT field is user data. On origin/main (aa2988cc) it fails — the module has %fmt_size_arr = OpTypeArray %uint %c80 with no %c80 = OpConstant %uint 128; with this PR it passes and the same module is spirv-as/spirv-val clean (--target-env vulkan1.2, SPIRV-Tools from Vulkan SDK 1.4.363.1).
  • python -I -m unittest discover -s tests/semantic -p test_buffer_store_format.py: 40 tests, OK (39 before this PR).
  • -p test_buffer_load_format.py: 34 OK; -p test_buffer_format_narrow_int.py: 3 OK; -p test_buffer_tbuffer_format_d16.py: 7 OK.
  • python -m compileall -q make_game.py tools tests: exit 0.

I did not re-run the full corpus: the trigger needs buffer_store_format_* at a run-time V#, and the fix only moves declarations, so counts are expected unchanged. I also did not reproduce one of @throny's four shaders directly — the reproduction is the hand-written store above, which fails with the same missing id.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. — No new rule; this restores the module's declaration order.

Depends on: nothing.

…f them

assemble() writes the OpConstant section in one loop over gen.consts, and the
%fmt_size_tab block only asked cu() for the array length and the six element
sizes afterwards. Anything registered there was referenced by %fmt_size_arr and
%fmt_size_init but never declared, which spirv-val rejects with "Operand '...'
requires a previous definition".

A buffer_load_format_* also decodes the format, and that path registers 128 on
its own; a buffer_store_format_* at a run-time V# decodes nothing, so the
array length was the undeclared id. Register the table's constants where
assemble() already pre-registers the ones its later blocks ask for, and share
the size list through Gen.fmt_size_table().
@LocranKing

Copy link
Copy Markdown
Contributor

Tested 84de12f on Windows 11 Build 26200 / RTX 3060 Ti / driver 621.01 / Python 3.12: pass.

  • python -I -m compileall -q make_game.py tools tests: exit 0
  • python -I make_game.py --help: exit 0
  • python -I tests/semantic/test_buffer_store_format.py -v: 39/39 tests passed in 0.278s (including new TestRuntimeFormatSizeTable.test_runtime_format_store_declares_every_table_constant, verifying array length 128 constant %c80 and table constants are declared before emission).
  • python -I tests/test_rdna2spirv_cli.py: 25 tests passed (1 skipped) in 4.128s; python -I tests/test_rdna2spirv_decode.py: 24/24 tests passed in 0.029s.

AI-assisted. No game files.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent CPU comparison on Windows with uv CPython 3.12.13 and SPIRV-Tools 2026.4. For a synthetic runtime-V# d16 store, both assemblers produced a module on main aa2988cc, then spirv-val rejected the undefined table-length constant. Both this head 84de12f7 and #3902 c225f859 validate that shader with both assemblers and match its expected CPU memory words. Load-only and load+store controls were byte-identical across refs in both assemblers. The existing test_u16_high_half.py went from 32/33 on main to 33/33 on each fix, and #4071’s new declaration test also passes against #3902’s translator.

This confirms the coordinator’s overlap note. The dry merge is clean (tree 894614ca) but adds no coverage for the undefined-length failure beyond either fix alone. #4071 pre-registers the format-size constants. #3902 also covers other late uint gen.cu() calls. #3902 verification with the semantic recovery stack. Synthetic inputs only. AI-assisted (Codex).

@Ayinaki

Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Withdrawing this as the duplicate of #3902.

@KoertLichtendonk's overlap note sent me to compare, and throny's own status comment (#3918) had already said the same thing on the day the finding was posted: fearkov claimed it at 22:20, which is before my claim at 23:14. I missed that comment when I claimed. The finding link in this body was wrong as well — throny's finding is 7086903759.

Compared both heads myself on the same two synthetic store forms this PR used (buffer_store_format_xyzw and buffer_store_format_x, V# format left as user data):

aa2988cc this head 84de12f7 #3902 c225f859
%fmt_size_arr present yes yes yes
constants referenced but never declared %c80 none none
spirv-val (vulkan1.2) reject pass pass

#3902 also covers fmt_mask_arr and the other late gen.cu() callers generically, which this PR does not, so it is the one to carry.

The one piece worth keeping from here is the store-side regression test. A buffer_load_format_* also decodes the format, and that path asks for 128 through 1 << (w-1), so the load tests still validate on main while a buffer_store_format_* at a run-time V# decodes nothing. I verified the test passes unchanged on #3902's tree (40 of 40 in tests/semantic/test_buffer_store_format.py) and offered it there; it is red on aa2988cc.

CPU only, synthetic inputs, no game files.

@Ayinaki

Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@LocranKing, @whattheflop: thank you, both checks still count, they just now describe #3902.

@LocranKing: the run you did (39 of 39, then 40 of 40 with the new TestRuntimeFormatSizeTable) is the coverage I have offered to #3902, and I re-ran that same file against its head c225f859 before withdrawing this PR: 40 of 40, red on aa2988cc.

@whattheflop: your comparison is what settled it for me — #3902 and this head both validate the runtime-V# store, #3902 also carries fmt_mask_arr and the other late gen.cu() callers, and the dry merge adds no coverage on top of either. The store-side fixture is the only piece worth carrying, which is what I have offered there.

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