Skip to content

Compile time: no module scan for every new global (2,000 classes: 100 s -> 3 s) - #862

Merged
ASDAlexander77 merged 2 commits into
mainfrom
perf-global-insert-scan
Nov 15, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
perf-global-insert-scan

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

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.

File (object, no optimisation, --no-default-lib) Before After
1,000 classes 10.6 s 1.3 s
2,000 classes 99.9 s 3.1 s
2,000 classes, --opt_level=3 16.4 s 1.8 s
1,000 classes, -mm=rc 8.8 s 1.6 s
1,000 classes, -mm=own 8.9 s 1.6 s
2,000 functions 1.8 s 1.8 s

A class is two fields, a constructor and one method. Measured on Windows x64 with the Release compiler.

Why it was slow

  1. Every new global walked the whole module. To find the last global to insert after, MLIR generation and the lowering to LLVM walked every operation of the module, function bodies included, once per global (a vtable, an RTTI record, a class's size field, a string literal). That was about 77% of the compile of 1,000 classes.
  2. Every new global scanned the module's top-level operations twice more in the lowering to LLVM: a symbol lookup ("is it there yet?", nearly always a miss) and a scan for the last global. With the first cause gone this was about 40% of the compile of 2,000 classes.

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.

  • The helpers reach it through CompileOptions::llvmGlobalsIndex, set only while the pass converts. Outside that, they scan as before.
  • Every LLVM global made during the conversion goes through LLVMCodeHelperBase::createGlobal, which reports it. Only that wrapper and the two sites that run after the conversion still call create<LLVM::GlobalOp>.
  • The conversion driver erases what a failed pattern created. The index is the conversion's listener (notifyPatternEnd arrives 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.
  • Where a new global stands among the others is read from the operation before it, not asked of the block's order.

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

  • Release suite on Windows x64: 7,445 tests, 5 failures, all test-borrow-facts-*. They fail on my machine only: the build cache's CMAKE_AR points at an MSVC toolset that is no longer installed. Rerun with the installed lib.exe, all five pass.
  • The same suite with the checking mode on: no difference logged.
  • LLVM IR from the compiler before and after, for the 150 largest tests, the ray tracer benchmark and a generated file, each 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 with the compiler before this change (the same lines in a different order).

Not covered

  • Not built or tested on Linux, and not run with a Debug-built compiler. CI is the first Linux build of this branch.
  • Compile time still grows somewhat faster than the source (4,000 classes: 8 s without optimisation, 4.6 s with). The earlier profiles point at MLIR generation, which the index does not cover.
  • No test is added: the change is in compile time only, and the suite has no timing tests.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 15, 2026 14:01
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>
@ASDAlexander77
ASDAlexander77 merged commit 2bfad20 into main Nov 15, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the perf-global-insert-scan branch November 15, 2026 14:49
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>
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