Update passes with new interface

This commit is contained in:
Dylan MacKenzie 2021-12-02 09:17:32 -08:00
parent c1a501b131
commit fd18b45e11
32 changed files with 142 additions and 99 deletions

View file

@ -41,6 +41,10 @@ pub struct PromoteTemps<'tcx> {
} }
impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> { impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
fn phase_change(&self) -> Option<MirPhase> {
Some(MirPhase::ConstPromotion)
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// There's not really any point in promoting errorful MIR. // There's not really any point in promoting errorful MIR.
// //

View file

@ -20,6 +20,7 @@ use crate::{Analysis, JoinSemiLattice, Results, ResultsCursor};
pub struct SanityCheck; pub struct SanityCheck;
// FIXME: This should be a `MirLint`, but it needs to be moved back to `rustc_mir_transform` first.
impl<'tcx> MirPass<'tcx> for SanityCheck { impl<'tcx> MirPass<'tcx> for SanityCheck {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
use crate::has_rustc_mir_with; use crate::has_rustc_mir_with;

View file

@ -58,11 +58,11 @@ fn may_be_reference(ty: Ty<'tcx>) -> bool {
} }
impl<'tcx> MirPass<'tcx> for AddRetag { impl<'tcx> MirPass<'tcx> for AddRetag {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if !tcx.sess.opts.debugging_opts.mir_emit_retag { sess.opts.debugging_opts.mir_emit_retag
return; }
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// We need an `AllCallEdges` pass before we can do any work. // We need an `AllCallEdges` pass before we can do any work.
super::add_call_guards::AllCallEdges.run_pass(tcx, body); super::add_call_guards::AllCallEdges.run_pass(tcx, body);

View file

@ -6,12 +6,12 @@ use rustc_middle::ty::TyCtxt;
use rustc_session::lint::builtin::CONST_ITEM_MUTATION; use rustc_session::lint::builtin::CONST_ITEM_MUTATION;
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
use crate::MirPass; use crate::MirLint;
pub struct CheckConstItemMutation; pub struct CheckConstItemMutation;
impl<'tcx> MirPass<'tcx> for CheckConstItemMutation { impl<'tcx> MirLint<'tcx> for CheckConstItemMutation {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let mut checker = ConstMutationChecker { body, tcx, target_local: None }; let mut checker = ConstMutationChecker { body, tcx, target_local: None };
checker.visit_body(&body); checker.visit_body(&body);
} }

View file

@ -7,7 +7,7 @@ use rustc_session::lint::builtin::UNALIGNED_REFERENCES;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use crate::util; use crate::util;
use crate::MirPass; use crate::MirLint;
pub(crate) fn provide(providers: &mut Providers) { pub(crate) fn provide(providers: &mut Providers) {
*providers = Providers { unsafe_derive_on_repr_packed, ..*providers }; *providers = Providers { unsafe_derive_on_repr_packed, ..*providers };
@ -15,8 +15,8 @@ pub(crate) fn provide(providers: &mut Providers) {
pub struct CheckPackedRef; pub struct CheckPackedRef;
impl<'tcx> MirPass<'tcx> for CheckPackedRef { impl<'tcx> MirLint<'tcx> for CheckPackedRef {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let param_env = tcx.param_env(body.source.def_id()); let param_env = tcx.param_env(body.source.def_id());
let source_info = SourceInfo::outermost(body.span); let source_info = SourceInfo::outermost(body.span);
let mut checker = PackedRefChecker { body, tcx, param_env, source_info }; let mut checker = PackedRefChecker { body, tcx, param_env, source_info };

View file

@ -15,11 +15,11 @@ use rustc_index::{bit_set::BitSet, vec::IndexVec};
pub struct ConstDebugInfo; pub struct ConstDebugInfo;
impl<'tcx> MirPass<'tcx> for ConstDebugInfo { impl<'tcx> MirPass<'tcx> for ConstDebugInfo {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if !tcx.sess.opts.debugging_opts.unsound_mir_opts { sess.opts.debugging_opts.unsound_mir_opts && sess.mir_opt_level() > 0
return; }
}
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
trace!("running ConstDebugInfo on {:?}", body.source); trace!("running ConstDebugInfo on {:?}", body.source);
for (local, constant) in find_optimization_oportunities(body) { for (local, constant) in find_optimization_oportunities(body) {

View file

@ -27,10 +27,11 @@ use super::simplify::{simplify_cfg, simplify_locals};
pub struct ConstGoto; pub struct ConstGoto;
impl<'tcx> MirPass<'tcx> for ConstGoto { impl<'tcx> MirPass<'tcx> for ConstGoto {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 4
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if tcx.sess.mir_opt_level() < 4 {
return;
}
trace!("Running ConstGoto on {:?}", body.source); trace!("Running ConstGoto on {:?}", body.source);
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id()); let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
let mut opt_finder = let mut opt_finder =

View file

@ -62,6 +62,13 @@ macro_rules! throw_machine_stop_str {
pub struct ConstProp; pub struct ConstProp;
impl<'tcx> MirPass<'tcx> for ConstProp { impl<'tcx> MirPass<'tcx> for ConstProp {
fn is_enabled(&self, _sess: &rustc_session::Session) -> bool {
// FIXME(#70073): Unlike the other passes in "optimizations", this one emits errors, so it
// runs even when MIR optimizations are disabled. We should separate the lint out from the
// transform and move the lint as early in the pipeline as possible.
true
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// will be evaluated by miri and produce its errors there // will be evaluated by miri and produce its errors there
if body.source.promoted.is_some() { if body.source.promoted.is_some() {

View file

@ -49,6 +49,10 @@ impl Error {
pub struct InstrumentCoverage; pub struct InstrumentCoverage;
impl<'tcx> MirPass<'tcx> for InstrumentCoverage { impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.instrument_coverage()
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
let mir_source = mir_body.source; let mir_source = mir_body.source;

View file

@ -15,10 +15,11 @@ use super::simplify::simplify_cfg;
pub struct DeduplicateBlocks; pub struct DeduplicateBlocks;
impl<'tcx> MirPass<'tcx> for DeduplicateBlocks { impl<'tcx> MirPass<'tcx> for DeduplicateBlocks {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 4
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if tcx.sess.mir_opt_level() < 4 {
return;
}
debug!("Running DeduplicateBlocks on `{:?}`", body.source); debug!("Running DeduplicateBlocks on `{:?}`", body.source);
let duplicates = find_duplicates(body); let duplicates = find_duplicates(body);
let has_opts_to_apply = !duplicates.is_empty(); let has_opts_to_apply = !duplicates.is_empty();

View file

@ -124,18 +124,15 @@ const MAX_BLOCKS: usize = 250;
pub struct DestinationPropagation; pub struct DestinationPropagation;
impl<'tcx> MirPass<'tcx> for DestinationPropagation { impl<'tcx> MirPass<'tcx> for DestinationPropagation {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
// FIXME(#79191, #82678) // FIXME(#79191, #82678): This is unsound.
if !tcx.sess.opts.debugging_opts.unsound_mir_opts { //
return;
}
// Only run at mir-opt-level=3 or higher for now (we don't fix up debuginfo and remove // Only run at mir-opt-level=3 or higher for now (we don't fix up debuginfo and remove
// storage statements at the moment). // storage statements at the moment).
if tcx.sess.mir_opt_level() < 3 { sess.opts.debugging_opts.unsound_mir_opts && sess.mir_opt_level() >= 3
return; }
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let def_id = body.source.def_id(); let def_id = body.source.def_id();
let candidates = find_candidates(tcx, body); let candidates = find_candidates(tcx, body);

View file

@ -25,16 +25,14 @@ use super::simplify::simplify_cfg;
pub struct EarlyOtherwiseBranch; pub struct EarlyOtherwiseBranch;
impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
// FIXME(#78496) // FIXME(#78496)
if !tcx.sess.opts.debugging_opts.unsound_mir_opts { sess.opts.debugging_opts.unsound_mir_opts && sess.mir_opt_level() >= 3
return; }
}
if tcx.sess.mir_opt_level() < 3 { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
return;
}
trace!("running EarlyOtherwiseBranch on {:?}", body.source); trace!("running EarlyOtherwiseBranch on {:?}", body.source);
// we are only interested in this bb if the terminator is a switchInt // we are only interested in this bb if the terminator is a switchInt
let bbs_with_switch = let bbs_with_switch =
body.basic_blocks().iter_enumerated().filter(|(_, bb)| is_switch(bb.terminator())); body.basic_blocks().iter_enumerated().filter(|(_, bb)| is_switch(bb.terminator()));

View file

@ -19,6 +19,10 @@ use std::fmt;
pub struct ElaborateDrops; pub struct ElaborateDrops;
impl<'tcx> MirPass<'tcx> for ElaborateDrops { impl<'tcx> MirPass<'tcx> for ElaborateDrops {
fn phase_change(&self) -> Option<MirPhase> {
Some(MirPhase::DropLowering)
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span); debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);

View file

@ -11,12 +11,12 @@ use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES;
use rustc_span::{symbol::sym, Span}; use rustc_span::{symbol::sym, Span};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use crate::MirPass; use crate::MirLint;
pub struct FunctionItemReferences; pub struct FunctionItemReferences;
impl<'tcx> MirPass<'tcx> for FunctionItemReferences { impl<'tcx> MirLint<'tcx> for FunctionItemReferences {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let mut checker = FunctionItemRefChecker { tcx, body }; let mut checker = FunctionItemRefChecker { tcx, body };
checker.visit_body(&body); checker.visit_body(&body);
} }

View file

@ -1232,6 +1232,10 @@ fn create_cases<'tcx>(
} }
impl<'tcx> MirPass<'tcx> for StateTransform { impl<'tcx> MirPass<'tcx> for StateTransform {
fn phase_change(&self) -> Option<MirPhase> {
Some(MirPhase::GeneratorLowering)
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let yield_ty = if let Some(yield_ty) = body.yield_ty() { let yield_ty = if let Some(yield_ty) = body.yield_ty() {
yield_ty yield_ty

View file

@ -37,21 +37,16 @@ struct CallSite<'tcx> {
source_info: SourceInfo, source_info: SourceInfo,
} }
/// Returns true if MIR inlining is enabled in the current compilation session.
crate fn is_enabled(tcx: TyCtxt<'_>) -> bool {
if let Some(enabled) = tcx.sess.opts.debugging_opts.inline_mir {
return enabled;
}
tcx.sess.mir_opt_level() >= 3
}
impl<'tcx> MirPass<'tcx> for Inline { impl<'tcx> MirPass<'tcx> for Inline {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if !is_enabled(tcx) { if let Some(enabled) = sess.opts.debugging_opts.inline_mir {
return; return enabled;
} }
sess.opts.mir_opt_level() >= 3
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id())); let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
let _guard = span.enter(); let _guard = span.enter();
if inline(tcx, body) { if inline(tcx, body) {

View file

@ -11,6 +11,10 @@ use rustc_middle::ty::{self, TyCtxt};
pub struct InstCombine; pub struct InstCombine;
impl<'tcx> MirPass<'tcx> for InstCombine { impl<'tcx> MirPass<'tcx> for InstCombine {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut(); let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
let ctx = InstCombineContext { tcx, local_decls }; let ctx = InstCombineContext { tcx, local_decls };

View file

@ -290,12 +290,12 @@ fn mir_const<'tcx>(
MirPhase::Const, MirPhase::Const,
&[&[ &[&[
// MIR-level lints. // MIR-level lints.
&check_packed_ref::CheckPackedRef, &Lint(check_packed_ref::CheckPackedRef),
&check_const_item_mutation::CheckConstItemMutation, &Lint(check_const_item_mutation::CheckConstItemMutation),
&function_item_references::FunctionItemReferences, &Lint(function_item_references::FunctionItemReferences),
// What we need to do constant evaluation. // What we need to do constant evaluation.
&simplify::SimplifyCfg::new("initial"), &simplify::SimplifyCfg::new("initial"),
&rustc_peek::SanityCheck, &rustc_peek::SanityCheck, // Just a lint
]], ]],
); );
tcx.alloc_steal_mir(body) tcx.alloc_steal_mir(body)
@ -443,7 +443,7 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
let def = ty::WithOptConstParam::unknown(did); let def = ty::WithOptConstParam::unknown(did);
// Do not compute the mir call graph without said call graph actually being used. // Do not compute the mir call graph without said call graph actually being used.
if inline::is_enabled(tcx) { if inline::Inline.is_enabled(&tcx.sess) {
let _ = tcx.mir_inliner_callees(ty::InstanceDef::Item(def)); let _ = tcx.mir_inliner_callees(ty::InstanceDef::Item(def));
} }
} }

View file

@ -10,6 +10,10 @@ use rustc_middle::ty::{self, TyCtxt};
pub struct LowerSliceLenCalls; pub struct LowerSliceLenCalls;
impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls { impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.opts.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
lower_slice_len_calls(tcx, body) lower_slice_len_calls(tcx, body)
} }

View file

@ -40,11 +40,11 @@ pub struct MatchBranchSimplification;
/// ``` /// ```
impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if tcx.sess.mir_opt_level() < 3 { sess.mir_opt_level() >= 3
return; }
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let def_id = body.source.def_id(); let def_id = body.source.def_id();
let param_env = tcx.param_env(def_id); let param_env = tcx.param_env(def_id);

View file

@ -9,11 +9,11 @@ use rustc_middle::ty::TyCtxt;
pub struct MultipleReturnTerminators; pub struct MultipleReturnTerminators;
impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators { impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if tcx.sess.mir_opt_level() < 4 { sess.mir_opt_level() >= 4
return; }
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// find basic blocks with no statement and a return terminator // find basic blocks with no statement and a return terminator
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len()); let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len());
let def_id = body.source.def_id(); let def_id = body.source.def_id();

View file

@ -14,11 +14,11 @@ const MAX_NUM_LOCALS: usize = 3000;
pub struct NormalizeArrayLen; pub struct NormalizeArrayLen;
impl<'tcx> MirPass<'tcx> for NormalizeArrayLen { impl<'tcx> MirPass<'tcx> for NormalizeArrayLen {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if tcx.sess.mir_opt_level() < 4 { sess.mir_opt_level() >= 4
return; }
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// early returns for edge cases of highly unrolled functions // early returns for edge cases of highly unrolled functions
if body.basic_blocks().len() > MAX_NUM_BLOCKS { if body.basic_blocks().len() > MAX_NUM_BLOCKS {
return; return;

View file

@ -33,11 +33,11 @@ use crate::MirPass;
pub struct RenameReturnPlace; pub struct RenameReturnPlace;
impl<'tcx> MirPass<'tcx> for RenameReturnPlace { impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if tcx.sess.mir_opt_level() == 0 { sess.mir_opt_level() > 0
return; }
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
let def_id = body.source.def_id(); let def_id = body.source.def_id();
let returned_local = match local_eligible_for_nrvo(body) { let returned_local = match local_eligible_for_nrvo(body) {
Some(l) => l, Some(l) => l,

View file

@ -10,18 +10,14 @@ use rustc_target::spec::PanicStrategy;
/// code for these. /// code for these.
pub struct RemoveNoopLandingPads; pub struct RemoveNoopLandingPads;
pub fn remove_noop_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if tcx.sess.panic_strategy() == PanicStrategy::Abort {
return;
}
debug!("remove_noop_landing_pads({:?})", body);
RemoveNoopLandingPads.remove_nop_landing_pads(body)
}
impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads { impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
remove_noop_landing_pads(tcx, body); sess.panic_strategy() != PanicStrategy::Abort
}
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!("remove_noop_landing_pads({:?})", body);
self.remove_nop_landing_pads(body)
} }
} }

View file

@ -7,6 +7,10 @@ use rustc_middle::ty::TyCtxt;
pub struct RemoveStorageMarkers; pub struct RemoveStorageMarkers;
impl<'tcx> MirPass<'tcx> for RemoveStorageMarkers { impl<'tcx> MirPass<'tcx> for RemoveStorageMarkers {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if tcx.sess.emit_lifetime_markers() { if tcx.sess.emit_lifetime_markers() {
return; return;

View file

@ -8,6 +8,10 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
pub struct RemoveZsts; pub struct RemoveZsts;
impl<'tcx> MirPass<'tcx> for RemoveZsts { impl<'tcx> MirPass<'tcx> for RemoveZsts {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// Avoid query cycles (generators require optimized MIR for layout). // Avoid query cycles (generators require optimized MIR for layout).
if tcx.type_of(body.source.def_id()).is_generator() { if tcx.type_of(body.source.def_id()).is_generator() {

View file

@ -8,15 +8,18 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
pub struct RevealAll; pub struct RevealAll;
impl<'tcx> MirPass<'tcx> for RevealAll { impl<'tcx> MirPass<'tcx> for RevealAll {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.opts.mir_opt_level() >= 3 || super::inline::Inline.is_enabled(sess)
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// This pass must run before inlining, since we insert callee bodies in RevealAll mode.
// Do not apply this transformation to generators. // Do not apply this transformation to generators.
if (tcx.sess.mir_opt_level() >= 3 || super::inline::is_enabled(tcx)) if body.generator.is_some() {
&& body.generator.is_none() return;
{
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
RevealAllVisitor { tcx, param_env }.visit_body(body);
} }
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
RevealAllVisitor { tcx, param_env }.visit_body(body);
} }
} }

View file

@ -45,11 +45,11 @@ use smallvec::SmallVec;
pub struct SeparateConstSwitch; pub struct SeparateConstSwitch;
impl<'tcx> MirPass<'tcx> for SeparateConstSwitch { impl<'tcx> MirPass<'tcx> for SeparateConstSwitch {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if tcx.sess.mir_opt_level() < 4 { sess.mir_opt_level() >= 4
return; }
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// If execution did something, applying a simplification layer // If execution did something, applying a simplification layer
// helps later passes optimize the copy away. // helps later passes optimize the copy away.
if separate_const_switch(body) > 0 { if separate_const_switch(body) > 0 {

View file

@ -368,6 +368,10 @@ fn save_unreachable_coverage(
pub struct SimplifyLocals; pub struct SimplifyLocals;
impl<'tcx> MirPass<'tcx> for SimplifyLocals { impl<'tcx> MirPass<'tcx> for SimplifyLocals {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
trace!("running SimplifyLocals on {:?}", body.source); trace!("running SimplifyLocals on {:?}", body.source);
simplify_locals(body, tcx); simplify_locals(body, tcx);

View file

@ -26,6 +26,10 @@ use rustc_middle::{
pub struct SimplifyComparisonIntegral; pub struct SimplifyComparisonIntegral;
impl<'tcx> MirPass<'tcx> for SimplifyComparisonIntegral { impl<'tcx> MirPass<'tcx> for SimplifyComparisonIntegral {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
trace!("Running SimplifyComparisonIntegral on {:?}", body.source); trace!("Running SimplifyComparisonIntegral on {:?}", body.source);

View file

@ -70,6 +70,10 @@ fn variant_discriminants<'tcx>(
} }
impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching { impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if body.source.promoted.is_some() { if body.source.promoted.is_some() {
return; return;

View file

@ -11,13 +11,13 @@ use rustc_middle::ty::TyCtxt;
pub struct UnreachablePropagation; pub struct UnreachablePropagation;
impl MirPass<'_> for UnreachablePropagation { impl MirPass<'_> for UnreachablePropagation {
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if tcx.sess.mir_opt_level() < 4 { // Enable only under -Zmir-opt-level=4 as in some cases (check the deeply-nested-opt
// Enable only under -Zmir-opt-level=4 as in some cases (check the deeply-nested-opt // perf benchmark) LLVM may spend quite a lot of time optimizing the generated code.
// perf benchmark) LLVM may spend quite a lot of time optimizing the generated code. sess.mir_opt_level() >= 4
return; }
}
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let mut unreachable_blocks = FxHashSet::default(); let mut unreachable_blocks = FxHashSet::default();
let mut replacements = FxHashMap::default(); let mut replacements = FxHashMap::default();