Skip to content

incr.comp.: Make DepNode's std::fmt::Debug implementation useful again. #42625

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

Merged
merged 2 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 126 additions & 4 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use ich::Fingerprint;
use ty::TyCtxt;
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
use ich::StableHashingContext;
use std::fmt;
use std::hash::Hash;

// erase!() just makes tokens go away. It's used to specify which macro argument
Expand Down Expand Up @@ -145,7 +146,7 @@ macro_rules! define_dep_nodes {
),*
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
RustcEncodable, RustcDecodable)]
pub struct DepNode {
pub kind: DepKind,
Expand All @@ -166,21 +167,45 @@ macro_rules! define_dep_nodes {
let tupled_args = ( $($tuple_arg,)* );
let hash = DepNodeParams::to_fingerprint(&tupled_args,
tcx);
return DepNode {
let dep_node = DepNode {
kind: DepKind::$variant,
hash
};

if cfg!(debug_assertions) &&
!dep_node.kind.can_reconstruct_query_key() &&
(tcx.sess.opts.debugging_opts.incremental_info ||
tcx.sess.opts.debugging_opts.query_dep_graph)
{
tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
tupled_args.to_debug_str(tcx)
});
}

return dep_node;
})*

// struct args
$({
let tupled_args = ( $($struct_arg_name,)* );
let hash = DepNodeParams::to_fingerprint(&tupled_args,
tcx);
return DepNode {
let dep_node = DepNode {
kind: DepKind::$variant,
hash
};

if cfg!(debug_assertions) &&
!dep_node.kind.can_reconstruct_query_key() &&
(tcx.sess.opts.debugging_opts.incremental_info ||
tcx.sess.opts.debugging_opts.query_dep_graph)
{
tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
tupled_args.to_debug_str(tcx)
});
}

return dep_node;
})*

DepNode {
Expand Down Expand Up @@ -267,6 +292,36 @@ 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() {
return Ok(());
}

write!(f, "(")?;

::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.item_path_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, ")")
}
}


impl DefPathHash {
#[inline]
pub fn to_dep_node(self, kind: DepKind) -> DepNode {
Expand Down Expand Up @@ -434,10 +489,11 @@ define_dep_nodes!(
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> {
const CAN_RECONSTRUCT_QUERY_KEY: bool;
fn to_fingerprint(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Fingerprint;
fn to_debug_str(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> String;
}

impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a, T> DepNodeParams<'a, 'gcx, 'tcx> for T
where T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
where T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + fmt::Debug
{
default const CAN_RECONSTRUCT_QUERY_KEY: bool = false;

Expand All @@ -449,6 +505,10 @@ impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a, T> DepNodeParams<'a, 'gcx, 'tcx> for T

hasher.finish()
}

default fn to_debug_str(&self, _: TyCtxt<'a, 'gcx, 'tcx>) -> String {
format!("{:?}", *self)
}
}

impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for (DefId,) {
Expand All @@ -457,6 +517,68 @@ impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for (DefId,) {
fn to_fingerprint(&self, tcx: TyCtxt) -> Fingerprint {
tcx.def_path_hash(self.0).0
}

fn to_debug_str(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> String {
tcx.item_path_str(self.0)
}
}

impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for (DefId, DefId) {
const CAN_RECONSTRUCT_QUERY_KEY: bool = false;

// 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
// hashing context and stable-hashing state.
fn to_fingerprint(&self, tcx: TyCtxt) -> Fingerprint {
let (def_id_0, def_id_1) = *self;

let def_path_hash_0 = tcx.def_path_hash(def_id_0);
let def_path_hash_1 = tcx.def_path_hash(def_id_1);
Copy link
Contributor

Choose a reason for hiding this comment

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

it'd be good to add a comment clarifying that this is a micro-opt, and the generic impl also suffices


def_path_hash_0.0.combine(def_path_hash_1.0)
}

fn to_debug_str(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> String {
let (def_id_0, def_id_1) = *self;

format!("({}, {})",
tcx.def_path(def_id_0).to_string(tcx),
tcx.def_path(def_id_1).to_string(tcx))
}
}


impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for (DefIdList,) {
const CAN_RECONSTRUCT_QUERY_KEY: bool = false;

// 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
// hashing context and stable-hashing state.
fn to_fingerprint(&self, tcx: TyCtxt) -> Fingerprint {
let mut fingerprint = Fingerprint::zero();

for &def_id in self.0.iter() {
let def_path_hash = tcx.def_path_hash(def_id);
fingerprint = fingerprint.combine(def_path_hash.0);
}

fingerprint
}

fn to_debug_str(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> String {
use std::fmt::Write;

let mut s = String::new();
write!(&mut s, "[").unwrap();

for &def_id in self.0.iter() {
write!(&mut s, "{}", tcx.def_path(def_id).to_string(tcx)).unwrap();
}

write!(&mut s, "]").unwrap();

s
}
}

/// A "work product" corresponds to a `.o` (or other) file that we
Expand Down
19 changes: 19 additions & 0 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct DepGraphData {

/// Work-products that we generate in this run.
work_products: RefCell<FxHashMap<WorkProductId, WorkProduct>>,

dep_node_debug: RefCell<FxHashMap<DepNode, String>>,
}

impl DepGraph {
Expand All @@ -46,6 +48,7 @@ impl DepGraph {
thread: DepGraphThreadData::new(enabled),
previous_work_products: RefCell::new(FxHashMap()),
work_products: RefCell::new(FxHashMap()),
dep_node_debug: RefCell::new(FxHashMap()),
})
}
}
Expand Down Expand Up @@ -152,6 +155,22 @@ impl DepGraph {
pub fn previous_work_products(&self) -> Ref<FxHashMap<WorkProductId, WorkProduct>> {
self.data.previous_work_products.borrow()
}

#[inline(always)]
pub(super) fn register_dep_node_debug_str<F>(&self,
dep_node: DepNode,
debug_str_gen: F)
where F: FnOnce() -> String
{
let mut dep_node_debug = self.data.dep_node_debug.borrow_mut();

dep_node_debug.entry(dep_node)
.or_insert_with(debug_str_gen);
}

pub(super) fn dep_node_debug_str(&self, dep_node: DepNode) -> Option<String> {
self.data.dep_node_debug.borrow().get(&dep_node).cloned()
}
}

/// A "work product" is an intermediate result that we save into the
Expand Down
11 changes: 11 additions & 0 deletions src/librustc/ich/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,20 @@ impl Fingerprint {
self.0
}

#[inline]
pub fn combine(self, other: Fingerprint) -> Fingerprint {
// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
Fingerprint(
self.0.wrapping_mul(3).wrapping_add(other.0),
self.1.wrapping_mul(3).wrapping_add(other.1)
)
}

pub fn to_hex(&self) -> String {
format!("{:x}{:x}", self.0, self.1)
}

}

impl ::std::fmt::Display for Fingerprint {
Expand Down