Repository navigation
startup: write shader requests from the shader archive before a run - #1750
Conversation
Translation requests were only written when the title created a pipeline, so every new pipeline cost a run before it could be drawn. A traced startup run showed the native programs come from 64-byte aligned zlib streams in the shader archive, byte for byte. tools/startup/archive_requests.py inflates those streams, joins a stream with the one that starts on the next 64-byte boundary, carves programs (36-byte header, s_inst_prefetch, 64-byte blocks on 256-byte steps) and pairs each pixel program with a directly following vertex program. Root signatures are not in the archive, so they come from earlier req_*.root files and every pair is written once per distinct root, with the runtime's key. gfx2hlsl.py --pending then translates them. Measured locally: 4,982 programs, 1,258 pairs, 3,774 requests in 22 s. Eight of the eleven pipelines of a cold run were among them with identical keys and byte-identical translations. Test: startup_archive_requests on a synthetic archive, with controls for unpaired programs, a pair cut off at the end of a stream, a stream off the 64-byte grid, a pair split across two consecutive streams, missing roots and a second run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tedin7
left a comment
There was a problem hiding this comment.
Review of head 615189a (comment only, not an approval). First review of this PR; the base is a4d17cf and main is now 10 commits ahead. I read the diff, not the existing comments.
Verdict: 0 P1 / 2 P2, plus nits.
P2-1: inflate is unbounded (tools/startup/archive_requests.py, chunks() and streams())
inflater.decompress(window)has nomax_length, andstreams()keeps every consecutive stream injoineduntil a gap appears. A stream of at most 128 KiB can inflate to about 100 MB.- Repro:
python3 -B tools/startup/archive_requests.py bomb8.bin cache --roots rootswith a 796 KB archive of 8 consecutive zlib streams, each inflating to 100 MiB:/usr/bin/time -vshowsMaximum resident set size 1669384 kB, exit 0. - The PR's premise is that a stream inflates to 64 KiB, so a
max_lengthcheck (reject above about 1 MiB) and a cap onjoinedare cheap. The archive is the user's own input, hence P2.
P2-2: the tests do not guard several behaviours the PR text and docs claim
- "Every mutant failed" covers only the 5 listed mutants. Of my production mutants, 5 were not caught (table below).
- The test computes expected keys with
gfx2hlsl.pipeline_key, the function the tool uses, so a wrong key formula or argument order cannot be seen; it also asserts stdout text such aspairs=4 written=8 streams=3.
No finding
- Path traversal: output names come only from
'req_%016x' % key, the archive carries no names, and the cache and roots paths come from argv. - Atomic publish: each file is written to
<name>.<pid>.tmpandos.replaced in the order.b,.root,.a; a concurrent run writes identical bytes, and the.acheck gives idempotence. - Garbage input: 300 random programs raised nothing except
gfx2hlsl.Unsupported; a truncated stream is skipped; an empty archive returns early. Fixtures are synthetic.
Run (worktree at the head)
python3 -B tests/startup_archive_requests_check.py: passed.tests/startup_gfx2hlsl_capture_check.py: passed. docs_links_check (17 files, 365 links), bidi_controls_check (1300 files),git diff --check origin/main...HEAD: clean.- Production mutations in archive_requests.py (each restored with
git checkout --). Caught: keep a program that reaches the end of its stream; pair a vertex program with any previous program; drop the root dedupe; drop the existing-.askip; swap thepipeline_keyarguments; drop the 64-byte rounding of the stream end; do not join consecutive streams. NOT caught:STEP0x100 -> 0x40; writing.afirst instead of last (the marker order is untested); dropping the header-tag check; dropping theat % 4 == 0check; keeping empty root files.
Nits
pipeline_key(vs_code, ps_code, root)is called with(ps, vs); consistent withtranslate_requestbut the parameter names mislead, so a comment would help.read_rootsusesopen().read()without closing; a failed write leaves a.tmpfile; zero streams or zero pairs exits 0, so pointing it at a wrong file looks like success;req_*.rootfiles from older runs are read from the cache by default (documented).- The docs paragraph runs the "Check with ..." sentence into the measurement paragraph.
- Overlap: #1749 (parallel translation during a run) reads
req_*.aas the ready marker this PR relies on, and #1641 touches the same area; all three edit the CMake and AGENTS.md test lists, so expect trivial conflicts. I did not check them semantically.
Not run: PowerShell, run.ps1, the real archive, the fxc translation, ctest, YAML parsing of windows.yml (PyYAML missing; the change is one list line). C++, llvm-mc and shader forms do not apply to this diff.
Linux limits: no Windows, WARP, DXC or GPU here; LLVM 19 only (CI pins LLVM 15). Anything that needs them was not run.
AI-assisted; commands listed were executed locally.
- "Directly following" is now enforced: a vertex program pairs only when it starts on the 256-byte step right after the pixel program. Before, any vertex program after a pixel program paired, whatever the gap. - A carved program must contain s_endpgm; one cut short by an all-zero block is counted as incomplete and never paired. - The memory map is closed, and the docs use a neutral <shader archive> placeholder. On the local archive both rules leave the result unchanged (1,258 pairs, 0 incomplete, 8 of 11 recorded keys), so every measured pair was already adjacent. The test adds a gap pair and a program without s_endpgm as controls, and a mutation of each rule is detected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-up. - A zlib stream that would inflate past 1 MiB (a measured one inflates to 64 KiB) is counted as oversized and skipped without inflating it in full, and a joined run of consecutive streams stops growing at 64 MiB. Before, a small crafted archive could make the tool allocate gigabytes. - A part that cannot be written leaves no .a and no temporary file and the run exits with an error; an archive without any pair also fails. Root files are read with a context manager. - The test computes the expected keys with its own copy of the runtime's FNV-1a (start value 1469598103934665603, as in d3d12_draw.inl) instead of gfx2hlsl.pipeline_key, parses the summary, and adds controls for an untagged program, a pair at offsets not a multiple of 4, an empty root file, an oversized stream, a blocked .root write and an archive without pairs. All nine mutants (step size, write order, tag check, alignment check, empty roots, inflate limit, temporary file cleanup, and both failure exits) are detected. The local archive gives the same 1,258 pairs and 8 of 11 recorded keys, with 0 oversized streams. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review. Everything is addressed in P2-1, unbounded inflation.
P2-2, test independence and mutants.
Four more mutants are also detected: the inflate limit removed, a temporary file left after a failed write, a write failure exiting 0, and an archive without pairs exiting 0. Nits.
On the local archive the result is unchanged: 1,258 pairs, 0 incomplete, 0 oversized, and 8 of 11 recorded keys. On the overlap: this PR and #1749 each add one line after AI-assisted (Claude Code). |
Keep both new test registrations in CMakeLists.txt (startup_archive_requests and upstream's startup_gfx2hlsl_op_refusals). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… support main now refuses some forms inside gfx2hlsl.decode (v_cndmask_b32_e64 with NEG on an immediate), and gfx2hlsl.classify goes through decode, so 289 carved programs became "undecodable" and 143 pairs were lost. Which programs pair must not depend on whether they can be translated. stage_of() applies the same export-target rule as classify but reads the disassembler directly. The local archive is back to 1,258 pairs with 0 undecodable programs, and 8 of the 11 recorded pipelines, re-keyed with the current revision, are among the offline requests. The test reads the key revision from src/startup_stubs/d3d12_draw.inl (main bumped it to x2w-startup-shader-4) instead of hard-coding it, and one vertex program now contains that refused v_cndmask form; going back to classify() loses the pair and is detected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R0Y00#1749 adds its check right after startup_gfx2hlsl_capture_check.py in AGENTS.md and the workflow, as this PR did, so whichever merged second conflicted. This entry now sits two lines further down, so the two PRs merge in either order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged current
AI-assisted (Claude Code). |
tedin7
left a comment
There was a problem hiding this comment.
Re-review at head b3d3ea1368d2d4a3af458408f898b6cb5481dfe2 (comment only, not an approval). The earlier review was at 615189af; I read the diff against current main and the author's replies.
Verdict: 0 P1 / 0 P2, two P3. Both P2 findings of the first review are resolved.
Earlier findings
- P2-1, unbounded inflate: resolved.
inflater.decompress(window, MAX_INFLATE)caps one stream at 1 MiB andMAX_JOINEDcaps a joined run at 64 MiB. I rebuilt the earlier repro (8 consecutive zlib streams, each inflating to 100 MiB, an 815,593-byte archive): the run now ends in 0.23 s with a maximum resident set of 27 MB (it was 1,669,384 kB), reportsoversizedand exits 1 with "no program pair found". - P2-2, tests: resolved for the five mutants I listed. I re-ran them, plus others; each makes the test print
FAILnow:STEP0x100 to 0x40,.awritten before.band.root("no ready marker"), header-tag check dropped,at % 4check dropped, empty roots kept. Also caught: inflate limit removed, pair accepted without adjacency,pipeline_keyarguments swapped,complete()always true. The expected keys now come from the test's own FNV-1a and the revision text fromd3d12_draw.inl, the summary is parsed into a dict.
Findings
- P3, the 64 MiB cap on a joined run is not guarded. Mutation:
size + len(data) > MAX_JOINEDreplaced byFalseintools/startup/archive_requests.py(streams());tests/startup_archive_requests_check.pystill passes. The PR text anddocs/startup-runtime.mdstate the cap. A guard needs about 65 consecutive streams of 1 MiB of zeros (a few KB of archive) and a check that the joined run is split. - P3, the key order is an empirical claim. The tool hashes the pixel program first (
pipeline_key(previous[2], program, root)), whilegfx2hlsl.pipeline_keynames its first parametervs_code, and the runtime hashesstages[0]/words[5]first (d3d12_draw.inl:271,:357). The comment added in this head says only the order matters, and the "8 of 11 recorded keys" in the PR text supports a = pixel, but I could not check it here (no real archive or run). The synthetic test pins only that the tool and the test agree.
Other things I checked, no finding
- Path traversal: names come only from
'req_%016x' % key; the archive carries no names. - Write order and failure:
.b,.root,.alast, each through<name>.<ext>.<pid>.tmpandos.replace; a blocked.rootleaves no.aand no temporary file and exits 1 (the test covers it). - Dense hostile input: streams of 0xFF filler with a program header every 68 bytes took 0.8, 1.0, 1.7 and 4.1 s for 64 KiB, 128 KiB, 256 KiB and 1 MB, memory flat at 27 MB. growth was roughly linear in these sizes.
- The merge-driven change (
stage_of()reading the disassembler directly instead ofgfx2hlsl.classify) matches the export-target rule it documents; the test has a vertex program with a refusedv_cndmask_b32_e64form to keep pairing independent of translatability.
Commands run (Linux, worktree at the head)
python3 -B tests/startup_archive_requests_check.py: passed.python3 -B tests/startup_gfx2hlsl_capture_check.py: passed.tests/docs_links_check.py(17 files, 368 links),tests/bidi_controls_check.py(1313 files),git diff --check origin/main...HEAD: clean.git merge-treeagainst currentmain: no conflicts.- Mutations in
archive_requests.pyas listed above, each restored withgit restore;git status --shortclean afterwards.
Limits: Linux only. I did not run PowerShell, a real archive, the fxc translation or ctest, and the Windows 2025 runner script could not be used in this session, so nothing in this diff was run on Windows.
AI-assisted; commands listed were executed locally.
130 consecutive 512 KiB zlib streams (65 MiB inflated, a few KB on disk) must come out as two joined runs. Before, removing the cap still passed the test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks.
AI-assisted (Claude Code). |
tedin7
left a comment
There was a problem hiding this comment.
Retest at head 3543b48 (comment only, not an approval). Earlier review was at b3d3ea1.
Result: no P1/P2 findings. The delta is one file, tests/startup_archive_requests_check.py (13 insertions, 1 deletion), and it resolves the earlier P3 about the 64 MiB joined-run cap: the check builds 130 consecutive 512 KiB zero streams (65 MiB inflated) and requires the summary to report streams=2.
Linux checks run locally in a clean worktree at this head:
- git diff --check origin/main...HEAD: clean
- tests/docs_links_check.py: passed (17 files, 368 links)
- tests/bidi_controls_check.py: passed (1313 files)
- tests/startup_archive_requests_check.py: passed
- tests/startup_gfx2hlsl_capture_check.py: passed
Mutation (local, reverted, tree clean afterwards): in tools/startup/archive_requests.py, 'size + len(data) > MAX_JOINED' replaced by 'False'. The check exits 1 with "FAIL joined run capped at 64 MiB: got '1', want '2'".
Win2025 (Windows Server 2025 build 26100, WARP, coordinator run, full default target set): Debug dev and Release /O2 builds succeeded; startup_stubs and startup_stream_request_warp 2/2 passed in both (Debug 1.49 s, Release 1.53 s). Windows Python check tests/startup_archive_requests_check.py ok (1.4 s).
Still open from the earlier review (P3, unchanged): the key order (pixel program hashed first) is an empirical claim that this session could not check against a real archive.
AI-assisted; commands listed were executed locally.
Keep both test-list entries in AGENTS.md and the workflow (this PR's archive check and upstream's new scalar/SOP2 checks). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r-requests # Conflicts: # .github/workflows/windows.yml # AGENTS.md
tedin7
left a comment
There was a problem hiding this comment.
Review of head 74a3460c77bcf85645b2ac8360ca054512f0c9ef (comment only).
Scope: tools/startup/archive_requests.py (new), tests/startup_archive_requests_check.py (new), docs/startup-runtime.md, plus CMake/CI/AGENTS.md registration of the new check. Title is neutral. Mergeable. CI: Linux Python checks, gfx1013 fixtures and Windows build/test all pass (the /O2 and chart jobs were skipped).
Commands run locally (Linux, worktree at the head SHA):
python3 -B tests/startup_archive_requests_check.py->startup_archive_requests_check passedpython3 tests/docs_links_check.py-> passed (18 files, 396 links)python3 tests/bidi_controls_check.py-> passed (1430 files)git diff --check origin/main...HEAD-> clean
Mutations (each reverted with git checkout -- .):
- Swap the pair order in
pipeline_key(program, previous[2], root)-> test fails (request keys and downstream checks). - Drop the
at == next_step(...)adjacency condition -> test fails (second runcount 10 vs 8). - Disable the
existingearly-return inwrite_request-> test fails (second runwritten 8 vs 0).
All three were killed, so the new logic is well covered.
Findings
- P1: none.
- P2: none.
- nit
archive_requests.py:94: only a zlib header of0x78is accepted. Streams with other CMF/FLG levels (for example0x48) are skipped silently. The docs say "measured archive", so this is fine, but a counter for skipped candidates would make a miss visible. - nit
archive_requests.py:96-98:WINDOWis 128 KiB of compressed input. A valid stream longer than that is dropped without a counter (only the inflated-size cap is counted asoversized). Consider counting it. - nit
archive_requests.py:35:complete()accepts any 4 bytes matchings_endpgmat a 4-byte offset. This could also match an inline literal. Low risk, because the pair is also gated on stage and position. - nit: every pair is written once per distinct root. With many roots in the cache this multiplies files. The behavior is documented, so I only note it.
Not run: Windows/WARP (win2025). This is an offline Python tool with no shader lowering or C++ change, and Windows CI already passed.
AI-assisted; commands listed were executed locally.
Change
Shader translation requests used to exist only after the title created a pipeline, so every new pipeline cost a run before it could be drawn. This adds
tools/startup/archive_requests.py, which writes runtime-keyed requests from the shader archive, without starting the title:How the format was found:
extract_shaders.pyreads a different record structure in the same file and does not see them.What the tool does:
s_inst_prefetch, padded in 64-byte blocks, starting on a 256-byte step. A program ends at an all-zero block or at the next program's header.s_endpgm. A program cut short by an all-zero block is counted as incomplete and never paired.req_<key>.{a,b,root}with the runtime's key:.band.rootfirst, then.a, each through a temporary file and a rename.What it cannot take from the archive (documented):
req_*.rootfrom earlier runs (by default the cache) and writes each pair once per distinct root. Unused combinations cost translation time and nothing else.Refs #1723 (the translator census this population produced, with a per-pair correction in its comments).
Verification
Branch head
754dd51f(615189afplus two commits addressing review findings: strict adjacency ands_endpgm, then an inflate limit, write-failure handling and independent test keys), onmainata4d17cf5. Windows 11 (26300), Python 3.13.The synthetic test builds an archive from documented encodings:
expands_endpgmwords pluss_inst_prefetch, with no title bytes. Each adjacent pixel/vertex pair must become one request per root, with the key computed by the test's own copy of the runtime's FNV-1a (notgfx2hlsl.pipeline_key) and exact parts. Controls that must write nothing:s_endpgm;--rootspath, and a cache without roots (both refused with a message).A pair split between two consecutive streams must be carved whole, and a second run must write nothing new.
Mutation checks, each run against a temporary edit that was then reverted. Every mutant failed the check:
s_endpgmruleSTEP0x100 → 0x40.awritten before.b/.root.aleft behind a blocked.rootat % 4check dropped…2econtrol)oversizedcount wrong.tmpfoundLocal data, not in the repository. On a Scarlett package's shader archive (the adjacency and
s_endpgmrules left these counts unchanged: 0 incomplete programs) the tool joined 1,079 zlib streams into 800 runs, carved 4,982 programs (2,730 pixel, 2,115 vertex, 137 other) and wrote 3,774 requests for 1,258 pairs and three recorded roots in 22 s. A cold startup run recorded eleven pipelines:<key>.txt, both.hlslfiles and both.csofiles were byte-identical to the run's.Translated on its own, the population gives 104 of 1,171 pairs that translate under at least one root (measured before stream joining). The refusals are tracked in #1723.
Evidence and follow-up
run.ps1 -Translatestill covers anything this misses.Checklist
AI-assisted (Claude Code).
🤖 Generated with Claude Code