Skip to content

Commit 9c154a6

Browse files
committed
rip out everything but MirPass, move the logic into suites
1 parent c253df5 commit 9c154a6

File tree

8 files changed

+92
-230
lines changed

8 files changed

+92
-230
lines changed

src/librustc/mir/transform.rs

+10-62
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use hir::def_id::DefId;
1515
use hir::map::DefPathData;
1616
use mir::{Mir, Promoted};
1717
use ty::TyCtxt;
18-
use std::cell::Ref;
1918
use std::rc::Rc;
2019
use syntax::ast::NodeId;
2120

@@ -83,29 +82,6 @@ pub fn default_name<T: ?Sized>() -> Cow<'static, str> {
8382
}
8483
}
8584

86-
/// Gives you access to various bits of state during your MIR pass.
87-
pub trait MirCtxt<'a, 'tcx: 'a> {
88-
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx>;
89-
fn def_id(&self) -> DefId;
90-
fn suite(&self) -> MirSuite;
91-
fn pass_num(&self) -> MirPassIndex;
92-
fn source(&self) -> MirSource;
93-
94-
// Get a read-only view on the MIR of this def-id from the
95-
// previous pass.
96-
fn read_previous_mir(&self) -> Ref<'tcx, Mir<'tcx>>;
97-
98-
// Steal the MIR of this def-id from the previous pass; any future
99-
// attempt to access the MIR from the previous pass is a bug.
100-
fn steal_previous_mir(&self) -> Mir<'tcx>;
101-
102-
// Same as `read_previous_mir()`, but for any def-id you want.
103-
fn read_previous_mir_of(&self, def_id: DefId) -> Ref<'tcx, Mir<'tcx>>;
104-
105-
// Same as `steal_previous_mir()`, but for any def-id you want.
106-
fn steal_previous_mir_of(&self, def_id: DefId) -> Mir<'tcx>;
107-
}
108-
10985
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
11086
pub struct MirSuite(pub usize);
11187

@@ -125,26 +101,19 @@ pub struct MirPassIndex(pub usize);
125101
/// the hook will be invoked once per output.
126102
pub trait PassHook {
127103
fn on_mir_pass<'a, 'tcx: 'a>(&self,
128-
mir_cx: &MirCtxt<'a, 'tcx>,
129-
mir: Option<(DefId, &Mir<'tcx>)>);
104+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
105+
suite: MirSuite,
106+
pass_num: MirPassIndex,
107+
pass_name: &str,
108+
source: MirSource,
109+
mir: &Mir<'tcx>,
110+
is_after: bool);
130111
}
131112

132113
/// The full suite of types that identifies a particular
133114
/// application of a pass to a def-id.
134115
pub type PassId = (MirSuite, MirPassIndex, DefId);
135116

136-
/// A streamlined trait that you can implement to create an
137-
/// intraprocedural pass; the pass will be invoked to process the MIR
138-
/// with the given `def_id`. This lets you do things before we fetch
139-
/// the MIR itself. You may prefer `MirPass`, which is even more streamlined.
140-
pub trait DefIdPass {
141-
fn name<'a>(&'a self) -> Cow<'a, str> {
142-
default_name::<Self>()
143-
}
144-
145-
fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> Mir<'tcx>;
146-
}
147-
148117
/// A streamlined trait that you can implement to create a pass; the
149118
/// pass will be named after the type, and it will consist of a main
150119
/// loop that goes over each available MIR and applies `run_pass`.
@@ -159,32 +128,11 @@ pub trait MirPass {
159128
mir: &mut Mir<'tcx>);
160129
}
161130

