Skip to content

Compile time: generics, interfaces, inheritance and enums (2,000 generic instances: 16.4 s -> 6-7 s) - #865

Merged
ASDAlexander77 merged 5 commits into
mainfrom
perf-compile-time-4
Nov 15, 2026
Merged

ASDAlexander77 merged 5 commits into
mainfrom
perf-compile-time-4

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

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:

Shape Before After
generics (an instance of a generic class per type) 16.4 s 6.0-7.0 s
interfaces (an interface, a class implementing it, a use) 10.9 s 7.3-8.7 s
inheritance (classes overriding a base method) 3.3 s 2.4 s
an enum of 2,000 members and a switch over it 1.8 s 0.3 s
one function of 2,000 statements 1.7 s 1.7 s

The fixes, one commit each except the last two

Stack-escape facts look functions up in a table. StackEscapeFacts asked module.lookupSymbol for 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.SymbolCallInternal to LLVM looked its callee up by name 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. 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_gctors there?" for every global constructor; the answer is nearly always no, and the lookup scanned the whole module to give it. LLVMGlobalsIndex now 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 of cf.assert declares puts. Lowering 2,000 interfaces went from 2.0 s to 1.4 s.

Benchmarks and notes. tslang/benchmarks/compile-time has the script that generates the shapes and times them (shapes.ps1, with -Stages for the per-stage times) and a stack sampler for the Release compiler (profile.ps1, sampler.cpp). docs/compile-time-work.md records 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:

  • Release suite on Windows x64 with TSLANG_VERIFY_LLVM_GLOBALS_INDEX set, which checks every table answer against the scan it replaces: no difference logged.
  • Release suite without it: 7,445 tests, 5 failures, all test-borrow-facts-*. As before they fail on my machine only (the build cache's CMAKE_AR names an MSVC toolset that is no longer installed) and pass when rerun with the installed lib.exe.
  • LLVM IR from the compiler before Compile time: no module scan for every new global (2,000 classes: 100 s -> 3 s) #862 and from this branch, for the 150 largest tests, the ray tracer benchmark and a generated file, under default options, --opt_level=3 and -mm=rc: 450 of 453 identical. The other three are 00param_assigned_owned.ts, whose output already differs from run to run.

Not covered

  • Not built or tested on Linux, and not run with a Debug-built compiler.
  • No test is added: the changes are in compile time only.
  • Still growing faster than the source:
    • interfaces, in MLIR generation (0.9 s to 3.2 s for twice as many): lookups by name for the method stubs of a null vtable;
    • one very large function: lowering an if splits its block, and under the conversion rewriter a split moves the rest of the block one operation at a time.

🤖 Generated with Claude Code

ASDAlexander77 and others added 5 commits November 15, 2026 16:51
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>
@ASDAlexander77 ASDAlexander77 changed the title Compile time: generics, inheritance and enums (2,000 generic instances: 16.4 s -> 7.0 s) Compile time: generics, interfaces, inheritance and enums (2,000 generic instances: 16.4 s -> 6-7 s) Nov 15, 2026
@ASDAlexander77
ASDAlexander77 merged commit 31d477f into main Nov 15, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the perf-compile-time-4 branch November 15, 2026 18:03
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