Skip to content

Commit 7dfdd64

Browse files
committed
Auto merge of rust-lang#99667 - ouz-a:some_branch, r=oli-obk
Optimize `UnDerefer` Addresses the performance [issues](rust-lang#98145 (comment)) faced here. r? `@oli-obk`
2 parents ea6ab1b + bd24b40 commit 7dfdd64

File tree

7 files changed

+22
-28
lines changed

7 files changed

+22
-28
lines changed

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn do_mir_borrowck<'a, 'tcx>(
215215

216216
let (move_data, move_errors): (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>) =
217217
match MoveData::gather_moves(&body, tcx, param_env) {
218-
Ok(move_data) => (move_data, Vec::new()),
218+
Ok((_, move_data)) => (move_data, Vec::new()),
219219
Err((move_data, move_errors)) => (move_data, move_errors),
220220
};
221221
let promoted_errors = promoted

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::move_paths::FxHashMap;
12
use crate::un_derefer::UnDerefer;
23
use rustc_index::vec::IndexVec;
34
use rustc_middle::mir::tcx::RvalueInitializationState;
@@ -206,10 +207,13 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
206207
}
207208
}
208209

210+
pub type MoveDat<'tcx> = Result<
211+
(FxHashMap<Local, Place<'tcx>>, MoveData<'tcx>),
212+
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
213+
>;
214+
209215
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
210-
fn finalize(
211-
self,
212-
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
216+
fn finalize(self) -> MoveDat<'tcx> {
213217
debug!("{}", {
214218
debug!("moves for {:?}:", self.body.span);
215219
for (j, mo) in self.data.moves.iter_enumerated() {
@@ -222,15 +226,19 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
222226
"done dumping moves"
223227
});
224228

225-
if !self.errors.is_empty() { Err((self.data, self.errors)) } else { Ok(self.data) }
229+
if self.errors.is_empty() {
230+
Ok((self.un_derefer.derefer_sidetable, self.data))
231+
} else {
232+
Err((self.data, self.errors))
233+
}
226234
}
227235
}
228236

229237
pub(super) fn gather_moves<'tcx>(
230238
body: &Body<'tcx>,
231239
tcx: TyCtxt<'tcx>,
232240
param_env: ty::ParamEnv<'tcx>,
233-
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
241+
) -> MoveDat<'tcx> {
234242
let mut builder = MoveDataBuilder::new(body, tcx, param_env);
235243

236244
builder.gather_args();

compiler/rustc_mir_dataflow/src/move_paths/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::move_paths::builder::MoveDat;
12
use rustc_data_structures::fx::FxHashMap;
23
use rustc_index::vec::IndexVec;
34
use rustc_middle::mir::*;
@@ -386,7 +387,7 @@ impl<'tcx> MoveData<'tcx> {
386387
body: &Body<'tcx>,
387388
tcx: TyCtxt<'tcx>,
388389
param_env: ParamEnv<'tcx>,
389-
) -> Result<Self, (Self, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
390+
) -> MoveDat<'tcx> {
390391
builder::gather_moves(body, tcx, param_env)
391392
}
392393

compiler/rustc_mir_dataflow/src/rustc_peek.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'tcx> MirPass<'tcx> for SanityCheck {
3030
}
3131

3232
let param_env = tcx.param_env(def_id);
33-
let move_data = MoveData::gather_moves(body, tcx, param_env).unwrap();
33+
let (_, move_data) = MoveData::gather_moves(body, tcx, param_env).unwrap();
3434
let mdpe = MoveDataParamEnv { move_data, param_env };
3535

3636
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {

compiler/rustc_mir_dataflow/src/un_derefer.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct UnDerefer<'tcx> {
99
}
1010

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

@@ -18,19 +19,4 @@ impl<'tcx> UnDerefer<'tcx> {
1819
}
1920
Some(new_place)
2021
}
21-
22-
pub fn ref_finder(&mut self, body: &Body<'tcx>) {
23-
for (_bb, data) in body.basic_blocks().iter_enumerated() {
24-
for stmt in data.statements.iter() {
25-
match stmt.kind {
26-
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
27-
if body.local_decls[place.local].is_deref_temp() {
28-
self.derefer_sidetable.insert(place.local, reffed);
29-
}
30-
}
31-
_ => (),
32-
}
33-
}
34-
}
35-
}
3622
}

compiler/rustc_mir_transform/src/elaborate_drops.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
2828
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2929
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);
3030

31-
let mut un_derefer = UnDerefer { tcx: tcx, derefer_sidetable: Default::default() };
32-
un_derefer.ref_finder(body);
3331
let def_id = body.source.def_id();
3432
let param_env = tcx.param_env_reveal_all_normalized(def_id);
35-
let move_data = match MoveData::gather_moves(body, tcx, param_env) {
33+
let (side_table, move_data) = match MoveData::gather_moves(body, tcx, param_env) {
3634
Ok(move_data) => move_data,
3735
Err((move_data, _)) => {
3836
tcx.sess.delay_span_bug(
3937
body.span,
4038
"No `move_errors` should be allowed in MIR borrowck",
4139
);
42-
move_data
40+
(Default::default(), move_data)
4341
}
4442
};
43+
let un_derefer = UnDerefer { tcx: tcx, derefer_sidetable: side_table };
4544
let elaborate_patch = {
4645
let body = &*body;
4746
let env = MoveDataParamEnv { move_data, param_env };

compiler/rustc_mir_transform/src/remove_uninit_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct RemoveUninitDrops;
2121
impl<'tcx> MirPass<'tcx> for RemoveUninitDrops {
2222
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2323
let param_env = tcx.param_env(body.source.def_id());
24-
let Ok(move_data) = MoveData::gather_moves(body, tcx, param_env) else {
24+
let Ok((_,move_data)) = MoveData::gather_moves(body, tcx, param_env) else {
2525
// We could continue if there are move errors, but there's not much point since our
2626
// init data isn't complete.
2727
return;

0 commit comments

Comments
 (0)