Skip to content

Commit 4ed0f0d

Browse files
authored
Rollup merge of #129926 - nnethercote:mv-SanityCheck-and-MirPass, r=cjgillot
Move `SanityCheck` and `MirPass` They are currently in `rustc_middle`. This PR moves them to `rustc_mir_transform`, which makes more sense. r? ``@cjgillot``
2 parents 485fd38 + 0b2b03c commit 4ed0f0d

Some content is hidden

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

59 files changed

+201
-211
lines changed

compiler/rustc_middle/src/mir/mod.rs

-62
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html
44
55
use std::borrow::Cow;
6-
use std::cell::RefCell;
7-
use std::collections::hash_map::Entry;
86
use std::fmt::{self, Debug, Formatter};
97
use std::ops::{Index, IndexMut};
108
use std::{iter, mem};
@@ -26,7 +24,6 @@ use rustc_index::bit_set::BitSet;
2624
use rustc_index::{Idx, IndexSlice, IndexVec};
2725
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
2826
use rustc_serialize::{Decodable, Encodable};
29-
use rustc_session::Session;
3027
use rustc_span::source_map::Spanned;
3128
use rustc_span::symbol::Symbol;
3229
use rustc_span::{Span, DUMMY_SP};
@@ -106,65 +103,6 @@ impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
106103
}
107104
}
108105

