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();