Skip to content

d3d12: write the session pipeline cache when the run ends with the device alive (RT-SESSION-CACHE-AT-EXIT) - #1950

Merged
aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
kurrutako:rt/session-cache-at-exit
Nov 25, 2026
Merged

aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
kurrutako:rt/session-cache-at-exit

Conversation

@kurrutako

Copy link
Copy Markdown
Contributor

RT-SESSION-CACHE-AT-EXIT, claimed in #1608.

What

pipeline_cache.session.bin was written only in ~Device. A title never releases its device, so a real boot kept none of what it compiled, and every boot built the world start again.

  • save_pipeline_caches_at_exit(wait_ms) (shims/d3d12_compile_exit.h) writes the session cache of every live device. The live devices are a list that the Device constructor and destructor maintain. It runs once per process, after stop_pipeline_compiles.
  • gta6rt calls it at the run's exit, at a console close (Ctrl+C, Ctrl+Break, close), and from an unhandled-exception filter: a fault in the title or one of its DLLs, or the int3 after a device loss. The filter then passes the exception on, so the exit code is the exception's, as before.
  • The retail title installs its own filter. It imports SetUnhandledExceptionFilter from api-ms-win-core-errorhandling-l1-1-0, which replaced the runtime's filter (measured: a title fault at 354 s wrote nothing). The runtime now serves that call: its own filter stays first and hands the exception to the title's, as Windows would.
  • Builds still running. A large build takes minutes, and at a crash the faulting thread may be inside one. A build still running gets wait_ms (1 s); then the cache is written with the pipelines finished so far. This is valid because the cache is not created EXTERNALLY_SYNCHRONIZED. Unlike at teardown, the previous file is not kept: the process is ending, so keeping it would throw away the whole run (measured: a 1,308 s boot ended with 2 builds running).
  • The write itself is bounded. It runs on its own thread and gets 3 s, so a driver that blocks the read cannot hold the ending process. CacheStagingFile publishes by rename, so a write cut off leaves the previous file whole.
  • Threads that fault together (measured: eight within a second) wait for the first one's write (std::call_once), so none reaches the title's filter, which may end the process, in the middle of it.

~Device and the checkpoints are unchanged. The size limit is #1886's subject; see below.

Measured

Retail boots on Windows 11, RTX 5070 12 GB, NVIDIA 621.01. Build: main + #1851, #1827, #1849, #1862 (shaders retranslated with it: 29/29 FSPX) + #1886, with LEONIDA_HOST_IMAGE_COPY=0 LEONIDA_ASYNC_PIPELINES=1 LEONIDA_DEVICE_TYPE=6 LEONIDA_ARENA_MB=2048 LEONIDA_TITLE_WATCHDOG=0 LEONIDA_DEVICE_FAULT=1, a person at the keyboard pad. Counted: the runtime's took N s to build lines.

boot how it ended session cache
before this change (dozen boots) title faults, device losses never written
A title fault at 652 s, 1 build running wrote 436563293 bytes
B (loads A's file: loaded 436563293 bytes) int3 at 687 s, 2 builds running wrote 510225527 bytes

Tests

d3d12_compile_teardown_test, exit case: with the device still alive after stop_pipeline_compiles, save_pipeline_caches_at_exit writes the session file, and a second call does nothing. It passes on the RTX 5070 (MSVC 2022, RelWithDebInfo); on llvmpipe the file stays absent, as in the existing cache case. The exception-filter path is checked by the boots above. Counts, offsets and program names only; no game data; python -I scripts/check_no_game_data.py passes.

AI-assisted.

🤖 Generated with Claude Code

kurrutako and others added 5 commits November 25, 2026 16:24
…vice alive (RT-SESSION-CACHE-AT-EXIT)

pipeline_cache.session.bin was written only in ~Device. A title never
releases its device, so a real boot kept none of what it compiled: not
after a normal exit, a console close or a crash, and every boot built the
world start again.

save_pipeline_caches_at_exit writes the session cache of every live device
(a registry the Device constructor and destructor keep), once per process,
after stop_pipeline_compiles. A build still running on another thread is
given wait_ms, then the previous file is kept, as at teardown. gta6rt calls
it at the run's exit, at a console close, and from an unhandled-exception
filter (a fault in the title or its DLLs, the int3 after a device loss),
which then passes the exception on, so the exit code is unchanged.

d3d12_compile_teardown_test's exit case checks that the file is written
with the device alive, and only once.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-SESSION-CACHE-AT-EXIT)

