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
100 changes: 50 additions & 50 deletions docs/compile-time-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,20 @@ Merged:
| #863 | The comdat table is first in the module. `--mlir-timing` works. |
| #864 | MLIR generation remembers each module's last global (`MLIRGenImpl::lastGlobalOfModule`). |

Open: PR #865, branch `perf-compile-time-4`.
| #865 | `StackEscapeFacts` and `LLVMGlobalsIndex::lookupSymbol` look symbols up in tables; an enum's member dictionary is made once; bounded comdat tables; the symbols the lowering makes are tabled (`createSymbol`, `lookupOwnSymbolAs`: a miss is final only for names the lowering itself invents - the check caught `puts`, which MLIR's `cf.assert` lowering declares); `tslang/benchmarks/compile-time/` and this document. |

| Commit | What |
| --- | --- |
| `8f303db6` | `StackEscapeFacts` looks functions up in a table. |
| `5254b577` | `LLVMGlobalsIndex::lookupSymbol`: the callee of a call is found from a table of the symbols the module has before the conversion. |
| `fea78525` | An enum's member dictionary is made once, not after every member. |

Also on `perf-compile-time-4`, after those three (see `git log`; all went through section 5):

- **Bounded comdat tables** (`addComdatSelector`, `COMDAT_TABLE_SELECTORS` in `LowerToLLVM.cpp`;
`comdatTable` in `LLVMGlobalsIndex.h`). Translation to LLVM IR for 2,000 generic instances:
1.8 s -> 0.5-0.7 s.
- **Symbols the lowering makes are tabled** (`createSymbol`, `lookupOwnSymbolAs`, `createdSymbols`
in `LLVMGlobalsIndex.h`; 14 lookups and 17 creations routed in `OwnershipRoutineLogic.h`,
`ConvertLogic.h`, `LLVMCodeHelperBase.h`, `LLVMCodeHelper.h`, `LowerToLLVM.cpp`). A first version
treated every name missing from the tables as absent from the module; the check caught `puts`,
which MLIR's own `cf.assert` lowering declares. The rule is now: a miss is final only for names
the lowering itself invents (`ownName`), otherwise the scan decides. LLVM lowering of 2,000
interfaces: 2.0 s -> 1.4 s.
- `tslang/benchmarks/compile-time/` (the scripts of section 2) and this document.
Not merged yet, branch `perf-compile-time-5`:

- **MLIR generation tables each module's symbols** (`symbolsOfModule`, `lookupModuleSymbol`,
`pushFunction` in `MLIRGenImpl.h`). Every `theModule.lookupSymbol` of MLIR generation goes through
it; a name that is not in the table is not in the module.
- **An exception's records are made once per type and module** (`mlirGenRttiForType`): a `throw`
of a type already seen no longer has `MLIRRTTIHelperVC` look each record up by name and scan
for the last global.
- MLIR generation of 2,000 interfaces: 3.1 s -> 1.0 s, and it now doubles with the source.
- **A block is split in one step** (`splitBlockOfSucceedingPattern` in `LowerToAffineLoops.cpp`)
by the lowering of `if`, the loops, `switch`, labels and the returns. One function of 2,000
`if`s: 1.7 s -> 0.9 s.

Figures, object file, no optimisation, `--no-default-lib`, Windows x64, Release compiler:

