Auto merge of #69716 - jonas-schievink:generator-size, r=tmandry

Don't store locals in generators that are immediately overwritten with the resume argument

This fixes https://github.com/rust-lang/rust/issues/69672 and makes https://github.com/rust-lang/rust/pull/69033 pass the async fn size tests again (in other words, there will be no size regression of async fn if both this and https://github.com/rust-lang/rust/pull/69033 land).

~~This is a small botch and I'd rather have a more precise analysis, but that seems much harder to pull off, so this special-cases `Yield` terminators that store the resume argument into a simple local (ie. without any field projections) and explicitly marks that local as "not live" in the suspend point of that yield. We know that this local does not need to be stored in the generator for this suspend point because the next resume would immediately overwrite it with the passed-in resume argument anyways. The local might still end up in the state if it is used across another yield.~~ (this now properly updates the dataflow framework to handle this case)
This commit is contained in:
bors 2020-03-14 02:04:49 +00:00
commit 5ed3453af9
11 changed files with 120 additions and 31 deletions

View file

@ -4,11 +4,15 @@
use std::ops::{Generator, GeneratorState};
fn mkstr(my_name: String, my_mood: String) -> String {
format!("{} is {}", my_name.trim(), my_mood.trim())
}
fn my_scenario() -> impl Generator<String, Yield = &'static str, Return = String> {
|_arg: String| {
let my_name = yield "What is your name?";
let my_mood = yield "How are you feeling?";
format!("{} is {}", my_name.trim(), my_mood.trim())
mkstr(my_name, my_mood)
}
}

View file

@ -0,0 +1,28 @@
#![feature(generators)]
// run-pass
use std::mem::size_of_val;
fn main() {
// Generator taking a `Copy`able resume arg.
let gen_copy = |mut x: usize| {
loop {
drop(x);
x = yield;
}
};
// Generator taking a non-`Copy` resume arg.
let gen_move = |mut x: Box<usize>| {
loop {
drop(x);
x = yield;
}
};
// Neither of these generators have the resume arg live across the `yield`, so they should be
// 4 Bytes in size (only storing the discriminant)
assert_eq!(size_of_val(&gen_copy), 4);
assert_eq!(size_of_val(&gen_move), 4);
}