Skip to content

Commit fd18b45

Browse files
Update passes with new interface
1 parent c1a501b commit fd18b45

32 files changed

+141
-98
lines changed

compiler/rustc_const_eval/src/transform/promote_consts.rs

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub struct PromoteTemps<'tcx> {
4141
}
4242

4343
impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
44+
fn phase_change(&self) -> Option<MirPhase> {
45+
Some(MirPhase::ConstPromotion)
46+
}
47+
4448
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4549
// There's not really any point in promoting errorful MIR.
4650
//

compiler/rustc_mir_dataflow/src/rustc_peek.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{Analysis, JoinSemiLattice, Results, ResultsCursor};
2020

2121
pub struct SanityCheck;
2222

23+
// FIXME: This should be a `MirLint`, but it needs to be moved back to `rustc_mir_transform` first.
2324
impl<'tcx> MirPass<'tcx> for SanityCheck {
2425
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2526
use crate::has_rustc_mir_with;

compiler/rustc_mir_transform/src/add_retag.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ fn may_be_reference(ty: Ty<'tcx>) -> bool {
5858
}
5959

6060
impl<'tcx> MirPass<'tcx> for AddRetag {
61-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
62-
if !tcx.sess.opts.debugging_opts.mir_emit_retag {
63-
return;
64-
}
61+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
62+
sess.opts.debugging_opts.mir_emit_retag
63+
}
6564

65+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
6666
// We need an `AllCallEdges` pass before we can do any work.
6767
super::add_call_guards::AllCallEdges.run_pass(tcx, body);
6868

compiler/rustc_mir_transform/src/check_const_item_mutation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use rustc_middle::ty::TyCtxt;
66
use rustc_session::lint::builtin::CONST_ITEM_MUTATION;
77
use rustc_span::def_id::DefId;
88

9-
use crate::MirPass;
9+
use crate::MirLint;
1010

1111
pub struct CheckConstItemMutation;
1212

13-
impl<'tcx> MirPass<'tcx> for CheckConstItemMutation {
14-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
13+
impl<'tcx> MirLint<'tcx> for CheckConstItemMutation {
14+
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1515
let mut checker = ConstMutationChecker { body, tcx, target_local: None };
1616
checker.visit_body(&body);
1717
}

compiler/rustc_mir_transform/src/check_packed_ref.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ use rustc_session::lint::builtin::UNALIGNED_REFERENCES;
77
use rustc_span::symbol::sym;
88

99
use crate::util;
10-
use crate::MirPass;
10+
use crate::MirLint;
1111

1212
pub(crate) fn provide(providers: &mut Providers) {
1313
*providers = Providers { unsafe_derive_on_repr_packed, ..*providers };
1414
}
1515

1616
pub struct CheckPackedRef;
1717

18-
impl<'tcx> MirPass<'tcx> for CheckPackedRef {
19-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
18+
impl<'tcx> MirLint<'tcx> for CheckPackedRef {
19+
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
2020
let param_env = tcx.param_env(body.source.def_id());
2121
let source_info = SourceInfo::outermost(body.span);
2222
let mut checker = PackedRefChecker { body, tcx, param_env, source_info };

compiler/rustc_mir_transform/src/const_debuginfo.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use rustc_index::{bit_set::BitSet, vec::IndexVec};
1515
pub struct ConstDebugInfo;
1616

1717
impl<'tcx> MirPass<'tcx> for ConstDebugInfo {
18-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19-
if !tcx.sess.opts.debugging_opts.unsound_mir_opts {
20-
return;
21-
}
18+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
19+
sess.opts.debugging_opts.unsound_mir_opts && sess.mir_opt_level() > 0
20+
}
2221

22+
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2323
trace!("running ConstDebugInfo on {:?}", body.source);
2424

2525
for (local, constant) in find_optimization_oportunities(body) {

compiler/rustc_mir_transform/src/const_goto.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ use super::simplify::{simplify_cfg, simplify_locals};
2727
pub struct ConstGoto;
2828

2929
impl<'tcx> MirPass<'tcx> for ConstGoto {
30+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
31+
sess.mir_opt_level() >= 4
32+
}
33+
3034
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
31-
if tcx.sess.mir_opt_level() < 4 {
32-
return;
33-
}
3435
trace!("Running ConstGoto on {:?}", body.source);
3536
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
3637
let mut opt_finder =

compiler/rustc_mir_transform/src/const_prop.rs

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ macro_rules! throw_machine_stop_str {
6262
pub struct ConstProp;
6363

6464
impl<'tcx> MirPass<'tcx> for ConstProp {
65+
fn is_enabled(&self, _sess: &rustc_session::Session) -> bool {
66+
// FIXME(#70073): Unlike the other passes in "optimizations", this one emits errors, so it
67+
// runs even when MIR optimizations are disabled. We should separate the lint out from the
68+
// transform and move the lint as early in the pipeline as possible.
69+
true
70+
}
71+
6572
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
6673
// will be evaluated by miri and produce its errors there
6774
if body.source.promoted.is_some() {

compiler/rustc_mir_transform/src/coverage/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ impl Error {
4949
pub struct InstrumentCoverage;
5050

5151
impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
52+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
53+
sess.instrument_coverage()
54+
}
55+
5256
fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
5357
let mir_source = mir_body.source;
5458

compiler/rustc_mir_transform/src/deduplicate_blocks.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ use super::simplify::simplify_cfg;
1515
pub struct DeduplicateBlocks;
1616

1717
impl<'tcx> MirPass<'tcx> for DeduplicateBlocks {
18+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
19+
sess.mir_opt_level() >= 4
20+
}
21+
1822
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19-
if tcx.sess.mir_opt_level() < 4 {
20-
return;
21-
}
2223
debug!("Running DeduplicateBlocks on `{:?}`", body.source);
2324
let duplicates = find_duplicates(body);
2425
let has_opts_to_apply = !duplicates.is_empty();

compiler/rustc_mir_transform/src/dest_prop.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,15 @@ const MAX_BLOCKS: usize = 250;
124124
pub struct DestinationPropagation;
125125

126126
impl<'tcx> MirPass<'tcx> for DestinationPropagation {
127-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
128-
// FIXME(#79191, #82678)
129-
if !tcx.sess.opts.debugging_opts.unsound_mir_opts {
130-
return;
131-
}
132-
127+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
128+
// FIXME(#79191, #82678): This is unsound.
129+
//
133130
// Only run at mir-opt-level=3 or higher for now (we don't fix up debuginfo and remove
134131
// storage statements at the moment).
135-
if tcx.sess.mir_opt_level() < 3 {
136-
return;
137-
}
132+
sess.opts.debugging_opts.unsound_mir_opts && sess.mir_opt_level() >= 3
133+
}
138134

135+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
139136
let def_id = body.source.def_id();
140137

141138
let candidates = find_candidates(tcx, body);

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ use super::simplify::simplify_cfg;
2525
pub struct EarlyOtherwiseBranch;
2626

2727
impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
28-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
28+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2929
// FIXME(#78496)
30-
if !tcx.sess.opts.debugging_opts.unsound_mir_opts {
31-
return;
32-
}
30+
sess.opts.debugging_opts.unsound_mir_opts && sess.mir_opt_level() >= 3
31+
}
3332

34-
if tcx.sess.mir_opt_level() < 3 {
35-
return;
36-
}
33+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3734
trace!("running EarlyOtherwiseBranch on {:?}", body.source);
35+
3836
// we are only interested in this bb if the terminator is a switchInt
3937
let bbs_with_switch =
4038
body.basic_blocks().iter_enumerated().filter(|(_, bb)| is_switch(bb.terminator()));

compiler/rustc_mir_transform/src/elaborate_drops.rs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use std::fmt;
1919
pub struct ElaborateDrops;
2020

2121
impl<'tcx> MirPass<'tcx> for ElaborateDrops {
22+
fn phase_change(&self) -> Option<MirPhase> {
23+
Some(MirPhase::DropLowering)
24+
}
25+
2226
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2327
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);
2428

compiler/rustc_mir_transform/src/function_item_references.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES;
1111
use rustc_span::{symbol::sym, Span};
1212
use rustc_target::spec::abi::Abi;
1313

14-
use crate::MirPass;
14+
use crate::MirLint;
1515

1616
pub struct FunctionItemReferences;
1717

18-
impl<'tcx> MirPass<'tcx> for FunctionItemReferences {
19-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
18+
impl<'tcx> MirLint<'tcx> for FunctionItemReferences {
19+
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
2020
let mut checker = FunctionItemRefChecker { tcx, body };
2121
checker.visit_body(&body);
2222
}

compiler/rustc_mir_transform/src/generator.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,10 @@ fn create_cases<'tcx>(
12321232
}
12331233

12341234
impl<'tcx> MirPass<'tcx> for StateTransform {
1235+
fn phase_change(&self) -> Option<MirPhase> {
1236+
Some(MirPhase::GeneratorLowering)
1237+
}
1238+
12351239
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
12361240
let yield_ty = if let Some(yield_ty) = body.yield_ty() {
12371241
yield_ty

compiler/rustc_mir_transform/src/inline.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,16 @@ struct CallSite<'tcx> {
3737
source_info: SourceInfo,
3838
}
3939

40-
/// Returns true if MIR inlining is enabled in the current compilation session.
41-
crate fn is_enabled(tcx: TyCtxt<'_>) -> bool {
42-
if let Some(enabled) = tcx.sess.opts.debugging_opts.inline_mir {
43-
return enabled;
44-
}
45-
46-
tcx.sess.mir_opt_level() >= 3
47-
}
48-
4940
impl<'tcx> MirPass<'tcx> for Inline {
50-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
51-
if !is_enabled(tcx) {
52-
return;
41+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
42+
if let Some(enabled) = sess.opts.debugging_opts.inline_mir {
43+
return enabled;
5344
}
5445

46+
sess.opts.mir_opt_level() >= 3
47+
}
48+
49+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5550
let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
5651
let _guard = span.enter();
5752
if inline(tcx, body) {

compiler/rustc_mir_transform/src/instcombine.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use rustc_middle::ty::{self, TyCtxt};
1111
pub struct InstCombine;
1212

1313
impl<'tcx> MirPass<'tcx> for InstCombine {
14+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
15+
sess.mir_opt_level() > 0
16+
}
17+
1418
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1519
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
1620
let ctx = InstCombineContext { tcx, local_decls };

compiler/rustc_mir_transform/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ fn mir_const<'tcx>(
290290
MirPhase::Const,
291291
&[&[
292292
// MIR-level lints.
293-
&check_packed_ref::CheckPackedRef,
294-
&check_const_item_mutation::CheckConstItemMutation,
295-
&function_item_references::FunctionItemReferences,
293+
&Lint(check_packed_ref::CheckPackedRef),
294+
&Lint(check_const_item_mutation::CheckConstItemMutation),
295+
&Lint(function_item_references::FunctionItemReferences),
296296
// What we need to do constant evaluation.
297297
&simplify::SimplifyCfg::new("initial"),
298-
&rustc_peek::SanityCheck,
298+
&rustc_peek::SanityCheck, // Just a lint
299299
]],
300300
);
301301
tcx.alloc_steal_mir(body)
@@ -443,7 +443,7 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
443443
let def = ty::WithOptConstParam::unknown(did);
444444

445445
// Do not compute the mir call graph without said call graph actually being used.
446-
if inline::is_enabled(tcx) {
446+
if inline::Inline.is_enabled(&tcx.sess) {
447447
let _ = tcx.mir_inliner_callees(ty::InstanceDef::Item(def));
448448
}
449449
}

compiler/rustc_mir_transform/src/lower_slice_len.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use rustc_middle::ty::{self, TyCtxt};
1010
pub struct LowerSliceLenCalls;
1111

1212
impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls {
13+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
14+
sess.opts.mir_opt_level() > 0
15+
}
16+
1317
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1418
lower_slice_len_calls(tcx, body)
1519
}

compiler/rustc_mir_transform/src/match_branches.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ pub struct MatchBranchSimplification;
4040
/// ```
4141
4242
impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
43-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
44-
if tcx.sess.mir_opt_level() < 3 {
45-
return;
46-
}
43+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
44+
sess.mir_opt_level() >= 3
45+
}
4746

47+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4848
let def_id = body.source.def_id();
4949
let param_env = tcx.param_env(def_id);
5050

compiler/rustc_mir_transform/src/multiple_return_terminators.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use rustc_middle::ty::TyCtxt;
99
pub struct MultipleReturnTerminators;
1010

1111
impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
12-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
13-
if tcx.sess.mir_opt_level() < 4 {
14-
return;
15-
}
12+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
13+
sess.mir_opt_level() >= 4
14+
}
1615

16+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1717
// find basic blocks with no statement and a return terminator
1818
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len());
1919
let def_id = body.source.def_id();

compiler/rustc_mir_transform/src/normalize_array_len.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ const MAX_NUM_LOCALS: usize = 3000;
1414
pub struct NormalizeArrayLen;
1515

1616
impl<'tcx> MirPass<'tcx> for NormalizeArrayLen {
17-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
18-
if tcx.sess.mir_opt_level() < 4 {
19-
return;
20-
}
17+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
18+
sess.mir_opt_level() >= 4
19+
}
2120

21+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2222
// early returns for edge cases of highly unrolled functions
2323
if body.basic_blocks().len() > MAX_NUM_BLOCKS {
2424
return;

compiler/rustc_mir_transform/src/nrvo.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ use crate::MirPass;
3333
pub struct RenameReturnPlace;
3434

3535
impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
36-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
37-
if tcx.sess.mir_opt_level() == 0 {
38-
return;
39-
}
36+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
37+
sess.mir_opt_level() > 0
38+
}
4039

40+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
4141
let def_id = body.source.def_id();
4242
let returned_local = match local_eligible_for_nrvo(body) {
4343
Some(l) => l,

compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@ use rustc_target::spec::PanicStrategy;
1010
/// code for these.
1111
pub struct RemoveNoopLandingPads;
1212

13-
pub fn remove_noop_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
14-
if tcx.sess.panic_strategy() == PanicStrategy::Abort {
15-
return;
13+
impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
14+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
15+
sess.panic_strategy() != PanicStrategy::Abort
1616
}
17-
debug!("remove_noop_landing_pads({:?})", body);
18-
19-
RemoveNoopLandingPads.remove_nop_landing_pads(body)
20-
}
2117

22-
impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
23-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
24-
remove_noop_landing_pads(tcx, body);
18+
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19+
debug!("remove_noop_landing_pads({:?})", body);
20+
self.remove_nop_landing_pads(body)
2521
}
2622
}
2723

0 commit comments

Comments
 (0)