diff --git a/docs/compile-time-work.md b/docs/compile-time-work.md index 4317dd378..569797ac4 100644 --- a/docs/compile-time-work.md +++ b/docs/compile-time-work.md @@ -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: @@ -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 | @@ -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(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 @@ -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=`** 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. @@ -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 diff --git a/tslang/benchmarks/compile-time/shapes.ps1 b/tslang/benchmarks/compile-time/shapes.ps1 index 729bc71a7..11ff4cfb4 100644 --- a/tslang/benchmarks/compile-time/shapes.ps1 +++ b/tslang/benchmarks/compile-time/shapes.ps1 @@ -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 @@ -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 diff --git a/tslang/include/TypeScript/TypeScriptPassContext.h b/tslang/include/TypeScript/TypeScriptPassContext.h index f63deca4e..42814e985 100644 --- a/tslang/include/TypeScript/TypeScriptPassContext.h +++ b/tslang/include/TypeScript/TypeScriptPassContext.h @@ -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> 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 splitAhead = llvm::DenseMap(); }; // A reference through an interface's field whose slot may name a cell of accessors (INTERFACE_ACCESSOR_CELL_TAG) diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index f88e91b26..365831a7f 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -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 blocks; + for (auto &block : function.getBody()) + { + blocks.push_back(&block); + } + + OpBuilder builder(function.getContext()); + for (auto *block : blocks) + { + SmallVector splitting; + for (auto &op : *block) + { + if (isa(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(op->getLoc(), continuation); + } + } +} + struct EntryOpLowering : public TsPattern { using TsPattern::TsPattern; @@ -124,7 +195,7 @@ struct ReturnOpLowering : public TsPattern auto *opBlock = rewriter.getInsertionBlock(); auto opPosition = rewriter.getInsertionPoint(); - auto *continuationBlock = rewriter.splitBlock(opBlock, opPosition); + auto *continuationBlock = splitBlockOfSucceedingPattern(rewriter, tsFuncContext, opBlock, opPosition); rewriter.setInsertionPointToEnd(opBlock); @@ -163,7 +234,7 @@ struct ReturnValOpLowering : public TsPattern // 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); @@ -406,7 +477,7 @@ struct IfOpLowering : public TsPattern // 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) { @@ -544,7 +615,7 @@ struct WhileOpLowering : public TsPattern // 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(); @@ -604,7 +675,7 @@ struct DoWhileOpLowering : public TsPattern // 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(); @@ -659,7 +730,7 @@ struct ForOpLowering : public TsPattern // 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(); @@ -733,7 +804,7 @@ struct LabelOpLowering : public TsPattern 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(); @@ -843,7 +914,7 @@ struct SwitchOpLowering : public TsPattern // 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(); @@ -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. diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index 5f580eca7..bcf9ee287 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -867,7 +867,7 @@ namespace mlirgen StringRef funcName = funcEntry.name; auto effectiveFuncType = funcEntry.funcType; - if (auto funcOp = theModule.lookupSymbol(funcName)) + if (auto funcOp = lookupModuleSymbol(funcName)) { if (!funcOp.getBody().empty()) { @@ -989,7 +989,7 @@ namespace mlirgen StringRef funcName = funcEntry.name; auto effectiveFuncType = funcEntry.funcType; - if (auto funcOp = theModule.lookupSymbol(funcName)) + if (auto funcOp = lookupModuleSymbol(funcName)) { if (!funcOp.getBody().empty()) { @@ -1234,7 +1234,7 @@ namespace mlirgen return symbolRef(); } - if (auto funcOp = theModule.lookupSymbol(funcName); funcOp && !funcOp.getBody().empty()) + if (auto funcOp = lookupModuleSymbol(funcName); funcOp && !funcOp.getBody().empty()) { return symbolRef(); } diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index dbdebc1a2..0e2278443 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -393,12 +393,7 @@ namespace mlirgen getStringAttr("Can't cast from interface")); 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); - })) + if (!mlirGenRttiForType(location, message.getType())) { emitError(location, "Not supported type in throw"); return ValueOrLogicalResult(mlir::failure()); @@ -446,11 +441,7 @@ namespace mlirgen "no captured variables and no 'this'")); 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); })) + if (!mlirGenRttiForType(location, message.getType())) { emitError(location, "Not supported type in throw"); return mlir::failure(); @@ -2440,7 +2431,7 @@ namespace mlirgen auto calleeName = symbolRefOp.getIdentifier(); auto thunkName = (calleeName + ".args" + std::to_string(resInputs.size())).str(); auto thunkType = mlir_ts::FunctionType::get(builder.getContext(), resInputs, resFuncType.getResults(), false); - if (!theModule.lookupSymbol(thunkName)) + if (!lookupModuleSymbol(thunkName)) { mlir::OpBuilder::InsertionGuard guard(builder); @@ -2491,7 +2482,7 @@ namespace mlirgen builder.create(noLocation, hasResult ? entryOp.getReference() : mlir::Value()); - theModule.push_back(funcOp); + pushFunction(funcOp); } if (boundOp) @@ -2573,7 +2564,7 @@ namespace mlirgen llvm::raw_string_ostream typesStream(typesText); typesStream << methodType << "|" << thunkType; auto thunkName = ".bound_adapter." + llvm::utohexstr(llvm::xxh3_64bits(typesText)); - if (!theModule.lookupSymbol(thunkName)) + if (!lookupModuleSymbol(thunkName)) { mlir::OpBuilder::InsertionGuard guard(builder); @@ -2647,7 +2638,7 @@ namespace mlirgen builder.create(noLocation, hasResult ? entryOp.getReference() : mlir::Value()); - theModule.push_back(funcOp); + pushFunction(funcOp); } // the box is the adapter's own, as a closure's capture box is (resolveFunctionWithCapture): it holds a diff --git a/tslang/lib/TypeScript/MLIRGenClasses.cpp b/tslang/lib/TypeScript/MLIRGenClasses.cpp index 06835d9d4..c6a25b839 100644 --- a/tslang/lib/TypeScript/MLIRGenClasses.cpp +++ b/tslang/lib/TypeScript/MLIRGenClasses.cpp @@ -3413,7 +3413,7 @@ genContext); return mlir::Value(); } - if (!theModule.lookupSymbol(thunkName)) + if (!lookupModuleSymbol(thunkName)) { mlir::OpBuilder::InsertionGuard guard(builder); @@ -3481,7 +3481,7 @@ genContext); builder.create(noLocation, hasResult ? entryOp.getReference() : mlir::Value()); - theModule.push_back(funcOp); + pushFunction(funcOp); } return builder.create(location, funcType, @@ -3499,7 +3499,7 @@ genContext); const GenContext &genContext) { auto thunkName = (calleeName + ".slot" + std::to_string(thunkType.getNumInputs())).str(); - if (!theModule.lookupSymbol(thunkName)) + if (!lookupModuleSymbol(thunkName)) { mlir::OpBuilder::InsertionGuard guard(builder); @@ -3584,7 +3584,7 @@ genContext); builder.create(noLocation, hasResult ? entryOp.getReference() : mlir::Value()); - theModule.push_back(funcOp); + pushFunction(funcOp); } return builder.create(location, thunkType, @@ -3599,7 +3599,7 @@ genContext); // made before, and in this module: a discovery pass makes its own in a module it then drops for (auto &erased : classInfo->erasedGenericMethods) { - if (erased.name == name && theModule.lookupSymbol(erased.funcName)) + if (erased.name == name && lookupModuleSymbol(erased.funcName)) { return {mlir::success(), &erased}; } @@ -4434,7 +4434,7 @@ genContext); if (registeredType && newInterfacePtr->isFinal) { - if (auto globalOp = theModule.lookupSymbol(fullClassInterfaceVTableFieldName)) + if (auto globalOp = lookupModuleSymbol(fullClassInterfaceVTableFieldName)) { globalOp->setAttr(FINAL_INTERFACE_ATTR_NAME, builder.getStringAttr(newInterfacePtr->fullName)); } diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index c8c7cd3dc..6949f6604 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -1278,15 +1278,15 @@ namespace mlirgen auto [fullFunctionName, functionName] = getNameOfFunction(functionLikeDeclarationBaseAST, funcDeclGenContext); auto funcEntry = lookupFunctionMap(functionName); - if (funcEntry && theModule.lookupSymbol(functionName) - || theModule.lookupSymbol(fullFunctionName)) + if (funcEntry && lookupModuleSymbol(functionName) + || lookupModuleSymbol(fullFunctionName)) { // resolve a live op from the module instead of returning a cached handle; // the registered symbol is usually the full name - auto funcOp = theModule.lookupSymbol(functionName); + auto funcOp = lookupModuleSymbol(functionName); if (!funcOp) { - funcOp = theModule.lookupSymbol(fullFunctionName); + funcOp = lookupModuleSymbol(fullFunctionName); } return {mlir::success(), funcOp, functionName, false}; @@ -1424,7 +1424,7 @@ namespace mlirgen if (!funcDeclGenContext.dummyRun) { - theModule.push_back(funcOp); + pushFunction(funcOp); } if (isGenericFunction) @@ -1458,7 +1458,7 @@ namespace mlirgen if (declared && !genContext.dummyRun) { auto globalName = isFullNamespaceName ? funcName.str() : getFullNamespaceName(funcName).str(); - if (auto globalOp = theModule.lookupSymbol(globalName)) + if (auto globalOp = lookupModuleSymbol(globalName)) { for (auto *name : {OWN_DECLARED_KEEPS_ATTR_NAME, OWN_DECLARED_BORROWS_ATTR_NAME, OWN_DECLARED_LENDS_ATTR_NAME, OWN_DECLARED_RECORD_ATTR_NAME, OWN_DECLARED_RETURNS_THIS_ATTR_NAME, OWN_IMPLIED_LENDS_ATTR_NAME, @@ -2225,7 +2225,7 @@ namespace mlirgen const GenContext &genContext, int firstParam, bool isPublic) { - if (theModule.lookupSymbol(fullFuncName)) + if (lookupModuleSymbol(fullFuncName)) { return mlir::success(); } @@ -2307,7 +2307,7 @@ namespace mlirgen } else { - theModule.push_back(funcOp); + pushFunction(funcOp); } if (isPublic) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index b2f8ef9df..7f39f1631 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -2438,7 +2438,7 @@ class MLIRGenImpl auto inferred = definingOp && definingOp->hasAttr(INTEGER_LITERAL_INFERRED_ATTR_NAME); if (auto addressOfOp = dyn_cast_or_null(definingOp)) { - auto globalOp = theModule.lookupSymbol(addressOfOp.getGlobalName()); + auto globalOp = lookupModuleSymbol(addressOfOp.getGlobalName()); inferred = globalOp && globalOp->hasAttr(INTEGER_LITERAL_INFERRED_ATTR_NAME); } @@ -2907,10 +2907,12 @@ class MLIRGenImpl // 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()); + moduleGlobalCreated(globalOp, /*kept=*/false); } else { lastGlobalOfModule[theModule.getOperation()] = globalOp; + moduleGlobalCreated(globalOp, /*kept=*/true); } if (variableDeclarationInfo.scope == VariableScope::Global) @@ -4350,6 +4352,7 @@ class MLIRGenImpl mlir::Value value = mlir::Value()); void setCatchCopyThunkBuilder(MLIRRTTIHelperVC &rtti); + bool mlirGenRttiForType(mlir::Location location, mlir::Type type); ValueOrLogicalResult mlirGen(TypeAssertion typeAssertionAST, const GenContext &genContext); @@ -7424,7 +7427,7 @@ class MLIRGenImpl auto setAccessorSymbolValueType = [&](mlir::FlatSymbolRefAttr setAccessor, mlir::Type accessorType) -> mlir::Type { if (setAccessor) { - if (auto funcOp = theModule.lookupSymbol(setAccessor)) + if (auto funcOp = lookupModuleSymbol(setAccessor.getValue())) { return setAccessorValueType(funcOp.getFunctionType(), accessorType); } @@ -8498,7 +8501,7 @@ class MLIRGenImpl // info, we add "_" extra as scanner append "_" in front of "__"; auto funcName = "___range_check"; - if (!theModule.lookupSymbol(funcName)) + if (!lookupModuleSymbol(funcName)) { // see castPrimitiveTypeFromAny: the call site is the location until the call instantiates it MLIRLocationGuard vgLoc(overwriteLoc); @@ -8513,7 +8516,7 @@ class MLIRGenImpl // a copy in each module that checks, its own: the default library has one as well, and two // public ones were a duplicate symbol where nothing optimized one away (a debug build's link) - if (auto funcOp = theModule.lookupSymbol(funcName)) + if (auto funcOp = lookupModuleSymbol(funcName)) { funcOp.setPrivate(); funcOp->setAttr("internal_linkage", builder.getUnitAttr()); @@ -9828,7 +9831,7 @@ class MLIRGenImpl // a defined body qualifies: a bodyless declaration FuncOp would lower // to a plain external symbol reference, which the dynamic import mode // (-shared without an import .lib) cannot link. - if (auto funcOp = theModule.lookupSymbol(funcName)) + if (auto funcOp = lookupModuleSymbol(funcName)) { if (!funcOp.getBody().empty()) { @@ -10749,7 +10752,7 @@ class MLIRGenImpl return false; } - auto funcOp = theModule.lookupSymbol(name); + auto funcOp = lookupModuleSymbol(name); return funcOp && funcOp.getBody().empty(); } @@ -14535,7 +14538,7 @@ class MLIRGenImpl auto funcType = setter ? getFunctionType({getOpaqueType(), fieldType}, {}, false) : getFunctionType({getOpaqueType()}, {fieldType}, false); - if (theModule.lookupSymbol(name)) + if (lookupModuleSymbol(name)) { return funcType; } @@ -15538,7 +15541,7 @@ class MLIRGenImpl ~DiscoveryModuleScope() { // members restore theModule and the insertion point after the erase - mlirGenImpl.forgetLastGlobalOf(discoveryModule.getOperation()); + mlirGenImpl.forgetModule(discoveryModule.getOperation()); discoveryModule.erase(); } @@ -15619,7 +15622,7 @@ class MLIRGenImpl tempEntryBlock->erase(); tempFuncOp.erase(); - forgetLastGlobalOf(tempModule.getOperation()); + forgetModule(tempModule.getOperation()); tempModule.erase(); tempEntryBlock = nullptr; @@ -17626,6 +17629,153 @@ class MLIRGenImpl lastGlobalOfModule.erase(moduleOp); } + // The symbols of each module by name (the module being generated, the temp module, a discovery + // module), so that "is there a symbol of this name?" - asked for every stub, thunk and + // instance of a generic function - is not a scan of the module (mlir::SymbolTable::lookupSymbolIn + // walks it). A name that is not here is not in the module. A name with an operation is that + // operation: a function (pushFunction; one is erased in a single place, forgetModuleSymbol), or + // a global made by a run that keeps its IR. A name with no operation is looked up by the scan: + // a global of a dummy run, which is erased with nothing here hearing of it, and any global the + // module had when the table was made. Made by one scan on the first question about a module; + // dropped when the module is erased (forgetModule). + // TSLANG_VERIFY_LLVM_GLOBALS_INDEX= checks every answer against the scan. + llvm::DenseMap> symbolsOfModule; + // the globals dummy runs made (addresses; some are erased by now) + llvm::DenseSet unkeptGlobals; + + llvm::StringMap &moduleSymbols() + { + auto [it, isNew] = symbolsOfModule.try_emplace(theModule.getOperation()); + if (isNew) + { + for (auto &op : *theModule.getBody()) + { + if (auto funcOp = dyn_cast(&op)) + { + it->second.try_emplace(funcOp.getSymName(), &op); + } + else if (auto globalOp = dyn_cast(&op)) + { + // the first of a name is the one a lookup finds: a function after it is not. + // A dummy run's global may be erased unseen; an address in unkeptGlobals that + // is another global's by now only costs that one a scan + auto [entry, isNew] = it->second.try_emplace(globalOp.getSymName(), unkeptGlobals.contains(&op) ? nullptr : &op); + if (!isNew) + { + entry->second = nullptr; + } + } + } + } + + return it->second; + } + + mlir::Operation *lookupModuleSymbol(mlir::StringRef name) + { + auto &symbols = moduleSymbols(); + mlir::Operation *found = nullptr; + if (auto it = symbols.find(name); it != symbols.end()) + { + if (!it->second) + { + return theModule.lookupSymbol(name); + } + + found = it->second; + } + + static const char *verifyFile = std::getenv("TSLANG_VERIFY_LLVM_GLOBALS_INDEX"); + if (verifyFile) + { + auto *scanned = theModule.lookupSymbol(name); + if (scanned != found) + { + std::error_code ec; + llvm::raw_fd_ostream out(verifyFile, ec, llvm::sys::fs::OF_Append); + if (!ec) + { + out << "MLIR generation: the symbol tabled for '" << name << "' differs from the scan (table: " + << (found ? "found" : "none") << ", scan: " << (scanned ? "found" : "none") << ")\n"; + } + + found = scanned; + } + } + + return found; + } + + template T lookupModuleSymbol(mlir::StringRef name) + { + return dyn_cast_or_null(lookupModuleSymbol(name)); + } + + // every function enters a module here + void pushFunction(mlir_ts::FuncOp funcOp) + { + theModule.push_back(funcOp); + if (auto known = symbolsOfModule.find(theModule.getOperation()); known != symbolsOfModule.end()) + { + auto [it, isNew] = known->second.try_emplace(funcOp.getSymName(), funcOp.getOperation()); + if (!isNew && it->second != funcOp.getOperation()) + { + // two of a name (a global and a function): the scan says which is first, and + // which is left when one of them is erased + it->second = nullptr; + } + } + } + + // a global created in theModule; `kept` - by a run that keeps its IR, so nothing erases it unseen + void moduleGlobalCreated(mlir_ts::GlobalOp globalOp, bool kept) + { + if (kept) + { + unkeptGlobals.erase(globalOp.getOperation()); + } + else + { + unkeptGlobals.insert(globalOp.getOperation()); + } + + if (auto known = symbolsOfModule.find(theModule.getOperation()); known != symbolsOfModule.end()) + { + auto [it, isNew] = known->second.try_emplace(globalOp.getSymName(), kept ? globalOp.getOperation() : nullptr); + if (!isNew && it->second != globalOp.getOperation()) + { + // two of a name: the scan says which is first + it->second = nullptr; + } + } + } + + // before a function or a global that is in a module is erased + void forgetModuleSymbol(mlir::Operation *symbolOp) + { + auto known = symbolsOfModule.find(symbolOp->getParentOp()); + if (known == symbolsOfModule.end()) + { + return; + } + + auto name = mlir::SymbolTable::getSymbolName(symbolOp).getValue(); + if (auto it = known->second.find(name); it != known->second.end() && it->second == symbolOp) + { + known->second.erase(it); + } + } + + void forgetModule(mlir::Operation *moduleOp) + { + forgetLastGlobalOf(moduleOp); + symbolsOfModule.erase(moduleOp); + rttiTypesOfModule.erase(moduleOp); + } + + // the types whose exception records each module has (mlirGenRttiForType) + llvm::DenseMap> rttiTypesOfModule; + mlir::StringRef mainSourceFileName; mlir::StringRef path; diff --git a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp index fb9b6d561..19f118085 100644 --- a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp +++ b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp @@ -476,7 +476,7 @@ namespace mlirgen methodInfo.orderWeight = 0; methodInfo.accessLevel = mlir_ts::AccessLevel::Public; - if (theModule.lookupSymbol(INSTANCEOF_NONE_NAME)) + if (lookupModuleSymbol(INSTANCEOF_NONE_NAME)) { return methodInfo; } @@ -497,7 +497,7 @@ namespace mlirgen builder.create(noLocation, falseValue, entryOp.getReference()); builder.create(noLocation, entryOp.getReference()); - theModule.push_back(funcOp); + pushFunction(funcOp); return methodInfo; } @@ -543,7 +543,7 @@ namespace mlirgen mlir_ts::FunctionType funcType, const GenContext &genContext, StringRef message) { - if (auto existing = theModule.lookupSymbol(stubName)) + if (auto existing = lookupModuleSymbol(stubName)) { // one a discovery run left without its body (GenContext::clean) is made again if (!existing.isDeclaration()) @@ -551,6 +551,7 @@ namespace mlirgen return mlir::success(); } + forgetModuleSymbol(existing); existing->erase(); } @@ -590,7 +591,7 @@ namespace mlirgen // it returns nothing at all: what the ownership passes conclude about the functions that do // (OwnedReturnConsumptionPass) does not wait on it - if (auto stubOp = theModule.lookupSymbol(stubName)) + if (auto stubOp = lookupModuleSymbol(stubName)) { stubOp->setAttr(NULL_INTERFACE_VTABLE_ATTR_NAME, builder.getUnitAttr()); } @@ -717,7 +718,7 @@ namespace mlirgen return mlir::failure(); } - if (auto globalOp = theModule.lookupSymbol(vtableName)) + if (auto globalOp = lookupModuleSymbol(vtableName)) { globalOp->setAttr(NULL_INTERFACE_VTABLE_ATTR_NAME, builder.getUnitAttr()); if (newInterfacePtr->isFinal) diff --git a/tslang/lib/TypeScript/MLIRGenModule.cpp b/tslang/lib/TypeScript/MLIRGenModule.cpp index 02068f182..23a244593 100644 --- a/tslang/lib/TypeScript/MLIRGenModule.cpp +++ b/tslang/lib/TypeScript/MLIRGenModule.cpp @@ -1140,13 +1140,13 @@ namespace mlirgen // A library - a module linked beside the program, or one the JIT cache compiles for an // import - runs its top level from a global constructor: a `main` of its own was a second // one at link time (#447). - if (!compileOptions.generateEntryPoint && hasDeferredStatements && !theModule.lookupSymbol(fullGlobalFuncName)) + if (!compileOptions.generateEntryPoint && hasDeferredStatements && !lookupModuleSymbol(fullGlobalFuncName)) { name = MLIRHelper::getAnonymousName(location, "." MAIN_ENTRY_NAME, ""); fullGlobalFuncName = getFullNamespaceName(name); useGlobalCtor = true; } - else if (theModule.lookupSymbol(fullGlobalFuncName)) + else if (lookupModuleSymbol(fullGlobalFuncName)) { // a user-written `main` already is the entry point, so with nothing deferred to run // ahead of it there is nothing left to generate diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index 5e3d21c9e..27a940cea 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -1562,7 +1562,7 @@ namespace mlirgen mlir::LogicalResult MLIRGenImpl::mlirGenCatchCopyThunk(mlir::Location location, StringRef name, mlir::Type source, mlir::Type target) { - if (theModule.lookupSymbol(name)) + if (lookupModuleSymbol(name)) { return mlir::success(); } @@ -1627,7 +1627,7 @@ namespace mlirgen builder.create(location, V(result), arguments[0]); builder.create(location, mlir::Value()); - theModule.push_back(funcOp); + pushFunction(funcOp); return mlir::success(); } @@ -1638,6 +1638,69 @@ namespace mlirgen }); } + // The records an exception of `type` is thrown and caught by (MLIRRTTIHelperVC::setRTTIForType): + // globals of the module, and the copy thunks they name. They are made once for a type and a + // module - the helper makes none that is there already - so a type they were made for is not + // handed to the helper again: it would look each of them up by name, and place them by a scan + // of the module, for every `throw`. False when the type can't be thrown. + bool MLIRGenImpl::mlirGenRttiForType(mlir::Location location, mlir::Type type) + { + auto &made = rttiTypesOfModule[theModule.getOperation()]; + auto isMade = made.contains(type); + + // TSLANG_VERIFY_LLVM_GLOBALS_INDEX=: the helper is asked all the same, and must + // add nothing to the module + static const char *verifyFile = std::getenv("TSLANG_VERIFY_LLVM_GLOBALS_INDEX"); + if (isMade && !verifyFile) + { + return true; + } + + auto countTopLevel = [&]() { return std::distance(theModule.getBody()->begin(), theModule.getBody()->end()); }; + auto before = countTopLevel(); + + MLIRRTTIHelperVC rtti(builder, theModule, compileOptions); + RttiGlobalsScope rttiGlobalsScope(*this); + setCatchCopyThunkBuilder(rtti); + auto supported = rtti.setRTTIForType(location, type, [&](StringRef classFullName) { return getClassInfoByFullName(classFullName); }); + auto grew = countTopLevel() != before; + if (grew) + { + // the helper's globals are made with nothing here hearing of them: the table of the + // module's symbols (symbolsOfModule) is made again by a scan. Once per type. + symbolsOfModule.erase(theModule.getOperation()); + } + + if (!supported) + { + if (isMade) + { + std::error_code ec; + llvm::raw_fd_ostream out(verifyFile, ec, llvm::sys::fs::OF_Append); + if (!ec) + { + out << "MLIR generation: the exception records of " << type << " were made, and making them again fails\n"; + } + } + + return false; + } + + if (isMade && grew) + { + std::error_code ec; + llvm::raw_fd_ostream out(verifyFile, ec, llvm::sys::fs::OF_Append); + if (!ec) + { + out << "MLIR generation: the exception records of " << type << " were made, and making them again adds to the module\n"; + } + } + + // the reference may be stale: making a copy thunk can throw, and come back here + rttiTypesOfModule[theModule.getOperation()].insert(type); + return true; + } + mlir::LogicalResult MLIRGenImpl::mlirGen(ThrowStatement throwStatementAST, const GenContext &genContext) { auto location = loc(throwStatementAST); @@ -1660,12 +1723,7 @@ namespace mlirgen if (!genContext.allowPartialResolve) { - MLIRRTTIHelperVC rtti(builder, theModule, compileOptions); - RttiGlobalsScope rttiGlobalsScope(*this); - setCatchCopyThunkBuilder(rtti); - if (!rtti.setRTTIForType( - location, exception.getType(), - [&](StringRef classFullName) { return getClassInfoByFullName(classFullName); })) + if (!mlirGenRttiForType(location, exception.getType())) { emitError(location, "Not supported type in throw"); return mlir::failure(); @@ -1792,13 +1850,7 @@ namespace mlirgen if (!genContext.allowPartialResolve) { - MLIRRTTIHelperVC rtti(builder, theModule, compileOptions); - RttiGlobalsScope rttiGlobalsScope(*this); - setCatchCopyThunkBuilder(rtti); - if (!rtti.setRTTIForType( - location, - varInfo.getType(), - [&](StringRef classFullName) { return getClassInfoByFullName(classFullName); })) + if (!mlirGenRttiForType(location, varInfo.getType())) { emitError(location, "Not supported type in catch"); return mlir::failure(); diff --git a/tslang/lib/TypeScript/MLIRGenVariables.cpp b/tslang/lib/TypeScript/MLIRGenVariables.cpp index 701b26980..f63243860 100644 --- a/tslang/lib/TypeScript/MLIRGenVariables.cpp +++ b/tslang/lib/TypeScript/MLIRGenVariables.cpp @@ -306,6 +306,7 @@ namespace mlirgen if (mlir::succeeded(isGlobalConstLambda(location, variableDeclarationInfo, genContext))) { forgetLastGlobalOf(variableDeclarationInfo.globalOp->getParentOp()); + forgetModuleSymbol(variableDeclarationInfo.globalOp); variableDeclarationInfo.globalOp->erase(); variableDeclarationInfo.deleted = true; }