Skip to content

test(semantic): add translator tests for v_sad_u8 - #5694

Open
slopgobblercobbler wants to merge 10 commits into
ItsNotRexed:mainfrom
slopgobblercobbler:test/semantic-valu-sad-u8-suite
Open

slopgobblercobbler wants to merge 10 commits into
ItsNotRexed:mainfrom
slopgobblercobbler:test/semantic-valu-sad-u8-suite

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

What

Adds translator-backed regression tests and reference oracle for v_sad_u8 in tests/semantic/test_valu_sad_u8_hw.py.

Covers:

  • Reference mathematical model matching RDNA2 hardware specification for unsigned byte-level sum-of-absolute-differences with 32-bit accumulator
  • Structural AST operand typing and register allocation validation
  • Deterministic scalar and multi-lane SIMD divergence under Wave32 / Wave64 EXEC masking
  • Register aliasing and in-place operand preservation (accumulator aliasing)
  • Condition code preservation verification (SCC / VCC untouched by VALU instruction)
  • Boundary extremes: max per-byte differences, 32-bit wrapping accumulator overflow, raw IEEE 754 floating-point bitfields
  • Negative controls, mutation resistance (unsigned vs. signed diff, missing accumulator), and end-to-end SPIR-V assembly translation through tools/rdna2spirv.py via support.translate_lines
  • Fragment execution and MRT0 export verification with spirv_mini_interp

Also adds from __future__ import annotations to tools/rdna2spirv.py for Python 3.9 compatibility with dict | None type annotations.

Tested

  • Tested with Python 3.9+ (python -I)
  • python -I tests/test_semantic_translator_use.py (Passes translator reachability check)
  • python -I tests/semantic/test_valu_sad_u8_hw.py (All tests OK)
  • Standard library Python only (unittest, struct, math)
  • Clean merge against upstream/main
  • Strictly zero game data, binaries, or proprietary keys

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested cf53e2bad19ebf1d901be40cca1c52e1a23e8778 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

At cf53e2ba, all 31 tests in the new file pass even if the translator swaps the two most significant bytes of the second v_sad_u8 source during extraction. A two-case synthetic compute shader distinguishes the error on a Linux RTX 5090:

Source 0 Source 1 Accumulator Submitted head Swapped bytes
0xff000000 0xff000000 5 5 515
0xff000000 0x00000000 5 260 260

Both modules passed SPIR-V validation. In the first case the inputs are identical, so all four differences should be zero. Swapping bytes 2 and 3 in source 1 adds two differences of 255. A translator-backed assertion for that first row would catch the regression; the all-zero second source is insensitive to the permutation.

AI-assisted verification.

@CaravelaLabs CaravelaLabs 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 cf53e2bad (CPU only, no GPU, no game files).

  • python3 -I tests/semantic/test_valu_sad_u8_hw.py: 31 tests, 0.049 s, OK
  • python3 -I tests/semantic/test_alu_sad.py (already on main): 5 tests, 0.708 s, OK
  • python3 -I tests/test_semantic_translator_use.py: 3 tests, OK

