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