]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/graph.rs
26e1dc7e0490c21f826b4657e67d13c04c4c8e81
[rust.git] / src / librustc / dep_graph / graph.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use hir::def_id::DefId;
12 use rustc_data_structures::fx::FxHashMap;
13 use session::config::OutputType;
14 use std::cell::{Ref, RefCell};
15 use std::rc::Rc;
16 use std::sync::Arc;
17
18 use super::dep_node::{DepNode, WorkProductId};
19 use super::query::DepGraphQuery;
20 use super::raii;
21 use super::thread::{DepGraphThreadData, DepMessage};
22
23 #[derive(Clone)]
24 pub struct DepGraph {
25     data: Rc<DepGraphData>
26 }
27
28 struct DepGraphData {
29     /// We send messages to the thread to let it build up the dep-graph
30     /// from the current run.
31     thread: DepGraphThreadData,
32
33     /// When we load, there may be `.o` files, cached mir, or other such
34     /// things available to us. If we find that they are not dirty, we
35     /// load the path to the file storing those work-products here into
36     /// this map. We can later look for and extract that data.
37     previous_work_products: RefCell<FxHashMap<Arc<WorkProductId>, WorkProduct>>,
38
39     /// Work-products that we generate in this run.
40     work_products: RefCell<FxHashMap<Arc<WorkProductId>, WorkProduct>>,
41 }
42
43 impl DepGraph {
44     pub fn new(enabled: bool) -> DepGraph {
45         DepGraph {
46             data: Rc::new(DepGraphData {
47                 thread: DepGraphThreadData::new(enabled),
48                 previous_work_products: RefCell::new(FxHashMap()),
49                 work_products: RefCell::new(FxHashMap()),
50             })
51         }
52     }
53
54     /// True if we are actually building the full dep-graph.
55     #[inline]
56     pub fn is_fully_enabled(&self) -> bool {
57         self.data.thread.is_fully_enabled()
58     }
59
60     pub fn query(&self) -> DepGraphQuery<DefId> {
61         self.data.thread.query()
62     }
63
64     pub fn in_ignore<'graph>(&'graph self) -> Option<raii::IgnoreTask<'graph>> {
65         raii::IgnoreTask::new(&self.data.thread)
66     }
67
68     pub fn in_task<'graph>(&'graph self, key: DepNode<DefId>) -> Option<raii::DepTask<'graph>> {
69         raii::DepTask::new(&self.data.thread, key)
70     }
71
72     pub fn with_ignore<OP,R>(&self, op: OP) -> R
73         where OP: FnOnce() -> R
74     {
75         let _task = self.in_ignore();
76         op()
77     }
78
79     pub fn with_task<OP,R>(&self, key: DepNode<DefId>, op: OP) -> R
80         where OP: FnOnce() -> R
81     {
82         let _task = self.in_task(key);
83         op()
84     }
85
86     pub fn read(&self, v: DepNode<DefId>) {
87         if self.data.thread.is_enqueue_enabled() {
88             self.data.thread.enqueue(DepMessage::Read(v));
89         }
90     }
91
92     pub fn write(&self, v: DepNode<DefId>) {
93         if self.data.thread.is_enqueue_enabled() {
94             self.data.thread.enqueue(DepMessage::Write(v));
95         }
96     }
97
98     /// Indicates that a previous work product exists for `v`. This is
99     /// invoked during initial start-up based on what nodes are clean
100     /// (and what files exist in the incr. directory).
101     pub fn insert_previous_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
102         debug!("insert_previous_work_product({:?}, {:?})", v, data);
103         self.data.previous_work_products.borrow_mut()
104                                         .insert(v.clone(), data);
105     }
106
107     /// Indicates that we created the given work-product in this run
108     /// for `v`. This record will be preserved and loaded in the next
109     /// run.
110     pub fn insert_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
111         debug!("insert_work_product({:?}, {:?})", v, data);
112         self.data.work_products.borrow_mut()
113                                .insert(v.clone(), data);
114     }
115
116     /// Check whether a previous work product exists for `v` and, if
117     /// so, return the path that leads to it. Used to skip doing work.
118     pub fn previous_work_product(&self, v: &Arc<WorkProductId>) -> Option<WorkProduct> {
119         self.data.previous_work_products.borrow()
120                                         .get(v)
121                                         .cloned()
122     }
123
124     /// Access the map of work-products created during this run. Only
125     /// used during saving of the dep-graph.
126     pub fn work_products(&self) -> Ref<FxHashMap<Arc<WorkProductId>, WorkProduct>> {
127         self.data.work_products.borrow()
128     }
129 }
130
131 /// A "work product" is an intermediate result that we save into the
132 /// incremental directory for later re-use. The primary example are
133 /// the object files that we save for each partition at code
134 /// generation time.
135 ///
136 /// Each work product is associated with a dep-node, representing the
137 /// process that produced the work-product. If that dep-node is found
138 /// to be dirty when we load up, then we will delete the work-product
139 /// at load time. If the work-product is found to be clean, then we
140 /// will keep a record in the `previous_work_products` list.
141 ///
142 /// In addition, work products have an associated hash. This hash is
143 /// an extra hash that can be used to decide if the work-product from
144 /// a previous compilation can be re-used (in addition to the dirty
145 /// edges check).
146 ///
147 /// As the primary example, consider the object files we generate for
148 /// each partition. In the first run, we create partitions based on
149 /// the symbols that need to be compiled. For each partition P, we
150 /// hash the symbols in P and create a `WorkProduct` record associated
151 /// with `DepNode::TransPartition(P)`; the hash is the set of symbols
152 /// in P.
153 ///
154 /// The next time we compile, if the `DepNode::TransPartition(P)` is
155 /// judged to be clean (which means none of the things we read to
156 /// generate the partition were found to be dirty), it will be loaded
157 /// into previous work products. We will then regenerate the set of
158 /// symbols in the partition P and hash them (note that new symbols
159 /// may be added -- for example, new monomorphizations -- even if
160 /// nothing in P changed!). We will compare that hash against the
161 /// previous hash. If it matches up, we can reuse the object file.
162 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
163 pub struct WorkProduct {
164     /// Extra hash used to decide if work-product is still suitable;
165     /// note that this is *not* a hash of the work-product itself.
166     /// See documentation on `WorkProduct` type for an example.
167     pub input_hash: u64,
168
169     /// Saved files associated with this CGU
170     pub saved_files: Vec<(OutputType, String)>,
171 }