From b4da68973c460a84f95442c37f183f57e75d41aa Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 16:47:00 +0000 Subject: [PATCH 1/3] rc: a temporary is released right after its last use, not at the end of its block Under -mm=rc OwnedReturnConsumptionPass gave a discarded temporary back where its stretch of the block ends, so every temporary was live across every call after it: each call that may throw got a cleanup pad giving back all of them (UnwindReleasePass), quadratic code - binaryArithmeticControlFlowGraphNotTooLarge (1300 lines of `any` arithmetic) took ~27 min to compile under rc, 860 s of it in LLVM's optimizer. - lastUseInBlock: the op of the block holding the last use of the value under any of its names (what users make of it unless it comes with a reference of its own, what a conditional hands on, loads of a local it is stored into); none when a use can't be followed, and the release stays where it was. The release goes right after that op; nested exits after it owe nothing. rc only: own's passes read where releases are. - UnwindReleasePass: a value whose release dominates the call is given back already and owes nothing (the walk on from the call for its release went to the end of the function, and in a loop found the next turn's); a resource's aliases and a release's hand-over test are answered once. rc compile of that file: ~27 min -> 10.5 s; n=200 synthetic: 13.7 s -> 1.6 s, cleanup releases linear. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/OwnedReturnConsumptionPass.cpp | 177 +++++++++++++++--- tslang/lib/TypeScript/UnwindReleasePass.cpp | 46 ++++- 2 files changed, 201 insertions(+), 22 deletions(-) diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index 5299df884..a997cd2df 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -2066,7 +2066,9 @@ class OwnedReturnConsumptionPass }; BlockScan scan; - llvm::SmallVector> endReleases; + // a temporary, the op its release goes in front of (null: the block's end), and the op it goes right + // after instead (rc: its last use), when there is one + llvm::SmallVector> endReleases; for (auto *op : discarded) { if (!allUsesReleasableInOwnBlock(op, scan) || storedIntoUnownedLocalOutside(op)) @@ -2077,6 +2079,41 @@ class OwnedReturnConsumptionPass auto *block = op->getBlock(); auto rangeEnd = scan.releaseRangeEnd(op); + // Where the stretch would end (see below): in front of the first op that leaves the block, of the + // end of the range, or of the block's terminator + mlir::Operation *before = nullptr; + auto *exiting = scan.firstExitingOpAfter(op); + if (exiting && (rangeEnd == block->end() || exiting->isBeforeInBlock(&*rangeEnd))) + { + before = exiting; + } + else if (rangeEnd != block->end()) + { + before = &*rangeEnd; + } + else if (auto *terminator = block->getTerminator()) + { + before = terminator; + } + + // -mm=rc: the temporary is given back right after the op of its block that uses it last + // (lastUseInBlock), not where its stretch ends. Held to the end, each one was live across every call + // after it in its block, and each call that may throw had to give all of them back on its unwind + // path (UnwindReleasePass): a function of many - 1300 lines of `any` arithmetic, + // binaryArithmeticControlFlowGraphNotTooLarge - got a cleanup pad of thousands of releases for each + // call, and took half an hour to compile. Not under -mm=own, whose passes after this read where the + // releases are; and not where a use can't be followed (lastUseInBlock gives none) or is not before + // the stretch's end. + mlir::Operation *after = nullptr; + if (compileOptions.isRefCounted()) + { + auto *last = lastUseInBlock(op); + if (last && (!before || last->isBeforeInBlock(before))) + { + after = last; + } + } + // 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 // never reaches the release placed below, so the temporary was simply lost on that @@ -2103,10 +2140,13 @@ class OwnedReturnConsumptionPass // jumpLeavesBlock answers it by walking from the jump up to this block and asking // whether anything on the way catches it. // - // The exits of the ops between the definition and the end of its range (exitsOf). + // The exits of the ops between the definition and the end of its range (exitsOf) - with the release + // right after the last use (`after`), the exits up to that op only: one after it leaves with the + // value given back already. for (auto [top, nested] : exitsOf(block)) { - if (!op->isBeforeInBlock(top) || (rangeEnd != block->end() && !top->isBeforeInBlock(&*rangeEnd))) + if (!op->isBeforeInBlock(top) || (rangeEnd != block->end() && !top->isBeforeInBlock(&*rangeEnd)) || + (after && after->isBeforeInBlock(top))) { continue; } @@ -2118,31 +2158,20 @@ class OwnedReturnConsumptionPass } } - mlir::Operation *before = nullptr; - auto *exiting = scan.firstExitingOpAfter(op); - if (exiting && (rangeEnd == block->end() || exiting->isBeforeInBlock(&*rangeEnd))) - { - before = exiting; - } - else if (rangeEnd != block->end()) - { - before = &*rangeEnd; - } - else if (auto *terminator = block->getTerminator()) - { - before = terminator; - } - - endReleases.push_back({op, before}); + endReleases.push_back({op, before, after}); } // 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) + for (auto [op, before, after] : endReleases) { - if (before) + if (after) + { + builder.setInsertionPointAfter(after); + } + else if (before) { builder.setInsertionPoint(before); } @@ -2387,6 +2416,112 @@ 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. + // The op of `op`'s block that holds the last use of its value (rc's release goes right after it), or none + // when one can't be followed - the release then stays where the stretch ends. The value goes by other names + // than its own, and their uses are its uses: + // - what an op makes of it, unless that comes with a reference of its own (OWNED_RESULT_ATTR_NAME): a view + // (a cast, a union made of it), a place in it (`ts.PropertyRef`, then a load), a number read out of it; + // - what a conditional hands on (handedOnByConditional); + // - what is loaded from a local of the block it is stored into: every use of that local is a load or a + // store, or the local is one it can't follow. + // A store anywhere else, or a terminator handing it on otherwise (a loop's), is a use it can't follow. + static mlir::Operation *lastUseInBlock(mlir::Operation *op) + { + auto *block = op->getBlock(); + mlir::Operation *last = op; + auto later = [&](mlir::Operation *user) { + auto *inBlock = block->findAncestorOpInBlock(*user); + if (!inBlock) + { + return false; + } + + if (last->isBeforeInBlock(inBlock)) + { + last = inBlock; + } + + return true; + }; + + llvm::SmallVector values{op->getResult(0)}; + llvm::DenseSet seen; + while (!values.empty()) + { + auto value = values.pop_back_val(); + if (!seen.insert(value).second) + { + continue; + } + + for (auto *user : value.getUsers()) + { + if (!later(user)) + { + return nullptr; + } + + if (auto storeOp = mlir::dyn_cast(user)) + { + if (storeOp.getValue() != value) + { + continue; + } + + auto varOp = storeOp.getReference().getDefiningOp(); + if (!varOp) + { + return nullptr; + } + + for (auto *varUser : varOp->getUsers()) + { + if (!later(varUser)) + { + return nullptr; + } + + if (auto loadOp = mlir::dyn_cast(varUser)) + { + values.push_back(loadOp.getResult()); + } + else if (auto varStoreOp = mlir::dyn_cast(varUser); + !varStoreOp || varStoreOp.getReference() != varOp.getResult()) + { + return nullptr; + } + } + + continue; + } + + if (auto handedOn = handedOnByConditional(user)) + { + values.push_back(handedOn); + continue; + } + + // handed on by a terminator other than a conditional's + if (user->hasTrait()) + { + return nullptr; + } + + if (user->hasAttr(OWNED_RESULT_ATTR_NAME)) + { + continue; + } + + for (auto result : user->getResults()) + { + values.push_back(result); + } + } + } + + return last; + } + struct BlockScan; static bool allUsesReleasableInOwnBlock(mlir::Operation *op, BlockScan &scan) diff --git a/tslang/lib/TypeScript/UnwindReleasePass.cpp b/tslang/lib/TypeScript/UnwindReleasePass.cpp index 3ec16735e..45cab36ca 100644 --- a/tslang/lib/TypeScript/UnwindReleasePass.cpp +++ b/tslang/lib/TypeScript/UnwindReleasePass.cpp @@ -957,6 +957,7 @@ class UnwindReleasePass : public mlir::PassWrapper resources; llvm::DenseSet seen; + llvm::DenseMap> valueReleasesOf; funcOp.walk([&](mlir::Operation *op) { if (auto resource = resourceOf(op)) { @@ -964,6 +965,11 @@ class UnwindReleasePass : public mlir::PassWrapper(op)) + { + valueReleasesOf[resource].push_back(op); + } } }); @@ -1008,6 +1014,30 @@ class UnwindReleasePass : public mlir::PassWrapper releases; }; + // asked of each pair of a call and a resource, answered once: a resource's names (aliasesOf) and whether a + // release is the giving-up half of an overwrite (isHandOver, a walk on through its block). Nothing is + // changed until every plan is made + llvm::DenseMap> 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::DenseMap handOverCache; + auto handOver = [&](mlir::Operation *release) { + auto [it, inserted] = handOverCache.try_emplace(release, false); + if (inserted) + { + it->second = isHandOver(release); + } + + return it->second; + }; + llvm::SmallVector plans; for (auto &site : sites) { @@ -1022,9 +1052,23 @@ class UnwindReleasePass : public mlir::PassWrappersecond, [&](mlir::Operation *release) { + return dominance.properlyDominates(release, site.call) && + (!definingOp || dominance.properlyDominates(definingOp, release)) && !handOver(release); + })) + { + continue; + } + // the handler an invoke unwinds to now must have nothing more to do with it: the // pad gives it back for good - if (site.dest && llvm::any_of(aliasesOf(resource), [&](mlir::Value alias) { + if (site.dest && llvm::any_of(aliasesOfResource(resource), [&](mlir::Value alias) { return usedFrom(site.dest).contains(alias); })) { From de602e9c73c370a9c512b13057389dd6391a5441 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 17:09:06 +0000 Subject: [PATCH 2/3] rc release at last use: not for a named const or a view; tests read the releases in front of a yield - A `const` holding the value itself (OWNED_RESULT_NAMED) is a local: given back where its scope ends, as an object let go of earlier is seen (Shared.count, a WeakRef's deref) - stack-promotion/shared_handle. - A view (an interface made of an object literal's copy) is the block it shows: the first `use(o)` gave it back and the literal's block, born with no count, was freed before the second (own_interface_literal_views under rc hung at exit). A view's users are followed as the value's (NewInterface is marked owned). - generator-temporaries.cmake counts the releases in front of each yield instead of reading the ops right before it; own-mlirgen-matches-rc compares with the releases left out (their places are each model's). Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/OwnedReturnConsumptionPass.cpp | 13 ++++- .../test/tester/generator-temporaries.cmake | 55 ++++++++++++++++--- .../test/tester/own-mlirgen-matches-rc.cmake | 3 + 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index a997cd2df..34499c4d3 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -2104,8 +2104,15 @@ class OwnedReturnConsumptionPass // call, and took half an hour to compile. Not under -mm=own, whose passes after this read where the // releases are; and not where a use can't be followed (lastUseInBlock gives none) or is not before // the stretch's end. + // Only a temporary with no name: a `const` holding the value itself (OWNED_RESULT_NAMED_ATTR_NAME) is a + // local, given back where its scope ends - an object let go of earlier is seen let go of + // (`Shared.count`, a `WeakRef`'s deref, a destructor). + // Nor a view (own_facts::isView: an interface made of an object, a union of a class): its release is the + // release of the block it shows, which the value it is made of, and that value's other views, may go on + // using - `use(o); use(o)` gave the first interface back after its call, and the + // object literal's block, born with no count, was freed before the second. mlir::Operation *after = nullptr; - if (compileOptions.isRefCounted()) + if (compileOptions.isRefCounted() && !op->hasAttr(OWNED_RESULT_NAMED_ATTR_NAME) && !own_facts::isView(op)) { auto *last = lastUseInBlock(op); if (last && (!before || last->isBeforeInBlock(before))) @@ -2507,7 +2514,9 @@ class OwnedReturnConsumptionPass return nullptr; } - if (user->hasAttr(OWNED_RESULT_ATTR_NAME)) + // (a view is the value still, whatever it is marked: an interface made of an object literal's copy, + // `ts.NewInterface`, is that copy - given back right after it, `use(o)` read freed memory) + if (user->hasAttr(OWNED_RESULT_ATTR_NAME) && !own_facts::isView(user)) { continue; } diff --git a/tslang/test/tester/generator-temporaries.cmake b/tslang/test/tester/generator-temporaries.cmake index 8db779f86..003057120 100644 --- a/tslang/test/tester/generator-temporaries.cmake +++ b/tslang/test/tester/generator-temporaries.cmake @@ -1,6 +1,7 @@ # What a generator body makes and what it yields is given back (00generator_temporaries.ts). No # run shows which references leak, so the IR is read, after OwnedReturnConsumptionPass -# (--emit=mlir). Under rc: +# (--emit=mlir). The releases are counted in front of the yield (or of the `if` holding one), not +# read as the ops right before it: rc gives a temporary back right after its last use. Under rc: # - lengths: `yield ("a" + i).length` releases the number's string and the concatenation # before the yield; # - strings: `yield "s" + i` retains the record before the yield and then releases both @@ -46,6 +47,30 @@ function(count pattern body outVar) set(${outVar} ${n} PARENT_SCOPE) endfunction() +# the text of `body` in front of the first `marker`, and the rest after it +function(split_at marker body beforeVar afterVar) + string(FIND "${body}" "${marker}" at) + if(at EQUAL -1) + message(FATAL_ERROR "no `${marker}` in:\n${body}") + endif() + string(SUBSTRING "${body}" 0 ${at} before) + string(LENGTH "${marker}" length) + math(EXPR at "${at} + ${length}") + string(SUBSTRING "${body}" ${at} -1 after) + set(${beforeVar} "${before}" PARENT_SCOPE) + set(${afterVar} "${after}" PARENT_SCOPE) +endfunction() + +# the text of `body` in front of the last `marker` there is in `head` +function(before_last marker head outVar) + string(FIND "${head}" "${marker}" at REVERSE) + if(at EQUAL -1) + message(FATAL_ERROR "no `${marker}` in:\n${head}") + endif() + string(SUBSTRING "${head}" 0 ${at} before) + set(${outVar} "${before}" PARENT_SCOPE) +endfunction() + # (CMake's regex has no {n}) set(release "\"ts[.]Release\"[(]%[0-9]+[)] : [(]!ts[.](string|class<@Item[^)]*)[)] -> [(][)][\r\n]+ *") set(recordRetain "\"ts[.]Retain\"[(]%[0-9]+[)] : [(]!ts[.]tuple<[^\r\n]*[\r\n]+ *") @@ -54,29 +79,45 @@ set(stringRelease "\"ts[.]Release\"[(]%[0-9]+[)] : [(]!ts[.]string[)]") emit(rc out) +set(yieldOp "\"ts.YieldReturnVal\"") + func_body("${out}" "\".f_lengths.." body) -if(NOT body MATCHES "${release}${release}${yield}") +split_at("${yieldOp}" "${body}" head tail) +count("${release}" "${head}" n) +if(n LESS 2) message(FATAL_ERROR "`lengths` does not release its two temporaries before the yield:\n${body}") endif() func_body("${out}" "\".f_strings.." body) -if(NOT body MATCHES "${recordRetain}${release}${release}${yield}") +split_at("${yieldOp}" "${body}" head tail) +count("${release}" "${head}" n) +count("${recordRetain}" "${head}" retains) +if(n LESS 2 OR retains LESS 1) message(FATAL_ERROR "`strings` does not retain its record and release both strings before the yield:\n${body}") endif() func_body("${out}" "\".f_items.." body) -if(NOT body MATCHES "${recordRetain}${release}${yield}") +split_at("${yieldOp}" "${body}" head tail) +count("${release}" "${head}" n) +count("${recordRetain}" "${head}" retains) +if(n LESS 1 OR retains LESS 1) message(FATAL_ERROR "`items` does not retain its record and release the instance before the yield:\n${body}") endif() func_body("${out}" "\".f_branchYield.." body) -if(NOT body MATCHES "${release}${release}\"ts[.]If\"") +split_at("${yieldOp}" "${body}" head tail) +before_last("\"ts.If\"" "${head}" beforeIf) +count("${release}" "${beforeIf}" n) +if(n LESS 2) message(FATAL_ERROR "`branchYield` does not release its temporaries before the `if` holding the yield:\n${body}") endif() func_body("${out}" "\".f_twoSegments.." body) -count("${release}${release}${yield}" "${body}" n) -if(NOT n EQUAL 2) +split_at("${yieldOp}" "${body}" first rest) +split_at("${yieldOp}" "${rest}" second tail) +count("${release}" "${first}" n1) +count("${release}" "${second}" n2) +if(n1 LESS 2 OR n2 LESS 2) message(FATAL_ERROR "`twoSegments` does not release two temporaries before each yield:\n${body}") endif() diff --git a/tslang/test/tester/own-mlirgen-matches-rc.cmake b/tslang/test/tester/own-mlirgen-matches-rc.cmake index 19238213b..2fd001f76 100644 --- a/tslang/test/tester/own-mlirgen-matches-rc.cmake +++ b/tslang/test/tester/own-mlirgen-matches-rc.cmake @@ -6,6 +6,9 @@ foreach(model rc own) OUTPUT_VARIABLE out_${model} ERROR_VARIABLE err_${model} RESULT_VARIABLE rc_${model}) string(APPEND out_${model} "${err_${model}}") string(REPLACE "__tsmm_${model}_" "__tsmm_MODEL_" out_${model} "${out_${model}}") + # where a discarded temporary is given back is the models' own (OwnedReturnConsumptionPass, which + # --emit=mlir has run: rc right after its last use, own where its stretch ends); a release has no result + string(REGEX REPLACE "[^\r\n]*\"ts[.]Release\"[(][^\r\n]*[\r\n]+" "" out_${model} "${out_${model}}") endforeach() if(NOT out_rc STREQUAL out_own) message(FATAL_ERROR "MLIRGen output differs between -mm=rc and -mm=own:\n--- rc\n${out_rc}\n--- own\n${out_own}") From f9c660f2254c007ba458d116da5a716234e18eed Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 17:19:22 +0000 Subject: [PATCH 3/3] TSLANG_ISSUES 154: compile time of a function of many any operations (gc #976, rc release at last use) Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 71382e04a..3ac6d3c33 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2224,3 +2224,22 @@ Still open, looked at: Tests: `00iterables.ts`, `00generator_typed_yield.ts` (compile, JIT, rc), diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts`, `00generator_return_throw.ts` (compile, JIT, rc, own), diagnostic-location `generator_yield_in_catch.ts`. + +154 (compile time of a function of many `any` operations, 2026-11-21). The tsc sweep's +binaryArithmeticControlFlowGraphNotTooLarge - 1300 lines of `any` arithmetic in one function - timed out now and +then: not flaky, a steady 45 s (gc) against the sweep's 60 s with 8 jobs, and ~27 min under rc. +- gc (#976): a discovery pass made a generic instance again for every call (`___any_key` for each `x[k]`), a + module walk after each `any` access, OwnedReturnConsumptionPass and UnwindReleasePass walking the rest of a + block, or a handler, once for each temporary: 45 s -> 4.7 s. +- rc: OwnedReturnConsumptionPass gave a discarded temporary back where its stretch of the block ends, so every + temporary was live across every call after it, and each call that may throw got a cleanup pad giving all of + them back - quadratic code (UnwindReleasePass ~8-11 min, LLVM's optimizer ~14 min). Now a temporary is given + back right after the op of its block that uses it last (lastUseInBlock: its users' results unless they come with + a reference of their own, views followed even then, what a conditional hands on, loads of a local it is stored + into; a use that can't be followed keeps the old place). Not a named `const` (a local: `Shared.count`, a + WeakRef see it let go of) nor a view (its release is the block it shows: an interface of an object literal's + copy freed the literal before its next use). UnwindReleasePass skips a value whose release dominates the call. + ~27 min -> 10.6 s; the cleanup releases grow linearly. rc only: own's passes read where the releases are. + +Verified: ctest (8781), the wasm32 rc tester suite debug and release (763 each), mm_fuzz run (551 programs) and +leaks (80): no failures.