Rollup merge of #67761 - cjgillot:split_graph, r=Zoxc

Move the dep_graph construction to a dedicated crate.

The interface for librustc consists in two traits: `DepKind` and `DepContext`.

The `DepKind` is the main interface. It allows to probe properties of the dependency.
As before, `DepNode` is the pair of a `DepKind` object and a hash fingerprint.

The `DepContext` takes the place of the `TyCtxt`, and handles communication with the query engine.

The use of the `ImplicitCtxt` through `ty::tls` is done through the `DepKind` trait.
This may not be the best choice, but it seemed like the simplest.
This commit is contained in:
Mazdak Farrokhzad 2020-03-24 00:49:36 +01:00 committed by GitHub
commit 9da25d9faf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 765 additions and 433 deletions

View File

@ -3116,6 +3116,7 @@ dependencies = [
"rustc_hir", "rustc_hir",
"rustc_index", "rustc_index",
"rustc_macros", "rustc_macros",
"rustc_query_system",
"rustc_session", "rustc_session",
"rustc_span", "rustc_span",
"rustc_target", "rustc_target",
@ -4021,6 +4022,22 @@ dependencies = [
"rustc_typeck", "rustc_typeck",
] ]
[[package]]
name = "rustc_query_system"
version = "0.0.0"
dependencies = [
"log",
"parking_lot 0.9.0",
"rustc_ast",
"rustc_data_structures",
"rustc_errors",
"rustc_hir",
"rustc_index",
"rustc_macros",
"serialize",
"smallvec 1.0.0",
]
[[package]] [[package]]
name = "rustc_resolve" name = "rustc_resolve"
version = "0.0.0" version = "0.0.0"

View File

@ -25,6 +25,7 @@ rustc_hir = { path = "../librustc_hir" }
rustc_target = { path = "../librustc_target" } rustc_target = { path = "../librustc_target" }
rustc_macros = { path = "../librustc_macros" } rustc_macros = { path = "../librustc_macros" }
rustc_data_structures = { path = "../librustc_data_structures" } rustc_data_structures = { path = "../librustc_data_structures" }
rustc_query_system = { path = "../librustc_query_system" }
rustc_errors = { path = "../librustc_errors" } rustc_errors = { path = "../librustc_errors" }
rustc_index = { path = "../librustc_index" } rustc_index = { path = "../librustc_index" }
rustc_serialize = { path = "../libserialize", package = "serialize" } rustc_serialize = { path = "../libserialize", package = "serialize" }

View File

@ -50,7 +50,7 @@
//! fingerprint for a given set of node parameters. //! fingerprint for a given set of node parameters.
use crate::hir::map::DefPathHash; use crate::hir::map::DefPathHash;
use crate::ich::{Fingerprint, StableHashingContext}; use crate::ich::Fingerprint;
use crate::mir; use crate::mir;
use crate::mir::interpret::{GlobalId, LitToConstInput}; use crate::mir::interpret::{GlobalId, LitToConstInput};
use crate::traits; use crate::traits;
@ -62,13 +62,13 @@ use crate::traits::query::{
use crate::ty::subst::SubstsRef; use crate::ty::subst::SubstsRef;
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt}; use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::HirId; use rustc_hir::HirId;
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
use std::fmt;
use std::hash::Hash; use std::hash::Hash;
pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
// erase!() just makes tokens go away. It's used to specify which macro argument // erase!() just makes tokens go away. It's used to specify which macro argument
// is repeated (i.e., which sub-expression of the macro we are in) but don't need // is repeated (i.e., which sub-expression of the macro we are in) but don't need
// to actually use any of the arguments. // to actually use any of the arguments.
@ -128,7 +128,7 @@ macro_rules! define_dep_nodes {
// tuple args // tuple args
$({ $({
return <$tuple_arg_ty as DepNodeParams> return <$tuple_arg_ty as DepNodeParams<TyCtxt<'_>>>
::CAN_RECONSTRUCT_QUERY_KEY; ::CAN_RECONSTRUCT_QUERY_KEY;
})* })*
@ -212,20 +212,39 @@ macro_rules! define_dep_nodes {
)* )*
} }
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
RustcEncodable, RustcDecodable)]
pub struct DepNode {
pub kind: DepKind,
pub hash: Fingerprint,
}
impl DepNode { pub trait DepNodeExt: Sized {
/// Construct a DepNode from the given DepKind and DefPathHash. This /// Construct a DepNode from the given DepKind and DefPathHash. This
/// method will assert that the given DepKind actually requires a /// method will assert that the given DepKind actually requires a
/// single DefId/DefPathHash parameter. /// single DefId/DefPathHash parameter.
pub fn from_def_path_hash(def_path_hash: DefPathHash, fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> Self;
kind: DepKind)
-> DepNode { /// Extracts the DefId corresponding to this DepNode. This will work
/// if two conditions are met:
///
/// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
/// 2. the item that the DefPath refers to exists in the current tcx.
///
/// Condition (1) is determined by the DepKind variant of the
/// DepNode. Condition (2) might not be fulfilled if a DepNode
/// refers to something from the previous compilation session that
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
/// Used in testing
fn from_label_string(label: &str, def_path_hash: DefPathHash)
-> Result<Self, ()>;
/// Used in testing
fn has_label_string(label: &str) -> bool;
}
impl DepNodeExt for DepNode {
/// Construct a DepNode from the given DepKind and DefPathHash. This
/// method will assert that the given DepKind actually requires a
/// single DefId/DefPathHash parameter.
fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
debug_assert!(kind.can_reconstruct_query_key() && kind.has_params()); debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
DepNode { DepNode {
kind, kind,
@ -233,17 +252,6 @@ macro_rules! define_dep_nodes {
} }
} }
/// Creates a new, parameterless DepNode. This method will assert
/// that the DepNode corresponding to the given DepKind actually
/// does not require any parameters.
pub fn new_no_params(kind: DepKind) -> DepNode {
debug_assert!(!kind.has_params());
DepNode {
kind,
hash: Fingerprint::ZERO,
}
}
/// Extracts the DefId corresponding to this DepNode. This will work /// Extracts the DefId corresponding to this DepNode. This will work
/// if two conditions are met: /// if two conditions are met:
/// ///
@ -254,20 +262,17 @@ macro_rules! define_dep_nodes {
/// DepNode. Condition (2) might not be fulfilled if a DepNode /// DepNode. Condition (2) might not be fulfilled if a DepNode
/// refers to something from the previous compilation session that /// refers to something from the previous compilation session that
/// has been removed. /// has been removed.
pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> { fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
if self.kind.can_reconstruct_query_key() { if self.kind.can_reconstruct_query_key() {
let def_path_hash = DefPathHash(self.hash); let def_path_hash = DefPathHash(self.hash);
tcx.def_path_hash_to_def_id.as_ref()? tcx.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
.get(&def_path_hash).cloned()
} else { } else {
None None
} }
} }
/// Used in testing /// Used in testing
pub fn from_label_string(label: &str, fn from_label_string(label: &str, def_path_hash: DefPathHash) -> Result<DepNode, ()> {
def_path_hash: DefPathHash)
-> Result<DepNode, ()> {
let kind = match label { let kind = match label {
$( $(
stringify!($variant) => DepKind::$variant, stringify!($variant) => DepKind::$variant,
@ -287,7 +292,7 @@ macro_rules! define_dep_nodes {
} }
/// Used in testing /// Used in testing
pub fn has_label_string(label: &str) -> bool { fn has_label_string(label: &str) -> bool {
match label { match label {
$( $(
stringify!($variant) => true, stringify!($variant) => true,
@ -308,35 +313,6 @@ macro_rules! define_dep_nodes {
); );
} }
impl fmt::Debug for DepNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.kind)?;
if !self.kind.has_params() && !self.kind.is_anon() {
return Ok(());
}
write!(f, "(")?;
crate::ty::tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
if let Some(def_id) = self.extract_def_id(tcx) {
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*self) {
write!(f, "{}", s)?;
} else {
write!(f, "{}", self.hash)?;
}
} else {
write!(f, "{}", self.hash)?;
}
Ok(())
})?;
write!(f, ")")
}
}
rustc_dep_node_append!([define_dep_nodes!][ <'tcx> rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
// We use this for most things when incr. comp. is turned off. // We use this for most things when incr. comp. is turned off.
[] Null, [] Null,
@ -349,58 +325,10 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
[] CompileCodegenUnit(Symbol), [] CompileCodegenUnit(Symbol),
]); ]);
pub(crate) trait DepNodeParams<'tcx>: fmt::Debug + Sized { impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
const CAN_RECONSTRUCT_QUERY_KEY: bool;
/// This method turns the parameters of a DepNodeConstructor into an opaque
/// Fingerprint to be used in DepNode.
/// Not all DepNodeParams support being turned into a Fingerprint (they
/// don't need to if the corresponding DepNode is anonymous).
fn to_fingerprint(&self, _: TyCtxt<'tcx>) -> Fingerprint {
panic!("Not implemented. Accidentally called on anonymous node?")
}
fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
format!("{:?}", self)
}
/// This method tries to recover the query key from the given `DepNode`,
/// something which is needed when forcing `DepNode`s during red-green
/// evaluation. The query system will only call this method if
/// `CAN_RECONSTRUCT_QUERY_KEY` is `true`.
/// It is always valid to return `None` here, in which case incremental
/// compilation will treat the query as having changed instead of forcing it.
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
}
impl<'tcx, T> DepNodeParams<'tcx> for T
where
T: HashStable<StableHashingContext<'tcx>> + fmt::Debug,
{
default const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
default fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
let mut hcx = tcx.create_stable_hashing_context();
let mut hasher = StableHasher::new();
self.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
}
default fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
format!("{:?}", *self)
}
default fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
None
}
}
impl<'tcx> DepNodeParams<'tcx> for DefId {
const CAN_RECONSTRUCT_QUERY_KEY: bool = true; const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint { fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
tcx.def_path_hash(*self).0 tcx.def_path_hash(*self).0
} }
@ -413,10 +341,10 @@ impl<'tcx> DepNodeParams<'tcx> for DefId {
} }
} }
impl<'tcx> DepNodeParams<'tcx> for LocalDefId { impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
const CAN_RECONSTRUCT_QUERY_KEY: bool = true; const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint { fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
self.to_def_id().to_fingerprint(tcx) self.to_def_id().to_fingerprint(tcx)
} }
@ -429,10 +357,10 @@ impl<'tcx> DepNodeParams<'tcx> for LocalDefId {
} }
} }
impl<'tcx> DepNodeParams<'tcx> for CrateNum { impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
const CAN_RECONSTRUCT_QUERY_KEY: bool = true; const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint { fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX }; let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
tcx.def_path_hash(def_id).0 tcx.def_path_hash(def_id).0
} }
@ -446,13 +374,13 @@ impl<'tcx> DepNodeParams<'tcx> for CrateNum {
} }
} }
impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) { impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
const CAN_RECONSTRUCT_QUERY_KEY: bool = false; const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
// We actually would not need to specialize the implementation of this // We actually would not need to specialize the implementation of this
// method but it's faster to combine the hashes than to instantiate a full // method but it's faster to combine the hashes than to instantiate a full
// hashing context and stable-hashing state. // hashing context and stable-hashing state.
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint { fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
let (def_id_0, def_id_1) = *self; let (def_id_0, def_id_1) = *self;
let def_path_hash_0 = tcx.def_path_hash(def_id_0); let def_path_hash_0 = tcx.def_path_hash(def_id_0);
@ -468,13 +396,13 @@ impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
} }
} }
impl<'tcx> DepNodeParams<'tcx> for HirId { impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
const CAN_RECONSTRUCT_QUERY_KEY: bool = false; const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
// We actually would not need to specialize the implementation of this // We actually would not need to specialize the implementation of this
// method but it's faster to combine the hashes than to instantiate a full // method but it's faster to combine the hashes than to instantiate a full
// hashing context and stable-hashing state. // hashing context and stable-hashing state.
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint { fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
let HirId { owner, local_id } = *self; let HirId { owner, local_id } = *self;
let def_path_hash = tcx.def_path_hash(owner.to_def_id()); let def_path_hash = tcx.def_path_hash(owner.to_def_id());
@ -483,27 +411,3 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
def_path_hash.0.combine(local_id) def_path_hash.0.combine(local_id)
} }
} }
/// A "work product" corresponds to a `.o` (or other) file that we
/// save in between runs. These IDs do not have a `DefId` but rather
/// some independent path or string that persists between runs without
/// the need to be mapped or unmapped. (This ensures we can serialize
/// them even in the absence of a tcx.)
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
#[derive(HashStable)]
pub struct WorkProductId {
hash: Fingerprint,
}
impl WorkProductId {
pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
let mut hasher = StableHasher::new();
cgu_name.len().hash(&mut hasher);
cgu_name.hash(&mut hasher);
WorkProductId { hash: hasher.finish() }
}
pub fn from_fingerprint(fingerprint: Fingerprint) -> WorkProductId {
WorkProductId { hash: fingerprint }
}
}