The retail title calls SetUnhandledExceptionFilter (from
api-ms-win-core-errorhandling-l1-1-0), which replaced the runtime's filter:
a title fault at 354 s ended the process without the cache write. The
title's call is now served by the runtime, which keeps its filter
installed and passes the exception on to the title's.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, on a bounded thread (RT-SESSION-CACHE-AT-EXIT)

A 1,308 s boot ended in a title fault with 2 builds still running, and the
exit save kept the old file, so the run's pipelines were lost again. At
exit there is no later moment to wait for: after wait_ms the cache is
written with the pipelines finished so far (it is not created
EXTERNALLY_SYNCHRONIZED, so reading it during a build is valid). The write
runs on its own thread and is given 3 s, so a driver that blocks the read
cannot hold the ending process; the file is published by a rename, so a
write cut off leaves the previous one whole.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ite (RT-SESSION-CACHE-AT-EXIT)

A boot ended with eight title threads faulting within a second. Each
entered the filter; the ones after the first returned at once and went on
to the title's filter, which can end the process during the write. The
write now runs under std::call_once, so they wait for it.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…CACHE-AT-EXIT)

AI-assisted.

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

Tyrannys commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Test report from a combined build on a second NVIDIA machine. Counts only.

Machine: Windows 11, RTX 4070 SUPER, NVIDIA 621.01, 64 GB, own 1.0.2.1 copy.

Build: main fe1553456 (already contains #1827 and #1893), plus #674, #1662, #1851, #1849, #1905, #1957, #1950, #1886 and #1894. All of them merged without conflicts.

Shaders: translated on 3de22a190 + #674 + #1662 with --wave64-emulate. 29,279 of 29,280 translated, and all 29,279 pass spirv-val. That is 2,683 library programs and 29 of 29 FSPX. main's translator changes since then are correctness fixes only.

Pipeline cache: warm and complete, 26,503 of 26,503 (9.6 GB).

  • Three library compute programs took 30 to 32 minutes each to build alone: the ScreenProbes ActivateRays, ActivateProbes and ActivateProbesTarget programs of indirect diffuse lighting.
  • Building all three at once crashed the NVIDIA compiler ("NVVM compilation failed: 3", after 22 minutes).

Recipe: scripts/windows-boot/boot.ps1 -Seconds 480, LEONIDA_KEYBOARD_PAD=1 LEONIDA_SKIP_INT3=1 LEONIDA_TITLE_WINDOWS_MB=1024 LEONIDA_MAP_READBACK=1, LEONIDA_PIPELINE_CACHE_DIR pointing at the offline cache. Host image copy: off (auto: NVIDIA) from #1851. One run per row.

run 1 run 2 (+ LEONIDA_TITLE_WATCHDOG=0)
end 344 s: the title's hang watchdog (null write at game_scarlett_master.exe+0x23cf683) full 484 s, stopped by the script
device loss / fault report none none
D3D12 calls 330,487 375,686
ExecuteCommandLists 5,490 6,017
Dispatch / DrawIndexedInstanced 2,435 / 2,726 2,779 / 6,026
PresentX 1,358 ok, 0 failed 1,465 ok, 0 failed
captures 23, all non-black 25, all non-black
indirect draws recorded / emitted / skipped 64 / 0 / 5,092 128 / 34 / 9,244 (#429 not included)

AI-assisted (Claude Code).

@kurrutako

Copy link
Copy Markdown
Contributor Author

Thanks for the test. Run 1 is the case this PR is for: the title's fatal exception went through the exit filter, the save ran while a build was still going, and it set out to write only the pipelines that had finished.

The write was then refused by the 4 GiB cap from #1886, not by this PR. With an offline seed loaded, the driver's cache data is the seed plus the session, so a 9.7 GB seed is over the cap before the session adds anything. That is the seeded case already raised on #1886, so I will leave the fix there and not change it here.

AI-assisted (Claude Code, Claude Opus 5.5).

@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Intel integration verification (AI-assisted: OpenAI Codex): Windows 11, MSVC RelWithDebInfo, Intel UHD Graphics / i5-1334U, driver 32.0.101.6314. Applied the four implementation commits through 98c457e plus the documentation commit, onto main 73cc1a2 and an existing private integration tree.

14/14 targeted tests passed: d3d12_compile_teardown (including the once-only live-device session-cache write), list_value_wait_budget, all nine xs_proxy_mock modes, d3d12_xmem_placed, d3d12_m2_textured, replay_copy_offset.

Also checked the actual gta6rt import/filter path with two synthetic, CRT-free guest PEs, one importing SetUnhandledExceptionFilter from KERNEL32.dll and one through api-ms-win-core-errorhandling-l1-1-0.dll. Each registers two filters, verifies the previous guest filter is returned, then raises a deliberate noncontinuable exception; the final guest filter exits with code 42. Both fixed-runtime runs log the cache-exit filter and then exit 42. The preceding runtime failed the filter-chain check (exit 43). These tiny fixtures create no graphics device; actual cache publication is covered by d3d12_compile_teardown, not by the PE fixtures.

This is a combined-tree, targeted result, not a full-suite or playability claim. No title data is included.

@aslan2142
aslan2142 merged commit e380a69 into ProjectLeonida:main Nov 25, 2026
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged, thanks! Reviewed at 2e61d408:

AI-assisted (Claude Code, Claude Opus 5.5).

@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Additional Intel diagnostic finding after the positive targeted tests above: with Khronos GPU-assisted validation enabled, a fatal shutdown while compute compilation was still active was followed by a driver access violation inside vkGetPipelineCacheData. Host symbols resolve the caller to save_session_pipeline_cache (d3d12_vulkan_device.cpp, the data query) from the bounded save_pipeline_caches_at_exit worker. The initial fatal exception occurred before this secondary driver failure; this does not explain or fix the initial shutdown.

Windows 11, Intel UHD Graphics / i5-1334U, driver 32.0.101.6314, Vulkan SDK 1.4.363.1, current-main integration including #1950. An earlier non-GPUAV run successfully saved the session cache at exit. No minimal standalone concurrency reproducer yet, so this is an observed GPUAV/driver interaction, not a confirmed general cache synchronization bug. A useful follow-up would be a synthetic active-compilation plus exit-cache-query stress test on Intel with GPUAV. No game logs or data included. AI-assisted with OpenAI Codex.

@kurrutako

Copy link
Copy Markdown
Contributor Author

Thanks, that's useful, and the caller you resolved is the expected one. Notes from the code at 2e61d408:

  • The read is allowed by Vulkan. The session cache is not created EXTERNALLY_SYNCHRONIZED, so vkGetPipelineCacheData while another thread's vkCreateComputePipelines uses the same cache is valid. The exit path counts on that: save_pipeline_caches_at_exit waits up to 1 s for active_compiles, then reads the pipelines finished so far. A driver that faults on that read in a valid call is a driver (or GPU-AV) problem, not missing locking on our side. That fits your clean non-GPU-AV exit save.
  • GPU-AV changes the call. It hooks pipeline creation and builds instrumented copies, so the driver sees different pipelines, and more of them, going into the same cache. The race window is therefore not the same as in a normal run.
  • Where did the first exception happen? This is the question that matters. If the first fatal exception was on a thread inside vkCreateComputePipelines (in the Intel driver or in the layer), that thread never leaves the build. active_compiles stays above zero, and the driver's cache can be left half-updated when we read it. A secondary access violation would then be likely, whatever the locking. A module and offset for the first exception, or just whether its stack shows a pipeline create, would settle it.
  • The secondary fault can't hang the exit. The write runs on its own detached thread. Its access violation goes back into the fatal filter, which waits on the call_once the first thread holds. The first thread stops waiting after 3 s (kExitCacheWriteMs) and hands the original exception to the title's filter. The previous pipeline_cache.session.bin stays whole, because it is only ever replaced by a rename.

If the first exception turns out to be inside a pipeline create, a narrow follow-up would be to skip the exit write when the faulting thread is in a build (a thread-local mark set by CompileActivity). The cost is losing that run's cache, not risking a second fault. I'd hold that until the first exception's location is known, and I agree a synthetic stress test (active compile plus exit-cache query, Intel, GPU-AV) is the way to tell a general race from this layer/driver combination.

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.

4 participants