]>
Commit | Line | Data |
---|---|---|
54a0048b SL |
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; | |
5bcae85e SL |
12 | use rustc_data_structures::fnv::FnvHashMap; |
13 | use session::config::OutputType; | |
14 | use std::cell::{Ref, RefCell}; | |
54a0048b | 15 | use std::rc::Rc; |
5bcae85e | 16 | use std::sync::Arc; |
54a0048b | 17 | |
5bcae85e | 18 | use super::dep_node::{DepNode, WorkProductId}; |
54a0048b SL |
19 | use super::query::DepGraphQuery; |
20 | use super::raii; | |
21 | use super::thread::{DepGraphThreadData, DepMessage}; | |
22 | ||
23 | #[derive(Clone)] | |
24 | pub struct DepGraph { | |
5bcae85e SL |
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<FnvHashMap<Arc<WorkProductId>, WorkProduct>>, | |
38 | ||
39 | /// Work-products that we generate in this run. | |
40 | work_products: RefCell<FnvHashMap<Arc<WorkProductId>, WorkProduct>>, | |
54a0048b SL |
41 | } |
42 | ||
43 | impl DepGraph { | |
44 | pub fn new(enabled: bool) -> DepGraph { | |
45 | DepGraph { | |
5bcae85e SL |
46 | data: Rc::new(DepGraphData { |
47 | thread: DepGraphThreadData::new(enabled), | |
48 | previous_work_products: RefCell::new(FnvHashMap()), | |
9e0c209e | 49 | work_products: RefCell::new(FnvHashMap()), |
5bcae85e | 50 | }) |
54a0048b SL |
51 | } |
52 | } | |
53 | ||
54 | /// True if we are actually building a dep-graph. If this returns false, | |
55 | /// then the other methods on this `DepGraph` will have no net effect. | |
56 | #[inline] | |
57 | pub fn enabled(&self) -> bool { | |
5bcae85e | 58 | self.data.thread.enabled() |
54a0048b SL |
59 | } |
60 | ||
61 | pub fn query(&self) -> DepGraphQuery<DefId> { | |
5bcae85e | 62 | self.data.thread.query() |
54a0048b SL |
63 | } |
64 | ||
65 | pub fn in_ignore<'graph>(&'graph self) -> raii::IgnoreTask<'graph> { | |
5bcae85e | 66 | raii::IgnoreTask::new(&self.data.thread) |
54a0048b SL |
67 | } |
68 | ||
69 | pub fn in_task<'graph>(&'graph self, key: DepNode<DefId>) -> raii::DepTask<'graph> { | |
5bcae85e | 70 | raii::DepTask::new(&self.data.thread, key) |
54a0048b SL |
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 | pub fn with_task<OP,R>(&self, key: DepNode<DefId>, op: OP) -> R | |
81 | where OP: FnOnce() -> R | |
82 | { | |
83 | let _task = self.in_task(key); | |
84 | op() | |
85 | } | |
86 | ||
87 | pub fn read(&self, v: DepNode<DefId>) { | |
5bcae85e | 88 | self.data.thread.enqueue(DepMessage::Read(v)); |
54a0048b SL |
89 | } |
90 | ||
91 | pub fn write(&self, v: DepNode<DefId>) { | |
5bcae85e SL |
92 | self.data.thread.enqueue(DepMessage::Write(v)); |
93 | } | |
94 | ||
95 | /// Indicates that a previous work product exists for `v`. This is | |
96 | /// invoked during initial start-up based on what nodes are clean | |
97 | /// (and what files exist in the incr. directory). | |
98 | pub fn insert_previous_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) { | |
99 | debug!("insert_previous_work_product({:?}, {:?})", v, data); | |
100 | self.data.previous_work_products.borrow_mut() | |
101 | .insert(v.clone(), data); | |
102 | } | |
103 | ||
104 | /// Indicates that we created the given work-product in this run | |
105 | /// for `v`. This record will be preserved and loaded in the next | |
106 | /// run. | |
107 | pub fn insert_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) { | |
108 | debug!("insert_work_product({:?}, {:?})", v, data); | |
109 | self.data.work_products.borrow_mut() | |
110 | .insert(v.clone(), data); | |
54a0048b | 111 | } |
5bcae85e SL |
112 | |
113 | /// Check whether a previous work product exists for `v` and, if | |
114 | /// so, return the path that leads to it. Used to skip doing work. | |
115 | pub fn previous_work_product(&self, v: &Arc<WorkProductId>) -> Option<WorkProduct> { | |
116 | self.data.previous_work_products.borrow() | |
117 | .get(v) | |
118 | .cloned() | |
119 | } | |
120 | ||
121 | /// Access the map of work-products created during this run. Only | |
122 | /// used during saving of the dep-graph. | |
123 | pub fn work_products(&self) -> Ref<FnvHashMap<Arc<WorkProductId>, WorkProduct>> { | |
124 | self.data.work_products.borrow() | |
125 | } | |
126 | } | |
127 | ||
128 | /// A "work product" is an intermediate result that we save into the | |
129 | /// incremental directory for later re-use. The primary example are | |
130 | /// the object files that we save for each partition at code | |
131 | /// generation time. | |
132 | /// | |
133 | /// Each work product is associated with a dep-node, representing the | |
134 | /// process that produced the work-product. If that dep-node is found | |
135 | /// to be dirty when we load up, then we will delete the work-product | |
136 | /// at load time. If the work-product is found to be clean, then we | |
137 | /// will keep a record in the `previous_work_products` list. | |
138 | /// | |
139 | /// In addition, work products have an associated hash. This hash is | |
140 | /// an extra hash that can be used to decide if the work-product from | |
141 | /// a previous compilation can be re-used (in addition to the dirty | |
142 | /// edges check). | |
143 | /// | |
144 | /// As the primary example, consider the object files we generate for | |
145 | /// each partition. In the first run, we create partitions based on | |
146 | /// the symbols that need to be compiled. For each partition P, we | |
147 | /// hash the symbols in P and create a `WorkProduct` record associated | |
148 | /// with `DepNode::TransPartition(P)`; the hash is the set of symbols | |
149 | /// in P. | |
150 | /// | |
151 | /// The next time we compile, if the `DepNode::TransPartition(P)` is | |
152 | /// judged to be clean (which means none of the things we read to | |
153 | /// generate the partition were found to be dirty), it will be loaded | |
154 | /// into previous work products. We will then regenerate the set of | |
155 | /// symbols in the partition P and hash them (note that new symbols | |
156 | /// may be added -- for example, new monomorphizations -- even if | |
157 | /// nothing in P changed!). We will compare that hash against the | |
158 | /// previous hash. If it matches up, we can reuse the object file. | |
159 | #[derive(Clone, Debug, RustcEncodable, RustcDecodable)] | |
160 | pub struct WorkProduct { | |
161 | /// Extra hash used to decide if work-product is still suitable; | |
162 | /// note that this is *not* a hash of the work-product itself. | |
163 | /// See documentation on `WorkProduct` type for an example. | |
164 | pub input_hash: u64, | |
165 | ||
166 | /// Saved files associated with this CGU | |
167 | pub saved_files: Vec<(OutputType, String)>, | |
54a0048b | 168 | } |