Skip to content

Commit db5fc10

Browse files
committed
[mir-opt] Turn on the ConstProp pass by default
perf.rlo shows that running the `ConstProp` pass results in across-the-board wins regardless of debug or opt complilation mode. As a result, we're turning it on to get the compile time benefits. `ConstProp` doesn't currently intern the memory used by its `Machine` so we can't yet propagate allocations which is why `ConstProp::should_const_prop()` checks if the value being propagated is a scalar or not.
1 parent bc0e288 commit db5fc10

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

src/librustc_mir/transform/const_prop.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
641641
}
642642
}
643643

644-
fn should_const_prop(&self) -> bool {
645-
self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
644+
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
645+
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
646+
return true;
647+
} else if self.tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
648+
return false;
649+
}
650+
651+
match *op {
652+
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Scalar(s))) =>
653+
s.is_bits(),
654+
interpret::Operand::Immediate(Immediate::ScalarPair(ScalarMaybeUndef::Scalar(l),
655+
ScalarMaybeUndef::Scalar(r))) =>
656+
l.is_bits() && r.is_bits(),
657+
_ => false
658+
}
646659
}
647660
}
648661

@@ -742,15 +755,15 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
742755
if self.can_const_prop[local] {
743756
trace!("propagated into {:?}", local);
744757

745-
if self.should_const_prop() {
746-
let value =
747-
self.get_const(local).expect("local was dead/uninitialized");
748-
trace!("replacing {:?} with {:?}", rval, value);
749-
self.replace_with_const(
750-
rval,
751-
value,
752-
statement.source_info,
753-
);
758+
if let Some(value) = self.get_const(local) {
759+
if self.should_const_prop(value) {
760+
trace!("replacing {:?} with {:?}", rval, value);
761+
self.replace_with_const(
762+
rval,
763+
value,
764+
statement.source_info,
765+
);
766+
}
754767
}
755768
} else {
756769
trace!("can't propagate into {:?}", local);
@@ -852,7 +865,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
852865
&msg,
853866
);
854867
} else {
855-
if self.should_const_prop() {
868+
if self.should_const_prop(value) {
856869
if let ScalarMaybeUndef::Scalar(scalar) = value_const {
857870
*cond = self.operand_from_scalar(
858871
scalar,
@@ -865,8 +878,8 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
865878
}
866879
},
867880
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
868-
if self.should_const_prop() {
869-
if let Some(value) = self.eval_operand(&discr, source_info) {
881+
if let Some(value) = self.eval_operand(&discr, source_info) {
882+
if self.should_const_prop(value) {
870883
if let ScalarMaybeUndef::Scalar(scalar) =
871884
self.ecx.read_scalar(value).unwrap() {
872885
*discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);

src/test/codegen/optimize-attr-1.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// CHECK-LABEL: define i32 @nothing
1010
// CHECK-SAME: [[NOTHING_ATTRS:#[0-9]+]]
11-
// NO-OPT: ret i32 %_1.0
11+
// NO-OPT: ret i32 4
1212
// SIZE-OPT: ret i32 4
1313
// SPEEC-OPT: ret i32 4
1414
#[no_mangle]
@@ -18,7 +18,7 @@ pub fn nothing() -> i32 {
1818

1919
// CHECK-LABEL: define i32 @size
2020
// CHECK-SAME: [[SIZE_ATTRS:#[0-9]+]]
21-
// NO-OPT: ret i32 %_1.0
21+
// NO-OPT: ret i32 6
2222
// SIZE-OPT: ret i32 6
2323
// SPEED-OPT: ret i32 6
2424
#[optimize(size)]
@@ -31,7 +31,7 @@ pub fn size() -> i32 {
3131
// NO-OPT-SAME: [[NOTHING_ATTRS]]
3232
// SPEED-OPT-SAME: [[NOTHING_ATTRS]]
3333
// SIZE-OPT-SAME: [[SPEED_ATTRS:#[0-9]+]]
34-
// NO-OPT: ret i32 %_1.0
34+
// NO-OPT: ret i32 8
3535
// SIZE-OPT: ret i32 8
3636
// SPEED-OPT: ret i32 8
3737
#[optimize(speed)]

src/test/incremental/hashes/while_let_loops.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn change_loop_condition() {
4848
}
4949

5050
#[cfg(not(cfail1))]
51-
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
51+
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
5252
#[rustc_clean(cfg="cfail3")]
5353
pub fn change_loop_condition() {
5454
let mut _x = 0;
@@ -70,7 +70,7 @@ pub fn add_break() {
7070
}
7171

7272
#[cfg(not(cfail1))]
73-
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir, typeck_tables_of")]
73+
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, typeck_tables_of")]
7474
#[rustc_clean(cfg="cfail3")]
7575
pub fn add_break() {
7676
let mut _x = 0;
@@ -141,7 +141,7 @@ pub fn change_break_label() {
141141
}
142142

143143
#[cfg(not(cfail1))]
144-
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
144+
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
145145
#[rustc_clean(cfg="cfail3")]
146146
pub fn change_break_label() {
147147
let mut _x = 0;
@@ -191,7 +191,7 @@ pub fn change_continue_label() {
191191
}
192192

193193
#[cfg(not(cfail1))]
194-
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
194+
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
195195
#[rustc_clean(cfg="cfail3")]
196196
pub fn change_continue_label() {
197197
let mut _x = 0;
@@ -216,7 +216,7 @@ pub fn change_continue_to_break() {
216216
}
217217

218218
#[cfg(not(cfail1))]
219-
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
219+
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
220220
#[rustc_clean(cfg="cfail3")]
221221
pub fn change_continue_to_break() {
222222
let mut _x = 0;

src/test/incremental/hashes/while_loops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn change_loop_condition() {
4848
}
4949

5050
#[cfg(not(cfail1))]
51-
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
51+
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
5252
#[rustc_clean(cfg="cfail3")]
5353
pub fn change_loop_condition() {
5454
let mut _x = 0;
@@ -191,7 +191,7 @@ pub fn change_continue_label() {
191191
}
192192

193193
#[cfg(not(cfail1))]
194-
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
194+
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
195195
#[rustc_clean(cfg="cfail3")]
196196
pub fn change_continue_label() {
197197
let mut _x = 0;

0 commit comments

Comments
 (0)