Skip to content

Commit 2b080be

Browse files
committed
refactor(mir-transform): Merge pass policies into one function
1 parent ad0c9dc commit 2b080be

54 files changed

Lines changed: 304 additions & 322 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use rustc_middle::ty::{self, TyCtxt, layout};
66
use rustc_span::sym;
77
use rustc_target::spec::PanicStrategy;
88

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

141-
fn is_required(&self) -> bool {
142-
true
143+
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
144+
// Implements part of MIR semantics, turning effectively implicit aborts into explicit
145+
// ones.
146+
PassPolicy::Required
143147
}
144148
}

compiler/rustc_mir_transform/src/add_call_guards.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use rustc_middle::mir::*;
2121
use rustc_middle::ty::TyCtxt;
2222
use tracing::debug;
2323

24+
use crate::PassPolicy;
25+
2426
#[derive(PartialEq)]
2527
pub(super) enum AddCallGuards {
2628
AllCallEdges,
@@ -127,8 +129,10 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
127129
basic_blocks.extend(new_blocks);
128130
}
129131

130-
fn is_required(&self) -> bool {
131-
true
132+
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
133+
// Breaks critical edges so codegen can place edge-specific actions without affecting
134+
// other control-flow edges.
135+
PassPolicy::Required
132136
}
133137
}
134138

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::ty::{self, TyCtxt};
44
use tracing::debug;
55

66
use crate::patch::MirPatch;
7-
use crate::util;
7+
use crate::{PassPolicy, util};
88

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

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

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_middle::mir::visit::MutVisitor;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::TyCtxt;
44

5+
use crate::PassPolicy;
56
use crate::patch::MirPatch;
67

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

68-
fn is_required(&self) -> bool {
69-
true
69+
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
70+
// FIXME: Why is this okay? (The pass is also lacking a proper module-level description.)
71+
PassPolicy::Required
7072
}
7173
}

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ use rustc_middle::mir::*;
77
use rustc_middle::ty::{Ty, TyCtxt};
88
use rustc_session::Session;
99

10+
use crate::PassPolicy;
1011
use crate::check_pointers::{BorrowedFieldProjectionMode, PointerCheck, check_pointers};
1112

1213
pub(super) struct CheckAlignment;
1314

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

1921
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -31,10 +33,6 @@ impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
3133
BorrowedFieldProjectionMode::FollowProjections,
3234
);
3335
}
34-
35-
fn is_required(&self) -> bool {
36-
true
37-
}
3836
}
3937

4038
/// Inserts the actual alignment check's logic. Returns a

compiler/rustc_mir_transform/src/check_enums.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ use rustc_middle::ty::{self, Ty, TyCtxt, TypingEnv};
1010
use rustc_session::Session;
1111
use tracing::debug;
1212

13+
use crate::PassPolicy;
14+
1315
/// This pass inserts checks for a valid enum discriminant where they are most
1416
/// likely to find UB, because checking everywhere like Miri would generate too
1517
/// much MIR.
1618
pub(super) struct CheckEnums;
1719

1820
impl<'tcx> crate::MirPass<'tcx> for CheckEnums {
19-
fn is_enabled(&self, sess: &Session) -> bool {
20-
sess.ub_checks()
21+
fn policy(&self, sess: &Session) -> PassPolicy {
22+
// When UB checks are enabled this is part of their semantics, not an optimization.
23+
PassPolicy::optional(sess.ub_checks())
2124
}
2225

2326
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -109,10 +112,6 @@ impl<'tcx> crate::MirPass<'tcx> for CheckEnums {
109112
}
110113
}
111114
}
112-
113-
fn is_required(&self) -> bool {
114-
true
115-
}
116115
}
117116

118117
/// Represent the different kind of enum checks we can insert.

compiler/rustc_mir_transform/src/check_null.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use rustc_middle::mir::*;
55
use rustc_middle::ty::{Ty, TyCtxt};
66
use rustc_session::Session;
77

8+
use crate::PassPolicy;
89
use crate::check_pointers::{BorrowedFieldProjectionMode, PointerCheck, check_pointers};
910

1011
pub(super) struct CheckNull;
1112

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

1719
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -23,10 +25,6 @@ impl<'tcx> crate::MirPass<'tcx> for CheckNull {
2325
BorrowedFieldProjectionMode::NoFollowProjections,
2426
);
2527
}
26-
27-
fn is_required(&self) -> bool {
28-
true
29-
}
3028
}
3129

3230
fn insert_null_check<'tcx>(

compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use rustc_middle::mir::*;
2121
use rustc_middle::ty::TyCtxt;
2222
use rustc_middle::ty::adjustment::PointerCoercion;
2323

24+
use crate::PassPolicy;
25+
2426
pub(super) struct CleanupPostBorrowck;
2527

2628
impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
@@ -85,7 +87,8 @@ impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
8587
}
8688
}
8789

88-
fn is_required(&self) -> bool {
89-
true
90+
fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
91+
// Removes administrative MIR instructions that later passes must never see.
92+
PassPolicy::Required
9093
}
9194
}

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::ty::TyCtxt;
66
use rustc_mir_dataflow::{Analysis, ResultsCursor};
77
use tracing::{debug, instrument};
88

9+
use crate::PassPolicy;
910
use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
1011

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

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

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

9697
crate::simplify::remove_unused_definitions(body);
9798
}
98-
99-
fn is_required(&self) -> bool {
100-
false
101-
}
10299
}
103100

104101
/// Utility to help performing substitution: for all key-value pairs in `copy_classes`,

compiler/rustc_mir_transform/src/coroutine/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use tracing::{debug, instrument};
8080

8181
use crate::deref_separator::deref_finder;
8282
use crate::patch::MirPatch;
83-
use crate::{abort_unwinding_calls, pass_manager as pm, simplify};
83+
use crate::{PassPolicy, abort_unwinding_calls, pass_manager as pm, simplify};
8484

8585
pub(super) struct StateTransform;
8686

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

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

0 commit comments

Comments
 (0)