Repository navigation
Compile time: generics, interfaces, inheritance and enums (2,000 generic instances: 16.4 s -> 6-7 s) - #865
Merged
Conversation
StackEscapeFacts asked module.lookupSymbol for the callee of every call and for every function reference, and that lookup scans the module's operations. The facts now table the module's symbols on the first question. They already hold the module's functions by address, so the table assumes nothing new. For a file of 2,000 instances of a generic class the stack promotion pass took 3.4 s and grew 4.8 times for twice the source; it no longer shows among the stages. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Lowering a ts.SymbolCallInternal to LLVM looked its callee up by name, which scans the module, once per call. LLVMGlobalsIndex now tables the symbols the module has before the conversion and finds the first operation of a name from the tabled one: what replaces a symbol during the conversion is created in front of it, and the replaced operation stays in place until the conversion ends, so stepping back over the operations of that name reaches the first. Only those symbols are tabled, since the conversion never erases them early; one made during the conversion is still found by the scan. Lowering 2,000 instances of a generic class to LLVM took 7.4 s and now takes 1.0 s. TSLANG_VERIFY_LLVM_GLOBALS_INDEX checks this lookup against the scan too. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The dictionary an enum's members are read from was made again after each member, so an enum took a time that grew with the square of its members: 1.6 s of MLIR generation for 2,000 of them. It is now made where it can be read - before an initializer that is not a literal, when the declaration ends, and when a member fails (the declaration is tried again against it). 2,000 members take 0.14 s. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…re tabled
Comdat tables. MLIR's verifier and its translation to LLVM IR find a comdat
selector by name for every function and global that has one: they scan the
module for the table, then the table for the selector. With one table for the
module that was a scan of every selector for each of them. The selectors now go
in tables of at most 128 ("__llvm_comdat", "__llvm_comdat_1", ...), kept at the
start of the module. In LLVM IR a comdat is named by its selector alone, so the
IR does not change. Translating 2,000 instances of a generic class to LLVM IR
took 1.8 s and four times that for twice as many; it takes 0.5-0.7 s and grows
with the source.
Symbols made during the conversion. The lowering asks "is this routine there
yet?" for every type (a release routine, a retain routine, a type's share
routine) and "is __mlir_gctors there?" for every global constructor; the answer
is nearly always no, and module.lookupSymbol scanned the whole module to give
it. LLVMGlobalsIndex now tables the symbols the lowering makes (createSymbol,
and createGlobal as before) beside those the module had before the conversion.
A name in neither table is absent from the module only if it is a name tslang's
lowering alone gives a symbol (lookupOwnSymbolAs): MLIR's own patterns make
symbols too - the lowering of cf.assert declares "puts", which the checking
mode caught in a first version - so any other name is still looked up by the
scan when the tables miss it. What a failed pattern created is erased, so the
table of created symbols is made again by a scan after one fails. Lowering
2,000 interfaces to LLVM took 2.0 s and takes 1.4 s.
TSLANG_VERIFY_LLVM_GLOBALS_INDEX checks every symbol lookup against the scan.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
tslang/benchmarks/compile-time: shapes.ps1 generates 14 program shapes at growing sizes and prints the compile time and its growth (or, with -Stages, the time of each compiler stage from --mlir-timing); profile.ps1 samples the call stacks of the Release compiler, relinked with symbols outside the build directory, with sampler.cpp. docs/compile-time-work.md: what was fixed and in which PR, how to measure, what still grows faster than the source and why, the rules the lookup tables rely on and how to verify a change to them. 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.
Follow-up to #862-#864. Those made classes and functions compile in a time proportional to the source; this is from a sweep of twelve other program shapes, generated at 500, 1,000 and 2,000 units and timed with
--mlir-timing. Seven were already linear. This fixes most of what made the other five slow.Object file, no optimisation,
--no-default-lib, Windows x64, Release compiler, n = 2,000. Timings of the larger inputs moved by 10-15% between runs:The fixes, one commit each except the last two
Stack-escape facts look functions up in a table.
StackEscapeFactsaskedmodule.lookupSymbolfor the callee of every call and for every function reference, and that lookup scans the module. It now tables the module's symbols on the first question. On the generics input the stack promotion pass took 3.4 s and no longer shows among the stages.The callee of a call is found without scanning the module. Lowering a
ts.SymbolCallInternalto LLVM looked its callee up by name once per call.LLVMGlobalsIndexnow tables the symbols the module has before the conversion, and finds the first operation of a name from the tabled one: what replaces a symbol during the conversion is created in front of it, and the replaced operation stays in place until the conversion ends. Lowering 2,000 generic instances went from 7.4 s to about 1 s.An enum's members are listed once. The dictionary an enum's members are read from was made again after each member. It is now made where it can be read: before an initializer that is not a literal, when the declaration ends, and when a member fails.
Bounded comdat tables. MLIR's verifier and its translation to LLVM IR find a comdat selector by scanning the module for the table and then the table for the selector, for every function and global that has one. The selectors now go in tables of at most 128 (
__llvm_comdat,__llvm_comdat_1, ...) kept at the start of the module. In LLVM IR a comdat is named by its selector alone, so the IR does not change. Translating 2,000 generic instances went from 1.8 s to 0.5-0.7 s.The symbols the lowering makes are tabled. The lowering asks "is this routine there yet?" for every type and "is
__mlir_gctorsthere?" for every global constructor; the answer is nearly always no, and the lookup scanned the whole module to give it.LLVMGlobalsIndexnow tables what the lowering creates (createSymbol; 17 creations and 14 lookups are routed through it). A name in neither table counts as absent only if it is a name tslang's lowering alone gives a symbol (lookupOwnSymbolAs); any other name the tables miss is still looked up by the scan, because MLIR's own patterns make symbols too. A first version did not make that distinction, and the checking mode caught it: the lowering ofcf.assertdeclaresputs. Lowering 2,000 interfaces went from 2.0 s to 1.4 s.Benchmarks and notes.
tslang/benchmarks/compile-timehas the script that generates the shapes and times them (shapes.ps1, with-Stagesfor the per-stage times) and a stack sampler for the Release compiler (profile.ps1,sampler.cpp).docs/compile-time-work.mdrecords what was fixed, what is left and why, the rules the tables rely on, and how to verify a change to them.Testing
Run on the last commit:
TSLANG_VERIFY_LLVM_GLOBALS_INDEXset, which checks every table answer against the scan it replaces: no difference logged.test-borrow-facts-*. As before they fail on my machine only (the build cache'sCMAKE_ARnames an MSVC toolset that is no longer installed) and pass when rerun with the installedlib.exe.--opt_level=3and-mm=rc: 450 of 453 identical. The other three are00param_assigned_owned.ts, whose output already differs from run to run.Not covered
ifsplits its block, and under the conversion rewriter a split moves the rest of the block one operation at a time.🤖 Generated with Claude Code