Skip to content

Commit d1da802

Browse files
committed
Auto merge of #66074 - wesleywiser:test_run_const_prop, r=oli-obk
[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.
2 parents 5c5a120 + db5fc10 commit d1da802

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
@@ -648,8 +648,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
648648
}
649649
}
650650

651-
fn should_const_prop(&self) -> bool {
652-
self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
651+
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
652+
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
653+
return true;
654+
} else if self.tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
655+
return false;
656+
}
657+
658+
match *op {
659+
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Scalar(s))) =>
660+
s.is_bits(),
661+
interpret::Operand::Immediate(Immediate::ScalarPair(ScalarMaybeUndef::Scalar(l),
662+
ScalarMaybeUndef::Scalar(r))) =>
663+
l.is_bits() && r.is_bits(),
664+
_ => false
665+
}
653666
}
654667
}
655668

@@ -749,15 +762,15 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
749762
if self.can_const_prop[local] {
750763
trace!("propagated into {:?}", local);
751764

752-
if self.should_const_prop() {
753-
let value =
754-
self.get_const(local).expect("local was dead/uninitialized");
755-
trace!("replacing {:?} with {:?}", rval, value);
756-
self.replace_with_const(
757-
rval,
758-
value,
759-
statement.source_info,
760-
);
765+
if let Some(value) = self.get_const(local) {
766+
if self.should_const_prop(value) {
767+
trace!("replacing {:?} with {:?}", rval, value);
768+
self.replace_with_const(
769+
rval,
770+
value,
771+
statement.source_info,
772+
);
773+
}
761774
}
762775
} else {
763776
trace!("can't propagate into {:?}", local);
@@ -859,7 +872,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
859872
&msg,
860873
);
861874
} else {
862-
if self.should_const_prop() {
875+
if self.should_const_prop(value) {
863876
if let ScalarMaybeUndef::Scalar(scalar) = value_const {
864877
*cond = self.operand_from_scalar(
865878
scalar,
@@ -872,8 +885,8 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
872885
}
873886
},
874887
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
875-
if self.should_const_prop() {
876-
if let Some(value) = self.eval_operand(&discr, source_info) {
888+
if let Some(value) = self.eval_operand(&discr, source_info) {
889+
if self.should_const_prop(value) {
877890
if let ScalarMaybeUndef::Scalar(scalar) =
878891
self.ecx.read_scalar(value).unwrap() {
879892
*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)