Skip to content

Commit

Permalink
Offload try_collect_active_jobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 16, 2020
1 parent 5dc7c2e commit 7d84f4f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
1 change: 0 additions & 1 deletion src/librustc/ty/query/mod.rs
Expand Up @@ -57,7 +57,6 @@ use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::ops::Deref;
use std::sync::Arc;

Expand Down
62 changes: 37 additions & 25 deletions src/librustc/ty/query/plumbing.rs
Expand Up @@ -2,10 +2,10 @@
//! generate the actual methods on tcx which find and execute the provider,
//! manage the caches, and so forth.

use crate::dep_graph::{DepNode, DepNodeIndex, SerializedDepNodeIndex};
use crate::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
use crate::ty::query::caches::QueryCache;
use crate::ty::query::config::{QueryAccessors, QueryConfig, QueryDescription};
use crate::ty::query::job::{QueryInfo, QueryJob, QueryJobId, QueryShardJobId};
use crate::ty::query::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryShardJobId};
use crate::ty::query::Query;
use crate::ty::tls;
use crate::ty::{self, TyCtxt};
Expand All @@ -20,6 +20,7 @@ use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, FatalError, H
use rustc_span::source_map::DUMMY_SP;
use rustc_span::Span;
use std::collections::hash_map::Entry;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::mem;
Expand Down Expand Up @@ -110,6 +111,35 @@ impl<'tcx, K, V, C: QueryCache<K, V>> QueryStateImpl<'tcx, K, V, C> {
let shards = self.shards.lock_shards();
shards.iter().all(|shard| shard.active.is_empty())
}

pub(super) fn try_collect_active_jobs(
&self,
kind: DepKind,
make_query: fn(K) -> Query<'tcx>,
jobs: &mut FxHashMap<QueryJobId, QueryJobInfo<'tcx>>,
) -> Option<()>
where
K: Clone,
{
// We use try_lock_shards here since we are called from the
// deadlock handler, and this shouldn't be locked.
let shards = self.shards.try_lock_shards()?;
let shards = shards.iter().enumerate();
jobs.extend(shards.flat_map(|(shard_id, shard)| {
shard.active.iter().filter_map(move |(k, v)| {
if let QueryResult::Started(ref job) = *v {
let id =
QueryJobId { job: job.id, shard: u16::try_from(shard_id).unwrap(), kind };
let info = QueryInfo { span: job.span, query: make_query(k.clone()) };
Some((id, QueryJobInfo { info, job: job.clone() }))
} else {
None
}
})
}));

Some(())
}
}

impl<'tcx, K, V, C: QueryCache<K, V>> Default for QueryStateImpl<'tcx, K, V, C> {
Expand Down Expand Up @@ -1135,29 +1165,11 @@ macro_rules! define_queries_struct {
let mut jobs = FxHashMap::default();

$(
// We use try_lock_shards here since we are called from the
// deadlock handler, and this shouldn't be locked.
let shards = self.$name.shards.try_lock_shards()?;
let shards = shards.iter().enumerate();
jobs.extend(shards.flat_map(|(shard_id, shard)| {
shard.active.iter().filter_map(move |(k, v)| {
if let QueryResult::Started(ref job) = *v {
let id = QueryJobId {
job: job.id,
shard: u16::try_from(shard_id).unwrap(),
kind:
<queries::$name<'tcx> as QueryAccessors<'tcx>>::DEP_KIND,
};
let info = QueryInfo {
span: job.span,
query: Query::$name(k.clone())
};
Some((id, QueryJobInfo { info, job: job.clone() }))
} else {
None
}
})
}));
self.$name.try_collect_active_jobs(
<queries::$name<'tcx> as QueryAccessors<'tcx>>::DEP_KIND,
Query::$name,
&mut jobs,
)?;
)*

Some(jobs)
Expand Down

0 comments on commit 7d84f4f

Please sign in to comment.