Skip to content

Commit

Permalink
Rollup merge of rust-lang#44364 - michaelwoerister:hash-all-the-thing…
Browse files Browse the repository at this point in the history
…s2, r=nikomatsakis

incr.comp.: Compute fingerprint for all query results.

This PR enables query result fingerprinting in incremental mode. This is an essential piece of infrastructure for red/green tracking. We don't do anything with the fingerprints yet but merging the infrastructure should protect it from bit-rotting and will make it easier to start measuring its performance impact (and thus let us determine if we should switch to a faster hashing algorithm rather sooner than later).

Note, this PR also includes the changes from rust-lang#43887 which I'm therefore closing. No need to re-review the first commit though.

r? @nikomatsakis
  • Loading branch information
alexcrichton authored Sep 18, 2017
2 parents 3a7b960 + 4961a8e commit 9ad5473
Show file tree
Hide file tree
Showing 61 changed files with 1,827 additions and 731 deletions.
16 changes: 14 additions & 2 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,12 @@ trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {
}

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

default fn to_fingerprint(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Fingerprint {
let mut hcx = StableHashingContext::new(tcx);
let mut hcx = tcx.create_stable_hashing_context();
let mut hasher = StableHasher::new();

self.hash_stable(&mut hcx, &mut hasher);
Expand All @@ -633,6 +633,18 @@ impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for (DefId,) {
}
}

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

fn to_fingerprint(&self, tcx: TyCtxt) -> Fingerprint {
tcx.hir.definitions().def_path_hash(self.0).0
}

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

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

Expand Down
36 changes: 25 additions & 11 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
// except according to those terms.

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHashingContextProvider};
use session::config::OutputType;
use std::cell::{Ref, RefCell};
use std::rc::Rc;
use util::common::{ProfileQueriesMsg, profq_msg};

use ich::Fingerprint;

use super::dep_node::{DepNode, DepKind, WorkProductId};
use super::query::DepGraphQuery;
use super::raii;
Expand Down Expand Up @@ -71,10 +75,6 @@ impl DepGraph {
self.data.as_ref().map(|data| raii::IgnoreTask::new(&data.edges))
}

pub fn in_task<'graph>(&'graph self, key: DepNode) -> Option<raii::DepTask<'graph>> {
self.data.as_ref().map(|data| raii::DepTask::new(&data.edges, key))
}

pub fn with_ignore<OP,R>(&self, op: OP) -> R
where OP: FnOnce() -> R
{
Expand Down Expand Up @@ -109,24 +109,38 @@ impl DepGraph {
/// `arg` parameter.
///
/// [README]: README.md
pub fn with_task<C, A, R>(&self,
key: DepNode,
cx: C,
arg: A,
task: fn(C, A) -> R)
-> (R, DepNodeIndex)
where C: DepGraphSafe
pub fn with_task<C, A, R, HCX>(&self,
key: DepNode,
cx: C,
arg: A,
task: fn(C, A) -> R)
-> (R, DepNodeIndex)
where C: DepGraphSafe + StableHashingContextProvider<ContextType=HCX>,
R: HashStable<HCX>,
{
if let Some(ref data) = self.data {
data.edges.borrow_mut().push_task(key);
if cfg!(debug_assertions) {
profq_msg(ProfileQueriesMsg::TaskBegin(key.clone()))
};

// In incremental mode, hash the result of the task. We don't
// do anything with the hash yet, but we are computing it
// anyway so that
// - we make sure that the infrastructure works and
// - we can get an idea of the runtime cost.
let mut hcx = cx.create_stable_hashing_context();

let result = task(cx, arg);
if cfg!(debug_assertions) {
profq_msg(ProfileQueriesMsg::TaskEnd)
};
let dep_node_index = data.edges.borrow_mut().pop_task(key);

let mut stable_hasher = StableHasher::new();
result.hash_stable(&mut hcx, &mut stable_hasher);
let _: Fingerprint = stable_hasher.finish();

(result, dep_node_index)
} else {
(task(cx, arg), DepNodeIndex::INVALID)
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/dep_graph/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ impl<'a, A> DepGraphSafe for &'a A
{
}

/// 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 () {
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub struct Map<'hir> {
/// plain old integers.
map: Vec<MapEntry<'hir>>,

definitions: Definitions,
definitions: &'hir Definitions,

/// Bodies inlined from other crates are cached here.
inlined_bodies: RefCell<DefIdMap<&'hir Body>>,
Expand Down Expand Up @@ -304,8 +304,8 @@ impl<'hir> Map<'hir> {
}

#[inline]
pub fn definitions(&self) -> &Definitions {
&self.definitions
pub fn definitions(&self) -> &'hir Definitions {
self.definitions
}

pub fn def_key(&self, def_id: DefId) -> DefKey {
Expand Down Expand Up @@ -1013,7 +1013,7 @@ impl Named for TraitItem { fn name(&self) -> Name { self.name } }
impl Named for ImplItem { fn name(&self) -> Name { self.name } }

pub fn map_crate<'hir>(forest: &'hir mut Forest,
definitions: Definitions)
definitions: &'hir Definitions)
-> Map<'hir> {
let map = {
let mut collector = NodeCollector::root(&forest.krate,
Expand Down
10 changes: 4 additions & 6 deletions src/librustc/ich/caching_codemap_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use std::rc::Rc;
use syntax::codemap::CodeMap;
use syntax_pos::{BytePos, FileMap};
use ty::TyCtxt;

#[derive(Clone)]
struct CacheEntry {
Expand All @@ -23,15 +22,14 @@ struct CacheEntry {
file_index: usize,
}

pub struct CachingCodemapView<'tcx> {
codemap: &'tcx CodeMap,
pub struct CachingCodemapView<'cm> {
codemap: &'cm CodeMap,
line_cache: [CacheEntry; 3],
time_stamp: usize,
}

impl<'gcx> CachingCodemapView<'gcx> {
pub fn new<'a, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> CachingCodemapView<'gcx> {
let codemap = tcx.sess.codemap();
impl<'cm> CachingCodemapView<'cm> {
pub fn new(codemap: &'cm CodeMap) -> CachingCodemapView<'cm> {
let files = codemap.files();
let first_file = files[0].clone();
let entry = CacheEntry {
Expand Down
Loading

0 comments on commit 9ad5473

Please sign in to comment.