]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/graph.rs
Auto merge of #42264 - GuillaumeGomez:new-error-codes, r=Susurrus
[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::safe::DepGraphSafe;
22 use super::thread::{DepGraphThreadData, DepMessage};
23
24 #[derive(Clone)]
25 pub struct DepGraph {
26     data: Rc<DepGraphData>
27 }
28
29 struct DepGraphData {
30     /// We send messages to the thread to let it build up the dep-graph
31     /// from the current run.
32     thread: DepGraphThreadData,
33
34     /// When we load, there may be `.o` files, cached mir, or other such
35     /// things available to us. If we find that they are not dirty, we
36     /// load the path to the file storing those work-products here into
37     /// this map. We can later look for and extract that data.
38     previous_work_products: RefCell<FxHashMap<Arc<WorkProductId>, WorkProduct>>,
39
40     /// Work-products that we generate in this run.
41     work_products: RefCell<FxHashMap<Arc<WorkProductId>, WorkProduct>>,
42 }
43
44 impl DepGraph {
45     pub fn new(enabled: bool) -> DepGraph {
46         DepGraph {
47             data: Rc::new(DepGraphData {
48                 thread: DepGraphThreadData::new(enabled),
49                 previous_work_products: RefCell::new(FxHashMap()),
50                 work_products: RefCell::new(FxHashMap()),
51             })
52         }
53     }
54
55     /// True if we are actually building the full dep-graph.
56     #[inline]
57     pub fn is_fully_enabled(&self) -> bool {
58         self.data.thread.is_fully_enabled()
59     }
60
61     pub fn query(&self) -> DepGraphQuery<DefId> {
62         self.data.thread.query()
63     }
64
65     pub fn in_ignore<'graph>(&'graph self) -> Option<raii::IgnoreTask<'graph>> {
66         raii::IgnoreTask::new(&self.data.thread)
67     }
68
69     pub fn in_task<'graph>(&'graph self, key: DepNode<DefId>) -> Option<raii::DepTask<'graph>> {
70         raii::DepTask::new(&self.data.thread, key)
71     }
72
73     pub fn with_ignore<OP,R>(&self, op: OP) -> R
74         where OP: FnOnce() -> R
75     {
76         let _task = self.in_ignore();
77         op()
78     }
79
80     /// Starts a new dep-graph task. Dep-graph tasks are specified
81     /// using a free function (`task`) and **not** a closure -- this
82     /// is intentional because we want to exercise tight control over
83     /// what state they have access to. In particular, we want to
84     /// prevent implicit 'leaks' of tracked state into the task (which
85     /// could then be read without generating correct edges in the
86     /// dep-graph -- see the [README] for more details on the
87     /// dep-graph). To this end, the task function gets exactly two
88     /// pieces of state: the context `cx` and an argument `arg`. Both
89     /// of these bits of state must be of some type that implements
90     /// `DepGraphSafe` and hence does not leak.
91     ///
92     /// The choice of two arguments is not fundamental. One argument
93     /// would work just as well, since multiple values can be
94     /// collected using tuples. However, using two arguments works out
95     /// to be quite convenient, since it is common to need a context
96     /// (`cx`) and some argument (e.g., a `DefId` identifying what
97     /// item to process).
98     ///
99     /// For cases where you need some other number of arguments:
100     ///
101     /// - If you only need one argument, just use `()` for the `arg`
102     ///   parameter.
103     /// - If you need 3+ arguments, use a tuple for the
104     ///   `arg` parameter.
105     ///
106     /// [README]: README.md
107     pub fn with_task<C, A, R>(&self, key: DepNode<DefId>, cx: C, arg: A, task: fn(C, A) -> R) -> R
108         where C: DepGraphSafe, A: DepGraphSafe
109     {
110         let _task = self.in_task(key);
111         task(cx, arg)
112     }
113
114     pub fn read(&self, v: DepNode<DefId>) {
115         if self.data.thread.is_enqueue_enabled() {
116             self.data.thread.enqueue(DepMessage::Read(v));
117         }
118     }
119
120     /// Indicates that a previous work product exists for `v`. This is
121     /// invoked during initial start-up based on what nodes are clean
122     /// (and what files exist in the incr. directory).
123     pub fn insert_previous_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
124         debug!("insert_previous_work_product({:?}, {:?})", v, data);
125         self.data.previous_work_products.borrow_mut()
126                                         .insert(v.clone(), data);
127     }
128
129     /// Indicates that we created the given work-product in this run
130     /// for `v`. This record will be preserved and loaded in the next
131     /// run.
132     pub fn insert_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
133         debug!("insert_work_product({:?}, {:?})", v, data);
134         self.data.work_products.borrow_mut()
135                                .insert(v.clone(), data);
136     }
137
138     /// Check whether a previous work product exists for `v` and, if
139     /// so, return the path that leads to it. Used to skip doing work.
140     pub fn previous_work_product(&self, v: &Arc<WorkProductId>) -> Option<WorkProduct> {
141         self.data.previous_work_products.borrow()
142                                         .get(v)
143                                         .cloned()
144     }
145
146     /// Access the map of work-products created during this run. Only
147     /// used during saving of the dep-graph.
148     pub fn work_products(&self) -> Ref<FxHashMap<Arc<WorkProductId>, WorkProduct>> {
149         self.data.work_products.borrow()
150     }
151
152     /// Access the map of work-products created during the cached run. Only
153     /// used during saving of the dep-graph.
154     pub fn previous_work_products(&self) -> Ref<FxHashMap<Arc<WorkProductId>, WorkProduct>> {
155         self.data.previous_work_products.borrow()
156     }
157 }
158
159 /// A "work product" is an intermediate result that we save into the
160 /// incremental directory for later re-use. The primary example are
161 /// the object files that we save for each partition at code
162 /// generation time.
163 ///
164 /// Each work product is associated with a dep-node, representing the
165 /// process that produced the work-product. If that dep-node is found
166 /// to be dirty when we load up, then we will delete the work-product
167 /// at load time. If the work-product is found to be clean, then we
168 /// will keep a record in the `previous_work_products` list.
169 ///
170 /// In addition, work products have an associated hash. This hash is
171 /// an extra hash that can be used to decide if the work-product from
172 /// a previous compilation can be re-used (in addition to the dirty
173 /// edges check).
174 ///
175 /// As the primary example, consider the object files we generate for
176 /// each partition. In the first run, we create partitions based on
177 /// the symbols that need to be compiled. For each partition P, we
178 /// hash the symbols in P and create a `WorkProduct` record associated
179 /// with `DepNode::TransPartition(P)`; the hash is the set of symbols
180 /// in P.
181 ///
182 /// The next time we compile, if the `DepNode::TransPartition(P)` is
183 /// judged to be clean (which means none of the things we read to
184 /// generate the partition were found to be dirty), it will be loaded
185 /// into previous work products. We will then regenerate the set of
186 /// symbols in the partition P and hash them (note that new symbols
187 /// may be added -- for example, new monomorphizations -- even if
188 /// nothing in P changed!). We will compare that hash against the
189 /// previous hash. If it matches up, we can reuse the object file.
190 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
191 pub struct WorkProduct {
192     /// Extra hash used to decide if work-product is still suitable;
193     /// note that this is *not* a hash of the work-product itself.
194     /// See documentation on `WorkProduct` type for an example.
195     pub input_hash: u64,
196
197     /// Saved files associated with this CGU
198     pub saved_files: Vec<(OutputType, String)>,
199 }