Skip to content

Commit f166609

Browse files
authored
Rollup merge of rust-lang#66216 - wesleywiser:const_prop_codegen_improvements, r=oli-obk
[mir-opt] Handle return place in ConstProp and improve SimplifyLocals pass Temporarily rebased on top of rust-lang#66074. The top 2 commits are new. r? @oli-obk
2 parents f135e33 + 4505ff4 commit f166609

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::hir::def_id::DefId;
99
use rustc::mir::{
1010
AggregateKind, Constant, Location, Place, PlaceBase, Body, Operand, Rvalue, Local, UnOp,
1111
StatementKind, Statement, LocalKind, TerminatorKind, Terminator, ClearCrossCrate, SourceInfo,
12-
BinOp, SourceScope, SourceScopeLocalData, LocalDecl, BasicBlock,
12+
BinOp, SourceScope, SourceScopeLocalData, LocalDecl, BasicBlock, RETURN_PLACE,
1313
};
1414
use rustc::mir::visit::{
1515
Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext,
@@ -25,6 +25,7 @@ use rustc::ty::layout::{
2525
LayoutOf, TyLayout, LayoutError, HasTyCtxt, TargetDataLayout, HasDataLayout,
2626
};
2727

28+
use crate::rustc::ty::subst::Subst;
2829
use crate::interpret::{
2930
self, InterpCx, ScalarMaybeUndef, Immediate, OpTy,
3031
StackPopCleanup, LocalValue, LocalState, AllocId, Frame,
@@ -269,6 +270,7 @@ struct ConstPropagator<'mir, 'tcx> {
269270
param_env: ParamEnv<'tcx>,
270271
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
271272
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
273+
ret: Option<OpTy<'tcx, ()>>,
272274
}
273275

274276
impl<'mir, 'tcx> LayoutOf for ConstPropagator<'mir, 'tcx> {
@@ -308,11 +310,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
308310
let mut ecx = InterpCx::new(tcx.at(span), param_env, ConstPropMachine, ());
309311
let can_const_prop = CanConstProp::check(body);
310312

313+
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
314+
315+
let ret =
316+
ecx
317+
.layout_of(body.return_ty().subst(tcx, substs))
318+
.ok()
319+
// Don't bother allocating memory for ZST types which have no values.
320+
.filter(|ret_layout| !ret_layout.is_zst())
321+
.map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack));
322+
311323
ecx.push_stack_frame(
312-
Instance::new(def_id, &InternalSubsts::identity_for_item(tcx, def_id)),
324+
Instance::new(def_id, substs),
313325
span,
314326
dummy_body,
315-
None,
327+
ret.map(Into::into),
316328
StackPopCleanup::None {
317329
cleanup: false,
318330
},
@@ -327,6 +339,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
327339
source_scope_local_data,
328340
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
329341
local_decls: body.local_decls.clone(),
342+
ret: ret.map(Into::into),
330343
}
331344
}
332345

@@ -335,6 +348,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
335348
}
336349

337350
fn get_const(&self, local: Local) -> Option<Const<'tcx>> {
351+
if local == RETURN_PLACE {
352+
// Try to read the return place as an immediate so that if it is representable as a
353+
// scalar, we can handle it as such, but otherwise, just return the value as is.
354+
return match self.ret.map(|ret| self.ecx.try_read_immediate(ret)) {
355+
Some(Ok(Ok(imm))) => Some(imm.into()),
356+
_ => self.ret,
357+
};
358+
}
359+
338360
self.ecx.access_local(self.ecx.frame(), local, None).ok()
339361
}
340362

@@ -643,7 +665,8 @@ impl CanConstProp {
643665
// lint for x != y
644666
// FIXME(oli-obk): lint variables until they are used in a condition
645667
// FIXME(oli-obk): lint if return value is constant
646-
*val = body.local_kind(local) == LocalKind::Temp;
668+
let local_kind = body.local_kind(local);
669+
*val = local_kind == LocalKind::Temp || local_kind == LocalKind::ReturnPointer;
647670

648671
if !*val {
649672
trace!("local {:?} can't be propagated because it's not a temporary", local);
@@ -731,7 +754,9 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
731754
}
732755
} else {
733756
trace!("can't propagate into {:?}", local);
734-
self.remove_const(local);
757+
if local != RETURN_PLACE {
758+
self.remove_const(local);
759+
}
735760
}
736761
}
737762
}

