diff --git a/src/librustc_query_system/query/config.rs b/src/librustc_query_system/query/config.rs index 48fbdfb153eee..a334e897e400e 100644 --- a/src/librustc_query_system/query/config.rs +++ b/src/librustc_query_system/query/config.rs @@ -28,6 +28,7 @@ pub(crate) struct QueryVtable { pub anon: bool, pub dep_kind: CTX::DepKind, pub eval_always: bool, + pub to_dep_node: fn(CTX, &K) -> DepNode, // Don't use this method to compute query results, instead use the methods on TyCtxt pub compute: fn(CTX, K) -> V, @@ -39,6 +40,10 @@ pub(crate) struct QueryVtable { } impl QueryVtable { + pub(crate) fn to_dep_node(&self, tcx: CTX, key: &K) -> DepNode { + (self.to_dep_node)(tcx, key) + } + pub(crate) fn compute(&self, tcx: CTX, key: K) -> V { (self.compute)(tcx, key) } @@ -112,6 +117,7 @@ where const VTABLE: QueryVtable = QueryVtable { anon: Q::ANON, dep_kind: Q::DEP_KIND, + to_dep_node: Q::to_dep_node, eval_always: Q::EVAL_ALWAYS, compute: Q::compute, hash_result: Q::hash_result, diff --git a/src/librustc_query_system/query/plumbing.rs b/src/librustc_query_system/query/plumbing.rs index ff01538d95e4c..74a2288cfa790 100644 --- a/src/librustc_query_system/query/plumbing.rs +++ b/src/librustc_query_system/query/plumbing.rs @@ -382,17 +382,21 @@ where } #[inline(always)] -fn try_execute_query( +fn try_execute_query( tcx: CTX, + state: &QueryState, span: Span, - key: Q::Key, - lookup: QueryLookup<'_, CTX, Q::Key, ::Sharded>, -) -> Q::Stored + key: C::Key, + lookup: QueryLookup<'_, CTX, C::Key, C::Sharded>, + query: &QueryVtable, +) -> C::Stored where - Q: QueryDescription, + C: QueryCache, + C::Key: Eq + Clone + Debug, + C::Stored: Clone, CTX: QueryContext, { - let job = match JobOwner::try_start(tcx, Q::query_state(tcx), span, &key, lookup, &Q::VTABLE) { + let job = match JobOwner::try_start(tcx, state, span, &key, lookup, query) { TryGetJob::NotYetStarted(job) => job, TryGetJob::Cycle(result) => return result, #[cfg(parallel_compiler)] @@ -406,18 +410,32 @@ where // expensive for some `DepKind`s. if !tcx.dep_graph().is_fully_enabled() { let null_dep_node = DepNode::new_no_params(DepKind::NULL); - return force_query_with_job(tcx, key, job, null_dep_node, &Q::VTABLE).0; + return force_query_with_job(tcx, key, job, null_dep_node, query).0; } - if Q::ANON { - let (result, dep_node_index) = try_execute_anon_query(tcx, key, job.id, &Q::VTABLE); + if query.anon { + let prof_timer = tcx.profiler().query_provider(); + + let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| { + tcx.start_query(job.id, diagnostics, |tcx| { + tcx.dep_graph().with_anon_task(query.dep_kind, || query.compute(tcx, key)) + }) + }); + + prof_timer.finish_with_query_invocation_id(dep_node_index.into()); + + tcx.dep_graph().read_index(dep_node_index); + + if unlikely!(!diagnostics.is_empty()) { + tcx.store_diagnostics_for_anon_node(dep_node_index, diagnostics); + } return job.complete(tcx, result, dep_node_index); } - let dep_node = Q::to_dep_node(tcx, &key); + let dep_node = query.to_dep_node(tcx, &key); - if !Q::EVAL_ALWAYS { + if !query.eval_always { // The diagnostics for this query will be // promoted to the current session during // `try_mark_green()`, so we can ignore them here. @@ -431,7 +449,7 @@ where prev_dep_node_index, dep_node_index, &dep_node, - &Q::VTABLE, + query, ), dep_node_index, ) @@ -442,40 +460,11 @@ where } } - let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, &Q::VTABLE); + let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, query); tcx.dep_graph().read_index(dep_node_index); result } -fn try_execute_anon_query( - tcx: CTX, - key: K, - job_id: QueryJobId, - query: &QueryVtable, -) -> (V, DepNodeIndex) -where - CTX: QueryContext, -{ - debug_assert!(query.anon); - let prof_timer = tcx.profiler().query_provider(); - - let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| { - tcx.start_query(job_id, diagnostics, |tcx| { - tcx.dep_graph().with_anon_task(query.dep_kind, || query.compute(tcx, key)) - }) - }); - - prof_timer.finish_with_query_invocation_id(dep_node_index.into()); - - tcx.dep_graph().read_index(dep_node_index); - - if unlikely!(!diagnostics.is_empty()) { - tcx.store_diagnostics_for_anon_node(dep_node_index, diagnostics); - } - - (result, dep_node_index) -} - fn load_from_disk_and_cache_in_memory( tcx: CTX, key: K, @@ -639,7 +628,7 @@ where tcx.dep_graph().read_index(index); value.clone() }, - |key, lookup| try_execute_query::(tcx, span, key, lookup), + |key, lookup| try_execute_query(tcx, Q::query_state(tcx), span, key, lookup, &Q::VTABLE), ) }