Skip to content

shader_disasm: feed llvm-mc 32-byte lines, not one line per program - #4210

Open
FIocker wants to merge 2 commits into
ItsNotRexed:mainfrom
FIocker:perf/disasm-input-lines
Open

FIocker wants to merge 2 commits into
ItsNotRexed:mainfrom
FIocker:perf/disasm-input-lines

Conversation

@FIocker

@FIocker FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #4084 (comment)

What and why

shader_disasm.disasm sent llvm-mc the whole program as one line of hex. llvm-mc echoes the full source line under every invalid instruction encoding warning, so stderr grew with warnings × program size. A large program with some undecodable words took minutes in one call and wrote hundreds of MB that were then copied into the .s file. I hit this on the 2,684 compute programs of the pipeline library in shaders.rpf: up to 160 KB each, 976 of them with invalid words. The set didn't finish in 30 minutes with 15 jobs.

hex_lines() now writes 32 bytes per line. llvm-mc reads the lines as one byte stream, so the decode doesn't change. A line always holds whole words, so each warned word is still on its echoed line, where tools/shader_profile.py (warning_words) reads it. fspx_unpack.py gets the same change, because it calls shader_disasm.disasm.

Tested

  • 600 random carved FSSM blobs from my own work folder, old function against new: identical stdout in 600/600, the same invalid-word count (312 in total), and identical shader_profile.warning_words results in 600/600. Instructions that straddle a line break decode the same, also checked with one word per line.
  • The same 2,684 library compute programs: 24 s, previously not finished after 30 min.
  • python -I tests/test_shader_disasm.py (new test_input_lines_hold_whole_words), tests/test_shader_profile.py and tests/test_text_write_line_endings.py pass.
  • Windows 11, llvm-mc built from scripts/build_llvm_mc.ps1 (LLVM 23). No GPU involved.

AI-assisted (Claude Code).

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 translator rule.)

🤖 Generated with Claude Code

llvm-mc echoes the whole source line under every "invalid instruction
encoding" warning. With the program on one line, stderr grew with
warnings x program size: on the pipeline library's compute programs (up
to 160 KB, 976 of 2,684 with invalid words) single calls ran for minutes
and the set didn't finish in 30 min at 15 jobs. With 32-byte lines it
takes 24 s.

llvm-mc reads the lines as one byte stream, and whole-word lines keep
each warned word on its echoed line, where shader_profile.py reads it.
On 600 random carved FSSM blobs (312 invalid words) the disassembly, the
invalid-word counts and shader_profile's warned words are identical.

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

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested acb06c6 on Windows, CPython 3.14.7, CPU only (no GPU, no game files): pass for the formatter and for the profile reader.

python -m pytest -q tests/test_shader_disasm.py gives 20 tests passed and 6 subtests passed. python -m pytest -q tests/test_shader_profile.py gives 38 tests passed and 99 subtests passed. I used a virtual environment with tests/requirements.txt.
With tools/shader_disasm.py from main (aa2988c), the new formatter test fails.

I gave bytes(range(256)) * 512 to the disassembly function and intercepted subprocess.run.
All 131,072 bytes come back unchanged. This head makes 4,096 lines of 159 characters. Main makes one line of 655,359 characters.
The tracemalloc peak before the receiver processes the text is 1,507,888 bytes at this head and 7,694,368 bytes at main. For 32,768 input bytes, the peaks are 377,776 and 1,915,936 bytes.

I also gave synthetic LLVM warnings at the first word and at the last word of four sequential lines to shader_profile.warning_words. All eight words match.

I did not test the real llvm-mc and its error output. I found no defect. AI-assisted.

@sds-228 sds-228 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.

Code review of acb06c6: no issues found. Disassembly input is split into whole-word lines so warning echoes stay bounded; a test confirms byte order is unchanged. Targeted suite: 20 tests OK.

AI-assisted (OpenCode).

@memorypressure

Copy link
Copy Markdown

Verified on Linux with real llvm-mc (LLVM 23.1.4): merges cleanly into main, compileall clean, and all 20 tests pass under python -I (19 on plain main plus yours). End-to-end against llvm-mc on a hand-written FSSM (s_nop, v_mov_b32_e32, 512 invalid data words, s_endpgm), your version and main's produce byte-identical disassembly bodies and identical invalid-word counts, while the listing shrinks from 7.9 MB to 146 KB; shader_profile.py parses both message sections to the same result (512 warnings, one distinct word, 0 unresolved), as expected now that line boundaries cannot split a word (LINE_BYTES % 4 == 0). No regressions found. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

…ines

