Skip to content

Commit

Permalink
Auto merge of #46299 - michaelwoerister:incr-comp-krimskrams, r=nikom…
Browse files Browse the repository at this point in the history
…atsakis

incr.comp.: Some preparatory work for caching more query results.

This PR
* adds and updates some encoding/decoding routines for various query result types so they can be cached later, and
* adds missing `[input]` annotations for a few `DepNode` variants.

The situation around having to explicitly mark dep-nodes/queries as inputs is not really satisfactory. I hope we can find a way of making this more fool-proof in the future.

r? @nikomatsakis
  • Loading branch information
bors committed Nov 30, 2017
2 parents d6b010f + 3bb25d6 commit 4fa202d
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/librustc/dep_graph/dep_node.rs
Expand Up @@ -562,7 +562,7 @@ define_dep_nodes!( <'tcx>
[] IsPanicRuntime(CrateNum),
[] IsCompilerBuiltins(CrateNum),
[] HasGlobalAllocator(CrateNum),
[] ExternCrate(DefId),
[input] ExternCrate(DefId),
[eval_always] LintLevels,
[] Specializes { impl1: DefId, impl2: DefId },
[input] InScopeTraits(DefIndex),
Expand Down Expand Up @@ -602,8 +602,8 @@ define_dep_nodes!( <'tcx>
[] MissingLangItems(CrateNum),
[] ExternConstBody(DefId),
[] VisibleParentMap,
[] MissingExternCrateItem(CrateNum),
[] UsedCrateSource(CrateNum),
[input] MissingExternCrateItem(CrateNum),
[input] UsedCrateSource(CrateNum),
[input] PostorderCnums,
[input] HasCloneClosures(CrateNum),
[input] HasCopyClosures(CrateNum),
Expand All @@ -619,7 +619,7 @@ define_dep_nodes!( <'tcx>
[input] Freevars(DefId),
[input] MaybeUnusedTraitImport(DefId),
[input] MaybeUnusedExternCrates,
[] StabilityIndex,
[eval_always] StabilityIndex,
[input] AllCrateNums,
[] ExportedSymbols(CrateNum),
[eval_always] CollectAndPartitionTranslationItems,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ich/impls_mir.rs
Expand Up @@ -98,7 +98,7 @@ for mir::Terminator<'gcx> {
}
}

impl<'gcx, T> HashStable<StableHashingContext<'gcx>> for mir::ClearOnDecode<T>
impl<'gcx, T> HashStable<StableHashingContext<'gcx>> for mir::ClearCrossCrate<T>
where T: HashStable<StableHashingContext<'gcx>>
{
#[inline]
Expand All @@ -107,8 +107,8 @@ impl<'gcx, T> HashStable<StableHashingContext<'gcx>> for mir::ClearOnDecode<T>
hasher: &mut StableHasher<W>) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
mir::ClearOnDecode::Clear => {}
mir::ClearOnDecode::Set(ref value) => {
mir::ClearCrossCrate::Clear => {}
mir::ClearCrossCrate::Set(ref value) => {
value.hash_stable(hcx, hasher);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/borrowck.rs
Expand Up @@ -15,6 +15,7 @@ use util::nodemap::FxHashSet;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHasherResult};

#[derive(Debug, RustcEncodable, RustcDecodable)]
pub struct BorrowCheckResult {
pub used_mut_nodes: FxHashSet<HirId>,
}
Expand Down
34 changes: 13 additions & 21 deletions src/librustc/mir/mod.rs
Expand Up @@ -36,6 +36,7 @@ use std::ops::{Index, IndexMut};
use std::rc::Rc;
use std::vec::IntoIter;
use syntax::ast::{self, Name};
use syntax::symbol::InternedString;
use syntax_pos::Span;

mod cache;
Expand Down Expand Up @@ -75,7 +76,7 @@ pub struct Mir<'tcx> {

/// Crate-local information for each visibility scope, that can't (and
/// needn't) be tracked across crates.
pub visibility_scope_info: ClearOnDecode<IndexVec<VisibilityScope, VisibilityScopeInfo>>,
pub visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope, VisibilityScopeInfo>>,

/// Rvalues promoted from this function, such as borrows of constants.
/// Each of them is the Mir of a constant with the fn's type parameters
Expand Down Expand Up @@ -129,8 +130,8 @@ pub const START_BLOCK: BasicBlock = BasicBlock(0);
impl<'tcx> Mir<'tcx> {
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
visibility_scope_info: ClearOnDecode<IndexVec<VisibilityScope,
VisibilityScopeInfo>>,
visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope,
VisibilityScopeInfo>>,
promoted: IndexVec<Promoted, Mir<'tcx>>,
yield_ty: Option<Ty<'tcx>>,
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
Expand Down Expand Up @@ -283,15 +284,15 @@ impl<'tcx> Mir<'tcx> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct VisibilityScopeInfo {
/// A NodeId with lint levels equivalent to this scope's lint levels.
pub lint_root: ast::NodeId,
/// The unsafe block that contains this node.
pub safety: Safety,
}

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
pub enum Safety {
Safe,
/// Unsafe because of a PushUnsafeBlock
Expand Down Expand Up @@ -335,22 +336,13 @@ impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
}

#[derive(Clone, Debug)]
pub enum ClearOnDecode<T> {
pub enum ClearCrossCrate<T> {
Clear,
Set(T)
}

impl<T> serialize::Encodable for ClearOnDecode<T> {
fn encode<S: serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
serialize::Encodable::encode(&(), s)
}
}

impl<T> serialize::Decodable for ClearOnDecode<T> {
fn decode<D: serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
serialize::Decodable::decode(d).map(|()| ClearOnDecode::Clear)
}
}
impl<T: serialize::Encodable> serialize::UseSpecializedEncodable for ClearCrossCrate<T> {}
impl<T: serialize::Decodable> serialize::UseSpecializedDecodable for ClearCrossCrate<T> {}

/// Grouped information about the source code origin of a MIR entity.
/// Intended to be inspected by diagnostics and debuginfo.
Expand Down Expand Up @@ -1733,21 +1725,21 @@ impl Location {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum UnsafetyViolationKind {
General,
ExternStatic(ast::NodeId),
BorrowPacked(ast::NodeId),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct UnsafetyViolation {
pub source_info: SourceInfo,
pub description: &'static str,
pub description: InternedString,
pub kind: UnsafetyViolationKind,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct UnsafetyCheckResult {
/// Violations that are propagated *upwards* from this function
pub violations: Rc<[UnsafetyViolation]>,
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/ty/context.rs
Expand Up @@ -1124,11 +1124,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn stability(self) -> Rc<stability::Index<'tcx>> {
// FIXME(#42293) we should actually track this, but fails too many tests
// today.
self.dep_graph.with_ignore(|| {
self.stability_index(LOCAL_CRATE)
})
self.stability_index(LOCAL_CRATE)
}

pub fn crates(self) -> Rc<Vec<CrateNum>> {
Expand Down
45 changes: 45 additions & 0 deletions src/librustc/ty/maps/on_disk_cache.rs
Expand Up @@ -15,6 +15,7 @@ use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId,
RESERVED_FOR_INCR_COMP_CACHE, LOCAL_CRATE};
use hir::map::definitions::DefPathHash;
use middle::cstore::CrateStore;
use mir;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque,
Expand All @@ -36,6 +37,9 @@ use ty::context::TyCtxt;
const PREV_DIAGNOSTICS_TAG: u64 = 0x1234_5678_A1A1_A1A1;
const QUERY_RESULT_INDEX_TAG: u64 = 0x1234_5678_C3C3_C3C3;

const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0;
const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1;

/// `OnDiskCache` provides an interface to incr. comp. data cached from the
/// previous compilation session. This data will eventually include the results
/// of a few selected queries (like `typeck_tables_of` and `mir_optimized`) and
Expand Down Expand Up @@ -518,12 +522,32 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<hir::HirId> for CacheDecoder<'a, 'tcx, 'x>
// NodeIds are not stable across compilation sessions, so we store them in their
// HirId representation. This allows use to map them to the current NodeId.
impl<'a, 'tcx, 'x> SpecializedDecoder<NodeId> for CacheDecoder<'a, 'tcx, 'x> {
#[inline]
fn specialized_decode(&mut self) -> Result<NodeId, Self::Error> {
let hir_id = hir::HirId::decode(self)?;
Ok(self.tcx().hir.hir_to_node_id(hir_id))
}
}

impl<'a, 'tcx, 'x, T: Decodable> SpecializedDecoder<mir::ClearCrossCrate<T>>
for CacheDecoder<'a, 'tcx, 'x> {
#[inline]
fn specialized_decode(&mut self) -> Result<mir::ClearCrossCrate<T>, Self::Error> {
let discr = u8::decode(self)?;

match discr {
TAG_CLEAR_CROSS_CRATE_CLEAR => Ok(mir::ClearCrossCrate::Clear),
TAG_CLEAR_CROSS_CRATE_SET => {
let val = T::decode(self)?;
Ok(mir::ClearCrossCrate::Set(val))
}
_ => {
unreachable!()
}
}
}
}

//- ENCODING -------------------------------------------------------------------

struct CacheEncoder<'enc, 'a, 'tcx, E>
Expand Down Expand Up @@ -658,6 +682,27 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<NodeId> for CacheEncoder<'enc, 'a, 't
}
}

impl<'enc, 'a, 'tcx, E, T> SpecializedEncoder<mir::ClearCrossCrate<T>>
for CacheEncoder<'enc, 'a, 'tcx, E>
where E: 'enc + ty_codec::TyEncoder,
T: Encodable,
{
#[inline]
fn specialized_encode(&mut self,
val: &mir::ClearCrossCrate<T>)
-> Result<(), Self::Error> {
match *val {
mir::ClearCrossCrate::Clear => {
TAG_CLEAR_CROSS_CRATE_CLEAR.encode(self)
}
mir::ClearCrossCrate::Set(ref val) => {
TAG_CLEAR_CROSS_CRATE_SET.encode(self)?;
val.encode(self)
}
}
}
}

macro_rules! encoder_methods {
($($name:ident($ty:ty);)*) => {
$(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -2702,7 +2702,7 @@ impl<'tcx> DtorckConstraint<'tcx> {
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, RustcEncodable, RustcDecodable)]
pub struct SymbolName {
// FIXME: we don't rely on interning or equality here - better have
// this be a `&'tcx str`.
Expand Down
21 changes: 21 additions & 0 deletions src/librustc_data_structures/indexed_set.rs
Expand Up @@ -17,6 +17,7 @@ use std::slice;
use bitslice::{BitSlice, Word};
use bitslice::{bitwise, Union, Subtract, Intersect};
use indexed_vec::Idx;
use rustc_serialize;

/// Represents a set (or packed family of sets), of some element type
/// E, where each E is identified by some unique index type `T`.
Expand All @@ -35,6 +36,26 @@ impl<T: Idx> Clone for IdxSetBuf<T> {
}
}

impl<T: Idx> rustc_serialize::Encodable for IdxSetBuf<T> {
fn encode<E: rustc_serialize::Encoder>(&self,
encoder: &mut E)
-> Result<(), E::Error> {
self.bits.encode(encoder)
}
}

impl<T: Idx> rustc_serialize::Decodable for IdxSetBuf<T> {
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<IdxSetBuf<T>, D::Error> {
let words: Vec<Word> = rustc_serialize::Decodable::decode(d)?;

Ok(IdxSetBuf {
_pd: PhantomData,
bits: words,
})
}
}


// pnkfelix wants to have this be `IdxSet<T>([Word]) and then pass
// around `&mut IdxSet<T>` or `&IdxSet<T>`.
//
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -21,6 +21,7 @@ use rustc::hir::def::{self, Def, CtorKind};
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::ich::Fingerprint;
use rustc::middle::lang_items;
use rustc::mir;
use rustc::session::Session;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::codec::TyDecoder;
Expand Down Expand Up @@ -327,6 +328,14 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
}
}

impl<'a, 'tcx, T: Decodable> SpecializedDecoder<mir::ClearCrossCrate<T>>
for DecodeContext<'a, 'tcx> {
#[inline]
fn specialized_decode(&mut self) -> Result<mir::ClearCrossCrate<T>, Self::Error> {
Ok(mir::ClearCrossCrate::Clear)
}
}

implement_ty_decoder!( DecodeContext<'a, 'tcx> );

impl<'a, 'tcx> MetadataBlob {
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -157,6 +157,15 @@ impl<'a, 'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext
}
}

impl<'a, 'tcx, T: Encodable> SpecializedEncoder<mir::ClearCrossCrate<T>>
for EncodeContext<'a, 'tcx> {
fn specialized_encode(&mut self,
_: &mir::ClearCrossCrate<T>)
-> Result<(), Self::Error> {
Ok(())
}
}

impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> {
fn position(&self) -> usize {
self.opaque.position()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/mod.rs
Expand Up @@ -543,7 +543,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {

Mir::new(self.cfg.basic_blocks,
self.visibility_scopes,
ClearOnDecode::Set(self.visibility_scope_info),
ClearCrossCrate::Set(self.visibility_scope_info),
IndexVec::new(),
yield_ty,
self.local_decls,
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_mir/shim.rs
Expand Up @@ -198,7 +198,7 @@ fn build_drop_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
IndexVec::from_elem_n(
VisibilityScopeData { span: span, parent_scope: None }, 1
),
ClearOnDecode::Clear,
ClearCrossCrate::Clear,
IndexVec::new(),
None,
local_decls_for_sig(&sig, span),
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
IndexVec::from_elem_n(
VisibilityScopeData { span: self.span, parent_scope: None }, 1
),
ClearOnDecode::Clear,
ClearCrossCrate::Clear,
IndexVec::new(),
None,
self.local_decls,
Expand Down Expand Up @@ -807,7 +807,7 @@ fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
IndexVec::from_elem_n(
VisibilityScopeData { span: span, parent_scope: None }, 1
),
ClearOnDecode::Clear,
ClearCrossCrate::Clear,
IndexVec::new(),
None,
local_decls,
Expand Down Expand Up @@ -885,7 +885,7 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
IndexVec::from_elem_n(
VisibilityScopeData { span: span, parent_scope: None }, 1
),
ClearOnDecode::Clear,
ClearCrossCrate::Clear,
IndexVec::new(),
None,
local_decls,
Expand Down

0 comments on commit 4fa202d

Please sign in to comment.