]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/pass_manager.rs
Add fine-grained LLVM CFI support to the Rust compiler
[rust.git] / compiler / rustc_mir_transform / src / pass_manager.rs
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<'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 pub fn run_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, passes: &[&dyn MirPass<'tcx>]) {
76     let start_phase = body.phase;
77     let mut cnt = 0;
78
79     let validate = tcx.sess.opts.debugging_opts.validate_mir;
80     let overridden_passes = &tcx.sess.opts.debugging_opts.mir_enable_passes;
81     trace!(?overridden_passes);
82
83     if validate {
84         validate_body(tcx, body, format!("start of phase transition from {:?}", start_phase));
85     }
86
87     for pass in passes {
88         let name = pass.name();
89
90         if let Some((_, polarity)) = overridden_passes.iter().rev().find(|(s, _)| s == &*name) {
91             trace!(
92                 pass = %name,
93                 "{} as requested by flag",
94                 if *polarity { "Running" } else { "Not running" },
95             );
96             if !polarity {
97                 continue;
98             }
99         } else {
100             if !pass.is_enabled(&tcx.sess) {
101                 continue;
102             }
103         }
104         let dump_enabled = pass.is_mir_dump_enabled();
105
106         if dump_enabled {
107             dump_mir(tcx, body, start_phase, &name, cnt, false);
108         }
109
110         pass.run_pass(tcx, body);
111
112         if dump_enabled {
113             dump_mir(tcx, body, start_phase, &name, cnt, true);
114             cnt += 1;
115         }
116
117         if let Some(new_phase) = pass.phase_change() {
118             if body.phase >= new_phase {
119                 panic!("Invalid MIR phase transition from {:?} to {:?}", body.phase, new_phase);
120             }
121
122             body.phase = new_phase;
123         }
124
125         if validate {
126             validate_body(tcx, body, format!("after pass {}", pass.name()));
127         }
128     }
129
130     if validate || body.phase == MirPhase::Optimized {
131         validate_body(tcx, body, format!("end of phase transition to {:?}", body.phase));
132     }
133 }
134
135 pub fn validate_body<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when: String) {
136     validate::Validator { when, mir_phase: body.phase }.run_pass(tcx, body);
137 }
138
139 pub fn dump_mir<'tcx>(
140     tcx: TyCtxt<'tcx>,
141     body: &Body<'tcx>,
142     phase: MirPhase,
143     pass_name: &str,
144     cnt: usize,
145     is_after: bool,
146 ) {
147     let phase_index = phase as u32;
148
149     mir::dump_mir(
150         tcx,
151         Some(&format_args!("{:03}-{:03}", phase_index, cnt)),
152         pass_name,
153         if is_after { &"after" } else { &"before" },
154         body,
155         |_, _| Ok(()),
156     );
157 }