Adjust SanityCheck.

The actual implementation remains in `rustc_mir_dataflow`, but this
commit moves the `MirPass` impl to `rustc_mir_transform` and changes it
to a `MirLint` (fixing a `FIXME` comment).

(I originally tried moving the full implementation from
`rustc_mir_dataflow` but I had some trait problems with `HasMoveData`
and `RustcPeekAt` and `MaybeLiveLocals`. This commit was much smaller
and simpler, but still will allow some follow-up cleanups.)
This commit is contained in:
Nicholas Nethercote 2024-09-03 15:01:46 +10:00
parent bd53aa3bf7
commit 5410900aaa
3 changed files with 50 additions and 42 deletions

View File

@ -1,7 +1,7 @@
use rustc_ast::MetaItem; use rustc_ast::MetaItem;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_index::bit_set::BitSet; use rustc_index::bit_set::BitSet;
use rustc_middle::mir::{self, Body, Local, Location, MirPass}; use rustc_middle::mir::{self, Body, Local, Location};
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span; use rustc_span::Span;
@ -18,8 +18,6 @@ use crate::impls::{
use crate::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex}; use crate::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
use crate::{Analysis, JoinSemiLattice, ResultsCursor}; use crate::{Analysis, JoinSemiLattice, ResultsCursor};
pub struct SanityCheck;
fn has_rustc_mir_with(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Option<MetaItem> { fn has_rustc_mir_with(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Option<MetaItem> {
for attr in tcx.get_attrs(def_id, sym::rustc_mir) { for attr in tcx.get_attrs(def_id, sym::rustc_mir) {
let items = attr.meta_item_list(); let items = attr.meta_item_list();
@ -33,53 +31,50 @@ fn has_rustc_mir_with(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Option<Me
None None
} }
// FIXME: This should be a `MirLint`, but it needs to be moved back to `rustc_mir_transform` first. pub fn sanity_check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
impl<'tcx> MirPass<'tcx> for SanityCheck { let def_id = body.source.def_id();
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { if !tcx.has_attr(def_id, sym::rustc_mir) {
let def_id = body.source.def_id(); debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
if !tcx.has_attr(def_id, sym::rustc_mir) { return;
debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id)); } else {
return; debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
} else { }
debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
}
let param_env = tcx.param_env(def_id); let param_env = tcx.param_env(def_id);
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true); let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() { if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data) let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
.into_engine(tcx, body) .into_engine(tcx, body)
.iterate_to_fixpoint(); .iterate_to_fixpoint();
sanity_check_via_rustc_peek(tcx, flow_inits.into_results_cursor(body)); sanity_check_via_rustc_peek(tcx, flow_inits.into_results_cursor(body));
} }
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_uninit).is_some() { if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_uninit).is_some() {
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data) let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
.into_engine(tcx, body) .into_engine(tcx, body)
.iterate_to_fixpoint(); .iterate_to_fixpoint();
sanity_check_via_rustc_peek(tcx, flow_uninits.into_results_cursor(body)); sanity_check_via_rustc_peek(tcx, flow_uninits.into_results_cursor(body));
} }
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_definite_init).is_some() { if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_definite_init).is_some() {
let flow_def_inits = DefinitelyInitializedPlaces::new(body, &move_data) let flow_def_inits = DefinitelyInitializedPlaces::new(body, &move_data)
.into_engine(tcx, body) .into_engine(tcx, body)
.iterate_to_fixpoint(); .iterate_to_fixpoint();
sanity_check_via_rustc_peek(tcx, flow_def_inits.into_results_cursor(body)); sanity_check_via_rustc_peek(tcx, flow_def_inits.into_results_cursor(body));
} }
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_liveness).is_some() { if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_liveness).is_some() {
let flow_liveness = MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint(); let flow_liveness = MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint();
sanity_check_via_rustc_peek(tcx, flow_liveness.into_results_cursor(body)); sanity_check_via_rustc_peek(tcx, flow_liveness.into_results_cursor(body));
} }
if has_rustc_mir_with(tcx, def_id, sym::stop_after_dataflow).is_some() { if has_rustc_mir_with(tcx, def_id, sym::stop_after_dataflow).is_some() {
tcx.dcx().emit_fatal(StopAfterDataFlowEndedCompilation); tcx.dcx().emit_fatal(StopAfterDataFlowEndedCompilation);
}
} }
} }

View File

@ -32,7 +32,6 @@ use rustc_middle::mir::{
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
use rustc_middle::util::Providers; use rustc_middle::util::Providers;
use rustc_middle::{bug, query, span_bug}; use rustc_middle::{bug, query, span_bug};
use rustc_mir_dataflow::rustc_peek;
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
use rustc_span::{sym, DUMMY_SP}; use rustc_span::{sym, DUMMY_SP};
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
@ -96,6 +95,7 @@ mod remove_unneeded_drops;
mod remove_zsts; mod remove_zsts;
mod required_consts; mod required_consts;
mod reveal_all; mod reveal_all;
mod sanity_check;
mod shim; mod shim;
mod ssa; mod ssa;
// This pass is public to allow external drivers to perform MIR cleanup // This pass is public to allow external drivers to perform MIR cleanup
@ -288,7 +288,7 @@ fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
&Lint(function_item_references::FunctionItemReferences), &Lint(function_item_references::FunctionItemReferences),
// What we need to do constant evaluation. // What we need to do constant evaluation.
&simplify::SimplifyCfg::Initial, &simplify::SimplifyCfg::Initial,
&rustc_peek::SanityCheck, // Just a lint &Lint(sanity_check::SanityCheck),
], ],
None, None,
); );

View File

@ -0,0 +1,13 @@
use rustc_middle::mir::Body;
use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::rustc_peek::sanity_check;
use crate::MirLint;
pub(super) struct SanityCheck;
impl<'tcx> MirLint<'tcx> for SanityCheck {
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
sanity_check(tcx, body);
}
}