diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index 1a61990bb..1ec305f5b 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -5193,6 +5193,29 @@ namespace mlirgen return mlir::failed(result) ? mlir::failure() : mlir::success(); } + // The calls through a value in the specializations of a helper made from source (`___any_key<...>`, + // `___any_invoke<...>`) are the adapter's (ANY_CALL_ADAPTER_CALL_ATTR_NAME). The helpers are functions of the + // module's own, so only its top-level functions are looked at, and one marked already is passed over: walking + // every op of the module after each `x[k]` or call of an `any` made a function of many of them quadratic + // (binaryArithmeticControlFlowGraphNotTooLarge, 1300 lines: 45 s, near the sweep's time limit) + void MLIRGenImpl::markAnyAdapterCalls(StringRef namePrefix) + { + for (auto funcOp : theModule.getOps()) + { + if (!funcOp.getName().starts_with(namePrefix)) + { + continue; + } + + funcOp.walk([&](mlir_ts::CallIndirectOp callOp) { + if (!callOp->hasAttr(ANY_CALL_ADAPTER_CALL_ATTR_NAME)) + { + callOp->setAttr(ANY_CALL_ADAPTER_CALL_ATTR_NAME, builder.getUnitAttr()); + } + }); + } + } + // `a(x, y)` of `a: any`: `__any_invoke(a, [x, y])`, which calls the adapter of the function `a` // holds (TYPE_DESCR_CALL): the result is `any`, a value that is no function a TypeError ValueOrLogicalResult MLIRGenImpl::mlirGenAnyCall(mlir::Location location, mlir::Value callee, CallExpression callExpression, @@ -5259,14 +5282,7 @@ namespace mlirgen // the one call through a value in `__any_invoke` is the adapter's: it returns what the adapter returns, // a box its caller owns (OwnedReturnConsumptionPass) - counted as a call of nothing known, it leaked - theModule.walk([&](mlir_ts::FuncOp funcOp) { - if (funcOp.getName().starts_with("___any_invoke<")) - { - funcOp.walk([&](mlir_ts::CallIndirectOp callOp) { - callOp->setAttr(ANY_CALL_ADAPTER_CALL_ATTR_NAME, builder.getUnitAttr()); - }); - } - }); + markAnyAdapterCalls("___any_invoke<"); return result; } @@ -5944,14 +5960,7 @@ namespace mlirgen // the one call through a value in `__any_key` is the adapter's: it returns what the adapter returns, a box // its caller owns (OwnedReturnConsumptionPass) - theModule.walk([&](mlir_ts::FuncOp funcOp) { - if (funcOp.getName().starts_with("___any_key<")) - { - funcOp.walk([&](mlir_ts::CallIndirectOp callOp) { - callOp->setAttr(ANY_CALL_ADAPTER_CALL_ATTR_NAME, builder.getUnitAttr()); - }); - } - }); + markAnyAdapterCalls("___any_key<"); if (op == ANY_KEYS_OP_HAS) { diff --git a/tslang/lib/TypeScript/MLIRGenGenerics.cpp b/tslang/lib/TypeScript/MLIRGenGenerics.cpp index 4501f4409..161997e38 100644 --- a/tslang/lib/TypeScript/MLIRGenGenerics.cpp +++ b/tslang/lib/TypeScript/MLIRGenGenerics.cpp @@ -1555,6 +1555,20 @@ namespace mlirgen return {mlir::failure(), mlir_ts::FunctionType(), ""}; } + // A discovery pass (dummyRun) whose module is thrown away needs the instance's type only, and an + // instance made already has it in the function map: its module does not keep the function, so + // mlirGenFunctionLikeDeclaration found none and made it again - for each call. Every `x[k]` of an + // `any` made `___any_key` (and its `___unbox`es) again in each such pass: a function of 1300 + // lines of them took 45 s (binaryArithmeticControlFlowGraphNotTooLarge). Its type arguments given, + // the type is the one the real pass makes the instance with. + if (genericTypeGenContext.dummyRun && typeArguments && typeArguments.size() == typeParams.size()) + { + if (auto funcEntry = lookupFunctionMap(name)) + { + return {mlir::success(), funcEntry.funcType, fullName}; + } + } + // `f` calling `f` would make instances without end if (functionGenericTypeInfo->processing.size() >= MAX_NESTED_GENERIC_INSTANCES) { diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index f909ee997..aa8b44648 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -13388,6 +13388,8 @@ class MLIRGenImpl // A call of a value of type `any` (user ruling 11-18): through the adapter made for the function's // type when it was boxed (TYPE_DESCR_CALL); a TypeError when it holds no function, or one with none + void markAnyAdapterCalls(StringRef namePrefix); + ValueOrLogicalResult mlirGenAnyCall(mlir::Location location, mlir::Value callee, CallExpression callExpression, const GenContext &genContext); diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index 03271674e..5299df884 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -2017,15 +2017,65 @@ class OwnedReturnConsumptionPass mlir::OpBuilder builder(&getContext()); std::set> placedAfterFinally; + + // The ways out of a block from inside its ops' regions (see below), each with the op of the block it is + // in, in the block's order: found once for a block, as they are the block's - not one temporary's. Looked + // for again after each temporary, a function of many of them (1300 lines of `any` arithmetic, + // binaryArithmeticControlFlowGraphNotTooLarge) walked the rest of its block once for each: 2.7 s here. + llvm::DenseMap>> exitsOfBlock; + auto exitsOf = [&](mlir::Block *block) -> llvm::SmallVector> & { + auto found = exitsOfBlock.find(block); + if (found != exitsOfBlock.end()) + { + return found->second; + } + + auto &exits = exitsOfBlock[block]; + for (auto &top : *block) + { + if (top.getNumRegions() == 0) + { + continue; + } + + top.walk([&](mlir::Operation *nested) { + // A nested function's `return` returns from that function, and its jumps are + // its own; nothing inside one is on a path out of this block. + if (mlir::isa(nested)) + { + return mlir::WalkResult::skip(); + } + + // A `throw` leaves as a `return` does - `const s = "x" + i; if (c) throw i;` lost + // `s` on every throw - unless a `try` between it and this block takes it: the + // block may then go on and give the value back at its own end. + auto leavesBlock = mlir::isa(nested) || + (mlir::isa(nested) && + jumpLeavesBlock(nested, block)) || + (mlir::isa(nested) && !throwLandsWithin(nested, block)); + if (leavesBlock) + { + exits.push_back({&top, nested}); + } + + return mlir::WalkResult::advance(); + }); + } + + return exits; + }; + + BlockScan scan; + llvm::SmallVector> endReleases; for (auto *op : discarded) { - if (!allUsesReleasableInOwnBlock(op) || storedIntoUnownedLocalOutside(op)) + if (!allUsesReleasableInOwnBlock(op, scan) || storedIntoUnownedLocalOutside(op)) { continue; } auto *block = op->getBlock(); - auto rangeEnd = releaseRangeEnd(op); + auto rangeEnd = scan.releaseRangeEnd(op); // The end of the block is not the only way out of it. A `return` written inside an // `if` - which is where returns are usually written - leaves from a nested region and @@ -2052,54 +2102,53 @@ class OwnedReturnConsumptionPass // // jumpLeavesBlock answers it by walking from the jump up to this block and asking // whether anything on the way catches it. - for (auto it = std::next(mlir::Block::iterator(op)); it != rangeEnd; ++it) + // + // The exits of the ops between the definition and the end of its range (exitsOf). + for (auto [top, nested] : exitsOf(block)) { - if (it->getNumRegions() == 0) + if (!op->isBeforeInBlock(top) || (rangeEnd != block->end() && !top->isBeforeInBlock(&*rangeEnd))) { continue; } - it->walk([&](mlir::Operation *nested) { - // A nested function's `return` returns from that function, and its jumps are - // its own; nothing inside one is on a path out of this block. - if (mlir::isa(nested)) - { - return mlir::WalkResult::skip(); - } - - // A `throw` leaves as a `return` does - `const s = "x" + i; if (c) throw i;` lost - // `s` on every throw - unless a `try` between it and this block takes it: the - // block may then go on and give the value back at its own end. - auto leavesBlock = mlir::isa(nested) || - (mlir::isa(nested) && - jumpLeavesBlock(nested, block)) || - (mlir::isa(nested) && !throwLandsWithin(nested, block)); - if (leavesBlock && !releaseAfterFinally(builder, op, nested, block, placedAfterFinally)) - { - builder.setInsertionPoint(nested); - builder.create(op->getLoc(), op->getResult(0)); - } - - return mlir::WalkResult::advance(); - }); + if (!releaseAfterFinally(builder, op, nested, block, placedAfterFinally)) + { + builder.setInsertionPoint(nested); + builder.create(op->getLoc(), op->getResult(0)); + } } - auto *exiting = firstExitingOpAfter(op); + mlir::Operation *before = nullptr; + auto *exiting = scan.firstExitingOpAfter(op); if (exiting && (rangeEnd == block->end() || exiting->isBeforeInBlock(&*rangeEnd))) { - builder.setInsertionPoint(exiting); + before = exiting; } else if (rangeEnd != block->end()) { - builder.setInsertionPoint(&*rangeEnd); + before = &*rangeEnd; } else if (auto *terminator = block->getTerminator()) { - builder.setInsertionPoint(terminator); + before = terminator; + } + + endReleases.push_back({op, before}); + } + + // The releases at the end of the temporaries' stretches go in once every place is found: one put + // into a block left no room in its order between its neighbours, and the next temporary's + // isBeforeInBlock numbered the whole block again - for each of them, in a block of many. In the + // order found, each in front of its op (or at the block's end), as they went in one by one. + for (auto [op, before] : endReleases) + { + if (before) + { + builder.setInsertionPoint(before); } else { - builder.setInsertionPointToEnd(block); + builder.setInsertionPointToEnd(op->getBlock()); } builder.create(op->getLoc(), op->getResult(0)); @@ -2338,10 +2387,12 @@ class OwnedReturnConsumptionPass // that result's users that decide (handedOnByConditional). A receiver of a result nobody owns // retains, and a branch that makes it owned retained `h` for it (consumeConditionalResults), // so `h`'s own reference is still given back at the end of its block either way. - static bool allUsesReleasableInOwnBlock(mlir::Operation *op) + struct BlockScan; + + static bool allUsesReleasableInOwnBlock(mlir::Operation *op, BlockScan &scan) { auto *block = op->getBlock(); - auto rangeEnd = releaseRangeEnd(op); + auto rangeEnd = scan.releaseRangeEnd(op); llvm::SmallVector values{op->getResult(0)}; while (!values.empty()) @@ -2397,41 +2448,103 @@ class OwnedReturnConsumptionPass // definition. A statement with a yield inside - `if (c) { yield 1; }` - is the same: resuming // in the middle of it falls through to the rest of the block. A plain function has neither and // gets the whole block. - static mlir::Block::iterator releaseRangeEnd(mlir::Operation *op) + // + // The ops of a block that end such a stretch, and the ones that end its execution (firstExitingOpAfter), are + // the block's: BlockScan finds them once for a block, in its order, and answers each temporary from them. + // Looked for again from each temporary on, a function of many of them (1300 lines of `any` arithmetic, + // binaryArithmeticControlFlowGraphNotTooLarge) walked the rest of its block, regions and all, for each. + // Releases put in afterwards neither end a stretch nor leave the block. + struct BlockScan { - auto *block = op->getBlock(); - for (auto it = std::next(mlir::Block::iterator(op)); it != block->end(); ++it) + llvm::DenseMap> resumesOf; + llvm::DenseMap> exitingOf; + + llvm::SmallVector &resumes(mlir::Block *block) { - if (mlir::isa(*it)) + auto found = resumesOf.find(block); + if (found != resumesOf.end()) { - return it; + return found->second; } - if (it->getNumRegions() == 0) + auto &ops = resumesOf[block]; + for (auto &it : *block) { - continue; - } + if (mlir::isa(it)) + { + ops.push_back(&it); + continue; + } + + if (it.getNumRegions() == 0) + { + continue; + } + + auto resumes = it.walk([&](mlir::Operation *nested) { + // a nested function's yields are its own + if (mlir::isa(nested)) + { + return mlir::WalkResult::skip(); + } + + return mlir::isa(nested) + ? mlir::WalkResult::interrupt() + : mlir::WalkResult::advance(); + }); - auto resumes = it->walk([&](mlir::Operation *nested) { - // a nested function's yields are its own - if (mlir::isa(nested)) + if (resumes.wasInterrupted()) { - return mlir::WalkResult::skip(); + ops.push_back(&it); } + } - return mlir::isa(nested) - ? mlir::WalkResult::interrupt() - : mlir::WalkResult::advance(); - }); + return ops; + } - if (resumes.wasInterrupted()) + mlir::Block::iterator releaseRangeEnd(mlir::Operation *op) + { + auto *block = op->getBlock(); + for (auto *resume : resumes(block)) { - return it; + if (op->isBeforeInBlock(resume)) + { + return mlir::Block::iterator(resume); + } } + + return block->end(); } - return block->end(); - } + mlir::Operation *firstExitingOpAfter(mlir::Operation *op) + { + auto *block = op->getBlock(); + auto found = exitingOf.find(block); + if (found == exitingOf.end()) + { + auto &ops = exitingOf[block]; + for (auto &it : *block) + { + if (exitsBlock(&it)) + { + ops.push_back(&it); + } + } + + found = exitingOf.find(block); + } + + for (auto *exiting : found->second) + { + if (op->isBeforeInBlock(exiting)) + { + return exiting; + } + } + + return nullptr; + } + }; // Does this generator temporary, or a view of it, get stored somewhere? Asked under every model but // rc (only own has releases left by the time it matters). Under own the `{ value, done }` record a `yield` hands back is filled by stores that move the @@ -2485,21 +2598,12 @@ class OwnedReturnConsumptionPass // Releasing before the return is right for a discarded temporary by definition: what is // being returned was consumed by the return's own retain (ยง9.24) and so is not in this set. // A generator's `yield` ends the call the same way (releaseRangeEnd). - static mlir::Operation *firstExitingOpAfter(mlir::Operation *op) + // (BlockScan::firstExitingOpAfter) - and a `throw`: what follows it in the block never runs, so a temporary of + // the thrown expression - `throw "x" + i`'s number printed into a string - was never given back + static bool exitsBlock(mlir::Operation *op) { - auto *block = op->getBlock(); - for (auto it = std::next(mlir::Block::iterator(op)); it != block->end(); ++it) - { - // (and a `throw`: what follows it in the block never runs, so a temporary of the thrown - // expression - `throw "x" + i`'s number printed into a string - was never given back) - if (mlir::isa(*it)) - { - return &*it; - } - } - - return nullptr; + return mlir::isa(op); } static bool producesOwnedResult(mlir::Value value) diff --git a/tslang/lib/TypeScript/UnwindReleasePass.cpp b/tslang/lib/TypeScript/UnwindReleasePass.cpp index e949b72c8..3ec16735e 100644 --- a/tslang/lib/TypeScript/UnwindReleasePass.cpp +++ b/tslang/lib/TypeScript/UnwindReleasePass.cpp @@ -738,6 +738,19 @@ class UnwindReleasePass : public mlir::PassWrapper> aliasesCache; + auto aliasesOfResource = [&](mlir::Value resource) -> llvm::SmallVector & { + auto found = aliasesCache.find(resource); + if (found == aliasesCache.end()) + { + found = aliasesCache.try_emplace(resource, aliasesOf(resource)).first; + } + + return found->second; + }; + llvm::SmallVector pads; for (auto &block : region) { @@ -833,12 +846,32 @@ class UnwindReleasePass : public mlir::PassWrapper readInHandler; + for (auto *block : blocks) + { + for (auto &op : *block) + { + op.walk([&](mlir::Operation *nested) { + readInHandler.insert(nested->operand_begin(), nested->operand_end()); + }); + } + } + for (auto resource : resources) { - auto aliases = aliasesOf(resource); + auto &aliases = aliasesOfResource(resource); // read there, or the chained pad gives it back - auto read = false; + auto read = llvm::any_of(aliases, [&](mlir::Value alias) { return readInHandler.contains(alias); }); + if (!read) + { + continue; + } + auto touched = false; for (auto *block : blocks) { @@ -847,14 +880,13 @@ class UnwindReleasePass : public mlir::PassWrappergetOperands(), alias); touched |= touches(nested, alias); } }); } } - if (!read || touched) + if (touched) { continue; } @@ -894,7 +926,9 @@ class UnwindReleasePass : public mlir::PassWrapperoperand_begin(), clone->operand_end()); } } }