Skip to content

d3d12: parallel PipelineCompiler workers with reserved demand lane (RT-PIPELINE-COMPILER-WORKERS) - #1577

Closed
madhyn wants to merge 1 commit into
ProjectLeonida:mainfrom
madhyn:feat/pipeline-compiler-workers
Closed

madhyn wants to merge 1 commit into
ProjectLeonida:mainfrom
madhyn:feat/pipeline-compiler-workers

Conversation

@madhyn

@madhyn madhyn commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-PIPELINE-COMPILER-WORKERS (addresses unclaimed finding 7150502283 on #14; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at PR time — confirmed 0 file overlap with #1547, which explicitly left PipelineCompiler / d3d12_pipeline_compile.h separate).

What and why

rt::d3d12::PipelineCompiler in runtime/shims/d3d12_pipeline_compile.h previously ran a single THREAD_PRIORITY_BELOW_NORMAL worker for both speculative compute warmup and demanded async builds (LEONIDA_ASYNC_PIPELINES=1). As noted in finding 7150502283 on #14, a long driver compile in that single worker stalls every other queued pipeline build behind it.

This change upgrades PipelineCompiler to a bounded worker pool while preserving existing single-worker semantics when requested:

  • Configurable worker count (LEONIDA_COMPILE_THREADS, 1..32): defaults to 2..6 workers based on std::thread::hardware_concurrency() (>=18 -> 6, >=12 -> 4, >=6 -> 3, else 2). LEONIDA_COMPILE_THREADS=1 preserves the strict single-worker behavior.
  • Demand-lane reservation (workers >= 2): demanded builds (priority == 0) can run across all N workers; speculative warmup jobs (0 < priority < SIZE_MAX) are capped at N - 1 concurrent workers so at least one worker remains available for demanded builds without starving behind warmup.
  • Idle checkpoint & waiter accounting: SIZE_MAX idle checkpoints wait until running_ == 0, and has_compile_waiter() counts in-flight compile jobs (compile_running_ > 0) in addition to queued non-checkpoint jobs so cache checkpoints back off cleanly.
  • Safe standalone instances: explicit PipelineCompiler(unsigned workers = 0) constructor and joining destructor enable isolated unit tests while instance() stays a process-lifetime singleton.

Per-PSO variants_mutex_, big_compile serialization (>128 KiB), std::atomic<unsigned> active_compiles, publication mutex, and driver-synchronized VkPipelineCache (created without EXTERNALLY_SYNCHRONIZED_BIT) already guard concurrent compilation across call sites in d3d12_vulkan_device.cpp.

Tested

  • Cross-compiled new runtime/test/pipeline_compiler_test.cpp with clang-cl --target=x86_64-pc-windows-msvc /std:c++20 /O2 /W4 /permissive- /utf-8 (0 warnings in d3d12_pipeline_compile.h and pipeline_compiler_test.cpp) and ran under Wine 11.22 on Arch Linux (Intel Core i7-12700):
    • test_worker_count_config (hardware concurrency tiers + valid/invalid LEONIDA_COMPILE_THREADS values)
    • test_parallel_demand (2 demanded jobs reach a barrier concurrently on a 2-worker pool)
    • test_demand_lane_reserved (demanded job finishes immediately while speculative warmup fills N-1 workers and caps further warmup)
    • test_checkpoint_waits_for_idle (has_compile_waiter() reflects in-flight compiles; SIZE_MAX checkpoint runs only after active compiles finish)
    • test_single_worker_strict_order (workers=1 drains 0, smallest warmup, then SIZE_MAX)
    • Result: pipeline_compiler: all checks passed (17/17 checks).
  • python3 scripts/check_no_game_data.py: passed.

GPUs tested: n/a for unit test (CPU scheduler test); also exercised with live Vulkan pipeline compilation on NVIDIA GeForce RTX 3060 Ti (8 GB, NVIDIA Linux driver + Wine 11.22, Arch Linux x86_64).

AI-assisted: yes (Antigravity)

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: no translator change)

