Skip to content

Commit 71f749a

Browse files
committed
Introduce a QueryEngine trait object.
1 parent 23f9d10 commit 71f749a

File tree

3 files changed

+92
-54
lines changed

3 files changed

+92
-54
lines changed

compiler/rustc_middle/src/ty/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::middle::stability;
1414
use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
1515
use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted};
1616
use crate::traits;
17-
use crate::ty::query::{self, OnDiskCache, Queries, TyCtxtAt};
17+
use crate::ty::query::{self, OnDiskCache, TyCtxtAt};
1818
use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts};
1919
use crate::ty::TyKind::*;
2020
use crate::ty::{
@@ -968,7 +968,7 @@ pub struct GlobalCtxt<'tcx> {
968968
/// This is `None` if we are not incremental compilation mode
969969
pub(crate) on_disk_cache: Option<OnDiskCache<'tcx>>,
970970

971-
pub queries: &'tcx query::Queries<'tcx>,
971+
pub queries: &'tcx dyn query::QueryEngine<'tcx>,
972972
pub query_caches: query::QueryCaches<'tcx>,
973973

974974
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
@@ -1115,7 +1115,7 @@ impl<'tcx> TyCtxt<'tcx> {
11151115
definitions: &'tcx Definitions,
11161116
dep_graph: DepGraph,
11171117
on_disk_cache: Option<query::OnDiskCache<'tcx>>,
1118-
queries: &'tcx Queries<'tcx>,
1118+
queries: &'tcx dyn query::QueryEngine<'tcx>,
11191119
crate_name: &str,
11201120
output_filenames: &OutputFilenames,
11211121
) -> GlobalCtxt<'tcx> {

compiler/rustc_middle/src/ty/query/mod.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_data_structures::stable_hasher::StableVec;
3737
use rustc_data_structures::steal::Steal;
3838
use rustc_data_structures::svh::Svh;
3939
use rustc_data_structures::sync::Lrc;
40-
use rustc_errors::ErrorReported;
40+
use rustc_errors::{Diagnostic, ErrorReported, Handler, Level};
4141
use rustc_hir as hir;
4242
use rustc_hir::def::DefKind;
4343
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId};
@@ -122,8 +122,7 @@ impl TyCtxt<'tcx> {
122122
}
123123

124124
pub fn try_mark_green(self, dep_node: &dep_graph::DepNode) -> bool {
125-
let qcx = QueryCtxt { tcx: self, queries: self.queries };
126-
self.dep_graph.try_mark_green(qcx, dep_node).is_some()
125+
self.queries.try_mark_green(self, dep_node)
127126
}
128127
}
129128

@@ -240,6 +239,40 @@ macro_rules! define_callbacks {
240239
impl Clone for Providers {
241240
fn clone(&self) -> Self { *self }
242241
}
242+
243+
pub trait QueryEngine<'tcx>: rustc_data_structures::sync::Sync {
244+
#[cfg(parallel_compiler)]
245+
unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry);
246+
247+
fn encode_query_results(
248+
&'tcx self,
249+
tcx: TyCtxt<'tcx>,
250+
encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
251+
query_result_index: &mut on_disk_cache::EncodedQueryResultIndex,
252+
) -> opaque::FileEncodeResult;
253+
254+
fn exec_cache_promotions(&'tcx self, tcx: TyCtxt<'tcx>);
255+
256+
fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool;
257+
258+
fn try_print_query_stack(
259+
&'tcx self,
260+
tcx: TyCtxt<'tcx>,
261+
query: Option<QueryJobId<dep_graph::DepKind>>,
262+
handler: &Handler,
263+
num_frames: Option<usize>,
264+
) -> usize;
265+
266+
$($(#[$attr])*
267+
fn $name(
268+
&'tcx self,
269+
tcx: TyCtxt<$tcx>,
270+
span: Span,
271+
key: query_keys::$name<$tcx>,
272+
lookup: QueryLookup,
273+
mode: QueryMode,
274+
) -> Option<query_stored::$name<$tcx>>;)*
275+
}
243276
};
244277
}
245278

compiler/rustc_middle/src/ty/query/plumbing.rs

