Skip to content

Commit 2fdcd24

Browse files
authored
Rollup merge of #79117 - cjkenn:mir-fuel, r=oli-obk
add optimization fuel checks to some mir passes Fixes #77402 Inserts a bunch of calls to `consider_optimizing`. Note that `consider_optimizing` is the method that actually decrements the fuel count, so the point at which it's called is when the optimization takes place, from a fuel perspective. This means that where we call it has some thought behind it: 1. We probably don't want to decrement the fuel count before other simple checks, otherwise we count an optimization as being performed even if nothing was mutated (ie. it returned early). 2. In cases like `InstCombine`, where we gather optimizations in a pass and then mutate values, we probably would rather skip the gathering pass for performance reasons rather than skip the mutations afterwards.
2 parents 04a4404 + 07de702 commit 2fdcd24

File tree

9 files changed

+61
-13
lines changed

9 files changed

+61
-13
lines changed

compiler/rustc_mir/src/transform/const_prop.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
800800
}
801801
}
802802

803-
trace!("attepting to replace {:?} with {:?}", rval, value);
803+
trace!("attempting to replace {:?} with {:?}", rval, value);
804804
if let Err(e) = self.ecx.const_validate_operand(
805805
value,
806806
vec![],
@@ -890,6 +890,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
890890
return false;
891891
}
892892

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

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
4646
let should_cleanup = !opts_to_apply.is_empty();
4747

4848
for opt_to_apply in opts_to_apply {
49+
if !tcx.consider_optimizing(|| format!("EarlyOtherwiseBranch {:?}", &opt_to_apply)) {
50+
break;
51+
}
52+
4953
trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply);
5054

5155
let statements_before =

compiler/rustc_mir/src/transform/instcombine.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
3939
tcx: TyCtxt<'tcx>,
4040
}
4141

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+
4250
impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
4351
fn tcx(&self) -> TyCtxt<'tcx> {
4452
self.tcx
4553
}
4654

4755
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
48-
if self.optimizations.and_stars.remove(&location) {
56+
if self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
4957
debug!("replacing `&*`: {:?}", rvalue);
5058
let new_place = match rvalue {
5159
Rvalue::Ref(_, _, place) => {
@@ -67,18 +75,24 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
6775
}
6876

6977
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
70-
debug!("replacing `Len([_; N])`: {:?}", rvalue);
71-
*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+
}
7282
}
7383

7484
if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
75-
debug!("replacing {:?} with {:?}", rvalue, operand);
76-
*rvalue = Rvalue::Use(operand);
85+
if self.should_combine(rvalue, location) {
86+
debug!("replacing {:?} with {:?}", rvalue, operand);
87+
*rvalue = Rvalue::Use(operand);
88+
}
7789
}
7890

7991
if let Some(place) = self.optimizations.unneeded_deref.remove(&location) {
80-
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
81-
*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+
}
8296
}
8397

8498
self.super_rvalue(rvalue, location)

compiler/rustc_mir/src/transform/match_branches.rs

+5
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4343
}
4444

4545
let param_env = tcx.param_env(body.source.def_id());
46+
let def_id = body.source.def_id();
4647
let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut();
4748
'outer: for bb_idx in bbs.indices() {
49+
if !tcx.consider_optimizing(|| format!("MatchBranchSimplification {:?} ", def_id)) {
50+
continue;
51+
}
52+
4853
let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind {
4954
TerminatorKind::SwitchInt {
5055
discr: ref discr @ (Operand::Copy(_) | Operand::Move(_)),

compiler/rustc_mir/src/transform/multiple_return_terminators.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
1616

1717
// find basic blocks with no statement and a return terminator
1818
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len());
19+
let def_id = body.source.def_id();
1920
let bbs = body.basic_blocks_mut();
2021
for idx in bbs.indices() {
2122
if bbs[idx].statements.is_empty()
@@ -26,6 +27,10 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
2627
}
2728

2829
for bb in bbs {
30+
if !tcx.consider_optimizing(|| format!("MultipleReturnTerminators {:?} ", def_id)) {
31+
break;
32+
}
33+
2934
if let TerminatorKind::Goto { target } = bb.terminator().kind {
3035
if bbs_simple_returns.contains(target) {
3136
bb.terminator_mut().kind = TerminatorKind::Return;

compiler/rustc_mir/src/transform/nrvo.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
3838
return;
3939
}
4040

41+
let def_id = body.source.def_id();
4142
let returned_local = match local_eligible_for_nrvo(body) {
4243
Some(l) => l,
4344
None => {
44-
debug!("`{:?}` was ineligible for NRVO", body.source.def_id());
45+
debug!("`{:?}` was ineligible for NRVO", def_id);
4546
return;
4647
}
4748
};
4849

50+
if !tcx.consider_optimizing(|| format!("RenameReturnPlace {:?}", def_id)) {
51+
return;
52+
}
53+
4954
debug!(
5055
"`{:?}` was eligible for NRVO, making {:?} the return place",
51-
body.source.def_id(),
52-
returned_local
56+
def_id, returned_local
5357
);
5458

5559
RenameToReturnPlace { tcx, to_rename: returned_local }.visit_body(body);

compiler/rustc_mir/src/transform/remove_unneeded_drops.rs

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
2121
opt_finder.visit_body(body);
2222
let should_simplify = !opt_finder.optimizations.is_empty();
2323
for (loc, target) in opt_finder.optimizations {
24+
if !tcx
25+
.consider_optimizing(|| format!("RemoveUnneededDrops {:?} ", body.source.def_id()))
26+
{
27+
break;
28+
}
29+
2430
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
2531
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
2632
terminator.kind = TerminatorKind::Goto { target };

compiler/rustc_mir/src/transform/unreachable_prop.rs

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ impl MirPass<'_> for UnreachablePropagation {
5050

5151
let replaced = !replacements.is_empty();
5252
for (bb, terminator_kind) in replacements {
53+
if !tcx.consider_optimizing(|| {
54+
format!("UnreachablePropagation {:?} ", body.source.def_id())
55+
}) {
56+
break;
57+
}
58+
5359
body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind;
5460
}
5561

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
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
6-
// build-pass (FIXME(62277): could be check-pass?)
5+
// compile-flags: -C opt-level=0 --error-format human -Z print-fuel=foo
6+
// check-pass
77

88
struct S1(u8, u16, u8);
99
struct S2(u8, u16, u8);

0 commit comments

Comments
 (0)