Skip to content

Commit

Permalink
Auto merge of #48691 - Zoxc:profq-chan, r=michaelwoerister
Browse files Browse the repository at this point in the history
Move PROFQ_CHAN to a Session field

r? @michaelwoerister
  • Loading branch information
bors committed Mar 11, 2018
2 parents 0bae326 + 184fd32 commit ae379bd
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 170 deletions.
37 changes: 19 additions & 18 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
// except according to those terms.

use errors::DiagnosticBuilder;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHashingContextProvider};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc_data_structures::sync::Lrc;
Expand All @@ -20,7 +19,7 @@ use std::hash::Hash;
use ty::TyCtxt;
use util::common::{ProfileQueriesMsg, profq_msg};

use ich::Fingerprint;
use ich::{StableHashingContext, StableHashingContextProvider, Fingerprint};

use super::debug::EdgeFilter;
use super::dep_node::{DepNode, DepKind, WorkProductId};
Expand Down Expand Up @@ -189,47 +188,49 @@ impl DepGraph {
/// `arg` parameter.
///
/// [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/incremental-compilation.html
pub fn with_task<C, A, R, HCX>(&self,
pub fn with_task<'gcx, C, A, R>(&self,
key: DepNode,
cx: C,
arg: A,
task: fn(C, A) -> R)
-> (R, DepNodeIndex)
where C: DepGraphSafe + StableHashingContextProvider<ContextType=HCX>,
R: HashStable<HCX>,
where C: DepGraphSafe + StableHashingContextProvider<'gcx>,
R: HashStable<StableHashingContext<'gcx>>,
{
self.with_task_impl(key, cx, arg, task,
|data, key| data.borrow_mut().push_task(key),
|data, key| data.borrow_mut().pop_task(key))
}

fn with_task_impl<C, A, R, HCX>(&self,
fn with_task_impl<'gcx, C, A, R>(&self,
key: DepNode,
cx: C,
arg: A,
task: fn(C, A) -> R,
push: fn(&RefCell<CurrentDepGraph>, DepNode),
pop: fn(&RefCell<CurrentDepGraph>, DepNode) -> DepNodeIndex)
-> (R, DepNodeIndex)
where C: DepGraphSafe + StableHashingContextProvider<ContextType=HCX>,
R: HashStable<HCX>,
where C: DepGraphSafe + StableHashingContextProvider<'gcx>,
R: HashStable<StableHashingContext<'gcx>>,
{
if let Some(ref data) = self.data {
push(&data.current, 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 mut hcx = cx.get_stable_hashing_context();

if cfg!(debug_assertions) {
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskBegin(key.clone()))
};

let result = task(cx, arg);

if cfg!(debug_assertions) {
profq_msg(ProfileQueriesMsg::TaskEnd)
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskEnd)
};

let dep_node_index = pop(&data.current, key);
Expand Down Expand Up @@ -274,7 +275,7 @@ impl DepGraph {
(result, dep_node_index)
} else {
if key.kind.fingerprint_needed_for_crate_hash() {
let mut hcx = cx.create_stable_hashing_context();
let mut hcx = cx.get_stable_hashing_context();
let result = task(cx, arg);
let mut stable_hasher = StableHasher::new();
result.hash_stable(&mut hcx, &mut stable_hasher);
Expand Down Expand Up @@ -314,14 +315,14 @@ impl DepGraph {

/// Execute something within an "eval-always" task which is a task
// that runs whenever anything changes.
pub fn with_eval_always_task<C, A, R, HCX>(&self,
pub fn with_eval_always_task<'gcx, C, A, R>(&self,
key: DepNode,
cx: C,
arg: A,
task: fn(C, A) -> R)
-> (R, DepNodeIndex)
where C: DepGraphSafe + StableHashingContextProvider<ContextType=HCX>,
R: HashStable<HCX>,
where C: DepGraphSafe + StableHashingContextProvider<'gcx>,
R: HashStable<StableHashingContext<'gcx>>,
{
self.with_task_impl(key, cx, arg, task,
|data, key| data.borrow_mut().push_eval_always_task(key),
Expand Down
32 changes: 24 additions & 8 deletions src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use syntax::symbol::Symbol;
use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::hygiene;

use rustc_data_structures::stable_hasher::{HashStable, StableHashingContextProvider,
use rustc_data_structures::stable_hasher::{HashStable,
StableHasher, StableHasherResult,
ToStableHashKey};
use rustc_data_structures::accumulate_vec::AccumulateVec;
Expand Down Expand Up @@ -192,17 +192,33 @@ impl<'a> StableHashingContext<'a> {
}
}

impl<'a, 'gcx, 'lcx> StableHashingContextProvider for TyCtxt<'a, 'gcx, 'lcx> {
type ContextType = StableHashingContext<'a>;
fn create_stable_hashing_context(&self) -> Self::ContextType {
(*self).create_stable_hashing_context()
/// Something that can provide a stable hashing context.
pub trait StableHashingContextProvider<'a> {
fn get_stable_hashing_context(&self) -> StableHashingContext<'a>;
}

impl<'a, 'b, T: StableHashingContextProvider<'a>> StableHashingContextProvider<'a>
for &'b T {
fn get_stable_hashing_context(&self) -> StableHashingContext<'a> {
(**self).get_stable_hashing_context()
}
}

impl<'a, 'b, T: StableHashingContextProvider<'a>> StableHashingContextProvider<'a>
for &'b mut T {
fn get_stable_hashing_context(&self) -> StableHashingContext<'a> {
(**self).get_stable_hashing_context()
}
}

impl<'a, 'gcx, 'lcx> StableHashingContextProvider<'a> for TyCtxt<'a, 'gcx, 'lcx> {
fn get_stable_hashing_context(&self) -> StableHashingContext<'a> {
(*self).create_stable_hashing_context()
}
}

impl<'a> StableHashingContextProvider for StableHashingContext<'a> {
type ContextType = StableHashingContext<'a>;
fn create_stable_hashing_context(&self) -> Self::ContextType {
impl<'a> StableHashingContextProvider<'a> for StableHashingContext<'a> {
fn get_stable_hashing_context(&self) -> StableHashingContext<'a> {
self.clone()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

pub use self::fingerprint::Fingerprint;
pub use self::caching_codemap_view::CachingCodemapView;
pub use self::hcx::{StableHashingContext, NodeIdHashingMode,
pub use self::hcx::{StableHashingContextProvider, StableHashingContext, NodeIdHashingMode,
hash_stable_trait_impls, compute_ignored_attr_names};
mod fingerprint;
mod caching_codemap_view;
Expand Down
8 changes: 7 additions & 1 deletion src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ use session::config::{DebugInfoLevel, OutputType};
use ty::tls;
use util::nodemap::{FxHashMap, FxHashSet};
use util::common::{duration_to_secs_str, ErrorReported};
use util::common::ProfileQueriesMsg;

use rustc_data_structures::sync::Lrc;
use rustc_data_structures::sync::{Lrc, Lock};

use syntax::ast::NodeId;
use errors::{self, DiagnosticBuilder, DiagnosticId};
Expand Down Expand Up @@ -53,6 +54,7 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Once, ONCE_INIT};
use std::time::Duration;
use std::sync::mpsc;

mod code_stats;
pub mod config;
Expand Down Expand Up @@ -126,6 +128,9 @@ pub struct Session {
/// A cache of attributes ignored by StableHashingContext
pub ignored_attr_names: FxHashSet<Symbol>,

/// Used by -Z profile-queries in util::common
pub profile_channel: Lock<Option<mpsc::Sender<ProfileQueriesMsg>>>,

/// Some measurements that are being gathered during compilation.
pub perf_stats: PerfStats,

Expand Down Expand Up @@ -1131,6 +1136,7 @@ pub fn build_session_(
imported_macro_spans: RefCell::new(HashMap::new()),
incr_comp_session: RefCell::new(IncrCompSession::NotInitialized),
ignored_attr_names: ich::compute_ignored_attr_names(),
profile_channel: Lock::new(None),
perf_stats: PerfStats {
svh_time: Cell::new(Duration::from_secs(0)),
incr_comp_hashes_time: Cell::new(Duration::from_secs(0)),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
macro_rules! profq_msg {
($tcx:expr, $msg:expr) => {
if cfg!(debug_assertions) {
if $tcx.sess.profile_queries() {
profq_msg($msg)
if $tcx.sess.profile_queries() {
profq_msg($tcx.sess, $msg)
}
}
}
Expand Down
62 changes: 32 additions & 30 deletions src/librustc/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ty::maps::{QueryMsg};
use dep_graph::{DepNode};
use proc_macro;
use lazy_static;
use session::Session;

// The name of the associated type for `Fn` return types
pub const FN_OUTPUT_NAME: &'static str = "Output";
Expand Down Expand Up @@ -55,9 +56,6 @@ pub fn install_panic_hook() {
lazy_static::initialize(&DEFAULT_HOOK);
}

/// Initialized for -Z profile-queries
thread_local!(static PROFQ_CHAN: RefCell<Option<Sender<ProfileQueriesMsg>>> = RefCell::new(None));

/// Parameters to the `Dump` variant of type `ProfileQueriesMsg`.
#[derive(Clone,Debug)]
pub struct ProfQDumpParams {
Expand Down Expand Up @@ -97,29 +95,23 @@ pub enum ProfileQueriesMsg {
}

/// If enabled, send a message to the profile-queries thread
pub fn profq_msg(msg: ProfileQueriesMsg) {
PROFQ_CHAN.with(|sender|{
if let Some(s) = sender.borrow().as_ref() {
s.send(msg).unwrap()
} else {
// Do nothing.
//
// FIXME(matthewhammer): Multi-threaded translation phase triggers the panic below.
// From backtrace: rustc_trans::back::write::spawn_work::{{closure}}.
//
// panic!("no channel on which to send profq_msg: {:?}", msg)
}
})
pub fn profq_msg(sess: &Session, msg: ProfileQueriesMsg) {
if let Some(s) = sess.profile_channel.borrow().as_ref() {
s.send(msg).unwrap()
} else {
// Do nothing
}
}

/// Set channel for profile queries channel
pub fn profq_set_chan(s: Sender<ProfileQueriesMsg>) -> bool {
PROFQ_CHAN.with(|chan|{
if chan.borrow().is_none() {
*chan.borrow_mut() = Some(s);
true
} else { false }
})
pub fn profq_set_chan(sess: &Session, s: Sender<ProfileQueriesMsg>) -> bool {
let mut channel = sess.profile_channel.borrow_mut();
if channel.is_none() {
*channel = Some(s);
true
} else {
false
}
}

/// Read the current depth of `time()` calls. This is used to
Expand All @@ -135,7 +127,13 @@ pub fn set_time_depth(depth: usize) {
TIME_DEPTH.with(|slot| slot.set(depth));
}

pub fn time<T, F>(do_it: bool, what: &str, f: F) -> T where
pub fn time<T, F>(sess: &Session, what: &str, f: F) -> T where
F: FnOnce() -> T,
{
time_ext(sess.time_passes(), Some(sess), what, f)
}

pub fn time_ext<T, F>(do_it: bool, sess: Option<&Session>, what: &str, f: F) -> T where
F: FnOnce() -> T,
{
if !do_it { return f(); }
Expand All @@ -146,15 +144,19 @@ pub fn time<T, F>(do_it: bool, what: &str, f: F) -> T where
r
});

if cfg!(debug_assertions) {
profq_msg(ProfileQueriesMsg::TimeBegin(what.to_string()))
};
if let Some(sess) = sess {
if cfg!(debug_assertions) {
profq_msg(sess, ProfileQueriesMsg::TimeBegin(what.to_string()))
}
}
let start = Instant::now();
let rv = f();
let dur = start.elapsed();
if cfg!(debug_assertions) {
profq_msg(ProfileQueriesMsg::TimeEnd)
};
if let Some(sess) = sess {
if cfg!(debug_assertions) {
profq_msg(sess, ProfileQueriesMsg::TimeEnd)
}
}

print_time_passes_entry_internal(what, dur);

Expand Down
23 changes: 0 additions & 23 deletions src/librustc_data_structures/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,29 +165,6 @@ impl<W> Hasher for StableHasher<W> {
}
}


/// Something that can provide a stable hashing context.
pub trait StableHashingContextProvider {
type ContextType;
fn create_stable_hashing_context(&self) -> Self::ContextType;
}

impl<'a, T: StableHashingContextProvider> StableHashingContextProvider for &'a T {
type ContextType = T::ContextType;

fn create_stable_hashing_context(&self) -> Self::ContextType {
(**self).create_stable_hashing_context()
}
}

impl<'a, T: StableHashingContextProvider> StableHashingContextProvider for &'a mut T {
type ContextType = T::ContextType;

fn create_stable_hashing_context(&self) -> Self::ContextType {
(**self).create_stable_hashing_context()
}
}

/// Something that implements `HashStable<CTX>` can be hashed in a way that is
/// stable across multiple compilation sessions.
pub trait HashStable<CTX> {
Expand Down
Loading

0 comments on commit ae379bd

Please sign in to comment.