Skip to content

Commit 51c2218

Browse files
committed
move fuel checks to later points in instcombine and const_prop, add opt level flag to test
1 parent b4c9424 commit 51c2218

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

compiler/rustc_mir/src/transform/const_prop.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
8989
return;
9090
}
9191

92-
if !tcx.consider_optimizing(|| format!("ConstantPropagation {:?} {:?}", def_id, hir_id)) {
93-
return;
94-
}
95-
9692
// Check if it's even possible to satisfy the 'where' clauses
9793
// for this item.
9894
// This branch will never be taken for any normal function.
@@ -804,7 +800,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
804800
}
805801
}
806802

807-
trace!("attepting to replace {:?} with {:?}", rval, value);
803+
trace!("attempting to replace {:?} with {:?}", rval, value);
808804
if let Err(e) = self.ecx.const_validate_operand(
809805
value,
810806
vec![],
@@ -894,6 +890,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
894890
return false;
895891
}
896892

893+
if !self.tcx.consider_optimizing(|| format!("ConstantPropagation - OpTy: {:?}", op)) {
894+
return false;
895+
}
896+
897897
match *op {
898898
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUninit::Scalar(s))) => {
899899
s.is_bits()

compiler/rustc_mir/src/transform/instcombine.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ pub struct InstCombine;
2020

2121
impl<'tcx> MirPass<'tcx> for InstCombine {
2222
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
23-
// Check for fuel here before gathering the optimization list. If we're out of fuel,
24-
// we don't want to take the time to pass over the MIR only to find optimizations
25-
// we won't run.
26-
if !tcx.consider_optimizing(|| format!("InstCombine {:?} ", body.source.def_id())) {
27-
return;
28-
}
29-
3023
// First, find optimization opportunities. This is done in a pre-pass to keep the MIR
3124
// read-only so that we can do global analyses on the MIR in the process (e.g.
3225
// `Place::ty()`).
@@ -46,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
4639
tcx: TyCtxt<'tcx>,
4740
}
4841

42+
impl<'tcx> InstCombineVisitor<'tcx> {
43+
fn should_combine(&self, rvalue: &Rvalue<'tcx>, location: Location) -> bool {
44+
self.tcx.consider_optimizing(|| {
45+
format!("InstCombine - Rvalue: {:?} Location: {:?}", rvalue, location)
46+
})
47+
}
48+
}
49+
4950
impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
5051
fn tcx(&self) -> TyCtxt<'tcx> {
5152
self.tcx
5253
}
5354

5455
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
55-
if self.optimizations.and_stars.remove(&location) {
56+
if self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
5657
debug!("replacing `&*`: {:?}", rvalue);
5758
let new_place = match rvalue {
5859
Rvalue::Ref(_, _, place) => {
@@ -74,18 +75,24 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
7475
}
7576

7677
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
77-
debug!("replacing `Len([_; N])`: {:?}", rvalue);
78-
*rvalue = Rvalue::Use(Operand::Constant(box constant));
78+
if self.should_combine(rvalue, location) {
79+
debug!("replacing `Len([_; N])`: {:?}", rvalue);
80+
*rvalue = Rvalue::Use(Operand::Constant(box constant));
81+
}
7982
}
8083

8184
if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
82-
debug!("replacing {:?} with {:?}", rvalue, operand);
83-
*rvalue = Rvalue::Use(operand);
85+
if self.should_combine(rvalue, location) {
86+
debug!("replacing {:?} with {:?}", rvalue, operand);
87+
*rvalue = Rvalue::Use(operand);
88+
}
8489
}
8590

8691
if let Some(place) = self.optimizations.unneeded_deref.remove(&location) {
87-
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
88-
*rvalue = Rvalue::Use(Operand::Copy(place));
92+
if self.should_combine(rvalue, location) {
93+
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
94+
*rvalue = Rvalue::Use(Operand::Copy(place));
95+
}
8996
}
9097

9198
self.super_rvalue(rvalue, location)

src/test/ui/print-fuel/print-fuel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(dead_code)]
33

44
// (#55495: The --error-format is to sidestep an issue in our test harness)
5-
// compile-flags: --error-format human -Z print-fuel=foo
5+
// compile-flags: -C opt-level=0 --error-format human -Z print-fuel=foo
66
// build-pass (FIXME(62277): could be check-pass?)
77

88
struct S1(u8, u16, u8);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fuel used by foo: 7
1+
Fuel used by foo: 6

0 commit comments

Comments
 (0)