Skip to content

shader_library: fix the #4343 review findings; recount with llvm-mc 21 and 23 - #6813

Closed
FIocker wants to merge 1 commit into
ProjectLeonida:mainfrom
FIocker:fix/shader-library-none
Closed

FIocker wants to merge 1 commit into
ProjectLeonida:mainfrom
FIocker:fix/shader-library-none

Conversation

@FIocker

@FIocker FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #4343. Claimed in #5781 (comment).

Review findings on #4343

From @tupacfan04's review:

  • A compute record field can be NONE. ref() allows it, but parse() indexed the blob table with a NONE layout (IndexError, not LibraryError), and main() decoded a NONE state. Now:
    • a record without a program names no stage;
    • a record without a layout links no digest;
    • a record without a state gets no dispatch fields;
    • library.json gives a missing program as null.
  • find_library inflated any false zlib header without a limit, up to about 132 MB per stream. Each stream now inflates to at most 64 KiB + 1 bytes: a library block is at most 64 KiB, so a longer stream isn't one. A header that asks for more than deflate's 1032:1 ratio of the bytes after it is rejected.

Doc corrections (docs/shader_library.md)

  • 91 compute programs set TG_SIZE_EN, not 90. The 90 was the refusal count; one program is refused earlier. No program is named by two records that disagree on the bit.

  • A default run writes 21,521 programs (@bielfreedom's count); --stages all adds the slot-6 one.

  • §5 named the wrong llvm-mc version. It said llvm-mc 23, but its counts were made with 21.1.8. LLVM 21 and 22 can't decode the scalar register groups that run into vcc, which these programs use often. LLVM 23 added VCCTuple for them and prints [s100,...,vcc_lo,vcc_hi], which the translator already reads. §5 now compares the versions at main 561b65a8 (counts only):

    VS MS PS CS total of 21,521
    llvm-mc 21.1.8 1,296 6,744 6,119 1,392 15,551
    llvm-mc 23.1.5 1,335 7,427 6,442 1,495 16,699
    23.1.5 with rdna2spirv: write the compute thread-group SGPR (TG_SIZE_EN) #6446, shader_ir: read LLVM 23's lit(V) operand as the constant V #6808, rdna2spirv: s_bfm_b64 in wave32 code is 64-bit data, not a lane mask #6809 1,340 7,473 7,871 1,860 18,544

    §5 also lists what stops the rest. The largest are 1,731 programs with words no llvm-mc version decodes (mostly VOPC with a DPP16 source) and 767 with v_cmp_*_e64 vcc_hi in wave32 code.

  • §4 says to use llvm-mc 23.

Tests

  • tests/test_shader_library.py: 12 passed. The three new tests (NONE fields, too-long stream, impossible size) fail on main.
  • tests/test_docs_*.py: 45 passed.

No game data. AI-assisted (Claude Code). Windows 11, Python 3.12, no GPU used.

🤖 Generated with Claude Code

…h llvm-mc 21 and 23

Review findings on ItsNotRexed#4343 (@tupacfan04):
* A compute record may hold NONE in any field (ref allows it), but parse()
  indexed the blob table with a NONE layout (IndexError) and main() decoded a
  NONE state. A record without a program now names no stage, one without a
  layout links no digest, one without a state gets no dispatch fields, and
  library.json gives a missing program as null.
* find_library inflated any false zlib header without a cap, up to ~132 MB per
  stream. Each stream now inflates to at most BLOCK + 1 bytes (a library block
  is at most 64 KiB, so a longer stream isn't one), and a header that asks for
  more than 1032:1 of the bytes after it is rejected.
Three new tests fail on main (the IndexError, the ratio, a too-long stream with a
library header) and pass here.

docs/shader_library.md:
* 91 compute programs set TG_SIZE_EN, not 90 (90 was the refusal count; one
  program is refused earlier).
* A default run writes 21,521 programs: --stages all adds the slot-6 one
  (@bielfreedom).
* Section 5 said llvm-mc 23, but those counts were made with 21.1.8, which can't
  decode the vcc tuples (LLVM 23 added VCCTuple). The section now compares 21.1.8
  (15,551), 23.1.5 (16,699) and 23.1.5 with ItsNotRexed#6446, ItsNotRexed#6808 and ItsNotRexed#6809 (18,544) at
  main 561b65a, and lists what stops the rest. Section 4 says to use llvm-mc 23.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Juryii

Juryii commented Nov 23, 2026

Copy link
Copy Markdown

Independently checked fd17a67a930334d21c9fb199bd8180ffed57c5c6 on Windows / CPython 3.12.5, synthetic inputs only.

  • python -I tests/test_shader_library.py -q: all 12 tests pass. git diff --check passes; checkout clean.
  • Additional full parse/export checks cover all 16 present/NONE combinations of the four compute references (state, blob, program, layout), using main(... --stages all). Checked stage/digest associations, null versus named programs in library.json, and whether exported metadata includes the compute state. All pass. Against the exact base tool 1cc6e3c12, 14 combinations instead raise IndexError, KeyError, or TypeError.
  • Additional archive round trips cover exact library lengths 128, 65535, 65536, 65537, 131072 and 131073 bytes, at zlib levels 0/1/6/9, with compressible and deterministic pseudorandom payloads: all 48 recover identical bytes and parse successfully.
  • A false zlib stream inflating to 4 MiB of zeros before a valid synthetic library recovers the same library on both revisions. In a separate tracemalloc interval, peak Python allocations are 9,839,328 bytes at base versus 176,709 bytes at head. This is a synthetic allocation comparison, not process RSS or a real-archive benchmark.

The same independent harness also passes on WSL Ubuntu 22.04 / Python 3.10.12: all 16 NONE combinations and 48 archive round trips, with the same 14 exceptions at base; traced peaks there are 9,839,856 versus 176,705 bytes.

No blocking findings in those code paths. I did not validate the game-derived corpus counts, LLVM-version results, disassembly, or GPU behavior. AI-assisted (Codex); the commands and independent harness were run locally.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

tools/shader_library.py bounds find_library() inflation to BLOCK + 1 bytes and rejects headers whose declared size exceeds deflate's maximum ratio from the remaining archive. Compute records now tolerate NONE in any field, so stage/digest/link building and cs_state skip nulls. Documentation and tests cover oversized streams, impossible sizes, and null compute fields.

Why merge

Unbounded decompression of false headers wasted memory and could raise on malformed archives, and real libraries contain compute records with null references. Hardening the scan and accepting those records makes library discovery robust on the shipped data.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

tools/shader_library.py bounds find_library() inflation to BLOCK + 1 bytes and rejects headers whose declared size exceeds deflate's maximum ratio from the remaining archive. Compute records now tolerate NONE in any field, so stage/digest/link building and cs_state skip nulls. Documentation and tests cover oversized streams, impossible sizes, and null compute fields.

Why merge

Unbounded decompression of false headers wasted memory and could raise on malformed archives, and real libraries contain compute records with null references. Hardening the scan and accepting those records makes library discovery robust on the shipped data.

Verdict: Approve

@airp1ane

Copy link
Copy Markdown
Contributor

Tested fd17a67 on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_shader_library.py - all exit 0.

AI-assisted (Claude Code).

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