]> git.proxmox.com Git - rustc.git/blob - src/librustc/dep_graph/graph.rs
New upstream version 1.17.0+dfsg1
[rustc.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 pub fn write(&self, v: DepNode<DefId>) {
121 if self.data.thread.is_enqueue_enabled() {
122 self.data.thread.enqueue(DepMessage::Write(v));
123 }
124 }
125
126 /// Indicates that a previous work product exists for `v`. This is
127 /// invoked during initial start-up based on what nodes are clean
128 /// (and what files exist in the incr. directory).
129 pub fn insert_previous_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
130 debug!("insert_previous_work_product({:?}, {:?})", v, data);
131 self.data.previous_work_products.borrow_mut()
132 .insert(v.clone(), data);
133 }
134
135 /// Indicates that we created the given work-product in this run
136 /// for `v`. This record will be preserved and loaded in the next
137 /// run.
138 pub fn insert_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
139 debug!("insert_work_product({:?}, {:?})", v, data);
140 self.data.work_products.borrow_mut()
141 .insert(v.clone(), data);
142 }
143
144 /// Check whether a previous work product exists for `v` and, if
145 /// so, return the path that leads to it. Used to skip doing work.
146 pub fn previous_work_product(&self, v: &Arc<WorkProductId>) -> Option<WorkProduct> {
147 self.data.previous_work_products.borrow()
148 .get(v)
149 .cloned()
150 }
151
152 /// Access the map of work-products created during this run. Only
153 /// used during saving of the dep-graph.
154 pub fn work_products(&self) -> Ref<FxHashMap<Arc<WorkProductId>, WorkProduct>> {
155 self.data.work_products.borrow()
156 }
157
158 /// Access the map of work-products created during the cached run. Only
159 /// used during saving of the dep-graph.
160 pub fn previous_work_products(&self) -> Ref<FxHashMap<Arc<WorkProductId>, WorkProduct>> {
161 self.data.previous_work_products.borrow()
162 }
163 }
164
165 /// A "work product" is an intermediate result that we save into the
166 /// incremental directory for later re-use. The primary example are
167 /// the object files that we save for each partition at code
168 /// generation time.
169 ///
170 /// Each work product is associated with a dep-node, representing the
171 /// process that produced the work-product. If that dep-node is found
172 /// to be dirty when we load up, then we will delete the work-product
173 /// at load time. If the work-product is found to be clean, then we
174 /// will keep a record in the `previous_work_products` list.
175 ///
176 /// In addition, work products have an associated hash. This hash is
177 /// an extra hash that can be used to decide if the work-product from
178 /// a previous compilation can be re-used (in addition to the dirty
179 /// edges check).
180 ///
181 /// As the primary example, consider the object files we generate for
182 /// each partition. In the first run, we create partitions based on
183 /// the symbols that need to be compiled. For each partition P, we
184 /// hash the symbols in P and create a `WorkProduct` record associated
185 /// with `DepNode::TransPartition(P)`; the hash is the set of symbols
186 /// in P.
187 ///
188 /// The next time we compile, if the `DepNode::TransPartition(P)` is
189 /// judged to be clean (which means none of the things we read to
190 /// generate the partition were found to be dirty), it will be loaded
191 /// into previous work products. We will then regenerate the set of
192 /// symbols in the partition P and hash them (note that new symbols
193 /// may be added -- for example, new monomorphizations -- even if
194 /// nothing in P changed!). We will compare that hash against the
195 /// previous hash. If it matches up, we can reuse the object file.
196 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
197 pub struct WorkProduct {
198 /// Extra hash used to decide if work-product is still suitable;
199 /// note that this is *not* a hash of the work-product itself.
200 /// See documentation on `WorkProduct` type for an example.
201 pub input_hash: u64,
202
203 /// Saved files associated with this CGU
204 pub saved_files: Vec<(OutputType, String)>,
205 }