Skip to content

[perf] Don't run some MirLints if lints are not enabled #142937

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

Closed
Closed
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
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/add_retag.rs
Original file line number Diff line number Diff line change
@@ -49,8 +49,8 @@ fn may_contain_reference<'tcx>(ty: Ty<'tcx>, depth: u32, tcx: TyCtxt<'tcx>) -> b
}

impl<'tcx> crate::MirPass<'tcx> for AddRetag {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.opts.unstable_opts.mir_emit_retag
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.opts.unstable_opts.mir_emit_retag
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5 changes: 2 additions & 3 deletions compiler/rustc_mir_transform/src/check_alignment.rs
Original file line number Diff line number Diff line change
@@ -4,15 +4,14 @@ use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::visit::PlaceContext;
use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_session::Session;

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 is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.ub_checks()
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
9 changes: 9 additions & 0 deletions compiler/rustc_mir_transform/src/check_call_recursion.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ use rustc_hir::LangItem;
use rustc_hir::def::DefKind;
use rustc_middle::mir::{self, BasicBlock, BasicBlocks, Body, Terminator, TerminatorKind};
use rustc_middle::ty::{self, GenericArg, GenericArgs, Instance, Ty, TyCtxt};
use rustc_session::lint::LintId;
use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION;
use rustc_span::Span;

@@ -16,6 +17,10 @@ use crate::pass_manager::MirLint;
pub(super) struct CheckCallRecursion;

impl<'tcx> MirLint<'tcx> for CheckCallRecursion {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(UNCONDITIONAL_RECURSION))
}

fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let def_id = body.source.def_id().expect_local();

@@ -38,6 +43,10 @@ impl<'tcx> MirLint<'tcx> for CheckCallRecursion {
pub(super) struct CheckDropRecursion;

impl<'tcx> MirLint<'tcx> for CheckDropRecursion {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(UNCONDITIONAL_RECURSION))
}

fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let def_id = body.source.def_id().expect_local();

5 changes: 5 additions & 0 deletions compiler/rustc_mir_transform/src/check_const_item_mutation.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use rustc_hir::HirId;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_session::lint::LintId;
use rustc_session::lint::builtin::CONST_ITEM_MUTATION;
use rustc_span::Span;
use rustc_span::def_id::DefId;
@@ -11,6 +12,10 @@ use crate::errors;
pub(super) struct CheckConstItemMutation;

impl<'tcx> crate::MirLint<'tcx> for CheckConstItemMutation {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(CONST_ITEM_MUTATION))
}

fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let mut checker = ConstMutationChecker { body, tcx, target_local: None };
checker.visit_body(body);
5 changes: 2 additions & 3 deletions compiler/rustc_mir_transform/src/check_null.rs
Original file line number Diff line number Diff line change
@@ -2,15 +2,14 @@ use rustc_index::IndexVec;
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext};
use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_session::Session;

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 is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.ub_checks()
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/copy_prop.rs
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@ use crate::ssa::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 is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.mir_opt_level() >= 1
}

#[instrument(level = "trace", skip(self, tcx, body))]
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@ use crate::coverage::mappings::ExtractedMappings;
pub(super) struct InstrumentCoverage;

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

fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
@@ -35,8 +35,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 is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.mir_opt_level() >= 3
}

#[instrument(skip_all level = "debug")]
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
@@ -140,8 +140,8 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
}
}

fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 2
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.mir_opt_level() >= 2
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/dest_prop.rs
Original file line number Diff line number Diff line change
@@ -149,7 +149,7 @@ use tracing::{debug, trace};
pub(super) struct DestinationPropagation;

impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
// For now, only run at MIR opt level 3. Two things need to be changed before this can be
// turned on by default:
// 1. Because of the overeager removal of storage statements, this can cause stack space
@@ -158,7 +158,7 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
// 2. Despite being an overall perf improvement, this still causes a 30% regression in
// keccak. We can temporarily fix this by bounding function size, but in the long term
// we should fix this by being smarter about invalidating analysis results.
sess.mir_opt_level() >= 3
tcx.sess.mir_opt_level() >= 3
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
@@ -93,8 +93,8 @@ use crate::patch::MirPatch;
pub(super) struct EarlyOtherwiseBranch;

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

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/errors.rs
Original file line number Diff line number Diff line change
@@ -92,6 +92,10 @@ impl<'a, P: std::fmt::Debug> LintDiagnostic<'a, ()> for AssertLint<P> {
}

impl AssertLintKind {
pub(crate) fn all_lints() -> [&'static Lint; 2] {
[lint::builtin::ARITHMETIC_OVERFLOW, lint::builtin::UNCONDITIONAL_PANIC]
}

pub(crate) fn lint(&self) -> &'static Lint {
match self {
AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,
5 changes: 5 additions & 0 deletions compiler/rustc_mir_transform/src/function_item_references.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ use rustc_hir::def_id::DefId;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, EarlyBinder, GenericArgsRef, Ty, TyCtxt};
use rustc_session::lint::LintId;
use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES;
use rustc_span::source_map::Spanned;
use rustc_span::{Span, sym};
@@ -13,6 +14,10 @@ use crate::errors;
pub(super) struct FunctionItemReferences;

impl<'tcx> crate::MirLint<'tcx> for FunctionItemReferences {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(FUNCTION_ITEM_REFERENCES))
}

fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let mut checker = FunctionItemRefChecker { tcx, body };
checker.visit_body(body);
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
@@ -114,8 +114,8 @@ use crate::ssa::SsaLocals;
pub(super) struct GVN;

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

#[instrument(level = "trace", skip(self, tcx, body))]
13 changes: 7 additions & 6 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
@@ -43,16 +43,17 @@ struct CallSite<'tcx> {
pub struct Inline;

impl<'tcx> crate::MirPass<'tcx> for Inline {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
if let Some(enabled) = sess.opts.unstable_opts.inline_mir {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
if let Some(enabled) = tcx.sess.opts.unstable_opts.inline_mir {
return enabled;
}

match sess.mir_opt_level() {
match tcx.sess.mir_opt_level() {
0 | 1 => false,
2 => {
(sess.opts.optimize == OptLevel::More || sess.opts.optimize == OptLevel::Aggressive)
&& sess.opts.incremental == None
(tcx.sess.opts.optimize == OptLevel::More
|| tcx.sess.opts.optimize == OptLevel::Aggressive)
&& tcx.sess.opts.incremental == None
}
_ => true,
}
@@ -82,7 +83,7 @@ impl ForceInline {
}

impl<'tcx> crate::MirPass<'tcx> for ForceInline {
fn is_enabled(&self, _: &rustc_session::Session) -> bool {
fn is_enabled(&self, _: TyCtxt<'tcx>) -> bool {
true
}

4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/instsimplify.rs
Original file line number Diff line number Diff line change
@@ -24,8 +24,8 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
}
}

fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.mir_opt_level() > 0
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
@@ -61,8 +61,8 @@ const MAX_COST: usize = 100;
const MAX_PLACES: usize = 100;

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

#[instrument(skip_all level = "debug")]
6 changes: 6 additions & 0 deletions compiler/rustc_mir_transform/src/known_panics_lint.rs
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC
use rustc_middle::mir::*;
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
use rustc_middle::ty::{self, ConstInt, ScalarInt, Ty, TyCtxt, TypeVisitableExt};
use rustc_session::lint::LintId;
use rustc_span::Span;
use tracing::{debug, instrument, trace};

@@ -27,6 +28,11 @@ use crate::errors::{AssertLint, AssertLintKind};
pub(super) struct KnownPanicsLint;

impl<'tcx> crate::MirLint<'tcx> for KnownPanicsLint {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
let ignored_lints = tcx.lints_that_dont_need_to_run(());
!AssertLintKind::all_lints().iter().all(|lint| ignored_lints.contains(&LintId::of(lint)))
}

fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
if body.tainted_by_errors.is_some() {
return;
5 changes: 2 additions & 3 deletions compiler/rustc_mir_transform/src/large_enums.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ use rustc_middle::mir::interpret::AllocId;
use rustc_middle::mir::*;
use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
use rustc_session::Session;

use crate::patch::MirPatch;

@@ -29,11 +28,11 @@ pub(super) struct EnumSizeOpt {
}

impl<'tcx> crate::MirPass<'tcx> for EnumSizeOpt {
fn is_enabled(&self, sess: &Session) -> bool {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
// There are some differences in behavior on wasm and ARM that are not properly
// understood, so we conservatively treat this optimization as unsound:
// https://github.com/rust-lang/rust/pull/85158#issuecomment-1101836457
sess.opts.unstable_opts.unsound_mir_opts || sess.mir_opt_level() >= 3
tcx.sess.opts.unstable_opts.unsound_mir_opts || tcx.sess.mir_opt_level() >= 3
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/lower_slice_len.rs
Original file line number Diff line number Diff line change
@@ -8,8 +8,8 @@ use rustc_middle::ty::TyCtxt;
pub(super) struct LowerSliceLenCalls;

impl<'tcx> crate::MirPass<'tcx> for LowerSliceLenCalls {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.mir_opt_level() > 0
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/match_branches.rs
Original file line number Diff line number Diff line change
@@ -13,8 +13,8 @@ use crate::patch::MirPatch;
pub(super) struct MatchBranchSimplification;

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

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3 changes: 1 addition & 2 deletions compiler/rustc_mir_transform/src/mentioned_items.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{self, Location, MentionedItem};
use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::Session;
use rustc_span::source_map::Spanned;

pub(super) struct MentionedItems;
@@ -14,7 +13,7 @@ struct MentionedItemsVisitor<'a, 'tcx> {
}

impl<'tcx> crate::MirPass<'tcx> for MentionedItems {
fn is_enabled(&self, _sess: &Session) -> bool {
fn is_enabled(&self, _tcx: TyCtxt<'tcx>) -> bool {
// If this pass is skipped the collector assume that nothing got mentioned! We could
// potentially skip it in opt-level 0 if we are sure that opt-level will never *remove* uses
// of anything, but that still seems fragile. Furthermore, even debug builds use level 1, so
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@ use crate::simplify;
pub(super) struct MultipleReturnTerminators;

impl<'tcx> crate::MirPass<'tcx> for MultipleReturnTerminators {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 4
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
tcx.sess.mir_opt_level() >= 4
}

fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/nrvo.rs
Original file line number Diff line number Diff line change
@@ -33,9 +33,9 @@ use tracing::{debug, trace};
pub(super) struct RenameReturnPlace;

impl<'tcx> crate::MirPass<'tcx> for RenameReturnPlace {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
// unsound: #111005
sess.mir_opt_level() > 0 && sess.opts.unstable_opts.unsound_mir_opts
tcx.sess.mir_opt_level() > 0 && tcx.sess.opts.unstable_opts.unsound_mir_opts
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
Loading