Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optimization fuel checks to some mir passes #79117

Merged
merged 7 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_mir/src/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
}
}

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

if !self.tcx.consider_optimizing(|| format!("ConstantPropagation - OpTy: {:?}", op)) {
return false;
}

match *op {
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUninit::Scalar(s))) => {
s.is_bits()
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir/src/transform/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
let should_cleanup = !opts_to_apply.is_empty();

for opt_to_apply in opts_to_apply {
if !tcx.consider_optimizing(|| format!("EarlyOtherwiseBranch {:?}", &opt_to_apply)) {
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
break;
}

trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply);

let statements_before =
Expand Down
28 changes: 21 additions & 7 deletions compiler/rustc_mir/src/transform/instcombine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl<'tcx> InstCombineVisitor<'tcx> {
fn should_combine(&self, rvalue: &Rvalue<'tcx>, location: Location) -> bool {
self.tcx.consider_optimizing(|| {
format!("InstCombine - Rvalue: {:?} Location: {:?}", rvalue, location)
})
}
}

impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

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

if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
debug!("replacing `Len([_; N])`: {:?}", rvalue);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
if self.should_combine(rvalue, location) {
debug!("replacing `Len([_; N])`: {:?}", rvalue);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
}
}

if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
if self.should_combine(rvalue, location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
}
}

if let Some(place) = self.optimizations.unneeded_deref.remove(&location) {
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
*rvalue = Rvalue::Use(Operand::Copy(place));
if self.should_combine(rvalue, location) {
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
*rvalue = Rvalue::Use(Operand::Copy(place));
}
}

self.super_rvalue(rvalue, location)
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_mir/src/transform/match_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
}

let param_env = tcx.param_env(body.source.def_id());
let def_id = body.source.def_id();
let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut();
'outer: for bb_idx in bbs.indices() {
if !tcx.consider_optimizing(|| format!("MatchBranchSimplification {:?} ", def_id)) {
continue;
}

let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind {
TerminatorKind::SwitchInt {
discr: ref discr @ (Operand::Copy(_) | Operand::Move(_)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
return;
}

if !tcx.consider_optimizing(|| {
cjkenn marked this conversation as resolved.
Show resolved Hide resolved
format!("MultipleReturnTerminators {:?} ", body.source.def_id())
}) {
return;
}

// find basic blocks with no statement and a return terminator
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len());
let bbs = body.basic_blocks_mut();
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_mir/src/transform/nrvo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
return;
}

let def_id = body.source.def_id();
let returned_local = match local_eligible_for_nrvo(body) {
Some(l) => l,
None => {
debug!("`{:?}` was ineligible for NRVO", body.source.def_id());
debug!("`{:?}` was ineligible for NRVO", def_id);
return;
}
};

if !tcx.consider_optimizing(|| format!("RenameReturnPlace {:?}", def_id)) {
return;
}

debug!(
"`{:?}` was eligible for NRVO, making {:?} the return place",
body.source.def_id(),
returned_local
def_id, returned_local
);

RenameToReturnPlace { tcx, to_rename: returned_local }.visit_body(body);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir/src/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
return;
}

if !tcx.consider_optimizing(|| format!("PromoteTemps {:?} ", body.source.def_id())) {
cjkenn marked this conversation as resolved.
Show resolved Hide resolved
return;
}

let mut rpo = traversal::reverse_postorder(body);
let ccx = ConstCx::new(tcx, body);
let (temps, all_candidates) = collect_temps_and_candidates(&ccx, &mut rpo);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir/src/transform/remove_unneeded_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub struct RemoveUnneededDrops;

impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if !tcx.consider_optimizing(|| format!("RemoveUnneededDrops {:?} ", body.source.def_id())) {
cjkenn marked this conversation as resolved.
Show resolved Hide resolved
return;
}

trace!("Running RemoveUnneededDrops on {:?}", body.source);
let mut opt_finder = RemoveUnneededDropsOptimizationFinder {
tcx,
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_mir/src/transform/unreachable_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ impl MirPass<'_> for UnreachablePropagation {
return;
}

if !tcx
cjkenn marked this conversation as resolved.
Show resolved Hide resolved
.consider_optimizing(|| format!("UnreachablePropagation {:?} ", body.source.def_id()))
{
return;
}

let mut unreachable_blocks = FxHashSet::default();
let mut replacements = FxHashMap::default();

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/print-fuel/print-fuel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#![allow(dead_code)]

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

struct S1(u8, u16, u8);
struct S2(u8, u16, u8);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/print-fuel/print-fuel.stderr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fuel used by foo: 3
Fuel used by foo: 4