3 use rustc_middle::mir::{self, Body, MirPhase, RuntimePhase};
4 use rustc_middle::ty::TyCtxt;
5 use rustc_session::Session;
7 use crate::{validate, MirPass};
9 /// Just like `MirPass`, except it cannot mutate `Body`.
10 pub trait MirLint<'tcx> {
11 fn name(&self) -> Cow<'_, str> {
12 let name = std::any::type_name::<Self>();
13 if let Some(tail) = name.rfind(':') {
14 Cow::from(&name[tail + 1..])
20 fn is_enabled(&self, _sess: &Session) -> bool {
24 fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>);
27 /// An adapter for `MirLint`s that implements `MirPass`.
28 #[derive(Debug, Clone)]
29 pub struct Lint<T>(pub T);
31 impl<'tcx, T> MirPass<'tcx> for Lint<T>
35 fn name(&self) -> Cow<'_, str> {
39 fn is_enabled(&self, sess: &Session) -> bool {
40 self.0.is_enabled(sess)
43 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
44 self.0.run_lint(tcx, body)
47 fn is_mir_dump_enabled(&self) -> bool {
52 pub struct WithMinOptLevel<T>(pub u32, pub T);
54 impl<'tcx, T> MirPass<'tcx> for WithMinOptLevel<T>
58 fn name(&self) -> Cow<'_, str> {
62 fn is_enabled(&self, sess: &Session) -> bool {
63 sess.mir_opt_level() >= self.0 as usize
66 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
67 self.1.run_pass(tcx, body)
71 /// Run the sequence of passes without validating the MIR after each pass. The MIR is still
72 /// validated at the end.
73 pub fn run_passes_no_validate<'tcx>(
75 body: &mut Body<'tcx>,
76 passes: &[&dyn MirPass<'tcx>],
77 phase_change: Option<MirPhase>,
79 run_passes_inner(tcx, body, passes, phase_change, false);
82 /// The optional `phase_change` is applied after executing all the passes, if present
83 pub fn run_passes<'tcx>(
85 body: &mut Body<'tcx>,
86 passes: &[&dyn MirPass<'tcx>],
87 phase_change: Option<MirPhase>,
89 run_passes_inner(tcx, body, passes, phase_change, true);
92 fn run_passes_inner<'tcx>(
94 body: &mut Body<'tcx>,
95 passes: &[&dyn MirPass<'tcx>],
96 phase_change: Option<MirPhase>,
99 let validate = validate_each & tcx.sess.opts.unstable_opts.validate_mir & !body.should_skip();
100 let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
101 trace!(?overridden_passes);
103 if !body.should_skip() {
105 let name = pass.name();
107 let overridden = overridden_passes.iter().rev().find(|(s, _)| s == &*name).map(
108 |(_name, polarity)| {
111 "{} as requested by flag",
112 if *polarity { "Running" } else { "Not running" },
117 if !overridden.unwrap_or_else(|| pass.is_enabled(&tcx.sess)) {
121 let dump_enabled = pass.is_mir_dump_enabled();
124 dump_mir_for_pass(tcx, body, &name, false);
127 validate_body(tcx, body, format!("before pass {}", name));
130 pass.run_pass(tcx, body);
133 dump_mir_for_pass(tcx, body, &name, true);
136 validate_body(tcx, body, format!("after pass {}", name));
139 body.pass_count += 1;
143 if let Some(new_phase) = phase_change {
144 if body.phase >= new_phase {
145 panic!("Invalid MIR phase transition from {:?} to {:?}", body.phase, new_phase);
148 body.phase = new_phase;
150 dump_mir_for_phase_change(tcx, body);
151 if validate || new_phase == MirPhase::Runtime(RuntimePhase::Optimized) {
152 validate_body(tcx, body, format!("after phase change to {}", new_phase));
159 pub fn validate_body<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when: String) {
160 validate::Validator { when, mir_phase: body.phase }.run_pass(tcx, body);
163 pub fn dump_mir_for_pass<'tcx>(
169 let phase_index = body.phase.phase_index();
173 Some(&format_args!("{:03}-{:03}", phase_index, body.pass_count)),
175 if is_after { &"after" } else { &"before" },
181 pub fn dump_mir_for_phase_change<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
182 let phase_index = body.phase.phase_index();
186 Some(&format_args!("{:03}-000", phase_index)),
187 &format!("{}", body.phase),