Skip to content

Commit 7ce7139

Browse files
committed
Auto merge of #30301 - michaelwoerister:mir-to-metadata2, r=nikomatsakis
This PR makes `Mir` `RustcEncodable` and `RustcDecodable` and stores it in crate metadata for inlinable items. Some other things in here: - `mir::visit::Visitor` is extended to also visit `Literals`, `Spans` and `DefIds`. - It also adds `mir::visit::MutVisitor` which allows to mutate the visited `Mir` structure in place. - Some numbers on how big MIR is in metadata (total metadata size in bytes): | | w/ MIR | w/o MIR | Rel. Size | |----------------|-----------:|------------:|:---------:| | libcore | 17,695,473 | 14,263,377 | 124% | | liblibc | 411,440 | 404,382 | 102% | | libcollections | 4,537,975 | 3,526,933 | 129% | | libserialize | 2,574,769 | 2,060,798 | 125% | | libsyntax | 15,262,894 | 12,075,574 | 126% | | librustc | 16,984,537 | 13,692,168 | 124% | So, adding MIR to metadata makes it about 25% bigger. It could be worse, considering that it still uses the inefficient RBML encoding. Still, the question is whether we should put MIR emission behind a `-Z` flag.
2 parents 0d36840 + 5addc31 commit 7ce7139

File tree

20 files changed

+567
-63
lines changed

20 files changed

+567
-63
lines changed

src/librustc/middle/const_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub fn lookup_const_fn_by_id<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: DefId)
242242
}
243243
}
244244

245-
#[derive(Clone, Debug)]
245+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
246246
pub enum ConstVal {
247247
Float(f64),
248248
Int(i64),

src/librustc/middle/cstore.rs

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use middle::def;
2828
use middle::lang_items;
2929
use middle::ty::{self, Ty};
3030
use middle::def_id::{DefId, DefIndex};
31+
use mir::repr::Mir;
3132
use session::Session;
3233
use session::search_paths::PathKind;
3334
use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
@@ -100,6 +101,7 @@ pub enum InlinedItem {
100101
}
101102

102103
/// A borrowed version of `hir::InlinedItem`.
104+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
103105
pub enum InlinedItemRef<'a> {
104106
Item(&'a hir::Item),
105107
TraitItem(DefId, &'a hir::TraitItem),
@@ -216,6 +218,8 @@ pub trait CrateStore<'tcx> : Any {
216218
// misc. metadata
217219
fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
218220
-> FoundAst<'tcx>;
221+
fn maybe_get_item_mir(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
222+
-> Option<Mir<'tcx>>;
219223
// This is basically a 1-based range of ints, which is a little
220224
// silly - I may fix that.
221225
fn crates(&self) -> Vec<ast::CrateNum>;
@@ -235,6 +239,7 @@ pub trait CrateStore<'tcx> : Any {
235239
item_symbols: &RefCell<NodeMap<String>>,
236240
link_meta: &LinkMeta,
237241
reachable: &NodeSet,
242+
mir_map: &NodeMap<Mir<'tcx>>,
238243
krate: &hir::Crate) -> Vec<u8>;
239244
fn metadata_encoding_version(&self) -> &[u8];
240245
}
@@ -383,6 +388,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
383388
// misc. metadata
384389
fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
385390
-> FoundAst<'tcx> { unimplemented!() }
391+
fn maybe_get_item_mir(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
392+
-> Option<Mir<'tcx>> { unimplemented!() }
393+
386394
// This is basically a 1-based range of ints, which is a little
387395
// silly - I may fix that.
388396
fn crates(&self) -> Vec<ast::CrateNum> { vec![] }
@@ -404,6 +412,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
404412
item_symbols: &RefCell<NodeMap<String>>,
405413
link_meta: &LinkMeta,
406414
reachable: &NodeSet,
415+
mir_map: &NodeMap<Mir<'tcx>>,
407416
krate: &hir::Crate) -> Vec<u8> { vec![] }
408417
fn metadata_encoding_version(&self) -> &[u8] { unimplemented!() }
409418
}

src/librustc/middle/ty/sty.rs

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

1111
//! This module contains TypeVariants and its major components
1212
13+
use middle::cstore;
1314
use middle::def_id::DefId;
1415
use middle::region;
1516
use middle::subst::{self, Substs};
@@ -26,6 +27,8 @@ use syntax::abi;
2627
use syntax::ast::{self, Name};
2728
use syntax::parse::token::special_idents;
2829

30+
use serialize::{Decodable, Decoder};
31+
2932
use rustc_front::hir;
3033

3134
use self::FnOutput::*;
@@ -233,7 +236,7 @@ pub enum TypeVariants<'tcx> {
233236
/// closure C wind up influencing the decisions we ought to make for
234237
/// closure C (which would then require fixed point iteration to
235238
/// handle). Plus it fixes an ICE. :P
236-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
239+
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
237240
pub struct ClosureSubsts<'tcx> {
238241
/// Lifetime and type parameters from the enclosing function.
239242
/// These are separated out because trans wants to pass them around
@@ -246,6 +249,23 @@ pub struct ClosureSubsts<'tcx> {
246249
pub upvar_tys: Vec<Ty<'tcx>>
247250
}
248251

252+
impl<'tcx> Decodable for &'tcx ClosureSubsts<'tcx> {
253+
fn decode<S: Decoder>(s: &mut S) -> Result<&'tcx ClosureSubsts<'tcx>, S::Error> {
254+
let closure_substs = try! { Decodable::decode(s) };
255+
let dummy_def_id: DefId = unsafe { mem::zeroed() };
256+
257+
cstore::tls::with_decoding_context(s, |dcx, _| {
258+
// Intern the value
259+
let ty = dcx.tcx().mk_closure_from_closure_substs(dummy_def_id,
260+
Box::new(closure_substs));
261+
match ty.sty {
262+
TyClosure(_, ref closure_substs) => Ok(&**closure_substs),
263+
_ => unreachable!()
264+
}
265+
})
266+
}
267+
}
268+
249269
#[derive(Clone, PartialEq, Eq, Hash)]
250270
pub struct TraitTy<'tcx> {
251271
pub principal: ty::PolyTraitRef<'tcx>,
@@ -434,7 +454,7 @@ pub struct ClosureTy<'tcx> {
434454
pub sig: PolyFnSig<'tcx>,
435455
}
436456

437-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
457+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
438458
pub enum FnOutput<'tcx> {
439459
FnConverging(Ty<'tcx>),
440460
FnDiverging
@@ -632,7 +652,7 @@ pub struct DebruijnIndex {
632652
///
633653
/// [1] http://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
634654
/// [2] http://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
635-
#[derive(Clone, PartialEq, Eq, Hash, Copy)]
655+
#[derive(Clone, PartialEq, Eq, Hash, Copy, RustcEncodable, RustcDecodable)]
636656
pub enum Region {
637657
// Region bound in a type or fn declaration which will be
638658
// substituted 'early' -- that is, at the same time when type
@@ -701,7 +721,7 @@ pub struct RegionVid {
701721
pub index: u32
702722
}
703723

704-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
724+
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
705725
pub struct SkolemizedRegionVid {
706726
pub index: u32
707727
}

0 commit comments

Comments
 (0)