Skip to content

Commit

Permalink
Disable non-required MIR opts with optimize(none)
Browse files Browse the repository at this point in the history
Co-authored-by: Waffle Lapkin <[email protected]>
  • Loading branch information
clubby789 and WaffleLapkin committed Jan 23, 2025
1 parent cd848c9 commit 7a9661d
Show file tree
Hide file tree
Showing 57 changed files with 319 additions and 10 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_attr_data_structures/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub enum OptimizeAttr {
Size,
}

impl OptimizeAttr {
pub fn do_not_optimize(&self) -> bool {
matches!(self, Self::DoNotOptimize)
}
}

#[derive(Clone, Debug, Encodable, Decodable)]
pub enum DiagnosticAttribute {
// tidy-alphabetical-start
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/abort_unwinding_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,8 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
// We may have invalidated some `cleanup` blocks so clean those up now.
super::simplify::remove_dead_blocks(body);
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/add_call_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {

body.basic_blocks_mut().extend(new_blocks);
}

fn is_required(&self) -> bool {
true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {

patch.apply(body);
}

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

fn add_move_for_packed_drop<'tcx>(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/add_retag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ impl<'tcx> crate::MirPass<'tcx> for AddRetag {
}
}
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/add_subtyping_projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ impl<'tcx> crate::MirPass<'tcx> for Subtyper {
}
checker.patcher.apply(body);
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/check_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
}
}
}

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

struct PointerFinder<'a, 'tcx> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
decl.user_ty = None;
}
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/copy_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
crate::simplify::remove_unused_definitions(body);
}
}

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

/// `SsaLocals` computed equivalence classes between locals considering copy/move assignments.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,10 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
// Run derefer to fix Derefs that are not in the first place
deref_finder(tcx, body);
}

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

/// Looks for any assignments between locals (e.g., `_4 = _5`) that will both be converted to fields
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {

instrument_function_for_coverage(tcx, mir_body);
}

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

fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/cross_crate_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
// Don't do any inference if codegen optimizations are disabled and also MIR inlining is not
// enabled. This ensures that we do inference even if someone only passes -Zinline-mir,
// which is less confusing than having to also enable -Copt-level=1.
let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline)
let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline, pm::Optimizations::Allowed)
|| inline::ForceInline::should_run_pass_for_callee(tcx, def_id.to_def_id());
if matches!(tcx.sess.opts.optimize, OptLevel::No) && !inliner_will_run {
return false;
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/ctfe_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ impl<'tcx> crate::MirPass<'tcx> for CtfeLimit {
);
}
}

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

fn has_back_edge(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ 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
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
eliminate(tcx, body);
}

fn is_required(&self) -> bool {
false
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/deduplicate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl<'tcx> crate::MirPass<'tcx> for DeduplicateBlocks {
simplify_cfg(body);
}
}

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

struct OptApplier<'tcx> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/deref_separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ impl<'tcx> crate::MirPass<'tcx> for Derefer {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
deref_finder(tcx, body);
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dest_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {

trace!(round_count);
}

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

#[derive(Debug, Default)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ impl<'tcx> crate::MirPass<'tcx> for Marker {
}

fn run_pass(&self, _tcx: TyCtxt<'tcx>, _body: &mut Body<'tcx>) {}

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

pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
simplify_cfg(body);
}
}

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

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/elaborate_box_derefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs {
}
}
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateDrops {
elaborate_patch.apply(body);
deref_finder(tcx, body);
}

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

/// Records unwind edges which are known to be unreachable, because they are in `drop` terminators
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
// statements.
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
}

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

newtype_index! {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/impossible_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ impl<'tcx> MirPass<'tcx> for ImpossiblePredicates {
body.local_decls.raw.truncate(body.arg_count + 1);
}
}

fn is_required(&self) -> bool {
true
}
}
8 changes: 8 additions & 0 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl<'tcx> crate::MirPass<'tcx> for Inline {
deref_finder(tcx, body);
}
}

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

