]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/graph.rs
5d2d30f8debb3f0030ef1fdd213b589b94eeb731
[rust.git] / src / librustc / dep_graph / graph.rs
1 use errors::Diagnostic;
2 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4 use rustc_index::vec::{Idx, IndexVec};
5 use smallvec::SmallVec;
6 use rustc_data_structures::sync::{Lrc, Lock, AtomicU32, AtomicU64, Ordering};
7 use std::sync::atomic::Ordering::SeqCst;
8 use std::env;
9 use std::hash::Hash;
10 use std::collections::hash_map::Entry;
11 use std::mem;
12 use crate::ty::{self, TyCtxt};
13 use parking_lot::{Mutex, Condvar};
14
15 use crate::ich::{StableHashingContext, StableHashingContextProvider, Fingerprint};
16
17 use super::debug::EdgeFilter;
18 use super::dep_node::{DepNode, DepKind, WorkProductId};
19 use super::query::DepGraphQuery;
20 use super::safe::DepGraphSafe;
21 use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
22 use super::prev::PreviousDepGraph;
23
24 #[derive(Clone)]
25 pub struct DepGraph {
26     data: Option<Lrc<DepGraphData>>,
27 }
28
29 rustc_index::newtype_index! {
30     pub struct DepNodeIndex { .. }
31 }
32
33 impl DepNodeIndex {
34     pub const INVALID: DepNodeIndex = DepNodeIndex::MAX;
35 }
36
37 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
38 pub enum DepNodeColor {
39     Red,
40     Green(DepNodeIndex)
41 }
42
43 impl DepNodeColor {
44     pub fn is_green(self) -> bool {
45         match self {
46             DepNodeColor::Red => false,
47             DepNodeColor::Green(_) => true,
48         }
49     }
50 }
51
52 struct DepGraphData {
53     /// The new encoding of the dependency graph, optimized for red/green
54     /// tracking. The `current` field is the dependency graph of only the
55     /// current compilation session: We don't merge the previous dep-graph into
56     /// current one anymore.
57     current: CurrentDepGraph,
58
59     /// The dep-graph from the previous compilation session. It contains all
60     /// nodes and edges as well as all fingerprints of nodes that have them.
61     previous: PreviousDepGraph,
62
63     colors: DepNodeColorMap,
64
65     /// A set of loaded diagnostics that is in the progress of being emitted.
66     emitting_diagnostics: Mutex<FxHashSet<DepNodeIndex>>,
67
68     /// Used to wait for diagnostics to be emitted.
69     emitting_diagnostics_cond_var: Condvar,
70
71     /// When we load, there may be `.o` files, cached MIR, or other such
72     /// things available to us. If we find that they are not dirty, we
73     /// load the path to the file storing those work-products here into
74     /// this map. We can later look for and extract that data.
75     previous_work_products: FxHashMap<WorkProductId, WorkProduct>,
76
77     dep_node_debug: Lock<FxHashMap<DepNode, String>>,
78 }
79
80 pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Option<Fingerprint>
81 where
82     R: for<'a> HashStable<StableHashingContext<'a>>,
83 {
84     let mut stable_hasher = StableHasher::new();
85     result.hash_stable(hcx, &mut stable_hasher);
86
87     Some(stable_hasher.finish())
88 }
89
90 impl DepGraph {
91     pub fn new(prev_graph: PreviousDepGraph,
92                prev_work_products: FxHashMap<WorkProductId, WorkProduct>) -> DepGraph {
93         let prev_graph_node_count = prev_graph.node_count();
94
95         DepGraph {
96             data: Some(Lrc::new(DepGraphData {
97                 previous_work_products: prev_work_products,
98                 dep_node_debug: Default::default(),
99                 current: CurrentDepGraph::new(prev_graph_node_count),
100                 emitting_diagnostics: Default::default(),
101                 emitting_diagnostics_cond_var: Condvar::new(),
102                 previous: prev_graph,
103                 colors: DepNodeColorMap::new(prev_graph_node_count),
104             })),
105         }
106     }
107
108     pub fn new_disabled() -> DepGraph {
109         DepGraph {
110             data: None,
111         }
112     }
113
114     /// Returns `true` if we are actually building the full dep-graph, and `false` otherwise.
115     #[inline]
116     pub fn is_fully_enabled(&self) -> bool {
117         self.data.is_some()
118     }
119
120     pub fn query(&self) -> DepGraphQuery {
121         let data = self.data.as_ref().unwrap().current.data.lock();
122         let nodes: Vec<_> = data.iter().map(|n| n.node).collect();
123         let mut edges = Vec::new();
124         for (from, edge_targets) in data.iter().map(|d| (d.node, &d.edges)) {
125             for &edge_target in edge_targets.iter() {
126                 let to = data[edge_target].node;
127                 edges.push((from, to));
128             }
129         }
130
131         DepGraphQuery::new(&nodes[..], &edges[..])
132     }
133
134     pub fn assert_ignored(&self)
135     {
136         if let Some(..) = self.data {
137             ty::tls::with_context_opt(|icx| {
138                 let icx = if let Some(icx) = icx { icx } else { return };
139                 assert!(icx.task_deps.is_none(), "expected no task dependency tracking");
140             })
141         }
142     }
143
144     pub fn with_ignore<OP,R>(&self, op: OP) -> R
145         where OP: FnOnce() -> R
146     {
147         ty::tls::with_context(|icx| {
148             let icx = ty::tls::ImplicitCtxt {
149                 task_deps: None,
150                 ..icx.clone()
151             };
152
153             ty::tls::enter_context(&icx, |_| {
154                 op()
155             })
156         })
157     }
158
159     /// Starts a new dep-graph task. Dep-graph tasks are specified
160     /// using a free function (`task`) and **not** a closure -- this
161     /// is intentional because we want to exercise tight control over
162     /// what state they have access to. In particular, we want to
163     /// prevent implicit 'leaks' of tracked state into the task (which
164     /// could then be read without generating correct edges in the
165     /// dep-graph -- see the [rustc guide] for more details on
166     /// the dep-graph). To this end, the task function gets exactly two
167     /// pieces of state: the context `cx` and an argument `arg`. Both
168     /// of these bits of state must be of some type that implements
169     /// `DepGraphSafe` and hence does not leak.
170     ///
171     /// The choice of two arguments is not fundamental. One argument
172     /// would work just as well, since multiple values can be
173     /// collected using tuples. However, using two arguments works out
174     /// to be quite convenient, since it is common to need a context
175     /// (`cx`) and some argument (e.g., a `DefId` identifying what
176     /// item to process).
177     ///
178     /// For cases where you need some other number of arguments:
179     ///
180     /// - If you only need one argument, just use `()` for the `arg`
181     ///   parameter.
182     /// - If you need 3+ arguments, use a tuple for the
183     ///   `arg` parameter.
184     ///
185     /// [rustc guide]: https://rust-lang.github.io/rustc-guide/incremental-compilation.html
186     pub fn with_task<'a, C, A, R>(
187         &self,
188         key: DepNode,
189         cx: C,
190         arg: A,
191         task: fn(C, A) -> R,
192         hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
193     ) -> (R, DepNodeIndex)
194     where
195         C: DepGraphSafe + StableHashingContextProvider<'a>,
196     {
197         self.with_task_impl(key, cx, arg, false, task,
198             |_key| Some(TaskDeps {
199                 #[cfg(debug_assertions)]
200                 node: Some(_key),
201                 reads: SmallVec::new(),
202                 read_set: Default::default(),
203             }),
204             |data, key, fingerprint, task| {
205                 data.complete_task(key, task.unwrap(), fingerprint)
206             },
207             hash_result)
208     }
209
210     /// Creates a new dep-graph input with value `input`
211     pub fn input_task<'a, C, R>(&self,
212                                    key: DepNode,
213                                    cx: C,
214                                    input: R)
215                                    -> (R, DepNodeIndex)
216         where C: DepGraphSafe + StableHashingContextProvider<'a>,
217               R: for<'b> HashStable<StableHashingContext<'b>>,
218     {
219         fn identity_fn<C, A>(_: C, arg: A) -> A {
220             arg
221         }
222
223         self.with_task_impl(key, cx, input, true, identity_fn,
224             |_| None,
225             |data, key, fingerprint, _| {
226                 data.alloc_node(key, SmallVec::new(), fingerprint)
227             },
228             hash_result::<R>)
229     }
230
231     fn with_task_impl<'a, C, A, R>(
232         &self,
233         key: DepNode,
234         cx: C,
235         arg: A,
236         no_tcx: bool,
237         task: fn(C, A) -> R,
238         create_task: fn(DepNode) -> Option<TaskDeps>,
239         finish_task_and_alloc_depnode: fn(&CurrentDepGraph,
240                                           DepNode,
241                                           Fingerprint,
242                                           Option<TaskDeps>) -> DepNodeIndex,
243         hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
244     ) -> (R, DepNodeIndex)
245     where
246         C: DepGraphSafe + StableHashingContextProvider<'a>,
247     {
248         if let Some(ref data) = self.data {
249             let task_deps = create_task(key).map(|deps| Lock::new(deps));
250
251             // In incremental mode, hash the result of the task. We don't
252             // do anything with the hash yet, but we are computing it
253             // anyway so that
254             //  - we make sure that the infrastructure works and
255             //  - we can get an idea of the runtime cost.
256             let mut hcx = cx.get_stable_hashing_context();
257
258             let result = if no_tcx {
259                 task(cx, arg)
260             } else {
261                 ty::tls::with_context(|icx| {
262                     let icx = ty::tls::ImplicitCtxt {
263                         task_deps: task_deps.as_ref(),
264                         ..icx.clone()
265                     };
266
267                     ty::tls::enter_context(&icx, |_| {
268                         task(cx, arg)
269                     })
270                 })
271             };
272
273             let current_fingerprint = hash_result(&mut hcx, &result);
274
275             let dep_node_index = finish_task_and_alloc_depnode(
276                 &data.current,
277                 key,
278                 current_fingerprint.unwrap_or(Fingerprint::ZERO),
279                 task_deps.map(|lock| lock.into_inner()),
280             );
281
282             let print_status = cfg!(debug_assertions) && hcx.sess().opts.debugging_opts.dep_tasks;
283
284             // Determine the color of the new DepNode.
285             if let Some(prev_index) = data.previous.node_to_index_opt(&key) {
286                 let prev_fingerprint = data.previous.fingerprint_by_index(prev_index);
287
288                 let color = if let Some(current_fingerprint) = current_fingerprint {
289                     if current_fingerprint == prev_fingerprint {
290                         if print_status {
291                             eprintln!("[task::green] {:?}", key);
292                         }
293                         DepNodeColor::Green(dep_node_index)
294                     } else {
295                         if print_status {
296                             eprintln!("[task::red] {:?}", key);
297                         }
298                         DepNodeColor::Red
299                     }
300                 } else {
301                     if print_status {
302                         eprintln!("[task::unknown] {:?}", key);
303                     }
304                     // Mark the node as Red if we can't hash the result
305                     DepNodeColor::Red
306                 };
307
308                 debug_assert!(data.colors.get(prev_index).is_none(),
309                             "DepGraph::with_task() - Duplicate DepNodeColor \
310                             insertion for {:?}", key);
311
312                 data.colors.insert(prev_index, color);
313             } else {
314                 if print_status {
315                     eprintln!("[task::new] {:?}", key);
316                 }
317             }
318
319             (result, dep_node_index)
320         } else {
321             (task(cx, arg), DepNodeIndex::INVALID)
322         }
323     }
324
325     /// Executes something within an "anonymous" task, that is, a task the
326     /// `DepNode` of which is determined by the list of inputs it read from.
327     pub fn with_anon_task<OP,R>(&self, dep_kind: DepKind, op: OP) -> (R, DepNodeIndex)
328         where OP: FnOnce() -> R
329     {
330         if let Some(ref data) = self.data {
331             let (result, task_deps) = ty::tls::with_context(|icx| {
332                 let task_deps = Lock::new(TaskDeps {
333                     #[cfg(debug_assertions)]
334                     node: None,
335                     reads: SmallVec::new(),
336                     read_set: Default::default(),
337                 });
338
339                 let r = {
340                     let icx = ty::tls::ImplicitCtxt {
341                         task_deps: Some(&task_deps),
342                         ..icx.clone()
343                     };
344
345                     ty::tls::enter_context(&icx, |_| {
346                         op()
347                     })
348                 };
349
350                 (r, task_deps.into_inner())
351             });
352             let dep_node_index = data.current
353                                      .complete_anon_task(dep_kind, task_deps);
354             (result, dep_node_index)
355         } else {
356             (op(), DepNodeIndex::INVALID)
357         }
358     }
359
360     /// Executes something within an "eval-always" task which is a task
361     /// that runs whenever anything changes.
362     pub fn with_eval_always_task<'a, C, A, R>(
363         &self,
364         key: DepNode,
365         cx: C,
366         arg: A,
367         task: fn(C, A) -> R,
368         hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
369     ) -> (R, DepNodeIndex)
370     where
371         C: DepGraphSafe + StableHashingContextProvider<'a>,
372     {
373         self.with_task_impl(key, cx, arg, false, task,
374             |_| None,
375             |data, key, fingerprint, _| {
376                 data.alloc_node(key, smallvec![], fingerprint)
377             },
378             hash_result)
379     }
380
381     #[inline]
382     pub fn read(&self, v: DepNode) {
383         if let Some(ref data) = self.data {
384             let map = data.current.node_to_node_index.lock();
385             if let Some(dep_node_index) = map.get(&v).copied() {
386                 std::mem::drop(map);
387                 data.read_index(dep_node_index);
388             } else {
389                 bug!("DepKind {:?} should be pre-allocated but isn't.", v.kind)
390             }
391         }
392     }
393
394     #[inline]
395     pub fn read_index(&self, dep_node_index: DepNodeIndex) {
396         if let Some(ref data) = self.data {
397             data.read_index(dep_node_index);
398         }
399     }
400
401     #[inline]
402     pub fn dep_node_index_of(&self, dep_node: &DepNode) -> DepNodeIndex {
403         self.data
404             .as_ref()
405             .unwrap()
406             .current
407             .node_to_node_index
408             .lock()
409             .get(dep_node)
410             .cloned()
411             .unwrap()
412     }
413
414     #[inline]
415     pub fn dep_node_exists(&self, dep_node: &DepNode) -> bool {
416         if let Some(ref data) = self.data {
417             data.current.node_to_node_index.lock().contains_key(dep_node)
418         } else {
419             false
420         }
421     }
422
423     #[inline]
424     pub fn fingerprint_of(&self, dep_node_index: DepNodeIndex) -> Fingerprint {
425         let data = self.data.as_ref().expect("dep graph enabled").current.data.lock();
426         data[dep_node_index].fingerprint
427     }
428
429     pub fn prev_fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
430         self.data.as_ref().unwrap().previous.fingerprint_of(dep_node)
431     }
432
433     #[inline]
434     pub fn prev_dep_node_index_of(&self, dep_node: &DepNode) -> SerializedDepNodeIndex {
435         self.data.as_ref().unwrap().previous.node_to_index(dep_node)
436     }
437
438     /// Checks whether a previous work product exists for `v` and, if
439     /// so, return the path that leads to it. Used to skip doing work.
440     pub fn previous_work_product(&self, v: &WorkProductId) -> Option<WorkProduct> {
441         self.data
442             .as_ref()
443             .and_then(|data| {
444                 data.previous_work_products.get(v).cloned()
445             })
446     }
447
448     /// Access the map of work-products created during the cached run. Only
449     /// used during saving of the dep-graph.
450     pub fn previous_work_products(&self) -> &FxHashMap<WorkProductId, WorkProduct> {
451         &self.data.as_ref().unwrap().previous_work_products
452     }
453
454     #[inline(always)]
455     pub fn register_dep_node_debug_str<F>(&self,
456                                           dep_node: DepNode,
457                                           debug_str_gen: F)
458         where F: FnOnce() -> String
459     {
460         let dep_node_debug = &self.data.as_ref().unwrap().dep_node_debug;
461
462         if dep_node_debug.borrow().contains_key(&dep_node) {
463             return
464         }
465         let debug_str = debug_str_gen();
466         dep_node_debug.borrow_mut().insert(dep_node, debug_str);
467     }
468
469     pub(super) fn dep_node_debug_str(&self, dep_node: DepNode) -> Option<String> {
470         self.data
471             .as_ref()?
472             .dep_node_debug
473             .borrow()
474             .get(&dep_node)
475             .cloned()
476     }
477
478     pub fn edge_deduplication_data(&self) -> Option<(u64, u64)> {
479         if cfg!(debug_assertions) {
480             let current_dep_graph = &self.data.as_ref().unwrap().current;
481
482             Some((current_dep_graph.total_read_count.load(SeqCst),
483                   current_dep_graph.total_duplicate_read_count.load(SeqCst)))
484         } else {
485             None
486         }
487     }
488
489     pub fn serialize(&self) -> SerializedDepGraph {
490         let data = self.data.as_ref().unwrap().current.data.lock();
491
492         let fingerprints: IndexVec<SerializedDepNodeIndex, _> =
493             data.iter().map(|d| d.fingerprint).collect();
494         let nodes: IndexVec<SerializedDepNodeIndex, _> =
495             data.iter().map(|d| d.node).collect();
496
497         let total_edge_count: usize = data.iter().map(|d| d.edges.len()).sum();
498
499         let mut edge_list_indices = IndexVec::with_capacity(nodes.len());
500         let mut edge_list_data = Vec::with_capacity(total_edge_count);
501
502         for (current_dep_node_index, edges) in data.iter_enumerated().map(|(i, d)| (i, &d.edges)) {
503             let start = edge_list_data.len() as u32;
504             // This should really just be a memcpy :/
505             edge_list_data.extend(edges.iter().map(|i| SerializedDepNodeIndex::new(i.index())));
506             let end = edge_list_data.len() as u32;
507
508             debug_assert_eq!(current_dep_node_index.index(), edge_list_indices.len());
509             edge_list_indices.push((start, end));
510         }
511
512         debug_assert!(edge_list_data.len() <= ::std::u32::MAX as usize);
513         debug_assert_eq!(edge_list_data.len(), total_edge_count);
514
515         SerializedDepGraph {
516             nodes,
517             fingerprints,
518             edge_list_indices,
519             edge_list_data,
520         }
521     }
522
523     pub fn node_color(&self, dep_node: &DepNode) -> Option<DepNodeColor> {
524         if let Some(ref data) = self.data {
525             if let Some(prev_index) = data.previous.node_to_index_opt(dep_node) {
526                 return data.colors.get(prev_index)
527             } else {
528                 // This is a node that did not exist in the previous compilation
529                 // session, so we consider it to be red.
530                 return Some(DepNodeColor::Red)
531             }
532         }
533
534         None
535     }
536
537     /// Try to read a node index for the node dep_node.
538     /// A node will have an index, when it's already been marked green, or when we can mark it
539     /// green. This function will mark the current task as a reader of the specified node, when
540     /// a node index can be found for that node.
541     pub fn try_mark_green_and_read(
542         &self,
543         tcx: TyCtxt<'_>,
544         dep_node: &DepNode,
545     ) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> {
546         self.try_mark_green(tcx, dep_node).map(|(prev_index, dep_node_index)| {
547             debug_assert!(self.is_green(&dep_node));
548             self.read_index(dep_node_index);
549             (prev_index, dep_node_index)
550         })
551     }
552
553     pub fn try_mark_green(
554         &self,
555         tcx: TyCtxt<'_>,
556         dep_node: &DepNode,
557     ) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> {
558         debug_assert!(!dep_node.kind.is_eval_always());
559
560         // Return None if the dep graph is disabled
561         let data = self.data.as_ref()?;
562
563         // Return None if the dep node didn't exist in the previous session
564         let prev_index = data.previous.node_to_index_opt(dep_node)?;
565
566         match data.colors.get(prev_index) {
567             Some(DepNodeColor::Green(dep_node_index)) => Some((prev_index, dep_node_index)),
568             Some(DepNodeColor::Red) => None,
569             None => {
570                 // This DepNode and the corresponding query invocation existed
571                 // in the previous compilation session too, so we can try to
572                 // mark it as green by recursively marking all of its
573                 // dependencies green.
574                 self.try_mark_previous_green(
575                     tcx,
576                     data,
577                     prev_index,
578                     &dep_node
579                 ).map(|dep_node_index| {
580                     (prev_index, dep_node_index)
581                 })
582             }
583         }
584     }
585
586     /// Try to mark a dep-node which existed in the previous compilation session as green.
587     fn try_mark_previous_green<'tcx>(
588         &self,
589         tcx: TyCtxt<'tcx>,
590         data: &DepGraphData,
591         prev_dep_node_index: SerializedDepNodeIndex,
592         dep_node: &DepNode,
593     ) -> Option<DepNodeIndex> {
594         debug!("try_mark_previous_green({:?}) - BEGIN", dep_node);
595
596         #[cfg(not(parallel_compiler))]
597         {
598             debug_assert!(!data.current.node_to_node_index.lock().contains_key(dep_node));
599             debug_assert!(data.colors.get(prev_dep_node_index).is_none());
600         }
601
602         // We never try to mark eval_always nodes as green
603         debug_assert!(!dep_node.kind.is_eval_always());
604
605         debug_assert_eq!(data.previous.index_to_node(prev_dep_node_index), *dep_node);
606
607         let prev_deps = data.previous.edge_targets_from(prev_dep_node_index);
608
609         let mut current_deps = SmallVec::new();
610
611         for &dep_dep_node_index in prev_deps {
612             let dep_dep_node_color = data.colors.get(dep_dep_node_index);
613
614             match dep_dep_node_color {
615                 Some(DepNodeColor::Green(node_index)) => {
616                     // This dependency has been marked as green before, we are
617                     // still fine and can continue with checking the other
618                     // dependencies.
619                     debug!("try_mark_previous_green({:?}) --- found dependency {:?} to \
620                             be immediately green",
621                             dep_node,
622                             data.previous.index_to_node(dep_dep_node_index));
623                     current_deps.push(node_index);
624                 }
625                 Some(DepNodeColor::Red) => {
626                     // We found a dependency the value of which has changed
627                     // compared to the previous compilation session. We cannot
628                     // mark the DepNode as green and also don't need to bother
629                     // with checking any of the other dependencies.
630                     debug!("try_mark_previous_green({:?}) - END - dependency {:?} was \
631                             immediately red",
632                             dep_node,
633                             data.previous.index_to_node(dep_dep_node_index));
634                     return None
635                 }
636                 None => {
637                     let dep_dep_node = &data.previous.index_to_node(dep_dep_node_index);
638
639                     // We don't know the state of this dependency. If it isn't
640                     // an eval_always node, let's try to mark it green recursively.
641                     if !dep_dep_node.kind.is_eval_always() {
642                          debug!("try_mark_previous_green({:?}) --- state of dependency {:?} \
643                                  is unknown, trying to mark it green", dep_node,
644                                  dep_dep_node);
645
646                         let node_index = self.try_mark_previous_green(
647                             tcx,
648                             data,
649                             dep_dep_node_index,
650                             dep_dep_node
651                         );
652                         if let Some(node_index) = node_index {
653                             debug!("try_mark_previous_green({:?}) --- managed to MARK \
654                                     dependency {:?} as green", dep_node, dep_dep_node);
655                             current_deps.push(node_index);
656                             continue;
657                         }
658                     } else {
659                         match dep_dep_node.kind {
660                             DepKind::Hir |
661                             DepKind::HirBody |
662                             DepKind::CrateMetadata => {
663                                 if dep_dep_node.extract_def_id(tcx).is_none() {
664                                     // If the node does not exist anymore, we
665                                     // just fail to mark green.
666                                     return None
667                                 } else {
668                                     // If the node does exist, it should have
669                                     // been pre-allocated.
670                                     bug!("DepNode {:?} should have been \
671                                           pre-allocated but wasn't.",
672                                           dep_dep_node)
673                                 }
674                             }
675                             _ => {
676                                 // For other kinds of nodes it's OK to be
677                                 // forced.
678                             }
679                         }
680                     }
681
682                     // We failed to mark it green, so we try to force the query.
683                     debug!("try_mark_previous_green({:?}) --- trying to force \
684                             dependency {:?}", dep_node, dep_dep_node);
685                     if crate::ty::query::force_from_dep_node(tcx, dep_dep_node) {
686                         let dep_dep_node_color = data.colors.get(dep_dep_node_index);
687
688                         match dep_dep_node_color {
689                             Some(DepNodeColor::Green(node_index)) => {
690                                 debug!("try_mark_previous_green({:?}) --- managed to \
691                                         FORCE dependency {:?} to green",
692                                         dep_node, dep_dep_node);
693                                 current_deps.push(node_index);
694                             }
695                             Some(DepNodeColor::Red) => {
696                                 debug!("try_mark_previous_green({:?}) - END - \
697                                         dependency {:?} was red after forcing",
698                                        dep_node,
699                                        dep_dep_node);
700                                 return None
701                             }
702                             None => {
703                                 if !tcx.sess.has_errors() {
704                                     bug!("try_mark_previous_green() - Forcing the DepNode \
705                                           should have set its color")
706                                 } else {
707                                     // If the query we just forced has resulted
708                                     // in some kind of compilation error, we
709                                     // don't expect that the corresponding
710                                     // dep-node color has been updated.
711                                 }
712                             }
713                         }
714                     } else {
715                         // The DepNode could not be forced.
716                         debug!("try_mark_previous_green({:?}) - END - dependency {:?} \
717                                 could not be forced", dep_node, dep_dep_node);
718                         return None
719                     }
720                 }
721             }
722         }
723
724         // If we got here without hitting a `return` that means that all
725         // dependencies of this DepNode could be marked as green. Therefore we
726         // can also mark this DepNode as green.
727
728         // There may be multiple threads trying to mark the same dep node green concurrently
729
730         let dep_node_index = {
731             // Copy the fingerprint from the previous graph,
732             // so we don't have to recompute it
733             let fingerprint = data.previous.fingerprint_by_index(prev_dep_node_index);
734
735             // We allocating an entry for the node in the current dependency graph and
736             // adding all the appropriate edges imported from the previous graph
737             data.current.intern_node(*dep_node, current_deps, fingerprint)
738         };
739
740         // ... emitting any stored diagnostic ...
741
742         // FIXME: Store the fact that a node has diagnostics in a bit in the dep graph somewhere
743         // Maybe store a list on disk and encode this fact in the DepNodeState
744         let diagnostics = tcx.queries.on_disk_cache
745                              .load_diagnostics(tcx, prev_dep_node_index);
746
747         #[cfg(not(parallel_compiler))]
748         debug_assert!(data.colors.get(prev_dep_node_index).is_none(),
749                       "DepGraph::try_mark_previous_green() - Duplicate DepNodeColor \
750                       insertion for {:?}", dep_node);
751
752         if unlikely!(diagnostics.len() > 0) {
753             self.emit_diagnostics(
754                 tcx,
755                 data,
756                 dep_node_index,
757                 prev_dep_node_index,
758                 diagnostics
759             );
760         }
761
762         // ... and finally storing a "Green" entry in the color map.
763         // Multiple threads can all write the same color here
764         data.colors.insert(prev_dep_node_index, DepNodeColor::Green(dep_node_index));
765
766         debug!("try_mark_previous_green({:?}) - END - successfully marked as green", dep_node);
767         Some(dep_node_index)
768     }
769
770     /// Atomically emits some loaded diagnostics.
771     /// This may be called concurrently on multiple threads for the same dep node.
772     #[cold]
773     #[inline(never)]
774     fn emit_diagnostics<'tcx>(
775         &self,
776         tcx: TyCtxt<'tcx>,
777         data: &DepGraphData,
778         dep_node_index: DepNodeIndex,
779         prev_dep_node_index: SerializedDepNodeIndex,
780         diagnostics: Vec<Diagnostic>,
781     ) {
782         let mut emitting = data.emitting_diagnostics.lock();
783
784         if data.colors.get(prev_dep_node_index) == Some(DepNodeColor::Green(dep_node_index)) {
785             // The node is already green so diagnostics must have been emitted already
786             return;
787         }
788
789         if emitting.insert(dep_node_index) {
790             // We were the first to insert the node in the set so this thread
791             // must emit the diagnostics and signal other potentially waiting
792             // threads after.
793             mem::drop(emitting);
794
795             // Promote the previous diagnostics to the current session.
796             tcx.queries.on_disk_cache
797                .store_diagnostics(dep_node_index, diagnostics.clone().into());
798
799             let handle = tcx.sess.diagnostic();
800
801             for diagnostic in diagnostics {
802                 handle.emit_diagnostic(&diagnostic);
803             }
804
805             // Mark the node as green now that diagnostics are emitted
806             data.colors.insert(prev_dep_node_index, DepNodeColor::Green(dep_node_index));
807
808             // Remove the node from the set
809             data.emitting_diagnostics.lock().remove(&dep_node_index);
810
811             // Wake up waiters
812             data.emitting_diagnostics_cond_var.notify_all();
813         } else {
814             // We must wait for the other thread to finish emitting the diagnostic
815
816             loop {
817                 data.emitting_diagnostics_cond_var.wait(&mut emitting);
818                 if data.colors
819                        .get(prev_dep_node_index) == Some(DepNodeColor::Green(dep_node_index)) {
820                     break;
821                 }
822             }
823         }
824     }
825
826     // Returns true if the given node has been marked as green during the
827     // current compilation session. Used in various assertions
828     pub fn is_green(&self, dep_node: &DepNode) -> bool {
829         self.node_color(dep_node).map(|c| c.is_green()).unwrap_or(false)
830     }
831
832     // This method loads all on-disk cacheable query results into memory, so
833     // they can be written out to the new cache file again. Most query results
834     // will already be in memory but in the case where we marked something as
835     // green but then did not need the value, that value will never have been
836     // loaded from disk.
837     //
838     // This method will only load queries that will end up in the disk cache.
839     // Other queries will not be executed.
840     pub fn exec_cache_promotions(&self, tcx: TyCtxt<'_>) {
841         let _prof_timer = tcx.prof.generic_activity("incr_comp_query_cache_promotion");
842
843         let data = self.data.as_ref().unwrap();
844         for prev_index in data.colors.values.indices() {
845             match data.colors.get(prev_index) {
846                 Some(DepNodeColor::Green(_)) => {
847                     let dep_node = data.previous.index_to_node(prev_index);
848                     dep_node.try_load_from_on_disk_cache(tcx);
849                 }
850                 None |
851                 Some(DepNodeColor::Red) => {
852                     // We can skip red nodes because a node can only be marked
853                     // as red if the query result was recomputed and thus is
854                     // already in memory.
855                 }
856             }
857         }
858     }
859 }
860
861 /// A "work product" is an intermediate result that we save into the
862 /// incremental directory for later re-use. The primary example are
863 /// the object files that we save for each partition at code
864 /// generation time.
865 ///
866 /// Each work product is associated with a dep-node, representing the
867 /// process that produced the work-product. If that dep-node is found
868 /// to be dirty when we load up, then we will delete the work-product
869 /// at load time. If the work-product is found to be clean, then we
870 /// will keep a record in the `previous_work_products` list.
871 ///
872 /// In addition, work products have an associated hash. This hash is
873 /// an extra hash that can be used to decide if the work-product from
874 /// a previous compilation can be re-used (in addition to the dirty
875 /// edges check).
876 ///
877 /// As the primary example, consider the object files we generate for
878 /// each partition. In the first run, we create partitions based on
879 /// the symbols that need to be compiled. For each partition P, we
880 /// hash the symbols in P and create a `WorkProduct` record associated
881 /// with `DepNode::CodegenUnit(P)`; the hash is the set of symbols
882 /// in P.
883 ///
884 /// The next time we compile, if the `DepNode::CodegenUnit(P)` is
885 /// judged to be clean (which means none of the things we read to
886 /// generate the partition were found to be dirty), it will be loaded
887 /// into previous work products. We will then regenerate the set of
888 /// symbols in the partition P and hash them (note that new symbols
889 /// may be added -- for example, new monomorphizations -- even if
890 /// nothing in P changed!). We will compare that hash against the
891 /// previous hash. If it matches up, we can reuse the object file.
892 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
893 pub struct WorkProduct {
894     pub cgu_name: String,
895     /// Saved files associated with this CGU.
896     pub saved_files: Vec<(WorkProductFileKind, String)>,
897 }
898
899 #[derive(Clone, Copy, Debug, RustcEncodable, RustcDecodable, PartialEq)]
900 pub enum WorkProductFileKind {
901     Object,
902     Bytecode,
903     BytecodeCompressed,
904 }
905
906 #[derive(Clone)]
907 struct DepNodeData {
908     node: DepNode,
909     edges: SmallVec<[DepNodeIndex; 8]>,
910     fingerprint: Fingerprint,
911 }
912
913 /// `CurrentDepGraph` stores the dependency graph for the current session.
914 /// It will be populated as we run queries or tasks.
915 ///
916 /// The nodes in it are identified by an index (`DepNodeIndex`).
917 /// The data for each node is stored in its `DepNodeData`, found in the `data` field.
918 ///
919 /// We never remove nodes from the graph: they are only added.
920 ///
921 /// This struct uses two locks internally. The `data` and `node_to_node_index` fields are
922 /// locked separately. Operations that take a `DepNodeIndex` typically just access
923 /// the data field.
924 ///
925 /// The only operation that must manipulate both locks is adding new nodes, in which case
926 /// we first acquire the `node_to_node_index` lock and then, once a new node is to be inserted,
927 /// acquire the lock on `data.`
928 pub(super) struct CurrentDepGraph {
929     data: Lock<IndexVec<DepNodeIndex, DepNodeData>>,
930     node_to_node_index: Lock<FxHashMap<DepNode, DepNodeIndex>>,
931
932     /// Used to trap when a specific edge is added to the graph.
933     /// This is used for debug purposes and is only active with `debug_assertions`.
934     #[allow(dead_code)]
935     forbidden_edge: Option<EdgeFilter>,
936
937     /// Anonymous `DepNode`s are nodes whose IDs we compute from the list of
938     /// their edges. This has the beneficial side-effect that multiple anonymous
939     /// nodes can be coalesced into one without changing the semantics of the
940     /// dependency graph. However, the merging of nodes can lead to a subtle
941     /// problem during red-green marking: The color of an anonymous node from
942     /// the current session might "shadow" the color of the node with the same
943     /// ID from the previous session. In order to side-step this problem, we make
944     /// sure that anonymous `NodeId`s allocated in different sessions don't overlap.
945     /// This is implemented by mixing a session-key into the ID fingerprint of
946     /// each anon node. The session-key is just a random number generated when
947     /// the `DepGraph` is created.
948     anon_id_seed: Fingerprint,
949
950     /// These are simple counters that are for profiling and
951     /// debugging and only active with `debug_assertions`.
952     total_read_count: AtomicU64,
953     total_duplicate_read_count: AtomicU64,
954 }
955
956 impl CurrentDepGraph {
957     fn new(prev_graph_node_count: usize) -> CurrentDepGraph {
958         use std::time::{SystemTime, UNIX_EPOCH};
959
960         let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
961         let nanos = duration.as_secs() * 1_000_000_000 +
962                     duration.subsec_nanos() as u64;
963         let mut stable_hasher = StableHasher::new();
964         nanos.hash(&mut stable_hasher);
965
966         let forbidden_edge = if cfg!(debug_assertions) {
967             match env::var("RUST_FORBID_DEP_GRAPH_EDGE") {
968                 Ok(s) => {
969                     match EdgeFilter::new(&s) {
970                         Ok(f) => Some(f),
971                         Err(err) => bug!("RUST_FORBID_DEP_GRAPH_EDGE invalid: {}", err),
972                     }
973                 }
974                 Err(_) => None,
975             }
976         } else {
977             None
978         };
979
980         // Pre-allocate the dep node structures. We over-allocate a little so
981         // that we hopefully don't have to re-allocate during this compilation
982         // session. The over-allocation is 2% plus a small constant to account
983         // for the fact that in very small crates 2% might not be enough.
984         let new_node_count_estimate = (prev_graph_node_count * 102) / 100 + 200;
985
986         CurrentDepGraph {
987             data: Lock::new(IndexVec::with_capacity(new_node_count_estimate)),
988             node_to_node_index: Lock::new(FxHashMap::with_capacity_and_hasher(
989                 new_node_count_estimate,
990                 Default::default(),
991             )),
992             anon_id_seed: stable_hasher.finish(),
993             forbidden_edge,
994             total_read_count: AtomicU64::new(0),
995             total_duplicate_read_count: AtomicU64::new(0),
996         }
997     }
998
999     fn complete_task(
1000         &self,
1001         node: DepNode,
1002         task_deps: TaskDeps,
1003         fingerprint: Fingerprint
1004     ) -> DepNodeIndex {
1005         self.alloc_node(node, task_deps.reads, fingerprint)
1006     }
1007
1008     fn complete_anon_task(&self, kind: DepKind, task_deps: TaskDeps) -> DepNodeIndex {
1009         debug_assert!(!kind.is_eval_always());
1010
1011         let mut hasher = StableHasher::new();
1012
1013         // The dep node indices are hashed here instead of hashing the dep nodes of the
1014         // dependencies. These indices may refer to different nodes per session, but this isn't
1015         // a problem here because we that ensure the final dep node hash is per session only by
1016         // combining it with the per session random number `anon_id_seed`. This hash only need
1017         // to map the dependencies to a single value on a per session basis.
1018         task_deps.reads.hash(&mut hasher);
1019
1020         let target_dep_node = DepNode {
1021             kind,
1022
1023             // Fingerprint::combine() is faster than sending Fingerprint
1024             // through the StableHasher (at least as long as StableHasher
1025             // is so slow).
1026             hash: self.anon_id_seed.combine(hasher.finish()),
1027         };
1028
1029         self.intern_node(target_dep_node, task_deps.reads, Fingerprint::ZERO)
1030     }
1031
1032     fn alloc_node(
1033         &self,
1034         dep_node: DepNode,
1035         edges: SmallVec<[DepNodeIndex; 8]>,
1036         fingerprint: Fingerprint
1037     ) -> DepNodeIndex {
1038         debug_assert!(!self.node_to_node_index.lock().contains_key(&dep_node));
1039         self.intern_node(dep_node, edges, fingerprint)
1040     }
1041
1042     fn intern_node(
1043         &self,
1044         dep_node: DepNode,
1045         edges: SmallVec<[DepNodeIndex; 8]>,
1046         fingerprint: Fingerprint
1047     ) -> DepNodeIndex {
1048         match self.node_to_node_index.lock().entry(dep_node) {
1049             Entry::Occupied(entry) => *entry.get(),
1050             Entry::Vacant(entry) => {
1051                 let mut data = self.data.lock();
1052                 let dep_node_index = DepNodeIndex::new(data.len());
1053                 data.push(DepNodeData {
1054                     node: dep_node,
1055                     edges,
1056                     fingerprint
1057                 });
1058                 entry.insert(dep_node_index);
1059                 dep_node_index
1060             }
1061         }
1062     }
1063 }
1064
1065 impl DepGraphData {
1066     fn read_index(&self, source: DepNodeIndex) {
1067         ty::tls::with_context_opt(|icx| {
1068             let icx = if let Some(icx) = icx { icx } else {  return };
1069             if let Some(task_deps) = icx.task_deps {
1070                 let mut task_deps = task_deps.lock();
1071                 if cfg!(debug_assertions) {
1072                     self.current.total_read_count.fetch_add(1, SeqCst);
1073                 }
1074                 if task_deps.read_set.insert(source) {
1075                     task_deps.reads.push(source);
1076
1077                     #[cfg(debug_assertions)]
1078                     {
1079                         if let Some(target) = task_deps.node {
1080                             let data = self.current.data.lock();
1081                             if let Some(ref forbidden_edge) = self.current.forbidden_edge {
1082                                 let source = data[source].node;
1083                                 if forbidden_edge.test(&source, &target) {
1084                                     bug!("forbidden edge {:?} -> {:?} created",
1085                                         source,
1086                                         target)
1087                                 }
1088                             }
1089                         }
1090                     }
1091                 } else if cfg!(debug_assertions) {
1092                     self.current.total_duplicate_read_count.fetch_add(1, SeqCst);
1093                 }
1094             }
1095         })
1096     }
1097 }
1098
1099 pub struct TaskDeps {
1100     #[cfg(debug_assertions)]
1101     node: Option<DepNode>,
1102     reads: SmallVec<[DepNodeIndex; 8]>,
1103     read_set: FxHashSet<DepNodeIndex>,
1104 }
1105
1106 // A data structure that stores Option<DepNodeColor> values as a contiguous
1107 // array, using one u32 per entry.
1108 struct DepNodeColorMap {
1109     values: IndexVec<SerializedDepNodeIndex, AtomicU32>,
1110 }
1111
1112 const COMPRESSED_NONE: u32 = 0;
1113 const COMPRESSED_RED: u32 = 1;
1114 const COMPRESSED_FIRST_GREEN: u32 = 2;
1115
1116 impl DepNodeColorMap {
1117     fn new(size: usize) -> DepNodeColorMap {
1118         DepNodeColorMap {
1119             values: (0..size).map(|_| AtomicU32::new(COMPRESSED_NONE)).collect(),
1120         }
1121     }
1122
1123     fn get(&self, index: SerializedDepNodeIndex) -> Option<DepNodeColor> {
1124         match self.values[index].load(Ordering::Acquire) {
1125             COMPRESSED_NONE => None,
1126             COMPRESSED_RED => Some(DepNodeColor::Red),
1127             value => Some(DepNodeColor::Green(DepNodeIndex::from_u32(
1128                 value - COMPRESSED_FIRST_GREEN
1129             )))
1130         }
1131     }
1132
1133     fn insert(&self, index: SerializedDepNodeIndex, color: DepNodeColor) {
1134         self.values[index].store(match color {
1135             DepNodeColor::Red => COMPRESSED_RED,
1136             DepNodeColor::Green(index) => index.as_u32() + COMPRESSED_FIRST_GREEN,
1137         }, Ordering::Release)
1138     }
1139 }