2016-03-28 21:37:34 +00:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-11-08 03:02:55 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2017-09-07 14:11:58 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
|
|
|
|
StableHashingContextProvider};
|
2016-07-25 14:51:14 +00:00
|
|
|
use session::config::OutputType;
|
2016-07-21 16:33:23 +00:00
|
|
|
use std::cell::{Ref, RefCell};
|
2016-03-28 21:37:34 +00:00
|
|
|
use std::rc::Rc;
|
2017-07-23 16:02:07 +00:00
|
|
|
use util::common::{ProfileQueriesMsg, profq_msg};
|
2016-03-28 21:37:34 +00:00
|
|
|
|
2017-09-07 14:11:58 +00:00
|
|
|
use ich::Fingerprint;
|
|
|
|
|
2017-06-23 14:37:12 +00:00
|
|
|
use super::dep_node::{DepNode, DepKind, WorkProductId};
|
2016-03-28 21:37:34 +00:00
|
|
|
use super::query::DepGraphQuery;
|
|
|
|
use super::raii;
|
isolate dep-graph tasks
A task function is now given as a `fn` pointer to ensure that it carries
no state. Each fn can take two arguments, because that worked out to be
convenient -- these two arguments must be of some type that is
`DepGraphSafe`, a new trait that is intended to prevent "leaking"
information into the task that was derived from tracked state.
This intentionally leaves `DepGraph::in_task()`, the more common form,
alone. Eventually all uses of `DepGraph::in_task()` should be ported
to `with_task()`, but I wanted to start with a smaller subset.
Originally I wanted to use closures bound by an auto trait, but that
approach has some limitations:
- the trait cannot have a `read()` method; since the current method
is unused, that may not be a problem.
- more importantly, we would want the auto trait to be "undefined" for all types
*by default* -- that is, this use case doesn't really fit the typical
auto trait scenario. For example, imagine that there is a `u32` loaded
out of a `hir::Node` -- we don't really want to be passing that
`u32` into the task!
2017-03-06 20:35:34 +00:00
|
|
|
use super::safe::DepGraphSafe;
|
2017-07-04 15:33:43 +00:00
|
|
|
use super::edges::{DepGraphEdges, DepNodeIndex};
|
2016-03-28 21:37:34 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DepGraph {
|
2017-09-14 15:43:03 +00:00
|
|
|
data: Option<Rc<DepGraphData>>,
|
|
|
|
fingerprints: Rc<RefCell<FxHashMap<DepNode, Fingerprint>>>
|
2016-07-21 16:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DepGraphData {
|
2017-07-04 13:06:57 +00:00
|
|
|
/// The actual graph data.
|
|
|
|
edges: RefCell<DepGraphEdges>,
|
2016-07-21 16:33:23 +00:00
|
|
|
|
2016-07-22 14:39:30 +00:00
|
|
|
/// When we load, there may be `.o` files, cached mir, or other such
|
2016-07-21 16:33:23 +00:00
|
|
|
/// things available to us. If we find that they are not dirty, we
|
|
|
|
/// load the path to the file storing those work-products here into
|
|
|
|
/// this map. We can later look for and extract that data.
|
2017-06-06 13:09:21 +00:00
|
|
|
previous_work_products: RefCell<FxHashMap<WorkProductId, WorkProduct>>,
|
2016-07-21 16:33:23 +00:00
|
|
|
|
2016-07-22 14:39:30 +00:00
|
|
|
/// Work-products that we generate in this run.
|
2017-06-06 13:09:21 +00:00
|
|
|
work_products: RefCell<FxHashMap<WorkProductId, WorkProduct>>,
|
2017-06-12 15:00:55 +00:00
|
|
|
|
|
|
|
dep_node_debug: RefCell<FxHashMap<DepNode, String>>,
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DepGraph {
|
|
|
|
pub fn new(enabled: bool) -> DepGraph {
|
|
|
|
DepGraph {
|
2017-07-04 13:06:57 +00:00
|
|
|
data: if enabled {
|
|
|
|
Some(Rc::new(DepGraphData {
|
|
|
|
previous_work_products: RefCell::new(FxHashMap()),
|
|
|
|
work_products: RefCell::new(FxHashMap()),
|
|
|
|
edges: RefCell::new(DepGraphEdges::new()),
|
|
|
|
dep_node_debug: RefCell::new(FxHashMap()),
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
None
|
2017-09-14 15:43:03 +00:00
|
|
|
},
|
|
|
|
fingerprints: Rc::new(RefCell::new(FxHashMap())),
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 20:46:11 +00:00
|
|
|
/// True if we are actually building the full dep-graph.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_fully_enabled(&self) -> bool {
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data.is_some()
|
2016-12-20 20:46:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 15:36:30 +00:00
|
|
|
pub fn query(&self) -> DepGraphQuery {
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data.as_ref().unwrap().edges.borrow().query()
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 03:46:41 +00:00
|
|
|
pub fn in_ignore<'graph>(&'graph self) -> Option<raii::IgnoreTask<'graph>> {
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data.as_ref().map(|data| raii::IgnoreTask::new(&data.edges))
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_ignore<OP,R>(&self, op: OP) -> R
|
|
|
|
where OP: FnOnce() -> R
|
|
|
|
{
|
|
|
|
let _task = self.in_ignore();
|
|
|
|
op()
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:14:27 +00:00
|
|
|
/// Starts a new dep-graph task. Dep-graph tasks are specified
|
|
|
|
/// using a free function (`task`) and **not** a closure -- this
|
|
|
|
/// is intentional because we want to exercise tight control over
|
|
|
|
/// what state they have access to. In particular, we want to
|
|
|
|
/// prevent implicit 'leaks' of tracked state into the task (which
|
|
|
|
/// could then be read without generating correct edges in the
|
|
|
|
/// dep-graph -- see the [README] for more details on the
|
|
|
|
/// dep-graph). To this end, the task function gets exactly two
|
|
|
|
/// pieces of state: the context `cx` and an argument `arg`. Both
|
|
|
|
/// of these bits of state must be of some type that implements
|
|
|
|
/// `DepGraphSafe` and hence does not leak.
|
|
|
|
///
|
|
|
|
/// The choice of two arguments is not fundamental. One argument
|
|
|
|
/// would work just as well, since multiple values can be
|
|
|
|
/// collected using tuples. However, using two arguments works out
|
|
|
|
/// to be quite convenient, since it is common to need a context
|
|
|
|
/// (`cx`) and some argument (e.g., a `DefId` identifying what
|
|
|
|
/// item to process).
|
|
|
|
///
|
|
|
|
/// For cases where you need some other number of arguments:
|
|
|
|
///
|
|
|
|
/// - If you only need one argument, just use `()` for the `arg`
|
|
|
|
/// parameter.
|
|
|
|
/// - If you need 3+ arguments, use a tuple for the
|
|
|
|
/// `arg` parameter.
|
|
|
|
///
|
|
|
|
/// [README]: README.md
|
2017-09-07 14:11:58 +00:00
|
|
|
pub fn with_task<C, A, R, HCX>(&self,
|
|
|
|
key: DepNode,
|
|
|
|
cx: C,
|
|
|
|
arg: A,
|
|
|
|
task: fn(C, A) -> R)
|
|
|
|
-> (R, DepNodeIndex)
|
|
|
|
where C: DepGraphSafe + StableHashingContextProvider<ContextType=HCX>,
|
|
|
|
R: HashStable<HCX>,
|
2016-03-28 21:37:34 +00:00
|
|
|
{
|
2017-07-04 15:33:43 +00:00
|
|
|
if let Some(ref data) = self.data {
|
|
|
|
data.edges.borrow_mut().push_task(key);
|
2017-07-23 16:02:07 +00:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
profq_msg(ProfileQueriesMsg::TaskBegin(key.clone()))
|
|
|
|
};
|
2017-09-07 14:11:58 +00:00
|
|
|
|
|
|
|
// In incremental mode, hash the result of the task. We don't
|
|
|
|
// do anything with the hash yet, but we are computing it
|
|
|
|
// anyway so that
|
|
|
|
// - we make sure that the infrastructure works and
|
|
|
|
// - we can get an idea of the runtime cost.
|
|
|
|
let mut hcx = cx.create_stable_hashing_context();
|
|
|
|
|
2017-07-04 15:33:43 +00:00
|
|
|
let result = task(cx, arg);
|
2017-07-23 16:02:07 +00:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
profq_msg(ProfileQueriesMsg::TaskEnd)
|
|
|
|
};
|
2017-07-04 15:33:43 +00:00
|
|
|
let dep_node_index = data.edges.borrow_mut().pop_task(key);
|
2017-09-07 14:11:58 +00:00
|
|
|
|
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
result.hash_stable(&mut hcx, &mut stable_hasher);
|
2017-09-14 15:43:03 +00:00
|
|
|
|
|
|
|
assert!(self.fingerprints
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(key, stable_hasher.finish())
|
|
|
|
.is_none());
|
2017-09-07 14:11:58 +00:00
|
|
|
|
2017-07-04 15:33:43 +00:00
|
|
|
(result, dep_node_index)
|
|
|
|
} else {
|
2017-09-14 15:43:03 +00:00
|
|
|
if key.kind.fingerprint_needed_for_crate_hash() {
|
|
|
|
let mut hcx = cx.create_stable_hashing_context();
|
|
|
|
let result = task(cx, arg);
|
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
result.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
assert!(self.fingerprints
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(key, stable_hasher.finish())
|
|
|
|
.is_none());
|
|
|
|
(result, DepNodeIndex::INVALID)
|
|
|
|
} else {
|
|
|
|
(task(cx, arg), DepNodeIndex::INVALID)
|
|
|
|
}
|
2017-07-04 15:33:43 +00:00
|
|
|
}
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 14:37:12 +00:00
|
|
|
/// Execute something within an "anonymous" task, that is, a task the
|
|
|
|
/// DepNode of which is determined by the list of inputs it read from.
|
2017-07-04 15:33:43 +00:00
|
|
|
pub fn with_anon_task<OP,R>(&self, dep_kind: DepKind, op: OP) -> (R, DepNodeIndex)
|
2017-06-23 14:37:12 +00:00
|
|
|
where OP: FnOnce() -> R
|
|
|
|
{
|
|
|
|
if let Some(ref data) = self.data {
|
|
|
|
data.edges.borrow_mut().push_anon_task();
|
|
|
|
let result = op();
|
|
|
|
let dep_node = data.edges.borrow_mut().pop_anon_task(dep_kind);
|
|
|
|
(result, dep_node)
|
|
|
|
} else {
|
2017-07-04 15:33:43 +00:00
|
|
|
(op(), DepNodeIndex::INVALID)
|
2017-06-23 14:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:06:57 +00:00
|
|
|
#[inline]
|
2017-06-02 15:36:30 +00:00
|
|
|
pub fn read(&self, v: DepNode) {
|
2017-07-04 13:06:57 +00:00
|
|
|
if let Some(ref data) = self.data {
|
|
|
|
data.edges.borrow_mut().read(v);
|
2016-10-18 03:46:41 +00:00
|
|
|
}
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 15:33:43 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn read_index(&self, v: DepNodeIndex) {
|
|
|
|
if let Some(ref data) = self.data {
|
|
|
|
data.edges.borrow_mut().read_index(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:06:57 +00:00
|
|
|
/// Only to be used during graph loading
|
|
|
|
#[inline]
|
|
|
|
pub fn add_edge_directly(&self, source: DepNode, target: DepNode) {
|
|
|
|
self.data.as_ref().unwrap().edges.borrow_mut().add_edge(source, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only to be used during graph loading
|
|
|
|
pub fn add_node_directly(&self, node: DepNode) {
|
|
|
|
self.data.as_ref().unwrap().edges.borrow_mut().add_node(node);
|
|
|
|
}
|
|
|
|
|
2017-08-18 18:24:19 +00:00
|
|
|
pub fn alloc_input_node(&self, node: DepNode) -> DepNodeIndex {
|
|
|
|
if let Some(ref data) = self.data {
|
|
|
|
data.edges.borrow_mut().add_node(node)
|
|
|
|
} else {
|
|
|
|
DepNodeIndex::INVALID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 15:43:03 +00:00
|
|
|
pub fn fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
|
|
|
|
self.fingerprints.borrow().get(dep_node).cloned()
|
|
|
|
}
|
|
|
|
|
2016-07-21 16:33:23 +00:00
|
|
|
/// Indicates that a previous work product exists for `v`. This is
|
|
|
|
/// invoked during initial start-up based on what nodes are clean
|
|
|
|
/// (and what files exist in the incr. directory).
|
2017-06-06 13:09:21 +00:00
|
|
|
pub fn insert_previous_work_product(&self, v: &WorkProductId, data: WorkProduct) {
|
2016-07-21 16:33:23 +00:00
|
|
|
debug!("insert_previous_work_product({:?}, {:?})", v, data);
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.previous_work_products
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(v.clone(), data);
|
2016-07-21 16:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates that we created the given work-product in this run
|
|
|
|
/// for `v`. This record will be preserved and loaded in the next
|
|
|
|
/// run.
|
2017-06-06 13:09:21 +00:00
|
|
|
pub fn insert_work_product(&self, v: &WorkProductId, data: WorkProduct) {
|
2016-07-21 16:33:23 +00:00
|
|
|
debug!("insert_work_product({:?}, {:?})", v, data);
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.work_products
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(v.clone(), data);
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|
2016-07-21 16:33:23 +00:00
|
|
|
|
|
|
|
/// Check whether a previous work product exists for `v` and, if
|
|
|
|
/// so, return the path that leads to it. Used to skip doing work.
|
2017-06-06 13:09:21 +00:00
|
|
|
pub fn previous_work_product(&self, v: &WorkProductId) -> Option<WorkProduct> {
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|data| {
|
|
|
|
data.previous_work_products.borrow().get(v).cloned()
|
|
|
|
})
|
2016-07-21 16:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the map of work-products created during this run. Only
|
|
|
|
/// used during saving of the dep-graph.
|
2017-06-06 13:09:21 +00:00
|
|
|
pub fn work_products(&self) -> Ref<FxHashMap<WorkProductId, WorkProduct>> {
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data.as_ref().unwrap().work_products.borrow()
|
2016-07-21 16:33:23 +00:00
|
|
|
}
|
2017-01-16 22:54:20 +00:00
|
|
|
|
|
|
|
/// Access the map of work-products created during the cached run. Only
|
|
|
|
/// used during saving of the dep-graph.
|
2017-06-06 13:09:21 +00:00
|
|
|
pub fn previous_work_products(&self) -> Ref<FxHashMap<WorkProductId, WorkProduct>> {
|
2017-07-04 13:06:57 +00:00
|
|
|
self.data.as_ref().unwrap().previous_work_products.borrow()
|
2017-01-16 22:54:20 +00:00
|
|
|
}
|
2017-06-12 15:00:55 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2017-06-23 14:37:12 +00:00
|
|
|
pub fn register_dep_node_debug_str<F>(&self,
|
|
|
|
dep_node: DepNode,
|
|
|
|
debug_str_gen: F)
|
2017-06-12 15:00:55 +00:00
|
|
|
where F: FnOnce() -> String
|
|
|
|
{
|
2017-08-30 18:53:57 +00:00
|
|
|
let dep_node_debug = &self.data.as_ref().unwrap().dep_node_debug;
|
2017-06-12 15:00:55 +00:00
|
|
|
|
2017-08-30 18:53:57 +00:00
|
|
|
if dep_node_debug.borrow().contains_key(&dep_node) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let debug_str = debug_str_gen();
|
|
|
|
dep_node_debug.borrow_mut().insert(dep_node, debug_str);
|
2017-06-12 15:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn dep_node_debug_str(&self, dep_node: DepNode) -> Option<String> {
|
2017-07-10 19:45:36 +00:00
|
|
|
self.data.as_ref().and_then(|t| t.dep_node_debug.borrow().get(&dep_node).cloned())
|
2017-06-12 15:00:55 +00:00
|
|
|
}
|
2016-07-21 16:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A "work product" is an intermediate result that we save into the
|
|
|
|
/// incremental directory for later re-use. The primary example are
|
|
|
|
/// the object files that we save for each partition at code
|
|
|
|
/// generation time.
|
|
|
|
///
|
|
|
|
/// Each work product is associated with a dep-node, representing the
|
|
|
|
/// process that produced the work-product. If that dep-node is found
|
|
|
|
/// to be dirty when we load up, then we will delete the work-product
|
2016-07-22 14:39:30 +00:00
|
|
|
/// at load time. If the work-product is found to be clean, then we
|
2016-07-21 16:33:23 +00:00
|
|
|
/// will keep a record in the `previous_work_products` list.
|
|
|
|
///
|
|
|
|
/// In addition, work products have an associated hash. This hash is
|
|
|
|
/// an extra hash that can be used to decide if the work-product from
|
|
|
|
/// a previous compilation can be re-used (in addition to the dirty
|
|
|
|
/// edges check).
|
|
|
|
///
|
|
|
|
/// As the primary example, consider the object files we generate for
|
|
|
|
/// each partition. In the first run, we create partitions based on
|
|
|
|
/// the symbols that need to be compiled. For each partition P, we
|
|
|
|
/// hash the symbols in P and create a `WorkProduct` record associated
|
|
|
|
/// with `DepNode::TransPartition(P)`; the hash is the set of symbols
|
|
|
|
/// in P.
|
|
|
|
///
|
|
|
|
/// The next time we compile, if the `DepNode::TransPartition(P)` is
|
|
|
|
/// judged to be clean (which means none of the things we read to
|
|
|
|
/// generate the partition were found to be dirty), it will be loaded
|
|
|
|
/// into previous work products. We will then regenerate the set of
|
|
|
|
/// symbols in the partition P and hash them (note that new symbols
|
|
|
|
/// may be added -- for example, new monomorphizations -- even if
|
|
|
|
/// nothing in P changed!). We will compare that hash against the
|
|
|
|
/// previous hash. If it matches up, we can reuse the object file.
|
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct WorkProduct {
|
2017-06-23 14:37:12 +00:00
|
|
|
pub cgu_name: String,
|
2016-07-25 14:51:14 +00:00
|
|
|
/// Extra hash used to decide if work-product is still suitable;
|
2016-07-21 16:33:23 +00:00
|
|
|
/// note that this is *not* a hash of the work-product itself.
|
|
|
|
/// See documentation on `WorkProduct` type for an example.
|
|
|
|
pub input_hash: u64,
|
|
|
|
|
2016-07-25 14:51:14 +00:00
|
|
|
/// Saved files associated with this CGU
|
|
|
|
pub saved_files: Vec<(OutputType, String)>,
|
2016-03-28 21:37:34 +00:00
|
|
|
}
|