Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions compiler/rustc_mir_transform/src/abort_unwinding_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use rustc_middle::ty::{self, TyCtxt, layout};
use rustc_span::sym;
use rustc_target::spec::PanicStrategy;

use crate::PassPolicy;

/// A pass that runs which is targeted at ensuring that codegen guarantees about
/// unwinding are upheld for compilations of panic=abort programs.
///
Expand Down Expand Up @@ -138,7 +140,9 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
super::simplify::remove_dead_blocks(body);
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// Implements part of MIR semantics, turning effectively implicit aborts into explicit
// ones.
PassPolicy::Required
}
}
8 changes: 6 additions & 2 deletions compiler/rustc_mir_transform/src/add_call_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use tracing::debug;

use crate::PassPolicy;

#[derive(PartialEq)]
pub(super) enum AddCallGuards {
AllCallEdges,
Expand Down Expand Up @@ -127,8 +129,10 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
basic_blocks.extend(new_blocks);
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// Breaks critical edges so codegen can place edge-specific actions without affecting
// other control-flow edges.
PassPolicy::Required
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_middle::ty::{self, TyCtxt};
use tracing::debug;

use crate::patch::MirPatch;
use crate::util;
use crate::{PassPolicy, util};

/// This pass moves values being dropped that are within a packed
/// struct to a separate local before dropping them, to ensure that
Expand Down Expand Up @@ -70,8 +70,9 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
patch.apply(body);
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// Implements part of MIR semantics by making implicit packed-drop handling explicit.
PassPolicy::Required
}
}

Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_mir_transform/src/add_subtyping_projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustc_middle::mir::visit::MutVisitor;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;

use crate::PassPolicy;
use crate::patch::MirPatch;

pub(super) struct Subtyper;
Expand Down Expand Up @@ -65,7 +66,8 @@ impl<'tcx> crate::MirPass<'tcx> for Subtyper {
checker.patcher.apply(body);
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// Later MIR phases expect all subtyping to be explicit.
PassPolicy::Required
}
}
10 changes: 4 additions & 6 deletions compiler/rustc_mir_transform/src/check_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_session::Session;

use crate::PassPolicy;
use crate::check_pointers::{BorrowedFieldProjectionMode, PointerCheck, check_pointers};

pub(super) struct CheckAlignment;

impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
fn is_enabled(&self, sess: &Session) -> bool {
sess.ub_checks()
fn policy(&self, sess: &Session) -> PassPolicy {
// When UB checks are enabled this is part of their semantics, not an optimization.
PassPolicy::optional_non_optimization(sess.ub_checks())
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand All @@ -31,10 +33,6 @@ impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
BorrowedFieldProjectionMode::FollowProjections,
);
}

fn is_required(&self) -> bool {
true
}
}

/// Inserts the actual alignment check's logic. Returns a
Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_mir_transform/src/check_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ use rustc_middle::ty::{self, Ty, TyCtxt, TypingEnv};
use rustc_session::Session;
use tracing::debug;

use crate::PassPolicy;

/// This pass inserts checks for a valid enum discriminant where they are most
/// likely to find UB, because checking everywhere like Miri would generate too
/// much MIR.
pub(super) struct CheckEnums;

impl<'tcx> crate::MirPass<'tcx> for CheckEnums {
fn is_enabled(&self, sess: &Session) -> bool {
sess.ub_checks()
fn policy(&self, sess: &Session) -> PassPolicy {
// When UB checks are enabled this is part of their semantics, not an optimization.
PassPolicy::optional_non_optimization(sess.ub_checks())
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand Down Expand Up @@ -109,10 +112,6 @@ impl<'tcx> crate::MirPass<'tcx> for CheckEnums {
}
}
}

fn is_required(&self) -> bool {
true
}
}

/// Represent the different kind of enum checks we can insert.
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_mir_transform/src/check_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_session::Session;

use crate::PassPolicy;
use crate::check_pointers::{BorrowedFieldProjectionMode, PointerCheck, check_pointers};

pub(super) struct CheckNull;

impl<'tcx> crate::MirPass<'tcx> for CheckNull {
fn is_enabled(&self, sess: &Session) -> bool {
sess.ub_checks()
fn policy(&self, sess: &Session) -> PassPolicy {
// When UB checks are enabled this is part of their semantics, not an optimization.
PassPolicy::optional_non_optimization(sess.ub_checks())
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand All @@ -23,10 +25,6 @@ impl<'tcx> crate::MirPass<'tcx> for CheckNull {
BorrowedFieldProjectionMode::NoFollowProjections,
);
}

fn is_required(&self) -> bool {
true
}
}

fn insert_null_check<'tcx>(
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::adjustment::PointerCoercion;

use crate::PassPolicy;

pub(super) struct CleanupPostBorrowck;

impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
Expand Down Expand Up @@ -85,7 +87,8 @@ impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
}
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// Removes administrative MIR instructions that later passes must never see.
PassPolicy::Required
}
}
9 changes: 3 additions & 6 deletions compiler/rustc_mir_transform/src/copy_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::{Analysis, ResultsCursor};
use tracing::{debug, instrument};

use crate::PassPolicy;
use crate::ssa::{MaybeUninitializedLocals, SsaLocals};

/// Unify locals that copy each other.
Expand All @@ -21,8 +22,8 @@ use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
pub(super) struct CopyProp;

