Skip to content

rdna2spirv: --merge refuses a shader that two parts both claim - #7809

Closed
pes7 wants to merge 1 commit into
ItsNotRexed:mainfrom
pes7:fix/merge-overlap
Closed

pes7 wants to merge 1 commit into
ItsNotRexed:mainfrom
pes7:fix/merge-overlap

Conversation

@pes7

@pes7 pes7 commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue: #7496

What and why

I went looking for a performance problem in --merge and did not find one: 26,400 entries in 8 synthetic parts merge in tens of milliseconds (4 µs per entry, linear — json.load per part plus dict.update). What the same reading turned up is a correctness hole next to the checks it already has.

merge_shard_parts() refuses an incomplete shard set, markers that disagree with the parts, a plan that disagrees with the markers, and a complete-but-empty set — all so a partial index.json is never published. It does not refuse the same shader appearing in two parts: merged.update(doc) publishes whichever part was read last and drops the other without a word.

That is reachable without anyone doing anything wrong. A bucket assignment moves when the directory listing changes, so parts written before a shader was added or removed can overlap parts written after; each part is valid and the set is complete, so every existing check passes, and index.json ends up describing one of two translations of that shader — possibly the one whose .spv this run did not write, while the other stays on disk.

The merge now intersects the keys it already holds with the part it is reading and raises ShardSetError naming the shaders and what to do:

ShardSetError: 1 shader(s) are in more than one part, for example ps_001_0000;
the file set changed between shard runs, so delete the parts and run every shard again

Cost

The check is a dict-key intersection per part, and it does not show up in the merge time: on the 26,400-entry synthetic set (8 parts) the merge is still tens of milliseconds, and the same measurement before the change gives the same order of magnitude. This is a small change on purpose — the point is that the fail-closed policy the function already follows now has no gap in it.

Tested

  • python -I tests/test_rdna2spirv_merge.py: 31 tests, OK (30 before; one new case with two parts that share a shader, asserting the refusal and that the message names it).
  • All 52 pixel and vertex fixtures still translate to byte-identical SPIR-V (sha256 157189dc...).
  • The synthetic merge timing above; synthetic inputs only, no game data.

cc @ItsNotRexed @KoertLichtendonk.

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. (Not applicable: no translation change.)

merge_shard_parts() checks that the shard set is complete, that the markers agree with the parts, and that the plan
agrees with the markers, all so a partial index is never published. One case was left: the same shader in two parts.
`merged.update(doc)` then published whichever part was read last and dropped the other silently.

That is reachable without anyone doing anything wrong. A bucket assignment moves when the listing changes, so parts
written before a shader was added or removed can overlap parts written after; the parts are individually valid and
the set is complete, so every existing check passes, and the index ends up describing one of two translations of the
same shader -- possibly the one whose .spv the current run did not write.

The merge now intersects the keys it already has with the part it is reading and raises ShardSetError naming the
shaders and saying what to do (delete the parts, run every shard again). The check is a dict-key intersection per
part: on a synthetic set of 26,400 entries in 8 parts the merge still takes tens of milliseconds, so there is no
measurable cost.

tests/test_rdna2spirv_merge.py: one new case, two parts that share a shader, expecting the refusal and the name.
@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

Duplicate of #3306

#3306 makes the same change and was opened earlier. Topic: rdna2spirv --merge: refuse a shader named by two parts.

Suggest closing this PR in favour of #3306, and porting anything this one adds that #3306 lacks there.

@bobcatchris

Copy link
Copy Markdown
Contributor

Code read at b26fa62, Windows 11 (26200.9974), CPU only: skorex's duplicate claim checks out.

  • Both PRs change the same two files (tools/rdna2spirv.py, tests/test_rdna2spirv_merge.py) and add the same guard in merge_shard_parts(): detect a shader named by two parts and raise ShardSetError.
  • rdna2spirv: refuse a shader named by two --merge parts #3306 (opened 2026-11-22 20:03 UTC) is earlier than this one (2026-11-23 17:35 UTC) and already adds two tests, one at the function level and one through the CLI. This branch adds one function-level test.
  • The only substantive difference is the diagnostic: rdna2spirv: refuse a shader named by two --merge parts #3306 names both conflicting part files, while this branch reports the count plus up to three shader names and suggests deleting all parts and rerunning the shards. The safety property is the same.

I did not build or run either branch's tests here and did not close either PR — just confirming the overlap for the maintainer. No game data used.

AI-assisted.

@pes7

pes7 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Duplicate — closing. #3306 (bobcatchris, opened 2026-11-22) makes the same change and does it better: it names both part files as well as the shader, so there is nothing here worth keeping.

For the record, the measurement that led to it, in case it is useful to #3306's review: --merge has no performance problem to fix (26,400 entries in 8 synthetic parts merge in tens of milliseconds, ~4 µs per entry, linear), and the overlap check is a dict-key intersection per part that does not move that number. All 52 pixel and vertex fixtures still translate to byte-identical SPIR-V with the check in place.

@azterixx

Copy link
Copy Markdown

checked: #7809 | with: python -I -m unittest discover -s tests -p test_rdna2spirv_merge.py | on: macOS 27.2.1, Apple M1 Pro, CPython 3.15.2, head b26fa62, CPU only | result: pass (31 tests; main@dde82f27e has 30)

@Tresillo2017

Copy link
Copy Markdown

Tested b26fa62 on RTX 2060 (Turing, NVIDIA 621.24), Windows 11, Python 3.13.1: pass.

  • python -I tests/test_rdna2spirv_merge.py: 31 tests OK.
  • With tools/rdna2spirv.py reverted to the parent, the new test fails ("ShardSetError not raised"), so it covers the change.
  • python -I scripts/run_semantic_tests.py tests/semantic: 5821 tests, 433 files, one file fails: test_float_nan_fract (3 failures in the f32 NaN payload tests, e.g. v_med3_f32). The same 3 fail with the parent's rdna2spirv.py, so they are not from this PR.
  • python -m compileall -q make_game.py tools tests: clean.
  • Read the diff: it matches the description (key intersection per part, message names the shaders and says to rerun every shard).

Not run: the GPU suite, since the change does not touch translation. Synthetic inputs only, no game data. AI-assisted check.

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.

5 participants