Skip to content

Commit 62139ff

Browse files
committed
Remove DepKind::CrateMetadata and pre-allocation of DepNodes
Remove much of the special-case handling around crate metadata dependency tracking by replacing `DepKind::CrateMetadata` and the pre-allocation of corresponding `DepNodes` with on-demand invocation of the `crate_hash` query.
1 parent 497c9a2 commit 62139ff

File tree

9 files changed

+24
-107
lines changed

9 files changed

+24
-107
lines changed

compiler/rustc_incremental/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ mod persist;
1717
pub use assert_dep_graph::assert_dep_graph;
1818
pub use persist::copy_cgu_workproduct_to_incr_comp_cache_dir;
1919
pub use persist::delete_workproduct_files;
20-
pub use persist::dep_graph_tcx_init;
2120
pub use persist::finalize_session_directory;
2221
pub use persist::garbage_collect_session_directories;
2322
pub use persist::in_incr_comp_dir;

compiler/rustc_incremental/src/persist/load.rs

-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::definitions::Definitions;
55
use rustc_middle::dep_graph::{PreviousDepGraph, SerializedDepGraph, WorkProduct, WorkProductId};
66
use rustc_middle::ty::query::OnDiskCache;
7-
use rustc_middle::ty::TyCtxt;
87
use rustc_serialize::opaque::Decoder;
98
use rustc_serialize::Decodable as RustcDecodable;
109
use rustc_session::Session;
@@ -15,14 +14,6 @@ use super::file_format;
1514
use super::fs::*;
1615
use super::work_product;
1716

18-
pub fn dep_graph_tcx_init(tcx: TyCtxt<'_>) {
19-
if !tcx.dep_graph.is_fully_enabled() {
20-
return;
21-
}
22-
23-
tcx.allocate_metadata_dep_nodes();
24-
}
25-
2617
type WorkProductMap = FxHashMap<WorkProductId, WorkProduct>;
2718

2819
pub enum LoadResult<T> {

compiler/rustc_incremental/src/persist/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub use fs::garbage_collect_session_directories;
1515
pub use fs::in_incr_comp_dir;
1616
pub use fs::in_incr_comp_dir_sess;
1717
pub use fs::prepare_session_directory;
18-
pub use load::dep_graph_tcx_init;
1918
pub use load::load_query_result_cache;
2019
pub use load::LoadResult;
2120
pub use load::{load_dep_graph, DepGraphFuture};

compiler/rustc_interface/src/passes.rs

-6
Original file line numberDiff line numberDiff line change
@@ -797,12 +797,6 @@ pub fn create_global_ctxt<'tcx>(
797797
})
798798
});
799799

800-
// Do some initialization of the DepGraph that can only be done with the tcx available.
801-
let icx = ty::tls::ImplicitCtxt::new(&gcx);
802-
ty::tls::enter_context(&icx, |_| {
803-
icx.tcx.sess.time("dep_graph_tcx_init", || rustc_incremental::dep_graph_tcx_init(icx.tcx));
804-
});
805-
806800
QueryContext(gcx)
807801
}
808802

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-33
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::captures::Captures;
1010
use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::svh::Svh;
13-
use rustc_data_structures::sync::{AtomicCell, Lock, LockGuard, Lrc, OnceCell};
13+
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
1414
use rustc_data_structures::unhash::UnhashMap;
1515
use rustc_errors::ErrorReported;
1616
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
@@ -21,7 +21,6 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}
2121
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
2222
use rustc_hir::lang_items;
2323
use rustc_index::vec::{Idx, IndexVec};
24-
use rustc_middle::dep_graph::{self, DepNode, DepNodeExt, DepNodeIndex};
2524
use rustc_middle::hir::exports::Export;
2625
use rustc_middle::middle::cstore::{CrateSource, ExternCrate};
2726
use rustc_middle::middle::cstore::{ForeignModule, LinkagePreference, NativeLib};
@@ -84,11 +83,6 @@ crate struct CrateMetadata {
8483
def_path_hash_map: OnceCell<UnhashMap<DefPathHash, DefIndex>>,
8584
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
8685
alloc_decoding_state: AllocDecodingState,
87-
/// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
88-
/// It is initialized on the first access in `get_crate_dep_node_index()`.
89-
/// Do not access the value directly, as it might not have been initialized yet.
90-
/// The field must always be initialized to `DepNodeIndex::INVALID`.
91-
dep_node_index: AtomicCell<DepNodeIndex>,
9286
/// Caches decoded `DefKey`s.
9387
def_key_cache: Lock<FxHashMap<DefIndex, DefKey>>,
9488
/// Caches decoded `DefPathHash`es.
@@ -1592,31 +1586,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15921586
self.def_path_hash_unlocked(index, &mut def_path_hashes)
15931587
}
15941588