109-
thread_local! {
110-
static PASS_NAMES: RefCell<FxHashMap<&'static str, &'static str>> = {
111-
RefCell::new(FxHashMap::default())
112-
};
113-
}
114-
115-
/// Converts a MIR pass name into a snake case form to match the profiling naming style.
116-
fn to_profiler_name(type_name: &'static str) -> &'static str {
117-
PASS_NAMES.with(|names| match names.borrow_mut().entry(type_name) {
118-
Entry::Occupied(e) => *e.get(),
119-
Entry::Vacant(e) => {
120-
let snake_case: String = type_name
121-
.chars()
122-
.flat_map(|c| {
123-
if c.is_ascii_uppercase() {
124-
vec!['_', c.to_ascii_lowercase()]
125-
} else if c == '-' {
126-
vec!['_']
127-
} else {
128-
vec![c]
129-
}
130-
})
131-
.collect();
132-
let result = &*String::leak(format!("mir_pass{}", snake_case));
133-
e.insert(result);
134-
result
135-
}
136-
})
137-
}
138-
139-
/// A streamlined trait that you can implement to create a pass; the
140-
/// pass will be named after the type, and it will consist of a main
141-
/// loop that goes over each available MIR and applies `run_pass`.
142-
pub trait MirPass<'tcx> {
143-
fn name(&self) -> &'static str {
144-
// FIXME Simplify the implementation once more `str` methods get const-stable.
145-
// See copypaste in `MirLint`
146-
const {
147-
let name = std::any::type_name::<Self>();
148-
crate::util::common::c_name(name)
149-
}
150-
}
151-
152-
fn profiler_name(&self) -> &'static str {
153-
to_profiler_name(self.name())
154-
}
155-
156-
/// Returns `true` if this pass is enabled with the current combination of compiler flags.
157-
fn is_enabled(&self, _sess: &Session) -> bool {
158-
true
159-
}
160-
161-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>);
162-
163-
fn is_mir_dump_enabled(&self) -> bool {
164-
true
165-
}
166-
}
167-
168106
impl MirPhase {
169107
/// Gets the index of the current MirPhase within the set of all `MirPhase`s.
170108
///

compiler/rustc_middle/src/util/common.rs

-16
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,3 @@ pub fn to_readable_str(mut val: usize) -> String {
2020

2121
groups.join("_")
2222
}
23-
24-
// const wrapper for `if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }`
25-
pub const fn c_name(name: &'static str) -> &'static str {
26-
// FIXME Simplify the implementation once more `str` methods get const-stable.
27-
// and inline into call site
28-
let bytes = name.as_bytes();
29-
let mut i = bytes.len();
30-
while i > 0 && bytes[i - 1] != b':' {
31-
i = i - 1;
32-
}
33-
let (_, bytes) = bytes.split_at(i);
34-
match std::str::from_utf8(bytes) {
35-
Ok(name) => name,
36-
Err(_) => name,
37-
}
38-
}

compiler/rustc_mir_dataflow/src/rustc_peek.rs

+35-40
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast::MetaItem;
22
use rustc_hir::def_id::DefId;
33
use rustc_index::bit_set::BitSet;
4-
use rustc_middle::mir::{self, Body, Local, Location, MirPass};
4+
use rustc_middle::mir::{self, Body, Local, Location};
55
use rustc_middle::ty::{self, Ty, TyCtxt};
66
use rustc_span::symbol::{sym, Symbol};
77
use rustc_span::Span;
@@ -18,8 +18,6 @@ use crate::impls::{
1818
use crate::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
1919
use crate::{Analysis, JoinSemiLattice, ResultsCursor};
2020

21-
pub struct SanityCheck;
22-
2321
fn has_rustc_mir_with(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Option<MetaItem> {
2422
for attr in tcx.get_attrs(def_id, sym::rustc_mir) {
2523
let items = attr.meta_item_list();
@@ -33,53 +31,50 @@ fn has_rustc_mir_with(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Option<Me
3331
None
3432
}
3533

36-
// FIXME: This should be a `MirLint`, but it needs to be moved back to `rustc_mir_transform` first.
37-
impl<'tcx> MirPass<'tcx> for SanityCheck {
38-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
39-
let def_id = body.source.def_id();
40-
if !tcx.has_attr(def_id, sym::rustc_mir) {
41-
debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
42-
return;
43-
} else {
44-
debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
45-
}
34+
pub fn sanity_check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
35+
let def_id = body.source.def_id();
36+
if !tcx.has_attr(def_id, sym::rustc_mir) {
37+
debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
38+
return;
39+
} else {
40+
debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
41+
}
4642

47-
let param_env = tcx.param_env(def_id);
48-
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
43+
let param_env = tcx.param_env(def_id);
44+
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
4945

50-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
51-
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
52-
.into_engine(tcx, body)
53-
.iterate_to_fixpoint();
46+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
47+
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
48+
.into_engine(tcx, body)
49+
.iterate_to_fixpoint();
5450

55-
sanity_check_via_rustc_peek(tcx, flow_inits.into_results_cursor(body));
56-
}
51+
sanity_check_via_rustc_peek(tcx, flow_inits.into_results_cursor(body));
52+
}
5753

58-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_uninit).is_some() {
59-
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
60-
.into_engine(tcx, body)
61-
.iterate_to_fixpoint();
54+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_uninit).is_some() {
55+
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
56+
.into_engine(tcx, body)
57+
.iterate_to_fixpoint();
6258

63-
sanity_check_via_rustc_peek(tcx, flow_uninits.into_results_cursor(body));
64-
}
59+
sanity_check_via_rustc_peek(tcx, flow_uninits.into_results_cursor(body));
60+
}
6561

66-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_definite_init).is_some() {
67-
let flow_def_inits = DefinitelyInitializedPlaces::new(body, &move_data)
68-
.into_engine(tcx, body)
69-
.iterate_to_fixpoint();
62+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_definite_init).is_some() {
63+
let flow_def_inits = DefinitelyInitializedPlaces::new(body, &move_data)
64+
.into_engine(tcx, body)
65+
.iterate_to_fixpoint();
7066

71-
sanity_check_via_rustc_peek(tcx, flow_def_inits.into_results_cursor(body));
72-
}
67+
sanity_check_via_rustc_peek(tcx, flow_def_inits.into_results_cursor(body));
68+
}
7369

74-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_liveness).is_some() {
75-
let flow_liveness = MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint();
70+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_liveness).is_some() {
71+
let flow_liveness = MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint();
7672

77-
sanity_check_via_rustc_peek(tcx, flow_liveness.into_results_cursor(body));
78-
}
73+
sanity_check_via_rustc_peek(tcx, flow_liveness.into_results_cursor(body));
74+
}
7975

