Skip to content
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

Remove queries from rustc_interface #59904

Closed
wants to merge 10 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,7 @@ name = "rustc_data_structures"
version = "0.0.0"
dependencies = [
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"ena 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"graphviz 0.0.0",
"indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
Expand Down
4 changes: 4 additions & 0 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ macro_rules! arena_types {
rustc::hir::def_id::DefId,
rustc::ty::subst::SubstsRef<$tcx>
)>,
[few] on_disk_cache: rustc::ty::query::OnDiskCache<$tcx>,
[few] dep_graph: rustc::dep_graph::DepGraph,
[few] lowered_hir: rustc::hir::LoweredHir,
[few] hir_map: rustc::hir::map::Map<$tcx>,
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
[decode] specialization_graph: rustc::traits::specialization_graph::Graph,
[] region_scope_tree: rustc::middle::region::ScopeTree,
Expand Down
33 changes: 26 additions & 7 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ macro_rules! define_dep_nodes {
(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, || {
tcx.dep_graph().register_dep_node_debug_str(dep_node, || {
arg.to_debug_str(tcx)
});
}
Expand All @@ -247,7 +247,7 @@ macro_rules! define_dep_nodes {
(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, || {
tcx.dep_graph().register_dep_node_debug_str(dep_node, || {
tupled_args.to_debug_str(tcx)
});
}
Expand Down Expand Up @@ -304,8 +304,7 @@ macro_rules! define_dep_nodes {
pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
if self.kind.can_reconstruct_query_key() {
let def_path_hash = DefPathHash(self.hash);
tcx.def_path_hash_to_def_id.as_ref()?
.get(&def_path_hash).cloned()
tcx.def_path_hash_to_def_id()?.get(&def_path_hash).cloned()
} else {
None
}
Expand Down Expand Up @@ -369,7 +368,7 @@ impl fmt::Debug for DepNode {
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) {
} else if let Some(ref s) = tcx.dep_graph().dep_node_debug_str(*self) {
write!(f, "{}", s)?;
} else {
write!(f, "{}", self.hash)?;
Expand Down Expand Up @@ -403,6 +402,10 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
// We use this for most things when incr. comp. is turned off.
[] Null,

// Represents all queries which are not incremental.
// This is always treated as a red dep node.
[] NonIncremental,

// Represents the `Krate` as a whole (the `hir::Krate` value) (as
// distinct from the krate module). This is basically a hash of
// the entire krate, so if you read from `Krate` (e.g., by calling
Expand Down Expand Up @@ -431,14 +434,18 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
[anon] TraitSelect,

[] CompileCodegenUnit(InternedString),

[eval_always] Analysis(CrateNum),
]);

pub trait RecoverKey<'tcx>: Sized {
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
}

impl RecoverKey<'tcx> for () {
fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
Some(())
}
}

impl RecoverKey<'tcx> for CrateNum {
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
dep_node.extract_def_id(tcx).map(|id| id.krate)
Expand Down Expand Up @@ -533,6 +540,18 @@ impl<'tcx> DepNodeParams<'tcx> for CrateNum {
}
}

impl<'tcx> DepNodeParams<'tcx> for () {
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;

fn to_fingerprint(&self, _: TyCtxt<'_>) -> Fingerprint {
Fingerprint::ZERO
}

fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
"<no-params>".to_string()
}
}

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

Expand Down
139 changes: 94 additions & 45 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ use super::safe::DepGraphSafe;
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
use super::prev::PreviousDepGraph;

pub type WorkProductMap = FxHashMap<WorkProductId, WorkProduct>;

pub enum LoadResult<T> {
Ok { data: T },
DataOutOfDate,
Error { message: String },
}

/// Either a result that has already be computed or a
/// handle that will let us wait until it is computed
/// by a background thread.
pub enum MaybeAsync<T> {
Sync(T),
Async(std::thread::JoinHandle<T>)
}
impl<T> MaybeAsync<T> {
pub fn open(self) -> std::thread::Result<T> {
match self {
MaybeAsync::Sync(result) => Ok(result),
MaybeAsync::Async(handle) => handle.join()
}
}
}

pub type DepGraphFuture = MaybeAsync<LoadResult<(PreviousDepGraph, WorkProductMap)>>;