src/librustc_mir/transform/simplify.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,20 @@ impl<'a, 'tcx> Visitor<'tcx> for DeclMarker<'a, 'tcx> {
359359
// Ignore stores of constants because `ConstProp` and `CopyProp` can remove uses of many
360360
// of these locals. However, if the local is still needed, then it will be referenced in
361361
// another place and we'll mark it as being used there.
362-
if ctx == PlaceContext::MutatingUse(MutatingUseContext::Store) {
363-
let stmt =
364-
&self.body.basic_blocks()[location.block].statements[location.statement_index];
365-
if let StatementKind::Assign(box (p, Rvalue::Use(Operand::Constant(c)))) = &stmt.kind {
366-
if p.as_local().is_some() {
367-
trace!("skipping store of const value {:?} to {:?}", c, local);
368-
return;
362+
if ctx == PlaceContext::MutatingUse(MutatingUseContext::Store) ||
363+
ctx == PlaceContext::MutatingUse(MutatingUseContext::Projection) {
364+
let block = &self.body.basic_blocks()[location.block];
365+
if location.statement_index != block.statements.len() {
366+
let stmt =
367+
&block.statements[location.statement_index];
368+
369+
if let StatementKind::Assign(
370+
box (p, Rvalue::Use(Operand::Constant(c)))
371+
) = &stmt.kind {
372+
if !p.is_indirect() {
373+
trace!("skipping store of const value {:?} to {:?}", c, p);
374+
return;
375+
}
369376
}
370377
}
371378
}
@@ -392,7 +399,7 @@ impl<'tcx> MutVisitor<'tcx> for LocalUpdater<'tcx> {
392399
self.map[*l].is_some()
393400
}
394401
StatementKind::Assign(box (place, _)) => {
395-
if let Some(local) = place.as_local() {
402+
if let PlaceBase::Local(local) = place.base {
396403
self.map[local].is_some()
397404
} else {
398405
true

src/test/incremental/hashes/struct_constructors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn change_constructor_path_regular_struct() {
152152
}
153153

154154
#[cfg(not(cfail1))]
155-
#[rustc_clean(cfg="cfail2", except="HirBody,optimized_mir,mir_built,typeck_tables_of")]
155+
#[rustc_clean(cfg="cfail2", except="HirBody,mir_built,typeck_tables_of")]
156156
#[rustc_clean(cfg="cfail3")]
157157
pub fn change_constructor_path_regular_struct() {
158158
let _ = RegularStruct2 {
@@ -213,7 +213,7 @@ pub fn change_constructor_path_tuple_struct() {
213213
}
214214

215215
#[cfg(not(cfail1))]
216-
#[rustc_clean(cfg="cfail2", except="HirBody,optimized_mir,mir_built,typeck_tables_of")]
216+
#[rustc_clean(cfg="cfail2", except="HirBody,mir_built,typeck_tables_of")]
217217
#[rustc_clean(cfg="cfail3")]
218218
pub fn change_constructor_path_tuple_struct() {
219219
let _ = TupleStruct2(0, 1, 2);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// compile-flags: -C overflow-checks=on
2+
3+
fn add() -> u32 {
4+
2 + 2
5+
}
6+
7+
fn main() {
8+
add();
9+
}
10+
11+
// END RUST SOURCE
12+
// START rustc.add.ConstProp.before.mir
13+
// fn add() -> u32 {
14+
// let mut _0: u32;
15+
// let mut _1: (u32, bool);
16+
// bb0: {
17+
// _1 = CheckedAdd(const 2u32, const 2u32);
18+
// assert(!move (_1.1: bool), "attempt to add with overflow") -> bb1;
19+
// }
20+
// bb1: {
21+
// _0 = move (_1.0: u32);
22+
// return;
23+
// }
24+
// bb2 (cleanup): {
25+
// resume;
26+
// }
27+
// }
28+
// END rustc.add.ConstProp.before.mir
29+
// START rustc.add.ConstProp.after.mir
30+
// fn add() -> u32 {
31+
// let mut _0: u32;
32+
// let mut _1: (u32, bool);
33+
// bb0: {
34+
// _1 = (const 4u32, const false);
35+
// assert(!const false, "attempt to add with overflow") -> bb1;
36+
// }
37+
// bb1: {
38+
// _0 = const 4u32;
39+
// return;
40+
// }
41+
// bb2 (cleanup): {
42+
// resume;
43+
// }
44+
// }
45+
// END rustc.add.ConstProp.after.mir
46+
// START rustc.add.PreCodegen.before.mir
47+
// fn add() -> u32 {
48+
// let mut _0: u32;
49+
// bb0: {
50+
// _0 = const 4u32;
51+
// return;
52+
// }
53+
// }
54+
// END rustc.add.PreCodegen.before.mir

0 commit comments

Comments
 (0)