Skip to content

Commit 0848ae2

Browse files
committed
rustc: move the MIR map into TyCtxt.
1 parent 4ec48b3 commit 0848ae2

File tree

26 files changed

+232
-408
lines changed

26 files changed

+232
-408
lines changed

src/librustc/middle/cstore.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use hir::svh::Svh;
3030
use middle::lang_items;
3131
use ty::{self, Ty, TyCtxt};
3232
use mir::Mir;
33-
use mir::mir_map::MirMap;
3433
use session::Session;
3534
use session::search_paths::PathKind;
3635
use util::nodemap::{NodeSet, DefIdMap};
@@ -209,8 +208,7 @@ pub trait CrateStore<'tcx> {
209208
fn local_node_for_inlined_defid(&'tcx self, def_id: DefId) -> Option<ast::NodeId>;
210209
fn defid_for_inlined_node(&'tcx self, node_id: ast::NodeId) -> Option<DefId>;
211210

212-
fn maybe_get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
213-
-> Option<Mir<'tcx>>;
211+
fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> Mir<'tcx>;
214212
fn is_item_mir_available(&self, def: DefId) -> bool;
215213

216214
// This is basically a 1-based range of ints, which is a little
@@ -228,8 +226,7 @@ pub trait CrateStore<'tcx> {
228226
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
229227
reexports: &def::ExportMap,
230228
link_meta: &LinkMeta,
231-
reachable: &NodeSet,
232-
mir_map: &MirMap<'tcx>) -> Vec<u8>;
229+
reachable: &NodeSet) -> Vec<u8>;
233230
fn metadata_encoding_version(&self) -> &[u8];
234231
}
235232

@@ -390,8 +387,8 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
390387
bug!("defid_for_inlined_node")
391388
}
392389

393-
fn maybe_get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
394-
-> Option<Mir<'tcx>> { bug!("maybe_get_item_mir") }
390+
fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
391+
-> Mir<'tcx> { bug!("get_item_mir") }
395392
fn is_item_mir_available(&self, def: DefId) -> bool {
396393
bug!("is_item_mir_available")
397394
}
@@ -412,8 +409,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
412409
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
413410
reexports: &def::ExportMap,
414411
link_meta: &LinkMeta,
415-
reachable: &NodeSet,
416-
mir_map: &MirMap<'tcx>) -> Vec<u8> { vec![] }
412+
reachable: &NodeSet) -> Vec<u8> { vec![] }
417413
fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
418414
}
419415

src/librustc/mir/mir_map.rs

-38
This file was deleted.

src/librustc/mir/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub mod tcx;
3737
pub mod visit;
3838
pub mod transform;
3939
pub mod traversal;
40-
pub mod mir_map;
4140

4241
macro_rules! newtype_index {
4342
($name:ident, $debug_name:expr) => (

src/librustc/mir/transform.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use dep_graph::DepNode;
1212
use hir;
1313
use hir::map::DefPathData;
14-
use mir::mir_map::MirMap;
1514
use mir::{Mir, Promoted};
1615
use ty::TyCtxt;
1716
use syntax::ast::NodeId;
@@ -85,12 +84,11 @@ pub trait Pass {
8584
fn disambiguator<'a>(&'a self) -> Option<Box<fmt::Display+'a>> { None }
8685
}
8786

88-
/// A pass which inspects the whole MirMap.
87+
/// A pass which inspects the whole Mir map.
8988
pub trait MirMapPass<'tcx>: Pass {
9089
fn run_pass<'a>(
9190
&mut self,
9291
tcx: TyCtxt<'a, 'tcx, 'tcx>,
93-
map: &mut MirMap<'tcx>,
9492
hooks: &mut [Box<for<'s> MirPassHook<'s>>]);
9593
}
9694