pub struct ForceInline;
Expand All @@ -85,6 +89,10 @@ impl<'tcx> crate::MirPass<'tcx> for ForceInline {
false
}

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

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let span = trace_span!("force_inline", body = %tcx.def_path_str(body.source.def_id()));
let _guard = span.enter();
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/instsimplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());
}
}

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

struct InstSimplifyContext<'a, 'tcx> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ impl<'tcx> crate::MirPass<'tcx> for JumpThreading {
}
OpportunitySet::new(body, opportunities).apply(body);
}

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

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/large_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ impl<'tcx> crate::MirPass<'tcx> for EnumSizeOpt {
});
}
}

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

impl EnumSizeOpt {
Expand Down
33 changes: 29 additions & 4 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
&Lint(sanity_check::SanityCheck),
],
None,
pm::Optimizations::Allowed,
);
tcx.alloc_steal_mir(body)
}
Expand Down Expand Up @@ -440,6 +441,7 @@ fn mir_promoted(
&mut body,
&[&promote_pass, &simplify::SimplifyCfg::PromoteConsts, &coverage::InstrumentCoverage],
Some(MirPhase::Analysis(AnalysisPhase::Initial)),
pm::Optimizations::Allowed,
);

lint_tail_expr_drop_order::run_lint(tcx, def, &body);
Expand Down Expand Up @@ -473,7 +475,7 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
};

let mut body = remap_mir_for_const_eval_select(tcx, body, hir::Constness::Const);
pm::run_passes(tcx, &mut body, &[&ctfe_limit::CtfeLimit], None);
pm::run_passes(tcx, &mut body, &[&ctfe_limit::CtfeLimit], None, pm::Optimizations::Allowed);

body
}
Expand All @@ -493,7 +495,7 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
let is_fn_like = tcx.def_kind(def).is_fn_like();
if is_fn_like {
// Do not compute the mir call graph without said call graph actually being used.
if pm::should_run_pass(tcx, &inline::Inline)
if pm::should_run_pass(tcx, &inline::Inline, pm::Optimizations::Allowed)
|| inline::ForceInline::should_run_pass_for_callee(tcx, def.to_def_id())
{
tcx.ensure_with_value().mir_inliner_callees(ty::InstanceKind::Item(def.to_def_id()));
Expand Down Expand Up @@ -533,6 +535,7 @@ pub fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
&Lint(post_drop_elaboration::CheckLiveDrops),
],
None,
pm::Optimizations::Allowed,
);
}

Expand All @@ -557,7 +560,13 @@ fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&deref_separator::Derefer,
];

pm::run_passes(tcx, body, passes, Some(MirPhase::Analysis(AnalysisPhase::PostCleanup)));
pm::run_passes(
tcx,
body,
passes,
Some(MirPhase::Analysis(AnalysisPhase::PostCleanup)),
pm::Optimizations::Allowed,
);
}

/// Returns the sequence of passes that lowers analysis to runtime MIR.
Expand Down Expand Up @@ -597,7 +606,13 @@ fn run_runtime_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&simplify::SimplifyCfg::PreOptimizations,
];

pm::run_passes(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::PostCleanup)));
pm::run_passes(
tcx,
body,
passes,
Some(MirPhase::Runtime(RuntimePhase::PostCleanup)),
pm::Optimizations::Allowed,
);

// Clear this by anticipation. Optimizations and runtime MIR have no reason to look
// into this information, which is meant for borrowck diagnostics.
Expand All @@ -611,6 +626,15 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
WithMinOptLevel(1, x)
}

let def_id = body.source.def_id();
let optimizations = if tcx.def_kind(def_id).has_codegen_attrs()
&& tcx.codegen_fn_attrs(def_id).optimize.do_not_optimize()
{
pm::Optimizations::Suppressed
} else {
pm::Optimizations::Allowed
};

// The main optimizations that we do on MIR.
pm::run_passes(
tcx,
Expand Down Expand Up @@ -683,6 +707,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&dump_mir::Marker("PreCodegen"),
],
Some(MirPhase::Runtime(RuntimePhase::Optimized)),
optimizations,
);
}

Expand Down
Loading

0 comments on commit 7a9661d

Please sign in to comment.