-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[WIP] mir-opt for generators/futures: copy propagate upvar into locals #108590
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
Changes from all commits
6277dbb
f0cbd03
e88e117
83e15f8
6dbe78d
f4a1d4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,162 @@ | ||||
//! Converts `y = <Future as IntoFuture>::into_future(x);` into just `y = x;`, | ||||
//! since we "know" that matches the behavior of the blanket implementation of | ||||
//! IntoFuture for F where F: Future. | ||||
//! | ||||
//! FIXME: determine such coalescing is sound. In particular, check whether | ||||
//! specialization could foil our plans here! | ||||
//! | ||||
//! This is meant to enhance the effectiveness of the upvar-to-local-prop | ||||
//! transformation in reducing the size of the generators constructed by the | ||||
//! compiler. | ||||
|
||||
use crate::MirPass; | ||||
use rustc_index::IndexVec; | ||||
use rustc_middle::mir::interpret::ConstValue; | ||||
use rustc_middle::mir::visit::MutVisitor; | ||||
use rustc_middle::mir::*; | ||||
use rustc_middle::ty::{self, Ty, TyCtxt}; | ||||
use rustc_span::def_id::DefId; | ||||
|
||||
pub struct InlineFutureIntoFuture; | ||||
impl<'tcx> MirPass<'tcx> for InlineFutureIntoFuture { | ||||
fn is_enabled(&self, sess: &rustc_session::Session) -> bool { | ||||
sess.mir_opt_level() > 0 // on by default w/o -Zmir-opt-level=0 | ||||
} | ||||
|
||||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||||
let Some(into_future_fn_def_id) = tcx.lang_items().into_future_fn() else { return; }; | ||||
let Some(future_trait_def_id) = tcx.lang_items().future_trait() else { return; }; | ||||
let mir_source_def_id = body.source.def_id(); | ||||
trace!("Running InlineFutureIntoFuture on {:?}", body.source); | ||||
let local_decls = body.local_decls().to_owned(); | ||||
let mut v = Inliner { | ||||
tcx, | ||||
into_future_fn_def_id, | ||||
future_trait_def_id, | ||||
mir_source_def_id, | ||||
local_decls, | ||||
}; | ||||
v.visit_body(body); | ||||
} | ||||
} | ||||
|
||||
struct Inliner<'tcx> { | ||||
tcx: TyCtxt<'tcx>, | ||||
mir_source_def_id: DefId, | ||||
into_future_fn_def_id: DefId, | ||||
future_trait_def_id: DefId, | ||||
local_decls: IndexVec<Local, LocalDecl<'tcx>>, | ||||
} | ||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)] | ||||
enum FoundImplFuture { | ||||
Yes, | ||||
No, | ||||
} | ||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)] | ||||
enum FoundIntoFutureCall { | ||||
Yes, | ||||
No, | ||||
} | ||||
|
||||
struct ImplFutureCallingIntoFuture<'tcx> { | ||||
args: Vec<Operand<'tcx>>, | ||||
destination: Place<'tcx>, | ||||
target: Option<BasicBlock>, | ||||
} | ||||
|
||||
impl<'tcx> Inliner<'tcx> { | ||||
// This verifies that `ty` implements `Future`, according to the where | ||||
// clauses (i.e. predicates) attached to the source code identified by | ||||
// `mir_source_def_id`). | ||||
fn does_ty_impl_future(&self, ty: Ty<'tcx>) -> FoundImplFuture { | ||||
|
let impls_future = self.type_implements_trait( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have now added as a to-do for the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot reuse the type_implements_trait
function; that is a method on the InferCtxt
, not on TyCtxt
(and I only have a TyCtxt
in the context of this code).
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only need to check that func.ty(local_decls, tcx).kind()
is a FnDef
. We don't need to check that it's actually a constant.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code looks like we are re-implementing a simplified version of instance resolution.
What about doing the same thing as the Inliner
optimization:
let substs = tcx.try_normalize_erasing_regions(param_env, substs).ok()?;
let callee = Instance::resolve(tcx, param_env, def_id, substs).flatten()?;
and check that callee.def
is InstanceDef::Item(<def_id of the default impl of IntoFuture::into_future>)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't really need a visitor here, a simple loop over basic blocks looking at terminators should be enough.