@@ -114,13 +112,18 @@ pub trait MirPass<'tcx>: Pass {
114112
impl<'tcx, T: MirPass<'tcx>> MirMapPass<'tcx> for T {
115113
fn run_pass<'a>(&mut self,
116114
tcx: TyCtxt<'a, 'tcx, 'tcx>,
117-
map: &mut MirMap<'tcx>,
118115
hooks: &mut [Box<for<'s> MirPassHook<'s>>])
119116
{
120-
let def_ids = map.map.keys();
117+
let def_ids = tcx.mir_map.borrow().keys();
121118
for def_id in def_ids {
119+
if !def_id.is_local() {
120+
continue;
121+
}
122+
122123
let _task = tcx.dep_graph.in_task(DepNode::Mir(def_id));
123-
let mir = map.map.get_mut(&def_id).unwrap();
124+
let mir = &mut tcx.mir_map.borrow()[&def_id].borrow_mut();
125+
tcx.dep_graph.write(DepNode::Mir(def_id));
126+
124127
let id = tcx.map.as_local_node_id(def_id).unwrap();
125128
let src = MirSource::from_node(tcx, id);
126129

@@ -163,11 +166,11 @@ impl<'a, 'tcx> Passes {
163166
passes
164167
}
165168

166-
pub fn run_passes(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, map: &mut MirMap<'tcx>) {
169+
pub fn run_passes(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>) {
167170
let Passes { ref mut passes, ref mut plugin_passes, ref mut pass_hooks } = *self;
168171
for pass in plugin_passes.iter_mut().chain(passes.iter_mut()) {
169172
time(tcx.sess.time_passes(), &*pass.name(),
170-
|| pass.run_pass(tcx, map, pass_hooks));
173+
|| pass.run_pass(tcx, pass_hooks));
171174
}
172175
}
173176

src/librustc/ty/context.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use middle::free_region::FreeRegionMap;
2222
use middle::region::RegionMaps;
2323
use middle::resolve_lifetime;
2424
use middle::stability;
25+
use mir::Mir;
2526
use ty::subst::{Kind, Substs};
2627
use traits;
2728
use ty::{self, TraitRef, Ty, TypeAndMut};
@@ -63,8 +64,9 @@ pub struct CtxtArenas<'tcx> {
6364

6465
// references
6566
generics: TypedArena<ty::Generics<'tcx>>,
66-
trait_defs: TypedArena<ty::TraitDef<'tcx>>,
67-
adt_defs: TypedArena<ty::AdtDefData<'tcx, 'tcx>>,
67+
trait_def: TypedArena<ty::TraitDef<'tcx>>,
68+
adt_def: TypedArena<ty::AdtDefData<'tcx, 'tcx>>,
69+
mir: TypedArena<RefCell<Mir<'tcx>>>,
6870
}
6971

7072
impl<'tcx> CtxtArenas<'tcx> {
@@ -79,8 +81,9 @@ impl<'tcx> CtxtArenas<'tcx> {
7981
layout: TypedArena::new(),
8082

8183
generics: TypedArena::new(),
82-
trait_defs: TypedArena::new(),
83-
adt_defs: TypedArena::new()
84+
trait_def: TypedArena::new(),
85+
adt_def: TypedArena::new(),
86+
mir: TypedArena::new()
8487
}
8588
}
8689
}
@@ -356,6 +359,15 @@ pub struct GlobalCtxt<'tcx> {
356359

357360
pub map: ast_map::Map<'tcx>,
358361

362+
/// Maps from the def-id of a function/method or const/static
363+
/// to its MIR. Mutation is done at an item granularity to
364+
/// allow MIR optimization passes to function and still
365+
/// access cross-crate MIR (e.g. inlining or const eval).
366+
///
367+
/// Note that cross-crate MIR appears to be always borrowed
368+
/// (in the `RefCell` sense) to prevent accidental mutation.
369+
pub mir_map: RefCell<DepTrackingMap<maps::Mir<'tcx>>>,
370+
359371
// Records the free variables refrenced by every closure
360372
// expression. Do not track deps for this, just recompute it from
361373
// scratch every time.
@@ -602,6 +614,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
602614
self.global_interners.arenas.generics.alloc(generics)
603615
}
604616

617+
pub fn alloc_mir(self, mir: Mir<'gcx>) -> &'gcx RefCell<Mir<'gcx>> {
618+
self.global_interners.arenas.mir.alloc(RefCell::new(mir))
619+
}
620+
605621
pub fn intern_trait_def(self, def: ty::TraitDef<'gcx>)
606622
-> &'gcx ty::TraitDef<'gcx> {
607623
let did = def.trait_ref.def_id;
@@ -615,7 +631,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
615631

616632
pub fn alloc_trait_def(self, def: ty::TraitDef<'gcx>)
617633
-> &'gcx ty::TraitDef<'gcx> {
618-
self.global_interners.arenas.trait_defs.alloc(def)
634+
self.global_interners.arenas.trait_def.alloc(def)
619635
}
620636

621637
pub fn insert_adt_def(self, did: DefId, adt_def: ty::AdtDefMaster<'gcx>) {
@@ -631,7 +647,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
631647
variants: Vec<ty::VariantDefData<'gcx, 'gcx>>)
632648
-> ty::AdtDefMaster<'gcx> {
633649
let def = ty::AdtDefData::new(self, did, kind, variants);
634-
let interned = self.global_interners.arenas.adt_defs.alloc(def);
650+
let interned = self.global_interners.arenas.adt_def.alloc(def);
635651
self.insert_adt_def(did, interned);
636652
interned
637653
}
@@ -736,6 +752,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
736752
super_predicates: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
737753
fulfilled_predicates: RefCell::new(fulfilled_predicates),
738754
map: map,
755+
mir_map: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
739756
freevars: RefCell::new(freevars),
740757
maybe_unused_trait_imports: maybe_unused_trait_imports,
741758
tcache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),

src/librustc/ty/maps.rs

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

1111
use dep_graph::{DepNode, DepTrackingMapConfig};
1212
use hir::def_id::DefId;
13+
use mir;
1314
use ty::{self, Ty};
15+
16+
use std::cell::RefCell;
1417
use std::marker::PhantomData;
1518
use std::rc::Rc;
1619
use syntax::{attr, ast};
@@ -43,3 +46,4 @@ dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Vec<DefId> }
4346
dep_map_ty! { TraitItems: TraitItems(DefId) -> Rc<Vec<ty::ImplOrTraitItem<'tcx>>> }
4447
dep_map_ty! { ReprHints: ReprHints(DefId) -> Rc<Vec<attr::ReprAttr>> }
4548
dep_map_ty! { InlinedClosures: Hir(DefId) -> ast::NodeId }
49+
dep_map_ty! { Mir: Mir(DefId) -> &'tcx RefCell<mir::Mir<'tcx>> }

src/librustc/ty/mod.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use hir::def::{Def, CtorKind, PathResolution, ExportMap};
2424
use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
2525
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
2626
use middle::region::{CodeExtent, ROOT_CODE_EXTENT};
27+
use mir::Mir;
2728
use traits;
2829
use ty;
2930
use ty::subst::{Subst, Substs};
@@ -34,9 +35,10 @@ use util::nodemap::FnvHashMap;
3435

3536
use serialize::{self, Encodable, Encoder};
3637
use std::borrow::Cow;
37-
use std::cell::{Cell, RefCell};
38+
use std::cell::{Cell, RefCell, Ref};
3839
use std::hash::{Hash, Hasher};
3940
use std::iter;
41+
use std::mem;
4042
use std::ops::Deref;
4143
use std::rc::Rc;
4244
use std::slice;
@@ -2510,6 +2512,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25102512
|| self.sess.cstore.item_super_predicates(self.global_tcx(), did))
25112513
}
25122514

