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;
10 use std::collections::hash_map::Entry;
12 use crate::ty::{self, TyCtxt};
13 use parking_lot::{Mutex, Condvar};
15 use crate::ich::{StableHashingContext, StableHashingContextProvider, Fingerprint};
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;
26 data: Option<Lrc<DepGraphData>>,
29 rustc_index::newtype_index! {
30 pub struct DepNodeIndex { .. }
34 pub const INVALID: DepNodeIndex = DepNodeIndex::MAX;
37 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
38 pub enum DepNodeColor {
44 pub fn is_green(self) -> bool {
46 DepNodeColor::Red => false,
47 DepNodeColor::Green(_) => true,
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,
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,
63 colors: DepNodeColorMap,
65 /// A set of loaded diagnostics that is in the progress of being emitted.
66 emitting_diagnostics: Mutex<FxHashSet<DepNodeIndex>>,
68 /// Used to wait for diagnostics to be emitted.
69 emitting_diagnostics_cond_var: Condvar,
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>,
77 dep_node_debug: Lock<FxHashMap<DepNode, String>>,
80 pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Option<Fingerprint>
82 R: for<'a> HashStable<StableHashingContext<'a>>,
84 let mut stable_hasher = StableHasher::new();
85 result.hash_stable(hcx, &mut stable_hasher);
87 Some(stable_hasher.finish())
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();
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),
108 pub fn new_disabled() -> DepGraph {
114 /// Returns `true` if we are actually building the full dep-graph, and `false` otherwise.
116 pub fn is_fully_enabled(&self) -> bool {
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));
131 DepGraphQuery::new(&nodes[..], &edges[..])
134 pub fn assert_ignored(&self)
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");
144 pub fn with_ignore<OP,R>(&self, op: OP) -> R
145 where OP: FnOnce() -> R
147 ty::tls::with_context(|icx| {
148 let icx = ty::tls::ImplicitCtxt {
153 ty::tls::enter_context(&icx, |_| {
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.
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).
178 /// For cases where you need some other number of arguments:
180 /// - If you only need one argument, just use `()` for the `arg`
182 /// - If you need 3+ arguments, use a tuple for the
185 /// [rustc guide]: https://rust-lang.github.io/rustc-guide/incremental-compilation.html
186 pub fn with_task<'a, C, A, R>(
192 hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
193 ) -> (R, DepNodeIndex)
195 C: DepGraphSafe + StableHashingContextProvider<'a>,
197 self.with_task_impl(key, cx, arg, false, task,
198 |_key| Some(TaskDeps {
199 #[cfg(debug_assertions)]
201 reads: SmallVec::new(),
202 read_set: Default::default(),
204 |data, key, fingerprint, task| {
205 data.complete_task(key, task.unwrap(), fingerprint)
210 /// Creates a new dep-graph input with value `input`
211 pub fn input_task<'a, C, R>(&self,
216 where C: DepGraphSafe + StableHashingContextProvider<'a>,
217 R: for<'b> HashStable<StableHashingContext<'b>>,
219 fn identity_fn<C, A>(_: C, arg: A) -> A {
223 self.with_task_impl(key, cx, input, true, identity_fn,
225 |data, key, fingerprint, _| {
226 data.alloc_node(key, SmallVec::new(), fingerprint)
231 fn with_task_impl<'a, C, A, R>(
238 create_task: fn(DepNode) -> Option<TaskDeps>,
239 finish_task_and_alloc_depnode: fn(&CurrentDepGraph,
242 Option<TaskDeps>) -> DepNodeIndex,
243 hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
244 ) -> (R, DepNodeIndex)
246 C: DepGraphSafe + StableHashingContextProvider<'a>,
248 if let Some(ref data) = self.data {
249 let task_deps = create_task(key).map(|deps| Lock::new(deps));
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
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();
258 let result = if no_tcx {
261 ty::tls::with_context(|icx| {
262 let icx = ty::tls::ImplicitCtxt {
263 task_deps: task_deps.as_ref(),
267 ty::tls::enter_context(&icx, |_| {
273 let current_fingerprint = hash_result(&mut hcx, &result);
275 let dep_node_index = finish_task_and_alloc_depnode(
278 current_fingerprint.unwrap_or(Fingerprint::ZERO),
279 task_deps.map(|lock| lock.into_inner()),
282 let print_status = cfg!(debug_assertions) && hcx.sess().opts.debugging_opts.dep_tasks;
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);
288 let color = if let Some(current_fingerprint) = current_fingerprint {
289 if current_fingerprint == prev_fingerprint {
291 eprintln!("[task::green] {:?}", key);
293 DepNodeColor::Green(dep_node_index)
296 eprintln!("[task::red] {:?}", key);
302 eprintln!("[task::unknown] {:?}", key);
304 // Mark the node as Red if we can't hash the result
308 debug_assert!(data.colors.get(prev_index).is_none(),
309 "DepGraph::with_task() - Duplicate DepNodeColor \
310 insertion for {:?}", key);
312 data.colors.insert(prev_index, color);
315 eprintln!("[task::new] {:?}", key);
319 (result, dep_node_index)
321 (task(cx, arg), DepNodeIndex::INVALID)
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
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)]
335 reads: SmallVec::new(),
336 read_set: Default::default(),
340 let icx = ty::tls::ImplicitCtxt {
341 task_deps: Some(&task_deps),
345 ty::tls::enter_context(&icx, |_| {
350 (r, task_deps.into_inner())
352 let dep_node_index = data.current
353 .complete_anon_task(dep_kind, task_deps);
354 (result, dep_node_index)
356 (op(), DepNodeIndex::INVALID)
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>(
368 hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
369 ) -> (R, DepNodeIndex)
371 C: DepGraphSafe + StableHashingContextProvider<'a>,
373 self.with_task_impl(key, cx, arg, false, task,
375 |data, key, fingerprint, _| {
376 data.alloc_node(key, smallvec![], fingerprint)
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() {
387 data.read_index(dep_node_index);
389 bug!("DepKind {:?} should be pre-allocated but isn't.", v.kind)
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);
402 pub fn dep_node_index_of(&self, dep_node: &DepNode) -> DepNodeIndex {
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)
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
429 pub fn prev_fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
430 self.data.as_ref().unwrap().previous.fingerprint_of(dep_node)
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)
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> {
444 data.previous_work_products.get(v).cloned()
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
455 pub fn register_dep_node_debug_str<F>(&self,
458 where F: FnOnce() -> String
460 let dep_node_debug = &self.data.as_ref().unwrap().dep_node_debug;
462 if dep_node_debug.borrow().contains_key(&dep_node) {
465 let debug_str = debug_str_gen();
466 dep_node_debug.borrow_mut().insert(dep_node, debug_str);
469 pub(super) fn dep_node_debug_str(&self, dep_node: DepNode) -> Option<String> {
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;
482 Some((current_dep_graph.total_read_count.load(SeqCst),
483 current_dep_graph.total_duplicate_read_count.load(SeqCst)))
489 pub fn serialize(&self) -> SerializedDepGraph {
490 let data = self.data.as_ref().unwrap().current.data.lock();
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();
497 let total_edge_count: usize = data.iter().map(|d| d.edges.len()).sum();
499 let mut edge_list_indices = IndexVec::with_capacity(nodes.len());
500 let mut edge_list_data = Vec::with_capacity(total_edge_count);
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;
508 debug_assert_eq!(current_dep_node_index.index(), edge_list_indices.len());
509 edge_list_indices.push((start, end));
512 debug_assert!(edge_list_data.len() <= ::std::u32::MAX as usize);
513 debug_assert_eq!(edge_list_data.len(), total_edge_count);
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)
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)
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(
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)
553 pub fn try_mark_green(
557 ) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> {
558 debug_assert!(!dep_node.kind.is_eval_always());
560 // Return None if the dep graph is disabled
561 let data = self.data.as_ref()?;
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)?;
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,
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(
579 ).map(|dep_node_index| {
580 (prev_index, dep_node_index)
586 /// Try to mark a dep-node which existed in the previous compilation session as green.
587 fn try_mark_previous_green<'tcx>(
591 prev_dep_node_index: SerializedDepNodeIndex,
593 ) -> Option<DepNodeIndex> {
594 debug!("try_mark_previous_green({:?}) - BEGIN", dep_node);
596 #[cfg(not(parallel_compiler))]
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());
602 // We never try to mark eval_always nodes as green
603 debug_assert!(!dep_node.kind.is_eval_always());
605 debug_assert_eq!(data.previous.index_to_node(prev_dep_node_index), *dep_node);
607 let prev_deps = data.previous.edge_targets_from(prev_dep_node_index);
609 let mut current_deps = SmallVec::new();
611 for &dep_dep_node_index in prev_deps {
612 let dep_dep_node_color = data.colors.get(dep_dep_node_index);
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
619 debug!("try_mark_previous_green({:?}) --- found dependency {:?} to \
620 be immediately green",
622 data.previous.index_to_node(dep_dep_node_index));
623 current_deps.push(node_index);
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 \
633 data.previous.index_to_node(dep_dep_node_index));
637 let dep_dep_node = &data.previous.index_to_node(dep_dep_node_index);
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,
646 let node_index = self.try_mark_previous_green(
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);
659 match dep_dep_node.kind {
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.
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.",
676 // For other kinds of nodes it's OK to be
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);
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);
695 Some(DepNodeColor::Red) => {
696 debug!("try_mark_previous_green({:?}) - END - \
697 dependency {:?} was red after forcing",
703 if !tcx.sess.has_errors() {
704 bug!("try_mark_previous_green() - Forcing the DepNode \
705 should have set its color")
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.
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);
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.
728 // There may be multiple threads trying to mark the same dep node green concurrently
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);
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)
740 // ... emitting any stored diagnostic ...
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);
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);
752 if unlikely!(diagnostics.len() > 0) {
753 self.emit_diagnostics(
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));
766 debug!("try_mark_previous_green({:?}) - END - successfully marked as green", dep_node);
770 /// Atomically emits some loaded diagnostics.
771 /// This may be called concurrently on multiple threads for the same dep node.
774 fn emit_diagnostics<'tcx>(
778 dep_node_index: DepNodeIndex,
779 prev_dep_node_index: SerializedDepNodeIndex,
780 diagnostics: Vec<Diagnostic>,
782 let mut emitting = data.emitting_diagnostics.lock();
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
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
795 // Promote the previous diagnostics to the current session.
796 tcx.queries.on_disk_cache
797 .store_diagnostics(dep_node_index, diagnostics.clone().into());
799 let handle = tcx.sess.diagnostic();
801 for diagnostic in diagnostics {
802 handle.emit_diagnostic(&diagnostic);
805 // Mark the node as green now that diagnostics are emitted
806 data.colors.insert(prev_dep_node_index, DepNodeColor::Green(dep_node_index));
808 // Remove the node from the set
809 data.emitting_diagnostics.lock().remove(&dep_node_index);
812 data.emitting_diagnostics_cond_var.notify_all();
814 // We must wait for the other thread to finish emitting the diagnostic
817 data.emitting_diagnostics_cond_var.wait(&mut emitting);
819 .get(prev_dep_node_index) == Some(DepNodeColor::Green(dep_node_index)) {
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)
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
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");
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);
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.
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
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.
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
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
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)>,
899 #[derive(Clone, Copy, Debug, RustcEncodable, RustcDecodable, PartialEq)]
900 pub enum WorkProductFileKind {
909 edges: SmallVec<[DepNodeIndex; 8]>,
910 fingerprint: Fingerprint,
913 /// `CurrentDepGraph` stores the dependency graph for the current session.
914 /// It will be populated as we run queries or tasks.
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.
919 /// We never remove nodes from the graph: they are only added.
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
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>>,
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`.
935 forbidden_edge: Option<EdgeFilter>,
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,
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,
956 impl CurrentDepGraph {
957 fn new(prev_graph_node_count: usize) -> CurrentDepGraph {
958 use std::time::{SystemTime, UNIX_EPOCH};
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);
966 let forbidden_edge = if cfg!(debug_assertions) {
967 match env::var("RUST_FORBID_DEP_GRAPH_EDGE") {
969 match EdgeFilter::new(&s) {
971 Err(err) => bug!("RUST_FORBID_DEP_GRAPH_EDGE invalid: {}", err),
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;
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,
992 anon_id_seed: stable_hasher.finish(),
994 total_read_count: AtomicU64::new(0),
995 total_duplicate_read_count: AtomicU64::new(0),
1002 task_deps: TaskDeps,
1003 fingerprint: Fingerprint
1005 self.alloc_node(node, task_deps.reads, fingerprint)
1008 fn complete_anon_task(&self, kind: DepKind, task_deps: TaskDeps) -> DepNodeIndex {
1009 debug_assert!(!kind.is_eval_always());
1011 let mut hasher = StableHasher::new();
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);
1020 let target_dep_node = DepNode {
1023 // Fingerprint::combine() is faster than sending Fingerprint
1024 // through the StableHasher (at least as long as StableHasher
1026 hash: self.anon_id_seed.combine(hasher.finish()),
1029 self.intern_node(target_dep_node, task_deps.reads, Fingerprint::ZERO)
1035 edges: SmallVec<[DepNodeIndex; 8]>,
1036 fingerprint: Fingerprint
1038 debug_assert!(!self.node_to_node_index.lock().contains_key(&dep_node));
1039 self.intern_node(dep_node, edges, fingerprint)
1045 edges: SmallVec<[DepNodeIndex; 8]>,
1046 fingerprint: Fingerprint
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 {
1058 entry.insert(dep_node_index);
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);
1074 if task_deps.read_set.insert(source) {
1075 task_deps.reads.push(source);
1077 #[cfg(debug_assertions)]
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",
1091 } else if cfg!(debug_assertions) {
1092 self.current.total_duplicate_read_count.fetch_add(1, SeqCst);
1099 pub struct TaskDeps {
1100 #[cfg(debug_assertions)]
1101 node: Option<DepNode>,
1102 reads: SmallVec<[DepNodeIndex; 8]>,
1103 read_set: FxHashSet<DepNodeIndex>,
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>,
1112 const COMPRESSED_NONE: u32 = 0;
1113 const COMPRESSED_RED: u32 = 1;
1114 const COMPRESSED_FIRST_GREEN: u32 = 2;
1116 impl DepNodeColorMap {
1117 fn new(size: usize) -> DepNodeColorMap {
1119 values: (0..size).map(|_| AtomicU32::new(COMPRESSED_NONE)).collect(),
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
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)