From f2ceaa1c8818ec5e433e3231f6a8e69d814d3e48 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 15 Nov 2026 15:55:22 +0000 Subject: [PATCH] Compile time: MLIR generation remembers each module's last global 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= checks the remembered global against the scan too: the suite run that way leaves no file. Co-Authored-By: Claude Fable 5.2 --- docs/performance.md | 4 +- tslang/lib/TypeScript/MLIRGenCast.cpp | 2 + tslang/lib/TypeScript/MLIRGenImpl.h | 100 ++++++++++++++++++-- tslang/lib/TypeScript/MLIRGenStatements.cpp | 2 + tslang/lib/TypeScript/MLIRGenVariables.cpp | 1 + 5 files changed, 100 insertions(+), 9 deletions(-) diff --git a/docs/performance.md b/docs/performance.md index 11a6b3abf..a99f616b1 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -128,5 +128,5 @@ Measured on Windows 11 x64 with the release compiler, `--no-default-lib`, on 202 | 2,000 classes of 5 lines | object, `--opt_level=3` | 1.7 s | Functions compile at about 5,500 lines a second, and twice the source takes twice the time. -Classes take a little more than that: from 2,000 to 4,000 classes MLIR generation takes 2.65 -times as long, every other stage about 2.2 times. +So do classes, within a tenth: from 4,000 to 8,000 classes every stage takes 2.0 to 2.5 times as +long, and the whole compilation 2.2 times (5.5 s and 11.9 s). diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index 67ba61a2e..dbdebc1a2 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -394,6 +394,7 @@ namespace mlirgen builder.create(location, message); MLIRRTTIHelperVC rtti(builder, theModule, compileOptions); + RttiGlobalsScope rttiGlobalsScope(*this); setCatchCopyThunkBuilder(rtti); if (!rtti.setRTTIForType(location, message.getType(), [&](StringRef classFullName) { return getClassInfoByFullName(classFullName); @@ -446,6 +447,7 @@ namespace mlirgen builder.create(location, message); MLIRRTTIHelperVC rtti(builder, theModule, compileOptions); + RttiGlobalsScope rttiGlobalsScope(*this); setCatchCopyThunkBuilder(rtti); if (!rtti.setRTTIForType(location, message.getType(), [&](StringRef classFullName) { return getClassInfoByFullName(classFullName); })) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 4a0aa7e58..b2f8ef9df 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -2809,17 +2809,61 @@ class MLIRGenImpl { mlir::OpBuilder::InsertionGuard insertGuard(builder); builder.setInsertionPointToStart(theModule.getBody()); - // after the last global: globals only sit at the top level of the module, and walking - // into every function body for each new global made compile time quadratic - for (auto &moduleOp : llvm::reverse(*theModule.getBody())) + // after the last global: the one remembered for this module (lastGlobalOfModule), or + // found by scanning the top level of the module backwards - globals do not nest + auto scanForLastGlobal = [&]() -> mlir::Operation * { + for (auto &moduleOp : llvm::reverse(*theModule.getBody())) + { + if (isa(&moduleOp)) + { + return &moduleOp; + } + } + + return nullptr; + }; + + mlir::Operation *lastGlobal = nullptr; + auto known = lastGlobalOfModule.find(theModule.getOperation()); + if (known == lastGlobalOfModule.end()) + { + lastGlobal = scanForLastGlobal(); + } + else { - if (auto globalOp = dyn_cast(&moduleOp)) + lastGlobal = known->second; + // TSLANG_VERIFY_LLVM_GLOBALS_INDEX=: checked against the scan, as the LLVM + // globals index is (LowerToLLVM/LLVMGlobalsIndex.h) + static const char *verifyFile = std::getenv("TSLANG_VERIFY_LLVM_GLOBALS_INDEX"); + if (verifyFile) { - builder.setInsertionPointAfter(globalOp); - break; + auto *scanned = scanForLastGlobal(); + if (scanned != lastGlobal) + { + std::error_code ec; + llvm::raw_fd_ostream out(verifyFile, ec, llvm::sys::fs::OF_Append); + if (!ec) + { + out << "MLIR generation: the last global remembered for '" + << variableDeclarationInfo.fullName << "' differs from the scan (scan: '"; + if (auto scannedGlobal = dyn_cast_or_null(scanned)) + { + out << scannedGlobal.getSymName(); + } + + out << "')\n"; + } + + lastGlobal = scanned; + } } } + if (lastGlobal) + { + builder.setInsertionPointAfter(lastGlobal); + } + SmallVector attrs; // add modifiers @@ -2860,6 +2904,13 @@ class MLIRGenImpl if (genContext.dummyRun && genContext.cleanUpOps) { genContext.cleanUpOps->push_back(globalOp); + // erased when the dummy run is cleaned up, which nothing here hears of: not + // remembered, so the next global of the module is placed by a scan + forgetLastGlobalOf(theModule.getOperation()); + } + else + { + lastGlobalOfModule[theModule.getOperation()] = globalOp; } if (variableDeclarationInfo.scope == VariableScope::Global) @@ -15474,7 +15525,7 @@ class MLIRGenImpl // A fresh module holds none of the files emitted so far, so it starts with an empty // `emittedFiles`: discovery has to see every declaration to resolve against it. DiscoveryModuleScope(MLIRGenImpl &mlirGenImpl) - : moduleGuard(mlirGenImpl.theModule), insertGuard(mlirGenImpl.builder), + : mlirGenImpl(mlirGenImpl), moduleGuard(mlirGenImpl.theModule), insertGuard(mlirGenImpl.builder), emittedFilesGuard(mlirGenImpl.emittedFiles, {}), discoveryDepthGuard(mlirGenImpl.discoveryDepth, mlirGenImpl.discoveryDepth + 1) { @@ -15487,10 +15538,12 @@ class MLIRGenImpl ~DiscoveryModuleScope() { // members restore theModule and the insertion point after the erase + mlirGenImpl.forgetLastGlobalOf(discoveryModule.getOperation()); discoveryModule.erase(); } private: + MLIRGenImpl &mlirGenImpl; MLIRValueGuard moduleGuard; mlir::OpBuilder::InsertionGuard insertGuard; MLIRValueGuard> emittedFilesGuard; @@ -15498,6 +15551,26 @@ class MLIRGenImpl mlir::ModuleOp discoveryModule; }; + // Around a use of MLIRRTTIHelperVC, which makes globals of its own (an exception's type + // records) and places them by its own scan: the last global remembered for the module + // (lastGlobalOfModule) is not the last one while it works, nor once it is done. + class RttiGlobalsScope + { + public: + RttiGlobalsScope(MLIRGenImpl &mlirGenImpl) : mlirGenImpl(mlirGenImpl) + { + mlirGenImpl.forgetLastGlobalOf(mlirGenImpl.theModule.getOperation()); + } + + ~RttiGlobalsScope() + { + mlirGenImpl.forgetLastGlobalOf(mlirGenImpl.theModule.getOperation()); + } + + private: + MLIRGenImpl &mlirGenImpl; + }; + // RAII scope that redirects theModule and the builder into the temp module // for speculative evaluation and restores both when it goes out of scope. class TempModuleScope @@ -15546,6 +15619,7 @@ class MLIRGenImpl tempEntryBlock->erase(); tempFuncOp.erase(); + forgetLastGlobalOf(tempModule.getOperation()); tempModule.erase(); tempEntryBlock = nullptr; @@ -17540,6 +17614,18 @@ class MLIRGenImpl /// A "module" matches a TypeScript source file: containing a list of functions. mlir::ModuleOp theModule; + // The last global of each module that createGlobalVariable has put one in (the module being + // generated, the temp module, a discovery module). Every global goes after the last one, so + // the one just created is the last: without this, each new global scanned the module + // backwards over every function generated so far. An entry is dropped when its global or its + // module is erased; a module with no entry is scanned. + llvm::DenseMap lastGlobalOfModule; + + void forgetLastGlobalOf(mlir::Operation *moduleOp) + { + lastGlobalOfModule.erase(moduleOp); + } + mlir::StringRef mainSourceFileName; mlir::StringRef path; diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index a0d97d4ba..5e3d21c9e 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -1661,6 +1661,7 @@ namespace mlirgen if (!genContext.allowPartialResolve) { MLIRRTTIHelperVC rtti(builder, theModule, compileOptions); + RttiGlobalsScope rttiGlobalsScope(*this); setCatchCopyThunkBuilder(rtti); if (!rtti.setRTTIForType( location, exception.getType(), @@ -1792,6 +1793,7 @@ namespace mlirgen if (!genContext.allowPartialResolve) { MLIRRTTIHelperVC rtti(builder, theModule, compileOptions); + RttiGlobalsScope rttiGlobalsScope(*this); setCatchCopyThunkBuilder(rtti); if (!rtti.setRTTIForType( location, diff --git a/tslang/lib/TypeScript/MLIRGenVariables.cpp b/tslang/lib/TypeScript/MLIRGenVariables.cpp index f8797ce41..701b26980 100644 --- a/tslang/lib/TypeScript/MLIRGenVariables.cpp +++ b/tslang/lib/TypeScript/MLIRGenVariables.cpp @@ -305,6 +305,7 @@ namespace mlirgen if (mlir::succeeded(isGlobalConstLambda(location, variableDeclarationInfo, genContext))) { + forgetLastGlobalOf(variableDeclarationInfo.globalOp->getParentOp()); variableDeclarationInfo.globalOp->erase(); variableDeclarationInfo.deleted = true; }