-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mir-opt] optimization idea: bubble return
terminators to predecessor blocks
#72022
Comments
I don't know if we have such a rule in (any part of) rustc, but in my experience this is a convenient assumption for some analyses and transforms. So depending on the expected value of "bubbling up" |
The docs for |
The built MIR only contains a single Return terminator. However the generator transform code appears to output MIR with multiple return terminators in |
I recall this being motivated by LLVM liking a singular return better than multiple returns. "Should" is not a "must", so it isn’t incorrect that we generate (sometimes) multiple returns. But it is still a guidance that stands. As thus I don’t love what #72563 did /me shrugs. |
I concur with @nagisa. Canonicalizing a mistake made in the generator transform is wrongheaded IMO. If we want to do this, we should make a conscious decision. There's probably code that relies on only one |
Cranelift likes multiple returns better than single returns. It never duplicates |
This was only added under mir-opt-level 3, in case anyone is wondering why this is closed. We haven't finished the discussion on whether we want multiple return terminators per body or whether we should fix generators |
Use multiple returns in MIR if it saves a block; still have only one in LLVM This is still an open question whether it's desired, per rust-lang#72022 (comment), so opening as a draft. Given that cranelift prefers multiple returns and we use optimized MIR for MIR inlining, it seems at least plausible that MIR shouldn't have the "one return" rule (the validator doesn't enforce it currently either) so we can have fewer total blocks, and then targets like LLVM can enforce it if they so choose (as this PR does). r? ghost
cc @rust-lang/wg-mir-opt
if we have a basic block with no statements and just a return terminator, any other basic block that just
goto
s into that block could just as well callreturn
itself. Or do we have some rule that we can only have a single return terminator permir::Body
?Maybe we can generalize this and bubble any terminator of a statement-less block into its predecessor terminators iff that predecessor terminator is a
goto
.As an example take
rust/src/test/mir-opt/issue-62289/rustc.test.ElaborateDrops.before.mir
Line 84 in 7b80539
return
terminator, and both predecessor blocks have agoto
to this block.The text was updated successfully, but these errors were encountered: