Skip to content

First draft at constructing a dependency graph #30393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
197 changes: 197 additions & 0 deletions src/librustc/dep_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright 2012-2015 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.

use middle::def_id::DefId;
use middle::ty;
use rustc_data_structures::fnv::FnvHashMap;
use rustc_data_structures::dependency;
use rustc_front::hir;
use rustc_front::intravisit::Visitor;
use std::ops::Index;
use std::hash::Hash;
use std::marker::PhantomData;
use std::rc::Rc;
use util::common;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum DepNode {
// Represents the `Krate` as a whole (the `hir::Krate` value) (as
// distinct from the krate module). This is basically a hash of
// the entire krate, so if you read from `Krate` (e.g., by calling
// `tcx.map.krate()`), we will have to assume that any change
// means that you need to be recompiled. This is because the
// `Krate` value gives you access to all other items. To avoid
// this fate, do not call `tcx.map.krate()`; instead, prefer
// wrappers like `tcx.visit_all_items_in_krate()`. If there is no
// suitable wrapper, you can use `tcx.dep_graph.ignore()` to gain
// access to the krate, but you must remember to add suitable
// edges yourself for the individual items that you read.
Krate,

// Represents the HIR node with the given node-id
Hir(DefId),

// Represents different phases in the compiler.
CollectItem(DefId),
TypeScheme(DefId),
Coherence,
CoherenceOverlapCheck(DefId),
CoherenceOverlapCheckSpecial(DefId),
CoherenceOrphanCheck(DefId),
Variance,
WfCheck(DefId),
TypeckItemType(DefId),
TypeckItemBody(DefId),
Dropck(DefId),
CheckConst(DefId),
Privacy,
IntrinsicCheck(DefId),
MatchCheck(DefId),
MirMapConstruction(DefId),
BorrowCheck(DefId),
RvalueCheck(DefId),
Reachability,
DeadCheck,
StabilityCheck,
LateLintCheck,
IntrinsicUseCheck,
TransLinkMeta,
TransCrateItem(DefId),
TransInlinedItem(DefId),
TransWriteMetadata,

// Nodes representing bits of computed IR in the tcx. Each of
// these corresponds to a particular table in the tcx.
ImplOrTraitItems(DefId),
TraitItemDefIds(DefId),
ImplTraitRef(DefId),
Tcache(DefId),
TraitDefs(DefId),
AdtDefs(DefId),
Predicates(DefId),
ItemVarianceMap(DefId),
}

impl dependency::DepNodeId for DepNode { }

pub type DepGraph = dependency::DepGraph<DepNode>;

/// A DepTrackingMap offers a subset of the `Map` API and ensures that
/// we make calls to `read` and `write` as appropriate. We key the
/// maps with a unique type for brevity.
pub struct DepTrackingMap<M: DepTrackingMapId> {
phantom: PhantomData<M>,
graph: Rc<DepGraph>,
map: FnvHashMap<M::Key, M::Value>,
}

pub trait DepTrackingMapId {
type Key: Eq + Hash + Clone;
type Value: Clone;
fn to_dep_node(key: &Self::Key) -> DepNode;
}

impl<M: DepTrackingMapId> DepTrackingMap<M> {
pub fn new(graph: Rc<DepGraph>) -> DepTrackingMap<M> {
DepTrackingMap {
phantom: PhantomData,
graph: graph,
map: FnvHashMap()
}
}

/// Registers a (synthetic) read from the key `k`. Usually this
/// is invoked automatically by `get`.
pub fn read(&self, k: &M::Key) {
let dep_node = M::to_dep_node(k);
self.graph.read(dep_node);
}

/// Registers a (synthetic) write to the key `k`. Usually this is
/// invoked automatically by `insert`.
fn write(&self, k: &M::Key) {
let dep_node = M::to_dep_node(k);
self.graph.write(dep_node);
}

pub fn get(&self, k: &M::Key) -> Option<&M::Value> {
self.read(k);
self.map.get(k)
}

pub fn insert(&mut self, k: M::Key, v: M::Value) -> Option<M::Value> {
self.write(&k);
self.map.insert(k, v)
}

pub fn contains_key(&self, k: &M::Key) -> bool {
self.read(k);
self.map.contains_key(k)
}
}

