d3d12: finish CompileActivity before checkpointing and retry deferred cache snapshots #1756
Closed
leonida/mandy
quietfold wants to merge 0 commits from
fix/startup-cache-checkpoint-race into main
pull from: fix/startup-cache-checkpoint-race
merge into: leonida:main
leonida:main
leonida:github-pr/3295
leonida:beta
leonida:docs/changelog-3483-3500
leonida:github-pr/3294
leonida:github-pr/2975
leonida:github-pr/2815
leonida:feat/gate-driver-start
leonida:feat/skip-pipelines-release
leonida:perf/lazy-compare
leonida:perf/mwaitx-wait
leonida:test/prio-ceiling-main
leonida:fix/ab-cpu-stdout
leonida:fix/gate-old-crashes
leonida:diag/ab-cpu
leonida:test/main-0312
leonida:github-pr/3291
leonida:diag/indirect-clamp-census
leonida:feat/barrier-enhanced
leonida:perf/texture-frame-ceiling
leonida:perf/dstorage-threads
leonida:fix/console-queue-dma-tables
leonida:fix/port-descriptors-record-parts
leonida:fix/frame-waits-fence
leonida:fix/console-sampler-words
leonida:fix/graphics-userdata-spill
leonida:fix/xg-reserved-layout-rotation
leonida:fix/barrier-console-flags
leonida:fix/switches-wait-test-child
leonida:test/list-value-waits
leonida:test/lot-gxdk-0412
leonida:fix/bvh-compacted-size
leonida:docs/i18n-quickstarts
leonida:feat/native-pipeline-slots
leonida:fix/clippy-leonida-gpu
leonida:fix/list-value-producers
leonida:fix/descriptor-page-straddle
leonida:fix/xmem-low-window
leonida:fix/ffx-jitter-never-zero
leonida:fix/run-stdout-closed
leonida:github-pr/2892
leonida:github-pr/2896
leonida:feat/warm-save-every
leonida:github-pr/2980
leonida:github-pr/2994
leonida:github-pr/3005
leonida:github-pr/3010
leonida:github-pr/3136
leonida:github-pr/3303
leonida:github-pr/3220
leonida:github-pr/3143
leonida:github-pr/3330
leonida:github-pr/3226
leonida:github-pr/3187
leonida:carry/3395
leonida:github-pr/2946
leonida:github-pr/2947
leonida:github-pr/2945
leonida:github-pr/2983
leonida:github-pr/3001
leonida:github-pr/3372
leonida:github-pr/3128
leonida:github-pr/3296
leonida:github-pr/2588
leonida:github-pr/3119
leonida:github-pr/3378
leonida:fix/crash-diagnostics
leonida:feat/dlss-mip-bias
leonida:fix/retired-out-of-order
leonida:fix/dxr-postbuild-barrier
leonida:fix/amd-relay-default
leonida:fix/removed-report-wait
leonida:feat/heap-twin
leonida:feat/vram-margin
leonida:github-pr/2472
leonida:github-pr/3123
leonida:github-pr/3088
leonida:assets/annonce-v1
leonida:github-pr/2794
leonida:github-pr/3300
leonida:github-pr/3304
leonida:github-pr/3308
leonida:github-pr/3367
leonida:github-pr/3369
leonida:fix/launcher-dlss-diagnostics
leonida:github-pr/3269
leonida:github-pr/2957
leonida:github-pr/3205
leonida:fix/crash-zero-guard
leonida:github-pr/3185
leonida:github-pr/3099
leonida:github-pr/3104
leonida:github-pr/3348
leonida:github-pr/3379
leonida:github-pr/3377
leonida:perf/darner-title-priority-ceiling
leonida:github-pr/3376
leonida:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
leonida:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
leonida:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
leonida:github-pr/3373
leonida:github-pr/3368
leonida:github-pr/3198
leonida:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
leonida:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
leonida:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
leonida:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
leonida:github-pr/3346
leonida:github-pr/3375
leonida:github-pr/2958
leonida:github-pr/3151
leonida:github-pr/3307
leonida:github-pr/3278
leonida:github-pr/3374
leonida:fix/runtime-test-conservative-fixtures
leonida:github-pr/3366
leonida:ci/shard-semantic-linux
leonida:github-pr/3354
leonida:docs/fg-limit-fps-switch
leonida:github-pr/3365
leonida:fix/read-file-hard-link
leonida:github-pr/3364
leonida:fix/book-slot-heap-never-read
leonida:github-pr/3361
leonida:github-pr/3263
leonida:github-pr/3360
leonida:github-pr/3353
leonida:github-pr/3331
leonida:fix/alias-later-queued-submissions
leonida:github-pr/3352
leonida:github-pr/3228
leonida:github-pr/3349
leonida:claude/gallant-sagan-aamatr
leonida:github-pr/3200
leonida:github-pr/3132
leonida:github-pr/3323
leonida:github-pr/3332
leonida:github-pr/3340
leonida:github-pr/3311
leonida:github-pr/3334
leonida:github-pr/3297
leonida:github-pr/3256
leonida:github-pr/2567
leonida:github-pr/2851
leonida:d3d12/agility-sdk-621
leonida:github-pr/3164
leonida:github-pr/3114
leonida:github-pr/3050
leonida:github-pr/3113
leonida:github-pr/3326
leonida:github-pr/3144
leonida:github-pr/2642
leonida:github-pr/3072
leonida:github-pr/2789
leonida:github-pr/2643
leonida:github-pr/3009
leonida:github-pr/3035
leonida:github-pr/3031
leonida:github-pr/2686
leonida:github-pr/2834
leonida:github-pr/3321
leonida:github-pr/3325
leonida:github-pr/3290
leonida:github-pr/3324
leonida:github-pr/3130
leonida:claude/fervent-curie-m19lnr
leonida:github-pr/3309
leonida:github-pr/3305
leonida:github-pr/3306
leonida:github-pr/3302
leonida:github-pr/3301
leonida:github-pr/3345
leonida:github-pr/3211
leonida:github-pr/3027
leonida:github-pr/3286
leonida:github-pr/3235
leonida:github-pr/3265
leonida:github-pr/3275
leonida:codex/skip-no-hit-ray-passes
leonida:github-pr/2823
leonida:github-pr/2911
leonida:github-pr/3262
leonida:github-pr/3125
leonida:github-pr/3206
leonida:github-pr/3250
leonida:github-pr/3238
leonida:github-pr/3188
leonida:github-pr/2952
leonida:github-pr/3217
leonida:github-pr/3168
leonida:t3/preserve-fsr-sdk-restart-path
leonida:t3/propagate-host-failure-hresult
leonida:github-pr/3209
leonida:t3/launcher-custom-parameters
leonida:github-pr/3207
leonida:github-pr/3183
leonida:github-pr/3160
leonida:github-pr/3201
leonida:ci/less-to-run
leonida:github-pr/2930
leonida:github-pr/2797
leonida:github-pr/2999
leonida:perf/native-compute-shader-forms
leonida:github-pr/3191
leonida:github-pr/3011
leonida:github-pr/3019
leonida:github-pr/2996
leonida:gh-readonly-queue/main/pr-2789-45dc41f95c1cd0dcc29c2c50582e0e9af8a77259
leonida:claude/blissful-franklin-ukp7ag
leonida:github-pr/3141
leonida:github-pr/3156
leonida:github-pr/3150
leonida:github-pr/3153
leonida:github-pr/3163
leonida:github-pr/3142
leonida:github-pr/3139
leonida:github-pr/2841
leonida:github-pr/3069
leonida:github-pr/3098
leonida:github-pr/3107
leonida:github-pr/3071
leonida:github-pr/2774
leonida:github-pr/2889
leonida:github-pr/3075
leonida:github-pr/2661
leonida:github-pr/3064
leonida:github-pr/3054
leonida:github-pr/3049
leonida:github-pr/2855
leonida:github-pr/2943
leonida:github-pr/3016
leonida:github-pr/2887
leonida:github-pr/3006
leonida:github-pr/3003
leonida:github-pr/2993
leonida:notes/card-time-and-plant-flicker
leonida:github-pr/2989
leonida:feat/vulkan-rs
leonida:github-pr/2985
leonida:github-pr/2951
leonida:github-pr/2982
leonida:github-pr/752
leonida:github-pr/792
leonida:github-pr/2877
leonida:github-pr/2654
leonida:github-pr/2893
leonida:github-pr/2948
leonida:github-pr/2944
leonida:github-pr/2919
leonida:github-pr/2915
leonida:archive/ci/less-to-run/20261202T1121Z
leonida:github-pr/2800
leonida:github-pr/2822
leonida:github-pr/2833
leonida:github-pr/2840
leonida:github-pr/2831
leonida:github-pr/2832
leonida:github-pr/2801
leonida:github-pr/2890
leonida:github-pr/2891
leonida:github-pr/2664
leonida:github-pr/2807
leonida:rt-call-trace
leonida:github-pr/2755
leonida:perf/ahead-split
leonida:github-pr/2587
leonida:github-pr/2698
leonida:github-pr/2609
leonida:github-pr/3219
leonida:docs/readme-runtime-proposal
leonida:github-pr/2017
leonida:github-pr/1741
leonida:github-pr/1667
leonida:codex/coord-claim-check-20261124-7e48
No reviewers
Assignees
Clear assignees
No assignees
Labels
Clear labels
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
enhancement
New feature or request
missing claim
Needs a claim in #14
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
Projects
Clear projects
No items
No project
Milestone
Clear milestone
No items
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
leonida/mandy!1756
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/startup-cache-checkpoint-race"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request closed
This pull request cannot be reopened because the branch was deleted.
Item ID:
RT-STARTUP-CACHE-CHECKPOINT-RACEClaim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7160859893
Addresses finding: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157762911 (
LEONIDA_STARTUP_CACHE_CHECKPOINTS=1wrote nothing during a 200 s run reaching world start)Root cause
Two bugs in
runtime/shims/d3d12_vulkan_device.cppprevented startup cache checkpoints from saving during active world-start compilation bursts:maybe_checkpoint(cache)ran whileCompileActivitystill helddevice_.active_compiles >= 1: bothPipelineState::build_compute()(lines 8038, 8091) andPipelineState::build()(lines 8202, 8386) keptCompileActivity activity(device_.active_compiles)alive acrossmaybe_checkpoint(cache). When the build ran on a submitting orPipelineWorkersthread, thePipelineCompilerworker woke up before the caller returned, sawactive_compiles != 0, and skippedsave_session_pipeline_cache().active_compiles != 0orhas_compile_waiter()(either beforesave_session_pipeline_cache()at line 8110 or after the size query at line 1348),maybe_checkpoint()unconditionally pushednext_checkpointout by another 120,000 ms (2 minutes). A single deferral att = 120 spushed the next attempt tot = 240 s— past the world-start crash at~195–200 s, leaving the cache directory empty.Changes
runtime/src/pipeline_cache_store.h:CheckpointOutcome(saved,deferred,skipped) and portable atomicCheckpointSchedule(try_claim,complete,abort_claim). Deferred checkpoints and queue-submission aborts stay immediately eligible on the next compile completion instead of burningdefault_checkpoint_interval_ms(120 s);savedandskipped(empty/over-cap/I/O error) advance the deadline by the full interval with saturation below thein_flightsentinel.runtime/shims/d3d12_pipeline_checkpoint.h:CompileActivity::finish()(withacq_relordering) so callers can release their active-compile count before queueing a checkpoint while keeping RAII cleanup on early returns.runtime/shims/d3d12_vulkan_device.cpp:next_checkpointatomic withCheckpointSchedule checkpoint_schedule.build_compute(), publishpipeline_.pipeline, unlockvariants_mutex_, and callactivity.finish()beforemaybe_checkpoint(cache); inbuild(), callactivity.finish()beforemaybe_checkpoint(cache).save_session_pipeline_cache()to returnCheckpointOutcome(returningdeferredifactive_compilesorhas_compile_waiter()became active during the size query) and wiredmaybe_checkpoint()tocheckpoint_schedule.complete(..., outcome).runtime/test/pipeline_cache_store_test.cpp:test_checkpoint_schedule()covering due-time gating, in-flight coalescing, deferred immediate retry,abort_claim, saved/skipped 120 s cooldown, and near-UINT64_MAXsaturation.Verification
clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined runtime/test/pipeline_cache_store_test.cpp: PASS.clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/pipeline_cache_store_test.cpp: PASS.python3 -I scripts/check_no_game_data.py runtime/src/pipeline_cache_store.h runtime/shims/d3d12_pipeline_checkpoint.h runtime/shims/d3d12_vulkan_device.cpp runtime/test/pipeline_cache_store_test.cpp: PASS.python3 -I tests/test_text_write_line_endings.py: 3/3 OK.runtime/shims/d3d12_pipeline_checkpoint.handruntime/shims/d3d12_vulkan_device.cppare Windows/Vulkan shims and rely on Windows CI / a retail boot run withLEONIDA_STARTUP_CACHE_CHECKPOINTS=1.Thanks for taking this from the stall finding. I had prepared a change for the same code, so here is what I measured (RTX 5080, NVIDIA 621.24, Windows 11,
main237deb64c plus a few open pull requests; details in this finding). I won't open a competing pull request. I have read #1756 at f32c1abcc but not run it.The release of
CompileActivitybefore the checkpoint and the retry without the 120 s penalty both look right to me. Three things your branch keeps were each enough to stop a save here:SIZE_MAX. The checkpoint waits behind every speculative warm-up build (about 900 programs). In a 140 s run it was never tried.finish()removes the caller's own build from that count, but builds on other threads and queued speculative jobs still defer it.save_session_pipeline_cacherepeats the check after the size query.What worked here, as a local experiment: first attempt at 30 s, priority 1 (ahead of the warm-up, behind builds that something waits for), yielding only when a priority 0 job is queued, again 30 s after a save and 5 s after a deferral. The save itself is short: 22 MiB took 7.3 ms to fill and 2.3 ms to publish, 77 MiB took 27.7 ms and 8.2 ms, so waiting for an idle moment gains little. With it, builds found in the cache went from 35 of 892 (no cache yet) to 617, 714 and 781 of about 1,000 over the next three runs, all of which crashed. Upstream's schedule saved nothing in any of my runs.
Would you like to take points 1 to 3 into this pull request? If you'd rather keep it to the race, I can send them as a follow-up with
Depends on: #1756, and I can also run your branch here for a retail result.AI-assisted (Claude Code).
Thank you @DadaCap for those RTX 5080 retail measurements (#1608#issuecomment-7161936928) — folded all three points into this PR at
90506453e(rebased onmain0d80bc5df):CheckpointSchedule:default_checkpoint_interval_ms = 30000,default_checkpoint_retry_ms = 5000).checkpoint_priority = 1) inmaybe_checkpoint(), behind priority-0 awaited first-use draws/dispatches and ahead of speculative compute warm-up builds (keyed by SPIR-V byte size>= 20).PipelineCompiler::has_compile_waiter()tojobs_.begin()->first == 0and removed theactive_compilesblock from the checkpoint path (keepingCompileActivity::finish()beforemaybe_checkpoint()so teardown accounting stays exact;vkGetPipelineCacheDatais thread-safe against concurrentvkCreate*Pipelines).docs/d3d12_vulkan.mdandruntime/test/pipeline_cache_store_test.cpp(re-verified locally under ASan/UBSan and-O2).If you have a chance to test
90506453eon a retail boot, that would be awesome!Retail result for
90506453eon the RTX 5080 (NVIDIA 621.24, Windows 11), merged withmain0d80bc5df, #1662 and our 674, 1694, 1705, withLEONIDA_STARTUP_CACHE_CHECKPOINTS=1(and, for an unrelated test, host image copy off): it saves. 16 snapshots in 540 s, the file went from 123 MiB at the start to 183 MiB part way through, while speculative builds were still queued. Builds in that run: 1,083 found in the cache, 243 not. The run was still in the story load when it was stopped, so it does not show a save before a crash, which is the case this is for. Thank you for folding in the three points. AI-assisted (Claude Code).Tested
90506453e(both commits cherry-picked ontomain98c2f7777) on native Windows 11 26300, RTX 2060 SUPER 8 GB (Turing, no Resizable BAR), NVIDIA 617.14, 16 GB RAM. Retail boot withLEONIDA_STARTUP_CACHE_CHECKPOINTS=1andLEONIDA_DEVICE_FAULT=1(identical in both runs). Each run was 600 s: past the landing prompt into the story load (session state0xdat 170 s), and stopped by my time guard, not a crash.mainwithout this PR, the folder stayed empty in all four of my earlier runs.library/lib_cs_*programs:AI-assisted: yes
@3L1A5-1 a data point against a general NVIDIA miss: on an RTX 5080 with NVIDIA 621.24 the library compute programs do hit a session file from the previous run. With
LEONIDA_TRACE_PIPELINE_COMPILE=1, a run that loaded a 268 MB file had 1,416 library builds withhit=1(147 of them over 600 KB of SPIR-V) and 138 withhit=0. All 75 that were slow (2 s or more) in the run before and slow again were ones that run could not save, because it was over the 256 MiB limit (7163972744); your 88 MB file is far below that, so that is not your cause. If you can, one run with the trace on would show whether the driver reportshit=0or hits that are slow. Two things that cost me hits: entries written withLEONIDA_DEVICE_FAULT=1miss without it and the reverse, and a retranslated shader. AI-assisted (Claude Code).Small follow-up for this PR, if useful: a queue-order test for the priority change. Patch below, based on 90506453e.
pipeline_compile_order_testdrivesPipelineCompilerwith no device: one blocking priority-0 job, 900 warm-ups (priority 20..919), then the checkpoint atcheckpoint_priority. It checks that the checkpoint runs second, and thathas_compile_waiter()only reports priority-0 jobs.SIZE_MAX, the same test fails: the checkpoint runs 902nd. That one constant is the only line I changed for main, sincecheckpoint_prioritydoes not exist there. On main the has_compile_waiter() check fails too, independently of that line: main reports any queued job that is not SIZE_MAX, warm-ups included.PipelineCompilerind3d12_pipeline_compile.h, which still says "an idle checkpoint runs last".I only ran this one test, not the full suite. Feel free to take the patch or drop it.
Patch (git format-patch, applies on this branch)
AI-assisted: yes (Claude Code wrote and ran the test)
Hi @IlyassRS3, thanks for this pull request. It currently conflicts with
main(conflicting file(s):docs/d3d12_vulkan.md,runtime/shims/d3d12_pipeline_compile.h,runtime/shims/d3d12_vulkan_device.cpp), so it carries theneeds rebaselabel. CONTRIBUTING.md asks authors to rebase their branch onmain; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.AI-assisted (Claude Code, Claude Opus 5.5).
Test run of #1756 on Windows (AI-assisted, Claude Code).
Heads: PR head
90506453e(merge base0d80bc5df, which is whereorigin/mainstood when the branch was cut),origin/maincc0f662a7at test time.Head-only, not merged: the branch conflicts with current
main(docs/d3d12_vulkan.md,runtime/shims/d3d12_pipeline_compile.h,runtime/shims/d3d12_vulkan_device.cpp; 3,807 commits behind), so I tested the PR head detached, as is.Commands (Ninja, RelWithDebInfo,
LEONIDA_REQUIRE_VULKAN=ON,-j 8):Green: all 3 passed (
pipeline_cache_store0.02 s,pipeline_cache_file0.55 s,d3d12_compile_teardown5.9 s, not skipped on this GPU).pipeline_cache_fileincludes the changedd3d12_pipeline_checkpoint.handd3d12_compile_teardownincludes the changedd3d12_pipeline_compile.h.Red (non-test files of the PR reverted to the merge base, test file kept):
pipeline_cache_store_testdoes not build (CheckpointOutcome,CheckpointSchedule,default_checkpoint_interval_ms,default_checkpoint_retry_ms,checkpoint_priorityare not members ofleonida::pipeline_cache_store). That is only a missing-API failure.CheckpointSchedule::complete()andabort_claim()always waiting the full interval (a deferral burns the cooldown, as before the PR), the test builds and fails:line 113: schedule.next_due_ms() == 35050.CompileActivity::finish()before the checkpoint, andhas_compile_waiter()yielding only to priority 0 (so no red for those two; the green runs above only show they do not break the existing tests).Link: no new imports vs the merge base (gta6rt.exe at PR head and at
0d80bc5df: 641 imports each, identical). Against currentmainthe lists differ (one new,api-ms-win-crt-stdio-l1-1-0.dll!__stdio_common_vswscanf, and some thatmainhas and the old base does not), which is the 3,807-commit distance, not this PR.Not run: the in-game check (
LEONIDA_STARTUP_CACHE_CHECKPOINTS=1, checkpoints written during a run). With the branch this far behindmain, it is best done after the author mergesmain; the RTX 3090 can do it then.Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.
@IlyassRS3 the branch conflicts with
main: could you mergemaininto it when you have a moment?Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (
d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools,d3d12/, or general docs), please feel free to reopen it.