Repository navigation
rc: @owned declarations, so calls through function values give their results back with the default library - #923
Merged
Merged
Conversation
…lls through function values give their results back A program with the default library never had a closed world: every lib.d.ts declaration (`String.trim`, `Number.toFixed`, `Error..new` ...) has no body, so OwnedReturnConsumptionPass could not classify it, and one unclassified heap-returning function turned off result consumption for every call through a function value, callback or `any`. Each such result leaked, and so did every direct library call's. `@owned` on a `declare function`, `declare class` (its methods and `..new`) or `declare namespace` (its functions) marks the FuncOp ts.declared_owned_result: the function is TypeScript compiled elsewhere under the same memory model, so it returns +1 like an `export`ed import. Unmarked declarations stay unclassified (a C function, or one returning a pointer into a buffer, such as convertNumber), so they leak rather than being freed twice. The parser leaves a namespace's statements without a parent; they are linked for an `@owned` namespace only. Probe (-mm=rc, 1M calls through `(n) => any` and `(n) => P`): 97 MB -> 5.6 MB peak, flat. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under own, OwnedReturnConsumptionPass counting `JsonValue.get` as returning a fresh result released what it lends: DefaultLib's json.ts / json_read.ts failed with "borrows the result of 'JsonValue.get' but is used here after it may be released". gc and none have no count to give back, so only rc reads the marker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the
-mm=rcleak of heap results returned through indirect calls (callbacks, function values, calls throughany) in programs that use the default library.Cause. OwnedReturnConsumptionPass consumes an indirect call's result only under closed world: no function in the module may return a heap value without retaining it. Every default-library declaration (
String.trim,Number.toFixed,Error..new, …) has no body and noexport, so it was unclassified, and closed world never held once the library was in. Direct library calls leaked as well.Fix. A new declaration decorator,
@owned, on adeclare function, adeclare class(its methods and..new) or adeclare namespace(its functions). It marks the FuncOpts.declared_owned_result, meaning "TypeScript compiled elsewhere under the same memory model, returns +1". Under rc the pass treats it like anexported import.convertNumber) leaks rather than being freed twice.@ownedon a function with a body is ignored, with a warning.JsonValue.get's lent result count as fresh, which broke DefaultLibjson.tsandjson_read.ts.@owned, so the decorator on the namespace can be found.Paired with ASDAlexander77/TypeScriptCompilerDefaultLib#106, which marks
lib.d.tsand thecore.d.tsclasses. The six buffer-returningcore.os.d.tsdeclarations stay unmarked.Measured (
-mm=rc, peak working set, with DefaultLib#106):(n) => anyreturning"s"+nand(n) => Preturningnew P.trim()in a loop, 1MTests
test-rc-owned-declarationsreads the IR ofowned-declarations/owned_declarations.tsunder rc. With@ownedon a function, a class and a namespace, all four FuncOps are marked and the call through a parameter is consumed. With every@ownedremoved, nothing is marked and the call keeps retaining.ctest -C Release -j 8: 8279/8279 pass.tests.ps1release (compile + JIT), built with this compiler and DefaultLib#106: passes in gc, rc, none and own.Follow-up (not in this PR):
toFixedstill leaks inside the library, fromconvertNumber'schar[]buffer.🤖 Generated with Claude Code