Skip to content

Commit c1a501b

Browse files
Implement a pass manager
1 parent a0de634 commit c1a501b

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hir::def::{CtorKind, Namespace};
1616
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
1717
use rustc_hir::{self, GeneratorKind};
1818
use rustc_hir::{self as hir, HirId};
19+
use rustc_session::Session;
1920
use rustc_target::abi::{Size, VariantIdx};
2021

2122
use polonius_engine::Atom;
@@ -99,7 +100,21 @@ pub trait MirPass<'tcx> {
99100
}
100101
}
101102

103+
/// Returns `true` if this pass is enabled with the current combination of compiler flags.
104+
fn is_enabled(&self, _sess: &Session) -> bool {
105+
true
106+
}
107+
102108
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>);
109+
110+
/// If this pass causes the MIR to enter a new phase, return that phase.
111+
fn phase_change(&self) -> Option<MirPhase> {
112+
None
113+
}
114+
115+
fn is_mir_dump_enabled(&self) -> bool {
116+
true
117+
}
103118
}
104119

105120
/// The various "big phases" that MIR goes through.

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ use rustc_middle::ty::query::Providers;
3232
use rustc_middle::ty::{self, TyCtxt, TypeFoldable};
3333
use rustc_span::{Span, Symbol};
3434

35+
#[macro_use]
36+
mod pass_manager;
37+
38+
use pass_manager::{Lint, MirLint};
39+
3540
mod abort_unwinding_calls;
3641
mod add_call_guards;
3742
mod add_moves_for_packed_drops;
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)