Skip to content
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

Remove coroutine info when building coroutine drop body #120330

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,23 @@ impl<'tcx> MirSource<'tcx> {
}
}

/// Additional information carried by a MIR body when it is lowered from a coroutine.
/// This information is modified as it is lowered during the `StateTransform` MIR pass,
/// so not all fields will be active at a given time. For example, the `yield_ty` is
/// taken out of the field after yields are turned into returns, and the `coroutine_drop`
/// body is only populated after the state transform pass.
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
pub struct CoroutineInfo<'tcx> {
/// The yield type of the function, if it is a coroutine.
/// The yield type of the function. This field is removed after the state transform pass.
pub yield_ty: Option<Ty<'tcx>>,

/// The resume type of the function, if it is a coroutine.
/// The resume type of the function. This field is removed after the state transform pass.
pub resume_ty: Option<Ty<'tcx>>,

/// Coroutine drop glue.
/// Coroutine drop glue. This field is populated after the state transform pass.
pub coroutine_drop: Option<Body<'tcx>>,

/// The layout of a coroutine. Produced by the state transformation.
/// The layout of a coroutine. This field is populated after the state transform pass.
pub coroutine_layout: Option<CoroutineLayout<'tcx>>,

/// If this is a coroutine then record the type of source expression that caused this coroutine
Expand Down Expand Up @@ -303,6 +308,12 @@ pub struct Body<'tcx> {
/// and used for debuginfo. Indexed by a `SourceScope`.
pub source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,

/// Additional information carried by a MIR body when it is lowered from a coroutine.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brief mention of when it goes from Some to None would be good.

Copy link
Member Author

@compiler-errors compiler-errors Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this never goes from Some to None for normal MIR bodies. The only reason it "goes from Some to None" when we're building the coroutine drop shim is because we're cloning the whole MIR body struct (presumably out out of convenience) rather than, for example, just copying the basic blocks and locals into a fresh body.

The idea is that this is just basically "take what we can from the parent MIR body, and then just fix up what is wrong". Other body information is patched up at the same time, such as the source and arg_count, and I don't believe that it makes sense to leave a parallel comment on arg_count, for example.

I can leave a comment on the take call below though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great info to put into the comment! E.g. "Some" in the normal case, None only when we're building the coroutine drop shim".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None only when we're building the coroutine drop shim

And every other MIR body, not just coroutine drop shims. Which is why I think it feels redundant to note this, since this already says "when it is lowered from a coroutine".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final version looks fine, thanks!

///
/// Note that the coroutine drop shim, any promoted consts, and other synthetic MIR
/// bodies that come from processing a coroutine body are not typically coroutines
/// themselves, and should probably set this to `None` to avoid carrying redundant
/// information.
pub coroutine: Option<Box<CoroutineInfo<'tcx>>>,

/// Declarations of locals.
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_mir_transform/src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,12 @@ fn create_coroutine_drop_shim<'tcx>(
drop_clean: BasicBlock,
) -> Body<'tcx> {
let mut body = body.clone();
body.arg_count = 1; // make sure the resume argument is not included here
// Take the coroutine info out of the body, since the drop shim is
// not a coroutine body itself; it just has its drop built out of it.
let _ = body.coroutine.take();
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
// Make sure the resume argument is not included here, since we're
// building a body for `drop_in_place`.
body.arg_count = 1;

let source_info = SourceInfo::outermost(body.span);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
// MIR for `main::{closure#0}` 0 coroutine_drop
/* coroutine_layout = CoroutineLayout {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Unsurprisingly, coroutine layout is not present in the drop shim body anymore. This does affect the MIR dump, but does not affect the drop's codegen.)

field_tys: {
_0: CoroutineSavedTy {
ty: std::string::String,
source_info: SourceInfo {
span: $DIR/coroutine_drop_cleanup.rs:12:13: 12:15 (#0),
scope: scope[0],
},
ignore_for_traits: false,
},
},
variant_fields: {
Unresumed(0): [],
Returned (1): [],
Panicked (2): [],
Suspend0 (3): [_0],
},
storage_conflicts: BitMatrix(1x1) {
(_0, _0),
},
} */

fn main::{closure#0}(_1: *mut {coroutine@$DIR/coroutine_drop_cleanup.rs:11:15: 11:17}) -> () {
let mut _0: ();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
// MIR for `main::{closure#0}` 0 coroutine_drop
/* coroutine_layout = CoroutineLayout {
field_tys: {
_0: CoroutineSavedTy {
ty: std::string::String,
source_info: SourceInfo {
span: $DIR/coroutine_drop_cleanup.rs:12:13: 12:15 (#0),
scope: scope[0],
},
ignore_for_traits: false,
},
},
variant_fields: {
Unresumed(0): [],
Returned (1): [],
Panicked (2): [],
Suspend0 (3): [_0],
},
storage_conflicts: BitMatrix(1x1) {
(_0, _0),
},
} */

fn main::{closure#0}(_1: *mut {coroutine@$DIR/coroutine_drop_cleanup.rs:11:15: 11:17}) -> () {
let mut _0: ();
Expand Down
Loading