Clean up the collector

This commit is contained in:
John Kåre Alsaker 2020-02-08 05:18:34 +01:00
parent fa09db8468
commit e9d166fac5
6 changed files with 34 additions and 139 deletions

View File

@ -1,7 +1,6 @@
use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
use crate::hir::map::definitions::{self, DefPathHash};
use crate::hir::map::{Entry, HirEntryMap, Map};
use crate::hir::map::{Entry, Map};
use crate::hir::{HirItem, HirOwner, HirOwnerItems};
use crate::ich::StableHashingContext;
use crate::middle::cstore::CrateStore;
@ -35,70 +34,38 @@ pub(super) struct NodeCollector<'a, 'hir> {
owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
owner_items_map: FxHashMap<DefIndex, &'hir mut HirOwnerItems<'hir>>,
/// The node map
map: HirEntryMap<'hir>,
/// The parent of this node
parent_node: hir::HirId,
// These fields keep track of the currently relevant DepNodes during
// the visitor's traversal.
current_dep_node_owner: DefIndex,
current_signature_dep_index: DepNodeIndex,
current_full_dep_index: DepNodeIndex,
currently_in_body: bool,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
hcx: StableHashingContext<'a>,
// We are collecting `DepNode::HirBody` hashes here so we can compute the
// crate hash from then later on.
// crate hash from them later on.
hir_body_nodes: Vec<(DefPathHash, Fingerprint)>,
}
fn input_dep_node_and_hash(
dep_graph: &DepGraph,
fn hash(
hcx: &mut StableHashingContext<'_>,
dep_node: DepNode,
input: impl for<'a> HashStable<StableHashingContext<'a>>,
) -> (DepNodeIndex, Fingerprint) {
let dep_node_index = dep_graph.input_task(dep_node, &mut *hcx, &input).1;
let hash = if dep_graph.is_fully_enabled() {
dep_graph.fingerprint_of(dep_node_index)
} else {
let mut stable_hasher = StableHasher::new();
input.hash_stable(hcx, &mut stable_hasher);
stable_hasher.finish()
};
(dep_node_index, hash)
) -> Fingerprint {
let mut stable_hasher = StableHasher::new();
input.hash_stable(hcx, &mut stable_hasher);
stable_hasher.finish()
}
fn alloc_hir_dep_nodes(
dep_graph: &DepGraph,
fn hash_body(
hcx: &mut StableHashingContext<'_>,
def_path_hash: DefPathHash,
item_like: impl for<'a> HashStable<StableHashingContext<'a>>,
hir_body_nodes: &mut Vec<(DefPathHash, Fingerprint)>,
) -> (DepNodeIndex, DepNodeIndex) {
let sig = dep_graph
.input_task(
DepNode::from_def_path_hash(def_path_hash, DepKind::Hir),
&mut *hcx,
HirItemLike { item_like: &item_like, hash_bodies: false },
)
.1;
let (full, hash) = input_dep_node_and_hash(
dep_graph,
hcx,
DepNode::from_def_path_hash(def_path_hash, DepKind::HirBody),
HirItemLike { item_like: &item_like, hash_bodies: true },
);
) {
let hash = hash(hcx, HirItemLike { item_like: &item_like });
hir_body_nodes.push((def_path_hash, hash));
(sig, full)
}
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
@ -121,7 +88,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
sess: &'a Session,
arena: &'hir Arena<'hir>,
krate: &'hir Crate<'hir>,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
mut hcx: StableHashingContext<'a>,
@ -130,8 +96,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
let mut hir_body_nodes = Vec::new();
// Allocate `DepNode`s for the root module.
let (root_mod_sig_dep_index, root_mod_full_dep_index) = {
{
let Crate {
ref item,
// These fields are handled separately:
@ -147,40 +112,31 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
proc_macros: _,
} = *krate;
alloc_hir_dep_nodes(
dep_graph,
&mut hcx,
root_mod_def_path_hash,
item,
&mut hir_body_nodes,
)
hash_body(&mut hcx, root_mod_def_path_hash, item, &mut hir_body_nodes)
};
let mut collector = NodeCollector {
arena,
krate,
source_map: sess.source_map(),
map: IndexVec::from_elem_n(IndexVec::new(), definitions.def_index_count()),
parent_node: hir::CRATE_HIR_ID,
current_signature_dep_index: root_mod_sig_dep_index,
current_full_dep_index: root_mod_full_dep_index,
current_dep_node_owner: CRATE_DEF_INDEX,
currently_in_body: false,
dep_graph,
definitions,
hir_to_node_id,
hcx,
hir_body_nodes,
owner_map: FxHashMap::default(),
owner_items_map: FxHashMap::default(),
owner_map: FxHashMap::with_capacity_and_hasher(
definitions.def_index_count(),
Default::default(),
),
owner_items_map: FxHashMap::with_capacity_and_hasher(
definitions.def_index_count(),
Default::default(),
),
};
collector.insert_entry(
hir::CRATE_HIR_ID,
Entry {
parent: hir::CRATE_HIR_ID,
dep_node: root_mod_sig_dep_index,
node: Node::Crate(&krate.item),
},
Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.item) },
);
collector
@ -192,7 +148,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
cstore: &dyn CrateStore,
commandline_args_hash: u64,
) -> (
HirEntryMap<'hir>,
FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
FxHashMap<DefIndex, &'hir mut HirOwnerItems<'hir>>,
Svh,
@ -239,7 +194,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
let crate_hash: Fingerprint = stable_hasher.finish();
let svh = Svh::new(crate_hash.to_smaller_hash());
(self.map, self.owner_map, self.owner_items_map, svh)
(self.owner_map, self.owner_items_map, svh)
}
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
@ -266,26 +221,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
items.items[id.local_id] =
Some(HirItem { parent: entry.parent.local_id, node: entry.node });
}
debug!("hir_map: {:?} => {:?}", id, entry);
let local_map = &mut self.map[id.owner];
let len = local_map.len();
if i >= len {
local_map.extend(repeat(None).take(i - len + 1));
}
local_map[id.local_id] = Some(entry);
}
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
let entry = Entry {
parent: self.parent_node,
dep_node: if self.currently_in_body {
self.current_full_dep_index
} else {
self.current_signature_dep_index
},
node,
};
let entry = Entry { parent: self.parent_node, node };
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
@ -340,29 +279,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
f: F,
) {
let prev_owner = self.current_dep_node_owner;
let prev_signature_dep_index = self.current_signature_dep_index;
let prev_full_dep_index = self.current_full_dep_index;
let prev_in_body = self.currently_in_body;
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
let (signature_dep_index, full_dep_index) = alloc_hir_dep_nodes(
self.dep_graph,
&mut self.hcx,
def_path_hash,
item_like,
&mut self.hir_body_nodes,
);
self.current_signature_dep_index = signature_dep_index;
self.current_full_dep_index = full_dep_index;
hash_body(&mut self.hcx, def_path_hash, item_like, &mut self.hir_body_nodes);
self.current_dep_node_owner = dep_node_owner;
self.currently_in_body = false;
f(self);
self.currently_in_body = prev_in_body;
self.current_dep_node_owner = prev_owner;
self.current_full_dep_index = prev_full_dep_index;
self.current_signature_dep_index = prev_signature_dep_index;
}
}
@ -391,10 +315,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}
fn visit_nested_body(&mut self, id: BodyId) {
let prev_in_body = self.currently_in_body;
self.currently_in_body = true;
self.visit_body(self.krate.body(id));
self.currently_in_body = prev_in_body;
}
fn visit_param(&mut self, param: &'hir Param<'hir>) {
@ -617,11 +538,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}
}
// This is a wrapper structure that allows determining if span values within
// the wrapped item should be hashed or not.
struct HirItemLike<T> {
item_like: T,
hash_bodies: bool,
}
impl<'hir, T> HashStable<StableHashingContext<'hir>> for HirItemLike<T>
@ -629,7 +547,7 @@ where
T: HashStable<StableHashingContext<'hir>>,
{
fn hash_stable(&self, hcx: &mut StableHashingContext<'hir>, hasher: &mut StableHasher) {
hcx.while_hashing_hir_bodies(self.hash_bodies, |hcx| {
hcx.while_hashing_hir_bodies(true, |hcx| {
self.item_like.hash_stable(hcx, hasher);
});
}

View File

@ -8,8 +8,8 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{HirId, ItemLocalId};*/
pub fn check_crate(hir_map: &EarlyMap<'_>, sess: &rustc_session::Session) {
hir_map.dep_graph.assert_ignored();
/*
/*hir_map.dep_graph.assert_ignored();
let errors = Lock::new(Vec::new());
par_iter(&hir_map.krate.modules).for_each(|(module_id, _)| {

View File

@ -4,7 +4,6 @@ pub use self::definitions::{
};
use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepNodeIndex};
use crate::hir::{HirOwner, HirOwnerItems};
use crate::middle::cstore::CrateStoreDyn;
use crate::ty::query::Providers;
@ -18,7 +17,6 @@ use rustc_hir::intravisit;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::print::Nested;
use rustc_hir::*;
use rustc_index::vec::IndexVec;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::kw;
@ -34,7 +32,6 @@ mod hir_id_validator;
#[derive(Copy, Clone, Debug)]
pub struct Entry<'hir> {
parent: HirId,
dep_node: DepNodeIndex,
node: Node<'hir>,
}
@ -132,26 +129,16 @@ fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
}
}
/// This type is effectively a `HashMap<HirId, Entry<'hir>>`,
/// but it is implemented as 2 layers of arrays.
/// - first we have `A = IndexVec<DefIndex, B>` mapping `DefIndex`s to an inner value
/// - which is `B = IndexVec<ItemLocalId, Option<Entry<'hir>>` which gives you the `Entry`.
pub(super) type HirEntryMap<'hir> = IndexVec<DefIndex, IndexVec<ItemLocalId, Option<Entry<'hir>>>>;
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
pub struct EarlyMap<'hir> {
pub krate: &'hir Crate<'hir>,
pub dep_graph: DepGraph,
/// The SVH of the local crate.
pub crate_hash: Svh,
pub(super) owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
pub(super) owner_items_map: FxHashMap<DefIndex, &'hir HirOwnerItems<'hir>>,
pub(super) map: HirEntryMap<'hir>,
pub(crate) definitions: &'hir Definitions,
/// The reverse mapping of `node_to_hir_id`.
@ -164,8 +151,6 @@ pub struct Map<'hir> {
pub(super) krate: &'hir Crate<'hir>,
pub dep_graph: DepGraph,
/// The SVH of the local crate.
pub crate_hash: Svh,
@ -383,15 +368,11 @@ impl<'hir> Map<'hir> {
fn get_entry(&self, id: HirId) -> Entry<'hir> {
if id.local_id == ItemLocalId::from_u32_const(0) {
let owner = self.tcx.hir_owner(id.owner_def_id());
Entry { parent: owner.parent, node: owner.node, dep_node: DepNodeIndex::INVALID }
Entry { parent: owner.parent, node: owner.node }
} else {
let owner = self.tcx.hir_owner_items(id.owner_def_id());
let item = owner.items[id.local_id].as_ref().unwrap();
Entry {
parent: HirId { owner: id.owner, local_id: item.parent },
node: item.node,
dep_node: DepNodeIndex::INVALID,
}
Entry { parent: HirId { owner: id.owner, local_id: item.parent }, node: item.node }
}
}
@ -1069,7 +1050,6 @@ pub fn map_crate<'hir>(
arena: &'hir Arena<'hir>,
cstore: &CrateStoreDyn,
krate: &'hir Crate<'hir>,
dep_graph: DepGraph,
definitions: Definitions,
) -> EarlyMap<'hir> {
let _prof_timer = sess.prof.generic_activity("build_hir_map");
@ -1081,11 +1061,11 @@ pub fn map_crate<'hir>(
.map(|(node_id, &hir_id)| (hir_id, node_id))
.collect();
let (map, owner_map, owner_items_map, crate_hash) = {
let (owner_map, owner_items_map, crate_hash) = {
let hcx = crate::ich::StableHashingContext::new(sess, krate, &definitions, cstore);
let mut collector =
NodeCollector::root(sess, arena, krate, &dep_graph, &definitions, &hir_to_node_id, hcx);
NodeCollector::root(sess, arena, krate, &definitions, &hir_to_node_id, hcx);
intravisit::walk_crate(&mut collector, krate);
let crate_disambiguator = sess.local_crate_disambiguator();
@ -1095,9 +1075,7 @@ pub fn map_crate<'hir>(
let map = EarlyMap {
krate,
dep_graph,
crate_hash,
map,
owner_map,
owner_items_map: owner_items_map.into_iter().map(|(k, v)| (k, &*v)).collect(),
hir_to_node_id,

View File

@ -100,8 +100,6 @@ pub fn provide(providers: &mut Providers<'_>) {
tcx,
krate: early.krate,
dep_graph: early.dep_graph,
crate_hash: early.crate_hash,
owner_map: early.owner_map,

View File

@ -1121,6 +1121,7 @@ impl<'tcx> TyCtxt<'tcx> {
arena: &'tcx WorkerLocal<Arena<'tcx>>,
resolutions: ty::ResolverOutputs,
hir: hir_map::EarlyMap<'tcx>,
dep_graph: DepGraph,
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
crate_name: &str,
output_filenames: &OutputFilenames,
@ -1132,7 +1133,6 @@ impl<'tcx> TyCtxt<'tcx> {
let common_types = CommonTypes::new(&interners);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);
let dep_graph = hir.dep_graph.clone();
let definitions = hir.definitions;
let cstore = resolutions.cstore;
let crates = cstore.crates_untracked();

View File

@ -716,7 +716,7 @@ pub fn create_global_ctxt<'tcx>(
let defs = mem::take(&mut resolver_outputs.definitions);
// Construct the HIR map.
let hir_map = map::map_crate(sess, &**arena, &*resolver_outputs.cstore, krate, dep_graph, defs);
let hir_map = map::map_crate(sess, &**arena, &*resolver_outputs.cstore, krate, defs);
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
@ -743,6 +743,7 @@ pub fn create_global_ctxt<'tcx>(
arena,
resolver_outputs,
hir_map,
dep_graph,
query_result_on_disk_cache,
&crate_name,
&outputs,