Skip to content

JIT cache: compile the program's files at the same time (--jit-jobs) - #1138

Merged
ASDAlexander77 merged 4 commits into
mainfrom
jit-parallel-compile
Dec 6, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
jit-parallel-compile

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Dec 6, 2026 •

Copy link
Copy Markdown
Owner

A cold --emit=jit run compiled every .ts file of the program one after another. For native_ts_graphics' golden_line (50 files) that was about 72 s before the program started.

What changes

  • Parallel JIT cache (jitcache.cpp): the files are compiled on a thread pool, each in an MLIR context of its own. The program's file goes first, because its manifest lists every module it reaches; then all the others start together. The units are put back in the order the sequential build gave them. New option --jit-jobs=<n> (default: the hardware threads, at most 12). Documented in docs/running.md.
  • Shared state the threads stepped on:
    • getCompileOptions() was one pointer for the whole process. An op verifier on one thread read another file's options, already freed, which gave bad_alloc and access violations. The options are now kept per MLIRContext.
    • The global LLVMContext in LLVMTypeConverterHelper.h is now one per thread.
    • The generic specialization depth counter is now per thread.
    • Constant tables were read with operator[], which inserts on a miss. The scanner's keyword table stored every identifier ever scanned. These reads are now lookups (lookupOrDefault, StringMap::lookup).
  • Global locale lock: every stows/wstos built a std::wstring_convert, and each std::locale takes the C++ runtime's one global lock, so the threads mostly waited. The UTF-8 ↔ UTF-16 conversion is written out instead. It matches MSVC's codecvt_utf8_utf16 on 8M random inputs, malformed ones included (results and throws). The scanner also copied a std::wregex per comment; it now takes a const reference.
  • An exception while compiling a file is now reported as that file failing. Before, it was lost in the pool's future and the JIT then crashed on a missing object.

Numbers (golden_line, cold cache, 24 hardware threads)

wall to first frame CPU
before ~72 s ~71 s
--jit-jobs=1 57 s 56 s
--jit-jobs=4 21 s 60 s
--jit-jobs=12 (default) 13 s 68 s
--jit-jobs=16 12 s 72 s

Only the program's file stale: 5.6 s. Warm: 0.7 s.

Verification

  • Full ctest (Release): 10594 of 10594 passed.
  • Determinism: 7 cold caches at 1, 1, 12, 12, 16, 16 and 4 jobs gave byte-identical objects for all 50 units.
  • Stress: no crash in more than 20 cold runs at 2 to 16 jobs (before the fixes, about every other run at 2 to 4 jobs crashed).

Second commit: cheaper MLIR generation (same objects)

A profile of golden_line.ts, which imports 48 modules, put most of its MLIR generation in discovering the imports. Discovery dummy-runs every body to infer return types and captured variables. A third of that was AST walks and their copies:

  • VisitorASTBase::visit passed forEachChild an array callback, and every array was copied into a new NodeArray<Node>. visitNodes, the NodeArray converting constructor and the callbacks were also taken by value. Now there is no array callback and those are const references.
  • declareFunctionVars walked every earlier statement again for each let/const/var, so n declarations meant n² walks. The names are now gathered once as the loop moves on.
  • debug(cond, message) built a std::wstring from its literal on every call, including in computeLineOfPosition, which every source location goes through.

golden_line.ts MLIR generation is about 12% shorter. Its object and the other 47 files' are byte-identical to before; only two files whose sources were edited in between differ. ctest: 10594 of 10594 passed.

Left for a follow-up

The program's own file is now the floor, at about 3 s alone. Nearly all of it is discovering the imports, which runs every function and method body of every imported module. Removing that means having importers read each module's declarations-only text instead of its source. That changes what an importer is compiled from, the build order and the cache's invalidation, so it is a separate decision.

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits December 6, 2026 01:40
A cold JIT run compiled every .ts file of the program one after another: 47-50 files of
native_ts_graphics' golden_line took about 72 s before the program started. The files are
independent - each is compiled from its sources alone, its imports only declared - so the JIT
cache now compiles them on a thread pool, each in an MLIR context of its own. The program's file
goes first (its manifest lists every module it reaches); then all of them start together. The
units are then put in the order the sequential build gave them. --jit-jobs=<n> sets how many run
at once (default: the hardware threads, at most 12). golden_line cold: 72 s -> 13 s; the objects
are byte for byte the same with 1, 4, 12 and 16 jobs.

What the threads shared, and why it was a problem:

- getCompileOptions() was one pointer for the process, set by each MLIRGenImpl: an op's verifier
  on one thread read another file's options, already freed (bad_alloc / access violations). The
  options are now kept per MLIRContext.
- LLVMTypeConverterHelper's global LLVMContext is now one per thread.
- The generic specialization depth counter is per thread (a shared one gave false "circular
  initialization" errors).
- Constant tables read with operator[], which inserts what it does not find: the scanner's
  keyword table took every identifier the compiler ever scanned. All are lookups now
  (lookupOrDefault in the parser, StringMap::lookup / find elsewhere).
- Every stows/wstos built a std::wstring_convert, and every std::locale constructed takes the C++
  runtime's single global lock: compiling threads spent most of their time waiting for it. The
  UTF-8 <-> UTF-16 conversion is written out instead, matching MSVC's codecvt_utf8_utf16 on 8
  million random inputs, malformed ones included. This alone makes one-thread compiles faster
  (72 s -> 57 s for golden_line).
- The scanner copied a std::wregex for every comment (appendIfCommentDirective and regex_exec
  took it by value): a const reference now.

An exception thrown while compiling a file is reported as that file failing to compile; the pool
used to keep it in a future nobody read, and the program then crashed loading a missing object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ard references

An import is discovered with dummy runs of its bodies, and a profile of golden_line.ts (which
imports 48 modules) put a third of that time in AST walks and their copies:

- VisitorASTBase::visit gave forEachChild a callback for arrays, and every array it passed was
  copied into a new NodeArray<Node>; visitNodes, the NodeArray converting constructor and
  forEachChild's callbacks were taken by value besides. The walk now has no array callback, and
  those are const references.
- declareFunctionVars walked every statement before each let/const/var of a body again, a body
  of n declarations n * n walks: the names used before (anywhere, and inside a function) are
  gathered once as the loop moves on.
- debug(cond, message) made a std::wstring of its literal on every call, an allocation in
  computeLineOfPosition, which every source location goes through.

golden_line.ts MLIR generation about 12% shorter; the objects are byte for byte the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The written-out UTF-8 <-> UTF-16 conversion threw std::range_error on malformed input, as
std::wstring_convert does, and the Linux build compiles MLIRGen with -fno-exceptions: "exception
handling disabled". Where exceptions are off it terminates now - what the library's throw did
there, out of a frame that cannot unwind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Linux build compiles tslang with -fno-exceptions, where the try/catch around a file's compile
does not build; there is nothing to catch there either.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 73d74c1 into main Dec 6, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the jit-parallel-compile branch December 6, 2026 03:37
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.

1 participant