Skip to content

Commit 908d735

Browse files
committed
Remove one-liners.
1 parent c0ccc7d commit 908d735

File tree

3 files changed

+63
-72
lines changed

3 files changed

+63
-72
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,48 @@ pub trait DepKindTrait: std::fmt::Debug + Sync {
9191
query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
9292
);
9393

94+
/// The red/green evaluation system will try to mark a specific DepNode in the
95+
/// dependency graph as green by recursively trying to mark the dependencies of
96+
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
97+
/// where we don't know if it is red or green and we therefore actually have
98+
/// to recompute its value in order to find out. Since the only piece of
99+
/// information that we have at that point is the `DepNode` we are trying to
100+
/// re-evaluate, we need some way to re-run a query from just that. This is what
101+
/// `force_from_dep_node()` implements.
102+
///
103+
/// In the general case, a `DepNode` consists of a `DepKind` and an opaque
104+
/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
105+
/// is usually constructed by computing a stable hash of the query-key that the
106+
/// `DepNode` corresponds to. Consequently, it is not in general possible to go
107+
/// back from hash to query-key (since hash functions are not reversible). For
108+
/// this reason `force_from_dep_node()` is expected to fail from time to time
109+
/// because we just cannot find out, from the `DepNode` alone, what the
110+
/// corresponding query-key is and therefore cannot re-run the query.
111+
///
112+
/// The system deals with this case letting `try_mark_green` fail which forces
113+
/// the root query to be re-evaluated.
114+
///
115+
/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
116+
/// Fortunately, we can use some contextual information that will allow us to
117+
/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
118+
/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
119+
/// valid `DefPathHash`. Since we also always build a huge table that maps every
120+
/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
121+
/// everything we need to re-run the query.
122+
///
123+
/// Take the `mir_promoted` query as an example. Like many other queries, it
124+
/// just has a single parameter: the `DefId` of the item it will compute the
125+
/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
126+
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
127+
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
128+
/// `DefId` in `tcx.def_path_hash_to_def_id`.
129+
///
130+
/// When you implement a new query, it will likely have a corresponding new
131+
/// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
132+
/// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
133+
/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
134+
/// add it to the "We don't have enough information to reconstruct..." group in
135+
/// the match below.
94136
fn force_from_dep_node(&self, tcx: TyCtxt<'_>, dep_node: &DepNode) -> bool;
95137

96138
fn query_stats(&self, tcx: TyCtxt<'_>) -> Option<query::stats::QueryStats>;
@@ -214,6 +256,24 @@ macro_rules! define_dep_kinds {
214256
#[allow(unused_lifetimes)]
215257
type Key<$tcx> = ($($tuple_arg_ty),*);
216258

259+
// We must avoid ever having to call `force_from_dep_node()` for a
260+
// `DepNode::codegen_unit`:
261+
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
262+
// would always end up having to evaluate the first caller of the
263+
// `codegen_unit` query that *is* reconstructible. This might very well be
264+
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
265+
// to re-trigger calling the `codegen_unit` query with the right key. At
266+
// that point we would already have re-done all the work we are trying to
267+
// avoid doing in the first place.
268+
// The solution is simple: Just explicitly call the `codegen_unit` query for
269+
// each CGU, right after partitioning. This way `try_mark_green` will always
270+
// hit the cache instead of having to go through `force_from_dep_node`.
271+
// This assertion makes sure, we actually keep applying the solution above.
272+
debug_assert!(
273+
dep_node.kind != &dep_kind::codegen_unit,
274+
"calling force_from_dep_node() on DepKind::codegen_unit"
275+
);
276+
217277
if !self.can_reconstruct_query_key() {
218278
return false;
219279
}

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::ich::StableHashingContext;
2-
use crate::ty::query::try_load_from_on_disk_cache;
32
use crate::ty::{self, TyCtxt};
43
use rustc_data_structures::profiling::SelfProfilerRef;
54
use rustc_data_structures::sync::Lock;
@@ -153,7 +152,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
153152
}
154153

155154
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
156-
ty::query::force_from_dep_node(*self, dep_node)
155+
dep_node.kind.force_from_dep_node(*self, dep_node)
157156
}
158157

159158
fn has_errors_or_delayed_span_bugs(&self) -> bool {
@@ -166,7 +165,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
166165

167166
// Interactions with on_disk_cache
168167
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
169-
try_load_from_on_disk_cache(*self, dep_node)
168+
dep_node.kind.try_load_from_on_disk_cache(*self, dep_node)
170169
}
171170

172171
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {

compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::dep_graph::{self, dep_kind, DepNode};
1+
use crate::dep_graph;
22
use crate::hir::exports::Export;
33
use crate::hir::map;
44
use crate::infer::canonical::{self, Canonical};
@@ -103,74 +103,6 @@ pub use self::profiling_support::{IntoSelfProfilingString, QueryKeyStringBuilder
103103

104104
rustc_query_append! { [define_queries!][<'tcx>] }
105105

106-
/// The red/green evaluation system will try to mark a specific DepNode in the
107-
/// dependency graph as green by recursively trying to mark the dependencies of
108-
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
109-
/// where we don't know if it is red or green and we therefore actually have
110-
/// to recompute its value in order to find out. Since the only piece of
111-
/// information that we have at that point is the `DepNode` we are trying to
112-
/// re-evaluate, we need some way to re-run a query from just that. This is what
113-
/// `force_from_dep_node()` implements.
114-
///
115-
/// In the general case, a `DepNode` consists of a `DepKind` and an opaque
116-
/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
117-
/// is usually constructed by computing a stable hash of the query-key that the
118-
/// `DepNode` corresponds to. Consequently, it is not in general possible to go
119-
/// back from hash to query-key (since hash functions are not reversible). For
120-
/// this reason `force_from_dep_node()` is expected to fail from time to time
121-
/// because we just cannot find out, from the `DepNode` alone, what the
122-
/// corresponding query-key is and therefore cannot re-run the query.
123-
///
124-
/// The system deals with this case letting `try_mark_green` fail which forces
125-
/// the root query to be re-evaluated.
126-
///
127-
/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
128-
/// Fortunately, we can use some contextual information that will allow us to
129-
/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
130-
/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
131-
/// valid `DefPathHash`. Since we also always build a huge table that maps every
132-
/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
133-
/// everything we need to re-run the query.
134-
///
135-
/// Take the `mir_promoted` query as an example. Like many other queries, it
136-
/// just has a single parameter: the `DefId` of the item it will compute the
137-
/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
138-
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
139-
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
140-
/// `DefId` in `tcx.def_path_hash_to_def_id`.
141-
///
142-
/// When you implement a new query, it will likely have a corresponding new
143-
/// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
144-
/// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
145-
/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
146-
/// add it to the "We don't have enough information to reconstruct..." group in
147-
/// the match below.
148-
pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool {
149-
// We must avoid ever having to call `force_from_dep_node()` for a
150-
// `DepNode::codegen_unit`:
151-
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
152-
// would always end up having to evaluate the first caller of the
153-
// `codegen_unit` query that *is* reconstructible. This might very well be
154-
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
155-
// to re-trigger calling the `codegen_unit` query with the right key. At
156-
// that point we would already have re-done all the work we are trying to
157-
// avoid doing in the first place.
158-
// The solution is simple: Just explicitly call the `codegen_unit` query for
159-
// each CGU, right after partitioning. This way `try_mark_green` will always
160-
// hit the cache instead of having to go through `force_from_dep_node`.
161-
// This assertion makes sure, we actually keep applying the solution above.
162-
debug_assert!(
163-
dep_node.kind != &dep_kind::codegen_unit,
164-
"calling force_from_dep_node() on DepKind::codegen_unit"
165-
);
166-
167-
dep_node.kind.force_from_dep_node(tcx, dep_node)
168-
}
169-
170-
pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
171-
dep_node.kind.try_load_from_on_disk_cache(tcx, dep_node)
172-
}
173-
174106
mod sealed {
175107
use super::{DefId, LocalDefId};
176108

0 commit comments

Comments
 (0)