From 73d4566329a00a708a8a9484c5eb32bfb116d7d9 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 6 Nov 2026 09:01:21 +0000 Subject: [PATCH] A source import is parsed once per compile, not each time a module generates it mlirGenInclude generates an import into every module that needs it (each discovery module, then theModule) and read and parsed the file again each time. powder_sandbox's checks.ts parsed its imports 211 times: 1.97 s of a 3.96 s compile (nri.ts alone 20 times). loadImportFile keeps the parsed import and the files it references in parsedImports, by canonical path. A reused tree has every node's `processed` flag cleared first, as a fresh parse has it; what else MLIRGen writes into the tree already had to hold for discovery followed by codegen of one parse. An import on the generation stack is never reloaded (filesInProgress), so no pass is still using the nodes. checks.ts --di -O0 -mm=gc: 4.10 s / 357 MB -> 2.63 s / 202 MB; -O3 -mm=rc: 4.82 s -> 3.25 s. LLVM IR of checks, powder_sandbox_nri and simulation is identical to main's. ctest -C Release: 6651/6651 passed. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenImpl.h | 11 ++++++ tslang/lib/TypeScript/MLIRGenModule.cpp | 47 ++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 793a61b79..c6dc3db23 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -218,6 +218,11 @@ class MLIRGenImpl std::pair> loadSourceBuf(mlir::Location location, const llvm::MemoryBuffer *sourceBuf, bool isMain = false); + // A source import and the files it references, parsed the first time it is imported and + // reused after that (parsedImports). + std::pair> loadImportFile(mlir::Location location, StringRef fullPath, + StringRef canonicalPath); + // The one name of a file, however it was spelled: `./x.d.ts`, `sub/../x.d.ts` and `x.d.ts` // are the same file. Keys `emittedFiles` and `filesInProgress`. static std::string canonicalFilePath(StringRef filePath); @@ -16855,6 +16860,12 @@ class MLIRGenImpl // Survives discovery, as filesInProgress does. llvm::StringSet<> discoveredImports; + // Source imports parsed once, by canonicalFilePath (loadImportFile): an import is generated + // into every module that needs it - each discovery module, then theModule - and each time it + // was read and parsed again. powder_sandbox's checks.ts parsed its imports 211 times, half of + // its compile. + llvm::StringMap>> parsedImports; + // The default library's files - lib.d.ts and every file it references - by file key. Its // ambient data is defined in its DLL when the program links that (see // isImportedFromSharedDefaultLib). diff --git a/tslang/lib/TypeScript/MLIRGenModule.cpp b/tslang/lib/TypeScript/MLIRGenModule.cpp index 51491c706..223ab231c 100644 --- a/tslang/lib/TypeScript/MLIRGenModule.cpp +++ b/tslang/lib/TypeScript/MLIRGenModule.cpp @@ -282,6 +282,51 @@ namespace mlirgen return {sourceFile, includeFiles}; } + std::pair> MLIRGenImpl::loadImportFile(mlir::Location location, StringRef fullPath, + StringRef canonicalPath) + { + auto cached = parsedImports.find(canonicalPath); + if (cached == parsedImports.end()) + { + auto loaded = loadIncludeFile(location, fullPath); + if (loaded.first) + { + parsedImports[canonicalPath] = loaded; + } + + return loaded; + } + + // As freshly parsed: a pass marks the statements and members it generated `processed`, + // and the next module generates them all again. An import already on the generation stack + // is never reloaded (filesInProgress), so no pass is still using these nodes. The rest + // MLIRGen writes into the tree (accessor methods added to a class, internal flags) is the + // same every time, as it already was for discovery followed by codegen of one parse. + FuncT<> visitNode; + ArrayFuncT<> visitArray; + visitNode = [&](Node node) -> Node { + node->processed = false; + forEachChild(node, visitNode, visitArray); + return undefined; + }; + visitArray = [&](NodeArray array) -> Node { + for (auto node : array) + { + visitNode(node); + } + + return undefined; + }; + + visitNode(cached->second.first.as()); + for (auto includeFile : cached->second.second) + { + visitNode(includeFile.as()); + } + + return cached->second; + } + mlir::LogicalResult MLIRGenImpl::showMessages(SourceFile module, std::vector includeFiles) { mlir::ScopedDiagnosticHandler diagHandler(builder.getContext(), [&](mlir::Diagnostic &diag) { @@ -1421,7 +1466,7 @@ namespace mlirgen exportCheckedDependenciesTypes = savedExportCheckedDependenciesTypes; }); - auto [importSource, importIncludeFiles] = loadIncludeFile(location, fullPath); + auto [importSource, importIncludeFiles] = loadImportFile(location, fullPath, canonicalPath); if (!importSource) { return mlir::failure();