-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[WIP] Don't drop an enum after all its fields have been moved from #68493
[WIP] Don't drop an enum after all its fields have been moved from #68493
Conversation
r? @zackmdavis (rust_highfive has picked a reviewer for you, use r? to override) |
@bors try |
Awaiting bors try build completion |
[WIP] Don't `drop` enum if all fields are moved out of Previously, drop elaboration is smart enough to realize when all fields of a struct or tuple are moved out of along a given code path and will remove drop terminators on that code path. However, if all fields of an *enum* with multiple variants are moved out of, drop terminators are still emitted. After this PR, drop elaboration will eliminate drops of an `Option` after the value in the `Some` variant is moved out of. `Option::unwrap` is an example of code whose MIR should be a bit better optimized as a result of this. ```rust fn unwrap<T>(opt: Option<T>) -> T { match opt { Some(inner) => inner, None => panic!("unwrap"), } } ``` When I say all fields of an enum, I mean all fields across all variants, so this optimization does not apply when, e.g., the value in `Result::Ok` is moved out of. To improve this case, we would need to incorporate information about the variant that is currently live into the `{Un,}InitializedPlaces` dataflow analyses. This is more involved, but I plan to give it a try.
drop
enum if all fields are moved out of
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors try- |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors retry |
@Mark-Simulacrum This one's not quite right yet unfortunately. So much for my one line change. There's a bit of discussion on Zulip. Edit: just saw the discord discussion |
This optimization will require additional information about the liveness of enum variants. Closing for now. |
Drop elaboration is smart enough to realize when all fields of a struct or tuple are moved out of along a given code path and will remove drop terminators on that code path. However, if all fields of an enum with multiple variants are moved out of, drop terminators are still emitted. After this PR, drop elaboration will eliminate drops of an
Option
after the value in theSome
variant is moved out of.Option::unwrap
is an example of code whose MIR should be a bit better optimized as a result.When I say all fields of an enum, I mean all fields across all variants, so this optimization does not apply when, e.g., the value in
Result::Ok
is moved out of. To improve this case, we would need to incorporate information about the variant that is currently live into the{Un,}InitializedPlaces
dataflow analyses. This is more involved, but I plan to give it a try.This is a WIP pending perf results and the addition of a test.