]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/pass_manager.rs
Rollup merge of #100984 - ChrisDenton:reinstate-init, r=Mark-Simulacrum
[rust.git] / compiler / rustc_mir_transform / src / pass_manager.rs
1 use std::borrow::Cow;
2
3 use rustc_middle::mir::{self, Body, MirPhase, RuntimePhase};
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<'tcx, 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 struct WithMinOptLevel<T>(pub u32, pub T);
53
54 impl<'tcx, T> MirPass<'tcx> for WithMinOptLevel<T>
55 where
56     T: MirPass<'tcx>,
57 {
58     fn name(&self) -> Cow<'_, str> {
59         self.1.name()
60     }
61
62     fn is_enabled(&self, sess: &Session) -> bool {
63         sess.mir_opt_level() >= self.0 as usize
64     }
65
66     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
67         self.1.run_pass(tcx, body)
68     }
69
70     fn phase_change(&self) -> Option<MirPhase> {
71         self.1.phase_change()
72     }
73 }
74
75 /// Run the sequence of passes without validating the MIR after each pass. The MIR is still
76 /// validated at the end.
77 pub fn run_passes_no_validate<'tcx>(
78     tcx: TyCtxt<'tcx>,
79     body: &mut Body<'tcx>,
80     passes: &[&dyn MirPass<'tcx>],
81 ) {
82     run_passes_inner(tcx, body, passes, false);
83 }
84
85 pub fn run_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, passes: &[&dyn MirPass<'tcx>]) {
86     run_passes_inner(tcx, body, passes, true);
87 }
88
89 fn run_passes_inner<'tcx>(
90     tcx: TyCtxt<'tcx>,
91     body: &mut Body<'tcx>,
92     passes: &[&dyn MirPass<'tcx>],
93     validate_each: bool,
94 ) {
95     let start_phase = body.phase;
96     let mut cnt = 0;
97
98     let validate = validate_each & tcx.sess.opts.unstable_opts.validate_mir;
99     let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
100     trace!(?overridden_passes);
101
102     for pass in passes {
103         let name = pass.name();
104
105         // Gather information about what we should be doing for this pass
106         let overriden =
107             overridden_passes.iter().rev().find(|(s, _)| s == &*name).map(|(_name, polarity)| {
108                 trace!(
109                     pass = %name,
110                     "{} as requested by flag",
111                     if *polarity { "Running" } else { "Not running" },
112                 );
113                 *polarity
114             });
115         let is_enabled = overriden.unwrap_or_else(|| pass.is_enabled(&tcx.sess));
116         let new_phase = pass.phase_change();
117         let dump_enabled = (is_enabled && pass.is_mir_dump_enabled()) || new_phase.is_some();
118         let validate = (validate && is_enabled)
119             || new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized));
120
121         if dump_enabled {
122             dump_mir(tcx, body, start_phase, &name, cnt, false);
123         }
124         if is_enabled {
125             pass.run_pass(tcx, body);
126         }
127         if dump_enabled {
128             dump_mir(tcx, body, start_phase, &name, cnt, true);
129             cnt += 1;
130         }
131         if let Some(new_phase) = pass.phase_change() {
132             if body.phase >= new_phase {
133                 panic!("Invalid MIR phase transition from {:?} to {:?}", body.phase, new_phase);
134             }
135
136             body.phase = new_phase;
137         }
138         if validate {
139             validate_body(tcx, body, format!("after pass {}", name));
140         }
141     }
142 }
143
144 pub fn validate_body<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when: String) {
145     validate::Validator { when, mir_phase: body.phase }.run_pass(tcx, body);
146 }
147
148 pub fn dump_mir<'tcx>(
149     tcx: TyCtxt<'tcx>,
150     body: &Body<'tcx>,
151     phase: MirPhase,
152     pass_name: &str,
153     cnt: usize,
154     is_after: bool,
155 ) {
156     let phase_index = phase.phase_index();
157
158     mir::dump_mir(
159         tcx,
160         Some(&format_args!("{:03}-{:03}", phase_index, cnt)),
161         pass_name,
162         if is_after { &"after" } else { &"before" },
163         body,
164         |_, _| Ok(()),
165     );
166 }