impl<M: DepTrackingMapId> common::MemoizationMap for DepTrackingMap<M> {
type Key = M::Key;
type Value = M::Value;
fn get(&self, key: &M::Key) -> Option<&M::Value> {
self.get(key)
}
fn insert(&mut self, key: M::Key, value: M::Value) -> Option<M::Value> {
self.insert(key, value)
}
}

impl<'k, M: DepTrackingMapId> Index<&'k M::Key> for DepTrackingMap<M> {
type Output = M::Value;

#[inline]
fn index(&self, k: &'k M::Key) -> &M::Value {
self.get(k).unwrap()
}
}

/// Visit all the items in the krate in some order. When visiting a
/// particular item, first create a dep-node by calling `dep_node_fn`
/// and push that onto the dep-graph stack of tasks, and also create a
/// read edge from the corresponding AST node. This is used in
/// compiler passes to automatically record the item that they are
/// working on.
pub fn visit_all_items_in_krate<'tcx,V,F>(tcx: &ty::ctxt<'tcx>,
mut dep_node_fn: F,
visitor: &mut V)
where F: FnMut(DefId) -> DepNode, V: Visitor<'tcx>
{
struct TrackingVisitor<'visit, 'tcx: 'visit, F: 'visit, V: 'visit> {
tcx: &'visit ty::ctxt<'tcx>,
dep_node_fn: &'visit mut F,
visitor: &'visit mut V
}

impl<'visit, 'tcx, F, V> Visitor<'tcx> for TrackingVisitor<'visit, 'tcx, F, V>
where F: FnMut(DefId) -> DepNode, V: Visitor<'tcx>
{
fn visit_item(&mut self, i: &'tcx hir::Item) {
let item_def_id = self.tcx.map.local_def_id(i.id);
let task_id = (self.dep_node_fn)(item_def_id);
debug!("About to start task {:?}", task_id);
let _task = self.tcx.dep_graph.in_task(task_id);
self.tcx.dep_graph.read(DepNode::Hir(item_def_id));
self.visitor.visit_item(i)
}
}

let krate = tcx.dep_graph.with_ignore(|| tcx.map.krate());
let mut tracking_visitor = TrackingVisitor {
tcx: tcx,
dep_node_fn: &mut dep_node_fn,
visitor: visitor
};
krate.visit_all_items(&mut tracking_visitor)
}
2 changes: 2 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub mod back {
pub use rustc_back::svh;
}

pub mod dep_graph;

