Skip to content

Commit 3d256b3

Browse files
committed
access local_decls through ecx
1 parent 5176945 commit 3d256b3

File tree

2 files changed

+14
-49
lines changed

2 files changed

+14
-49
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use rustc_middle::mir::visit::{
1212
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
1313
};
1414
use rustc_middle::mir::{
15-
BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind, Location,
16-
Operand, Place, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
17-
RETURN_PLACE,
15+
BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalKind, Location, Operand, Place,
16+
Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
1817
};
1918
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
2019
use rustc_middle::ty::subst::{InternalSubsts, Subst};
@@ -313,9 +312,6 @@ struct ConstPropagator<'mir, 'tcx> {
313312
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
314313
tcx: TyCtxt<'tcx>,
315314
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>>,
319315
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
320316
// the last known `SourceInfo` here and just keep revisiting it.
321317
source_info: Option<SourceInfo>,
@@ -361,10 +357,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
361357
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
362358
let param_env = tcx.param_env_reveal_all_normalized(def_id);
363359

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.
368360
let can_const_prop = CanConstProp::check(tcx, param_env, body);
369361
let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len());
370362
for (l, mode) in can_const_prop.iter_enumerated() {
@@ -374,7 +366,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
374366
}
375367
let mut ecx = InterpCx::new(
376368
tcx,
377-
span,
369+
tcx.def_span(def_id),
378370
param_env,
379371
ConstPropMachine::new(only_propagate_inside_block_locals, can_const_prop),
380372
);
@@ -401,16 +393,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
401393
)
402394
.expect("failed to push initial stack frame");
403395

404-
ConstPropagator {
405-
ecx,
406-
tcx,
407-
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(),
412-
source_info: None,
413-
}
396+
ConstPropagator { ecx, tcx, param_env, source_info: None }
414397
}
415398

416399
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
@@ -511,7 +494,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
511494
let r = r?;
512495
// We need the type of the LHS. We cannot use `place_layout` as that is the type
513496
// of the result, which for checked binops is not the same!
514-
let left_ty = left.ty(&self.local_decls, self.tcx);
497+
let left_ty = left.ty(&self.ecx.frame().body.local_decls, self.tcx);
515498
let left_size = self.ecx.layout_of(left_ty).ok()?.size;
516499
let right_size = r.layout.size;
517500
let r_bits = r.to_scalar().ok();
@@ -1133,7 +1116,7 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
11331116
assert!(
11341117
self.get_const(local.into()).is_none()
11351118
|| self
1136-
.layout_of(self.local_decls[local].ty)
1119+
.layout_of(self.ecx.frame().body.local_decls[local].ty)
11371120
.map_or(true, |layout| layout.is_zst())
11381121
)
11391122
}

compiler/rustc_mir_transform/src/const_prop_lint.rs

+8-26
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use rustc_index::bit_set::BitSet;
1111
use rustc_index::vec::IndexVec;
1212
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
1313
use rustc_middle::mir::{
14-
AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind,
15-
Location, Operand, Place, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
16-
StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
14+
AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalKind, Location,
15+
Operand, Place, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
16+
RETURN_PLACE,
1717
};
1818
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
1919
use rustc_middle::ty::subst::{InternalSubsts, Subst};
@@ -308,10 +308,6 @@ 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>>,
315311
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
316312
// the last known `SourceInfo` here and just keep revisiting it.
317313
source_info: Option<SourceInfo>,
@@ -357,10 +353,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
357353
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
358354
let param_env = tcx.param_env_reveal_all_normalized(def_id);
359355

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.
364356
let can_const_prop = CanConstProp::check(tcx, param_env, body);
365357
let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len());
366358
for (l, mode) in can_const_prop.iter_enumerated() {
@@ -370,7 +362,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
370362
}
371363
let mut ecx = InterpCx::new(
372364
tcx,
373-
span,
365+
tcx.def_span(def_id),
374366
param_env,
375367
ConstPropMachine::new(only_propagate_inside_block_locals, can_const_prop),
376368
);
@@ -397,17 +389,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
397389
)
398390
.expect("failed to push initial stack frame");
399391

400-
ConstPropagator {
401-
ecx,
402-
tcx,
403-
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(),
409-
source_info: None,
410-
}
392+
ConstPropagator { ecx, tcx, param_env, source_info: None }
411393
}
412394

413395
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
@@ -435,7 +417,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
435417
}
436418

437419
fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
438-
source_info.scope.lint_root(&self.source_scopes)
420+
source_info.scope.lint_root(&self.ecx.frame().body.source_scopes)
439421
}
440422

441423
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
@@ -572,7 +554,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
572554
let r = r?;
573555
// We need the type of the LHS. We cannot use `place_layout` as that is the type
574556
// of the result, which for checked binops is not the same!
575-
let left_ty = left.ty(&self.local_decls, self.tcx);
557+
let left_ty = left.ty(&self.ecx.frame().body.local_decls, self.tcx);
576558
let left_size = self.ecx.layout_of(left_ty).ok()?.size;
577559
let right_size = r.layout.size;
578560
let r_bits = r.to_scalar().ok();
@@ -1026,7 +1008,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
10261008
assert!(
10271009
self.get_const(local.into()).is_none()
10281010
|| self
1029-
.layout_of(self.local_decls[local].ty)
1011+
.layout_of(self.ecx.frame().body.local_decls[local].ty)
10301012
.map_or(true, |layout| layout.is_zst())
10311013
)
10321014
}

0 commit comments

Comments
 (0)