Skip to content

Commit b21759f

Browse files
committed
Auto merge of #96281 - SparrowLii:const_prop, r=wesleywiser
Optimize `const_prop` mir-opt by accessing `local_decls` through `ecx` From the FIXME in the impl of `ConstPropagator`. Accessing `local_decls` and `scource_scopes` from `ecx` can reduce `clone` calls and save compile time. Besides, according to #96213 , the FIXME about writing `layouts` to `ecx` in advance can also be removed.
2 parents 143eaa8 + db23e77 commit b21759f

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

Diff for: compiler/rustc_mir_transform/src/const_prop.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ struct ConstPropagator<'mir, 'tcx> {
313313
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
314314
tcx: TyCtxt<'tcx>,
315315
param_env: ParamEnv<'tcx>,
316-
// FIXME(eddyb) avoid cloning this field more than once,
317-
// by accessing it through `ecx` instead.
318-
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
316+
local_decls: &'mir IndexVec<Local, LocalDecl<'tcx>>,
319317
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
320318
// the last known `SourceInfo` here and just keep revisiting it.
321319
source_info: Option<SourceInfo>,
@@ -361,10 +359,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
361359
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
362360
let param_env = tcx.param_env_reveal_all_normalized(def_id);
363361

364-
let span = tcx.def_span(def_id);
365-
// FIXME: `CanConstProp::check` computes the layout of all locals, return those layouts
366-
// so we can write them to `ecx.frame_mut().locals.layout, reducing the duplication in
367-
// `layout_of` query invocations.
368362
let can_const_prop = CanConstProp::check(tcx, param_env, body);
369363
let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len());
370364
for (l, mode) in can_const_prop.iter_enumerated() {
@@ -374,7 +368,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
374368
}
375369
let mut ecx = InterpCx::new(
376370
tcx,
377-
span,
371+
tcx.def_span(def_id),
378372
param_env,
379373
ConstPropMachine::new(only_propagate_inside_block_locals, can_const_prop),
380374
);
@@ -405,10 +399,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
405399
ecx,
406400
tcx,
407401
param_env,
408-
// FIXME(eddyb) avoid cloning this field more than once,
409-
// by accessing it through `ecx` instead.
410-
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
411-
local_decls: body.local_decls.clone(),
402+
local_decls: &dummy_body.local_decls,
412403
source_info: None,
413404
}
414405
}
@@ -511,7 +502,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
511502
let r = r?;
512503
// We need the type of the LHS. We cannot use `place_layout` as that is the type
513504
// of the result, which for checked binops is not the same!
514-
let left_ty = left.ty(&self.local_decls, self.tcx);
505+
let left_ty = left.ty(self.local_decls, self.tcx);
515506
let left_size = self.ecx.layout_of(left_ty).ok()?.size;
516507
let right_size = r.layout.size;
517508
let r_bits = r.to_scalar().ok();

Diff for: compiler/rustc_mir_transform/src/const_prop_lint.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,8 @@ struct ConstPropagator<'mir, 'tcx> {
308308
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
309309
tcx: TyCtxt<'tcx>,
310310
param_env: ParamEnv<'tcx>,
311-
// FIXME(eddyb) avoid cloning these two fields more than once,
312-
// by accessing them through `ecx` instead.
313-
source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
314-
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
311+
source_scopes: &'mir IndexVec<SourceScope, SourceScopeData<'tcx>>,
312+
local_decls: &'mir IndexVec<Local, LocalDecl<'tcx>>,
315313
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
316314
// the last known `SourceInfo` here and just keep revisiting it.
317315
source_info: Option<SourceInfo>,
@@ -357,10 +355,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
357355
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
358356
let param_env = tcx.param_env_reveal_all_normalized(def_id);
359357

360-
let span = tcx.def_span(def_id);
361-
// FIXME: `CanConstProp::check` computes the layout of all locals, return those layouts
362-
// so we can write them to `ecx.frame_mut().locals.layout, reducing the duplication in
363-
// `layout_of` query invocations.
364358
let can_const_prop = CanConstProp::check(tcx, param_env, body);
365359
let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len());
366360
for (l, mode) in can_const_prop.iter_enumerated() {
@@ -370,7 +364,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
370364
}
371365
let mut ecx = InterpCx::new(
372366
tcx,
373-
span,
367+
tcx.def_span(def_id),
374368
param_env,
375369
ConstPropMachine::new(only_propagate_inside_block_locals, can_const_prop),
376370
);
@@ -401,11 +395,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
401395
ecx,
402396
tcx,
403397
param_env,
404-
// FIXME(eddyb) avoid cloning these two fields more than once,
405-
// by accessing them through `ecx` instead.
406-
source_scopes: body.source_scopes.clone(),
407-
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
408-
local_decls: body.local_decls.clone(),
398+
source_scopes: &dummy_body.source_scopes,
399+
local_decls: &dummy_body.local_decls,
409400
source_info: None,
410401
}
411402
}
@@ -435,7 +426,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
435426
}
436427

437428
fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
438-
source_info.scope.lint_root(&self.source_scopes)
429+
source_info.scope.lint_root(self.source_scopes)
439430
}
440431

441432
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
@@ -572,7 +563,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
572563
let r = r?;
573564
// We need the type of the LHS. We cannot use `place_layout` as that is the type
574565
// of the result, which for checked binops is not the same!
575-
let left_ty = left.ty(&self.local_decls, self.tcx);
566+
let left_ty = left.ty(self.local_decls, self.tcx);
576567
let left_size = self.ecx.layout_of(left_ty).ok()?.size;
577568
let right_size = r.layout.size;
578569
let r_bits = r.to_scalar().ok();

0 commit comments

Comments
 (0)