162-
impl<T: MirPass> DefIdPass for T {
163-
fn name<'a>(&'a self) -> Cow<'a, str> {
164-
MirPass::name(self)
165-
}
166-
167-
fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> Mir<'tcx> {
168-
let tcx = mir_cx.tcx();
169-
let source = mir_cx.source();
170-
let mut mir = mir_cx.steal_previous_mir();
171-
MirPass::run_pass(self, tcx, source, &mut mir);
172-
173-
let item_id = source.item_id();
174-
for (promoted_index, promoted_mir) in mir.promoted.iter_enumerated_mut() {
175-
let promoted_source = MirSource::Promoted(item_id, promoted_index);
176-
MirPass::run_pass(self, tcx, promoted_source, promoted_mir);
177-
}
178-
179-
mir
180-
}
181-
}
182-
183131
/// A manager for MIR passes.
184132
#[derive(Clone)]
185133
pub struct Passes {
186134
pass_hooks: Vec<Rc<PassHook>>,
187-
suites: Vec<Vec<Rc<DefIdPass>>>,
135+
suites: Vec<Vec<Rc<MirPass>>>,
188136
}
189137

190138
/// The number of "pass suites" that we have:
@@ -212,7 +160,7 @@ impl<'a, 'tcx> Passes {
212160
}
213161

214162
/// Pushes a built-in pass.
215-
pub fn push_pass<T: DefIdPass + 'static>(&mut self, suite: MirSuite, pass: T) {
163+
pub fn push_pass<T: MirPass + 'static>(&mut self, suite: MirSuite, pass: T) {
216164
self.suites[suite.0].push(Rc::new(pass));
217165
}
218166

@@ -225,7 +173,7 @@ impl<'a, 'tcx> Passes {
225173
self.suites[suite.0].len()
226174
}
227175

228-
pub fn pass(&self, suite: MirSuite, pass: MirPassIndex) -> &DefIdPass {
176+
pub fn pass(&self, suite: MirSuite, pass: MirPassIndex) -> &MirPass {
229177
&*self.suites[suite.0][pass.0]
230178
}
231179

src/librustc/ty/maps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ define_maps! { <'tcx>
818818
/// manually, you're doing it wrong.
819819
///
820820
/// See the README for the `mir` module for details.
821-
[multi] mir_pass: mir_pass((MirSuite, MirPassIndex, DefId)) -> &'tcx Steal<mir::Mir<'tcx>>,
821+
[] mir_pass: mir_pass((MirSuite, MirPassIndex, DefId)) -> &'tcx Steal<mir::Mir<'tcx>>,
822822

823823
/// MIR after our optimization passes have run. This is MIR that is ready
824824
/// for trans. This is also the only query that can fetch non-local MIR, at present.

src/librustc_mir/callgraph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct CallGraph {
3030
}
3131

3232
impl CallGraph {
33-
pub fn build<'a, 'mir, 'tcx>(cx: &mut InterproceduralCx<'a, 'mir, 'tcx>) -> CallGraph {
33+
pub fn build<'a, 'tcx>(cx: &mut InterproceduralCx<'a, 'tcx>) -> CallGraph {
3434
let mut callgraph = CallGraph {
3535
node_map: DefIdMap(),
3636
graph: graph::Graph::new()

src/librustc_mir/transform/dump_mir.rs

+18-32
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ use std::fmt;
1515
use std::fs::File;
1616
use std::io;
1717

18-
use rustc::hir::def_id::DefId;
1918
use rustc::mir::Mir;
20-
use rustc::mir::transform::{DefIdPass, MirCtxt, MirSource, PassHook};
19+
use rustc::mir::transform::{MirPass, MirPassIndex, MirSource, MirSuite, PassHook};
2120
use rustc::session::config::{OutputFilenames, OutputType};
2221
use rustc::ty::TyCtxt;
2322
use util as mir_util;
2423

2524
pub struct Marker(pub &'static str);
2625

27-
impl DefIdPass for Marker {
26+
impl MirPass for Marker {
2827
fn name<'a>(&'a self) -> Cow<'a, str> {
2928
Cow::Borrowed(self.0)
3029
}
3130

32-
fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> Mir<'tcx> {
33-
mir_cx.steal_previous_mir()
31+
fn run_pass<'a, 'tcx>(&self,
32+
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
33+
_source: MirSource,
34+
_mir: &mut Mir<'tcx>)
35+
{
3436
}
3537
}
3638

@@ -49,37 +51,21 @@ pub struct DumpMir;
4951

5052
impl PassHook for DumpMir {
5153
fn on_mir_pass<'a, 'tcx: 'a>(&self,
52-
mir_cx: &MirCtxt<'a, 'tcx>,
53-
mir: Option<(DefId, &Mir<'tcx>)>)
54+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
55+
suite: MirSuite,
56+
pass_num: MirPassIndex,
57+
pass_name: &str,
58+
source: MirSource,
59+
mir: &Mir<'tcx>,
60+
is_after: bool)
5461
{
55-
let tcx = mir_cx.tcx();
56-
let suite = mir_cx.suite();
57-
let pass_num = mir_cx.pass_num();
58-
let pass = tcx.mir_passes.pass(suite, pass_num);
59-
let name = &pass.name();
60-
let source = match mir {
61-
None => mir_cx.source(),
62-
Some((def_id, _)) => {
63-
let id = tcx.hir.as_local_node_id(def_id)
64-
.expect("mir source requires local def-id");
65-
MirSource::from_node(tcx, id)
66-
}
67-
};
68-
if mir_util::dump_enabled(tcx, name, source) {
69-
let previous_mir;
70-
let mir_to_dump = match mir {
71-
Some((_, m)) => m,
72-
None => {
73-
previous_mir = mir_cx.read_previous_mir();
74-
&*previous_mir
75-
}
76-
};
62+
if mir_util::dump_enabled(tcx, pass_name, source) {
7763
mir_util::dump_mir(tcx,
7864
Some((suite, pass_num)),
79-
name,
80-
&Disambiguator { is_after: mir.is_some() },
65+
pass_name,
66+
&Disambiguator { is_after },
8167
source,
82-
mir_to_dump);
68+
mir);
8369
}
8470
}
8571
}

src/librustc_mir/transform/inline.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::graph;
1818

1919
use rustc::dep_graph::DepNode;
2020
use rustc::mir::*;
21-
use rustc::mir::transform::{MirCtxt, MirSource, PassId};
21+
use rustc::mir::transform::{MirSource, PassId};
2222
use rustc::mir::visit::*;
2323
use rustc::traits;
2424
use rustc::ty::{self, Ty, TyCtxt};
@@ -46,19 +46,14 @@ const UNKNOWN_SIZE_COST: usize = 10;
4646
pub struct Inline;
4747

4848
pub trait Pass {
49-
fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>)
49+
fn run_pass<'a, 'tcx: 'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>)
5050
-> Multi<PassId, &'tcx Steal<Mir<'tcx>>>;
5151
}
5252

5353
impl Pass for Inline {
54-
fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>)
54+
fn run_pass<'a, 'tcx: 'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>)
5555
-> Multi<PassId, &'tcx Steal<Mir<'tcx>>> {
56-
let tcx = mir_cx.tcx();
57-
if tcx.sess.opts.debugging_opts.mir_opt_level < 2 {
58-
return Multi::from(tcx.alloc_steal_mir(mir_cx.steal_previous_mir()));
59-
}
60-
61-
let mut cx = InterproceduralCx::new(mir_cx);
56+
let mut cx = InterproceduralCx::new(tcx);
6257

6358
let callgraph = callgraph::CallGraph::build(&mut cx);
6459

@@ -72,8 +67,8 @@ impl Pass for Inline {
7267
}
7368
}
7469

75-
struct Inliner<'mir, 'tcx: 'mir> {
76-
tcx: TyCtxt<'mir, 'tcx, 'tcx>,
70+
struct Inliner<'a, 'tcx: 'a> {
71+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
7772
}
7873

7974
#[derive(Copy, Clone)]
@@ -85,11 +80,11 @@ struct CallSite<'tcx> {
8580
location: SourceInfo,
8681
}
8782

88-
impl<'mir, 'tcx> Inliner<'mir, 'tcx> {
89-
fn inline_scc<'a>(&mut self,
90-
cx: &mut InterproceduralCx<'a, 'mir, 'tcx>,
91-
callgraph: &callgraph::CallGraph,
92-
scc: &[graph::NodeIndex]) -> bool {
83+
impl<'a, 'tcx> Inliner<'a, 'tcx> {
84+
fn inline_scc(&mut self,
85+
cx: &mut InterproceduralCx<'a, 'tcx>,
86+
callgraph: &callgraph::CallGraph,
87+
scc: &[graph::NodeIndex]) -> bool {
9388
let tcx = self.tcx;
9489
let mut callsites = Vec::new();
9590
let mut in_scc = DefIdSet();

src/librustc_mir/transform/interprocedural.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use rustc::hir::def_id::DefId;
1212
use rustc::mir::Mir;
13-
use rustc::mir::transform::{MirCtxt, PassId};
13+
use rustc::mir::transform::{PassId};
1414
use rustc::ty::steal::Steal;
1515
use rustc::ty::TyCtxt;
1616
use rustc_data_structures::fx::FxHashMap;
@@ -21,31 +21,21 @@ use rustc_data_structures::fx::FxHashMap;
2121
/// stolen and so forth. It is more of a placeholder meant to get
2222
/// inlining up and going again, and is probably going to need heavy
2323
/// revision as we scale up to more interesting optimizations.
24-
pub struct InterproceduralCx<'a, 'mir: 'a, 'tcx: 'mir> {
25-
pub tcx: TyCtxt<'mir, 'tcx, 'tcx>,
26-
pub mir_cx: &'a MirCtxt<'mir, 'tcx>,
24+
pub struct InterproceduralCx<'a, 'tcx: 'a> {
25+
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
2726
local_cache: FxHashMap<DefId, Mir<'tcx>>,
2827
}
2928

30-
impl<'a, 'mir, 'tcx> InterproceduralCx<'a, 'mir, 'tcx> {
31-
pub fn new(mir_cx: &'a MirCtxt<'mir, 'tcx>) -> Self {
29+
impl<'a, 'tcx> InterproceduralCx<'a, 'tcx> {
30+
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
3231
InterproceduralCx {
33-
mir_cx,
34-
tcx: mir_cx.tcx(),
32+
tcx: tcx,
3533
local_cache: FxHashMap::default(),
3634
}
3735
}
3836

3937
pub fn into_local_mirs(self) -> Vec<(PassId, &'tcx Steal<Mir<'tcx>>)> {
40-
let tcx = self.tcx;
41-
let suite = self.mir_cx.suite();
42-
let pass_num = self.mir_cx.pass_num();
43-
self.local_cache.into_iter()
44-
.map(|(def_id, mir)| {
45-
let mir = tcx.alloc_steal_mir(mir);
46-
((suite, pass_num, def_id), mir)
47-
})
48-
.collect()
38+
unimplemented!()
4939
}
5040

5141
/// Ensures that the mir for `def_id` is available, if it can be
@@ -93,8 +83,6 @@ impl<'a, 'mir, 'tcx> InterproceduralCx<'a, 'mir, 'tcx> {
9383

9484
pub fn mir_mut(&mut self, def_id: DefId) -> &mut Mir<'tcx> {
9585
assert!(def_id.is_local(), "cannot get mutable mir of remote entry");
96-
let mir_cx = self.mir_cx;
97-
self.local_cache.entry(def_id)
98-
.or_insert_with(|| mir_cx.steal_previous_mir_of(def_id))
86+
unimplemented!()
9987
}
10088
}

0 commit comments

Comments
 (0)