Skip to content

Commit f1b0974

Browse files
authored
Rollup merge of rust-lang#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 ec5e74f + db5fc10 commit f1b0974

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
638638
}
639639
}
640640

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

@@ -739,15 +752,15 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
739752
if self.can_const_prop[local] {
740753
trace!("propagated into {:?}", local);
741754

742-
if self.should_const_prop() {
743-
let value =
744-
self.get_const(local).expect("local was dead/uninitialized");
745-
trace!("replacing {:?} with {:?}", rval, value);
746-
self.replace_with_const(
747-
rval,
748-
value,
749-
statement.source_info,
750-
);
755+
if let Some(value) = self.get_const(local) {
756+
if self.should_const_prop(value) {
757+
trace!("replacing {:?} with {:?}", rval, value);
758+
self.replace_with_const(
759+
rval,
760+
value,
761+
statement.source_info,
762+
);
763+
}
751764
}
752765
} else {
753766
trace!("can't propagate into {:?}", local);
@@ -849,7 +862,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
849862
&msg,
850863
);
851864
} else {
852-
if self.should_const_prop() {
865+
if self.should_const_prop(value) {
853866
if let ScalarMaybeUndef::Scalar(scalar) = value_const {
854867
*cond = self.operand_from_scalar(
855868
scalar,
@@ -862,8 +875,8 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
862875
}
863876
},
864877
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
865-
if self.should_const_prop() {
866-
if let Some(value) = self.eval_operand(&discr, source_info) {
878+
if let Some(value) = self.eval_operand(&discr, source_info) {
879+
if self.should_const_prop(value) {
867880
if let ScalarMaybeUndef::Scalar(scalar) =
868881
self.ecx.read_scalar(value).unwrap() {
869882
*discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 2 additions & 2 deletions
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)