Skip to content

A discovery module skips an import discovered already; its globals are put back - #773

Merged
ASDAlexander77 merged 1 commit into
mainfrom
import-codegen-clone
Nov 6, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
import-codegen-clone

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

After #772 every import is parsed once, but it is still generated 211 times for powder_sandbox's checks.ts (0.67 s of codegen, where once each would be about 0.07 s). theModule receives each import only once, because emittedFiles guards it. Nearly all the other generations go into discovery modules, which are thrown away when discovery ends.

Fix (MLIRGenModule.cpp, MLIRGenImpl.h, MLIRGenVariables.cpp)

In a discovery module (discoveryDepth > 0), an import that has already been discovered (discoveredImports) is no longer generated.

  • Why it needs more than a skip: what discovery resolves against an import is in the compile's maps, except fullNameGlobalsMap, which is scoped to each module. Generating an import registers its globals there, including each class's .rtti. Without them, testing a C | null for truth (p && p.read()) failed with "Class member '.rtti' can't be found". powder_sandbox's simulation.ts hit this.
  • Record: the first time a file is generated (an import, or a file one references), it records the globals it registered and the files it reached, in importUnits.
  • Replay: when an import is skipped, its globals are put back into the discovery module's scope, together with those of every file it reached.
    • A file this module has already emitted is present along with its globals.
    • A referenced .d.ts that has not been recorded is generated, and recorded, right then. A module's references are emitted by its discovery's dummy run, so an import nested inside it never generates them.
  • Unchanged:
    • An import that reached a library (mlirGenImportSharedLib, libraryHoldingModule, a library's __decls) or an import cycle is generated every time, as before.
    • theModule still generates every import.

Results

main (#772) this PR
checks.ts --di -O0 -mm=gc 2.68 s 2.10 s
checks.ts -O3 -mm=rc 3.29 s 2.60 s
powder_sandbox_nri.ts --di -O0 -mm=gc 2.45 s 1.59 s
simulation.ts --di -O0 -mm=gc 1.64 s 1.21 s

The LLVM IR of checks, powder_sandbox_nri and simulation is identical to main's, after normalizing generated numeric ids. Compiled 10 times with each binary, checks.ts gave the same IR every time.

Tests

  • New import-chain-rtti-skipped (compile, -shared, -jit -shared, -jit), in tslang/test/tester/import-chain/rtti_*.ts. The program reads the .rtti of a class two imports down, in a module its discovery skips. With the globals not put back, the compile and -jit variants fail with the error above. The -shared variants reach the module through the library, so it is never skipped there.
  • Full ctest -C Release on Windows: 6655/6655 passed.

Next

In discovery, nri_interop.d.ts, which most powder_sandbox files reference, is still generated about 20 times (0.26 s). Skipping it the same way would be a separate change.

🤖 Generated with Claude Code

…e put back

theModule receives each import once (emittedFiles), but every discovery
module that reached an import generated it again, and was then thrown
away: powder_sandbox's checks.ts generated its imports 211 times, nearly
all of them into discovery modules.

In a discovery module (discoveryDepth > 0) an import whose discovery has
run already (discoveredImports) is no longer generated. What discovery
resolves against it is in the compile's maps, except fullNameGlobalsMap,
which is scoped to each module: generating an import registers its
globals there, a class's .rtti among them, and without it testing a
`C | null` for truth failed with "Class member '.rtti' can't be found".
So the first generation of each file (an import, or a file one
references) records the globals it registered and the files it reached
(importUnits), and a skipped import's are put back, with those of every
file it reached. A file this module emitted already is there with its
globals. A referenced file is first generated here, and recorded: a
module's references are emitted by its discovery's dummy run, so an
import nested in it never generated them. An import that reached a
library or an import cycle is generated each time, as before.

checks.ts --di -O0 -mm=gc: 2.68 s -> 2.10 s; -O3 -mm=rc 3.29 -> 2.60 s;
powder_sandbox_nri 2.45 -> 1.59 s. LLVM IR of checks, powder_sandbox_nri
and simulation is identical to main's.

New test import-chain-rtti-skipped (compile, -shared, -jit -shared, -jit):
the program reads the .rtti of a class two imports down that its
discovery module skips; without the globals put back the compile and JIT
variants fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 96ebeb1 into main Nov 6, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the import-codegen-clone branch November 6, 2026 11:01
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