]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/pass_manager.rs
Rollup merge of #105098 - lyming2007:issue-103869-fix, r=eholk
[rust.git] / compiler / rustc_mir_transform / src / pass_manager.rs
1 use rustc_middle::mir::{self, Body, MirPhase, RuntimePhase};
2 use rustc_middle::ty::TyCtxt;
3 use rustc_session::Session;
4
5 use crate::{validate, MirPass};
6
7 /// Just like `MirPass`, except it cannot mutate `Body`.
8 pub trait MirLint<'tcx> {
9     fn name(&self) -> &str {
10         let name = std::any::type_name::<Self>();
11         if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }
12     }
13
14     fn is_enabled(&self, _sess: &Session) -> bool {
15         true
16     }
17
18     fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>);
19 }
20
21 /// An adapter for `MirLint`s that implements `MirPass`.
22 #[derive(Debug, Clone)]
23 pub struct Lint<T>(pub T);
24
25 impl<'tcx, T> MirPass<'tcx> for Lint<T>
26 where
27     T: MirLint<'tcx>,
28 {
29     fn name(&self) -> &str {
30         self.0.name()
31     }
32
33     fn is_enabled(&self, sess: &Session) -> bool {
34         self.0.is_enabled(sess)
35     }
36
37     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
38         self.0.run_lint(tcx, body)
39     }
40
41     fn is_mir_dump_enabled(&self) -> bool {
42         false
43     }
44 }
45
46 pub struct WithMinOptLevel<T>(pub u32, pub T);
47
48 impl<'tcx, T> MirPass<'tcx> for WithMinOptLevel<T>
49 where
50     T: MirPass<'tcx>,
51 {
52     fn name(&self) -> &str {
53         self.1.name()
54     }
55
56     fn is_enabled(&self, sess: &Session) -> bool {
57         sess.mir_opt_level() >= self.0 as usize
58     }
59
60     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
61         self.1.run_pass(tcx, body)
62     }
63 }
64
65 /// Run the sequence of passes without validating the MIR after each pass. The MIR is still
66 /// validated at the end.
67 pub fn run_passes_no_validate<'tcx>(
68     tcx: TyCtxt<'tcx>,
69     body: &mut Body<'tcx>,
70     passes: &[&dyn MirPass<'tcx>],
71     phase_change: Option<MirPhase>,
72 ) {
73     run_passes_inner(tcx, body, passes, phase_change, false);
74 }
75
76 /// The optional `phase_change` is applied after executing all the passes, if present
77 pub fn run_passes<'tcx>(
78     tcx: TyCtxt<'tcx>,
79     body: &mut Body<'tcx>,
80     passes: &[&dyn MirPass<'tcx>],
81     phase_change: Option<MirPhase>,
82 ) {
83     run_passes_inner(tcx, body, passes, phase_change, true);
84 }
85
86 fn run_passes_inner<'tcx>(
87     tcx: TyCtxt<'tcx>,
88     body: &mut Body<'tcx>,
89     passes: &[&dyn MirPass<'tcx>],
90     phase_change: Option<MirPhase>,
91     validate_each: bool,
92 ) {
93     let validate = validate_each & tcx.sess.opts.unstable_opts.validate_mir & !body.should_skip();
94     let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
95     trace!(?overridden_passes);
96
97     if !body.should_skip() {
98         for pass in passes {
99             let name = pass.name();
100
101             let overridden = overridden_passes.iter().rev().find(|(s, _)| s == &*name).map(
102                 |(_name, polarity)| {
103                     trace!(
104                         pass = %name,
105                         "{} as requested by flag",
106                         if *polarity { "Running" } else { "Not running" },
107                     );
108                     *polarity
109                 },
110             );
111             if !overridden.unwrap_or_else(|| pass.is_enabled(&tcx.sess)) {
112                 continue;
113             }
114
115             let dump_enabled = pass.is_mir_dump_enabled();
116
117             if dump_enabled {
118                 dump_mir_for_pass(tcx, body, &name, false);
119             }
120             if validate {
121                 validate_body(tcx, body, format!("before pass {}", name));
122             }
123
124             pass.run_pass(tcx, body);
125
126             if dump_enabled {
127                 dump_mir_for_pass(tcx, body, &name, true);
128             }
129             if validate {
130                 validate_body(tcx, body, format!("after pass {}", name));
131             }
132
133             body.pass_count += 1;
134         }
135     }
136
137     if let Some(new_phase) = phase_change {
138         if body.phase >= new_phase {
139             panic!("Invalid MIR phase transition from {:?} to {:?}", body.phase, new_phase);
140         }
141
142         body.phase = new_phase;
143         body.pass_count = 0;
144
145         dump_mir_for_phase_change(tcx, body);
146         if validate || new_phase == MirPhase::Runtime(RuntimePhase::Optimized) {
147             validate_body(tcx, body, format!("after phase change to {}", new_phase.name()));
148         }
149
150         body.pass_count = 1;
151     }
152 }
153
154 pub fn validate_body<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when: String) {
155     validate::Validator { when, mir_phase: body.phase }.run_pass(tcx, body);
156 }
157
158 pub fn dump_mir_for_pass<'tcx>(
159     tcx: TyCtxt<'tcx>,
160     body: &Body<'tcx>,
161     pass_name: &str,
162     is_after: bool,
163 ) {
164     mir::dump_mir(
165         tcx,
166         true,
167         pass_name,
168         if is_after { &"after" } else { &"before" },
169         body,
170         |_, _| Ok(()),
171     );
172 }
173
174 pub fn dump_mir_for_phase_change<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
175     assert_eq!(body.pass_count, 0);
176     mir::dump_mir(tcx, true, body.phase.name(), &"after", body, |_, _| Ok(()))
177 }