Skip to content

Compile time: MLIR generation remembers each module's last global (8,000 classes: 7.2 s -> 2.4 s) - #864

Merged
ASDAlexander77 merged 1 commit into
mainfrom
perf-compile-time-3
Nov 15, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
perf-compile-time-3

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Follow-up to #862 and #863: the last stage of the compiler that grew faster than the source.

What

Every global goes after the module's last one, and createGlobalVariable found that one by scanning the module backwards over every function generated so far. MLIR generation now remembers the global it created last, for each module it creates globals in (the module being generated, the temp module, a discovery module), in MLIRGenImpl::lastGlobalOfModule.

Stage times for an object file, no optimisation, --no-default-lib, from --mlir-timing (Windows x64, Release compiler; a class is two fields, a constructor and one method):

Classes MLIR generation before MLIR generation after Whole compile after
2,000 0.64 s 0.55 s 2.4 s
4,000 1.73 s 1.13 s 5.5 s
8,000 7.15 s 2.36 s 11.9 s

With this every stage roughly doubles when the source doubles.

Keeping the remembered global true

The entry of a module is dropped wherever the answer can change without createGlobalVariable knowing; a module with no entry is scanned as before, so globals are placed where they were.

  • A global erased: the one of a constant lambda (MLIRGenVariables.cpp), and a dummy run's globals, which are erased when the run is cleaned up (those are never remembered).
  • A temp or discovery module erased.
  • Around MLIRRTTIHelperVC (RttiGlobalsScope, at its four uses): it makes an exception's type records itself and places them by its own scan.

The last case was found by the check below: my first version did not have it, the check logged 765 differences across the suite, and the LLVM IR of several tests changed.

Checking mode

TSLANG_VERIFY_LLVM_GLOBALS_INDEX=<file>, added in #862 for the LLVM globals index, now also compares the remembered global with the scan it replaces, appends any difference to the file and uses the scan's answer.

Testing

  • Release suite on Windows x64 with the check on: 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.

docs/performance.md: the "Compile time" paragraph has the new figures.

Not covered

  • Not built or tested on Linux, and not run with a Debug-built compiler.
  • No test is added: the change is in compile time only.

🤖 Generated with Claude Code

Every global goes after the module's last one, which createGlobalVariable found
by scanning the module backwards over every function generated so far. It now
remembers the global it created last, for each module it creates globals in
(the module being generated, the temp module, a discovery module).

The entry is dropped wherever the answer can change behind its back: a global
erased (a const lambda's, a dummy run's), a temp or discovery module erased,
and around MLIRRTTIHelperVC, which makes an exception's type records itself. A
module with no entry is scanned as before, so the globals are placed where
they were.

MLIR generation of 8,000 small classes takes 2.4 s instead of 7.2 s, and now
doubles when the source doubles.

TSLANG_VERIFY_LLVM_GLOBALS_INDEX=<file> checks the remembered global against
the scan too: the suite run that way leaves no file.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 492868a into main Nov 15, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the perf-compile-time-3 branch November 15, 2026 16:32
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