From 09134982e53830a4268a95b57fda2b9e39c1908b Mon Sep 17 00:00:00 2001 From: ouz-a Date: Sun, 24 Jul 2022 14:40:43 +0300 Subject: [PATCH 1/5] optimize un_derefer --- compiler/rustc_borrowck/src/lib.rs | 2 +- .../src/move_paths/builder.rs | 17 ++++++++++++++--- .../rustc_mir_dataflow/src/move_paths/mod.rs | 5 ++++- compiler/rustc_mir_dataflow/src/rustc_peek.rs | 4 ++-- compiler/rustc_mir_dataflow/src/un_derefer.rs | 16 +--------------- .../rustc_mir_transform/src/elaborate_drops.rs | 7 +++---- .../src/remove_uninit_drops.rs | 2 +- 7 files changed, 26 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index e6142cab5c299..5bda6a41cedb9 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -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 diff --git a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs index 19aa71d7bc772..462429d1a2281 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs @@ -1,4 +1,5 @@ use crate::un_derefer::UnDerefer; +use rustc_data_structures::stable_map::FxHashMap; use rustc_index::vec::IndexVec; use rustc_middle::mir::tcx::RvalueInitializationState; use rustc_middle::mir::*; @@ -209,7 +210,10 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> { impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> { fn finalize( self, - ) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { + ) -> Result< + (FxHashMap>, MoveData<'tcx>), + (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>), + > { debug!("{}", { debug!("moves for {:?}:", self.body.span); for (j, mo) in self.data.moves.iter_enumerated() { @@ -222,7 +226,11 @@ 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)) + } } } @@ -230,7 +238,10 @@ pub(super) fn gather_moves<'tcx>( body: &Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, -) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { +) -> Result< + (FxHashMap>, MoveData<'tcx>), + (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>), +> { let mut builder = MoveDataBuilder::new(body, tcx, param_env); builder.gather_args(); diff --git a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs index 1af789b4885e6..b6050a1ccaa05 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs @@ -386,7 +386,10 @@ impl<'tcx> MoveData<'tcx> { body: &Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, - ) -> Result, MoveError<'tcx>)>)> { + ) -> Result< + (FxHashMap>, MoveData<'tcx>), + (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>), + > { builder::gather_moves(body, tcx, param_env) } diff --git a/compiler/rustc_mir_dataflow/src/rustc_peek.rs b/compiler/rustc_mir_dataflow/src/rustc_peek.rs index e1df482786f1b..98671a8681635 100644 --- a/compiler/rustc_mir_dataflow/src/rustc_peek.rs +++ b/compiler/rustc_mir_dataflow/src/rustc_peek.rs @@ -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 }; if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() { let flow_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe) diff --git a/compiler/rustc_mir_dataflow/src/un_derefer.rs b/compiler/rustc_mir_dataflow/src/un_derefer.rs index ec2e516f7ac5f..04f626b49e841 100644 --- a/compiler/rustc_mir_dataflow/src/un_derefer.rs +++ b/compiler/rustc_mir_dataflow/src/un_derefer.rs @@ -9,6 +9,7 @@ pub struct UnDerefer<'tcx> { } impl<'tcx> UnDerefer<'tcx> { + #[inline] pub fn derefer(&self, place: PlaceRef<'tcx>, body: &Body<'tcx>) -> Option> { let reffed = self.derefer_sidetable.get(&place.local)?; @@ -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); - } - } - _ => (), - } - } - } - } } diff --git a/compiler/rustc_mir_transform/src/elaborate_drops.rs b/compiler/rustc_mir_transform/src/elaborate_drops.rs index 71ab6dee1b662..9c1fcbaa69d6b 100644 --- a/compiler/rustc_mir_transform/src/elaborate_drops.rs +++ b/compiler/rustc_mir_transform/src/elaborate_drops.rs @@ -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 }; diff --git a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs index efa45883eab4b..4c33f839f9b8b 100644 --- a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs +++ b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs @@ -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; From 4e726e04cd777fb2a95a2a45d89a6a3ebc98c20d Mon Sep 17 00:00:00 2001 From: ouz-a Date: Sun, 24 Jul 2022 14:57:49 +0300 Subject: [PATCH 2/5] fix import error --- compiler/rustc_mir_dataflow/src/move_paths/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs index 462429d1a2281..417b331dc50cc 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs @@ -1,5 +1,5 @@ +use crate::move_paths::FxHashMap; use crate::un_derefer::UnDerefer; -use rustc_data_structures::stable_map::FxHashMap; use rustc_index::vec::IndexVec; use rustc_middle::mir::tcx::RvalueInitializationState; use rustc_middle::mir::*; From a5c895e1d8f8b9f17d4c91a6741f082eca67a3cc Mon Sep 17 00:00:00 2001 From: ouz-a Date: Mon, 25 Jul 2022 17:08:54 +0300 Subject: [PATCH 3/5] remove clone --- compiler/rustc_mir_dataflow/src/move_paths/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs index 417b331dc50cc..8145dcd6e2ec2 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs @@ -229,7 +229,7 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> { if !self.errors.is_empty() { Err((self.data, self.errors)) } else { - Ok((self.un_derefer.derefer_sidetable.clone(), self.data)) + Ok((self.un_derefer.derefer_sidetable, self.data)) } } } From bd52f58e3b4008c35f677eec5fe18676f686274b Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Jul 2022 12:56:57 +0300 Subject: [PATCH 4/5] create type alias --- .../src/move_paths/builder.rs | 18 +++++++----------- .../rustc_mir_dataflow/src/move_paths/mod.rs | 6 ++---- compiler/rustc_mir_dataflow/src/rustc_peek.rs | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs index 8145dcd6e2ec2..b8634fd568bc8 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs @@ -207,13 +207,12 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> { } } +pub type MoveDat<'tcx> = (FxHashMap>, MoveData<'tcx>); + impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> { fn finalize( self, - ) -> Result< - (FxHashMap>, MoveData<'tcx>), - (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>), - > { + ) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { debug!("{}", { debug!("moves for {:?}:", self.body.span); for (j, mo) in self.data.moves.iter_enumerated() { @@ -226,10 +225,10 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> { "done dumping moves" }); - if !self.errors.is_empty() { - Err((self.data, self.errors)) - } else { + if self.errors.is_empty() { Ok((self.un_derefer.derefer_sidetable, self.data)) + } else { + Err((self.data, self.errors)) } } } @@ -238,10 +237,7 @@ pub(super) fn gather_moves<'tcx>( body: &Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, -) -> Result< - (FxHashMap>, MoveData<'tcx>), - (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>), -> { +) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { let mut builder = MoveDataBuilder::new(body, tcx, param_env); builder.gather_args(); diff --git a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs index b6050a1ccaa05..aeb985143fddc 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs @@ -1,3 +1,4 @@ +use crate::move_paths::builder::MoveDat; use rustc_data_structures::fx::FxHashMap; use rustc_index::vec::IndexVec; use rustc_middle::mir::*; @@ -386,10 +387,7 @@ impl<'tcx> MoveData<'tcx> { body: &Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, - ) -> Result< - (FxHashMap>, MoveData<'tcx>), - (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>), - > { + ) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { builder::gather_moves(body, tcx, param_env) } diff --git a/compiler/rustc_mir_dataflow/src/rustc_peek.rs b/compiler/rustc_mir_dataflow/src/rustc_peek.rs index 98671a8681635..f2471f37a5266 100644 --- a/compiler/rustc_mir_dataflow/src/rustc_peek.rs +++ b/compiler/rustc_mir_dataflow/src/rustc_peek.rs @@ -31,7 +31,7 @@ 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: move_data, param_env }; + let mdpe = MoveDataParamEnv { move_data, param_env }; if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() { let flow_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe) From bd24b4006c98425aa994763acf7f7e18607c7df1 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 28 Jul 2022 13:52:49 +0300 Subject: [PATCH 5/5] type alias covers whole return --- compiler/rustc_mir_dataflow/src/move_paths/builder.rs | 11 ++++++----- compiler/rustc_mir_dataflow/src/move_paths/mod.rs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs index b8634fd568bc8..116e5c1f3ce0a 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/builder.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/builder.rs @@ -207,12 +207,13 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> { } } -pub type MoveDat<'tcx> = (FxHashMap>, MoveData<'tcx>); +pub type MoveDat<'tcx> = Result< + (FxHashMap>, MoveData<'tcx>), + (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>), +>; impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> { - fn finalize( - self, - ) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { + fn finalize(self) -> MoveDat<'tcx> { debug!("{}", { debug!("moves for {:?}:", self.body.span); for (j, mo) in self.data.moves.iter_enumerated() { @@ -237,7 +238,7 @@ pub(super) fn gather_moves<'tcx>( body: &Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, -) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { +) -> MoveDat<'tcx> { let mut builder = MoveDataBuilder::new(body, tcx, param_env); builder.gather_args(); diff --git a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs index aeb985143fddc..a951c5b0b1c94 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs @@ -387,7 +387,7 @@ impl<'tcx> MoveData<'tcx> { body: &Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, - ) -> Result, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> { + ) -> MoveDat<'tcx> { builder::gather_moves(body, tcx, param_env) }