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
4 changes: 2 additions & 2 deletions docs/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
2 changes: 2 additions & 0 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ namespace mlirgen
builder.create<mlir_ts::ThrowOp>(location, message);

MLIRRTTIHelperVC rtti(builder, theModule, compileOptions);
RttiGlobalsScope rttiGlobalsScope(*this);
setCatchCopyThunkBuilder(rtti);
if (!rtti.setRTTIForType(location, message.getType(), [&](StringRef classFullName) {
return getClassInfoByFullName(classFullName);
Expand Down Expand Up @@ -446,6 +447,7 @@ namespace mlirgen
builder.create<mlir_ts::ThrowOp>(location, message);

MLIRRTTIHelperVC rtti(builder, theModule, compileOptions);
RttiGlobalsScope rttiGlobalsScope(*this);
setCatchCopyThunkBuilder(rtti);
if (!rtti.setRTTIForType(location, message.getType(),
[&](StringRef classFullName) { return getClassInfoByFullName(classFullName); }))
Expand Down
100 changes: 93 additions & 7 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir_ts::GlobalOp>(&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<mlir_ts::GlobalOp>(&moduleOp))
lastGlobal = known->second;
// TSLANG_VERIFY_LLVM_GLOBALS_INDEX=<file>: 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<mlir_ts::GlobalOp>(scanned))
{
out << scannedGlobal.getSymName();
}

out << "')\n";
}

lastGlobal = scanned;
}
}
}

if (lastGlobal)
{
builder.setInsertionPointAfter(lastGlobal);
}

SmallVector<mlir::NamedAttribute> attrs;

// add modifiers
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -15487,17 +15538,39 @@ class MLIRGenImpl
~DiscoveryModuleScope()
{
// members restore theModule and the insertion point after the erase
mlirGenImpl.forgetLastGlobalOf(discoveryModule.getOperation());
discoveryModule.erase();
}

private:
MLIRGenImpl &mlirGenImpl;
MLIRValueGuard<mlir::ModuleOp> moduleGuard;
mlir::OpBuilder::InsertionGuard insertGuard;
MLIRValueGuard<llvm::StringSet<>> emittedFilesGuard;
MLIRValueGuard<int> discoveryDepthGuard;
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
Expand Down Expand Up @@ -15546,6 +15619,7 @@ class MLIRGenImpl
tempEntryBlock->erase();

tempFuncOp.erase();
forgetLastGlobalOf(tempModule.getOperation());
tempModule.erase();

tempEntryBlock = nullptr;
Expand Down Expand Up @@ -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<mlir::Operation *, mlir::Operation *> lastGlobalOfModule;

void forgetLastGlobalOf(mlir::Operation *moduleOp)
{
lastGlobalOfModule.erase(moduleOp);
}

mlir::StringRef mainSourceFileName;

mlir::StringRef path;
Expand Down
2 changes: 2 additions & 0 deletions tslang/lib/TypeScript/MLIRGenStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -1792,6 +1793,7 @@ namespace mlirgen
if (!genContext.allowPartialResolve)
{
MLIRRTTIHelperVC rtti(builder, theModule, compileOptions);
RttiGlobalsScope rttiGlobalsScope(*this);
setCatchCopyThunkBuilder(rtti);
if (!rtti.setRTTIForType(
location,
Expand Down
1 change: 1 addition & 0 deletions tslang/lib/TypeScript/MLIRGenVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ namespace mlirgen

if (mlir::succeeded(isGlobalConstLambda(location, variableDeclarationInfo, genContext)))
{
forgetLastGlobalOf(variableDeclarationInfo.globalOp->getParentOp());
variableDeclarationInfo.globalOp->erase();
variableDeclarationInfo.deleted = true;
}
Expand Down
Loading