View File

@ -1,17 +1,191 @@
pub mod debug; use crate::ich::StableHashingContext;
mod dep_node; use crate::ty::query::try_load_from_on_disk_cache;
mod graph; use crate::ty::{self, TyCtxt};
mod prev; use rustc_data_structures::profiling::SelfProfilerRef;
mod query; use rustc_data_structures::sync::Lock;
mod safe; use rustc_data_structures::thin_vec::ThinVec;
mod serialized; use rustc_errors::Diagnostic;
use rustc_hir::def_id::DefId;
pub(crate) use self::dep_node::DepNodeParams; mod dep_node;
pub use self::dep_node::{label_strs, DepConstructor, DepKind, DepNode, WorkProductId}; mod safe;
pub use self::graph::WorkProductFileKind;
pub use self::graph::{hash_result, DepGraph, DepNodeColor, DepNodeIndex, TaskDeps, WorkProduct}; pub(crate) use rustc_query_system::dep_graph::DepNodeParams;
pub use self::prev::PreviousDepGraph; pub use rustc_query_system::dep_graph::{
pub use self::query::DepGraphQuery; debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, SerializedDepNodeIndex,
pub use self::safe::AssertDepGraphSafe; WorkProduct, WorkProductFileKind, WorkProductId,
pub use self::safe::DepGraphSafe; };
pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
pub use dep_node::{label_strs, DepConstructor, DepKind, DepNode, DepNodeExt};
pub use safe::AssertDepGraphSafe;
pub use safe::DepGraphSafe;
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
pub type PreviousDepGraph = rustc_query_system::dep_graph::PreviousDepGraph<DepKind>;
pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
impl rustc_query_system::dep_graph::DepKind for DepKind {
fn is_eval_always(&self) -> bool {
DepKind::is_eval_always(self)
}
fn has_params(&self) -> bool {
DepKind::has_params(self)
}
fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", node.kind)?;
if !node.kind.has_params() && !node.kind.is_anon() {
return Ok(());
}
write!(f, "(")?;
ty::tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
if let Some(def_id) = node.extract_def_id(tcx) {
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
write!(f, "{}", s)?;
} else {
write!(f, "{}", node.hash)?;
}
} else {
write!(f, "{}", node.hash)?;
}
Ok(())
})?;
write!(f, ")")
}
fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
where
OP: FnOnce() -> R,
{
ty::tls::with_context(|icx| {
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
ty::tls::enter_context(&icx, |_| op())
})
}
fn read_deps<OP>(op: OP) -> ()
where
OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>) -> (),
{
ty::tls::with_context_opt(|icx| {
let icx = if let Some(icx) = icx { icx } else { return };
op(icx.task_deps)
})
}
}
impl<'tcx> DepContext for TyCtxt<'tcx> {
type DepKind = DepKind;
type StableHashingContext = StableHashingContext<'tcx>;
fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
TyCtxt::create_stable_hashing_context(*self)
}
fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
// FIXME: This match is just a workaround for incremental bugs and should
// be removed. https://github.com/rust-lang/rust/issues/62649 is one such
// bug that must be fixed before removing this.
match dep_node.kind {
DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
if let Some(def_id) = dep_node.extract_def_id(*self) {
if def_id_corresponds_to_hir_dep_node(*self, def_id) {
if dep_node.kind == DepKind::CrateMetadata {
// The `DefPath` has corresponding node,
// and that node should have been marked
// either red or green in `data.colors`.
bug!(
"DepNode {:?} should have been \
pre-marked as red or green but wasn't.",
dep_node
);
}
} else {
// This `DefPath` does not have a
// corresponding `DepNode` (e.g. a
// struct field), and the ` DefPath`
// collided with the `DefPath` of a
// proper item that existed in the
// previous compilation session.
//
// Since the given `DefPath` does not
// denote the item that previously
// existed, we just fail to mark green.
return false;
}
} else {
// If the node does not exist anymore, we
// just fail to mark green.
return false;
}
}
_ => {
// For other kinds of nodes it's OK to be
// forced.
}
}
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
ty::query::force_from_dep_node(*self, dep_node)
}
fn has_errors_or_delayed_span_bugs(&self) -> bool {
self.sess.has_errors_or_delayed_span_bugs()
}
fn diagnostic(&self) -> &rustc_errors::Handler {
self.sess.diagnostic()
}
// Interactions with on_disk_cache
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
try_load_from_on_disk_cache(*self, dep_node)
}
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
self.queries.on_disk_cache.load_diagnostics(*self, prev_dep_node_index)
}
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
self.queries.on_disk_cache.store_diagnostics(dep_node_index, diagnostics)
}
fn profiler(&self) -> &SelfProfilerRef {
&self.prof
}
}
fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
def_id.index == hir_id.owner.local_def_index
}
impl rustc_query_system::HashStableContext for StableHashingContext<'_> {
fn debug_dep_tasks(&self) -> bool {
self.sess().opts.debugging_opts.dep_tasks
}
}
impl rustc_query_system::HashStableContextProvider<StableHashingContext<'tcx>> for TyCtxt<'tcx> {
fn get_stable_hashing_context(&self) -> StableHashingContext<'tcx> {
self.create_stable_hashing_context()
}
}
impl rustc_query_system::HashStableContextProvider<StableHashingContext<'a>>
for StableHashingContext<'a>
{
fn get_stable_hashing_context(&self) -> Self {
self.clone()
}
}

View File

@ -2,56 +2,8 @@
use crate::ty::TyCtxt; use crate::ty::TyCtxt;
use rustc_ast::ast::NodeId; pub use rustc_query_system::dep_graph::{AssertDepGraphSafe, DepGraphSafe};
use rustc_hir::def_id::DefId;
use rustc_hir::BodyId;
/// The `DepGraphSafe` trait is used to specify what kinds of values
/// are safe to "leak" into a task. The idea is that this should be
/// only be implemented for things like the tcx as well as various id
/// types, which will create reads in the dep-graph whenever the trait
/// loads anything that might depend on the input program.
pub trait DepGraphSafe {}
/// A `BodyId` on its own doesn't give access to any particular state.
/// You must fetch the state from the various maps or generate
/// on-demand queries, all of which create reads.
impl DepGraphSafe for BodyId {}
/// A `NodeId` on its own doesn't give access to any particular state.
/// You must fetch the state from the various maps or generate
/// on-demand queries, all of which create reads.
impl DepGraphSafe for NodeId {}
/// A `DefId` on its own doesn't give access to any particular state.
/// You must fetch the state from the various maps or generate
/// on-demand queries, all of which create reads.
impl DepGraphSafe for DefId {}
/// The type context itself can be used to access all kinds of tracked /// The type context itself can be used to access all kinds of tracked
/// state, but those accesses should always generate read events. /// state, but those accesses should always generate read events.
impl<'tcx> DepGraphSafe for TyCtxt<'tcx> {} impl<'tcx> DepGraphSafe for TyCtxt<'tcx> {}
/// Tuples make it easy to build up state.
impl<A, B> DepGraphSafe for (A, B)
where
A: DepGraphSafe,
B: DepGraphSafe,
{
}
/// Shared ref to dep-graph-safe stuff should still be dep-graph-safe.
impl<'a, A> DepGraphSafe for &'a A where A: DepGraphSafe {}
/// Mut ref to dep-graph-safe stuff should still be dep-graph-safe.
impl<'a, A> DepGraphSafe for &'a mut A where A: DepGraphSafe {}
/// No data here! :)
impl DepGraphSafe for () {}
/// A convenient override that lets you pass arbitrary state into a
/// task. Every use should be accompanied by a comment explaining why
/// it makes sense (or how it could be refactored away in the future).
pub struct AssertDepGraphSafe<T>(pub T);
impl<T> DepGraphSafe for AssertDepGraphSafe<T> {}

