Repository navigation
JIT cache: compile the program's files at the same time (--jit-jobs) - #1138
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A cold
--emit=jitrun compiled every.tsfile 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
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 indocs/running.md.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 perMLIRContext.LLVMContextinLLVMTypeConverterHelper.his now one per thread.operator[], which inserts on a miss. The scanner's keyword table stored every identifier ever scanned. These reads are now lookups (lookupOrDefault,StringMap::lookup).stows/wstosbuilt astd::wstring_convert, and eachstd::localetakes 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'scodecvt_utf8_utf16on 8M random inputs, malformed ones included (results and throws). The scanner also copied astd::wregexper comment; it now takes a const reference.Numbers (golden_line, cold cache, 24 hardware threads)
--jit-jobs=1--jit-jobs=4--jit-jobs=12(default)--jit-jobs=16Only the program's file stale: 5.6 s. Warm: 0.7 s.
Verification
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::visitpassedforEachChildan array callback, and every array was copied into a newNodeArray<Node>.visitNodes, theNodeArrayconverting constructor and the callbacks were also taken by value. Now there is no array callback and those are const references.declareFunctionVarswalked every earlier statement again for eachlet/const/var, so n declarations meant n² walks. The names are now gathered once as the loop moves on.debug(cond, message)built astd::wstringfrom its literal on every call, including incomputeLineOfPosition, which every source location goes through.golden_line.tsMLIR 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