Skip to content

compile gate: auto default, calibrated per driver at device creation (RT-COMPILE-GATE-AUTO) - #2462

Merged
aslan2142 merged 3 commits into
ProjectLeonida:mainfrom
OreoFR:rt-compile-gate-auto
Nov 27, 2026
Merged

aslan2142 merged 3 commits into
ProjectLeonida:mainfrom
OreoFR:rt-compile-gate-auto

Conversation

@OreoFR

@OreoFR OreoFR commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-COMPILE-GATE-AUTO. The claim describes an allow-list of driver branches; this PR measures the driver instead.

Depends on: nothing (#2412 is merged)

What and why

The follow-up @aslan2142 asked for on #2412 (merge comment). It turns on the adaptive compile gate where the driver does not hold a submission behind a pipeline create, and keeps the classic gate where it does.

The new default is LEONIDA_COMPILE_GATE unset or auto. At device creation, before the VulkanContext and its SubmitMutex exist, the runtime calibrates the gate once (decide_auto_compile_gate, runtime/shims/d3d12_vulkan_device.cpp; runtime/src/compile_gate_calibration.h):

  • A worker thread creates a synthetic compute pipeline, with no pipeline cache. The SPIR-V is written in code by compile_gate_policy::slow_compute_spirv: a chain of fract(sin(..)) steps with branches, and specialization constant 0 seeded anew on each run, so no cache can answer it. No game data.
  • Meanwhile this thread submits an empty command buffer to the device's queue and waits for its fence, over and over, until the create returns.
  • The verdict (compile_gate_policy::judge, a pure function):
  • The verdict is cached per vendor, device, driver version and pipeline cache UUID in compile_gate_calibration.txt, beside the pipeline cache. So the calibration runs once per driver. Failures are not cached. LEONIDA_COMPILE_GATE_RECALIBRATE=1 measures again.
  • The decision is logged once, as [rt] compile-gate: auto: adaptive (calibrated on NVIDIA 621.40 in 0.7 s: 115 submissions came back in <= 0.5 ms beside a 722 ms create; cached in ...\compile_gate_calibration.txt; LEONIDA_COMPILE_GATE=classic|adaptive overrides). Otherwise it says auto: classic (...) with the reason, or (cached calibration of ...).
  • Only gta6rt's title process calibrates (compile_gate_policy::calibration_allowed, set in main.cpp). --precompile-pipelines, --compile-server and the test processes keep the classic gate. A forced value never waits for the calibration.

What forcing still does:

  • classic (new), adaptive, 1 and 0 force a mode, as before.
  • An unknown value is classic, as on main, and is now logged.
  • Until the decision, auto is classic, so nothing can be gated differently from main before it.

How the gate's mode is read:

  • SubmitMutex::compile_gate_mode() now reads an atomic, set once by decide_auto_gate.
  • gate_defaults() keeps one set per mode, so the adaptive limit and budget apply when auto picks adaptive.

Why a calibration can ask what an in-game probe cannot: the adaptive gate learns by letting a submission into the driver beside one of the title's creates, which can take 30-60 s. On a driver that holds submissions (630.20), that probe loses the device once it waits 10 s more. The calibration's create takes about a second and its submissions are empty. On such a driver, one is held for about a second and comes back with the create, which cannot reach the 10 s TDR.

Evidence the verdict should agree with, from #2412 comment 7217585003, now in docs/d3d12_vulkan.md:

README rows: LEONIDA_COMPILE_GATE (default auto), the new LEONIDA_COMPILE_GATE_RECALIBRATE, and the wait and probe rows now say "with the adaptive gate".

What changes for users: on main, LEONIDA_COMPILE_GATE unset means the classic gate. With this PR, unset means auto: calibrate once per driver, then adaptive or classic. Behaviour with any explicit value is unchanged. The real test is therefore a run with the variable unset, on several drivers.

Tested

  • compile_gate_policy_test (new, pure stdlib, no GPU) passes. It covers:
  • vk_compile_gate_calibration_test (new, GPU; 77 without a device) expects a verdict within 20 s, and adaptive on NVIDIA 621.40. MEASURED, on this RTX 4070 Laptop (621.40):
  • submit_mutex_test passes with LEONIDA_COMPILE_GATE unset, auto, classic, adaptive, 1, 0 and an unknown value. vulkan_device_fault_test passes. MEASURED
  • gta6rt, gta6rt_tls_host and the tests build with MSVC 14.44.
  • The clang front end in clang-tidy (LLVM 19.1.5, -std=c++20 -Wall -Wextra, header filter on the new headers) reports no errors or warnings in the two new tests, submit_mutex_test.cpp or vulkan_device_fault_test.cpp. GCC was not checked.
  • tests/test_docs_*.py pass. check_no_game_data.py is clean on every changed file.
  • Not run: the title. The path in decide_auto_compile_gate (cache read and write, the log line) runs only in the title process, so it is READ IN CODE only. The calibration it calls is the one the GPU test runs.

GPUs tested: NVIDIA GeForce RTX 4070 Laptop 8 GB, driver 621.40, Windows 11 (calibration: adaptive). NVIDIA GeForce RTX 4060, driver 610.88 (MoonEyes): merges cleanly, builds, the new tests and submit_mutex_test pass, calibration adaptive (163 submissions <= 0.7 ms beside a 957 ms create, 1.07 s); his in-game run follows.

AI-assisted: yes (Claude Code, Claude Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: runtime only)

🤖 Generated with Claude Code

With LEONIDA_COMPILE_GATE unset or `auto`, the device measures the driver once
before the VulkanContext exists: a worker creates a synthetic compute pipeline
(SPIR-V written in code, new specialization seed each run, no pipeline cache)
while the queue gets empty submissions waited on a fence. Quick submissions with
the create still in the driver give the adaptive gate; a held one, a create too
fast to tell, an unexplained slow submission or any failure give classic. The
verdict is cached per vendor/device/driver/pipelineCacheUUID in
compile_gate_calibration.txt beside the pipeline cache;
LEONIDA_COMPILE_GATE_RECALIBRATE=1 measures again. Only the title process
calibrates; build hosts and tests keep classic. `classic`, `adaptive`, `1`, `0`
still force a mode.

Tests: compile_gate_policy_test (no GPU: settings, NVIDIA version decode,
verdicts, cache file, SPIR-V shape) and vk_compile_gate_calibration_test (GPU:
a verdict within 20 s, adaptive on NVIDIA 621.40).

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

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

In-game check of the title path, which the PR body listed as not run. main + this PR only, RTX 4070 Laptop, NVIDIA 621.40, LEONIDA_COMPILE_GATE unset, first run on this driver:

[rt] compile-gate: calibration trial 1: 600 statements, create 462 ms, 73 submissions, adaptive
[rt] compile-gate: auto: adaptive (calibrated on NVIDIA 621.40 in 0.5 s: 69 submissions came back in <= 11.3 ms beside a 462 ms create; cached in …\compile_gate_calibration.txt; …)

The game then booted normally into the world phase, with no device loss and no gate hang. Startup cost: 0.5 s. MEASURED (n=1). The cached-verdict path, on a second run, is still to check. AI-assisted (Claude Code, Claude Opus 5.5).

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Second run on the same driver: the cached verdict is reused, with no calibration and no startup delay:

[rt] compile-gate: auto: adaptive (cached calibration of NVIDIA 621.40: 69 submissions came back in <= 11.3 ms beside a 462 ms create; in …\compile_gate_calibration.txt, LEONIDA_COMPILE_GATE_RECALIBRATE=1 measures again; …)

Both title paths (first run and cache) are now checked in game. AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142

Copy link
Copy Markdown
Collaborator

Measuring the driver instead of maintaining a branch allow-list is much cleaner — and vk_compile_gate_calibration_test passes cleanly on Linux AMD RADV (RDNA2 iGPU: trial 1, 1,000 statements, 391 ms create, 61 submissions <= 1.5 ms → adaptive in 393 ms), compile_gate_policy_test builds with 0 warnings under GCC -Wall -Wextra, and slow_compute_spirv(1000) passes spirv-val (vulkan1.0 and vulkan1.2).

Two small issues on 1d7c662b4 to fix before merge:

  • runtime/shims/d3d12_vulkan_device.cpp:218-236 (decide_auto_compile_gate): Outcome::unclear still has result.ok == true, so a transient background GPU spike during the first launch writes outcome=unclear to compile_gate_calibration.txt and permanently locks subsequent runs onto classic until a driver update or manual LEONIDA_COMPILE_GATE_RECALIBRATE=1. Skipping cache write on Outcome::unclear (just like !result.ok) lets the next launch get a clean measurement.
  • runtime/src/compile_gate_calibration.h:198-210: if vk.wait_fences(..., kFenceTimeoutNs) returns VK_TIMEOUT, fail() immediately calls cleanup() (vkDestroyFence / vkDestroyCommandPool) while the submitted command buffer and fence may still be pending on queue (Vulkan UB). After worker.join(), waiting on fence (or vkQueueWaitIdle) before destroying them when submit_result == VK_TIMEOUT makes cleanup spec-valid.

AI-assisted review (Antigravity, Gemini 4 Neon).

OreoFR and others added 2 commits November 27, 2026 13:43
…fence

Review of ProjectLeonida#2462:
- An unclear calibration (a slow submission the create does not explain) is no
  longer written to compile_gate_calibration.txt, and an unclear line an
  earlier build wrote is not read as a cached verdict: the next launch measures
  again instead of staying classic until the driver changes
  (compile_gate_policy::cache_verdict, tested in compile_gate_policy_test).
- After a fence wait that fails, the fence and command pool are destroyed only
  once the pending submission's fence signals (up to 60 s); on a lost device or
  if it still has not, they are left alive. The verdict stays classic.

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

Copy link
Copy Markdown
Collaborator
RUN w62 | 2026-11-27 | MoonEyes | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:        PR head 1d7c662b4 (based on f75b2b425; merges cleanly with main 3d6050b96) | shaders: locally retranslated, not the documented default
MACHINE:     RTX 4060 8 GB, desktop, only GPU, display attached | NVIDIA 610.88 | Windows 11 Pro 26300 | 32 GB
RECIPE:      none documented for this PR; my usual local recipe + DEVIATIONS: LEONIDA_COMPILE_GATE unset (auto), a few other local variables | precompile: warm pipeline cache
OS/DRIVER:   Windows GPU timeout at the default (2 s)
TARGET:      compile-gate verdict on 610.88 | PREDICTED (written before the run): adaptive, no device loss in 600 s
MEASURED:    auto: adaptive, calibrated in 1.6 s, 264 submissions back in <= 1.5 ms beside a 1543 ms create | duration 604 s | game stopped presenting around 300 s
END:         time limit, read from the log
CACHES:      pipeline cache warm; calibration cold (written by this run) | RUN 1 of 2, same recipe
INSTRUMENTS: a few local diagnostic variables, none about the gate
VERDICT:     prediction held

Second run, same recipe, 120 s, calibration cached: the log line reads auto: adaptive (cached calibration of NVIDIA 610.88 ...) and no calibration trial ran again.

What I checked on this branch:

  • Merge: clean with current main (git merge-tree, no conflict). MEASURED.
  • Build: gta6rt and the tests build with MSVC. MEASURED.
  • compile_gate_policy_test: passes. submit_mutex_test: passes. MEASURED.
  • vk_compile_gate_calibration_test on the RTX 4060 (610.88): adaptive at the first trial, 163 submissions of at most 0.7 ms beside a 957 ms create, 1071 ms in all. MEASURED.
  • In the game (one run): the runtime logged compile-gate: auto: adaptive (calibrated on NVIDIA 610.88 in 1.6 s: 264 submissions came back in <= 1.5 ms beside a 1543 ms create; ...). During 600 s, 581 submissions waited for pipeline creates, 10.3 s in all, longest 1.9 s, 7 went ahead at the limit. No device loss. MEASURED.
  • The calibration file was written next to the pipeline cache and was read back on the next run. MEASURED.

Notes:

  • This agrees with the earlier note that the gate was turned off on this card with no regression, but with auto the gate is adaptive here, not off. The adaptive gate still made short waits (up to 1.9 s) once the title started creating compute pipelines; whether that matters for the 2 s Windows timeout is a hypothesis, not tested (I ran no classic comparison).
  • My game stops presenting around 300 s in this and in my other recent runs; I do not attribute it to the gate.
  • One machine, one 600 s run and one 120 s run. Indicative only.

AI-assisted (Claude Code, Claude Opus 5.5)

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @aslan2142, both points are fixed in e31f50b. It is on top of a merge of current main (6af3b1d), which merged cleanly.

  • Unclear verdicts cached: fixed.
    • decide_auto_compile_gate writes the cache only when compile_gate_policy::cache_verdict(outcome) is true. That is every outcome except unclear.
    • An unclear run still gives classic for this launch. The log ends with not cached: the next launch measures again.
    • find_entry also ignores an unclear line written by the previous head, so a file that already has one measures again.
    • compile_gate_policy_test covers this: cache_verdict per outcome, and an unclear line next to a valid one for another key.
    • A too_fast result at the largest size is still cached, because it does not change from one launch to the next.
  • Fence destroyed while pending: fixed.
    • A trial now tracks whether a submission is in flight.
    • After worker.join(), if the submit or wait failed, it waits on the fence again, for up to 60 s, before cleanup() destroys the fence and command pool.
    • On VK_ERROR_DEVICE_LOST, or if the fence still has not signalled, the fence and pool are left alive. The verdict is classic, as before.

Re-run on Windows 11, MSVC 14.44, RTX 4070 Laptop, NVIDIA 621.40, MEASURED:

  • compile_gate_policy_test passes.
  • vk_compile_gate_calibration_test, 3 runs: adaptive each time, 95-109 submissions of at most 0.5 ms beside 600-686 ms creates.
  • submit_mutex_test and vulkan_device_fault_test pass.
  • clang front end (LLVM 19, -Wall -Wextra): clean.
  • test_docs_*.py pass, and check_no_game_data.py is clean.
  • The docs paragraph and the README row now say that unclear verdicts are not cached.

The timeout path itself was not exercised: it needs a fence that does not signal within 10 s.

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

@aslan2142

Copy link
Copy Markdown
Collaborator

Both review items look cleanly fixed in e31f50bbc (cache_verdict excluding Outcome::unclear on both write and read, and draining a pending fence before destroying fence/pool).

Verified locally on main (f5c3ea2e0) + e31f50bbc:

  • scripts/check_no_game_data.py clean; test_docs_env_vars (5/5), test_docs_repo_paths (3/3), test_ctest_registration (6/6), test_ci_registration (18/18) pass.
  • compile_gate_policy_test builds with 0 warnings under GCC -Wall -Wextra and passes.
  • Full clang-cl + xwin + Wine runtime test suite on Linux AMD RDNA2 iGPU (RADV, 498 tests): compile_gate_policy and vk_compile_gate_calibration pass; 0 regressions vs main-e5a7c8942 (433 pass, 40 skip, 23 pre-existing Wine-iGPU failures, 2 disabled).

Merging.

AI-assisted review (Antigravity, Gemini 4 Neon).

@aslan2142
aslan2142 merged commit 17d3af0 into ProjectLeonida:main Nov 27, 2026
6 of 11 checks passed
Tyrannys added a commit to Tyrannys/leonida that referenced this pull request Nov 27, 2026
…-warmup-free

Main now has ubiquitarianism#2233 (the compile pool) and the adaptive compile gate (ubiquitarianism#2412, ubiquitarianism#2462).
The warm-up-free gate is ported onto it: a speculative create is an InDriver entry
flagged speculative that submissions do not wait for, at most warmup_max in the
driver at once, except one of a kind whose verdict is driver_holds, which is waited
for as a demanded create is. submit_mutex_test checks that case too.

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

DadaCap commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Third driver, as asked by @OreoFR: the auto line, the cache reread, and a world run in auto.

RUN m189-m190-m192 | 2026-11-27 | DadaCap | AI-assisted: Claude Code (Opus 5.5)
BASE:        m189/m190: main 3d6050b96 + #2462 (e31f50bbc) | m192: main 3d6050b96 + #2462 (e31f50bbc) + #1920 + #2465 + local unmerged test commits | shaders: locally retranslated library, wave64 emulation on
MACHINE:     NVIDIA GeForce RTX 5080 16 GB, desktop (profile M) | 621.24 | Windows 11 | 62 GB
RECIPE:      documented ("Windows, NVIDIA") + DEVIATIONS: LEONIDA_ARENA_MB=4095 LEONIDA_TITLE_WINDOWS_MB=4096 LEONIDA_PIPELINE_CACHE_BYTES=4294967296 LEONIDA_SERIAL_BIG_COMPILES=0, LEONIDA_COMPILE_GATE unset | precompile: no
OS/DRIVER:   TdrDelay / TdrDdiDelay not set (Windows defaults)
TARGET:      auto verdict on first and second start; world run in auto: device lost yes/no | PREDICTED: none written before the runs
MEASURED:    m189 (first start, new build): "auto: adaptive (calibrated ... in 0.6 s: 96 submissions came back in <= 0.3 ms beside a 586 ms create)"
             m190 (second start, same build): "auto: adaptive (cached calibration ...)", no calibration, no startup delay
             m192 (another build, same driver, same cache folder): cached verdict reused; world from 106 s; 0 device lost in 1,201 s;
             about 0.44 frames/s in the world (425 world frames); submit lock held at most about 1.1 s (twice, at 177 s and 217 s)
END:         time limit (all three; m190 was a 240 s start-up check)
CACHES:      warm: pipeline cache and journal shared by the three runs; calibration file written by m189, read by m190 and m192 | RUN 1 of 1 each
INSTRUMENTS: memory probe every 10 s (m192), periodic captures (m192)
VERDICT:     no prediction written; auto behaved as described in the PR on this driver

Not caused by this PR, as far as I can tell: m189 (main + #2462 only) stayed on the title screen for its whole 20 minutes. Its pipeline cache was warm (11 graphics and 1,757 compute pipelines made in 18 s of creates in all), and the submit lock was never reported held. m192 with the same gate verdict reached the world. MEASURED (n=1); the difference between the two trees is the unmerged commits listed in BASE, not the gate.

This comment was AI-assisted (Claude Code, Opus 5.5).

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