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

Optimize UnDerefer #99667

Merged
merged 5 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn do_mir_borrowck<'a, 'tcx>(

let (move_data, move_errors): (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>) =
match MoveData::gather_moves(&body, tcx, param_env) {
Ok(move_data) => (move_data, Vec::new()),
Ok((_, move_data)) => (move_data, Vec::new()),
Err((move_data, move_errors)) => (move_data, move_errors),
};
let promoted_errors = promoted
Expand Down
17 changes: 14 additions & 3 deletions compiler/rustc_mir_dataflow/src/move_paths/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::move_paths::FxHashMap;
use crate::un_derefer::UnDerefer;
use rustc_index::vec::IndexVec;
use rustc_middle::mir::tcx::RvalueInitializationState;
Expand Down Expand Up @@ -209,7 +210,10 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
fn finalize(
self,
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
) -> Result<
(FxHashMap<rustc_middle::mir::Local, rustc_middle::mir::Place<'tcx>>, MoveData<'tcx>),
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
> {
ouz-a marked this conversation as resolved.
Show resolved Hide resolved
debug!("{}", {
debug!("moves for {:?}:", self.body.span);
for (j, mo) in self.data.moves.iter_enumerated() {
Expand All @@ -222,15 +226,22 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
"done dumping moves"
});

if !self.errors.is_empty() { Err((self.data, self.errors)) } else { Ok(self.data) }
if !self.errors.is_empty() {
Err((self.data, self.errors))
} else {
Ok((self.un_derefer.derefer_sidetable.clone(), self.data))
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

pub(super) fn gather_moves<'tcx>(
body: &Body<'tcx>,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
) -> Result<
(FxHashMap<rustc_middle::mir::Local, rustc_middle::mir::Place<'tcx>>, MoveData<'tcx>),
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
> {
let mut builder = MoveDataBuilder::new(body, tcx, param_env);

builder.gather_args();
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_mir_dataflow/src/move_paths/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ impl<'tcx> MoveData<'tcx> {
body: &Body<'tcx>,
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
) -> Result<Self, (Self, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
) -> Result<
(FxHashMap<rustc_middle::mir::Local, rustc_middle::mir::Place<'tcx>>, MoveData<'tcx>),
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
> {
builder::gather_moves(body, tcx, param_env)
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_dataflow/src/rustc_peek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl<'tcx> MirPass<'tcx> for SanityCheck {
}

let param_env = tcx.param_env(def_id);
let move_data = MoveData::gather_moves(body, tcx, param_env).unwrap();
let mdpe = MoveDataParamEnv { move_data, param_env };
let (_, move_data) = MoveData::gather_moves(body, tcx, param_env).unwrap();
let mdpe = MoveDataParamEnv { move_data: move_data, param_env };
ouz-a marked this conversation as resolved.
Show resolved Hide resolved

if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe)
Expand Down
16 changes: 1 addition & 15 deletions compiler/rustc_mir_dataflow/src/un_derefer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct UnDerefer<'tcx> {
}

impl<'tcx> UnDerefer<'tcx> {
#[inline]
pub fn derefer(&self, place: PlaceRef<'tcx>, body: &Body<'tcx>) -> Option<Place<'tcx>> {
let reffed = self.derefer_sidetable.get(&place.local)?;

Expand All @@ -18,19 +19,4 @@ impl<'tcx> UnDerefer<'tcx> {
}
Some(new_place)
}

pub fn ref_finder(&mut self, body: &Body<'tcx>) {
for (_bb, data) in body.basic_blocks().iter_enumerated() {
for stmt in data.statements.iter() {
match stmt.kind {
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
if body.local_decls[place.local].is_deref_temp() {
self.derefer_sidetable.insert(place.local, reffed);
}
}
_ => (),
}
}
}
}
}
7 changes: 3 additions & 4 deletions compiler/rustc_mir_transform/src/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);

let mut un_derefer = UnDerefer { tcx: tcx, derefer_sidetable: Default::default() };
un_derefer.ref_finder(body);
let def_id = body.source.def_id();
let param_env = tcx.param_env_reveal_all_normalized(def_id);
let move_data = match MoveData::gather_moves(body, tcx, param_env) {
let (side_table, move_data) = match MoveData::gather_moves(body, tcx, param_env) {
Ok(move_data) => move_data,
Err((move_data, _)) => {
tcx.sess.delay_span_bug(
body.span,
"No `move_errors` should be allowed in MIR borrowck",
);
move_data
(Default::default(), move_data)
}
};
let un_derefer = UnDerefer { tcx: tcx, derefer_sidetable: side_table };
let elaborate_patch = {
let body = &*body;
let env = MoveDataParamEnv { move_data, param_env };
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/remove_uninit_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct RemoveUninitDrops;
impl<'tcx> MirPass<'tcx> for RemoveUninitDrops {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let param_env = tcx.param_env(body.source.def_id());
let Ok(move_data) = MoveData::gather_moves(body, tcx, param_env) else {
let Ok((_,move_data)) = MoveData::gather_moves(body, tcx, param_env) else {
// We could continue if there are move errors, but there's not much point since our
// init data isn't complete.
return;
Expand Down