LLVM: Memory becomes saturated in configurations with many threads. #3041

Open
opened 2026-12-02 04:24:49 +00:00 by boludoz · 3 comments
boludoz commented 2026-12-02 04:24:49 +00:00 (Migrated from github.com)
Contributor

Imported from ubiquitarianism/leonida. Originally posted by boludoz on 2026-12-02T04:24:49Z.


The shader LLVM will fail when faced with many threads and cores; It is well known that each additional core places stress on the RAM.

Specs: RTX 3080 ASUS 12 GB Edition, 32 GB of ram DDR5, Ryzen 7 9850X3D.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:6736924547 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3041). Originally posted by [boludoz](https://github.com/boludoz) on 2026-12-02T04:24:49Z. --- The shader LLVM will fail when faced with many threads and cores; It is well known that each additional core places stress on the RAM. Specs: RTX 3080 ASUS 12 GB Edition, 32 GB of ram DDR5, Ryzen 7 9850X3D.
lixonik commented 2026-12-02 07:21:21 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T07:21:21Z.


finding: source audit + 16C/32T RAM scaling check on main (f37c4a87d, Windows 10 Pro 22H2, AMD Ryzen 9 5950X 16C/32T, 64 GiB RAM, NVIDIA RTX 5070 12 GB, driver 621.40).

1. Where high thread counts currently scale memory without a RAM cap

While d3d12/crates/leonida-shader emits LLVM 3.7 DXIL bitcode directly in Rust (emit/bitcode.rs) rather than linking LLVM, there are four concrete shader / LLVM / driver-compiler paths in the repo whose default concurrency scales purely with logical core count and ignores available host RAM:

  1. d3d12/apps/leonida/src/warm.rs:71-72 (warm::warm, invoked by play-nvidia.ps1:894-913): defaults to cores * 2 / 3 separate child processes (21 processes on a 32-thread CPU), each creating its own ID3D12Device, GPU driver shader compiler context, and mimalloc arenas, with no upper bound and no host-RAM check.
  2. d3d12/apps/leonida/src/warm.rs:305 (warm::store_these): defaults to available_parallelism() * 2 / 3 unbounded threads inside a single process creating pipelines concurrently via known::store, with no upper clamp or RAM check.
  3. d3d12/apps/leonida/src/translate.rs:275, 291-306, 402 (leonida translate and leonida shaders): defaults to 100% of available_parallelism() (32 threads on 16C/32T) with no upper cap. In translate.rs:291-306, every worker inside the per-file thread::scope also instantiates its own Validator::load (dxil.dll LLVM 3.7 validator) and Host::new(false) (ID3D12Device + driver compiler) per file.
  4. scripts/build_llvm_mc.ps1:60, 130: builds LLVM's llvm-mc from C++ source with -j ([Environment]::ProcessorCount / 2) (16 parallel MSVC cl.exe processes on 32T) without checking available RAM or limiting LLVM_PARALLEL_LINK_JOBS / LLVM_PARALLEL_COMPILE_JOBS.

2. Measured RAM scaling on 16C/32T (Ryzen 9 5950X, 64 GiB RAM)

30-35 s sampled runs of leonida.exe (main f37c4a87d, --profile diag, diagnostics,dlss,mimalloc) against a local title package (read-only translate):

Command --jobs Peak private bytes Peak working set
leonida translate (CPU DXIL emission only) 8 887.8 MB 876.5 MB
leonida translate (CPU DXIL emission only) 32 (default) 1,865.7 MB (2.10x) 1,854.6 MB
leonida translate --pipelines (per-thread Host::new) 8 / 32 3,495.6 MB / 3,495.1 MB (within 35 s) 3,460-3,463 MB

3. Existing pattern in the repo worth reusing

In-game pipeline compilation (d3d12/crates/leonida-gpu/src/pipeline/make.rs:1034-1053) already caps workers to (processors * 2 / 3).clamp(2, 16) and drops the cap to (processors * 3 / 4).clamp(2, 8) when residency::limited_host_ram() (GlobalMemoryStatusEx, ullTotalPhys <= 32 GiB, d3d12/crates/leonida-gpu/src/residency.rs:175-178) holds.

Applying a similar RAM-aware cap (e.g., GlobalMemoryStatusEx total/available physical RAM + clamp on default jobs in warm.rs, translate.rs, and build_llvm_mc.ps1, plus hoisting Host::new / Validator reuse out of the per-file worker loop in translate.rs:291-306) would prevent high-core-count machines with 16-32 GiB RAM from saturating memory while still allowing explicit --jobs N overrides.

AI-assisted: yes (Antigravity).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7338084170 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3041#issuecomment-7338084170). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-02T07:21:21Z. --- finding: source audit + 16C/32T RAM scaling check on `main` (`f37c4a87d`, Windows 10 Pro 22H2, AMD Ryzen 9 5950X 16C/32T, 64 GiB RAM, NVIDIA RTX 5070 12 GB, driver `621.40`). ### 1. Where high thread counts currently scale memory without a RAM cap While `d3d12/crates/leonida-shader` emits LLVM 3.7 DXIL bitcode directly in Rust (`emit/bitcode.rs`) rather than linking LLVM, there are **four concrete shader / LLVM / driver-compiler paths** in the repo whose default concurrency scales purely with logical core count and ignores available host RAM: 1. **`d3d12/apps/leonida/src/warm.rs:71-72` (`warm::warm`, invoked by `play-nvidia.ps1:894-913`):** defaults to `cores * 2 / 3` **separate child processes** (`21` processes on a 32-thread CPU), each creating its own `ID3D12Device`, GPU driver shader compiler context, and mimalloc arenas, with no upper bound and no host-RAM check. 2. **`d3d12/apps/leonida/src/warm.rs:305` (`warm::store_these`):** defaults to `available_parallelism() * 2 / 3` unbounded threads inside a single process creating pipelines concurrently via `known::store`, with no upper clamp or RAM check. 3. **`d3d12/apps/leonida/src/translate.rs:275, 291-306, 402` (`leonida translate` and `leonida shaders`):** defaults to `100%` of `available_parallelism()` (`32` threads on 16C/32T) with no upper cap. In `translate.rs:291-306`, every worker inside the per-file `thread::scope` also instantiates its own `Validator::load` (`dxil.dll` LLVM 3.7 validator) and `Host::new(false)` (`ID3D12Device` + driver compiler) per file. 4. **`scripts/build_llvm_mc.ps1:60, 130`:** builds LLVM's `llvm-mc` from C++ source with `-j ([Environment]::ProcessorCount / 2)` (`16` parallel MSVC `cl.exe` processes on 32T) without checking available RAM or limiting `LLVM_PARALLEL_LINK_JOBS` / `LLVM_PARALLEL_COMPILE_JOBS`. ### 2. Measured RAM scaling on 16C/32T (Ryzen 9 5950X, 64 GiB RAM) 30-35 s sampled runs of `leonida.exe` (`main` `f37c4a87d`, `--profile diag`, `diagnostics,dlss,mimalloc`) against a local title package (read-only `translate`): | Command | `--jobs` | Peak private bytes | Peak working set | |---|---:|---:|---:| | `leonida translate` (CPU DXIL emission only) | 8 | 887.8 MB | 876.5 MB | | `leonida translate` (CPU DXIL emission only) | 32 (default) | **1,865.7 MB** (2.10x) | **1,854.6 MB** | | `leonida translate --pipelines` (per-thread `Host::new`) | 8 / 32 | **3,495.6 MB / 3,495.1 MB** (within 35 s) | **3,460-3,463 MB** | ### 3. Existing pattern in the repo worth reusing In-game pipeline compilation (`d3d12/crates/leonida-gpu/src/pipeline/make.rs:1034-1053`) **already** caps workers to `(processors * 2 / 3).clamp(2, 16)` and drops the cap to `(processors * 3 / 4).clamp(2, 8)` when `residency::limited_host_ram()` (`GlobalMemoryStatusEx`, `ullTotalPhys <= 32 GiB`, `d3d12/crates/leonida-gpu/src/residency.rs:175-178`) holds. Applying a similar RAM-aware cap (e.g., `GlobalMemoryStatusEx` total/available physical RAM + `clamp` on default jobs in `warm.rs`, `translate.rs`, and `build_llvm_mc.ps1`, plus hoisting `Host::new` / `Validator` reuse out of the per-file worker loop in `translate.rs:291-306`) would prevent high-core-count machines with 16-32 GiB RAM from saturating memory while still allowing explicit `--jobs N` overrides. AI-assisted: yes (Antigravity).
fiestorra commented 2026-12-02 15:05:31 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T15:05:31Z.


Measured on a 16-thread machine, and the number narrows this: the shader cache build is not the memory hog. Peak working set of leonida.exe shaders over the whole archive (25,689 files, empty cache each time):

--jobs time peak RSS
4 107.5 s 1,135 MB
8 72.7 s 1,581 MB
16 65.5 s 2,301 MB

About 950 MB of baseline and some 85 MB for each worker (950 + 85 x jobs fits all three points within 60 MB), so thirty-two threads of this would be around 3.7 GB - a lot for a start-up phase, but not "memory saturated" on the 16-64 GB machines people run this on, and it says nothing about the crash the report is about.

Which leaves warm, exactly where the finding above points. warm compiles host pipelines, and each of those is a driver-compiler job; the process's memory is then dominated by whatever the compiler holds per pipeline, not by the translator's own allocations. That is a different measurement than the one above and needs a known-pipelines.bin from a real run to make - the book is what tells warm which pipelines to make, and without the game there is nothing for it to compile here.

So: with a known-pipelines.bin (the cache folder of anyone who has run the title, %LOCALAPPDATA%\Leonida\d3d12\cache) leonida warm --jobs N can be measured the same way, at 4, 8 and 16, and then a default that respects free memory can be written with a number behind it rather than a guess. The measurement above is the part of the path that does not need the game, and it says the cap belongs on warm, not on shaders.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7349701718 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3041#issuecomment-7349701718). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T15:05:31Z. --- **Measured on a 16-thread machine, and the number narrows this: the shader cache build is not the memory hog. Peak working set of `leonida.exe shaders` over the whole archive (25,689 files, empty cache each time):** | `--jobs` | time | peak RSS | | --- | --- | --- | | 4 | 107.5 s | **1,135 MB** | | 8 | 72.7 s | **1,581 MB** | | 16 | 65.5 s | **2,301 MB** | **About 950 MB of baseline and some 85 MB for each worker** (`950 + 85 x jobs` fits all three points within 60 MB), so **thirty-two threads of this would be around 3.7 GB** - a lot for a start-up phase, but **not "memory saturated" on the 16-64 GB machines people run this on**, and it says nothing about the crash the report is about. **Which leaves `warm`, exactly where the finding above points.** `warm` compiles **host pipelines**, and each of those is a driver-compiler job; the process's memory is then dominated by whatever the compiler holds per pipeline, not by the translator's own allocations. **That is a different measurement than the one above and needs a `known-pipelines.bin` from a real run to make** - the book is what tells `warm` which pipelines to make, and without the game there is nothing for it to compile here. **So**: with a `known-pipelines.bin` (the cache folder of anyone who has run the title, `%LOCALAPPDATA%\Leonida\d3d12\cache`) **`leonida warm --jobs N` can be measured the same way**, at 4, 8 and 16, and then a default that respects free memory can be written with a number behind it rather than a guess. **The measurement above is the part of the path that does *not* need the game, and it says the cap belongs on `warm`, not on `shaders`.** **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
fiestorra commented 2026-12-02 15:12:19 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T15:12:19Z.


Measured warm itself, on this machine's own book (48 MB, known-pipelines.bin from a real run) with the compiler children counted - peak of the parent plus every leonida*.exe child, polled every 200 ms:

pipelines made --jobs time peak (parent + children)
24 4 6.5 s 518 MB
24 8 8.4 s 765 MB
24 16 6.0 s 760 MB
140 4 4.4 s 1,514 MB
540 16 52.9 s 4,578 MB

Two things follow, and the second one changes what the fix should be:

  1. It grows with how many pipelines are made, roughly 8-11 MB each (140 at 1.5 GB, 540 at 4.6 GB). The whole book is thousands, and known.rs says the title creates 84,000; the ones a session draws with are some 2,000 - so a full warm of the drawn ones is 15-20 GB on the numbers above, which is the saturation this issue reports, and it is why play-nvidia.ps1 gives up on a machine that cannot hold it.
  2. Fewer processes alone may not bound it. At 24 pipelines, 4 and 16 processes peak at 518 and 760 MB, so that part is in flight; but at 140 pipelines four processes already reach 1.5 GB, which is more than four compilers hold at once - the peak follows the count, not only the concurrency. So the lever that bounds memory is --limit: make a piece, let it end, start the next. warm already takes it; nothing in the launcher uses it in a loop.

What I could not do: run the whole book. It needs many minutes and the peaks above say why I would not do it on a 16 GB machine deliberately.

What I would suggest, then: --limit chunked in the launcher (or in warm itself, restarting its children every N pipelines) with a ceiling taken from the machine's free memory, rather than cores * 2 / 3 processes and hope. The per-pipeline figure above (about 10 MB) is enough to size the chunk: free memory minus 2 GB, divided by 10 MB.

One correction to my earlier comment in this issue: I measured shaders first and said the cache build was not the hog - that part stands (2.3 GB at 16 workers), but warm is, and these are its numbers.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7349921290 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3041#issuecomment-7349921290). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T15:12:19Z. --- **Measured `warm` itself, on this machine's own book (48 MB, `known-pipelines.bin` from a real run) with the compiler children counted** - peak of the parent plus every `leonida*.exe` child, polled every 200 ms: | pipelines made | `--jobs` | time | peak (parent + children) | | --- | --- | --- | --- | | 24 | 4 | 6.5 s | 518 MB | | 24 | 8 | 8.4 s | 765 MB | | 24 | 16 | 6.0 s | 760 MB | | **140** | **4** | 4.4 s | **1,514 MB** | | **540** | **16** | 52.9 s | **4,578 MB** | **Two things follow, and the second one changes what the fix should be:** 1. **It grows with how many pipelines are made, roughly 8-11 MB each** (140 at 1.5 GB, 540 at 4.6 GB). **The whole book is thousands**, and `known.rs` says the title creates **84,000**; **the ones a session draws with are some 2,000** - so a full `warm` of the drawn ones is **15-20 GB** on the numbers above, which is the saturation this issue reports, and it is why `play-nvidia.ps1` gives up on a machine that cannot hold it. 2. **Fewer processes alone may not bound it.** At 24 pipelines, 4 and 16 processes peak at 518 and 760 MB, so that part is in flight; but at 140 pipelines **four** processes already reach 1.5 GB, which is more than four compilers hold at once - **the peak follows the count, not only the concurrency.** **So the lever that bounds memory is `--limit`: make a piece, let it end, start the next.** `warm` already takes it; nothing in the launcher uses it in a loop. **What I could not do**: run the whole book. It needs many minutes and the peaks above say why I would not do it on a 16 GB machine deliberately. **What I would suggest, then**: **`--limit` chunked in the launcher (or in `warm` itself, restarting its children every N pipelines) with a ceiling taken from the machine's free memory**, rather than `cores * 2 / 3` processes and hope. **The per-pipeline figure above (about 10 MB) is enough to size the chunk**: free memory minus 2 GB, divided by 10 MB. **One correction to my earlier comment in this issue**: I measured `shaders` first and said the cache build was not the hog - **that part stands** (2.3 GB at 16 workers), **but `warm` is, and these are its numbers.** **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
Sign in to join this conversation.
No labels
No milestone
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.

Reference
ProjectLeonida/mandy#3041
No description provided.