View File

@ -150,8 +150,6 @@ rustc_query_append! { [define_queries!][<'tcx>] }
/// add it to the "We don't have enough information to reconstruct..." group in /// add it to the "We don't have enough information to reconstruct..." group in
/// the match below. /// the match below.
pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool { pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool {
use crate::dep_graph::DepKind;
// We must avoid ever having to call `force_from_dep_node()` for a // We must avoid ever having to call `force_from_dep_node()` for a
// `DepNode::codegen_unit`: // `DepNode::codegen_unit`:
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
@ -166,7 +164,7 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool
// hit the cache instead of having to go through `force_from_dep_node`. // hit the cache instead of having to go through `force_from_dep_node`.
// This assertion makes sure, we actually keep applying the solution above. // This assertion makes sure, we actually keep applying the solution above.
debug_assert!( debug_assert!(
dep_node.kind != DepKind::codegen_unit, dep_node.kind != crate::dep_graph::DepKind::codegen_unit,
"calling force_from_dep_node() on DepKind::codegen_unit" "calling force_from_dep_node() on DepKind::codegen_unit"
); );
@ -177,14 +175,14 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool
rustc_dep_node_force!([dep_node, tcx] rustc_dep_node_force!([dep_node, tcx]
// These are inputs that are expected to be pre-allocated and that // These are inputs that are expected to be pre-allocated and that
// should therefore always be red or green already. // should therefore always be red or green already.
DepKind::CrateMetadata | crate::dep_graph::DepKind::CrateMetadata |
// These are anonymous nodes. // These are anonymous nodes.
DepKind::TraitSelect | crate::dep_graph::DepKind::TraitSelect |
// We don't have enough information to reconstruct the query key of // We don't have enough information to reconstruct the query key of
// these. // these.
DepKind::CompileCodegenUnit => { crate::dep_graph::DepKind::CompileCodegenUnit => {
bug!("force_from_dep_node: encountered {:?}", dep_node) bug!("force_from_dep_node: encountered {:?}", dep_node)
} }
); );
@ -192,15 +190,6 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool
false false
} }
impl DepNode { pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
/// Check whether the query invocation corresponding to the given rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx)
/// DepNode is eligible for on-disk-caching. If so, this is method
/// will execute the query corresponding to the given DepNode.
/// Also, as a sanity check, it expects that the corresponding query
/// invocation has been marked as green already.
pub fn try_load_from_on_disk_cache<'tcx>(&self, tcx: TyCtxt<'tcx>) {
use crate::dep_graph::DepKind;
rustc_dep_node_try_load_from_on_disk_cache!(self, tcx)
}
} }

View File

@ -35,7 +35,7 @@
use graphviz as dot; use graphviz as dot;
use rustc::dep_graph::debug::{DepNodeFilter, EdgeFilter}; use rustc::dep_graph::debug::{DepNodeFilter, EdgeFilter};
use rustc::dep_graph::{DepGraphQuery, DepKind, DepNode}; use rustc::dep_graph::{DepGraphQuery, DepKind, DepNode, DepNodeExt};
use rustc::hir::map::Map; use rustc::hir::map::Map;
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
use rustc_ast::ast; use rustc_ast::ast;

View File

@ -13,7 +13,7 @@
//! Errors are reported if we are in the suitable configuration but //! Errors are reported if we are in the suitable configuration but
//! the required condition is not met. //! the required condition is not met.
use rustc::dep_graph::{label_strs, DepNode}; use rustc::dep_graph::{label_strs, DepNode, DepNodeExt};
use rustc::hir::map::Map; use rustc::hir::map::Map;
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
use rustc_ast::ast::{self, Attribute, NestedMetaItem}; use rustc_ast::ast::{self, Attribute, NestedMetaItem};

View File

@ -429,14 +429,14 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
}); });
try_load_from_on_disk_cache_stream.extend(quote! { try_load_from_on_disk_cache_stream.extend(quote! {
DepKind::#name => { ::rustc::dep_graph::DepKind::#name => {
if <#arg as DepNodeParams>::CAN_RECONSTRUCT_QUERY_KEY { if <#arg as DepNodeParams<TyCtxt<'_>>>::CAN_RECONSTRUCT_QUERY_KEY {
debug_assert!($tcx.dep_graph debug_assert!($tcx.dep_graph
.node_color($dep_node) .node_color($dep_node)
.map(|c| c.is_green()) .map(|c| c.is_green())
.unwrap_or(false)); .unwrap_or(false));
let key = <#arg as DepNodeParams>::recover($tcx, $dep_node).unwrap(); let key = <#arg as DepNodeParams<TyCtxt<'_>>>::recover($tcx, $dep_node).unwrap();
if queries::#name::cache_on_disk($tcx, key, None) { if queries::#name::cache_on_disk($tcx, key, None) {
let _ = $tcx.#name(key); let _ = $tcx.#name(key);
} }
@ -486,9 +486,9 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
// Add a match arm to force the query given the dep node // Add a match arm to force the query given the dep node
dep_node_force_stream.extend(quote! { dep_node_force_stream.extend(quote! {
DepKind::#name => { ::rustc::dep_graph::DepKind::#name => {
if <#arg as DepNodeParams>::CAN_RECONSTRUCT_QUERY_KEY { if <#arg as DepNodeParams<TyCtxt<'_>>>::CAN_RECONSTRUCT_QUERY_KEY {
if let Some(key) = <#arg as DepNodeParams>::recover($tcx, $dep_node) { if let Some(key) = <#arg as DepNodeParams<TyCtxt<'_>>>::recover($tcx, $dep_node) {
$tcx.force_query::<crate::ty::query::queries::#name<'_>>( $tcx.force_query::<crate::ty::query::queries::#name<'_>>(
key, key,
DUMMY_SP, DUMMY_SP,
@ -509,7 +509,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
} }
dep_node_force_stream.extend(quote! { dep_node_force_stream.extend(quote! {
DepKind::Null => { ::rustc::dep_graph::DepKind::Null => {
bug!("Cannot force dep node: {:?}", $dep_node) bug!("Cannot force dep node: {:?}", $dep_node)
} }
}); });

View File

@ -4,7 +4,7 @@ use crate::creader::CrateMetadataRef;
use crate::rmeta::table::{FixedSizeEncoding, Table}; use crate::rmeta::table::{FixedSizeEncoding, Table};
use crate::rmeta::*; use crate::rmeta::*;
use rustc::dep_graph::{self, DepNode, DepNodeIndex}; use rustc::dep_graph::{self, DepNode, DepNodeExt, DepNodeIndex};
use rustc::hir::exports::Export; use rustc::hir::exports::Export;
use rustc::middle::cstore::{CrateSource, ExternCrate}; use rustc::middle::cstore::{CrateSource, ExternCrate};
use rustc::middle::cstore::{ForeignModule, LinkagePreference, NativeLibrary}; use rustc::middle::cstore::{ForeignModule, LinkagePreference, NativeLibrary};

View File

@ -0,0 +1,22 @@
[package]
authors = ["The Rust Project Developers"]
name = "rustc_query_system"
version = "0.0.0"
edition = "2018"
[lib]
name = "rustc_query_system"
path = "lib.rs"
doctest = false
[dependencies]
log = { version = "0.4", features = ["release_max_level_info", "std"] }
rustc_ast = { path = "../librustc_ast" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_errors = { path = "../librustc_errors" }
rustc_hir = { path = "../librustc_hir" }
rustc_index = { path = "../librustc_index" }
rustc_macros = { path = "../librustc_macros" }
rustc_serialize = { path = "../libserialize", package = "serialize" }
parking_lot = "0.9"
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

View File

@ -1,6 +1,6 @@
//! Code for debugging the dep-graph. //! Code for debugging the dep-graph.
use super::dep_node::DepNode; use super::{DepKind, DepNode};
use std::error::Error; use std::error::Error;
/// A dep-node filter goes from a user-defined string to a query over /// A dep-node filter goes from a user-defined string to a query over
@ -26,7 +26,7 @@ impl DepNodeFilter {
} }
/// Tests whether `node` meets the filter, returning true if so. /// Tests whether `node` meets the filter, returning true if so.
pub fn test(&self, node: &DepNode) -> bool { pub fn test<K: DepKind>(&self, node: &DepNode<K>) -> bool {
let debug_str = format!("{:?}", node); let debug_str = format!("{:?}", node);
self.text.split('&').map(|s| s.trim()).all(|f| debug_str.contains(f)) self.text.split('&').map(|s| s.trim()).all(|f| debug_str.contains(f))
} }
@ -52,7 +52,7 @@ impl EdgeFilter {
} }
} }
pub fn test(&self, source: &DepNode, target: &DepNode) -> bool { pub fn test<K: DepKind>(&self, source: &DepNode<K>, target: &DepNode<K>) -> bool {
self.source.test(source) && self.target.test(target) self.source.test(source) && self.target.test(target)
} }
} }

View File