1595-
/// Get the `DepNodeIndex` corresponding this crate. The result of this
1596-
/// method is cached in the `dep_node_index` field.
1597-
fn get_crate_dep_node_index(&self, tcx: TyCtxt<'tcx>) -> DepNodeIndex {
1598-
let mut dep_node_index = self.dep_node_index.load();
1599-
1600-
if unlikely!(dep_node_index == DepNodeIndex::INVALID) {
1601-
// We have not cached the DepNodeIndex for this upstream crate yet,
1602-
// so use the dep-graph to find it out and cache it.
1603-
// Note that multiple threads can enter this block concurrently.
1604-
// That is fine because the DepNodeIndex remains constant
1605-
// throughout the whole compilation session, and multiple stores
1606-
// would always write the same value.
1607-
1608-
let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX);
1609-
let dep_node =
1610-
DepNode::from_def_path_hash(def_path_hash, dep_graph::DepKind::CrateMetadata);
1611-
1612-
dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node);
1613-
assert!(dep_node_index != DepNodeIndex::INVALID);
1614-
self.dep_node_index.store(dep_node_index);
1615-
}
1616-
1617-
dep_node_index
1618-
}
1619-
16201589
/// Imports the source_map from an external crate into the source_map of the crate
16211590
/// currently being compiled (the "local crate").
16221591
///
@@ -1833,7 +1802,6 @@ impl CrateMetadata {
18331802
source_map_import_info: OnceCell::new(),
18341803
def_path_hash_map: Default::default(),
18351804
alloc_decoding_state,
1836-
dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
18371805
cnum,
18381806
cnum_map,
18391807
dependencies,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,33 @@ macro_rules! provide {
4444
let ($def_id, $other) = def_id_arg.into_args();
4545
assert!(!$def_id.is_local());
4646

47-
let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate);
48-
4947
if $tcx.dep_graph.is_fully_enabled() {
50-
let crate_dep_node_index = $cdata.get_crate_dep_node_index($tcx);
51-
$tcx.dep_graph.read_index(crate_dep_node_index);
48+
$tcx.ensure().crate_hash($def_id.krate);
5249
}
5350

51+
let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate);
52+
5453
$compute
5554
})*
5655

56+
// The other external query providers call `crate_hash` in order to register a
57+
// dependency on the crate metadata. The `crate_hash` implementation differs in
58+
// that it doesn't need to do this (and can't, as it would cause a query cycle).
59+
fn crate_hash<'tcx>(
60+
tcx: TyCtxt<'tcx>,
61+
def_id_arg: ty::query::query_keys::crate_hash<'tcx>,
62+
) -> ty::query::query_values::crate_hash<'tcx> {
63+
let _prof_timer = tcx.prof.generic_activity("metadata_decode_entry_crate_hash");
64+
65+
let (def_id, _) = def_id_arg.into_args();
66+
assert!(!def_id.is_local());
67+
68+
CStore::from_tcx(tcx).get_crate_data(def_id.krate).root.hash
69+
}
70+
5771
*providers = Providers {
5872
$($name,)*
73+
crate_hash,
5974
..*providers
6075
};
6176
}
@@ -191,7 +206,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
191206
})
192207
}
193208
crate_disambiguator => { cdata.root.disambiguator }
194-
crate_hash => { cdata.root.hash }
195209
crate_host_hash => { cdata.host_hash }
196210
original_crate_name => { cdata.root.name }
197211

compiler/rustc_middle/src/dep_graph/dep_node.rs

-14
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,6 @@ pub mod dep_kind {
214214
try_load_from_on_disk_cache: |_, _| {},
215215
};
216216

