Skip to content

GPU: cache the error of a container that does not translate - #1094

Closed
adamdotpy wants to merge 5 commits into
Marvisak:mainfrom
adamdotpy:gpu/cache-translation-failures
Closed

adamdotpy wants to merge 5 commits into
Marvisak:mainfrom
adamdotpy:gpu/cache-translation-failures

Conversation

@adamdotpy

Copy link
Copy Markdown

Carries @lines9686-rgb's commit from #1089, verified here; AI-assisted.

The change is lines9686-rgb@1c2b2d6, pushed unchanged with its authorship, because external PR creation is still restricted and the issue proposes the branch for cherry-pick.

What it does

shader_cache stored only successful translations, so every run re-translated every container that does not translate — measured in #1089 at ~400 programs per run, the large compute ones 2-3 s each, with the creation workers spending about 1.5 minutes after world entry failing the same programs again while their draws were dropped.

  • Translate::convert now stores a refusal as a VSF1 entry (the error's text) under the same key instead of returning Err, so it is written like a translation.
  • cached reads either magic: VSH1 gives the shader, VSF1 gives the error back. A damaged VSF1 entry is a miss, like a damaged shader entry, and is tried again.
  • A cached refusal counts as a hit, since no translation ran.

The key is build(): the BLAKE3 of our executable plus the loop limit. A rebuilt translator, or another VICTORY_SHADER_LOOP_LIMIT, is a different key and tries again, so a fix is not hidden by a cached refusal. Translation itself is pure over the container, so the same key cannot succeed after failing.

Verification here

Windows 11, Rust 1.98.1, on ffe310e with this commit applied:

  • cargo test -p victory-gpu: 52 passed, 1 ignored (50 before; the two new tests are failures_come_back_from_the_cache_too and failure_entries_round_trip_and_are_no_shader).
  • cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings: clean.
  • cargo build --release -p victory: builds.
  • rustfmt --edition 2024 --check crates/victory-gpu/src/shader_cache.rs: clean (the other fmt differences in the tree are on main already).

I read the change for the three things that could go wrong and all three hold: a refusal cannot become a stale success (the key holds the build), a truncated entry is retried rather than trusted (the decode returns None and the file is removed), and nothing outside host_shader reads the entry (only pipeline.rs does, and it drops the draws as before).

Two things worth knowing

  • The per-frame log at queue.rs:270 says "shaders from the cache and translated so far"; a cached refusal is counted in the first number now, so a run with many refusals shows a higher hit rate than it has translations. Not wrong, but the wording could say "from the cache (refusals included)".
  • This removes the need for the dedupe I suggested in GPU diagnostics: find what the world hang waits for, and look under a black frame #638's VICTORY_DUMP_FAILED review point: a refused container is translated once per build now, so the dump is written once per hash rather than per retry.

Fixes #1089.

Translation depends only on the container and on what the cache key
already hashes (our executable and the loop limit), so a container
that fails fails the same way again. Failures were not stored, so every
run translated the same few hundred refused programs again - seconds
each for the large compute ones - while the title waited at world
entry. Store the error text under the same key (a VSF1 entry); a new
build tries again.

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

Copy link
Copy Markdown

On 1c2b2d6, I populated a disposable cache with a synthetic translation refusal, then shortened the saved entry. VSF1 by itself returned an empty error. VSF1 followed by a valid UTF-8 prefix returned just that prefix. Both counted as cache hits and skipped translation. The short-marker and invalid-UTF-8 controls both retried and recovered the full error.

I simulated truncation after the write completed. If truncated entries are meant to be cache misses, storing and checking the payload length would cover both cases.

AI-assisted with Codex.

A VSF1 entry was the marker and then the text, so a file cut short after
the marker decoded as an empty reason, and one cut inside the text decoded
as the prefix that survived. Both are valid UTF-8 often enough that they
were served as cache hits: the refusal held, but its reason was empty or
truncated, in the one place that reason is read.

The entry now carries the text's length, and a decode whose text is not
exactly that long is damage: no entry, so the container is translated
again and the reason comes back whole. Reported by @DrDarkDK, who cut
entries short after writing them and saw both cases counted as hits.

The test cuts a real entry at the marker, four bytes past it, and one byte
before its end, and also checks the marker followed by non-UTF-8 and a
shader entry's magic with a reason after it.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@adamdotpy

Copy link
Copy Markdown
Author

Good catch, and you are right about both cases: the entry was marker-then-text, so a file cut after the marker decoded as an empty reason and one cut inside the text decoded as the prefix that survived. Both are usually valid UTF-8, so both were served as hits — the refusal held, but its reason was empty or truncated, in the one place that reason is read.

Fixed in 0061ed8 as you suggested: the entry carries the text's length (VSF1, 4 bytes little endian, then the text), and a decode whose text is not exactly that long is damage, so the container is translated again and the reason comes back whole. Your short-marker and prefix cases are now the test a_damaged_failure_entry_is_no_entry, which cuts a real entry at the marker, four bytes past it and one byte before its end, and also checks the marker followed by non-UTF-8 and a shader entry's magic with a reason after it.

Verified on Windows 11, Rust 1.98.1: cargo test -p victory-gpu 53 passed / 1 ignored (51 before; the cache tests are 5), cargo clippy -p victory-gpu --all-targets --all-features -D warnings clean, cargo build --release -p victory builds, and the file is rustfmt-clean. Before the fix the new test failed exactly as your repro says (cut to 4 bytes: Some("") vs None).

@DoubleD17 DoubleD17 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.

I compared the shader-cache error path on 0061ed8 with ffe310e by source inspection. On a cache miss, the base returns a translation refusal before it attempts a write. The PR encodes the refusal as a successful VSF1 payload. If persistence then fails, cached returns the cache I/O error to the pipeline caller before it decodes the original reason.

In an independent Windows filesystem model, a directory at the destination made the rename fail on two successive requests. Both requests returned the rename error and reran the stub translator. With a writable destination, both returned the original refusal and the stub ran once. The model exercised the filesystem scenario. The caller behavior follows from inspection of the Rust control flow, and the PR itself was not executed. Could a regression test check that the caller still receives the original translation reason when only persistence fails?

AI-assisted.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Tested the VSF1 disk failure entry format (with the 4-byte length prefix in 0061ed8) on Windows across RTX 5090 and WARP (VICTORY_D3D12_DEBUG=1). A few behavioral edge cases worth tightening here:

  1. HITS / MISSES counter distortion: Incrementing MISSES on initial failure and HITS on VSF1 hits causes queue.rs to count refused shaders under "translated so far". Gating both counters on result.is_ok() keeps the frame translation stats accurate.
  2. Retry loop & no-disk-cache fallback: converter.ran stays true across the 2-iteration corruption retry loop unless reset at the top of each iteration, and host_shader() still re-runs translate() when disk caching is disabled or unavailable unless backed by a small build-keyed in-memory negative map.
  3. Sibling Pending pipeline stalls in pipeline/make.rs: Even once a Shader has failed in memory (translated.get() == Some(None)), hundreds of sibling Pending PSOs sharing that Arc<Shader> still get pushed onto the worker channel in make::ready and stall the render thread up to the 500 ms frame wait budget behind slow driver PSOs. Short-circuiting Pending::ready when any active stage shader has already failed avoids queueing and waiting altogether.

Reference commit with synthetic unit tests covering all three points (feel free to cherry-pick or fold in any pieces): fuzeofficlal@46b2878

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 1c2b2d6: cargo test -p victory-gpu passes, 52 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Measured 0061ed8, it does what it says. I replayed offline the shaders one warm launch asked for (1,918 of them, 8 threads, Ryzen 9 5900X), on main plus a set of open shader PRs plus this one:

wall time refused thread time on refusals
no refusal kept 10.3 to 11.1 s 339 80 to 87 s
refusals kept (this PR) 0.05 to 0.06 s 339 about 0.08 s

In game, one warm launch on plain main spent 277 thread-seconds on 401 refusals. Load time and frame time were not measured.

I had written the same thing separately and dropped it in favour of this PR. Two cases my tests had that fail here, for a follow-up or this one:

  1. Cache folder not writable: the translator's reason is replaced by an OS error, so the log no longer says why the shader is refused. main keeps the reason. (My version had the same defect.) GPU: a shader cache that cannot keep an entry still gives its translation #358 and Shader cache: a cache that cannot write still translates #389 edit the same function.
  2. Damage that keeps the length (some bytes changed): the entry is served with a wrong reason. A checksum of the text in the header, next to the length, makes it a miss.

A test that a different loop limit or exe makes a different key would also pin the retry.

AI-assisted (Claude Code).

…not be written keeps the reason

Two defects reported on Marvisak#1094 by TG0000, both reproduced here first:

- Damage that keeps the entry's length (a torn write, some changed bytes)
  was served as a refusal with a wrong reason: the length check passed.
  The entry now carries the first four bytes of the text's BLAKE3 next to
  the length, and `decode_failure` rejects a mismatch. The marker moves to
  VSF2, so entries of the old layout are misses and are translated again
  rather than read with their text's first bytes as a checksum.
  `a_damaged_failure_entry_is_no_entry` now flips a byte in the text with
  the length intact; without the checksum it returns
  `Some("aascalar memory read as data")`.

- A cache folder that cannot be written made `cached` return the OS error
  instead of the reason, so the log no longer said why the shader was
  refused. A failed write now logs a warning and falls through to the
  translation itself. `a_cache_that_cannot_be_written_still_says_why`
  blocks the entry's path with a directory; without the fix it returns
  `Err("Access is denied. (os error 5)")`.

`cargo test -p victory-gpu` 54 passed / 1 ignored, clippy
`--all-targets --all-features -D warnings` clean, release build fine,
rustfmt clean on the changed file.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@adamdotpy

Copy link
Copy Markdown
Author

Both of your cases are fixed in 097a790 on this branch, and each was reproduced here before the fix, with the failing assertion quoted in the commit message.

1. Damage that keeps the length. decode_failure only compared lengths, so a torn write with the right length was served as a refusal with a wrong reason. The entry now carries the first four bytes of the text's BLAKE3 between the length and the text, and a mismatch is a miss. The marker moves VSF1 → VSF2, so entries written by the previous revision are clean misses and get translated again rather than parsed with their text's first bytes as a checksum. Red test: a_damaged_failure_entry_is_no_entry now flips one byte in the text with the length intact — without the checksum it returns Some("aascalar memory read as data"), with it None.

2. Unwritable cache folder. cached propagated get_or_convert's error with ?, so the answer became the OS error and the log lost the reason. A failed write now logs shader cache entry not written; translating without it and falls through to the translation itself, which returns the real reason. Red test: a_cache_that_cannot_be_written_still_says_why blocks the entry's own path with a directory so the rename onto it fails — without the fix the result is Err("Access is denied. (os error 5)"), with it Err("a scalar memory read as data").

Verified on Windows 11 / Rust 1.98.1: cargo test -p victory-gpu 54 passed / 1 ignored (6 in shader_cache, both new ones included), cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings clean, cargo build --release -p victory builds, rustfmt --edition 2024 --check clean on the changed file. Pushed as a fast-forward to gpu/cache-translation-failures, no force.

Your third suggestion — a test that a different loop limit or exe makes a different key — is already covered by the existing translations_come_back_from_the_cache ("another build translates again") plus build() hashing the exe and VICTORY_SHADER_LOOP_LIMIT together, so I did not add a duplicate.

Thanks for the measurement table, by the way: 0.05 s against 10.3 s for 339 refusals is the number that justifies this whole branch.

adamdotpy and others added 2 commits November 24, 2026 18:08
The 'shaders from the cache and translated so far' line counted a
refusal served from a VSF2 entry as a hit and a fresh refusal as a
translation, so with hundreds of refused shaders the line overstated
both. Only a successful entry moves its counter now.

Red first: refusals_are_not_counted_as_translated saw (hits, misses)
move by (1, 1) for one fresh plus one cached refusal; now (0, 0),
while a translating shader still moves misses then hits.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
ready() queued every not-yet-made Pending onto the worker channel and
waited for it within the frame budget, even when one of its stage
shaders had already failed to translate and HostPipeline::new could
only refuse it. Sibling pipelines sharing that shader then sat in the
queue behind slow driver compiles, each costing the render thread wait
time. ready() now answers at once and records the refusal in made, so
diagnostics still name the shader.

Red first: a_refused_shader_skips_the_worker_queue failed on
'not sent to a worker' (queued was true); now ready returns None with
queued false and made Some(None).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@adamdotpy

Copy link
Copy Markdown
Author

Point by point on the three edge cases, checked against this branch at d03d93c (Windows 11 / Rust 1.98.1, isolated target dir):

1. Counter distortion — real, fixed in e090242. Reproduced first: the new test refusals_are_not_counted_as_translated saw (hits, misses) move by (1, 1) for one fresh refusal plus one served from a VSF2 entry — a cached refusal incremented HITS, a fresh one MISSES, both under the "shaders from the cache and translated so far" frame line. Now only a successful entry moves its counter (the refusal is still logged with ok=false); the test passes with (0, 0) for the refusals and (0, 1) then (1, 0) for a shader that translates and is then cached. The cache tests now share a mutex so the process-wide counters can't race between them.

2a. ran across the retry loop — not a defect, no reset needed. ran is only read after an entry decodes. In iteration 2 an entry can only be served if the file exists, and after a damaged entry the code removes it first — so either convert re-ran in iteration 2 (ran true, correctly a miss) or the removal failed and the file is iteration 1's own convert output (ran still true, also correctly a miss: it was translated in this call). A pre-existing file would have been served in iteration 1, so a true hit never sees ran == true.

2b. No disk cache — main's behavior, not this PR's. The None => translate(container) arm of host_shader is byte-identical on main (git show upstream/main:...shader_cache.rs), and the cache is on by default (run.rs always calls use_shader_cache with the cache subdir; use_shader_cache only leaves CACHE unset when the folder can't be opened). So re-translating refusals with the cache off is exactly what main does. A build-keyed in-memory negative map for that path is a reasonable follow-up, but it's a new feature with a growth/keying decision, not a fix to this branch — happy to take it as its own PR if you want it.

3. Sibling Pending stalls — real, fixed in d03d93c. Confirmed in code: ready() queued any not-yet-made Pending and waited within the 500 ms frame budget even when HostPipeline::new could only refuse it (s.translated()? per stage). ready() now answers None at once when any stage shader has translated == Some(None), and still records the refusal in made so the diagnostics path (missing) keeps naming the shader. Red first: the new a_refused_shader_skips_the_worker_queue failed on "not sent to a worker"; now ready returns None with queued == false and made == Some(None).

Verification on d03d93c: cargo test -p victory-gpu 56 passed / 1 ignored (both new tests included), cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings clean, cargo build --release -p victory builds, rustfmt --edition 2024 --check clean on shader_cache.rs and on the lines changed in make.rs (its three remaining fmt hunks are pre-existing on main). Pushed as a fast-forward to gpu/cache-translation-failures, no force.

simnJS pushed a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
Translation depends only on the container and on what the cache key
already hashes (our executable and the loop limit), so a container
that fails fails the same way again. Failures were not stored, so every
run translated the same few hundred refused programs again - seconds
each for the large compute ones - while the title waited at world
entry. Store the error text under the same key (a VSF1 entry); a new
build tries again.

Ported from Marvisak/VIctory#1094 (head 097a790) by adamdotpy

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
simnJS pushed a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
A VSF1 entry was the marker and then the text, so a file cut short after
the marker decoded as an empty reason, and one cut inside the text decoded
as the prefix that survived. Both are valid UTF-8 often enough that they
were served as cache hits: the refusal held, but its reason was empty or
truncated, in the one place that reason is read.

The entry now carries the text's length, and a decode whose text is not
exactly that long is damage: no entry, so the container is translated
again and the reason comes back whole. Reported by DrDarkDK, who cut
entries short after writing them and saw both cases counted as hits.

The test cuts a real entry at the marker, four bytes past it, and one byte
before its end, and also checks the marker followed by non-UTF-8 and a
shader entry's magic with a reason after it.

Ported from Marvisak/VIctory#1094 (head 097a790) by adamdotpy

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
simnJS pushed a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
Two defects reported on Marvisak/VIctory#1094 by TG0000, both reproduced here first:

- Damage that keeps the entry's length (a torn write, some changed bytes)
  was served as a refusal with a wrong reason: the length check passed.
  The entry now carries the first four bytes of the text's BLAKE3 next to
  the length, and `decode_failure` rejects a mismatch. The marker moves to
  VSF2, so entries of the old layout are misses and are translated again
  rather than read with their text's first bytes as a checksum.
  `a_damaged_failure_entry_is_no_entry` now flips a byte in the text with
  the length intact; without the checksum it returns
  `Some("aascalar memory read as data")`.

- A cache folder that cannot be written made `cached` return the OS error
  instead of the reason, so the log no longer said why the shader was
  refused. A failed write now logs a warning and falls through to the
  translation itself. `a_cache_that_cannot_be_written_still_says_why`
  blocks the entry's path with a directory; without the fix it returns
  `Err("Access is denied. (os error 5)")`.

`cargo test -p victory-gpu` 54 passed / 1 ignored, clippy
`--all-targets --all-features -D warnings` clean, release build fine,
rustfmt clean on the changed file.

Ported from Marvisak/VIctory#1094 (head 097a790) by adamdotpy

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
From the review of the port (the three commits before this):

- The module notes say what keys a refusal: the build and the target,
  as for a translation (the upstream text names the build only; XTP keys
  its entries by target since the wave64 wiring). And why nothing
  transient is kept: the translator's errors (ShaderError) are all
  refusals of the container itself; out of memory and a removed device
  happen in the driver's compile, after this cache. `kept` says that its
  `translate` must fail only that way.
- An entry that cannot be written is answered from the translation
  convert() already made, not by translating a second time (upstream's
  test asked for at least one run). That holds for a translation that
  succeeds too, which before the port came back as the OS error, the
  shader refused. The warning is told once, then at debug level: a full
  disk would log it for every shader of a world.
- Tests: refusals_are_kept_by_build_and_target (translated once per
  build and target, then from the cache; a rebuilt translator or another
  target translates again and may take it), the translator's own refusal
  of a non-DXBC container kept with its text, and an unwritable cache
  translating once, for a refusal and for a translation.

Ported from Marvisak/VIctory#1094 (head 097a790) by adamdotpy, reviewed
simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
…ice at most

Review points on the #1094 port (pipe-cache):

- The frame line "shaders from the cache and translated so far" counted
  a refusal served from its entry as a hit and a fresh one as a
  translation. Only a shader moves a counter now. Ported from
  Marvisak/VIctory#1094 (commit e090242) by adamdotpy, with its test;
  every test that goes through `cached` holds one lock, so the test that
  reads the process-wide counters sees only its own.
- An entry that did not read back after its second write made a third
  translation; the one made for that entry is the answer now. (Not
  tested: the cache writes the entry itself, so no test can tear it
  between the write and the read.)
- The damaged-entry test also cuts right after the header (no text).
@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Already in main: a container that does not translate is cached with its reason (shader_cache.rs REFUSED entries, c47b529), so it is not translated again every run.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

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.

Shader cache: translation failures are not cached, so every run re-translates the same refused programs (fix on a fork)

8 participants