Skip to content

Commit

Permalink
Remove RawDefId tracking infrastructure from incr. comp. framework.
Browse files Browse the repository at this point in the history
This infrastructure is obsolete now with the new encoding scheme for
the DefPathHash->DefIndex maps in crate metadata.
  • Loading branch information
michaelwoerister committed Sep 14, 2021
1 parent 960893c commit 5445715
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 134 deletions.
12 changes: 1 addition & 11 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
}

fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
let hash = tcx.def_path_hash(*self);
// If this is a foreign `DefId`, store its current value
// in the incremental cache. When we decode the cache,
// we will use the old DefIndex as an initial guess for
// a lookup into the crate metadata.
if !self.is_local() {
if let Some(cache) = &tcx.on_disk_cache {
cache.store_foreign_def_id_hash(*self, hash);
}
}
hash.0
tcx.def_path_hash(*self).0
}

fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
type DepKind = DepKind;
type StableHashingContext = StableHashingContext<'tcx>;

fn register_reused_dep_node(&self, dep_node: &DepNode) {
if let Some(cache) = self.on_disk_cache.as_ref() {
cache.register_reused_dep_node(*self, dep_node)
}
}

#[inline]
fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
TyCtxt::create_stable_hashing_context(*self)
}
Expand Down
14 changes: 1 addition & 13 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Type context book-keeping.
use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepNode};
use crate::dep_graph::DepGraph;
use crate::hir::place::Place as HirPlace;
use crate::ich::{NodeIdHashingMode, StableHashingContext};
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
Expand Down Expand Up @@ -89,18 +89,6 @@ pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
def_path_hash: DefPathHash,
) -> Option<DefId>;

/// If the given `dep_node`'s hash still exists in the current compilation,
/// and its current `DefId` is foreign, calls `store_foreign_def_id` with it.
///
/// Normally, `store_foreign_def_id_hash` can be called directly by
/// the dependency graph when we construct a `DepNode`. However,
/// when we re-use a deserialized `DepNode` from the previous compilation
/// session, we only have the `DefPathHash` available. This method is used
/// to that any `DepNode` that we re-use has a `DefPathHash` -> `RawId` written
/// out for usage in the next compilation session.
fn register_reused_dep_node(&self, tcx: TyCtxt<'tcx>, dep_node: &DepNode);
fn store_foreign_def_id_hash(&self, def_id: DefId, hash: DefPathHash);

fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>);

fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileEncodeResult;
Expand Down
79 changes: 3 additions & 76 deletions compiler/rustc_query_impl/src/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_data_structures::unhash::UnhashMap;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::DefPathHash;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::dep_graph::{DepNode, DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
use rustc_middle::mir::{self, interpret};
use rustc_middle::thir;
Expand Down Expand Up @@ -86,27 +86,9 @@ pub struct OnDiskCache<'sess> {
expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
// Additional information used when decoding hygiene data.
hygiene_context: HygieneDecodeContext,
// Maps `DefPathHash`es to their `RawDefId`s from the *previous*
// compilation session. This is used as an initial 'guess' when
// we try to map a `DefPathHash` to its `DefId` in the current compilation
// session.
foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
// FIXME(mw): Update this comment:
// Likewise for ExpnId.
foreign_expn_data: UnhashMap<ExpnHash, u32>,

// The *next* compilation sessison's `foreign_def_path_hashes` - at
// the end of our current compilation session, this will get written
// out to the `foreign_def_path_hashes` field of the `Footer`, which
// will become `foreign_def_path_hashes` of the next compilation session.
// This stores any `DefPathHash` that we may need to map to a `DefId`
// during the next compilation session.
latest_foreign_def_path_hashes: Lock<UnhashMap<DefPathHash, RawDefId>>,

// Caches all lookups of `DefPathHashes`, both for local and foreign
// definitions. A definition from the previous compilation session
// may no longer exist in the current compilation session, so
// we use `Option<DefId>` so that we can cache a lookup failure.
def_path_hash_to_def_id_cache: Lock<UnhashMap<DefPathHash, Option<DefId>>>,
}

// This type is used only for serialization and deserialization.
Expand All @@ -121,7 +103,6 @@ struct Footer {
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
// See `OnDiskCache.expn_data`
expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
foreign_expn_data: UnhashMap<ExpnHash, u32>,
}

Expand All @@ -144,19 +125,6 @@ impl AbsoluteBytePos {
}
}

/// Represents a potentially invalid `DefId`. This is used during incremental
/// compilation to represent a `DefId` from the *previous* compilation session,
/// which may no longer be valid. This is used to help map a `DefPathHash`
/// to a `DefId` in the current compilation session.
#[derive(Encodable, Decodable, Copy, Clone, Debug)]
crate struct RawDefId {
// We deliberately do not use `CrateNum` and `DefIndex`
// here, since a crate/index from the previous compilation
// session may no longer exist.
pub krate: u32,
pub index: u32,
}

/// An `EncodedSourceFileId` is the same as a `StableSourceFileId` except that
/// the source crate is represented as a [StableCrateId] instead of as a
/// `CrateNum`. This way `EncodedSourceFileId` can be encoded and decoded
Expand Down Expand Up @@ -220,9 +188,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
expn_data: footer.expn_data,
foreign_expn_data: footer.foreign_expn_data,
hygiene_context: Default::default(),
foreign_def_path_hashes: footer.foreign_def_path_hashes,
latest_foreign_def_path_hashes: Default::default(),
def_path_hash_to_def_id_cache: Default::default(),
}
}

Expand All @@ -241,9 +206,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
expn_data: UnhashMap::default(),
foreign_expn_data: UnhashMap::default(),
hygiene_context: Default::default(),
foreign_def_path_hashes: Default::default(),
latest_foreign_def_path_hashes: Default::default(),
def_path_hash_to_def_id_cache: Default::default(),
}
}

Expand All @@ -253,13 +215,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
/// In order to serialize the new on-disk cache, the former on-disk cache file needs to be
/// deleted, hence we won't be able to refer to its memmapped data.
fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>) {
// Register any dep nodes that we reused from the previous session,
// but didn't `DepNode::construct` in this session. This ensures
// that their `DefPathHash` to `RawDefId` mappings are registered
// in 'latest_foreign_def_path_hashes' if necessary, since that
// normally happens in `DepNode::construct`.
tcx.dep_graph.register_reused_dep_nodes(tcx);

// Load everything into memory so we can write it out to the on-disk
// cache. The vast majority of cacheable query results should already
// be in memory, so this should be a cheap operation.
Expand Down Expand Up @@ -293,7 +248,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
(file_to_file_index, file_index_to_stable_id)
};

let latest_foreign_def_path_hashes = self.latest_foreign_def_path_hashes.lock().clone();
let hygiene_encode_context = HygieneEncodeContext::default();

let mut encoder = CacheEncoder {
Expand All @@ -305,7 +259,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
source_map: CachingSourceMapView::new(tcx.sess.source_map()),
file_to_file_index,
hygiene_context: &hygiene_encode_context,
latest_foreign_def_path_hashes,
};

// Encode query results.
Expand Down Expand Up @@ -382,9 +335,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
},
)?;

let foreign_def_path_hashes =
std::mem::take(&mut encoder.latest_foreign_def_path_hashes);

// `Encode the file footer.
let footer_pos = encoder.position() as u64;
encoder.encode_tagged(
Expand All @@ -397,7 +347,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
syntax_contexts,
expn_data,
foreign_expn_data,
foreign_def_path_hashes,
},
)?;

Expand Down Expand Up @@ -460,17 +409,6 @@ impl<'sess> OnDiskCache<'sess> {
debug_assert!(prev.is_none());
}

fn get_raw_def_id(&self, hash: &DefPathHash) -> Option<RawDefId> {
self.foreign_def_path_hashes.get(hash).copied()
}

fn try_remap_cnum(&self, tcx: TyCtxt<'_>, stable_crate_id: StableCrateId) -> Option<CrateNum> {
let cnum_map = self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx));
debug!("try_remap_cnum({:?}): cnum_map={:?}", stable_crate_id, cnum_map);

cnum_map.get(&stable_crate_id).copied()
}

/// Returns the cached query result if there is something in the cache for
/// the given `SerializedDepNodeIndex`; otherwise returns `None`.
pub fn try_load_query_result<'tcx, T>(
Expand Down Expand Up @@ -911,7 +849,6 @@ pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
source_map: CachingSourceMapView<'tcx>,
file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
hygiene_context: &'a HygieneEncodeContext,
latest_foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
}

impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E>
Expand Down Expand Up @@ -1044,17 +981,7 @@ where
E: 'a + OpaqueEncoder,
{
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
let def_path_hash = s.tcx.def_path_hash(*self);
// Store additional information when we encode a foreign `DefId`,
// so that we can map its `DefPathHash` back to a `DefId` in the next
// compilation session.
if !self.is_local() {
s.latest_foreign_def_path_hashes.insert(
def_path_hash,
RawDefId { krate: self.krate.as_u32(), index: self.index.as_u32() },
);
}
def_path_hash.encode(s)
s.tcx.def_path_hash(*self).encode(s)
}
}

Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_query_system/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ use std::hash::Hash;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
pub struct DepNode<K> {
pub kind: K,
// Important - whenever a `DepNode` is constructed, we need to make
// sure to register a `DefPathHash -> DefId` mapping if needed.
// This is currently done in two places:
//
// * When a `DepNode::construct` is called, `arg.to_fingerprint()`
// is responsible for calling `OnDiskCache::store_foreign_def_id_hash`
// if needed
// * When we serialize the on-disk cache, `OnDiskCache::serialize` is
// responsible for calling `DepGraph::register_reused_dep_nodes`.
//
// FIXME: Enforce this by preventing manual construction of `DefNode`
// (e.g. add a `_priv: ()` field)
pub hash: PackedFingerprint,
}

Expand Down
14 changes: 0 additions & 14 deletions compiler/rustc_query_system/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,20 +760,6 @@ impl<K: DepKind> DepGraph<K> {
}
}

// Register reused dep nodes (i.e. nodes we've marked red or green) with the context.
pub fn register_reused_dep_nodes<Ctxt: DepContext<DepKind = K>>(&self, tcx: Ctxt) {
let data = self.data.as_ref().unwrap();
for prev_index in data.colors.values.indices() {
match data.colors.get(prev_index) {
Some(DepNodeColor::Red) | Some(DepNodeColor::Green(_)) => {
let dep_node = data.previous.index_to_node(prev_index);
tcx.register_reused_dep_node(&dep_node);
}
None => {}
}
}
}

pub fn print_incremental_info(&self) {
if let Some(data) = &self.data {
data.current.encoder.borrow().print_incremental_info(
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_query_system/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pub trait DepContext: Copy {
/// Access the DepGraph.
fn dep_graph(&self) -> &DepGraph<Self::DepKind>;

fn register_reused_dep_node(&self, dep_node: &DepNode<Self::DepKind>);

/// Access the profiler.
fn profiler(&self) -> &SelfProfilerRef;

Expand Down

0 comments on commit 5445715

Please sign in to comment.