pub mod front {
pub mod check_attr;
pub mod map;
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//! for all lint attributes.
use self::TargetLint::*;

use dep_graph::DepNode;
use middle::privacy::AccessLevels;
use middle::ty::{self, Ty};
use session::{early_error, Session};
Expand Down Expand Up @@ -1025,6 +1026,8 @@ impl LateLintPass for GatherNodeLevels {
///
/// Consumes the `lint_store` field of the `Session`.
pub fn check_crate(tcx: &ty::ctxt, access_levels: &AccessLevels) {
let _task = tcx.dep_graph.in_task(DepNode::LateLintCheck);

let krate = tcx.map.krate();
let mut cx = LateContext::new(tcx, krate, access_levels);

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// - It's not possible to take the address of a static item with unsafe interior. This is enforced
// by borrowck::gather_loans

use dep_graph::DepNode;
use middle::ty::cast::{CastKind};
use middle::const_eval::{self, ConstEvalErr};
use middle::const_eval::ErrKind::IndexOpFeatureGated;
Expand Down Expand Up @@ -840,13 +841,12 @@ fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Exp
}

pub fn check_crate(tcx: &ty::ctxt) {
tcx.map.krate().visit_all_items(&mut CheckCrateVisitor {
tcx.visit_all_items_in_krate(DepNode::CheckConst, &mut CheckCrateVisitor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice how simple adapting existing passes seems to be :)

tcx: tcx,
mode: Mode::Var,
qualif: ConstQualif::NOT_CONST,
rvalue_borrows: NodeMap()
});

tcx.sess.abort_if_errors();
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use self::Constructor::*;
use self::Usefulness::*;
use self::WitnessPreference::*;

use dep_graph::DepNode;
use middle::const_eval::{compare_const_vals, ConstVal};
use middle::const_eval::{eval_const_expr, eval_const_expr_partial};
use middle::const_eval::{const_expr_to_pat, lookup_const_by_id};
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MatchCheckCtxt<'a, 'tcx> {
}

pub fn check_crate(tcx: &ty::ctxt) {
tcx.map.krate().visit_all_items(&mut MatchCheckCtxt {
tcx.visit_all_items_in_krate(DepNode::MatchCheck, &mut MatchCheckCtxt {
tcx: tcx,
param_env: tcx.empty_parameter_environment(),
});
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/check_rvalues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
// Checks that all rvalues in a crate have statically known size. check_crate
// is the public starting point.

use dep_graph::DepNode;
use middle::expr_use_visitor as euv;
use middle::infer;
use middle::mem_categorization as mc;
use middle::ty::ParameterEnvironment;
use middle::ty;

use syntax::ast;
use rustc_front::hir;
use syntax::codemap::Span;
use rustc_front::intravisit;
use syntax::ast;
use syntax::codemap::Span;

pub fn check_crate(tcx: &ty::ctxt,
krate: &hir::Crate) {
pub fn check_crate(tcx: &ty::ctxt) {
let mut rvcx = RvalueContext { tcx: tcx };
krate.visit_all_items(&mut rvcx);
tcx.visit_all_items_in_krate(DepNode::RvalueCheck, &mut rvcx);
}

struct RvalueContext<'a, 'tcx: 'a> {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// closely. The idea is that all reachable symbols are live, codes called
// from live codes are live, and everything else is dead.

use dep_graph::DepNode;
use front::map as ast_map;
use rustc_front::hir;
use rustc_front::intravisit::{self, Visitor};
Expand Down Expand Up @@ -590,6 +591,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
}

pub fn check_crate(tcx: &ty::ctxt, access_levels: &privacy::AccessLevels) {
let _task = tcx.dep_graph.in_task(DepNode::DeadCheck);
let krate = tcx.map.krate();
let live_symbols = find_live(tcx, access_levels, krate);
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ enum PassArgs {
}

impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
pub fn new(delegate: &'d mut (Delegate<'tcx>),
pub fn new(delegate: &'d mut Delegate<'tcx>,
typer: &'t infer::InferCtxt<'a, 'tcx>)
-> ExprUseVisitor<'d,'t,'a,'tcx> where 'tcx:'a+'d
{
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use dep_graph::DepNode;
use middle::def::DefFn;
use middle::def_id::DefId;
use middle::subst::{Subst, Substs, EnumeratedItems};
Expand All @@ -29,7 +30,7 @@ pub fn check_crate(tcx: &ctxt) {
dummy_sized_ty: tcx.types.isize,
dummy_unsized_ty: tcx.mk_slice(tcx.types.isize),
};
tcx.map.krate().visit_all_items(&mut visitor);
tcx.visit_all_items_in_krate(DepNode::IntrinsicCheck, &mut visitor);
}

struct IntrinsicCheckingVisitor<'a, 'tcx: 'a> {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// makes all other generics or inline functions that it references
// reachable as well.

use dep_graph::DepNode;
use front::map as ast_map;
use middle::def;
use middle::def_id::DefId;
Expand Down Expand Up @@ -349,6 +350,7 @@ impl<'a, 'v> Visitor<'v> for CollectPrivateImplItemsVisitor<'a> {
pub fn find_reachable(tcx: &ty::ctxt,
access_levels: &privacy::AccessLevels)
-> NodeSet {
let _task = tcx.dep_graph.in_task(DepNode::Reachability);

let mut reachable_context = ReachableContext::new(tcx);

Expand Down
5 changes: 3 additions & 2 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

pub use self::StabilityLevel::*;

use dep_graph::DepNode;
use session::Session;
use lint;
use middle::cstore::{CrateStore, LOCAL_CRATE};
Expand Down Expand Up @@ -301,6 +302,7 @@ impl<'tcx> Index<'tcx> {
/// features used.
pub fn check_unstable_api_usage(tcx: &ty::ctxt)
-> FnvHashMap<InternedString, StabilityLevel> {
let _task = tcx.dep_graph.in_task(DepNode::StabilityCheck);
let ref active_lib_features = tcx.sess.features.borrow().declared_lib_features;

// Put the active features into a map for quick lookup
Expand All @@ -314,8 +316,7 @@ pub fn check_unstable_api_usage(tcx: &ty::ctxt)
};
intravisit::walk_crate(&mut checker, tcx.map.krate());

let used_features = checker.used_features;
return used_features;
checker.used_features
}

struct Checker<'a, 'tcx: 'a> {
Expand Down
Loading