217-
// Represents metadata from an extern crate.
218-
pub const CrateMetadata: DepKindStruct = DepKindStruct {
219-
has_params: true,
220-
is_anon: false,
221-
is_eval_always: true,
222-
223-
can_reconstruct_query_key: || true,
224-
force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node),
225-
try_load_from_on_disk_cache: |_, _| {},
226-
};
227-
228217
pub const TraitSelect: DepKindStruct = DepKindStruct {
229218
has_params: false,
230219
is_anon: true,
@@ -379,9 +368,6 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
379368
// We use this for most things when incr. comp. is turned off.
380369
[] Null,
381370

382-
// Represents metadata from an extern crate.
383-
[eval_always] CrateMetadata(CrateNum),
384-
385371
[anon] TraitSelect,
386372

387373
[] CompileCodegenUnit(Symbol),

compiler/rustc_middle/src/dep_graph/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,9 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
115115
// be removed. https://github.com/rust-lang/rust/issues/62649 is one such
116116
// bug that must be fixed before removing this.
117117
match dep_node.kind {
118-
DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
118+
DepKind::hir_owner | DepKind::hir_owner_nodes => {
119119
if let Some(def_id) = dep_node.extract_def_id(*self) {
120-
if def_id_corresponds_to_hir_dep_node(*self, def_id.expect_local()) {
121-
if dep_node.kind == DepKind::CrateMetadata {
122-
// The `DefPath` has corresponding node,
123-
// and that node should have been marked
124-
// either red or green in `data.colors`.
125-
bug!(
126-
"DepNode {:?} should have been \
127-
pre-marked as red or green but wasn't.",
128-
dep_node
129-
);
130-
}
131-
} else {
120+
if !def_id_corresponds_to_hir_dep_node(*self, def_id.expect_local()) {
132121
// This `DefPath` does not have a
133122
// corresponding `DepNode` (e.g. a
134123
// struct field), and the ` DefPath`

compiler/rustc_middle/src/ty/context.rs

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Type context book-keeping.
22
33
use crate::arena::Arena;
4-
use crate::dep_graph::{self, DepGraph, DepKind, DepNode, DepNodeExt};
4+
use crate::dep_graph::DepGraph;
55
use crate::hir::exports::ExportMap;
66
use crate::ich::{NodeIdHashingMode, StableHashingContext};
77
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
@@ -37,8 +37,7 @@ use rustc_data_structures::sync::{self, Lock, Lrc, WorkerLocal};
3737
use rustc_errors::ErrorReported;
3838
use rustc_hir as hir;
3939
use rustc_hir::def::{DefKind, Res};
40-
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId};
41-
use rustc_hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE};
40+
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
4241
use rustc_hir::definitions::Definitions;
4342
use rustc_hir::intravisit::Visitor;
4443
use rustc_hir::lang_items::LangItem;
@@ -1315,28 +1314,6 @@ impl<'tcx> TyCtxt<'tcx> {
13151314
StableHashingContext::ignore_spans(self.sess, krate, self.definitions, &*self.cstore)
13161315
}
13171316

1318-
// This method makes sure that we have a DepNode and a Fingerprint for
1319-
// every upstream crate. It needs to be called once right after the tcx is
1320-
// created.
1321-
// With full-fledged red/green, the method will probably become unnecessary
1322-
// as this will be done on-demand.
1323-
pub fn allocate_metadata_dep_nodes(self) {
1324-
// We cannot use the query versions of crates() and crate_hash(), since
1325-
// those would need the DepNodes that we are allocating here.
1326-
for cnum in self.cstore.crates_untracked() {
1327-
let def_path_hash = self.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX });
1328-
let dep_node = DepNode::from_def_path_hash(def_path_hash, DepKind::CrateMetadata);
1329-
let crate_hash = self.cstore.crate_hash_untracked(cnum);
1330-
self.dep_graph.with_task(
1331-
dep_node,
1332-
self,
1333-
crate_hash,
1334-
|_, x| x, // No transformation needed
1335-
dep_graph::hash_result,
1336-
);
1337-
}
1338-
}
1339-
13401317
pub fn serialize_query_result_cache(self, encoder: &mut FileEncoder) -> FileEncodeResult {
13411318
self.queries.on_disk_cache.as_ref().map(|c| c.serialize(self, encoder)).unwrap_or(Ok(()))
13421319
}

0 commit comments

Comments
 (0)