v_sad_u8 is already CPU-checked by tests/semantic/test_alu_sad.py (#544): a 9x9 edge grid, 40 seeded cases, an SGPR source, and clamp rejected, all through rdna2spirv.translate. The contract row in docs/design.md names that file.

This PR adds a second file. 8 of the 31 methods call support.translate_lines. The rest never reach the translator:

  • TestSymbolicAST, TestScalarInterpreter, TestSIMDDivergence, test_vcc_and_scc_preservation, and the "negative controls" exercise a private AST/interpreter defined in the test file. A bug in tools/rdna2spirv.py would not fail them. The EXEC-masking and SCC/VCC cases only assert that the private interpreter does not write those fields.
  • test_mutation_unsigned_vs_signed_diff / test_mutation_accumulator_missing compare ref_sad_u8 with itself; they do not patch the translator.
  • The translator cases that do run (0x04030201 vs 0 = 10, acc 50 -> 60, dst=src alias, 0x80000000 vs 0 = 128) are a subset of test_alu_sad.py.

The one-line from __future__ import annotations in tools/rdna2spirv.py is unrelated to the tests.

I would not merge this as extra coverage of v_sad_u8.

AI-assisted.

@BeegSchmooze

Copy link
Copy Markdown

Review of cf53e2ba, with a mutation check of the translator path. Windows 11, Python 3.13.

python -I tests/semantic/test_valu_sad_u8_hw.py: 31 tests OK.

To see which translator bugs the new tests would catch, I changed one line of v_sad_u8's translation in tools/rdna2spirv.py (lines 8386–8414 at this head), ran the file, and restored the line after each run:

mutation caught?
accumulator dropped (total + 0 instead of total + S2) yes (4 failures)
only 3 of the 4 bytes summed yes (6)
16-bit lanes instead of bytes yes (6)
UMin in place of UMax (every difference 0) yes (6)
wrapping difference S0.b - S1.b instead of abs no, all 31 pass

The surviving one is the case test_mutation_unsigned_vs_signed_diff is named for. That test only checks Python arithmetic (abs(0x00 - 0xFF)) and never runs the translator. All the translator-backed vectors have every byte of S0 ≥ the matching byte of S1 (0x04030201 against 0), so a missing abs can't show. A translated case with S0 bytes below S1, e.g. v_sad_u8 v3, v0, v1, v2 with v0 = 0, v1 = 0x04030201, v2 = 0 (expected 10; the wrapping version gives a large value), would cover it.

Separately: the from __future__ import annotations line in tools/rdna2spirv.py isn't needed. The project requires Python 3.11+ (docs/testing.md; the README says 3.12+), and dict | None works natively there. #5715 adds the same line in a different place, so the two PRs conflict with each other on it.

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

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Merge hazard with #5715 (checked by merging both onto main 561b65a8): #5694 adds from __future__ import annotations to tools/rdna2spirv.py after the module docstring, #5715 adds it at line 1 above the docstring. Git merges the two cleanly, but the result fails to import: SyntaxError: from __future__ imports must occur at the beginning of the file, so whichever lands second breaks every translator run without a conflict to warn anyone. Simplest fix: drop the rdna2spirv.py hunk from one (or both — the repo already needs Python 3.11+). AI-assisted (Claude Code).

@sovljanskii

Copy link
Copy Markdown
Contributor

A merge clash with #5715, found while checking that PR (I didn't re-test this head otherwise; CaravelaLabs and tylerstraub already checked cf53e2ba):

AI-assisted.

@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

Dropped the rdna2spirv.py future-annotations change to resolve the clash with #5715, and added the S0 < S1 translator-backed mutation test.

@SlopTart

Copy link
Copy Markdown

Head-check pinned to cf53e2bad19ebf1d901be40cca1c52e1a23e8778 (current head is 8d234ca2..., results below are for the older head), AMD Radeon RX 7900 XT box (results are CPU-side):

  • python3 -I -m unittest discover -s tests/semantic -p 'test_valu_sad_u8_hw.py' -> 31/31 OK
  • full tests/semantic: Ran 6181 tests OK (skipped=1, expected failures=1), 582s

Happy to re-run against the current head on request. Posted by Hermes (agent) on my machine.

@BeegSchmooze

Copy link
Copy Markdown

Rechecked 8d234ca2: the rdna2spirv.py change is gone (the PR is test-only now, so the clash with #5715 is resolved), the file passes, and the mutation that survived before (wrapping S0.b - S1.b instead of abs) is now caught (2 failures). Thanks.

One more lead, optional, from a generic mutation run (tools/semantic_mutants.py, branch BeegSchmooze:semantic-mutants) on the previous head: OpBitFieldUExtract → OpBitFieldSExtract on S1's bytes survived, meaning no translated case had an S1 byte ≥ 0x80. If the new cases don't include one, something like v1 = 0x80FF0000 against v0 = 0 would pin it.

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

@SlopTart

Copy link
Copy Markdown

Re-verified after the head moved. At 8d234ca2fa11e8961ad935299085b38972931026: full semantic suite python3 -I -m unittest discover -s tests/semantic -> OK (skipped=1, expected failures=1), same as the previous head. RX 7900 XT, Arch, Python 3.14. No game files involved - your own test suite on bare hardware. Posted by Hermes (agent) on my machine.

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.

7 participants