]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/pass_manager.rs
Rollup merge of #105208 - chenyukang:yukang/fix-105069, r=cjgillot
[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
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>(
74     tcx: TyCtxt<'tcx>,
75     body: &mut Body<'tcx>,
76     passes: &[&dyn MirPass<'tcx>],
77     phase_change: Option<MirPhase>,
78 ) {
79     run_passes_inner(tcx, body, passes, phase_change, false);
80 }
81
82 /// The optional `phase_change` is applied after executing all the passes, if present
83 pub fn run_passes<'tcx>(
84     tcx: TyCtxt<'tcx>,
85     body: &mut Body<'tcx>,
86     passes: &[&dyn MirPass<'tcx>],
87     phase_change: Option<MirPhase>,
88 ) {
89     run_passes_inner(tcx, body, passes, phase_change, true);
90 }
91
92 fn run_passes_inner<'tcx>(
93     tcx: TyCtxt<'tcx>,
94     body: &mut Body<'tcx>,
95     passes: &[&dyn MirPass<'tcx>],
96     phase_change: Option<MirPhase>,
97     validate_each: bool,
98 ) {
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);
102
103     if !body.should_skip() {
104         for pass in passes {
105             let name = pass.name();
106
107             let overridden = overridden_passes.iter().rev().find(|(s, _)| s == &*name).map(
108                 |(_name, polarity)| {
109                     trace!(
110                         pass = %name,
111                         "{} as requested by flag",
112                         if *polarity { "Running" } else { "Not running" },
113                     );
114                     *polarity
115                 },
116             );
117             if !overridden.unwrap_or_else(|| pass.is_enabled(&tcx.sess)) {
118                 continue;
119             }
120
121             let dump_enabled = pass.is_mir_dump_enabled();
122
123             if dump_enabled {
124                 dump_mir_for_pass(tcx, body, &name, false);
125             }
126             if validate {
127                 validate_body(tcx, body, format!("before pass {}", name));
128             }
129
130             pass.run_pass(tcx, body);
131
132             if dump_enabled {
133                 dump_mir_for_pass(tcx, body, &name, true);
134             }
135             if validate {
136                 validate_body(tcx, body, format!("after pass {}", name));
137             }
138
139             body.pass_count += 1;
140         }
141     }
142
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);
146         }
147
148         body.phase = new_phase;
149
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));
153         }
154
155         body.pass_count = 1;
156     }
157 }
158
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);
161 }
162
163 pub fn dump_mir_for_pass<'tcx>(
164     tcx: TyCtxt<'tcx>,
165     body: &Body<'tcx>,
166     pass_name: &str,
167     is_after: bool,
168 ) {
169     let phase_index = body.phase.phase_index();
170
171     mir::dump_mir(
172         tcx,
173         Some(&format_args!("{:03}-{:03}", phase_index, body.pass_count)),
174         pass_name,
175         if is_after { &"after" } else { &"before" },
176         body,
177         |_, _| Ok(()),
178     );
179 }
180
181 pub fn dump_mir_for_phase_change<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
182     let phase_index = body.phase.phase_index();
183
184     mir::dump_mir(
185         tcx,
186         Some(&format_args!("{:03}-000", phase_index)),
187         &format!("{}", body.phase),
188         &"after",
189         body,
190         |_, _| Ok(()),
191     )
192 }