Skip to content

Add merge yields pass - #162429

Open
diondokter wants to merge 10 commits into
rust-lang:mainfrom
diondokter:collapse_yields
Open

Add merge yields pass#162429
diondokter wants to merge 10 commits into
rust-lang:mainfrom
diondokter:collapse_yields

Conversation

@diondokter

@diondokter diondokter commented Sep 7, 2026

Copy link
Copy Markdown
Member

Part of Async statemachine optimisation project goal

r? dingxiangfei2009

Added a pass that merges functionally identical yields right before the async StateTransform pass. The StateTransform pass creates a unique state for every yield, so by merging yields we reduce the amount of states in the state machines.

See the module documentation for more information.

As for how much binary size is saved, that's hard to say in general. There's plenty of code that doesn't have identical yields. But when there is, the savings can be huge. In this project it saves 616 bytes. For a customer, when they applied this pass manually, it saved ~2kb in one instance.

It'll also play nice with the next optimization I'm going to work on, where we optimize async functions with a single await.

I think the biggest risk in the PR are the compare functions for the basic blocks and their parts. That's most of the code in the pass and can break if the basic block structures are updated but this code is forgotten. I don't know how to solve/improve that, so I'm curious if anyone can think of something. The threat is that the compare functions say the blocks are identical, but they're not in reality.

Also, the pass is currently always enabled. I don't know what people want there. Does it need an unstable flag? Or depend on the mir opt level?

@rustbot

rustbot commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 7, 2026
@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 7, 2026
@rustbot

rustbot commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 7, 2026
@diondokter diondokter changed the title Add merge yields Add merge yields pass Sep 7, 2026
@rust-log-analyzer

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants