|
12 | 12 | //! that generate the actual methods on tcx which find and execute the
|
13 | 13 | //! provider, manage the caches, and so forth.
|
14 | 14 |
|
15 |
| -use dep_graph::{DepNodeIndex, DepNode, DepKind}; |
| 15 | +use dep_graph::{DepNodeIndex, DepNode, DepKind, DepNodeColor}; |
16 | 16 | use errors::{Diagnostic, DiagnosticBuilder};
|
17 | 17 | use ty::{TyCtxt};
|
18 | 18 | use ty::maps::Query; // NB: actually generated by the macros in this file
|
@@ -133,6 +133,40 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
133 | 133 |
|
134 | 134 | Ok(result)
|
135 | 135 | }
|
| 136 | + |
| 137 | + /// Try to read a node index for the node dep_node. |
| 138 | + /// A node will have an index, when it's already been marked green, or when we can mark it |
| 139 | + /// green. This function will mark the current task as a reader of the specified node, when |
| 140 | + /// the a node index can be found for that node. |
| 141 | + pub(super) fn try_mark_green_and_read(self, dep_node: &DepNode) -> Option<DepNodeIndex> { |
| 142 | + match self.dep_graph.node_color(dep_node) { |
| 143 | + Some(DepNodeColor::Green(dep_node_index)) => { |
| 144 | + self.dep_graph.read_index(dep_node_index); |
| 145 | + Some(dep_node_index) |
| 146 | + } |
| 147 | + Some(DepNodeColor::Red) => { |
| 148 | + None |
| 149 | + } |
| 150 | + None => { |
| 151 | + // try_mark_green (called below) will panic when full incremental |
| 152 | + // compilation is disabled. If that's the case, we can't try to mark nodes |
| 153 | + // as green anyway, so we can safely return None here. |
| 154 | + if !self.dep_graph.is_fully_enabled() { |
| 155 | + return None; |
| 156 | + } |
| 157 | + match self.dep_graph.try_mark_green(self, &dep_node) { |
| 158 | + Some(dep_node_index) => { |
| 159 | + debug_assert!(self.dep_graph.is_green(dep_node_index)); |
| 160 | + self.dep_graph.read_index(dep_node_index); |
| 161 | + Some(dep_node_index) |
| 162 | + } |
| 163 | + None => { |
| 164 | + None |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
136 | 170 | }
|
137 | 171 |
|
138 | 172 | // If enabled, send a message to the profile-queries thread
|
@@ -309,25 +343,8 @@ macro_rules! define_maps {
|
309 | 343 | }
|
310 | 344 |
|
311 | 345 | if !dep_node.kind.is_input() {
|
312 |
| - use dep_graph::DepNodeColor; |
313 |
| - if let Some(DepNodeColor::Green(dep_node_index)) = tcx.dep_graph |
314 |
| - .node_color(&dep_node) { |
| 346 | + if let Some(dep_node_index) = tcx.try_mark_green_and_read(&dep_node) { |
315 | 347 | profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
|
316 |
| - tcx.dep_graph.read_index(dep_node_index); |
317 |
| - return Self::load_from_disk_and_cache_in_memory(tcx, |
318 |
| - key, |
319 |
| - span, |
320 |
| - dep_node_index) |
321 |
| - } |
322 |
| - |
323 |
| - debug!("ty::queries::{}::try_get_with(key={:?}) - running try_mark_green", |
324 |
| - stringify!($name), |
325 |
| - key); |
326 |
| - |
327 |
| - if let Some(dep_node_index) = tcx.dep_graph.try_mark_green(tcx, &dep_node) { |
328 |
| - debug_assert!(tcx.dep_graph.is_green(dep_node_index)); |
329 |
| - profq_msg!(tcx, ProfileQueriesMsg::CacheHit); |
330 |
| - tcx.dep_graph.read_index(dep_node_index); |
331 | 348 | return Self::load_from_disk_and_cache_in_memory(tcx,
|
332 | 349 | key,
|
333 | 350 | span,
|
@@ -357,36 +374,14 @@ macro_rules! define_maps {
|
357 | 374 | // Ensuring an "input" or anonymous query makes no sense
|
358 | 375 | assert!(!dep_node.kind.is_anon());
|
359 | 376 | assert!(!dep_node.kind.is_input());
|
360 |
| - use dep_graph::DepNodeColor; |
361 |
| - match tcx.dep_graph.node_color(&dep_node) { |
362 |
| - Some(DepNodeColor::Green(dep_node_index)) => { |
363 |
| - tcx.dep_graph.read_index(dep_node_index); |
364 |
| - } |
365 |
| - Some(DepNodeColor::Red) => { |
366 |
| - // A DepNodeColor::Red DepNode means that this query was executed |
367 |
| - // before. We can not call `dep_graph.read()` here as we don't have |
368 |
| - // the DepNodeIndex. Instead, We call the query again to issue the |
369 |
| - // appropriate `dep_graph.read()` call. The performance cost this |
370 |
| - // introduces should be negligible as we'll immediately hit the |
371 |
| - // in-memory cache. |
372 |
| - let _ = tcx.$name(key); |
373 |
| - } |
374 |
| - None => { |
375 |
| - // Huh |
376 |
| - if !tcx.dep_graph.is_fully_enabled() { |
377 |
| - let _ = tcx.$name(key); |
378 |
| - return; |
379 |
| - } |
380 |
| - match tcx.dep_graph.try_mark_green(tcx, &dep_node) { |
381 |
| - Some(dep_node_index) => { |
382 |
| - debug_assert!(tcx.dep_graph.is_green(dep_node_index)); |
383 |
| - tcx.dep_graph.read_index(dep_node_index); |
384 |
| - } |
385 |
| - None => { |
386 |
| - let _ = tcx.$name(key); |
387 |
| - } |
388 |
| - } |
389 |
| - } |
| 377 | + if tcx.try_mark_green_and_read(&dep_node).is_none() { |
| 378 | + // A None return from `try_mark_green_and_read` means that this is either |
| 379 | + // a new dep node or that the dep node has already been marked red. |
| 380 | + // Either way, we can't call `dep_graph.read()` as we don't have the |
| 381 | + // DepNodeIndex. We must invoke the query itself. The performance cost |
| 382 | + // this introduces should be negligible as we'll immediately hit the |
| 383 | + // in-memory cache, or another query down the line will. |
| 384 | + let _ = tcx.$name(key); |
390 | 385 | }
|
391 | 386 | }
|
392 | 387 |
|
|
0 commit comments