Skip to content

rc: a temporary is released right after its last use (rc compile ~27 min -> 10.6 s) - #977

Merged
ASDAlexander77 merged 3 commits into
mainfrom
rc-release-at-last-use
Nov 21, 2026
Merged

ASDAlexander77 merged 3 commits into
mainfrom
rc-release-at-last-use

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Follow-up to #976 (TSLANG 154). Under -mm=rc, binaryArithmeticControlFlowGraphNotTooLarge (1300 lines of any arithmetic in one function) took ~27 min to compile; it now takes 10.6 s.

Cause. Under rc, OwnedReturnConsumptionPass released a discarded temporary at the end of its stretch of the block. Every temporary therefore stayed live across every call after it. Each call that may throw got a cleanup pad (UnwindReleasePass) that released all of them, so the code grew quadratically. Doubling the function multiplied the cleanup releases by about 4. UnwindReleasePass took ~8–11 min and LLVM's optimizer ~14 min.

Fix (rc only, because own's passes read where the releases are).

  • Placement. A temporary is released right after the op of its block that uses it last (lastUseInBlock). The value's other names count as uses:

    • what its users make of it, unless that result comes with a reference of its own (a view is followed even then);
    • what a conditional hands on;
    • loads of a local in the block that it is stored into.

    If a use can't be followed (a store anywhere else, a terminator handing it on), the release stays where it was. Nested exits after the last use no longer release it.

  • Exceptions. Two cases keep the old placement:

    • a named const (__owned_result_named) is a local; releasing it earlier would be visible through Shared.count or a WeakRef (stack-promotion/shared_handle);
    • a view, such as an interface made of an object literal's copy. Its release is the release of the block it shows, which can still be in use (own_interface_literal_views under rc).
  • Unwind pass. UnwindReleasePass skips a value whose release dominates the call: it was released before the call, so the call owes nothing for it. A resource's aliases and a release's hand-over test are each computed once.

Cleanup releases now grow linearly (528 → 878 → 1578 for 25/50/100 lines, against 7.5k → 26k → 97k before).

Tests updated.

  • generator-temporaries.cmake counts the releases in front of each yield instead of reading the ops just before it.
  • own-mlirgen-matches-rc.cmake compares the two models with release lines left out, since where releases go is now each model's own.

Verified locally.

  • ctest: 8781 tests, 0 failed.
  • wasm32 rc tester suite: 763 passed, 0 failed in both debug and release. Its heap checks every free, so it catches an early or double free that Windows misses.
  • mm_fuzz.py run: 551 programs, 0 failures. mm_fuzz.py leaks: 80 programs, no leaks.

🤖 Generated with Claude Code

ASDAlexander77 and others added 3 commits November 21, 2026 17:09
…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 <noreply@anthropic.com>
…he 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(<I>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 <noreply@anthropic.com>
…(gc #976, rc release at last use)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 2eaeab7 into main Nov 21, 2026
11 of 12 checks passed
@ASDAlexander77
ASDAlexander77 deleted the rc-release-at-last-use branch November 21, 2026 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant