]> git.lizzy.rs Git - rust.git/commitdiff
add optimization fuel checks to some mir passes
authorcjkenn <cjkenn@users.noreply.github.com>
Mon, 16 Nov 2020 23:09:10 +0000 (18:09 -0500)
committercjkenn <cjkenn@users.noreply.github.com>
Mon, 16 Nov 2020 23:09:10 +0000 (18:09 -0500)
compiler/rustc_mir/src/transform/const_prop.rs
compiler/rustc_mir/src/transform/early_otherwise_branch.rs
compiler/rustc_mir/src/transform/instcombine.rs
compiler/rustc_mir/src/transform/match_branches.rs
compiler/rustc_mir/src/transform/multiple_return_terminators.rs
compiler/rustc_mir/src/transform/nrvo.rs
compiler/rustc_mir/src/transform/promote_consts.rs
compiler/rustc_mir/src/transform/remove_unneeded_drops.rs
compiler/rustc_mir/src/transform/unreachable_prop.rs

index aeb9920c0e321c36c8f804c32211c992f0b0203b..1f266a95f03f6536547f82b73681d560e79924b9 100644 (file)
@@ -89,6 +89,10 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
             return;
         }
 
+        if !tcx.consider_optimizing(|| format!("ConstantPropagation {:?} {:?}", def_id, hir_id)) {
+            return;
+        }
+
         // Check if it's even possible to satisfy the 'where' clauses
         // for this item.
         // This branch will never be taken for any normal function.
index f97dcf4852df477a398485fce1207949200a4329..f91477911a489015363ab72bcd08ca9c6dbfd1aa 100644 (file)
@@ -46,6 +46,10 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         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)) {
+                break;
+            }
+
             trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply);
 
             let statements_before =
index 59b7db243190081361b77f2e458e4422d412d46e..5a21cdc6891f4020797a7e69673a0a0dbabde001 100644 (file)
 
 impl<'tcx> MirPass<'tcx> for InstCombine {
     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
+        // Check for fuel here before gathering the optimization list. If we're out of fuel,
+        // we don't want to take the time to pass over the MIR only to find optimizations
+        // we won't run.
+        if !tcx.consider_optimizing(|| format!("InstCombine {:?} ", body.source.def_id())) {
+            return;
+        }
+
         // First, find optimization opportunities. This is done in a pre-pass to keep the MIR
         // read-only so that we can do global analyses on the MIR in the process (e.g.
         // `Place::ty()`).
index 82c0b924f287e3330facfed2e52b6bb43d654249..53eeecc780f6f1ed628c1ec766ac147b24aa3d1f 100644 (file)
@@ -43,8 +43,13 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         }
 
         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(_)),
index c37b54a3190f8fb7e6eb736b89d1341a59a968ea..4a66bc45ae37b959319ba7d3545da06663cb266e 100644 (file)
@@ -14,6 +14,12 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
             return;
         }
 
+        if !tcx.consider_optimizing(|| {
+            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();
index 45b906bf542d5a37703c98e1d1ad7b039020664d..ce02fb261df6f981991fa955cc3772d72861fe65 100644 (file)
@@ -38,18 +38,22 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
             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);
index 927aae82a36efab1c3cb9a66b4abd18548a453d0..0539d8fffabc8413bb5c53dd94d6ea8b708899a5 100644 (file)
@@ -60,6 +60,10 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
             return;
         }
 
+        if !tcx.consider_optimizing(|| format!("PromoteTemps {:?} ", body.source.def_id())) {
+            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);
index aaf3ecab4dca728ac3302d78f0fc7a0307369a14..2a72751071bb1907b94dc86cf0bb477780a9d437 100644 (file)
 
 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())) {
+            return;
+        }
+
         trace!("Running RemoveUnneededDrops on {:?}", body.source);
         let mut opt_finder = RemoveUnneededDropsOptimizationFinder {
             tcx,
index f6d39dae3429e18b2651c73abeedb3b38e5454b6..4100a50c225b35fe4646d0e0c67d615ecd9eda8e 100644 (file)
@@ -18,6 +18,12 @@ fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
             return;
         }
 
+        if !tcx
+            .consider_optimizing(|| format!("UnreachablePropagation {:?} ", body.source.def_id()))
+        {
+            return;
+        }
+
         let mut unreachable_blocks = FxHashSet::default();
         let mut replacements = FxHashMap::default();