80-
if has_rustc_mir_with(tcx, def_id, sym::stop_after_dataflow).is_some() {
81-
tcx.dcx().emit_fatal(StopAfterDataFlowEndedCompilation);
82-
}
76+
if has_rustc_mir_with(tcx, def_id, sym::stop_after_dataflow).is_some() {
77+
tcx.dcx().emit_fatal(StopAfterDataFlowEndedCompilation);
8378
}
8479
}
8580

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_target::spec::PanicStrategy;
2222
#[derive(PartialEq)]
2323
pub struct AbortUnwindingCalls;
2424

25-
impl<'tcx> MirPass<'tcx> for AbortUnwindingCalls {
25+
impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
2626
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2727
let def_id = body.source.def_id();
2828
let kind = tcx.def_kind(def_id);

compiler/rustc_mir_transform/src/add_call_guards.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use self::AddCallGuards::*;
3030
*
3131
*/
3232

33-
impl<'tcx> MirPass<'tcx> for AddCallGuards {
33+
impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
3434
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3535
self.add_call_guards(body);
3636
}

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::util;
3737
/// blowup.
3838
pub struct AddMovesForPackedDrops;
3939

40-
impl<'tcx> MirPass<'tcx> for AddMovesForPackedDrops {
40+
impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
4141
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4242
debug!("add_moves_for_packed_drops({:?} @ {:?})", body.source, body.span);
4343
add_moves_for_packed_drops(tcx, body);

compiler/rustc_mir_transform/src/add_retag.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn may_contain_reference<'tcx>(ty: Ty<'tcx>, depth: u32, tcx: TyCtxt<'tcx>) -> b
4848
}
4949
}
5050

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

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn subtype_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
6262
checker.patcher.apply(body);
6363
}
6464

