Skip to content

Commit 3099fd4

Browse files
committed
store the mir into a map, restructure to avoid rebuilding so many times
1 parent 5858f6b commit 3099fd4

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/librustc_mir/dump.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ use self::rustc::middle::infer;
3030
use self::rustc::middle::region::CodeExtentData;
3131
use self::rustc::middle::ty::{self, Ty};
3232
use self::rustc::util::common::ErrorReported;
33+
use self::rustc::util::nodemap::NodeMap;
3334
use self::rustc_front::hir;
3435
use self::rustc_front::visit;
3536
use self::syntax::ast;
3637
use self::syntax::attr::AttrMetaMethods;
3738
use self::syntax::codemap::Span;
3839

3940
pub fn dump_crate(tcx: &ty::ctxt) {
40-
let mut dump = OuterDump { tcx: tcx };
41+
let mut map = NodeMap();
42+
let mut dump = OuterDump { tcx: tcx, map: &mut map };
4143
visit::walk_crate(&mut dump, tcx.map.krate());
4244
}
4345

@@ -46,21 +48,19 @@ pub fn dump_crate(tcx: &ty::ctxt) {
4648

4749
struct OuterDump<'a,'tcx:'a> {
4850
tcx: &'a ty::ctxt<'tcx>,
51+
map: &'a mut NodeMap<Mir<'tcx>>,
4952
}
5053

5154
impl<'a, 'tcx> OuterDump<'a, 'tcx> {
52-
fn visit_mir<OP>(&self, attributes: &'tcx [ast::Attribute], mut walk_op: OP)
53-
where OP: FnMut(&mut InnerDump<'a,'tcx>)
55+
fn visit_mir<OP>(&mut self, attributes: &'a [ast::Attribute], mut walk_op: OP)
56+
where OP: for<'m> FnMut(&mut InnerDump<'a,'m,'tcx>)
5457
{
55-
let mut built_mir = false;
56-
57-
let mut closure_dump = InnerDump { tcx: self.tcx, attr: None };
58+
let mut closure_dump = InnerDump { tcx: self.tcx, attr: None, map: &mut *self.map };
5859
for attr in attributes {
5960
if attr.check_name("rustc_mir") {
6061
closure_dump.attr = Some(attr);
6162
}
6263
}
63-
6464
walk_op(&mut closure_dump);
6565
}
6666
}
@@ -77,25 +77,47 @@ impl<'a, 'tcx> visit::Visitor<'tcx> for OuterDump<'a, 'tcx> {
7777
hir::MethodTraitItem(_, Some(_)) => {
7878
self.visit_mir(&trait_item.attrs, |c| visit::walk_trait_item(c, trait_item));
7979
}
80-
_ => { }
80+
hir::MethodTraitItem(_, None) |
81+
hir::ConstTraitItem(..) |
82+
hir::TypeTraitItem(..) => {
83+
}
8184
}
8285
visit::walk_trait_item(self, trait_item);
8386
}
87+
88+
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
89+
match impl_item.node {
90+
hir::MethodImplItem(..) => {
91+
self.visit_mir(&impl_item.attrs, |c| visit::walk_impl_item(c, impl_item));
92+
}
93+
hir::ConstImplItem(..) | hir::TypeImplItem(..) => { }
94+
}
95+
visit::walk_impl_item(self, impl_item);
96+
}
8497
}
8598

8699
///////////////////////////////////////////////////////////////////////////
87100
// InnerDump -- dumps MIR for a single fn and its contained closures
88101

89-
struct InnerDump<'a,'tcx:'a> {
102+
struct InnerDump<'a,'m,'tcx:'a+'m> {
90103
tcx: &'a ty::ctxt<'tcx>,
104+
map: &'m mut NodeMap<Mir<'tcx>>,
91105
attr: Option<&'a ast::Attribute>,
92106
}
93107

94-
impl<'a, 'tcx> visit::Visitor<'tcx> for InnerDump<'a,'tcx> {
108+
impl<'a, 'm, 'tcx> visit::Visitor<'tcx> for InnerDump<'a,'m,'tcx> {
95109
fn visit_item(&mut self, _: &'tcx hir::Item) {
96110
// ignore nested items; they need their own graphviz annotation
97111
}
98112

113+
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) {
114+
// ignore nested items; they need their own graphviz annotation
115+
}
116+
117+
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) {
118+
// ignore nested items; they need their own graphviz annotation
119+
}
120+
99121
fn visit_fn(&mut self,
100122
fk: visit::FnKind<'tcx>,
101123
decl: &'tcx hir::FnDecl,
@@ -150,6 +172,9 @@ impl<'a, 'tcx> visit::Visitor<'tcx> for InnerDump<'a,'tcx> {
150172
}
151173
}
152174
}
175+
176+
let previous = self.map.insert(id, mir);
177+
assert!(previous.is_none());
153178
}
154179
Err(ErrorReported) => { }
155180
}

0 commit comments

Comments
 (0)