#[derive(Clone)]
pub struct DepGraph {
data: Option<Lrc<DepGraphData>>,
Expand All @@ -30,7 +56,7 @@ newtype_index! {
}

impl DepNodeIndex {
const INVALID: DepNodeIndex = DepNodeIndex::MAX;
pub(crate) const INVALID: DepNodeIndex = DepNodeIndex::MAX;
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -79,32 +105,44 @@ struct DepGraphData {
loaded_from_cache: Lock<FxHashMap<DepNodeIndex, bool>>,
}

pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Option<Fingerprint>
pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Fingerprint
where
R: for<'a> HashStable<StableHashingContext<'a>>,
{
let mut stable_hasher = StableHasher::new();
result.hash_stable(hcx, &mut stable_hasher);

Some(stable_hasher.finish())
stable_hasher.finish()
}

impl DepGraph {
pub fn new(prev_graph: PreviousDepGraph,
prev_work_products: FxHashMap<WorkProductId, WorkProduct>) -> DepGraph {
let prev_graph_node_count = prev_graph.node_count();

let mut data = DepGraphData {
previous_work_products: prev_work_products,
dep_node_debug: Default::default(),
current: Lock::new(CurrentDepGraph::new(prev_graph_node_count)),
emitted_diagnostics: Default::default(),
emitted_diagnostics_cond_var: Condvar::new(),
previous: prev_graph,
colors: DepNodeColorMap::new(prev_graph_node_count),
loaded_from_cache: Default::default(),
};

let non_incr_dep_node = DepNode::new_no_params(DepKind::NonIncremental);

// Allocate the NonIncremental node
data.current.get_mut().alloc_node(non_incr_dep_node, smallvec![], Fingerprint::ZERO);

data.previous.node_to_index_opt(&non_incr_dep_node).map(|prev_index| {
// Color previous NonIncremental node as red
data.colors.insert(prev_index, DepNodeColor::Red);
});

DepGraph {
data: Some(Lrc::new(DepGraphData {
previous_work_products: prev_work_products,
dep_node_debug: Default::default(),
current: Lock::new(CurrentDepGraph::new(prev_graph_node_count)),
emitted_diagnostics: Default::default(),
emitted_diagnostics_cond_var: Condvar::new(),
previous: prev_graph,
colors: DepNodeColorMap::new(prev_graph_node_count),
loaded_from_cache: Default::default(),
})),
data: Some(Lrc::new(data)),
}
}

Expand Down Expand Up @@ -135,18 +173,21 @@ impl DepGraph {
DepGraphQuery::new(&nodes[..], &edges[..])
}

pub fn assert_ignored(&self)
{
if let Some(..) = self.data {
ty::tls::with_context_opt(|icx| {
let icx = if let Some(icx) = icx { icx } else { return };
assert!(icx.task_deps.is_none(), "expected no task dependency tracking");
})
}
pub fn assert_ignored() {
ty::tls::with_context_opt(|icx| {
let icx = if let Some(icx) = icx { icx } else { return };
assert!(icx.task_deps.is_none(), "expected no task dependency tracking");
})
}

pub fn with_ignore<OP,R>(&self, op: OP) -> R
where OP: FnOnce() -> R
{
Self::ignore_deps(op)
}

pub fn ignore_deps<OP,R>(op: OP) -> R
where OP: FnOnce() -> R
{
ty::tls::with_context(|icx| {
let icx = ty::tls::ImplicitCtxt {
Expand Down Expand Up @@ -193,7 +234,7 @@ impl DepGraph {
cx: C,
arg: A,
task: fn(C, A) -> R,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex)
where
C: DepGraphSafe + StableHashingContextProvider<'a>,
Expand Down Expand Up @@ -229,7 +270,8 @@ impl DepGraph {
|data, key, fingerprint, _| {
data.borrow_mut().alloc_node(key, SmallVec::new(), fingerprint)
},
hash_result::<R>)
Some(hash_result::<R>)
)
}

fn with_task_impl<'a, C, A, R>(
Expand All @@ -244,24 +286,21 @@ impl DepGraph {
DepNode,
Fingerprint,
Option<TaskDeps>) -> DepNodeIndex,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex)
where
C: DepGraphSafe + StableHashingContextProvider<'a>,
{
if let Some(ref data) = self.data {
let task_deps = create_task(key).map(|deps| Lock::new(deps));

// 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.get_stable_hashing_context();

if cfg!(debug_assertions) {
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskBegin(key.clone()))
};
let hcx = hash_result.as_ref().map(|_| {
let hcx = cx.get_stable_hashing_context();
if cfg!(debug_assertions) {
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskBegin(key.clone()))
};
hcx
});

let result = if no_tcx {
task(cx, arg)
Expand All @@ -279,10 +318,12 @@ impl DepGraph {
};

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

let current_fingerprint = hash_result(&mut hcx, &result);
let current_fingerprint = hash_result.map(|hash_result| {
hash_result(&mut hcx.unwrap(), &result)
});

let dep_node_index = finish_task_and_alloc_depnode(
&data.current,
Expand All @@ -291,7 +332,9 @@ impl DepGraph {
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) && ty::tls::with_opt(|tcx| {
tcx.map(|tcx| tcx.sess.opts.debugging_opts.dep_tasks).unwrap_or(false)
});

// Determine the color of the new DepNode.
if let Some(prev_index) = data.previous.node_to_index_opt(&key) {
Expand Down Expand Up @@ -378,7 +421,7 @@ impl DepGraph {
cx: C,
arg: A,
task: fn(C, A) -> R,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex)
where
C: DepGraphSafe + StableHashingContextProvider<'a>,
Expand All @@ -392,6 +435,16 @@ impl DepGraph {
hash_result)
}

#[inline]
pub fn read_non_incr(tcx: TyCtxt<'_>) {
// Avoid loading the `dep_graph` here if we don't need to track dependencies.
// We want to load the `dep_graph` in the background.
if ty::tls::with_context(|icx| icx.task_deps.is_none()) {
return;
}
tcx.dep_graph().read(DepNode::new_no_params(DepKind::NonIncremental));
}

#[inline]
pub fn read(&self, v: DepNode) {
if let Some(ref data) = self.data {
Expand Down Expand Up @@ -674,8 +727,6 @@ impl DepGraph {
}
} else {
match dep_dep_node.kind {
DepKind::Hir |
DepKind::HirBody |
DepKind::CrateMetadata => {
if dep_dep_node.extract_def_id(tcx).is_none() {
// If the node does not exist anymore, we
Expand Down Expand Up @@ -719,7 +770,7 @@ impl DepGraph {
None => {
if !tcx.sess.has_errors() {
bug!("try_mark_previous_green() - Forcing the DepNode \
should have set its color")
should have set its color - dep node {:?}", dep_dep_node)
} else {
// If the query we just forced has resulted
// in some kind of compilation error, we
Expand Down Expand Up @@ -758,8 +809,7 @@ impl DepGraph {

// ... emitting any stored diagnostic ...

let diagnostics = tcx.queries.on_disk_cache
.load_diagnostics(tcx, prev_dep_node_index);
let diagnostics = tcx.on_disk_cache().load_diagnostics(tcx, prev_dep_node_index);

if unlikely!(diagnostics.len() > 0) {
self.emit_diagnostics(
Expand Down Expand Up @@ -801,8 +851,7 @@ impl DepGraph {
let handle = tcx.sess.diagnostic();

// Promote the previous diagnostics to the current session.
tcx.queries.on_disk_cache
.store_diagnostics(dep_node_index, diagnostics.clone().into());
tcx.on_disk_cache().store_diagnostics(dep_node_index, diagnostics.clone().into());

for diagnostic in diagnostics {
DiagnosticBuilder::new_diagnostic(handle, diagnostic).emit();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod cgu_reuse_tracker;
pub use self::dep_tracking_map::{DepTrackingMap, DepTrackingMapConfig};
pub use self::dep_node::{DepNode, DepKind, DepConstructor, WorkProductId, RecoverKey, label_strs};
pub use self::graph::{DepGraph, WorkProduct, DepNodeIndex, DepNodeColor, TaskDeps, hash_result};
pub use self::graph::WorkProductFileKind;
pub use self::graph::{WorkProductFileKind, DepGraphFuture, LoadResult, WorkProductMap, MaybeAsync};
pub use self::prev::PreviousDepGraph;
pub use self::query::DepGraphQuery;
pub use self::safe::AssertDepGraphSafe;
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/hir/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ impl fmt::Debug for DefId {

ty::tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
// Only print the path after HIR lowering is done
if tcx.is_hir_lowered() {
write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
}
}
Ok(())
})?;
Expand Down
Loading