Skip to content

Compile time of a function of many any operations: 45 s -> 4.7 s - #976

Merged
ASDAlexander77 merged 1 commit into
mainfrom
any-compile-time
Nov 21, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
any-compile-time

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

The tsc sweep's binaryArithmeticControlFlowGraphNotTooLarge sometimes timed out. It is not flaky: it is one function of 1300 lines of any arithmetic that took a steady 45 s to compile, against the sweep's 60 s limit with 8 jobs running. It now takes 4.7 s (7.2 s inside the sweep).

Fixes

  • MLIR generation, 41 s -> 3.7 s. Two causes:
    • A discovery pass (dummyRun) rebuilt a generic instance on every call. Its module is thrown away and doesn't keep the function, so mlirGenFunctionLikeDeclaration never found the one made by the previous call. When the type arguments are given explicitly, the pass now takes the instance's type from the function map instead. Every x[k] of an any rebuilt ___any_key<any> and its ___unbox helpers: 2740 instances for 100 reads, now 12. Reading x[k] 200 times went from 8.6 s to 0.26 s.
    • markAnyAdapterCalls looks only at the module's top-level functions, instead of walking every op of the module after each x[k] or call of an any.
  • OwnedReturnConsumptionPass, 2.7 s -> ~0. For each temporary, releaseDiscardedTemporaries used to walk the rest of the block, nested regions included, to find the block's exits and where the temporary's stretch ends. These belong to the block, so they are now found once per block (BlockScan, exitsOf). The releases at the end of each stretch are inserted after the loop, so the block's op order is no longer renumbered for every temporary. The releases and their positions are the same as before.
  • UnwindReleasePass::releaseAtUnwindExits walked each landing pad's handler once per resource. It now collects the handler's reads once per pad and each resource's aliases once.

Still open: under -mm=rc the same file takes much longer (24 min before this change). Temporaries are released at the end of their block, so each call that may throw gets a cleanup pad that releases every temporary made before it. The generated code grows quadratically, and LLVM's optimizer spends most of the time on it. A fix would release temporaries after their last use, which is a change of its own.

Tests: full ctest locally, 8776 tests, 0 failed. Full tsc compiler-tests sweep: 3256 passing, the same as before, with no regressions.

🤖 Generated with Claude Code

…inaryArithmeticControlFlowGraphNotTooLarge)

The tsc sweep's binaryArithmeticControlFlowGraphNotTooLarge (1300 lines of `any` arithmetic in one function)
compiled in a steady 45 s, near the sweep's 60 s limit with 8 jobs: it timed out now and then.
- A discovery pass (dummyRun) made a generic instance again for each call: its module, thrown away, does not
  keep the function, so mlirGenFunctionLikeDeclaration found none. With its type arguments given, the
  instance's type from the function map is what the pass needs. Every `x[k]` of an `any` made
  `___any_key<any>` and its `___unbox`es again: 2740 instances for 100 reads; now 12.
- markAnyAdapterCalls looks at the module's top-level functions only, not every op of the module after each
  `x[k]` or call of an `any`.
- OwnedReturnConsumptionPass::releaseDiscardedTemporaries found the ways out of a block, and where a
  temporary's stretch ends, by walking the rest of the block for each temporary; they are the block's, found
  once (BlockScan, exitsOf). The releases at the stretches' ends go in after the loop, so a block's op order is
  not numbered again for each.
- UnwindReleasePass::releaseAtUnwindExits walked each pad's handler once for each resource: the handler's reads
  are collected once a pad, a resource's aliases once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit d7dc80c into main Nov 21, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the any-compile-time branch November 21, 2026 17:03
ASDAlexander77 added a commit that referenced this pull request Nov 21, 2026
…min -> 10.6 s) (#977)

* 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 <noreply@anthropic.com>

* 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(<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>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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