Expand All @@ -47,7 +40,7 @@ Figures, object file, no optimisation, `--no-default-lib`, Windows x64, Release
| 2,000 classes of 5 lines | 99.9 s | 2.5 s |
| 8,000 classes | not measured (4,000: 19.7 s after #862) | 11.9 s |
| 2,000 instances of a generic class | 16.4 s | 6.0-7.0 s |
| 2,000 interfaces with a class and a use each | 10.9 s | 7.3-8.7 s |
| 2,000 interfaces with a class and a use each | 10.9 s | 5.0 s |
| 2,000 classes overriding a base method | 3.3 s | 2.4 s |
| an enum of 2,000 members and a switch | 1.8 s | 0.3 s |
| 2,000 functions of 5 lines | 1.8 s | 1.7 s |
Expand Down Expand Up @@ -76,34 +69,34 @@ The suite's largest test is 1,500 lines and shows none of this. Only generated i

## 3. What is left, with the cause of each

Measured with all of the above, n = 1,000 -> 2,000. Timings of the larger inputs moved by 10-15%
between runs on the development machine, and at n = 2,000 even code emission (LLVM's, linear)
came out at 2.8x on these two shapes, so read 2.5-2.9x here as "about linear, plus memory":

| Shape | Total | Growth | Where |
| --- | --- | --- | --- |
| interfaces | 2.8 -> 8.7 s | 3.1x | MLIR generation 0.9 -> 3.2 s (3.6x) is the one clear case; LLVM lowering 0.46 -> 1.36 s (2.9x); `OwnedReturnConsumptionPass` and `StackPromotionPass` 0.04 -> 0.2 s (4-5x, small) |
| one function of n `if` statements | 0.65 -> 1.7 s | 2.7x | `TypeScriptToAffineLoweringTSFuncPass` 0.23 -> 0.89 s (3.9x) |
| generics | 2.8 -> 7.0 s | 2.5x | nothing stands out; every stage 2.1-2.8x |
| inheritance, classes | | 2.3x | nothing stands out |

**Interfaces, MLIR generation.** `mlirGenNullInterfaceVTable` (`MLIRGenInterfaces.cpp`) makes one
throwing stub per interface method through `mlirGenThrowingMethodStub`, which asks
`theModule.lookupSymbol<FuncOp>(stubName)` before and after: the first is a miss and scans the
module. `INSTANCEOF_NONE_NAME` is looked up for every vtable and the function is pushed at the
*end* of the module, so each lookup scans all of it. `createGlobalVariable` still scans after a
dummy run made a global (the entry of `lastGlobalOfModule` is dropped then). A fix needs a symbol
table for MLIR generation that survives its erases: `funcOp.erase()` in about eight places,
`GenContext::clean` for dummy runs, and the temp and discovery modules. Count the creation and
erase sites of `mlir_ts::FuncOp` first; if they are few, the pattern of `lastGlobalOfModule` (drop
the entry wherever it can go stale, check against the scan) applies.

**One very large function.** The `IfOp` lowering in `LowerToAffineLoops.cpp` calls
`rewriter.splitBlock`. Under a conversion rewriter that moves the rest of the block one operation
at a time, each with a recorded rewrite, so n sequential `if`s move n^2/2 operations. Options, none
tried: lower the `if`s of a block last to first, so each split moves little; or build the
continuation block once per region instead of splitting at every `if`. Upstream's SCF-to-CF has
the same shape.
All 14 shapes of `shapes.ps1` grow 1.7-2.3x from n = 1,000 to 2,000, which is linear: timings of
the larger inputs move by 10-15% between runs on the development machine.

**One very large function** (n `if` statements): 0.86 s at 2,000, 1.8 s at 4,000, 4.8 s at 8,000,
11.2 s at 16,000 (it was 6.9 s and 21.7 s at the last two). The lowering pass is linear now; from
4,000 to 8,000 `TypeScriptToLLVMLoweringPass` and the canonicalizer grow about 3x (0.28 -> 0.82 s
and 0.26 -> 0.79 s). Not looked at.

**Seen while testing, not from this work:** `tslang --emit=llvm --no-default-lib -mm=own
tslang/test/tester/own/own_field_chain_let.ts` ends with an access violation about once in 300 to
500 runs, with the compiler from before #862 as well.
**Block splits under the conversion driver.** `rewriter.splitBlock` moves the rest of the block
one operation at a time and records each move for undo (`RewriterBase::splitBlock` with a
listener; `MoveOperationRewrite` in MLIR's `DialectConversion.cpp`). The patterns that cannot fail
after the split call `Block::splitBlock` instead, which the driver does not hear of: if the
conversion fails and is undone, the block stays split and the function is not valid, but the pass
has failed and nothing uses it (checked: a setter-less property assignment after an `if` gives
the same error and exit code). `TryOpLowering` still uses `rewriter.splitBlock`, as do the
patterns of `LowerToLLVM.cpp`.

Even `Block::splitBlock` sets the parent of every operation it moves, so n splits of one block
were n^2/2 pointer writes (25% of the samples at 8,000 `if`s). So the function's own blocks are
split before the conversion (`splitFunctionBlocksAhead`): after each operation whose lowering
splits, from the last of a block to the first, each part ending in a `cf.br` to the next (the
driver walks blocks in dominance order, so they must be linked). The pattern of such an operation
(`TSFunctionContext::splitAhead`) erases the branch and takes the block it went to as its
continuation. Only the function's own blocks: the patterns of the operations around a nested
region take it as it was generated.

**Not looked at:** `OwnedReturnConsumptionPass` and `BorrowFactsExportPass` showed 4-5x growth on
some shapes but small absolute times (0.2 s); the canonicalizer runs grow 2.3-2.6x; `-mm=rc` and
Expand All @@ -116,7 +109,7 @@ Every table answers a question a scan used to answer, and must give the same ans

- **`TSLANG_VERIFY_LLVM_GLOBALS_INDEX=<file>`** makes the compiler ask the scan too, append any
difference to the file and use the scan's answer. It covers `LLVMGlobalsIndex` (global lookup,
last global, symbol lookup) and `lastGlobalOfModule`. After a change to any of them: run the
last global, symbol lookup), `lastGlobalOfModule`, `symbolsOfModule` and `mlirGenRttiForType`. After a change to any of them: run the
whole suite with it set, and the file must not exist afterwards. It has caught two wrong
versions that the plain suite's IR comparison would also have shown, and one (`puts`) that it
would not have.
Expand All @@ -132,6 +125,13 @@ Every table answers a question a scan used to answer, and must give the same ans
- **In MLIR generation** the remembered global of a module is dropped where a global or a module
is erased, for dummy-run globals, and around `MLIRRTTIHelperVC` (`RttiGlobalsScope`), which
makes globals itself.
- **The table of a module's symbols** (`symbolsOfModule`) hears of a function at `pushFunction`,
the only way one enters a module, and of a global at `createGlobalVariable`. A name with no
operation in it is looked up by the scan: a global of a dummy run (erased unseen), and two
symbols of one name. The globals `MLIRRTTIHelperVC` makes are not heard of, so the table is
dropped, and made again by a scan, when a call of the helper added to the module
(`mlirGenRttiForType`, the one place it is called from). Under the check the helper is still
called for a type it was called for, and must add nothing.
- A cheaper comparison inside a scan does not help: the cost is walking the operation list.

## 5. Verifying a change
Expand Down
3 changes: 2 additions & 1 deletion tslang/benchmarks/compile-time/shapes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# stage of the compiler the growth is in.
param(
[string]$Tslang = "$PSScriptRoot\..\..\..\__build\tslang\windows-msbuild-2026-release\bin\tslang.exe",
[int[]]$Sizes = @(500, 1000, 2000),
[string[]]$Sizes = @('500', '1000', '2000'),
[string[]]$Only = @(),
[string[]]$Flags = @(),
[switch]$Stages
Expand All @@ -21,6 +21,7 @@ $ErrorActionPreference = "Continue"
# `pwsh -File shapes.ps1 -Only a,b` hands the list over as one string
$Only = @($Only | ForEach-Object { $_ -split ',' } | Where-Object { $_ })
$Flags = @($Flags | ForEach-Object { $_ -split ',' } | Where-Object { $_ })
[int[]]$Sizes = @($Sizes | ForEach-Object { $_ -split ',' } | Where-Object { $_ })
$out = Join-Path $PSScriptRoot "out"
New-Item -ItemType Directory -Force $out | Out-Null

Expand Down
5 changes: 5 additions & 0 deletions tslang/include/TypeScript/TypeScriptPassContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ struct TSFunctionContext
// the loads and stores through an interface's field inside a `try`, and the blocks their accessor calls unwind
// to (the innermost try's, which comes last): AccessorUnwindEdgeOps once the function is lowered
mlir::SmallVector<std::pair<Operation *, mlir::Block *>> accessorUnwinds;

// the operations of the function's own blocks that split their block when lowered, and the
// branch put after each before the conversion: what follows the operation is in the block the
// branch goes to already (splitFunctionBlocksAhead in LowerToAffineLoops.cpp)
llvm::DenseMap<Operation *, Operation *> splitAhead = llvm::DenseMap<Operation *, Operation *>();
};

// A reference through an interface's field whose slot may name a cell of accessors (INTERFACE_ACCESSOR_CELL_TAG)
Expand Down
89 changes: 81 additions & 8 deletions tslang/lib/TypeScript/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,77 @@ namespace
// TypeScriptToAffine RewritePatterns
//===----------------------------------------------------------------------===//

// rewriter.splitBlock for a pattern that does not fail once it has split. Under the conversion
// driver the rewriter moves the rest of the block into the new block one operation at a time, and
// records every move so as to undo it (RewriterBase::splitBlock with a listener): a block of n
// `if`s moved n^2/2 operations. This is Block::splitBlock, one splice, which the driver does not
// hear of at all. If the conversion fails and the driver undoes its rewrites, the block stays
// split, with every operation still in the function: the function is not valid then, and is not
// used, as the pass has failed.
//
// Even one splice sets the parent of every operation it moves, so n of them in one block are
// n^2/2 steps still. The function's own blocks are therefore split before the conversion
// (splitFunctionBlocksAhead), and for an operation split there nothing is moved: the block it
// would have split off is the one the branch after it goes to. The operation stays where it is,
// with the branch, until the driver erases them; what the pattern puts at the end of the block
// follows them.
static mlir::Block *splitBlockOfSucceedingPattern(PatternRewriter &rewriter, TSFunctionContext *tsFuncContext, mlir::Block *block,
mlir::Block::iterator before)
{
if (auto *branch = tsFuncContext ? tsFuncContext->splitAhead.lookup(&*before) : nullptr)
{
if (std::next(before) == branch->getIterator() && branch == &block->back())
{
auto *continuation = branch->getSuccessor(0);
rewriter.eraseOp(branch);
return continuation;
}
}

return block->splitBlock(before);
}

// Splits the blocks of the function's body after each operation whose lowering splits its block
// (the callers of splitBlockOfSucceedingPattern), from the last such operation of a block to the
// first, so that each split moves only what is between two of them, and ends each part with a
// branch to the next. Not the blocks of the regions nested in those operations: the patterns of
// the operations around them take such a region as it was generated.
static void splitFunctionBlocksAhead(mlir_ts::FuncOp function, TSFunctionContext &tsFuncContext)
{
SmallVector<mlir::Block *> blocks;
for (auto &block : function.getBody())
{
blocks.push_back(&block);
}

OpBuilder builder(function.getContext());
for (auto *block : blocks)
{
SmallVector<Operation *> splitting;
for (auto &op : *block)
{
if (isa<mlir_ts::IfOp, mlir_ts::WhileOp, mlir_ts::DoWhileOp, mlir_ts::ForOp, mlir_ts::LabelOp, mlir_ts::SwitchOp,
mlir_ts::ReturnOp, mlir_ts::ReturnValOp>(op))
{
splitting.push_back(&op);
}
}

for (auto *op : llvm::reverse(splitting))
{
if (op == &op->getBlock()->back())
{
continue;
}

auto *opBlock = op->getBlock();
auto *continuation = opBlock->splitBlock(std::next(op->getIterator()));
builder.setInsertionPointToEnd(opBlock);
tsFuncContext.splitAhead[op] = builder.create<mlir::cf::BranchOp>(op->getLoc(), continuation);
}
}
}

struct EntryOpLowering : public TsPattern<mlir_ts::EntryOp>
{
using TsPattern<mlir_ts::EntryOp>::TsPattern;
Expand Down Expand Up @@ -124,7 +195,7 @@ struct ReturnOpLowering : public TsPattern<mlir_ts::ReturnOp>

auto *opBlock = rewriter.getInsertionBlock();
auto opPosition = rewriter.getInsertionPoint();
auto *continuationBlock = rewriter.splitBlock(opBlock, opPosition);
auto *continuationBlock = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, opBlock, opPosition);

rewriter.setInsertionPointToEnd(opBlock);

Expand Down Expand Up @@ -163,7 +234,7 @@ struct ReturnValOpLowering : public TsPattern<mlir_ts::ReturnValOp>
// Split block at `assert` operation.
auto *opBlock = rewriter.getInsertionBlock();
auto opPosition = rewriter.getInsertionPoint();
auto *continuationBlock = rewriter.splitBlock(opBlock, opPosition);
auto *continuationBlock = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, opBlock, opPosition);

rewriter.setInsertionPointToEnd(opBlock);

Expand Down Expand Up @@ -406,7 +477,7 @@ struct IfOpLowering : public TsPattern<mlir_ts::IfOp>
// continuation point.
auto *condBlock = rewriter.getInsertionBlock();
auto opPosition = rewriter.getInsertionPoint();
auto *remainingOpsBlock = rewriter.splitBlock(condBlock, opPosition);
auto *remainingOpsBlock = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, condBlock, opPosition);
mlir::Block *continueBlock;
if (ifOp.getNumResults() == 0)
{
Expand Down Expand Up @@ -544,7 +615,7 @@ struct WhileOpLowering : public TsPattern<mlir_ts::WhileOp>

// Split the current block before the WhileOp to create the inlining point.
auto *currentBlock = rewriter.getInsertionBlock();
auto *continuation = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint());
auto *continuation = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, currentBlock, rewriter.getInsertionPoint());

auto *body = &whileOp.getBody().front();
auto *bodyLast = &whileOp.getBody().back();
Expand Down Expand Up @@ -604,7 +675,7 @@ struct DoWhileOpLowering : public TsPattern<mlir_ts::DoWhileOp>
// Split the current block before the WhileOp to create the inlining point.
OpBuilder::InsertionGuard guard(rewriter);
mlir::Block *currentBlock = rewriter.getInsertionBlock();
mlir::Block *continuation = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint());
mlir::Block *continuation = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, currentBlock, rewriter.getInsertionPoint());

