Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ class MLIRGenImpl

std::pair<SourceFile, std::vector<SourceFile>> 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<SourceFile, std::vector<SourceFile>> 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);
Expand Down Expand Up @@ -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<std::pair<SourceFile, std::vector<SourceFile>>> 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).
Expand Down
47 changes: 46 additions & 1 deletion tslang/lib/TypeScript/MLIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,51 @@ namespace mlirgen
return {sourceFile, includeFiles};
}

std::pair<SourceFile, std::vector<SourceFile>> 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<Node> array) -> Node {
for (auto node : array)
{
visitNode(node);
}

return undefined;
};

visitNode(cached->second.first.as<Node>());
for (auto includeFile : cached->second.second)
{
visitNode(includeFile.as<Node>());
}

return cached->second;
}

mlir::LogicalResult MLIRGenImpl::showMessages(SourceFile module, std::vector<SourceFile> includeFiles)
{
mlir::ScopedDiagnosticHandler diagHandler(builder.getContext(), [&](mlir::Diagnostic &diag) {
Expand Down Expand Up @@ -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();
Expand Down
Loading