@ -0,0 +1,146 @@
//! This module defines the `DepNode` type which the compiler uses to represent
//! nodes in the dependency graph. A `DepNode` consists of a `DepKind` (which
//! specifies the kind of thing it represents, like a piece of HIR, MIR, etc)
//! and a `Fingerprint`, a 128 bit hash value the exact meaning of which
//! depends on the node's `DepKind`. Together, the kind and the fingerprint
//! fully identify a dependency node, even across multiple compilation sessions.
//! In other words, the value of the fingerprint does not depend on anything
//! that is specific to a given compilation session, like an unpredictable
//! interning key (e.g., NodeId, DefId, Symbol) or the numeric value of a
//! pointer. The concept behind this could be compared to how git commit hashes
//! uniquely identify a given commit and has a few advantages:
//!
//! * A `DepNode` can simply be serialized to disk and loaded in another session
//! without the need to do any "rebasing (like we have to do for Spans and
//! NodeIds) or "retracing" like we had to do for `DefId` in earlier
//! implementations of the dependency graph.
//! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
//! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
//! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
//! memory without any post-processing (e.g., "abomination-style" pointer
//! reconstruction).
//! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
//! refer to things that do not exist anymore. In previous implementations
//! `DepNode` contained a `DefId`. A `DepNode` referring to something that
//! had been removed between the previous and the current compilation session
//! could not be instantiated because the current compilation session
//! contained no `DefId` for thing that had been removed.
//!
//! `DepNode` definition happens in `librustc` with the `define_dep_nodes!()` macro.
//! This macro defines the `DepKind` enum and a corresponding `DepConstructor` enum. The
//! `DepConstructor` enum links a `DepKind` to the parameters that are needed at runtime in order
//! to construct a valid `DepNode` fingerprint.
//!
//! Because the macro sees what parameters a given `DepKind` requires, it can
//! "infer" some properties for each kind of `DepNode`:
//!
//! * Whether a `DepNode` of a given kind has any parameters at all. Some
//! `DepNode`s could represent global concepts with only one value.
//! * Whether it is possible, in principle, to reconstruct a query key from a
//! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
//! in which case it is possible to map the node's fingerprint back to the
//! `DefId` it was computed from. In other cases, too much information gets
//! lost during fingerprint computation.
use super::{DepContext, DepKind};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_macros::HashStable_Generic;
use std::fmt;
use std::hash::Hash;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct DepNode<K> {
pub kind: K,
pub hash: Fingerprint,
}
impl<K: DepKind> DepNode<K> {
/// Creates a new, parameterless DepNode. This method will assert
/// that the DepNode corresponding to the given DepKind actually
/// does not require any parameters.
pub fn new_no_params(kind: K) -> DepNode<K> {
debug_assert!(!kind.has_params());
DepNode { kind, hash: Fingerprint::ZERO }
}
}
impl<K: DepKind> fmt::Debug for DepNode<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
K::debug_node(self, f)
}
}
pub trait DepNodeParams<Ctxt: DepContext>: fmt::Debug + Sized {
const CAN_RECONSTRUCT_QUERY_KEY: bool;
/// This method turns the parameters of a DepNodeConstructor into an opaque
/// Fingerprint to be used in DepNode.
/// Not all DepNodeParams support being turned into a Fingerprint (they
/// don't need to if the corresponding DepNode is anonymous).
fn to_fingerprint(&self, _: Ctxt) -> Fingerprint {
panic!("Not implemented. Accidentally called on anonymous node?")
}
fn to_debug_str(&self, _: Ctxt) -> String {
format!("{:?}", self)
}
/// This method tries to recover the query key from the given `DepNode`,
/// something which is needed when forcing `DepNode`s during red-green
/// evaluation. The query system will only call this method if
/// `CAN_RECONSTRUCT_QUERY_KEY` is `true`.
/// It is always valid to return `None` here, in which case incremental
/// compilation will treat the query as having changed instead of forcing it.
fn recover(tcx: Ctxt, dep_node: &DepNode<Ctxt::DepKind>) -> Option<Self>;
}
impl<Ctxt: DepContext, T> DepNodeParams<Ctxt> for T
where
T: HashStable<Ctxt::StableHashingContext> + fmt::Debug,
{
default const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
default fn to_fingerprint(&self, tcx: Ctxt) -> Fingerprint {
let mut hcx = tcx.create_stable_hashing_context();
let mut hasher = StableHasher::new();
self.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
}
default fn to_debug_str(&self, _: Ctxt) -> String {
format!("{:?}", *self)
}
default fn recover(_: Ctxt, _: &DepNode<Ctxt::DepKind>) -> Option<Self> {
None
}
}
/// A "work product" corresponds to a `.o` (or other) file that we
/// save in between runs. These IDs do not have a `DefId` but rather
/// some independent path or string that persists between runs without
/// the need to be mapped or unmapped. (This ensures we can serialize
/// them even in the absence of a tcx.)
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
#[derive(HashStable_Generic)]
pub struct WorkProductId {
hash: Fingerprint,
}
impl WorkProductId {
pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
let mut hasher = StableHasher::new();
cgu_name.len().hash(&mut hasher);
cgu_name.hash(&mut hasher);
WorkProductId { hash: hasher.finish() }
}
pub fn from_fingerprint(fingerprint: Fingerprint) -> WorkProductId {
WorkProductId { hash: fingerprint }
}
}

View File