// Only the "before" region should be inlined.
auto *body = &doWhileOp.getBody().front();
Expand Down Expand Up @@ -659,7 +730,7 @@ struct ForOpLowering : public TsPattern<mlir_ts::ForOp>

// Split the current block before the WhileOp to create the inlining point.
auto *currentBlock = rewriter.getInsertionBlock();
auto *continuation = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint());
auto *continuation = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, currentBlock, rewriter.getInsertionPoint());

auto *incr = &forOp.getIncr().front();
auto *incrLast = &forOp.getIncr().back();
Expand Down Expand Up @@ -733,7 +804,7 @@ struct LabelOpLowering : public TsPattern<mlir_ts::LabelOp>
Location loc = labelOp.getLoc();

mlir::Block *currentBlock = rewriter.getInsertionBlock();
mlir::Block *continuation = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint());
mlir::Block *continuation = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, currentBlock, rewriter.getInsertionPoint());

auto *begin = &labelOp.getLabelRegion().front();

Expand Down Expand Up @@ -843,7 +914,7 @@ struct SwitchOpLowering : public TsPattern<mlir_ts::SwitchOp>
// Split the current block before the WhileOp to create the inlining point.
OpBuilder::InsertionGuard guard(rewriter);
mlir::Block *currentBlock = rewriter.getInsertionBlock();
mlir::Block *continuation = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint());
mlir::Block *continuation = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, currentBlock, rewriter.getInsertionPoint());

auto *casesRegion = &switchOp.getCasesRegion().front();

Expand Down Expand Up @@ -3237,6 +3308,8 @@ void TypeScriptToAffineLoweringTSFuncPass::runOnFunction()
tsContext.beginRun();
AddTsAffinePatterns(getContext(), target, patterns, tsContext, tsFuncContext);

splitFunctionBlocksAhead(function, tsFuncContext);

// With the target and rewrite patterns defined, we can now attempt the
// conversion. The conversion will signal failure if any of our `illegal`
// operations were not converted successfully.
Expand Down
Loading
Loading