65-
impl<'tcx> MirPass<'tcx> for Subtyper {
65+
impl<'tcx> crate::MirPass<'tcx> for Subtyper {
6666
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
6767
subtype_finder(tcx, body);
6868
}

compiler/rustc_mir_transform/src/check_alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tracing::{debug, trace};
99

1010
pub struct CheckAlignment;
1111

12-
impl<'tcx> MirPass<'tcx> for CheckAlignment {
12+
impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
1313
fn is_enabled(&self, sess: &Session) -> bool {
1414
// FIXME(#112480) MSVC and rustc disagree on minimum stack alignment on x86 Windows
1515
if sess.target.llvm_target == "i686-pc-windows-msvc" {

compiler/rustc_mir_transform/src/check_const_item_mutation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use rustc_session::lint::builtin::CONST_ITEM_MUTATION;
66
use rustc_span::def_id::DefId;
77
use rustc_span::Span;
88

9-
use crate::{errors, MirLint};
9+
use crate::errors;
1010

1111
pub struct CheckConstItemMutation;
1212

13-
impl<'tcx> MirLint<'tcx> for CheckConstItemMutation {
13+
impl<'tcx> crate::MirLint<'tcx> for CheckConstItemMutation {
1414
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);

compiler/rustc_mir_transform/src/check_packed_ref.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use rustc_middle::mir::*;
33
use rustc_middle::span_bug;
44
use rustc_middle::ty::{self, TyCtxt};
55

6-
use crate::{errors, util, MirLint};
6+
use crate::{errors, util};
77

88
pub struct CheckPackedRef;
99

10-
impl<'tcx> MirLint<'tcx> for CheckPackedRef {
10+
impl<'tcx> crate::MirLint<'tcx> for CheckPackedRef {
1111
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1212
let param_env = tcx.param_env(body.source.def_id());
1313
let source_info = SourceInfo::outermost(body.span);

compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ use rustc_middle::mir::{Body, BorrowKind, CastKind, Rvalue, StatementKind, Termi
2121
use rustc_middle::ty::adjustment::PointerCoercion;
2222
use rustc_middle::ty::TyCtxt;
2323

24-
use crate::MirPass;
25-
2624
pub struct CleanupPostBorrowck;
2725

28-
impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck {
26+
impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
2927
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3028
for basic_block in body.basic_blocks.as_mut() {
3129
for statement in basic_block.statements.iter_mut() {

compiler/rustc_mir_transform/src/copy_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::ssa::SsaLocals;
1919
/// We want to replace all those locals by `_a`, either copied or moved.
2020
pub struct CopyProp;
2121

22-
impl<'tcx> MirPass<'tcx> for CopyProp {
22+
impl<'tcx> crate::MirPass<'tcx> for CopyProp {
2323
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2424
sess.mir_opt_level() >= 1
2525
}

compiler/rustc_mir_transform/src/coroutine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ fn check_field_tys_sized<'tcx>(
15351535
}
15361536
}
15371537

1538-
impl<'tcx> MirPass<'tcx> for StateTransform {
1538+
impl<'tcx> crate::MirPass<'tcx> for StateTransform {
15391539
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15401540
let Some(old_yield_ty) = body.yield_ty() else {
15411541
// This only applies to coroutines

compiler/rustc_mir_transform/src/coverage/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ use tracing::{debug, debug_span, instrument, trace};
2828
use crate::coverage::counters::{CounterIncrementSite, CoverageCounters};
2929
use crate::coverage::graph::CoverageGraph;
3030
use crate::coverage::mappings::ExtractedMappings;
31-
use crate::MirPass;
3231

3332
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
3433
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
3534
/// to construct the coverage map.
3635
pub struct InstrumentCoverage;
3736

38-
impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
37+
impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
3938
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
4039
sess.instrument_coverage()
4140
}

compiler/rustc_mir_transform/src/ctfe_limit.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ use rustc_middle::mir::{
88
use rustc_middle::ty::TyCtxt;
99
use tracing::instrument;
1010

11-
use crate::MirPass;
12-
1311
pub struct CtfeLimit;
1412

15-
impl<'tcx> MirPass<'tcx> for CtfeLimit {
13+
impl<'tcx> crate::MirPass<'tcx> for CtfeLimit {
1614
#[instrument(skip(self, _tcx, body))]
1715
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1816
let doms = body.basic_blocks.dominators();

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const PLACE_LIMIT: usize = 100;
2828

2929
pub struct DataflowConstProp;
3030

31-
impl<'tcx> MirPass<'tcx> for DataflowConstProp {
31+
impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
3232
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
3333
sess.mir_opt_level() >= 3
3434
}

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub enum DeadStoreElimination {
132132
Final,
133133
}
134134

135-
impl<'tcx> MirPass<'tcx> for DeadStoreElimination {
135+
impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
136136
fn name(&self) -> &'static str {
137137
match self {
138138
DeadStoreElimination::Initial => "DeadStoreElimination-initial",

compiler/rustc_mir_transform/src/deduplicate_blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::simplify::simplify_cfg;
1515

1616
pub struct DeduplicateBlocks;
1717

18-
impl<'tcx> MirPass<'tcx> for DeduplicateBlocks {
18+
impl<'tcx> crate::MirPass<'tcx> for DeduplicateBlocks {
1919
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2020
sess.mir_opt_level() >= 4
2121
}

compiler/rustc_mir_transform/src/deref_separator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
7878
checker.patcher.apply(body);
7979
}
8080

81-
impl<'tcx> MirPass<'tcx> for Derefer {
81+
impl<'tcx> crate::MirPass<'tcx> for Derefer {
8282
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8383
deref_finder(tcx, body);
8484
}

compiler/rustc_mir_transform/src/dest_prop.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ use rustc_mir_dataflow::points::{save_as_intervals, DenseLocationMap, PointIndex
146146
use rustc_mir_dataflow::Analysis;
147147
use tracing::{debug, trace};
148148

149-
use crate::MirPass;
150-
151149
pub struct DestinationPropagation;
152150

153-
impl<'tcx> MirPass<'tcx> for DestinationPropagation {
151+
impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
154152
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
155153
// For now, only run at MIR opt level 3. Two things need to be changed before this can be
156154
// turned on by default:

0 commit comments

Comments
 (0)