Skip to content

Commit

Permalink
Erase lifetimes in SROA.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 5, 2023
1 parent 248a530 commit fc1a861
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 7 deletions.
9 changes: 5 additions & 4 deletions compiler/rustc_mir_dataflow/src/value_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ impl Map {
}

// Recurse with all fields of this place.
iter_fields(ty, tcx, |variant, field, ty| {
iter_fields(ty, tcx, ty::ParamEnv::reveal_all(), |variant, field, ty| {
if let Some(variant) = variant {
projection.push(PlaceElem::Downcast(None, variant));
let _ = self.make_place(local, projection);
Expand Down Expand Up @@ -939,6 +939,7 @@ impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
pub fn iter_fields<'tcx>(
ty: Ty<'tcx>,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
mut f: impl FnMut(Option<VariantIdx>, Field, Ty<'tcx>),
) {
match ty.kind() {
Expand All @@ -956,14 +957,14 @@ pub fn iter_fields<'tcx>(
for (f_index, f_def) in v_def.fields.iter().enumerate() {
let field_ty = f_def.ty(tcx, substs);
let field_ty = tcx
.try_normalize_erasing_regions(ty::ParamEnv::reveal_all(), field_ty)
.unwrap_or(field_ty);
.try_normalize_erasing_regions(param_env, field_ty)
.unwrap_or_else(|_| tcx.erase_regions(field_ty));
f(variant, f_index.into(), field_ty);
}
}
}
ty::Closure(_, substs) => {
iter_fields(substs.as_closure().tupled_upvars_ty(), tcx, f);
iter_fields(substs.as_closure().tupled_upvars_ty(), tcx, param_env, f);
}
_ => (),
}
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_mir_transform/src/sroa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_index::vec::IndexVec;
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_mir_dataflow::value_analysis::{excluded_locals, iter_fields};

pub struct ScalarReplacementOfAggregates;
Expand All @@ -18,11 +18,12 @@ impl<'tcx> MirPass<'tcx> for ScalarReplacementOfAggregates {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!(def_id = ?body.source.def_id());
let mut excluded = excluded_locals(body);
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
loop {
debug!(?excluded);
let escaping = escaping_locals(&excluded, body);
debug!(?escaping);
let replacements = compute_flattening(tcx, body, escaping);
let replacements = compute_flattening(tcx, param_env, body, escaping);
debug!(?replacements);
let all_dead_locals = replace_flattened_locals(tcx, body, replacements);
if !all_dead_locals.is_empty() {
Expand Down Expand Up @@ -144,6 +145,7 @@ impl<'tcx> ReplacementMap<'tcx> {
/// The replacement will be done later in `ReplacementVisitor`.
fn compute_flattening<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
body: &mut Body<'tcx>,
escaping: BitSet<Local>,
) -> ReplacementMap<'tcx> {
Expand All @@ -155,7 +157,7 @@ fn compute_flattening<'tcx>(
}
let decl = body.local_decls[local].clone();
let ty = decl.ty;
iter_fields(ty, tcx, |variant, field, field_ty| {
iter_fields(ty, tcx, param_env, |variant, field, field_ty| {
if variant.is_some() {
// Downcasts are currently not supported.
return;
Expand Down
Loading

0 comments on commit fc1a861

Please sign in to comment.