|
| 1 | +use std::borrow::Cow; |
| 2 | + |
| 3 | +use rustc_middle::mir::{self, Body, MirPhase}; |
| 4 | +use rustc_middle::ty::TyCtxt; |
| 5 | +use rustc_session::Session; |
| 6 | + |
| 7 | +use crate::{validate, MirPass}; |
| 8 | + |
| 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..]) |
| 15 | + } else { |
| 16 | + Cow::from(name) |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + fn is_enabled(&self, _sess: &Session) -> bool { |
| 21 | + true |
| 22 | + } |
| 23 | + |
| 24 | + fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>); |
| 25 | +} |
| 26 | + |
| 27 | +/// An adapter for `MirLint`s that implements `MirPass`. |
| 28 | +#[derive(Debug, Clone)] |
| 29 | +pub struct Lint<T>(pub T); |
| 30 | + |
| 31 | +impl<T> MirPass<'tcx> for Lint<T> |
| 32 | +where |
| 33 | + T: MirLint<'tcx>, |
| 34 | +{ |
| 35 | + fn name(&self) -> Cow<'_, str> { |
| 36 | + self.0.name() |
| 37 | + } |
| 38 | + |
| 39 | + fn is_enabled(&self, sess: &Session) -> bool { |
| 40 | + self.0.is_enabled(sess) |
| 41 | + } |
| 42 | + |
| 43 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 44 | + self.0.run_lint(tcx, body) |
| 45 | + } |
| 46 | + |
| 47 | + fn is_mir_dump_enabled(&self) -> bool { |
| 48 | + false |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub fn run_passes(tcx: TyCtxt<'tcx>, body: &'mir mut Body<'tcx>, passes: &[&dyn MirPass<'tcx>]) { |
| 53 | + let start_phase = body.phase; |
| 54 | + let mut cnt = 0; |
| 55 | + |
| 56 | + let validate = tcx.sess.opts.debugging_opts.validate_mir; |
| 57 | + |
| 58 | + if validate { |
| 59 | + validate_body(tcx, body, format!("start of phase transition from {:?}", start_phase)); |
| 60 | + } |
| 61 | + |
| 62 | + for pass in passes { |
| 63 | + if !pass.is_enabled(&tcx.sess) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + let name = pass.name(); |
| 68 | + let dump_enabled = pass.is_mir_dump_enabled(); |
| 69 | + |
| 70 | + if dump_enabled { |
| 71 | + dump_mir(tcx, body, start_phase, &name, cnt, false); |
| 72 | + } |
| 73 | + |
| 74 | + pass.run_pass(tcx, body); |
| 75 | + |
| 76 | + if dump_enabled { |
| 77 | + dump_mir(tcx, body, start_phase, &name, cnt, true); |
| 78 | + cnt += 1; |
| 79 | + } |
| 80 | + |
| 81 | + if let Some(new_phase) = pass.phase_change() { |
| 82 | + if body.phase >= new_phase { |
| 83 | + panic!("Invalid MIR phase transition from {:?} to {:?}", body.phase, new_phase); |
| 84 | + } |
| 85 | + |
| 86 | + body.phase = new_phase; |
| 87 | + } |
| 88 | + |
| 89 | + if validate { |
| 90 | + validate_body(tcx, body, format!("after pass {}", pass.name())); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if validate || body.phase == MirPhase::Optimization { |
| 95 | + validate_body(tcx, body, format!("end of phase transition to {:?}", body.phase)); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +pub fn validate_body(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when: String) { |
| 100 | + validate::Validator { when, mir_phase: body.phase }.run_pass(tcx, body); |
| 101 | +} |
| 102 | + |
| 103 | +pub fn dump_mir( |
| 104 | + tcx: TyCtxt<'tcx>, |
| 105 | + body: &Body<'tcx>, |
| 106 | + phase: MirPhase, |
| 107 | + pass_name: &str, |
| 108 | + cnt: usize, |
| 109 | + is_after: bool, |
| 110 | +) { |
| 111 | + let phase_index = phase as u32; |
| 112 | + |
| 113 | + mir::dump_mir( |
| 114 | + tcx, |
| 115 | + Some(&format_args!("{:03}-{:03}", phase_index, cnt)), |
| 116 | + pass_name, |
| 117 | + if is_after { &"after" } else { &"before" }, |
| 118 | + body, |
| 119 | + |_, _| Ok(()), |
| 120 | + ); |
| 121 | +} |
0 commit comments