Repository navigation
Compile time: no module scan for every new global (2,000 classes: 100 s -> 3 s) - #862
Merged
Merged
Conversation
Every new global (a vtable, an RTTI record, a class's size field, a string literal) walked every operation of the module, function bodies included, to find the last global to insert after. Compile time grew with globals x module size: a file of 2,000 small classes took 100 s to compile to an object without optimisation, 16 s with --opt_level=3. The operations looked for (globals, global constructors) only sit at the top level of the module, so the module's body is now scanned backwards and the scan stops at the first match. The same file takes 5.4 s and 2.0 s. The insertion point is the same one, so the emitted IR does not change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Lowering asked two questions for every global it made - is one of this name there yet, and where does the next one go - and answered each by scanning the module's top-level operations. With thousands of globals that was quadratic: a file of 4,000 small classes took 20 s to compile to an object without optimisation. LLVMGlobalsIndex keeps the names and the last global (of any kind, holding a string, holding dense elements) for the one conversion the LLVM lowering pass runs, and gives the answers the scans gave. The helpers reach it through CompileOptions::llvmGlobalsIndex; every LLVM global made during the conversion goes through LLVMCodeHelperBase::createGlobal, which reports it. The index is the conversion's listener and forgets everything when a pattern that made a global fails, since the driver erases what a failed pattern created; the next question rebuilds it with one scan. The same file now takes 8 s; 1,000 classes under -mm=rc go from 8.8 s to 1.6 s. The emitted IR does not change. TSLANG_VERIFY_LLVM_GLOBALS_INDEX=<file> checks every answer against the scan it replaces and appends any difference to the file: the suite run that way leaves no file. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This was referenced Nov 15, 2026
ASDAlexander77
added a commit
that referenced
this pull request
Nov 15, 2026
…s: 7.3 s -> 5.0 s; 16,000 ifs: 21.7 s -> 11.2 s) (#866) * Compile-time notes: where MLIR generation creates and erases symbols, and the plan for a table of them Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Compile time: MLIR generation tables each module's symbols (2,000 interfaces: 7.3 s -> 5.0 s) MLIR generation asked theModule.lookupSymbol "is there a symbol of this name?" for every stub of a null interface's vtable, every thunk and every instance of a generic function, and the lookup walks the module. It now keeps the symbols of each module by name (symbolsOfModule, lookupModuleSymbol): a function is heard of at pushFunction, the one way a function enters a module, and a global at createGlobalVariable. A name that is not in the table is not in the module. A name with no operation in it - a global of a dummy run, which is erased unseen, or two symbols of one name - is still looked up by the scan. An exception's records are made once for a type and a module (mlirGenRttiForType). Every `throw` and typed `catch` handed its type to MLIRRTTIHelperVC, which looked each record up by name and scanned the module for the last global, to make nothing: the records were there. The helper makes its globals unseen, so the symbol table is made again when a call of it added to the module, and the last global of the module stays remembered when it is not called. MLIR generation of 2,000 interfaces took 3.1 s and 3.6 times that for twice as many; it takes 1.0 s and doubles with the source. TSLANG_VERIFY_LLVM_GLOBALS_INDEX checks every answer of the table against the scan, and calls the helper again for a type it was called for: it must add nothing. Release suite on Windows x64 with and without it; LLVM IR unchanged on the 150 largest tests. The Linux helper was read (each record sits behind an "already there" lookup), not run. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Compile time: a block is split in one step (one function of 2,000 ifs: 1.7 s -> 0.9 s) Lowering an `if`, a loop, a `switch`, a label or a return splits the block it is in. Under the conversion driver rewriter.splitBlock moves the rest of the block into the new one operation by operation and records every move, to undo it if the conversion fails: a block of n `if`s moved n^2/2 operations. These patterns do not fail once they have split, so they split with Block::splitBlock, one splice the driver does not hear of. If the conversion fails elsewhere and is undone, the block stays split; the pass has failed and the function is not used - a compile error raised in this pass gives the same message and exit code. The `try` lowering is unchanged. TypeScriptToAffineLoweringTSFuncPass on one function of 2,000 `if`s took 0.89 s and 3.9 times that of 1,000; the whole compile now takes 0.9 s and doubles with the source up to 4,000 (8,000 is 3.0x that, not looked at). shapes.ps1: -Sizes accepts a comma list when run with pwsh -File. Release suite on Windows x64, with and without TSLANG_VERIFY_LLVM_GLOBALS_INDEX; LLVM IR unchanged on the 150 largest tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Compile-time notes: why one function of 8,000 ifs is still slower than twice 4,000 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Compile time: a function's blocks are split before its lowering (16,000 ifs: 21.7 s -> 11.2 s) Block::splitBlock is one splice, but the splice sets the parent of every operation it moves, so lowering n `if`s of one block was still n^2/2 steps: a quarter of the samples for a function of 8,000. The function's own blocks are now split before the conversion starts (splitFunctionBlocksAhead): after each operation whose lowering splits its block, from the last one of a block to the first, so that each split moves only what lies between two of them; each part ends in a branch to the next, since the conversion driver walks blocks in dominance order. The pattern of such an operation erases that branch and takes the block it went to as the block it would have split off. Blocks of nested regions are left as they were generated: the patterns of the operations around them expect that. One function of n `if`s: 4.8 s at 8,000 and 11.2 s at 16,000, from 6.9 s and 21.7 s; TypeScriptToAffineLoweringTSFuncPass grew 7.3x from 4,000 to 8,000 and now grows with the source. Release suite on Windows x64, with and without TSLANG_VERIFY_LLVM_GLOBALS_INDEX; LLVM IR unchanged on the 150 largest tests. One run had an access violation in test-own-no-counting-own_field_chain_let: it happens about once in 300 to 500 runs with the compiler from before #862 too. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <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.
What
Compile time grew much faster than the source once one file declared many classes: a file of 2,000 small classes took 100 s to compile to an object. This fixes the two causes found by profiling the compiler.
--no-default-lib)--opt_level=3-mm=rc-mm=ownA class is two fields, a constructor and one method. Measured on Windows x64 with the Release compiler.
Why it was slow
The change
First commit. The operations looked for (globals, global constructors) only sit at the top level of the module, so the module's body is scanned backwards and the scan stops at the first match. Five files.
Second commit.
LLVMGlobalsIndex(new,LowerToLLVM/LLVMGlobalsIndex.h) keeps the names of the module's LLVM globals and the last one (of any kind, holding a string, holding dense elements) for the one conversion the LLVM lowering pass runs.CompileOptions::llvmGlobalsIndex, set only while the pass converts. Outside that, they scan as before.LLVMCodeHelperBase::createGlobal, which reports it. Only that wrapper and the two sites that run after the conversion still callcreate<LLVM::GlobalOp>.notifyPatternEndarrives when the pattern fails, in MLIR 22.1.8) and forgets everything when a pattern that made a global fails; the next question rebuilds it with one scan.The insertion points and lookups are the same ones, so the emitted IR does not change.
Checking mode
TSLANG_VERIFY_LLVM_GLOBALS_INDEX=<file>checks every answer of the index against the scan it replaces and appends any difference to the file (the scan's answer is then used).Testing
test-borrow-facts-*. They fail on my machine only: the build cache'sCMAKE_ARpoints at an MSVC toolset that is no longer installed. Rerun with the installedlib.exe, all five pass.--opt_level=3and-mm=rc: 450 of 453 identical. The other three are00param_assigned_owned.ts, whose output already differs from run to run with the compiler before this change (the same lines in a different order).Not covered
🤖 Generated with Claude Code