# Conflicts:
#	tests/test_shader_disasm.py
#	tools/shader_disasm.py
@FIocker

FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Merged main a847ee64 to resolve the conflicts with the new Python decoder backend. disasm() keeps main's backend/wave32 parameters and now feeds llvm-mc hex_lines(code). Both new tests are kept (test_input_lines_hold_whole_words and main's test_code_range_stops_before_a_constant_data_block). tests/test_shader_disasm.py + tests/test_shader_profile.py: 69 passed. AI-assisted (Claude Code).

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested bc29d9d7 merged onto main 8575d36e on Windows 11, CPython 3.14.3, CPU only: pass.

  • python -I tests/test_shader_disasm.py: 30 tests OK.
  • Negative control (tools/shader_disasm.py from main, the PR's tests kept): tests/test_shader_disasm.py 30 tests FAILED (errors=1).
  • py_compile on the 2 changed Python files: OK; git diff --check: clean.

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

quackottu added a commit to quackottu/Leonida that referenced this pull request Nov 23, 2026
The fallback read each warning's byte offset from its column, which holds
only while the program goes to llvm-mc as one line. ItsNotRexed#4210 splits it into
short lines; take the offset from the warning's line and column in the
text that was sent, so the splice works with either input layout.
@quackottu

Copy link
Copy Markdown
Contributor

Measurement in favour of this, llvm-mc 21.1.8 gfx1013 on Windows: one ~98 KB pipeline-library program took 43 s as one input line and 0.63 s as 64-byte lines, with byte-identical stdout and the same number of invalid-word warnings. Over the library corpus this is the difference between hours and minutes for shader_library.py --disasm. (#7104 places llvm-mc's warnings by line and column, so it works with this change too.)

@mewajda

mewajda commented Nov 23, 2026

Copy link
Copy Markdown

Code read (CPU) — Linux, LLVM 18.1.8 (llvm-mc), no GPU, no game files.

Tested the premise and the one consumer this can break, since stderr shrinking is only safe if the decode and shader_profile.py's word recovery both survive.

  • python -I tests/test_shader_disasm.py: 30/30 pass.
  • The decode is unchanged. Feeding a program as one line and as 32-byte lines gives byte-identical llvm-mc stdout (gfx1030, -show-encoding). Through the tool itself on 20 real blobs from my own carve (gfx1013): the instruction text before the ; --- llvm-mc messages --- marker is identical in 20 of 20, and the .s files are the same size to the byte.
  • The stderr saving is the one the body predicts. An 8 KB program with 2,048 invalid words: 125,924,820 bytes of stderr in one line against 585,376 in 32-byte lines — 215× — with the same 2,048 warnings. At 1 KB it is 1,977,800 against 72,952.
  • tools/shader_profile.py still recovers every word. That is the risk of this change: warning_words() finds a warning's word by the column llvm-mc reports, not by assuming one word per line. On a 1 KB program with 256 invalid words, the messages section built from the new stderr gives 256 resolved, 0 unresolved, and the same 256 words (ff ff ff ff …) as the one-line format. So undecoded stays correct and the change does not need a parser change.

Two small notes

  1. hex_lines does not enforce that a line is a whole word: its last line is whatever bytes remain, so a stream whose length is not a multiple of 4 would split a dword across lines. Shader code is dword-aligned, so this cannot happen today; an assertion or a trailing-remainder test would make that an explicit precondition rather than an assumption.
  2. The new test checks the line count and that every line but the last is full; it does not check that each line is a multiple of 4 tokens, which is the property the docstring actually relies on.

Verdict: the change does what it says, the decode is unaffected on synthetic and real input, and the only in-tree consumer still resolves every word. No blocking issues. AI-assisted.

@kett1m

kett1m commented Nov 23, 2026

Copy link
Copy Markdown

Independent check on Linux, llvm-mc 23.1.5, CPU only, no game data.

  • python -I tests/test_shader_disasm.py: 30 tests, OK.
  • Measured the flood itself, main vs this head, on a synthetic 2 KiB program of undecodable words (208 invalid encodings):
invalid words stderr disassembly time
main 208 3,216,913 B 21,336 B 0.52 s
this head 208 60,284 B 21,336 B 0.02 s

Same count of invalid encodings and byte-identical disassembly, so nothing in the output changed; stderr is 53× smaller and the call 26× faster on 2 KiB. On main that output grows with warnings × program length, which is the gigabytes on the 160 KB pipeline-library programs in the report.

hex_lines keeps whole words on each line, so an echoed line still carries the offending word that shader_profile reads; the unit test pins the line length to a multiple of 4 for that reason.

Nothing to change from my side.

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.

8 participants