[PR #1756] d3d12: finish CompileActivity before checkpointing and retry deferred cache snapshots #1770

Open
opened 2026-11-26 21:18:53 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1756
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/startup-cache-checkpoint-race


📝 Commits (2)

  • de3c463 d3d12: finish CompileActivity before checkpointing and retry deferred cache snapshots
  • 9050645 d3d12: schedule startup cache checkpoints at 30s/priority-1 and yield only to priority-0 waiters

📊 Changes

6 files changed (+172 additions, -32 deletions)

View changed files

📝 docs/d3d12_vulkan.md (+3 -2)
📝 runtime/shims/d3d12_pipeline_checkpoint.h (+13 -2)
📝 runtime/shims/d3d12_pipeline_compile.h (+3 -1)
📝 runtime/shims/d3d12_vulkan_device.cpp (+38 -26)
📝 runtime/src/pipeline_cache_store.h (+57 -0)
📝 runtime/test/pipeline_cache_store_test.cpp (+58 -1)

📄 Description

Item ID: RT-STARTUP-CACHE-CHECKPOINT-RACE
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7160859893
Addresses finding: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157762911 (LEONIDA_STARTUP_CACHE_CHECKPOINTS=1 wrote nothing during a 200 s run reaching world start)

Root cause

Two bugs in runtime/shims/d3d12_vulkan_device.cpp prevented startup cache checkpoints from saving during active world-start compilation bursts:

  1. maybe_checkpoint(cache) ran while CompileActivity still held device_.active_compiles >= 1: both PipelineState::build_compute() (lines 8038, 8091) and PipelineState::build() (lines 8202, 8386) kept CompileActivity activity(device_.active_compiles) alive across maybe_checkpoint(cache). When the build ran on a submitting or PipelineWorkers thread, the PipelineCompiler worker woke up before the caller returned, saw active_compiles != 0, and skipped save_session_pipeline_cache().
  2. Deferred checkpoints still burned the 120-second cooldown: whenever a checkpoint job was skipped because active_compiles != 0 or has_compile_waiter() (either before save_session_pipeline_cache() at line 8110 or after the size query at line 1348), maybe_checkpoint() unconditionally pushed next_checkpoint out by another 120,000 ms (2 minutes). A single deferral at t = 120 s pushed the next attempt to t = 240 s — past the world-start crash at ~195–200 s, leaving the cache directory empty.

Changes

  • runtime/src/pipeline_cache_store.h:
    • Added CheckpointOutcome (saved, deferred, skipped) and portable atomic CheckpointSchedule (try_claim, complete, abort_claim). Deferred checkpoints and queue-submission aborts stay immediately eligible on the next compile completion instead of burning default_checkpoint_interval_ms (120 s); saved and skipped (empty/over-cap/I/O error) advance the deadline by the full interval with saturation below the in_flight sentinel.
  • runtime/shims/d3d12_pipeline_checkpoint.h:
    • Added idempotent CompileActivity::finish() (with acq_rel ordering) 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:
    • Replaced raw next_checkpoint atomic with CheckpointSchedule checkpoint_schedule.
    • In build_compute(), publish pipeline_.pipeline, unlock variants_mutex_, and call activity.finish() before maybe_checkpoint(cache); in build(), call activity.finish() before maybe_checkpoint(cache).
    • Updated save_session_pipeline_cache() to return CheckpointOutcome (returning deferred if active_compiles or has_compile_waiter() became active during the size query) and wired maybe_checkpoint() to checkpoint_schedule.complete(..., outcome).
  • runtime/test/pipeline_cache_store_test.cpp:
    • Added test_checkpoint_schedule() covering due-time gating, in-flight coalescing, deferred immediate retry, abort_claim, saved/skipped 120 s cooldown, and near-UINT64_MAX saturation.

Verification

  • AI-assisted: yes
  • GPUs tested: none (developed on macOS without Windows SDK or Vulkan SDK)
  • Tested locally:
    • 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.
  • Not compiled/executed locally: runtime/shims/d3d12_pipeline_checkpoint.h and runtime/shims/d3d12_vulkan_device.cpp are Windows/Vulkan shims and rely on Windows CI / a retail boot run with LEONIDA_STARTUP_CACHE_CHECKPOINTS=1.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1756 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/startup-cache-checkpoint-race` --- ### 📝 Commits (2) - [`de3c463`](https://github.com/ubiquitarianism/leonida/commit/de3c463e2f226359fc7771e4673eaa7ece38a7fe) d3d12: finish CompileActivity before checkpointing and retry deferred cache snapshots - [`9050645`](https://github.com/ubiquitarianism/leonida/commit/90506453e8da6494a41fc9b024f766d8d4d7876f) d3d12: schedule startup cache checkpoints at 30s/priority-1 and yield only to priority-0 waiters ### 📊 Changes **6 files changed** (+172 additions, -32 deletions) <details> <summary>View changed files</summary> 📝 `docs/d3d12_vulkan.md` (+3 -2) 📝 `runtime/shims/d3d12_pipeline_checkpoint.h` (+13 -2) 📝 `runtime/shims/d3d12_pipeline_compile.h` (+3 -1) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+38 -26) 📝 `runtime/src/pipeline_cache_store.h` (+57 -0) 📝 `runtime/test/pipeline_cache_store_test.cpp` (+58 -1) </details> ### 📄 Description Item ID: `RT-STARTUP-CACHE-CHECKPOINT-RACE` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7160859893 Addresses finding: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157762911 (`LEONIDA_STARTUP_CACHE_CHECKPOINTS=1` wrote nothing during a 200 s run reaching world start) ## Root cause Two bugs in `runtime/shims/d3d12_vulkan_device.cpp` prevented startup cache checkpoints from saving during active world-start compilation bursts: 1. **`maybe_checkpoint(cache)` ran while `CompileActivity` still held `device_.active_compiles >= 1`**: both `PipelineState::build_compute()` (lines 8038, 8091) and `PipelineState::build()` (lines 8202, 8386) kept `CompileActivity activity(device_.active_compiles)` alive across `maybe_checkpoint(cache)`. When the build ran on a submitting or `PipelineWorkers` thread, the `PipelineCompiler` worker woke up before the caller returned, saw `active_compiles != 0`, and skipped `save_session_pipeline_cache()`. 2. **Deferred checkpoints still burned the 120-second cooldown**: whenever a checkpoint job was skipped because `active_compiles != 0` or `has_compile_waiter()` (either before `save_session_pipeline_cache()` at line 8110 or after the size query at line 1348), `maybe_checkpoint()` unconditionally pushed `next_checkpoint` out by another 120,000 ms (2 minutes). A single deferral at `t = 120 s` pushed the next attempt to `t = 240 s` — past the world-start crash at `~195–200 s`, leaving the cache directory empty. ## Changes - **`runtime/src/pipeline_cache_store.h`**: - Added `CheckpointOutcome` (`saved`, `deferred`, `skipped`) and portable atomic `CheckpointSchedule` (`try_claim`, `complete`, `abort_claim`). Deferred checkpoints and queue-submission aborts stay immediately eligible on the next compile completion instead of burning `default_checkpoint_interval_ms` (120 s); `saved` and `skipped` (empty/over-cap/I/O error) advance the deadline by the full interval with saturation below the `in_flight` sentinel. - **`runtime/shims/d3d12_pipeline_checkpoint.h`**: - Added idempotent `CompileActivity::finish()` (with `acq_rel` ordering) 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`**: - Replaced raw `next_checkpoint` atomic with `CheckpointSchedule checkpoint_schedule`. - In `build_compute()`, publish `pipeline_.pipeline`, unlock `variants_mutex_`, and call `activity.finish()` before `maybe_checkpoint(cache)`; in `build()`, call `activity.finish()` before `maybe_checkpoint(cache)`. - Updated `save_session_pipeline_cache()` to return `CheckpointOutcome` (returning `deferred` if `active_compiles` or `has_compile_waiter()` became active during the size query) and wired `maybe_checkpoint()` to `checkpoint_schedule.complete(..., outcome)`. - **`runtime/test/pipeline_cache_store_test.cpp`**: - Added `test_checkpoint_schedule()` covering due-time gating, in-flight coalescing, deferred immediate retry, `abort_claim`, saved/skipped 120 s cooldown, and near-`UINT64_MAX` saturation. ## Verification - **AI-assisted**: yes - **GPUs tested**: none (developed on macOS without Windows SDK or Vulkan SDK) - **Tested locally**: - `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**. - **Not compiled/executed locally**: `runtime/shims/d3d12_pipeline_checkpoint.h` and `runtime/shims/d3d12_vulkan_device.cpp` are Windows/Vulkan shims and rely on Windows CI / a retail boot run with `LEONIDA_STARTUP_CACHE_CHECKPOINTS=1`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1770
No description provided.