Skip to content

tests: catch dangling SPIR-V ids without spirv-as/spirv-val - #4710

Open
FZNull wants to merge 1 commit into
ItsNotRexed:mainfrom
FZNull:test/spirv-undefined-ids
Open

FZNull wants to merge 1 commit into
ItsNotRexed:mainfrom
FZNull:test/spirv-undefined-ids

Conversation

@FZNull

@FZNull FZNull commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Problem

aa2988cc built %fmt_size_arr at assembly time from gen.cu(128) and the format-size constants after assemble() had already dumped the OpConstant %uint list, so a shader using the dynamic-format table declared its array from an id that was never defined. The text assembled but spirv-val rejected the binary with Operand 'N[%N]' requires a previous definition.

That specific defect is fixed on main (registering the table constants in the typed-load path; e.g. 06b47de1, and fmt_int_flags() registers the format-mask constants at use time), and tests/test_u16_high_half.py passes there. This PR does not change the translator: it adds the regression guard that would have caught the defect without the external tools.

Why a guard is still needed

tests/test_u16_high_half.py assembles and validates the translator output, but returns early when spirv-as / spirv-val are not on PATH (conftest.HAVE_SPIRV false). In that configuration the module is never checked: on the parent commit pytest tests/test_u16_high_half.py -q reported 33 passed while the module was invalid. Any CI job or contributor machine without the optional tools has the same hole, and the same class of bug (a gen.cu() call placed after assemble() dumps the constants) can come back.

What the test does

tests/test_spirv_undefined_ids.py checks the module text that rdna2spirv.translate() already returns. It never invokes llvm-mc, spirv-as or spirv-val, so it runs everywhere.

  • undefined_ids(): every %name referenced by a line must be defined by some %name = Op... line. The check is generic over every id, not only %cXX: a missing definition is invalid for any id, so no forward-reference exception is needed.
  • used_before_definition(): a type or constant used by another type or constant declaration before its definition line. Forward references are legal only for labels, branches and switches, merge operands, OpPhi and function ids, so the order check is deliberately limited to type/constant declaration operands (and skips OpTypeForwardPointer, which the translator does not emit).

Twelve cases: two store-only dynamic-format listings (one is the minimized sequence from tests/test_u16_high_half.py), one dynamic-format load, two controls (a plain pixel shader and a fixed-format MTBUF store), two existing tests/semantic/fixtures/disasm compute fixtures, and checker self-tests (legal forward references accepted; undefined %c80, %fmt_size_len and %t9000 reported; a constant defined after the type that uses it caught by the order check).

Evidence

The test fails on the parent of the upstream fix and passes now:

# 03b09a08, without SPIRV-Tools on PATH (HAVE_SPIRV false, no llvm-mc)
python -m pytest tests/test_spirv_undefined_ids.py -q
2 failed, 10 passed
E  AssertionError: ps_store_fmt_d16_xy: the module references ids it never defines: ['%c80']

# current main 98fa503f
python -m pytest tests/test_spirv_undefined_ids.py -q
12 passed in 0.40s

Cross-check with a local SPIRV-Tools v2026.4 build: on 03b09a08 the module assembles and spirv-val fails with Operand '135[%135]' requires a previous definition; on current main both spirv-as and spirv-val pass.

Related files on current main: 69 passed, 585 skipped, 8 subtests passed (the skips are the tool-gated tests in this tool-less environment), python -I tests/test_ci_registration.py -> Ran 8 tests ... OK, so the existing translator-tests pytest glob picks the new file up and no workflow change is needed.

Limits

The checks verify id existence and type/constant declaration order only, not operand types, dominance, duplicate definitions or instruction form; a module without dangling ids can still be rejected by a real validator. This is the tool-less floor for jobs and machines without spirv-as/spirv-val, not a replacement for validating where the tools are installed.

AI-assisted.

aa2988c built %fmt_size_arr from gen.cu(128) after assemble() had already
dumped the OpConstant %uint list, so a dynamic-format buffer store referenced
%c80 that no line defined. tests/test_u16_high_half.py assembles and validates
the module, but returns early when the tools are not on PATH, so a tool-less
run passed while the module was invalid.

Check the module text instead, which needs no external tool: every %name a line
references must be defined, and types/constants must be declared before their
use. The check is generic over all ids (not only %cXX) and allows the legal
forward references (labels, branches, OpPhi, OpEntryPoint's %main).

@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 464635c1b on Windows 11: pytest tests/test_spirv_undefined_ids.py -q -> 12 passed; git diff --check clean.

The new test walks the translator's emitted ids (definitions, uses, OpPhi/label forms) and reports an id that is used without a definition, so the %c80-style late-constant bug (#3902/#4466) and any future dangling id are caught without needing spirv-as/spirv-val. The positive/negative cases and a synthetic dangling module are covered. No blocking issue; this is complementary to #3902/#4466, not a duplicate.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 464635c1 on Windows 11, Python 3.15.2: pass.

  • python -m pytest tests/test_spirv_undefined_ids.py: 12 passed. This box has no spirv-as/spirv-val, which is exactly the gap this guard fills — it drives the real rdna2spirv/translate_listing path and scans emitted module text, plus positive/negative checker cases (undefined %name reported; legal forward references and late-defined constants accepted).
  • git diff --check + python -I scripts/check_no_game_data.py: clean.

No game files. AI-assisted (Devin).

@memorypressure

Copy link
Copy Markdown

Verified on Linux, CPU-only. Merged with current main: compileall clean, pytest tests/test_spirv_undefined_ids.py 12/12 (re-run here), and python -I on the file fails loudly (ModuleNotFoundError: pytest) rather than silently running nothing. The story checks out at the quoted commits: at aa2988c the two dynamic-format store cases fail on the dangling %c80, while the pre-registration fix 06b47de already predates this PR head — so the suite passes, and the guard earns its place as the SPIRV-Tools-free floor. Closest existing coverage test_u16_high_half also passes (33, real spirv-as/spirv-val on PATH). No game data; fixtures hand-written/synthetic. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 464635c on Windows Server 2022, CPython 3.13.12: pass

  • pytest tests/test_spirv_undefined_ids.py -> 12 passed
  • Test-only change; verifies.

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. 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.

4 participants