2021-11-30 06:46:32 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2022-07-10 01:04:38 +00:00
|
|
|
use rustc_middle::mir::{self, Body, MirPhase, RuntimePhase};
|
2021-11-30 06:46:32 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
|
|
|
use rustc_session::Session;
|
|
|
|
|
|
|
|
use crate::{validate, MirPass};
|
|
|
|
|
|
|
|
/// Just like `MirPass`, except it cannot mutate `Body`.
|
|
|
|
pub trait MirLint<'tcx> {
|
|
|
|
fn name(&self) -> Cow<'_, str> {
|
|
|
|
let name = std::any::type_name::<Self>();
|
|
|
|
if let Some(tail) = name.rfind(':') {
|
|
|
|
Cow::from(&name[tail + 1..])
|
|
|
|
} else {
|
|
|
|
Cow::from(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_enabled(&self, _sess: &Session) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An adapter for `MirLint`s that implements `MirPass`.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Lint<T>(pub T);
|
|
|
|
|
2021-12-06 08:48:37 +00:00
|
|
|
impl<'tcx, T> MirPass<'tcx> for Lint<T>
|
2021-11-30 06:46:32 +00:00
|
|
|
where
|
|
|
|
T: MirLint<'tcx>,
|
|
|
|
{
|
|
|
|
fn name(&self) -> Cow<'_, str> {
|
|
|
|
self.0.name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_enabled(&self, sess: &Session) -> bool {
|
|
|
|
self.0.is_enabled(sess)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
self.0.run_lint(tcx, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_mir_dump_enabled(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 22:38:04 +00:00
|
|
|
pub struct WithMinOptLevel<T>(pub u32, pub T);
|
|
|
|
|
2021-12-06 08:48:37 +00:00
|
|
|
impl<'tcx, T> MirPass<'tcx> for WithMinOptLevel<T>
|
2021-12-02 22:38:04 +00:00
|
|
|
where
|
|
|
|
T: MirPass<'tcx>,
|
|
|
|
{
|
|
|
|
fn name(&self) -> Cow<'_, str> {
|
|
|
|
self.1.name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_enabled(&self, sess: &Session) -> bool {
|
|
|
|
sess.mir_opt_level() >= self.0 as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
self.1.run_pass(tcx, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn phase_change(&self) -> Option<MirPhase> {
|
|
|
|
self.1.phase_change()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 01:04:38 +00:00
|
|
|
/// Run the sequence of passes without validating the MIR after each pass. The MIR is still
|
|
|
|
/// validated at the end.
|
|
|
|
pub fn run_passes_no_validate<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &mut Body<'tcx>,
|
|
|
|
passes: &[&dyn MirPass<'tcx>],
|
|
|
|
) {
|
|
|
|
run_passes_inner(tcx, body, passes, false);
|
|
|
|
}
|
|
|
|
|
2021-12-06 08:48:37 +00:00
|
|
|
pub fn run_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, passes: &[&dyn MirPass<'tcx>]) {
|
2022-07-10 01:04:38 +00:00
|
|
|
run_passes_inner(tcx, body, passes, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_passes_inner<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &mut Body<'tcx>,
|
|
|
|
passes: &[&dyn MirPass<'tcx>],
|
|
|
|
validate_each: bool,
|
|
|
|
) {
|
2021-11-30 06:46:32 +00:00
|
|
|
let start_phase = body.phase;
|
|
|
|
let mut cnt = 0;
|
|
|
|
|
2022-07-10 01:04:38 +00:00
|
|
|
let validate = validate_each & tcx.sess.opts.unstable_opts.validate_mir;
|
2022-07-06 12:44:47 +00:00
|
|
|
let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
|
2022-04-16 22:44:27 +00:00
|
|
|
trace!(?overridden_passes);
|
2021-11-30 06:46:32 +00:00
|
|
|
|
|
|
|
for pass in passes {
|
|
|
|
let name = pass.name();
|
2022-04-11 19:17:52 +00:00
|
|
|
|
2022-08-08 00:19:16 +00:00
|
|
|
// Gather information about what we should be doing for this pass
|
2022-09-07 10:32:28 +00:00
|
|
|
let overridden =
|
2022-08-08 00:19:16 +00:00
|
|
|
overridden_passes.iter().rev().find(|(s, _)| s == &*name).map(|(_name, polarity)| {
|
|
|
|
trace!(
|
|
|
|
pass = %name,
|
|
|
|
"{} as requested by flag",
|
|
|
|
if *polarity { "Running" } else { "Not running" },
|
|
|
|
);
|
|
|
|
*polarity
|
|
|
|
});
|
2022-09-07 10:32:28 +00:00
|
|
|
let is_enabled = overridden.unwrap_or_else(|| pass.is_enabled(&tcx.sess));
|
2022-08-08 00:19:16 +00:00
|
|
|
let new_phase = pass.phase_change();
|
|
|
|
let dump_enabled = (is_enabled && pass.is_mir_dump_enabled()) || new_phase.is_some();
|
|
|
|
let validate = (validate && is_enabled)
|
|
|
|
|| new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized));
|
2021-11-30 06:46:32 +00:00
|
|
|
|
|
|
|
if dump_enabled {
|
|
|
|
dump_mir(tcx, body, start_phase, &name, cnt, false);
|
|
|
|
}
|
2022-08-08 00:19:16 +00:00
|
|
|
if is_enabled {
|
|
|
|
pass.run_pass(tcx, body);
|
|
|
|
}
|
2021-11-30 06:46:32 +00:00
|
|
|
if dump_enabled {
|
|
|
|
dump_mir(tcx, body, start_phase, &name, cnt, true);
|
|
|
|
cnt += 1;
|
|
|
|
}
|
|
|
|
if let Some(new_phase) = pass.phase_change() {
|
|
|
|
if body.phase >= new_phase {
|
|
|
|
panic!("Invalid MIR phase transition from {:?} to {:?}", body.phase, new_phase);
|
|
|
|
}
|
|
|
|
|
|
|
|
body.phase = new_phase;
|
|
|
|
}
|
|
|
|
if validate {
|
2022-08-08 00:19:16 +00:00
|
|
|
validate_body(tcx, body, format!("after pass {}", name));
|
2021-11-30 06:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 08:48:37 +00:00
|
|
|
pub fn validate_body<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when: String) {
|
2021-11-30 06:46:32 +00:00
|
|
|
validate::Validator { when, mir_phase: body.phase }.run_pass(tcx, body);
|
|
|
|
}
|
|
|
|
|
2021-12-06 08:48:37 +00:00
|
|
|
pub fn dump_mir<'tcx>(
|
2021-11-30 06:46:32 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
phase: MirPhase,
|
|
|
|
pass_name: &str,
|
|
|
|
cnt: usize,
|
|
|
|
is_after: bool,
|
|
|
|
) {
|
2022-07-10 01:04:38 +00:00
|
|
|
let phase_index = phase.phase_index();
|
2021-11-30 06:46:32 +00:00
|
|
|
|
|
|
|
mir::dump_mir(
|
|
|
|
tcx,
|
|
|
|
Some(&format_args!("{:03}-{:03}", phase_index, cnt)),
|
|
|
|
pass_name,
|
|
|
|
if is_after { &"after" } else { &"before" },
|
|
|
|
body,
|
|
|
|
|_, _| Ok(()),
|
|
|
|
);
|
|
|
|
}
|