…T-PIPELINE-COMPILER-WORKERS)

Upgrade rt::d3d12::PipelineCompiler from a single below-normal worker to a
bounded worker pool (LEONIDA_COMPILE_THREADS, default 2..6 by hardware
concurrency; 1 preserves the single-worker path). With two or more workers,
speculative compute warmup is capped at N-1 concurrent jobs so at least one
worker stays reserved for demanded priority-0 builds, and SIZE_MAX idle
checkpoints wait until no compile jobs are running. Also count in-flight
compile jobs in has_compile_waiter().

Add unit tests in runtime/test/pipeline_compiler_test.cpp covering parallel
demanded builds, demand-lane reservation, idle checkpoint ordering,
single-worker priority order, and LEONIDA_COMPILE_THREADS parsing.
@QemmHD

QemmHD commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

One real-boot observation, n=1 and with a caveat (Windows 11, RTX 4070 SUPER, NVIDIA 621.24; main c7e82e2 + #1558 + this PR at b9ce8f2, with a fresh pipeline cache folder and the title's hang watchdog left on). Right after the first ExecuteIndirect at world start, the D3D12 call stream stopped for about 30 s while four pipeline-library compute pipelines of about 1.0 MB each built one after another (14.2 + 3.0 + 7.4 + 7.0 s = 31.6 s), and the watchdog ended the process.

So when one submission needs several uncompiled pipelines in a row, the reserved demand lane still serves them in sequence on the submitting thread. Each demand build blocks before the next one starts. The case of building a submission's pipelines side by side, which #1547 targets, might need to be combined with this.

Caveat: each cold boot compiles a different subset of the library (no overlap between four runs' build lists), and four large builds lining up at world start happened in 3 of 6 runs tonight with or without either PR. So one run can't rank the PRs. I'll repeat it with more runs per arm and post counts.

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

@minimob1337

Copy link
Copy Markdown

Native Windows run of this PR's unit test: Windows 11, 16 threads, NVIDIA RTX 4060 (610.88), MSVC (VS 2026 Community), main 237deb64c with this PR's diff (b9ce8f26c) applied cleanly.

  • pipeline_compiler_test builds with no new warnings and passes all 17 checks.
  • 300 consecutive runs on an idle machine: 300 passed, none over 30 s.
  • 300 consecutive runs with all 16 hardware threads pinned by normal-priority busy loops (the workers run below normal): 300 passed, none over 30 s.

One review note on what the extra workers can reach. On main, build_compute takes a process-wide big_compile mutex for any module over 128 KiB (runtime/shims/d3d12_vulkan_device.cpp:8028-8030, on unless LEONIDA_SERIAL_BIG_COMPILES=0). So whatever the worker count, only one large compute module compiles at a time, which fits @QemmHD's real-boot observation above (four ~1.0 MB pipelines built back to back). It also means the reserved demand lane can still wait behind speculative work: warm-up takes modules up to 1 MiB (LEONIDA_COMPUTE_WARMUP_MAX_BYTES), and a demanded build of a module over 128 KiB blocks on big_compile while a warm-up compile holds it. This is from reading the code, not a measurement; a real-boot run with LEONIDA_SERIAL_BIG_COMPILES=0 (memory permitting) or a priority-aware big-module gate would show whether this PR helps the world-start stall.

AI-assisted (Claude Code). No game files.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@madhyn

madhyn commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Closing this one. Since #1547 was merged and #1723 brought in the new PipelineCompiler lifetime model (cancel / after_running / retain-release / stop_for_exit), this pool design doesn't fit the current main anymore. As @QemmHD and @minimob1337 pointed out (thanks to both for testing and reviewing), the process-wide big_compile gate still serializes large modules no matter how many workers there are, so the extra workers would gain little. A priority-aware big_compile gate, so demanded builds don't wait behind warm-up, would be a better follow-up as its own small PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants