Skip to content

Commit 8b36d33

Browse files
Allocate DefIndices for global crate metadata.
This allows for treating global crate metadata the same as regular metadata with regard to incr. comp.
1 parent 12e5b69 commit 8b36d33

File tree

14 files changed

+159
-119
lines changed

14 files changed

+159
-119
lines changed

src/librustc/dep_graph/dep_node.rs

-17
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ pub enum DepNode<D: Clone + Debug> {
5353
/// in an extern crate.
5454
MetaData(D),
5555

56-
/// Represents some piece of metadata global to its crate.
57-
GlobalMetaData(D, GlobalMetaDataKind),
58-
5956
/// Represents some artifact that we save to disk. Note that these
6057
/// do not have a def-id as part of their identifier.
6158
WorkProduct(WorkProductId),
@@ -309,7 +306,6 @@ impl<D: Clone + Debug> DepNode<D> {
309306
ItemBodyNestedBodies(ref d) => op(d).map(ItemBodyNestedBodies),
310307
ConstIsRvaluePromotableToStatic(ref d) => op(d).map(ConstIsRvaluePromotableToStatic),
311308
IsMirAvailable(ref d) => op(d).map(IsMirAvailable),
312-
GlobalMetaData(ref d, kind) => op(d).map(|d| GlobalMetaData(d, kind)),
313309
}
314310
}
315311
}
@@ -330,16 +326,3 @@ impl WorkProductId {
330326
WorkProductId(hasher.finish())
331327
}
332328
}
333-
334-
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
335-
pub enum GlobalMetaDataKind {
336-
Krate,
337-
CrateDeps,
338-
DylibDependencyFormats,
339-
LangItems,
340-
LangItemsMissing,
341-
NativeLibraries,
342-
CodeMap,
343-
Impls,
344-
ExportedSymbols,
345-
}

src/librustc/dep_graph/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ mod thread;
2222
pub use self::dep_tracking_map::{DepTrackingMap, DepTrackingMapConfig};
2323
pub use self::dep_node::DepNode;
2424
pub use self::dep_node::WorkProductId;
25-
pub use self::dep_node::GlobalMetaDataKind;
2625
pub use self::graph::DepGraph;
2726
pub use self::graph::WorkProduct;
2827
pub use self::query::DepGraphQuery;

src/librustc/hir/map/definitions.rs

+106-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
//! expressions) that are mostly just leftovers.
1616
1717
use hir;
18-
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, DefIndexAddressSpace};
18+
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, DefIndexAddressSpace,
19+
CRATE_DEF_INDEX};
1920
use ich::Fingerprint;
2021
use rustc_data_structures::fx::FxHashMap;
2122
use rustc_data_structures::indexed_vec::IndexVec;
@@ -396,6 +397,11 @@ pub enum DefPathData {
396397
ImplTrait,
397398
/// A `typeof` type node.
398399
Typeof,
400+
401+
/// GlobalMetaData identifies a piece of crate metadata that is global to
402+
/// a whole crate (as opposed to just one item). GlobalMetaData components
403+
/// are only supposed to show up right below the crate root.
404+
GlobalMetaData(Ident)
399405
}
400406

401407
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
@@ -427,8 +433,8 @@ impl Definitions {
427433

428434
/// Get the number of definitions.
429435
pub fn def_index_counts_lo_hi(&self) -> (usize, usize) {
430-
(self.def_index_to_node[DefIndexAddressSpace::Low.index()].len(),
431-
self.def_index_to_node[DefIndexAddressSpace::High.index()].len())
436+
(self.table.index_to_key[DefIndexAddressSpace::Low.index()].len(),
437+
self.table.index_to_key[DefIndexAddressSpace::High.index()].len())
432438
}
433439

434440
pub fn def_key(&self, index: DefIndex) -> DefKey {
@@ -469,7 +475,12 @@ impl Definitions {
469475
if def_id.krate == LOCAL_CRATE {
470476
let space_index = def_id.index.address_space().index();
471477
let array_index = def_id.index.as_array_index();
472-
Some(self.def_index_to_node[space_index][array_index])
478+
let node_id = self.def_index_to_node[space_index][array_index];
479+
if node_id != ast::DUMMY_NODE_ID {
480+
Some(node_id)
481+
} else {
482+
None
483+
}
473484
} else {
474485
None
475486
}
@@ -498,12 +509,16 @@ impl Definitions {
498509

499510
// Create the definition.
500511
let address_space = super::ITEM_LIKE_SPACE;
501-
let index = self.table.allocate(key, def_path_hash, address_space);
512+
let root_index = self.table.allocate(key, def_path_hash, address_space);
513+
assert_eq!(root_index, CRATE_DEF_INDEX);
502514
assert!(self.def_index_to_node[address_space.index()].is_empty());
503515
self.def_index_to_node[address_space.index()].push(ast::CRATE_NODE_ID);
504-
self.node_to_def_index.insert(ast::CRATE_NODE_ID, index);
516+
self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
505517

506-
index
518+
// Allocate some other DefIndices that always must exist.
519+
GlobalMetaDataKind::allocate_def_indices(self);
520+
521+
root_index
507522
}
508523

509524
/// Add a definition with a parent definition.
@@ -550,13 +565,19 @@ impl Definitions {
550565
assert_eq!(index.as_array_index(),
551566
self.def_index_to_node[address_space.index()].len());
552567
self.def_index_to_node[address_space.index()].push(node_id);
568+
569+
// Some things for which we allocate DefIndices don't correspond to
570+
// anything in the AST, so they don't have a NodeId. For these cases
571+
// we don't need a mapping from NodeId to DefIndex.
572+
if node_id != ast::DUMMY_NODE_ID {
573+
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
574+
self.node_to_def_index.insert(node_id, index);
575+
}
576+
553577
if expansion.is_modern() {
554578
self.expansions.insert(index, expansion);
555579
}
556580

557-
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
558-
self.node_to_def_index.insert(node_id, index);
559-
560581
index
561582
}
562583

@@ -594,7 +615,8 @@ impl DefPathData {
594615
LifetimeDef(ident) |
595616
EnumVariant(ident) |
596617
Binding(ident) |
597-
Field(ident) => Some(ident),
618+
Field(ident) |
619+
GlobalMetaData(ident) => Some(ident),
598620

599621
Impl |
600622
CrateRoot |
@@ -622,7 +644,8 @@ impl DefPathData {
622644
LifetimeDef(ident) |
623645
EnumVariant(ident) |
624646
Binding(ident) |
625-
Field(ident) => {
647+
Field(ident) |
648+
GlobalMetaData(ident) => {
626649
return ident.name.as_str();
627650
}
628651

@@ -667,3 +690,74 @@ impl ::std::hash::Hash for DefPathData {
667690
}
668691
}
669692
}
693+
694+
695+
// We define the GlobalMetaDataKind enum with this macro because we want to
696+
// make sure that we exhaustively iterate over all variants when registering
697+
// the corresponding DefIndices in the DefTable.
698+
macro_rules! define_global_metadata_kind {
699+
(pub enum GlobalMetaDataKind {
700+
$($variant:ident),*
701+
}) => (
702+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
703+
RustcEncodable, RustcDecodable)]
704+
pub enum GlobalMetaDataKind {
705+
$($variant),*
706+
}
707+
708+
impl GlobalMetaDataKind {
709+
fn allocate_def_indices(definitions: &mut Definitions) {
710+
$({
711+
let instance = GlobalMetaDataKind::$variant;
712+
definitions.create_def_with_parent(
713+
CRATE_DEF_INDEX,
714+
ast::DUMMY_NODE_ID,
715+
DefPathData::GlobalMetaData(instance.ident()),
716+
DefIndexAddressSpace::High,
717+
Mark::root()
718+
);
719+
720+
// Make sure calling def_index does not crash.
721+
instance.def_index(&definitions.table);
722+
})*
723+
}
724+
725+
pub fn def_index(&self, def_path_table: &DefPathTable) -> DefIndex {
726+
let def_key = DefKey {
727+
parent: Some(CRATE_DEF_INDEX),
728+
disambiguated_data: DisambiguatedDefPathData {
729+
data: DefPathData::GlobalMetaData(self.ident()),
730+
disambiguator: 0,
731+
}
732+
};
733+
734+
def_path_table.key_to_index[&def_key]
735+
}
736+
737+
fn ident(&self) -> Ident {
738+
739+
let string = match *self {
740+
$(
741+
GlobalMetaDataKind::$variant => {
742+
concat!("{{GlobalMetaData::", stringify!($variant), "}}")
743+
}
744+
)*
745+
};
746+
747+
Ident::from_str(string)
748+
}
749+
}
750+
)
751+
}
752+
753+
define_global_metadata_kind!(pub enum GlobalMetaDataKind {
754+
Krate,
755+
CrateDeps,
756+
DylibDependencyFormats,
757+
LangItems,
758+
LangItemsMissing,
759+
NativeLibraries,
760+
CodeMap,
761+
Impls,
762+
ExportedSymbols
763+
});

src/librustc/middle/cstore.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
// probably get a better home if someone can find one.
2424

2525
use hir::def;
26-
use dep_graph::DepNode;
2726
use hir::def_id::{CrateNum, DefId, DefIndex};
2827
use hir::map as hir_map;
2928
use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData,
@@ -190,15 +189,14 @@ pub struct EncodedMetadataHash {
190189
/// upstream crate.
191190
#[derive(Debug, RustcEncodable, RustcDecodable, Clone)]
192191
pub struct EncodedMetadataHashes {
193-
pub entry_hashes: Vec<EncodedMetadataHash>,
194-
pub global_hashes: Vec<(DepNode<()>, ich::Fingerprint)>,
192+
// Stable content hashes for things in crate metadata, indexed by DefIndex.
193+
pub hashes: Vec<EncodedMetadataHash>,
195194
}
196195

197196
impl EncodedMetadataHashes {
198197
pub fn new() -> EncodedMetadataHashes {
199198
EncodedMetadataHashes {
200-
entry_hashes: Vec::new(),
201-
global_hashes: Vec::new(),
199+
hashes: Vec::new(),
202200
}
203201
}
204202
}

src/librustc/ty/item_path.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
197197
data @ DefPathData::ClosureExpr |
198198
data @ DefPathData::Binding(..) |
199199
data @ DefPathData::ImplTrait |
200-
data @ DefPathData::Typeof => {
200+
data @ DefPathData::Typeof |
201+
data @ DefPathData::GlobalMetaData(..) => {
201202
let parent_def_id = self.parent_def_id(def_id).unwrap();
202203
self.push_item_path(buffer, parent_def_id);
203204
buffer.push(&data.as_interned_str());

src/librustc_incremental/persist/data.rs

-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ pub struct SerializedMetadataHashes {
126126
/// (matching the one found in this structure).
127127
pub entry_hashes: Vec<EncodedMetadataHash>,
128128

129-
/// This map contains fingerprints that are not specific to some DefId but
130-
/// describe something global to the whole crate.
131-
pub global_hashes: Vec<(DepNode<()>, Fingerprint)>,
132-
133129
/// For each DefIndex (as it occurs in SerializedMetadataHash), this
134130
/// map stores the DefPathIndex (as it occurs in DefIdDirectory), so
135131
/// that we can find the new DefId for a SerializedMetadataHash in a

src/librustc_incremental/persist/hash.rs

+6-31
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use rustc::dep_graph::DepNode;
12-
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
12+
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1313
use rustc::hir::svh::Svh;
1414
use rustc::ich::Fingerprint;
1515
use rustc::ty::TyCtxt;
@@ -29,9 +29,8 @@ use std::fmt::Debug;
2929
pub struct HashContext<'a, 'tcx: 'a> {
3030
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
3131
incremental_hashes_map: &'a IncrementalHashesMap,
32-
item_metadata_hashes: FxHashMap<DefId, Fingerprint>,
32+
metadata_hashes: FxHashMap<DefId, Fingerprint>,
3333
crate_hashes: FxHashMap<CrateNum, Svh>,
34-
global_metadata_hashes: FxHashMap<DepNode<DefId>, Fingerprint>,
3534
}
3635

3736
impl<'a, 'tcx> HashContext<'a, 'tcx> {
@@ -41,9 +40,8 @@ impl<'a, 'tcx> HashContext<'a, 'tcx> {
4140
HashContext {
4241
tcx: tcx,
4342
incremental_hashes_map: incremental_hashes_map,
44-
item_metadata_hashes: FxHashMap(),
43+
metadata_hashes: FxHashMap(),
4544
crate_hashes: FxHashMap(),
46-
global_metadata_hashes: FxHashMap(),
4745
}
4846
}
4947

@@ -53,8 +51,7 @@ impl<'a, 'tcx> HashContext<'a, 'tcx> {
5351
DepNode::Hir(_) |
5452
DepNode::HirBody(_) =>
5553
true,
56-
DepNode::MetaData(def_id) |
57-
DepNode::GlobalMetaData(def_id, _) => !def_id.is_local(),
54+
DepNode::MetaData(def_id) => !def_id.is_local(),
5855
_ => false,
5956
}
6057
}
@@ -83,13 +80,7 @@ impl<'a, 'tcx> HashContext<'a, 'tcx> {
8380
DepNode::MetaData(def_id) if !def_id.is_local() => {
8481
Some(self.metadata_hash(def_id,
8582
def_id.krate,
86-
|this| &mut this.item_metadata_hashes))
87-
}
88-
89-
DepNode::GlobalMetaData(def_id, kind) => {
90-
Some(self.metadata_hash(DepNode::GlobalMetaData(def_id, kind),
91-
def_id.krate,
92-
|this| &mut this.global_metadata_hashes))
83+
|this| &mut this.metadata_hashes))
9384
}
9485

9586
_ => {
@@ -217,27 +208,11 @@ impl<'a, 'tcx> HashContext<'a, 'tcx> {
217208
let def_id = DefId { krate: cnum, index: serialized_hash.def_index };
218209

219210
// record the hash for this dep-node
220-
let old = self.item_metadata_hashes.insert(def_id, serialized_hash.hash);
211+
let old = self.metadata_hashes.insert(def_id, serialized_hash.hash);
221212
debug!("load_from_data: def_id={:?} hash={}", def_id, serialized_hash.hash);
222213
assert!(old.is_none(), "already have hash for {:?}", def_id);
223214
}
224215

225-
for (dep_node, fingerprint) in serialized_hashes.global_hashes {
226-
// Here we need to remap the CrateNum in the DepNode.
227-
let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
228-
let dep_node = match dep_node {
229-
DepNode::GlobalMetaData(_, kind) => DepNode::GlobalMetaData(def_id, kind),
230-
other => {
231-
bug!("unexpected DepNode variant: {:?}", other)
232-
}
233-
};
234-
235-
// record the hash for this dep-node
236-
debug!("load_from_data: def_node={:?} hash={}", dep_node, fingerprint);
237-
let old = self.global_metadata_hashes.insert(dep_node.clone(), fingerprint);
238-
assert!(old.is_none(), "already have hash for {:?}", dep_node);
239-
}
240-
241216
Ok(())
242217
}
243218
}

src/librustc_incremental/persist/save.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,11 @@ pub fn encode_metadata_hashes(tcx: TyCtxt,
255255
current_metadata_hashes: &mut FxHashMap<DefId, Fingerprint>,
256256
encoder: &mut Encoder)
257257
-> io::Result<()> {
258+
assert_eq!(metadata_hashes.hashes.len(),
259+
metadata_hashes.hashes.iter().map(|x| (x.def_index, ())).collect::<FxHashMap<_,_>>().len());
260+
258261
let mut serialized_hashes = SerializedMetadataHashes {
259-
entry_hashes: metadata_hashes.entry_hashes.to_vec(),
260-
global_hashes: metadata_hashes.global_hashes.to_vec(),
262+
entry_hashes: metadata_hashes.hashes.to_vec(),
261263
index_map: FxHashMap()
262264
};
263265

0 commit comments

Comments
 (0)