2515+
/// Given the did of an item, returns its MIR, borrowed immutably.
2516+
pub fn item_mir(self, did: DefId) -> Ref<'gcx, Mir<'gcx>> {
2517+
lookup_locally_or_in_crate_store("mir_map", did, &self.mir_map, || {
2518+
let mir = self.sess.cstore.get_item_mir(self.global_tcx(), did);
2519+
let mir = self.alloc_mir(mir);
2520+
2521+
// Perma-borrow MIR from extern crates to prevent mutation.
2522+
mem::forget(mir.borrow());
2523+
2524+
mir
2525+
}).borrow()
2526+
}
2527+
25132528
/// If `type_needs_drop` returns true, then `ty` is definitely
25142529
/// non-copy and *might* have a destructor attached; if it returns
25152530
/// false, then `ty` definitely has no destructor (i.e. no drop glue).

src/librustc_borrowck/borrowck/mir/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ pub struct MoveDataParamEnv<'tcx> {
5555
param_env: ty::ParameterEnvironment<'tcx>,
5656
}
5757

58-
pub fn borrowck_mir<'a, 'tcx: 'a>(
59-
bcx: &mut BorrowckCtxt<'a, 'tcx>,
60-
fk: FnKind,
61-
_decl: &hir::FnDecl,
62-
mir: &'a Mir<'tcx>,
63-
body: &hir::Block,
64-
_sp: Span,
65-
id: ast::NodeId,
66-
attributes: &[ast::Attribute]) {
58+
pub fn borrowck_mir(bcx: &mut BorrowckCtxt,
59+
fk: FnKind,
60+
_decl: &hir::FnDecl,
61+
body: &hir::Block,
62+
_sp: Span,
63+
id: ast::NodeId,
64+
attributes: &[ast::Attribute]) {
6765
match fk {
6866
FnKind::ItemFn(name, ..) |
6967
FnKind::Method(name, ..) => {
@@ -75,8 +73,10 @@ pub fn borrowck_mir<'a, 'tcx: 'a>(
7573
}
7674

7775
let tcx = bcx.tcx;
78-
7976
let param_env = ty::ParameterEnvironment::for_item(tcx, id);
77+
78+
let mir = &tcx.item_mir(tcx.map.local_def_id(id));
79+
8080
let move_data = MoveData::gather_moves(mir, tcx, &param_env);
8181
let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };
8282
let flow_inits =
@@ -170,8 +170,8 @@ pub struct MirBorrowckCtxt<'b, 'a: 'b, 'tcx: 'a> {
170170
mir: &'b Mir<'tcx>,
171171
node_id: ast::NodeId,
172172
move_data: MoveData<'tcx>,
173-
flow_inits: DataflowResults<MaybeInitializedLvals<'a, 'tcx>>,
174-
flow_uninits: DataflowResults<MaybeUninitializedLvals<'a, 'tcx>>
173+
flow_inits: DataflowResults<MaybeInitializedLvals<'b, 'tcx>>,
174+
flow_uninits: DataflowResults<MaybeUninitializedLvals<'b, 'tcx>>
175175
}
176176

177177
impl<'b, 'a: 'b, 'tcx: 'a> MirBorrowckCtxt<'b, 'a, 'tcx> {

0 commit comments

Comments
 (0)