|
1 |
| -use crate::dep_graph::{self, DepConstructor, DepNode}; |
| 1 | +use crate::dep_graph::{self, DepConstructor, DepNode, DepNodeParams}; |
2 | 2 | use crate::hir::exports::Export;
|
3 | 3 | use crate::infer::canonical::{self, Canonical};
|
4 | 4 | use crate::lint::LintLevelMap;
|
@@ -60,8 +60,8 @@ use std::sync::Arc;
|
60 | 60 |
|
61 | 61 | #[macro_use]
|
62 | 62 | mod plumbing;
|
| 63 | +pub use self::plumbing::CycleError; |
63 | 64 | use self::plumbing::*;
|
64 |
| -pub use self::plumbing::{force_from_dep_node, CycleError}; |
65 | 65 |
|
66 | 66 | mod stats;
|
67 | 67 | pub use self::stats::print_stats;
|
@@ -105,3 +105,104 @@ pub use self::profiling_support::{IntoSelfProfilingString, QueryKeyStringBuilder
|
105 | 105 | // as they will raise an fatal error on query cycles instead.
|
106 | 106 |
|
107 | 107 | rustc_query_append! { [define_queries!][<'tcx>] }
|
| 108 | + |
| 109 | +/// The red/green evaluation system will try to mark a specific DepNode in the |
| 110 | +/// dependency graph as green by recursively trying to mark the dependencies of |
| 111 | +/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode` |
| 112 | +/// where we don't know if it is red or green and we therefore actually have |
| 113 | +/// to recompute its value in order to find out. Since the only piece of |
| 114 | +/// information that we have at that point is the `DepNode` we are trying to |
| 115 | +/// re-evaluate, we need some way to re-run a query from just that. This is what |
| 116 | +/// `force_from_dep_node()` implements. |
| 117 | +/// |
| 118 | +/// In the general case, a `DepNode` consists of a `DepKind` and an opaque |
| 119 | +/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint |
| 120 | +/// is usually constructed by computing a stable hash of the query-key that the |
| 121 | +/// `DepNode` corresponds to. Consequently, it is not in general possible to go |
| 122 | +/// back from hash to query-key (since hash functions are not reversible). For |
| 123 | +/// this reason `force_from_dep_node()` is expected to fail from time to time |
| 124 | +/// because we just cannot find out, from the `DepNode` alone, what the |
| 125 | +/// corresponding query-key is and therefore cannot re-run the query. |
| 126 | +/// |
| 127 | +/// The system deals with this case letting `try_mark_green` fail which forces |
| 128 | +/// the root query to be re-evaluated. |
| 129 | +/// |
| 130 | +/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless. |
| 131 | +/// Fortunately, we can use some contextual information that will allow us to |
| 132 | +/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we |
| 133 | +/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a |
| 134 | +/// valid `DefPathHash`. Since we also always build a huge table that maps every |
| 135 | +/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have |
| 136 | +/// everything we need to re-run the query. |
| 137 | +/// |
| 138 | +/// Take the `mir_validated` query as an example. Like many other queries, it |
| 139 | +/// just has a single parameter: the `DefId` of the item it will compute the |
| 140 | +/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode` |
| 141 | +/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode` |
| 142 | +/// is actually a `DefPathHash`, and can therefore just look up the corresponding |
| 143 | +/// `DefId` in `tcx.def_path_hash_to_def_id`. |
| 144 | +/// |
| 145 | +/// When you implement a new query, it will likely have a corresponding new |
| 146 | +/// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As |
| 147 | +/// a rule of thumb, if your query takes a `DefId` or `DefIndex` as sole parameter, |
| 148 | +/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just |
| 149 | +/// add it to the "We don't have enough information to reconstruct..." group in |
| 150 | +/// the match below. |
| 151 | +pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool { |
| 152 | + use crate::dep_graph::DepKind; |
| 153 | + |
| 154 | + // We must avoid ever having to call `force_from_dep_node()` for a |
| 155 | + // `DepNode::codegen_unit`: |
| 156 | + // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we |
| 157 | + // would always end up having to evaluate the first caller of the |
| 158 | + // `codegen_unit` query that *is* reconstructible. This might very well be |
| 159 | + // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just |
| 160 | + // to re-trigger calling the `codegen_unit` query with the right key. At |
| 161 | + // that point we would already have re-done all the work we are trying to |
| 162 | + // avoid doing in the first place. |
| 163 | + // The solution is simple: Just explicitly call the `codegen_unit` query for |
| 164 | + // each CGU, right after partitioning. This way `try_mark_green` will always |
| 165 | + // hit the cache instead of having to go through `force_from_dep_node`. |
| 166 | + // This assertion makes sure, we actually keep applying the solution above. |
| 167 | + debug_assert!( |
| 168 | + dep_node.kind != DepKind::codegen_unit, |
| 169 | + "calling force_from_dep_node() on DepKind::codegen_unit" |
| 170 | + ); |
| 171 | + |
| 172 | + if !dep_node.kind.can_reconstruct_query_key() { |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + rustc_dep_node_force!([dep_node, tcx] |
| 177 | + // These are inputs that are expected to be pre-allocated and that |
| 178 | + // should therefore always be red or green already. |
| 179 | + DepKind::AllLocalTraitImpls | |
| 180 | + DepKind::CrateMetadata | |
| 181 | + DepKind::HirBody | |
| 182 | + DepKind::Hir | |
| 183 | + |
| 184 | + // These are anonymous nodes. |
| 185 | + DepKind::TraitSelect | |
| 186 | + |
| 187 | + // We don't have enough information to reconstruct the query key of |
| 188 | + // these. |
| 189 | + DepKind::CompileCodegenUnit => { |
| 190 | + bug!("force_from_dep_node: encountered {:?}", dep_node) |
| 191 | + } |
| 192 | + ); |
| 193 | + |
| 194 | + false |
| 195 | +} |
| 196 | + |
| 197 | +impl DepNode { |
| 198 | + /// Check whether the query invocation corresponding to the given |
| 199 | + /// DepNode is eligible for on-disk-caching. If so, this is method |
| 200 | + /// will execute the query corresponding to the given DepNode. |
| 201 | + /// Also, as a sanity check, it expects that the corresponding query |
| 202 | + /// invocation has been marked as green already. |
| 203 | + pub fn try_load_from_on_disk_cache<'tcx>(&self, tcx: TyCtxt<'tcx>) { |
| 204 | + use crate::dep_graph::DepKind; |
| 205 | + |
| 206 | + rustc_dep_node_try_load_from_on_disk_cache!(self, tcx) |
| 207 | + } |
| 208 | +} |
0 commit comments