+53-48
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! generate the actual methods on tcx which find and execute the provider,
33
//! manage the caches, and so forth.
44
5-
use crate::dep_graph::{self, DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex};
6-
use crate::ty::query::{on_disk_cache, queries, Queries, Query};
5+
use crate::dep_graph::{DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex};
6+
use crate::ty::query::{on_disk_cache, queries, Query};
77
use crate::ty::tls::{self, ImplicitCtxt};
88
use crate::ty::{self, TyCtxt};
99
use rustc_query_system::dep_graph::HasDepContext;
@@ -13,7 +13,7 @@ use rustc_query_system::query::{QueryContext, QueryDescription};
1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_data_structures::sync::Lock;
1515
use rustc_data_structures::thin_vec::ThinVec;
16-
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, Handler, Level};
16+
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder};
1717
use rustc_serialize::opaque;
1818
use rustc_span::def_id::{DefId, LocalDefId};
1919
use rustc_span::Span;
@@ -266,49 +266,6 @@ impl<'tcx> QueryCtxt<'tcx> {
266266
}
267267
}
268268

269-
impl<'tcx> Queries<'tcx> {
270-
pub fn try_print_query_stack(
271-
&'tcx self,
272-
tcx: TyCtxt<'tcx>,
273-
query: Option<QueryJobId<dep_graph::DepKind>>,
274-
handler: &Handler,
275-
num_frames: Option<usize>,
276-
) -> usize {
277-
let query_map = self.try_collect_active_jobs();
278-
279-
let mut current_query = query;
280-
let mut i = 0;
281-
282-
while let Some(query) = current_query {
283-
if Some(i) == num_frames {
284-
break;
285-
}
286-
let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query))
287-
{
288-
info
289-
} else {
290-
break;
291-
};
292-
let mut diag = Diagnostic::new(
293-
Level::FailureNote,
294-
&format!(
295-
"#{} [{}] {}",
296-
i,
297-
query_info.info.query.name(),
298-
query_info.info.query.describe(QueryCtxt { tcx, queries: self })
299-
),
300-
);
301-
diag.span = tcx.sess.source_map().guess_head_span(query_info.info.span).into();
302-
handler.force_print_diagnostic(diag);
303-
304-
current_query = query_info.job.parent;
305-
i += 1;
306-
}
307-
308-
i
309-
}
310-
}
311-
312269
/// This struct stores metadata about each Query.
313270
///
314271
/// Information is retrieved by indexing the `QUERIES` array using the integer value
@@ -689,14 +646,16 @@ macro_rules! define_queries_struct {
689646

690647
Some(jobs)
691648
}
649+
}
692650

651+
impl QueryEngine<'tcx> for Queries<'tcx> {
693652
#[cfg(parallel_compiler)]
694-
pub unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry) {
653+
unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry) {
695654
let tcx = QueryCtxt { tcx, queries: self };
696655
rustc_query_system::query::deadlock(tcx, registry)
697656
}
698657

699-
pub(crate) fn encode_query_results(
658+
fn encode_query_results(
700659
&'tcx self,
701660
tcx: TyCtxt<'tcx>,
702661
encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
@@ -711,6 +670,52 @@ macro_rules! define_queries_struct {
711670
tcx.dep_graph.exec_cache_promotions(tcx)
712671
}
713672

673+
fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
674+
let qcx = QueryCtxt { tcx, queries: self };
675+
tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
676+
}
677+
678+
fn try_print_query_stack(
679+
&'tcx self,
680+
tcx: TyCtxt<'tcx>,
681+
query: Option<QueryJobId<dep_graph::DepKind>>,
682+
handler: &Handler,
683+
num_frames: Option<usize>,
684+
) -> usize {
685+
let query_map = self.try_collect_active_jobs();
686+
687+
let mut current_query = query;
688+
let mut i = 0;
689+
690+
while let Some(query) = current_query {
691+
if Some(i) == num_frames {
692+
break;
693+
}
694+
let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query))
695+
{
696+
info
697+
} else {
698+
break;
699+
};
700+
let mut diag = Diagnostic::new(
701+
Level::FailureNote,
702+
&format!(
703+
"#{} [{}] {}",
704+
i,
705+
query_info.info.query.name(),
706+
query_info.info.query.describe(QueryCtxt { tcx, queries: self })
707+
),
708+
);
709+
diag.span = tcx.sess.source_map().guess_head_span(query_info.info.span).into();
710+
handler.force_print_diagnostic(diag);
711+
712+
current_query = query_info.job.parent;
713+
i += 1;
714+
}
715+
716+
i
717+
}
718+
714719
$($(#[$attr])*
715720
#[inline(always)]
716721
fn $name(

0 commit comments

Comments
 (0)