Skip to content

Commit fcbd305

Browse files
committed
Auto merge of #80602 - tgnottingham:cratemetadata_you_aint_special, r=michaelwoerister
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.
2 parents bc39d4d + 8e7cbc2 commit fcbd305

File tree

10 files changed

+15
-107
lines changed

10 files changed

+15
-107
lines changed

Diff for: 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;

Diff for: 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> {

Diff for: 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};

Diff for: 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

Diff for: 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.
@@ -1611,31 +1605,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16111605
self.def_path_hash_unlocked(index, &mut def_path_hashes)
16121606
}
16131607

1614-
/// Get the `DepNodeIndex` corresponding this crate. The result of this
1615-
/// method is cached in the `dep_node_index` field.
1616-
fn get_crate_dep_node_index(&self, tcx: TyCtxt<'tcx>) -> DepNodeIndex {
1617-
let mut dep_node_index = self.dep_node_index.load();
1618-
1619-
if unlikely!(dep_node_index == DepNodeIndex::INVALID) {
1620-
// We have not cached the DepNodeIndex for this upstream crate yet,
1621-
// so use the dep-graph to find it out and cache it.
1622-
// Note that multiple threads can enter this block concurrently.
1623-
// That is fine because the DepNodeIndex remains constant
1624-
// throughout the whole compilation session, and multiple stores
1625-
// would always write the same value.
1626-
1627-
let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX);
1628-
let dep_node =
1629-
DepNode::from_def_path_hash(def_path_hash, dep_graph::DepKind::CrateMetadata);
1630-
1631-
dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node);
1632-
assert!(dep_node_index != DepNodeIndex::INVALID);
1633-
self.dep_node_index.store(dep_node_index);
1634-
}
1635-
1636-
dep_node_index
1637-
}
1638-
16391608
/// Imports the source_map from an external crate into the source_map of the crate
16401609
/// currently being compiled (the "local crate").
16411610
///
@@ -1852,7 +1821,6 @@ impl CrateMetadata {
18521821
source_map_import_info: OnceCell::new(),
18531822
def_path_hash_map: Default::default(),
18541823
alloc_decoding_state,
1855-
dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
18561824
cnum,
18571825
cnum_map,
18581826
dependencies,

Diff for: compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ 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-
49-
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);
47+
// External query providers call `crate_hash` in order to register a dependency
48+
// on the crate metadata. The exception is `crate_hash` itself, which obviously
49+
// doesn't need to do this (and can't, as it would cause a query cycle).
50+
use rustc_middle::dep_graph::DepKind;
51+
if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() {
52+
$tcx.ensure().crate_hash($def_id.krate);
5253
}
5354

55+
let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate);
56+
5457
$compute
5558
})*
5659

Diff for: compiler/rustc_middle/src/dep_graph/dep_node.rs

-14
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,6 @@ pub mod dep_kind {
207207
try_load_from_on_disk_cache: |_, _| {},
208208
};
209209

210-
// Represents metadata from an extern crate.
211-
pub const CrateMetadata: DepKindStruct = DepKindStruct {
212-
has_params: true,
213-
is_anon: false,
214-
is_eval_always: true,
215-
216-
can_reconstruct_query_key: || true,
217-
force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node),
218-
try_load_from_on_disk_cache: |_, _| {},
219-
};
220-
221210
pub const TraitSelect: DepKindStruct = DepKindStruct {
222211
has_params: false,
223212
is_anon: true,
@@ -353,9 +342,6 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
353342
// We use this for most things when incr. comp. is turned off.
354343
[] Null,
355344

356-
// Represents metadata from an extern crate.
357-
[eval_always] CrateMetadata(CrateNum),
358-
359345
[anon] TraitSelect,
360346

361347
// WARNING: if `Symbol` is changed, make sure you update `make_compile_codegen_unit` below.

Diff for: compiler/rustc_middle/src/dep_graph/mod.rs

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

Diff for: compiler/rustc_middle/src/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,8 @@ rustc_queries! {
12401240
eval_always
12411241
desc { "looking up the disambiguator a crate" }
12421242
}
1243+
// The macro which defines `rustc_metadata::provide_extern` depends on this query's name.
1244+
// Changing the name should cause a compiler error, but in case that changes, be aware.
12431245
query crate_hash(_: CrateNum) -> Svh {
12441246
eval_always
12451247
desc { "looking up the hash a crate" }

Diff for: 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_or(Ok(()), |c| c.serialize(self, encoder))
13421319
}

0 commit comments

Comments
 (0)