@ -1,32 +1,33 @@
use crate::ty::{self, TyCtxt}; use rustc_data_structures::fingerprint::Fingerprint;
use parking_lot::{Condvar, Mutex};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::profiling::QueryInvocationId; use rustc_data_structures::profiling::QueryInvocationId;
use rustc_data_structures::sharded::{self, Sharded}; use rustc_data_structures::sharded::{self, Sharded};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc, Ordering}; use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc, Ordering};
use rustc_data_structures::unlikely;
use rustc_errors::Diagnostic; use rustc_errors::Diagnostic;
use rustc_hir::def_id::DefId;
use rustc_index::vec::{Idx, IndexVec}; use rustc_index::vec::{Idx, IndexVec};
use smallvec::SmallVec;
use parking_lot::{Condvar, Mutex};
use smallvec::{smallvec, SmallVec};
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::env; use std::env;
use std::hash::Hash; use std::hash::Hash;
use std::marker::PhantomData;
use std::mem; use std::mem;
use std::sync::atomic::Ordering::Relaxed; use std::sync::atomic::Ordering::Relaxed;
use crate::ich::{Fingerprint, StableHashingContext, StableHashingContextProvider};
use super::debug::EdgeFilter; use super::debug::EdgeFilter;
use super::dep_node::{DepKind, DepNode, WorkProductId};
use super::prev::PreviousDepGraph; use super::prev::PreviousDepGraph;
use super::query::DepGraphQuery; use super::query::DepGraphQuery;
use super::safe::DepGraphSafe; use super::safe::DepGraphSafe;
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex}; use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
use super::{DepContext, DepKind, DepNode, WorkProductId};
use crate::{HashStableContext, HashStableContextProvider};
#[derive(Clone)] #[derive(Clone)]
pub struct DepGraph { pub struct DepGraph<K: DepKind> {
data: Option<Lrc<DepGraphData>>, data: Option<Lrc<DepGraphData<K>>>,
/// This field is used for assigning DepNodeIndices when running in /// This field is used for assigning DepNodeIndices when running in
/// non-incremental mode. Even in non-incremental mode we make sure that /// non-incremental mode. Even in non-incremental mode we make sure that
@ -65,16 +66,16 @@ impl DepNodeColor {
} }
} }
struct DepGraphData { struct DepGraphData<K: DepKind> {
/// The new encoding of the dependency graph, optimized for red/green /// The new encoding of the dependency graph, optimized for red/green
/// tracking. The `current` field is the dependency graph of only the /// tracking. The `current` field is the dependency graph of only the
/// current compilation session: We don't merge the previous dep-graph into /// current compilation session: We don't merge the previous dep-graph into
/// current one anymore. /// current one anymore.
current: CurrentDepGraph, current: CurrentDepGraph<K>,
/// The dep-graph from the previous compilation session. It contains all /// The dep-graph from the previous compilation session. It contains all
/// nodes and edges as well as all fingerprints of nodes that have them. /// nodes and edges as well as all fingerprints of nodes that have them.
previous: PreviousDepGraph, previous: PreviousDepGraph<K>,
colors: DepNodeColorMap, colors: DepNodeColorMap,
@ -90,12 +91,12 @@ struct DepGraphData {
/// this map. We can later look for and extract that data. /// this map. We can later look for and extract that data.
previous_work_products: FxHashMap<WorkProductId, WorkProduct>, previous_work_products: FxHashMap<WorkProductId, WorkProduct>,
dep_node_debug: Lock<FxHashMap<DepNode, String>>, dep_node_debug: Lock<FxHashMap<DepNode<K>, String>>,
} }
pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Option<Fingerprint> pub fn hash_result<HashCtxt, R>(hcx: &mut HashCtxt, result: &R) -> Option<Fingerprint>
where where
R: for<'a> HashStable<StableHashingContext<'a>>, R: HashStable<HashCtxt>,
{ {
let mut stable_hasher = StableHasher::new(); let mut stable_hasher = StableHasher::new();
result.hash_stable(hcx, &mut stable_hasher); result.hash_stable(hcx, &mut stable_hasher);
@ -103,11 +104,11 @@ where
Some(stable_hasher.finish()) Some(stable_hasher.finish())
} }
impl DepGraph { impl<K: DepKind> DepGraph<K> {
pub fn new( pub fn new(
prev_graph: PreviousDepGraph, prev_graph: PreviousDepGraph<K>,
prev_work_products: FxHashMap<WorkProductId, WorkProduct>, prev_work_products: FxHashMap<WorkProductId, WorkProduct>,
) -> DepGraph { ) -> DepGraph<K> {
let prev_graph_node_count = prev_graph.node_count(); let prev_graph_node_count = prev_graph.node_count();
DepGraph { DepGraph {
@ -124,7 +125,7 @@ impl DepGraph {
} }
} }
pub fn new_disabled() -> DepGraph { pub fn new_disabled() -> DepGraph<K> {
DepGraph { data: None, virtual_dep_node_index: Lrc::new(AtomicU32::new(0)) } DepGraph { data: None, virtual_dep_node_index: Lrc::new(AtomicU32::new(0)) }
} }
@ -134,7 +135,7 @@ impl DepGraph {
self.data.is_some() self.data.is_some()
} }
pub fn query(&self) -> DepGraphQuery { pub fn query(&self) -> DepGraphQuery<K> {
let data = self.data.as_ref().unwrap().current.data.lock(); let data = self.data.as_ref().unwrap().current.data.lock();
let nodes: Vec<_> = data.iter().map(|n| n.node).collect(); let nodes: Vec<_> = data.iter().map(|n| n.node).collect();
let mut edges = Vec::new(); let mut edges = Vec::new();
@ -150,9 +151,8 @@ impl DepGraph {
pub fn assert_ignored(&self) { pub fn assert_ignored(&self) {
if let Some(..) = self.data { if let Some(..) = self.data {
ty::tls::with_context_opt(|icx| { K::read_deps(|task_deps| {
let icx = if let Some(icx) = icx { icx } else { return }; assert!(task_deps.is_none(), "expected no task dependency tracking");
assert!(icx.task_deps.is_none(), "expected no task dependency tracking");
}) })
} }
} }
@ -161,11 +161,7 @@ impl DepGraph {
where where
OP: FnOnce() -> R, OP: FnOnce() -> R,
{ {
ty::tls::with_context(|icx| { K::with_deps(None, op)
let icx = ty::tls::ImplicitCtxt { task_deps: None, ..icx.clone() };
ty::tls::enter_context(&icx, |_| op())
})
} }
/// Starts a new dep-graph task. Dep-graph tasks are specified /// Starts a new dep-graph task. Dep-graph tasks are specified
@ -195,16 +191,17 @@ impl DepGraph {
/// `arg` parameter. /// `arg` parameter.
/// ///
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/incremental-compilation.html /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/incremental-compilation.html
pub fn with_task<'a, C, A, R>( pub fn with_task<H, C, A, R>(
&self, &self,
key: DepNode, key: DepNode<K>,
cx: C, cx: C,
arg: A, arg: A,
task: fn(C, A) -> R, task: fn(C, A) -> R,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>, hash_result: impl FnOnce(&mut H, &R) -> Option<Fingerprint>,
) -> (R, DepNodeIndex) ) -> (R, DepNodeIndex)
where where
C: DepGraphSafe + StableHashingContextProvider<'a>, C: DepGraphSafe + HashStableContextProvider<H>,
H: HashStableContext,
{ {
self.with_task_impl( self.with_task_impl(
key, key,
@ -218,6 +215,7 @@ impl DepGraph {
node: Some(_key), node: Some(_key),
reads: SmallVec::new(), reads: SmallVec::new(),
read_set: Default::default(), read_set: Default::default(),
phantom_data: PhantomData,
}) })
}, },
|data, key, fingerprint, task| data.complete_task(key, task.unwrap(), fingerprint), |data, key, fingerprint, task| data.complete_task(key, task.unwrap(), fingerprint),
@ -225,24 +223,25 @@ impl DepGraph {
) )
} }
fn with_task_impl<'a, C, A, R>( fn with_task_impl<H, C, A, R>(
&self, &self,
key: DepNode, key: DepNode<K>,
cx: C, cx: C,
arg: A, arg: A,
no_tcx: bool, no_tcx: bool,
task: fn(C, A) -> R, task: fn(C, A) -> R,
create_task: fn(DepNode) -> Option<TaskDeps>, create_task: fn(DepNode<K>) -> Option<TaskDeps<K>>,
finish_task_and_alloc_depnode: fn( finish_task_and_alloc_depnode: fn(
&CurrentDepGraph, &CurrentDepGraph<K>,
DepNode, DepNode<K>,
Fingerprint, Fingerprint,
Option<TaskDeps>, Option<TaskDeps<K>>,
) -> DepNodeIndex, ) -> DepNodeIndex,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>, hash_result: impl FnOnce(&mut H, &R) -> Option<Fingerprint>,
) -> (R, DepNodeIndex) ) -> (R, DepNodeIndex)
where where
C: DepGraphSafe + StableHashingContextProvider<'a>, C: DepGraphSafe + HashStableContextProvider<H>,
H: HashStableContext,
{ {
if let Some(ref data) = self.data { if let Some(ref data) = self.data {
let task_deps = create_task(key).map(Lock::new); let task_deps = create_task(key).map(Lock::new);
@ -257,12 +256,7 @@ impl DepGraph {
let result = if no_tcx { let result = if no_tcx {
task(cx, arg) task(cx, arg)
} else { } else {
ty::tls::with_context(|icx| { K::with_deps(task_deps.as_ref(), || task(cx, arg))
let icx =
ty::tls::ImplicitCtxt { task_deps: task_deps.as_ref(), ..icx.clone() };
ty::tls::enter_context(&icx, |_| task(cx, arg))
})
}; };
let current_fingerprint = hash_result(&mut hcx, &result); let current_fingerprint = hash_result(&mut hcx, &result);
@ -274,7 +268,7 @@ impl DepGraph {
task_deps.map(|lock| lock.into_inner()), task_deps.map(|lock| lock.into_inner()),
); );
let print_status = cfg!(debug_assertions) && hcx.sess().opts.debugging_opts.dep_tasks; let print_status = cfg!(debug_assertions) && hcx.debug_dep_tasks();
// Determine the color of the new DepNode. // Determine the color of the new DepNode.
if let Some(prev_index) = data.previous.node_to_index_opt(&key) { if let Some(prev_index) = data.previous.node_to_index_opt(&key) {
@ -322,22 +316,16 @@ impl DepGraph {
/// Executes something within an "anonymous" task, that is, a task the /// Executes something within an "anonymous" task, that is, a task the
/// `DepNode` of which is determined by the list of inputs it read from. /// `DepNode` of which is determined by the list of inputs it read from.
pub fn with_anon_task<OP, R>(&self, dep_kind: DepKind, op: OP) -> (R, DepNodeIndex) pub fn with_anon_task<OP, R>(&self, dep_kind: K, op: OP) -> (R, DepNodeIndex)
where where
OP: FnOnce() -> R, OP: FnOnce() -> R,
{ {
if let Some(ref data) = self.data { if let Some(ref data) = self.data {
let (result, task_deps) = ty::tls::with_context(|icx| { let task_deps = Lock::new(TaskDeps::default());
let task_deps = Lock::new(TaskDeps::default());
let r = { let result = K::with_deps(Some(&task_deps), op);
let icx = ty::tls::ImplicitCtxt { task_deps: Some(&task_deps), ..icx.clone() }; let task_deps = task_deps.into_inner();
ty::tls::enter_context(&icx, |_| op())
};
(r, task_deps.into_inner())
});
let dep_node_index = data.current.complete_anon_task(dep_kind, task_deps); let dep_node_index = data.current.complete_anon_task(dep_kind, task_deps);
(result, dep_node_index) (result, dep_node_index)
} else { } else {
@ -347,16 +335,17 @@ impl DepGraph {
/// Executes something within an "eval-always" task which is a task /// Executes something within an "eval-always" task which is a task
/// that runs whenever anything changes. /// that runs whenever anything changes.
pub fn with_eval_always_task<'a, C, A, R>( pub fn with_eval_always_task<H, C, A, R>(
&self, &self,
key: DepNode, key: DepNode<K>,
cx: C, cx: C,
arg: A, arg: A,
task: fn(C, A) -> R, task: fn(C, A) -> R,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>, hash_result: impl FnOnce(&mut H, &R) -> Option<Fingerprint>,
) -> (R, DepNodeIndex) ) -> (R, DepNodeIndex)
where where
C: DepGraphSafe + StableHashingContextProvider<'a>, C: DepGraphSafe + HashStableContextProvider<H>,
H: HashStableContext,
{ {
self.with_task_impl( self.with_task_impl(
key, key,
@ -371,14 +360,14 @@ impl DepGraph {
} }
#[inline] #[inline]
pub fn read(&self, v: DepNode) { pub fn read(&self, v: DepNode<K>) {
if let Some(ref data) = self.data { if let Some(ref data) = self.data {
let map = data.current.node_to_node_index.get_shard_by_value(&v).lock(); let map = data.current.node_to_node_index.get_shard_by_value(&v).lock();
if let Some(dep_node_index) = map.get(&v).copied() { if let Some(dep_node_index) = map.get(&v).copied() {
std::mem::drop(map); std::mem::drop(map);
data.read_index(dep_node_index); data.read_index(dep_node_index);
} else { } else {
bug!("DepKind {:?} should be pre-allocated but isn't.", v.kind) panic!("DepKind {:?} should be pre-allocated but isn't.", v.kind)
} }
} }
} }
@ -391,7 +380,7 @@ impl DepGraph {
} }
#[inline] #[inline]
pub fn dep_node_index_of(&self, dep_node: &DepNode) -> DepNodeIndex { pub fn dep_node_index_of(&self, dep_node: &DepNode<K>) -> DepNodeIndex {
self.data self.data
.as_ref() .as_ref()
.unwrap() .unwrap()
@ -405,7 +394,7 @@ impl DepGraph {
} }
#[inline] #[inline]
pub fn dep_node_exists(&self, dep_node: &DepNode) -> bool { pub fn dep_node_exists(&self, dep_node: &DepNode<K>) -> bool {
if let Some(ref data) = self.data { if let Some(ref data) = self.data {
data.current data.current
.node_to_node_index .node_to_node_index
@ -423,12 +412,12 @@ impl DepGraph {
data[dep_node_index].fingerprint data[dep_node_index].fingerprint
} }
pub fn prev_fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> { pub fn prev_fingerprint_of(&self, dep_node: &DepNode<K>) -> Option<Fingerprint> {
self.data.as_ref().unwrap().previous.fingerprint_of(dep_node) self.data.as_ref().unwrap().previous.fingerprint_of(dep_node)
} }
#[inline] #[inline]
pub fn prev_dep_node_index_of(&self, dep_node: &DepNode) -> SerializedDepNodeIndex { pub fn prev_dep_node_index_of(&self, dep_node: &DepNode<K>) -> SerializedDepNodeIndex {
self.data.as_ref().unwrap().previous.node_to_index(dep_node) self.data.as_ref().unwrap().previous.node_to_index(dep_node)
} }
@ -445,7 +434,7 @@ impl DepGraph {
} }
#[inline(always)] #[inline(always)]
pub fn register_dep_node_debug_str<F>(&self, dep_node: DepNode, debug_str_gen: F) pub fn register_dep_node_debug_str<F>(&self, dep_node: DepNode<K>, debug_str_gen: F)
where where
F: FnOnce() -> String, F: FnOnce() -> String,
{ {
@ -458,7 +447,7 @@ impl DepGraph {
dep_node_debug.borrow_mut().insert(dep_node, debug_str); dep_node_debug.borrow_mut().insert(dep_node, debug_str);
} }
pub(super) fn dep_node_debug_str(&self, dep_node: DepNode) -> Option<String> { pub fn dep_node_debug_str(&self, dep_node: DepNode<K>) -> Option<String> {
self.data.as_ref()?.dep_node_debug.borrow().get(&dep_node).cloned() self.data.as_ref()?.dep_node_debug.borrow().get(&dep_node).cloned()
} }
@ -475,7 +464,7 @@ impl DepGraph {
} }
} }
pub fn serialize(&self) -> SerializedDepGraph { pub fn serialize(&self) -> SerializedDepGraph<K> {
let data = self.data.as_ref().unwrap().current.data.lock(); let data = self.data.as_ref().unwrap().current.data.lock();
let fingerprints: IndexVec<SerializedDepNodeIndex, _> = let fingerprints: IndexVec<SerializedDepNodeIndex, _> =
@ -503,7 +492,7 @@ impl DepGraph {
SerializedDepGraph { nodes, fingerprints, edge_list_indices, edge_list_data } SerializedDepGraph { nodes, fingerprints, edge_list_indices, edge_list_data }
} }
pub fn node_color(&self, dep_node: &DepNode) -> Option<DepNodeColor> { pub fn node_color(&self, dep_node: &DepNode<K>) -> Option<DepNodeColor> {
if let Some(ref data) = self.data { if let Some(ref data) = self.data {
if let Some(prev_index) = data.previous.node_to_index_opt(dep_node) { if let Some(prev_index) = data.previous.node_to_index_opt(dep_node) {
return data.colors.get(prev_index); return data.colors.get(prev_index);
@ -521,10 +510,10 @@ impl DepGraph {
/// A node will have an index, when it's already been marked green, or when we can mark it /// A node will have an index, when it's already been marked green, or when we can mark it
/// green. This function will mark the current task as a reader of the specified node, when /// green. This function will mark the current task as a reader of the specified node, when
/// a node index can be found for that node. /// a node index can be found for that node.
pub fn try_mark_green_and_read( pub fn try_mark_green_and_read<Ctxt: DepContext<DepKind = K>>(
&self, &self,
tcx: TyCtxt<'_>, tcx: Ctxt,
dep_node: &DepNode, dep_node: &DepNode<K>,
) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> { ) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> {
self.try_mark_green(tcx, dep_node).map(|(prev_index, dep_node_index)| { self.try_mark_green(tcx, dep_node).map(|(prev_index, dep_node_index)| {
debug_assert!(self.is_green(&dep_node)); debug_assert!(self.is_green(&dep_node));
@ -533,10 +522,10 @@ impl DepGraph {
}) })
} }
pub fn try_mark_green( pub fn try_mark_green<Ctxt: DepContext<DepKind = K>>(
&self, &self,
tcx: TyCtxt<'_>, tcx: Ctxt,
dep_node: &DepNode, dep_node: &DepNode<K>,
) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> { ) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> {
debug_assert!(!dep_node.kind.is_eval_always()); debug_assert!(!dep_node.kind.is_eval_always());
@ -561,12 +550,12 @@ impl DepGraph {
} }
/// Try to mark a dep-node which existed in the previous compilation session as green. /// Try to mark a dep-node which existed in the previous compilation session as green.
fn try_mark_previous_green<'tcx>( fn try_mark_previous_green<Ctxt: DepContext<DepKind = K>>(
&self, &self,
tcx: TyCtxt<'tcx>, tcx: Ctxt,
data: &DepGraphData, data: &DepGraphData<K>,
prev_dep_node_index: SerializedDepNodeIndex, prev_dep_node_index: SerializedDepNodeIndex,
dep_node: &DepNode, dep_node: &DepNode<K>,
) -> Option<DepNodeIndex> { ) -> Option<DepNodeIndex> {
debug!("try_mark_previous_green({:?}) - BEGIN", dep_node); debug!("try_mark_previous_green({:?}) - BEGIN", dep_node);
@ -648,50 +637,6 @@ impl DepGraph {
current_deps.push(node_index); current_deps.push(node_index);
continue; continue;
} }
} else {
// FIXME: This match is just a workaround for incremental bugs and should
// be removed. https://github.com/rust-lang/rust/issues/62649 is one such
// bug that must be fixed before removing this.
match dep_dep_node.kind {
DepKind::hir_owner
| DepKind::hir_owner_nodes
| DepKind::CrateMetadata => {
if let Some(def_id) = dep_dep_node.extract_def_id(tcx) {
if def_id_corresponds_to_hir_dep_node(tcx, def_id) {
if dep_dep_node.kind == DepKind::CrateMetadata {
// The `DefPath` has corresponding node,
// and that node should have been marked
// either red or green in `data.colors`.
bug!(
"DepNode {:?} should have been \
pre-marked as red or green but wasn't.",
dep_dep_node
);
}
} else {
// This `DefPath` does not have a
// corresponding `DepNode` (e.g. a
// struct field), and the ` DefPath`
// collided with the `DefPath` of a
// proper item that existed in the
// previous compilation session.
//
// Since the given `DefPath` does not
// denote the item that previously
// existed, we just fail to mark green.
return None;
}
} else {
// If the node does not exist anymore, we
// just fail to mark green.
return None;
}
}
_ => {
// For other kinds of nodes it's OK to be
// forced.
}
}
} }
// We failed to mark it green, so we try to force the query. // We failed to mark it green, so we try to force the query.
@ -700,7 +645,7 @@ impl DepGraph {
dependency {:?}", dependency {:?}",
dep_node, dep_dep_node dep_node, dep_dep_node
); );
if crate::ty::query::force_from_dep_node(tcx, dep_dep_node) { if tcx.try_force_from_dep_node(dep_dep_node) {
let dep_dep_node_color = data.colors.get(dep_dep_node_index); let dep_dep_node_color = data.colors.get(dep_dep_node_index);
match dep_dep_node_color { match dep_dep_node_color {
@ -721,8 +666,8 @@ impl DepGraph {
return None; return None;
} }
None => { None => {
if !tcx.sess.has_errors_or_delayed_span_bugs() { if !tcx.has_errors_or_delayed_span_bugs() {
bug!( panic!(
"try_mark_previous_green() - Forcing the DepNode \ "try_mark_previous_green() - Forcing the DepNode \
should have set its color" should have set its color"
) )
@ -779,7 +724,7 @@ impl DepGraph {
// FIXME: Store the fact that a node has diagnostics in a bit in the dep graph somewhere // FIXME: Store the fact that a node has diagnostics in a bit in the dep graph somewhere
// Maybe store a list on disk and encode this fact in the DepNodeState // Maybe store a list on disk and encode this fact in the DepNodeState
let diagnostics = tcx.queries.on_disk_cache.load_diagnostics(tcx, prev_dep_node_index); let diagnostics = tcx.load_diagnostics(prev_dep_node_index);
#[cfg(not(parallel_compiler))] #[cfg(not(parallel_compiler))]
debug_assert!( debug_assert!(
@ -805,10 +750,10 @@ impl DepGraph {
/// This may be called concurrently on multiple threads for the same dep node. /// This may be called concurrently on multiple threads for the same dep node.
#[cold] #[cold]
#[inline(never)] #[inline(never)]
fn emit_diagnostics<'tcx>( fn emit_diagnostics<Ctxt: DepContext<DepKind = K>>(
&self, &self,
tcx: TyCtxt<'tcx>, tcx: Ctxt,
data: &DepGraphData, data: &DepGraphData<K>,
dep_node_index: DepNodeIndex, dep_node_index: DepNodeIndex,
prev_dep_node_index: SerializedDepNodeIndex, prev_dep_node_index: SerializedDepNodeIndex,
diagnostics: Vec<Diagnostic>, diagnostics: Vec<Diagnostic>,
@ -827,9 +772,9 @@ impl DepGraph {
mem::drop(emitting); mem::drop(emitting);
// Promote the previous diagnostics to the current session. // Promote the previous diagnostics to the current session.
tcx.queries.on_disk_cache.store_diagnostics(dep_node_index, diagnostics.clone().into()); tcx.store_diagnostics(dep_node_index, diagnostics.clone().into());
let handle = tcx.sess.diagnostic(); let handle = tcx.diagnostic();
for diagnostic in diagnostics { for diagnostic in diagnostics {
handle.emit_diagnostic(&diagnostic); handle.emit_diagnostic(&diagnostic);
@ -858,7 +803,7 @@ impl DepGraph {
// Returns true if the given node has been marked as green during the // Returns true if the given node has been marked as green during the
// current compilation session. Used in various assertions // current compilation session. Used in various assertions
pub fn is_green(&self, dep_node: &DepNode) -> bool { pub fn is_green(&self, dep_node: &DepNode<K>) -> bool {
self.node_color(dep_node).map(|c| c.is_green()).unwrap_or(false) self.node_color(dep_node).map(|c| c.is_green()).unwrap_or(false)
} }
@ -870,15 +815,15 @@ impl DepGraph {
// //
// This method will only load queries that will end up in the disk cache. // This method will only load queries that will end up in the disk cache.
// Other queries will not be executed. // Other queries will not be executed.
pub fn exec_cache_promotions(&self, tcx: TyCtxt<'_>) { pub fn exec_cache_promotions<Ctxt: DepContext<DepKind = K>>(&self, tcx: Ctxt) {
let _prof_timer = tcx.prof.generic_activity("incr_comp_query_cache_promotion"); let _prof_timer = tcx.profiler().generic_activity("incr_comp_query_cache_promotion");
let data = self.data.as_ref().unwrap(); let data = self.data.as_ref().unwrap();
for prev_index in data.colors.values.indices() { for prev_index in data.colors.values.indices() {
match data.colors.get(prev_index) { match data.colors.get(prev_index) {
Some(DepNodeColor::Green(_)) => { Some(DepNodeColor::Green(_)) => {
let dep_node = data.previous.index_to_node(prev_index); let dep_node = data.previous.index_to_node(prev_index);
dep_node.try_load_from_on_disk_cache(tcx); tcx.try_load_from_on_disk_cache(&dep_node);
} }
None | Some(DepNodeColor::Red) => { None | Some(DepNodeColor::Red) => {
// We can skip red nodes because a node can only be marked // We can skip red nodes because a node can only be marked
@ -895,11 +840,6 @@ impl DepGraph {
} }
} }
fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
def_id.index == hir_id.owner.local_def_index
}
/// A "work product" is an intermediate result that we save into the /// A "work product" is an intermediate result that we save into the
/// incremental directory for later re-use. The primary example are /// incremental directory for later re-use. The primary example are
/// the object files that we save for each partition at code /// the object files that we save for each partition at code
@ -946,8 +886,8 @@ pub enum WorkProductFileKind {
} }
#[derive(Clone)] #[derive(Clone)]
struct DepNodeData { struct DepNodeData<K> {
node: DepNode, node: DepNode<K>,
edges: EdgesVec, edges: EdgesVec,
fingerprint: Fingerprint, fingerprint: Fingerprint,
} }
@ -967,9 +907,9 @@ struct DepNodeData {
/// The only operation that must manipulate both locks is adding new nodes, in which case /// The only operation that must manipulate both locks is adding new nodes, in which case
/// we first acquire the `node_to_node_index` lock and then, once a new node is to be inserted, /// we first acquire the `node_to_node_index` lock and then, once a new node is to be inserted,
/// acquire the lock on `data.` /// acquire the lock on `data.`
pub(super) struct CurrentDepGraph { pub(super) struct CurrentDepGraph<K> {
data: Lock<IndexVec<DepNodeIndex, DepNodeData>>, data: Lock<IndexVec<DepNodeIndex, DepNodeData<K>>>,
node_to_node_index: Sharded<FxHashMap<DepNode, DepNodeIndex>>, node_to_node_index: Sharded<FxHashMap<DepNode<K>, DepNodeIndex>>,
/// Used to trap when a specific edge is added to the graph. /// Used to trap when a specific edge is added to the graph.
/// This is used for debug purposes and is only active with `debug_assertions`. /// This is used for debug purposes and is only active with `debug_assertions`.
@ -995,8 +935,8 @@ pub(super) struct CurrentDepGraph {
total_duplicate_read_count: AtomicU64, total_duplicate_read_count: AtomicU64,
} }
impl CurrentDepGraph { impl<K: DepKind> CurrentDepGraph<K> {
fn new(prev_graph_node_count: usize) -> CurrentDepGraph { fn new(prev_graph_node_count: usize) -> CurrentDepGraph<K> {
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
@ -1008,7 +948,7 @@ impl CurrentDepGraph {
match env::var("RUST_FORBID_DEP_GRAPH_EDGE") { match env::var("RUST_FORBID_DEP_GRAPH_EDGE") {
Ok(s) => match EdgeFilter::new(&s) { Ok(s) => match EdgeFilter::new(&s) {
Ok(f) => Some(f), Ok(f) => Some(f),
Err(err) => bug!("RUST_FORBID_DEP_GRAPH_EDGE invalid: {}", err), Err(err) => panic!("RUST_FORBID_DEP_GRAPH_EDGE invalid: {}", err),
}, },
Err(_) => None, Err(_) => None,
} }
@ -1039,14 +979,14 @@ impl CurrentDepGraph {
fn complete_task( fn complete_task(
&self, &self,
node: DepNode, node: DepNode<K>,
task_deps: TaskDeps, task_deps: TaskDeps<K>,
fingerprint: Fingerprint, fingerprint: Fingerprint,
) -> DepNodeIndex { ) -> DepNodeIndex {
self.alloc_node(node, task_deps.reads, fingerprint) self.alloc_node(node, task_deps.reads, fingerprint)
} }
fn complete_anon_task(&self, kind: DepKind, task_deps: TaskDeps) -> DepNodeIndex { fn complete_anon_task(&self, kind: K, task_deps: TaskDeps<K>) -> DepNodeIndex {
debug_assert!(!kind.is_eval_always()); debug_assert!(!kind.is_eval_always());
let mut hasher = StableHasher::new(); let mut hasher = StableHasher::new();
@ -1072,7 +1012,7 @@ impl CurrentDepGraph {
fn alloc_node( fn alloc_node(
&self, &self,
dep_node: DepNode, dep_node: DepNode<K>,
edges: EdgesVec, edges: EdgesVec,
fingerprint: Fingerprint, fingerprint: Fingerprint,
) -> DepNodeIndex { ) -> DepNodeIndex {
@ -1084,7 +1024,7 @@ impl CurrentDepGraph {
fn intern_node( fn intern_node(
&self, &self,
dep_node: DepNode, dep_node: DepNode<K>,
edges: EdgesVec, edges: EdgesVec,
fingerprint: Fingerprint, fingerprint: Fingerprint,
) -> DepNodeIndex { ) -> DepNodeIndex {
@ -1101,12 +1041,11 @@ impl CurrentDepGraph {
} }
} }
impl DepGraphData { impl<K: DepKind> DepGraphData<K> {
#[inline(never)] #[inline(never)]
fn read_index(&self, source: DepNodeIndex) { fn read_index(&self, source: DepNodeIndex) {
ty::tls::with_context_opt(|icx| { K::read_deps(|task_deps| {
let icx = if let Some(icx) = icx { icx } else { return }; if let Some(task_deps) = task_deps {
if let Some(task_deps) = icx.task_deps {
let mut task_deps = task_deps.lock(); let mut task_deps = task_deps.lock();
let task_deps = &mut *task_deps; let task_deps = &mut *task_deps;
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
@ -1135,7 +1074,7 @@ impl DepGraphData {
if let Some(ref forbidden_edge) = self.current.forbidden_edge { if let Some(ref forbidden_edge) = self.current.forbidden_edge {
let source = data[source].node; let source = data[source].node;
if forbidden_edge.test(&source, &target) { if forbidden_edge.test(&source, &target) {
bug!("forbidden edge {:?} -> {:?} created", source, target) panic!("forbidden edge {:?} -> {:?} created", source, target)
} }
} }
} }
@ -1151,12 +1090,25 @@ impl DepGraphData {
/// The capacity of the `reads` field `SmallVec` /// The capacity of the `reads` field `SmallVec`
const TASK_DEPS_READS_CAP: usize = 8; const TASK_DEPS_READS_CAP: usize = 8;
type EdgesVec = SmallVec<[DepNodeIndex; TASK_DEPS_READS_CAP]>; type EdgesVec = SmallVec<[DepNodeIndex; TASK_DEPS_READS_CAP]>;
#[derive(Default)]
pub struct TaskDeps { pub struct TaskDeps<K> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
node: Option<DepNode>, node: Option<DepNode<K>>,
reads: EdgesVec, reads: EdgesVec,
read_set: FxHashSet<DepNodeIndex>, read_set: FxHashSet<DepNodeIndex>,
phantom_data: PhantomData<DepNode<K>>,
}
impl<K> Default for TaskDeps<K> {
fn default() -> Self {
Self {
#[cfg(debug_assertions)]
node: None,
reads: EdgesVec::new(),
read_set: FxHashSet::default(),
phantom_data: PhantomData,
}
}
} }
// A data structure that stores Option<DepNodeColor> values as a contiguous // A data structure that stores Option<DepNodeColor> values as a contiguous

View File

@ -0,0 +1,75 @@
pub mod debug;
mod dep_node;
mod graph;
mod prev;
mod query;
mod safe;
mod serialized;
pub use dep_node::{DepNode, DepNodeParams, WorkProductId};
pub use graph::WorkProductFileKind;
pub use graph::{hash_result, DepGraph, DepNodeColor, DepNodeIndex, TaskDeps, WorkProduct};
pub use prev::PreviousDepGraph;
pub use query::DepGraphQuery;
pub use safe::AssertDepGraphSafe;
pub use safe::DepGraphSafe;
pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::Lock;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Diagnostic;
use std::fmt;
use std::hash::Hash;
pub trait DepContext: Copy {
type DepKind: self::DepKind;
type StableHashingContext: crate::HashStableContext;
/// Create a hashing context for hashing new results.
fn create_stable_hashing_context(&self) -> Self::StableHashingContext;
/// Try to force a dep node to execute and see if it's green.
fn try_force_from_dep_node(&self, dep_node: &DepNode<Self::DepKind>) -> bool;
/// Return whether the current session is tainted by errors.
fn has_errors_or_delayed_span_bugs(&self) -> bool;
/// Return the diagnostic handler.
fn diagnostic(&self) -> &rustc_errors::Handler;
/// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode<Self::DepKind>);
/// Load diagnostics associated to the node in the previous session.
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic>;
/// Register diagnostics for the given node, for use in next session.
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>);
/// Access the profiler.
fn profiler(&self) -> &SelfProfilerRef;
}
/// Describe the different families of dependency nodes.
pub trait DepKind: Copy + fmt::Debug + Eq + Ord + Hash {
/// Return whether this kind always require evaluation.
fn is_eval_always(&self) -> bool;
/// Return whether this kind requires additional parameters to be executed.
fn has_params(&self) -> bool;
/// Implementation of `std::fmt::Debug` for `DepNode`.
fn debug_node(node: &DepNode<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
/// Execute the operation with provided dependencies.
fn with_deps<OP, R>(deps: Option<&Lock<TaskDeps<Self>>>, op: OP) -> R
where
OP: FnOnce() -> R;
/// Access dependencies from current implicit context.
fn read_deps<OP>(op: OP) -> ()
where
OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps<Self>>>) -> ();
}

View File

@ -1,16 +1,22 @@
use super::dep_node::DepNode;
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex}; use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
use crate::ich::Fingerprint; use super::{DepKind, DepNode};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
#[derive(Debug, RustcEncodable, RustcDecodable, Default)] #[derive(Debug, RustcEncodable, RustcDecodable)]
pub struct PreviousDepGraph { pub struct PreviousDepGraph<K: DepKind> {
data: SerializedDepGraph, data: SerializedDepGraph<K>,
index: FxHashMap<DepNode, SerializedDepNodeIndex>, index: FxHashMap<DepNode<K>, SerializedDepNodeIndex>,
} }
impl PreviousDepGraph { impl<K: DepKind> Default for PreviousDepGraph<K> {
pub fn new(data: SerializedDepGraph) -> PreviousDepGraph { fn default() -> Self {
PreviousDepGraph { data: Default::default(), index: Default::default() }
}
}
impl<K: DepKind> PreviousDepGraph<K> {
pub fn new(data: SerializedDepGraph<K>) -> PreviousDepGraph<K> {
let index: FxHashMap<_, _> = let index: FxHashMap<_, _> =
data.nodes.iter_enumerated().map(|(idx, &dep_node)| (dep_node, idx)).collect(); data.nodes.iter_enumerated().map(|(idx, &dep_node)| (dep_node, idx)).collect();
PreviousDepGraph { data, index } PreviousDepGraph { data, index }
@ -25,22 +31,22 @@ impl PreviousDepGraph {
} }
#[inline] #[inline]
pub fn index_to_node(&self, dep_node_index: SerializedDepNodeIndex) -> DepNode { pub fn index_to_node(&self, dep_node_index: SerializedDepNodeIndex) -> DepNode<K> {
self.data.nodes[dep_node_index] self.data.nodes[dep_node_index]
} }
#[inline] #[inline]
pub fn node_to_index(&self, dep_node: &DepNode) -> SerializedDepNodeIndex { pub fn node_to_index(&self, dep_node: &DepNode<K>) -> SerializedDepNodeIndex {
self.index[dep_node] self.index[dep_node]
} }
#[inline] #[inline]
pub fn node_to_index_opt(&self, dep_node: &DepNode) -> Option<SerializedDepNodeIndex> { pub fn node_to_index_opt(&self, dep_node: &DepNode<K>) -> Option<SerializedDepNodeIndex> {
self.index.get(dep_node).cloned() self.index.get(dep_node).cloned()
} }
#[inline] #[inline]
pub fn fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> { pub fn fingerprint_of(&self, dep_node: &DepNode<K>) -> Option<Fingerprint> {
self.index.get(dep_node).map(|&node_index| self.data.fingerprints[node_index]) self.index.get(dep_node).map(|&node_index| self.data.fingerprints[node_index])
} }

View File

@ -3,15 +3,15 @@ use rustc_data_structures::graph::implementation::{
Direction, Graph, NodeIndex, INCOMING, OUTGOING, Direction, Graph, NodeIndex, INCOMING, OUTGOING,
}; };
use super::DepNode; use super::{DepKind, DepNode};
pub struct DepGraphQuery { pub struct DepGraphQuery<K> {
pub graph: Graph<DepNode, ()>, pub graph: Graph<DepNode<K>, ()>,
pub indices: FxHashMap<DepNode, NodeIndex>, pub indices: FxHashMap<DepNode<K>, NodeIndex>,
} }
impl DepGraphQuery { impl<K: DepKind> DepGraphQuery<K> {
pub fn new(nodes: &[DepNode], edges: &[(DepNode, DepNode)]) -> DepGraphQuery { pub fn new(nodes: &[DepNode<K>], edges: &[(DepNode<K>, DepNode<K>)]) -> DepGraphQuery<K> {
let mut graph = Graph::with_capacity(nodes.len(), edges.len()); let mut graph = Graph::with_capacity(nodes.len(), edges.len());
let mut indices = FxHashMap::default(); let mut indices = FxHashMap::default();
for node in nodes { for node in nodes {
@ -27,15 +27,15 @@ impl DepGraphQuery {
DepGraphQuery { graph, indices } DepGraphQuery { graph, indices }
} }
pub fn contains_node(&self, node: &DepNode) -> bool { pub fn contains_node(&self, node: &DepNode<K>) -> bool {
self.indices.contains_key(&node) self.indices.contains_key(&node)
} }
pub fn nodes(&self) -> Vec<&DepNode> { pub fn nodes(&self) -> Vec<&DepNode<K>> {
self.graph.all_nodes().iter().map(|n| &n.data).collect() self.graph.all_nodes().iter().map(|n| &n.data).collect()
} }
pub fn edges(&self) -> Vec<(&DepNode, &DepNode)> { pub fn edges(&self) -> Vec<(&DepNode<K>, &DepNode<K>)> {
self.graph self.graph
.all_edges() .all_edges()
.iter() .iter()
@ -44,7 +44,7 @@ impl DepGraphQuery {
.collect() .collect()
} }
fn reachable_nodes(&self, node: &DepNode, direction: Direction) -> Vec<&DepNode> { fn reachable_nodes(&self, node: &DepNode<K>, direction: Direction) -> Vec<&DepNode<K>> {
if let Some(&index) = self.indices.get(node) { if let Some(&index) = self.indices.get(node) {
self.graph.depth_traverse(index, direction).map(|s| self.graph.node_data(s)).collect() self.graph.depth_traverse(index, direction).map(|s| self.graph.node_data(s)).collect()
} else { } else {
@ -54,17 +54,17 @@ impl DepGraphQuery {
/// All nodes reachable from `node`. In other words, things that /// All nodes reachable from `node`. In other words, things that
/// will have to be recomputed if `node` changes. /// will have to be recomputed if `node` changes.
pub fn transitive_successors(&self, node: &DepNode) -> Vec<&DepNode> { pub fn transitive_successors(&self, node: &DepNode<K>) -> Vec<&DepNode<K>> {
self.reachable_nodes(node, OUTGOING) self.reachable_nodes(node, OUTGOING)
} }
/// All nodes that can reach `node`. /// All nodes that can reach `node`.
pub fn transitive_predecessors(&self, node: &DepNode) -> Vec<&DepNode> { pub fn transitive_predecessors(&self, node: &DepNode<K>) -> Vec<&DepNode<K>> {
self.reachable_nodes(node, INCOMING) self.reachable_nodes(node, INCOMING)
} }
/// Just the outgoing edges from `node`. /// Just the outgoing edges from `node`.
pub fn immediate_successors(&self, node: &DepNode) -> Vec<&DepNode> { pub fn immediate_successors(&self, node: &DepNode<K>) -> Vec<&DepNode<K>> {
if let Some(&index) = self.indices.get(&node) { if let Some(&index) = self.indices.get(&node) {
self.graph.successor_nodes(index).map(|s| self.graph.node_data(s)).collect() self.graph.successor_nodes(index).map(|s| self.graph.node_data(s)).collect()
} else { } else {

View File

@ -0,0 +1,51 @@
//! The `DepGraphSafe` trait
use rustc_ast::ast::NodeId;
use rustc_hir::def_id::DefId;
use rustc_hir::BodyId;
/// The `DepGraphSafe` trait is used to specify what kinds of values
/// are safe to "leak" into a task. The idea is that this should be
/// only be implemented for things like the tcx as well as various id
/// types, which will create reads in the dep-graph whenever the trait
/// loads anything that might depend on the input program.
pub trait DepGraphSafe {}
/// A `BodyId` on its own doesn't give access to any particular state.
/// You must fetch the state from the various maps or generate
/// on-demand queries, all of which create reads.
impl DepGraphSafe for BodyId {}
/// A `NodeId` on its own doesn't give access to any particular state.
/// You must fetch the state from the various maps or generate
/// on-demand queries, all of which create reads.
impl DepGraphSafe for NodeId {}
/// A `DefId` on its own doesn't give access to any particular state.
/// You must fetch the state from the various maps or generate
/// on-demand queries, all of which create reads.
impl DepGraphSafe for DefId {}
/// Tuples make it easy to build up state.
impl<A, B> DepGraphSafe for (A, B)
where
A: DepGraphSafe,
B: DepGraphSafe,
{
}
/// Shared ref to dep-graph-safe stuff should still be dep-graph-safe.
impl<'a, A> DepGraphSafe for &'a A where A: DepGraphSafe {}
/// Mut ref to dep-graph-safe stuff should still be dep-graph-safe.
impl<'a, A> DepGraphSafe for &'a mut A where A: DepGraphSafe {}
/// No data here! :)
impl DepGraphSafe for () {}
/// A convenient override that lets you pass arbitrary state into a
/// task. Every use should be accompanied by a comment explaining why
/// it makes sense (or how it could be refactored away in the future).
pub struct AssertDepGraphSafe<T>(pub T);
impl<T> DepGraphSafe for AssertDepGraphSafe<T> {}

View File

@ -1,7 +1,7 @@
//! The data that we will serialize and deserialize. //! The data that we will serialize and deserialize.
use crate::dep_graph::DepNode; use super::{DepKind, DepNode};
use crate::ich::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
rustc_index::newtype_index! { rustc_index::newtype_index! {
@ -9,10 +9,10 @@ rustc_index::newtype_index! {
} }
/// Data for use when recompiling the **current crate**. /// Data for use when recompiling the **current crate**.
#[derive(Debug, RustcEncodable, RustcDecodable, Default)] #[derive(Debug, RustcEncodable, RustcDecodable)]
pub struct SerializedDepGraph { pub struct SerializedDepGraph<K: DepKind> {
/// The set of all DepNodes in the graph /// The set of all DepNodes in the graph
pub nodes: IndexVec<SerializedDepNodeIndex, DepNode>, pub nodes: IndexVec<SerializedDepNodeIndex, DepNode<K>>,
/// The set of all Fingerprints in the graph. Each Fingerprint corresponds to /// The set of all Fingerprints in the graph. Each Fingerprint corresponds to
/// the DepNode at the same index in the nodes vector. /// the DepNode at the same index in the nodes vector.
pub fingerprints: IndexVec<SerializedDepNodeIndex, Fingerprint>, pub fingerprints: IndexVec<SerializedDepNodeIndex, Fingerprint>,
@ -25,7 +25,18 @@ pub struct SerializedDepGraph {
pub edge_list_data: Vec<SerializedDepNodeIndex>, pub edge_list_data: Vec<SerializedDepNodeIndex>,
} }
impl SerializedDepGraph { impl<K: DepKind> Default for SerializedDepGraph<K> {
fn default() -> Self {
SerializedDepGraph {
nodes: Default::default(),
fingerprints: Default::default(),
edge_list_indices: Default::default(),
edge_list_data: Default::default(),
}
}
}
impl<K: DepKind> SerializedDepGraph<K> {
#[inline] #[inline]
pub fn edge_targets_from(&self, source: SerializedDepNodeIndex) -> &[SerializedDepNodeIndex] { pub fn edge_targets_from(&self, source: SerializedDepNodeIndex) -> &[SerializedDepNodeIndex] {
let targets = self.edge_list_indices[source]; let targets = self.edge_list_indices[source];

View File

@ -0,0 +1,32 @@
#![feature(const_fn)]
#![feature(const_if_match)]
#![feature(const_panic)]
#![feature(core_intrinsics)]
#![feature(specialization)]
#![feature(stmt_expr_attributes)]
#[macro_use]
extern crate log;
pub mod dep_graph;
pub trait HashStableContext {
fn debug_dep_tasks(&self) -> bool;
}
/// Something that can provide a stable hashing context.
pub trait HashStableContextProvider<Ctxt> {
fn get_stable_hashing_context(&self) -> Ctxt;
}
impl<Ctxt, T: HashStableContextProvider<Ctxt>> HashStableContextProvider<Ctxt> for &T {
fn get_stable_hashing_context(&self) -> Ctxt {
(**self).get_stable_hashing_context()
}
}
impl<Ctxt, T: HashStableContextProvider<Ctxt>> HashStableContextProvider<Ctxt> for &mut T {
fn get_stable_hashing_context(&self) -> Ctxt {
(**self).get_stable_hashing_context()
}
}