transform: various escape analysis improvements#5461
Open
jakebailey wants to merge 3 commits into
Open
Conversation
Add allocation diagnostics coverage for pointer-returning helpers, aggregate slice returns, and conditional pointer returns before changing the allocation optimizer. The golden files record the current heap-allocation behavior so later commits show exactly which diagnostics each optimizer improvement removes.
Follow returned pointer aliases when deciding whether runtime.alloc calls can be lowered to stack allocations. A returned parameter is not the same as nocapture: it still flows back to the caller and must be checked as an alias of the original allocation. Keep the analysis conservative for recursive returned-parameter chains and unknown operands. The existing golden test now shows that the non-escaping returned pointer cases no longer require heap allocation.
Track whether an allocation reaches a callee return separately from the
instruction where it escapes. The extra result state is needed because LLVM's
returned parameter attribute only covers scalar returns: a slice helper returns
its data pointer inside a {ptr, len, cap} aggregate, so the alias is only
visible by walking insertvalue and ret uses in the callee.
This lets OptimizeAllocs keep the backing array for returnIntSlice(s) on the
stack when the returned slice is ignored, matching gc's escape decision for the
same pattern. The golden output still keeps the escaping returned-slice case on
the heap.
Member
Author
|
Some raw data about the 787 Representative removed variables/sites:
|
There was a problem hiding this comment.
Pull request overview
This PR improves TinyGo’s heap-to-stack allocation optimization by enhancing escape analysis to recognize values that flow through returned parameters (including through aggregate operations), addressing unnecessary heap allocations like the one reported in #5378.
Changes:
- Extend
transform/allocs.goescape analysis to track “returned” flows and follow them across calls (including throughinsertvalue/extractvalue) while guarding against recursion. - Add new alloc-focused scenarios to
transform/testdata/allocs2.go(returned pointer/slice cases, including a recursive case). - Update golden outputs (
allocs2.out.reason/allocs2.out.cover) to reflect the reduced/shifted allocation set after the analysis improvements.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| transform/allocs.go | Enhances escape analysis to better detect non-escaping returned-pointer flows and aggregate return propagation. |
| transform/testdata/allocs2.go | Adds new test cases covering returned pointer/slice scenarios and recursion behavior. |
| transform/testdata/allocs2.out.reason | Updates expected “reason” output for heap allocations after the analysis change. |
| transform/testdata/allocs2.out.cover | Updates expected coverage-style allocation output after the analysis change. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 #5378
This began as a fix to #5378, but like most of my PRs, ballooned into a bit more.
Each commit has a description, but the gist is that we are able to stack allocate more stuff that BigGo could also already do.
Unfortunately after #5220 the diff for the output is not helpful, so I have structured the PR such that all new tests are added in the first commit, that way looking at individual later commits show which allocs they remove.