impl<'tcx> crate::MirPass<'tcx> for CopyProp {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 1
fn policy(&self, sess: &rustc_session::Session) -> PassPolicy {
PassPolicy::optimization(sess.mir_opt_level() >= 1)
}

#[instrument(level = "trace", skip(self, tcx, body))]
Expand Down Expand Up @@ -95,10 +96,6 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {

crate::simplify::remove_unused_definitions(body);
}

fn is_required(&self) -> bool {
false
}
}

/// Utility to help performing substitution: for all key-value pairs in `copy_classes`,
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_mir_transform/src/coroutine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use tracing::{debug, instrument};

use crate::deref_separator::deref_finder;
use crate::patch::MirPatch;
use crate::{abort_unwinding_calls, pass_manager as pm, simplify};
use crate::{PassPolicy, abort_unwinding_calls, pass_manager as pm, simplify};

pub(super) struct StateTransform;

Expand Down Expand Up @@ -1219,8 +1219,9 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
create_coroutine_resume_function(tcx, transform, body, can_return, can_unwind);
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// Implements coroutine semantics by lowering the coroutine body to a state machine.
PassPolicy::Required
}
}

Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustc_middle::mir::{self, BasicBlock, Statement, StatementKind, TerminatorKi
use rustc_middle::ty::TyCtxt;
use tracing::{debug, debug_span, trace};

use crate::PassPolicy;
use crate::coverage::counters::BcbCountersData;
use crate::coverage::graph::CoverageGraph;
use crate::coverage::mappings::ExtractedMappings;
Expand All @@ -24,8 +25,8 @@ mod tests;
pub(super) struct InstrumentCoverage;

impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.instrument_coverage()
fn policy(&self, sess: &rustc_session::Session) -> PassPolicy {
PassPolicy::optional_non_optimization(sess.instrument_coverage())
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
Expand Down Expand Up @@ -54,10 +55,6 @@ impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {

instrument_function_for_coverage(tcx, mir_body);
}

fn is_required(&self) -> bool {
true
}
}

fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_mir_transform/src/ctfe_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use rustc_middle::mir::{
use rustc_middle::ty::TyCtxt;
use tracing::instrument;

use crate::PassPolicy;

pub(super) struct CtfeLimit;

impl<'tcx> crate::MirPass<'tcx> for CtfeLimit {
Expand Down Expand Up @@ -37,8 +39,9 @@ impl<'tcx> crate::MirPass<'tcx> for CtfeLimit {
}
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// This is part of CTFE diagnostics rather than an optimization.
PassPolicy::optional_non_optimization(true)
}
}

Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use rustc_mir_dataflow::{Analysis, ResultsVisitor, visit_reachable_results};
use rustc_span::DUMMY_SP;
use tracing::{debug, debug_span, instrument};

use crate::PassPolicy;

// These constants are somewhat random guesses and have not been optimized.
// If `tcx.sess.mir_opt_level() >= 4`, we ignore the limits (this can become very expensive).
const BLOCK_LIMIT: usize = 100;
Expand All @@ -34,8 +36,8 @@ const PLACE_LIMIT: usize = 100;
pub(super) struct DataflowConstProp;

impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 3
fn policy(&self, sess: &rustc_session::Session) -> PassPolicy {
PassPolicy::optimization(sess.mir_opt_level() >= 3)
}

#[instrument(skip_all level = "debug")]
Expand Down Expand Up @@ -74,10 +76,6 @@ impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
let mut patch = visitor.patch;
debug_span!("patch").in_scope(|| patch.visit_body_preserves_cfg(body));
}

fn is_required(&self) -> bool {
false
}
}

// Note: Currently, places that have their reference taken cannot be tracked. Although this would
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use rustc_mir_dataflow::impls::{
LivenessTransferFunction, MaybeTransitiveLiveLocals, borrowed_locals,
};

use crate::PassPolicy;
use crate::simplify::UsedInStmtLocals;
use crate::util::most_packed_projection;

Expand Down Expand Up @@ -140,8 +141,8 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
}
}

fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 2
fn policy(&self, sess: &rustc_session::Session) -> PassPolicy {
PassPolicy::optimization(sess.mir_opt_level() >= 2)
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand All @@ -152,8 +153,4 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
}
}
}

fn is_required(&self) -> bool {
false
}
}
6 changes: 4 additions & 2 deletions compiler/rustc_mir_transform/src/deref_separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;

use crate::PassPolicy;
use crate::patch::MirPatch;

pub(super) struct Derefer;
Expand Down Expand Up @@ -101,7 +102,8 @@ impl<'tcx> crate::MirPass<'tcx> for Derefer {
deref_finder(tcx, body, true);
}

fn is_required(&self) -> bool {
true
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
// Later MIR stages expect derefs to only appear as the first place projection.
PassPolicy::Required
}
}
10 changes: 4 additions & 6 deletions compiler/rustc_mir_transform/src/dest_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ use rustc_mir_dataflow::points::DenseLocationMap;
use rustc_mir_dataflow::{Analysis, EntryStates, GenKill};
use tracing::{debug, trace};

use crate::PassPolicy;

pub(super) struct DestinationPropagation;

impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 2
fn policy(&self, sess: &rustc_session::Session) -> PassPolicy {
PassPolicy::optimization(sess.mir_opt_level() >= 2)
}

#[tracing::instrument(level = "trace", skip(self, tcx, body))]
Expand Down Expand Up @@ -232,10 +234,6 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {

apply_merges(body, tcx, relevant, merged_locals);
}

fn is_required(&self) -> bool {
false
}
}

//////////////////////////////////////////////////////////
Expand Down
Loading
Loading