Skip to content

Commit 66d8543

Browse files
committed
Remove QueryEngine trait
1 parent 897a146 commit 66d8543

File tree

20 files changed

+271
-326
lines changed

20 files changed

+271
-326
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -3641,6 +3641,7 @@ dependencies = [
36413641
"rustc_plugin_impl",
36423642
"rustc_privacy",
36433643
"rustc_query_impl",
3644+
"rustc_query_system",
36443645
"rustc_resolve",
36453646
"rustc_session",
36463647
"rustc_span",
@@ -3770,6 +3771,7 @@ dependencies = [
37703771
"derive_more",
37713772
"either",
37723773
"gsgdt",
3774+
"measureme",
37733775
"polonius-engine",
37743776
"rustc-rayon",
37753777
"rustc-rayon-core",

compiler/rustc_incremental/src/persist/load.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::errors;
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::memmap::Mmap;
66
use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
7-
use rustc_middle::ty::OnDiskCache;
7+
use rustc_middle::query::on_disk_cache::OnDiskCache;
88
use rustc_serialize::opaque::MemDecoder;
99
use rustc_serialize::Decodable;
1010
use rustc_session::config::IncrementalStateAssertion;
@@ -211,7 +211,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
211211
/// If we are not in incremental compilation mode, returns `None`.
212212
/// Otherwise, tries to load the query result cache from disk,
213213
/// creating an empty cache if it could not be loaded.
214-
pub fn load_query_result_cache<'a, C: OnDiskCache<'a>>(sess: &'a Session) -> Option<C> {
214+
pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache<'_>> {
215215
if sess.opts.incremental.is_none() {
216216
return None;
217217
}
@@ -223,7 +223,9 @@ pub fn load_query_result_cache<'a, C: OnDiskCache<'a>>(sess: &'a Session) -> Opt
223223
&query_cache_path(sess),
224224
sess.is_nightly_build(),
225225
) {
226-
LoadResult::Ok { data: (bytes, start_pos) } => Some(C::new(sess, bytes, start_pos)),
227-
_ => Some(C::new_empty(sess.source_map())),
226+
LoadResult::Ok { data: (bytes, start_pos) } => {
227+
Some(OnDiskCache::new(sess, bytes, start_pos))
228+
}
229+
_ => Some(OnDiskCache::new_empty(sess.source_map())),
228230
}
229231
}

compiler/rustc_incremental/src/persist/save.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
4848
move || {
4949
sess.time("incr_comp_persist_result_cache", || {
5050
// Drop the memory map so that we can remove the file and write to it.
51-
if let Some(odc) = &tcx.on_disk_cache {
51+
if let Some(odc) = &tcx.query_system.on_disk_cache {
5252
odc.drop_serialized_data(tcx);
5353
}
5454

compiler/rustc_interface/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ rustc_lint = { path = "../rustc_lint" }
4444
rustc_errors = { path = "../rustc_errors" }
4545
rustc_plugin_impl = { path = "../rustc_plugin_impl" }
4646
rustc_privacy = { path = "../rustc_privacy" }
47+
rustc_query_system = { path = "../rustc_query_system" }
4748
rustc_query_impl = { path = "../rustc_query_impl" }
4849
rustc_resolve = { path = "../rustc_resolve" }
4950
rustc_target = { path = "../rustc_target" }

compiler/rustc_interface/src/interface.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_lint::LintStore;
1212
use rustc_middle::ty;
1313
use rustc_parse::maybe_new_parser_from_source_str;
1414
use rustc_query_impl::QueryCtxt;
15+
use rustc_query_system::query::print_query_stack;
1516
use rustc_session::config::{self, CheckCfg, ErrorOutputType, Input, OutputFilenames};
1617
use rustc_session::lint;
1718
use rustc_session::parse::{CrateConfig, ParseSess};
@@ -317,7 +318,7 @@ pub fn try_print_query_stack(handler: &Handler, num_frames: Option<usize>) {
317318
// state if it was responsible for triggering the panic.
318319
let i = ty::tls::with_context_opt(|icx| {
319320
if let Some(icx) = icx {
320-
QueryCtxt::from_tcx(icx.tcx).try_print_query_stack(icx.query, handler, num_frames)
321+
print_query_stack(QueryCtxt { tcx: icx.tcx }, icx.query, handler, num_frames)
321322
} else {
322323
0
323324
}

compiler/rustc_interface/src/passes.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_mir_build as mir_build;
2323
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
2424
use rustc_passes::{self, hir_stats, layout_test};
2525
use rustc_plugin_impl as plugin;
26-
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
2726
use rustc_resolve::Resolver;
2827
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
2928
use rustc_session::cstore::{MetadataLoader, Untracked};
@@ -669,7 +668,6 @@ pub fn create_global_ctxt<'tcx>(
669668
lint_store: Lrc<LintStore>,
670669
dep_graph: DepGraph,
671670
untracked: Untracked,
672-
queries: &'tcx OnceCell<TcxQueries<'tcx>>,
673671
gcx_cell: &'tcx OnceCell<GlobalCtxt<'tcx>>,
674672
arena: &'tcx WorkerLocal<Arena<'tcx>>,
675673
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
@@ -693,10 +691,6 @@ pub fn create_global_ctxt<'tcx>(
693691
callback(sess, &mut local_providers, &mut extern_providers);
694692
}
695693

696-
let queries = queries.get_or_init(|| {
697-
TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache)
698-
});
699-
700694
sess.time("setup_global_ctxt", || {
701695
gcx_cell.get_or_init(move || {
702696
TyCtxt::create_global_ctxt(
@@ -706,9 +700,9 @@ pub fn create_global_ctxt<'tcx>(
706700
hir_arena,
707701
untracked,
708702
dep_graph,
709-
queries.on_disk_cache.as_ref().map(OnDiskCache::as_dyn),
710-
queries.as_dyn(),
703+
query_result_on_disk_cache,
711704
rustc_query_impl::query_callbacks(arena),
705+
rustc_query_impl::query_system_fns(local_providers, extern_providers),
712706
)
713707
})
714708
})

compiler/rustc_interface/src/queries.rs

-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_metadata::creader::CStore;
1616
use rustc_middle::arena::Arena;
1717
use rustc_middle::dep_graph::DepGraph;
1818
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
19-
use rustc_query_impl::Queries as TcxQueries;
2019
use rustc_session::config::{self, OutputFilenames, OutputType};
2120
use rustc_session::cstore::Untracked;
2221
use rustc_session::{output::find_crate_name, Session};
@@ -81,7 +80,6 @@ impl<T> Default for Query<T> {
8180
pub struct Queries<'tcx> {
8281
compiler: &'tcx Compiler,
8382
gcx_cell: OnceCell<GlobalCtxt<'tcx>>,
84-
queries: OnceCell<TcxQueries<'tcx>>,
8583

8684
arena: WorkerLocal<Arena<'tcx>>,
8785
hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
@@ -102,7 +100,6 @@ impl<'tcx> Queries<'tcx> {
102100
Queries {
103101
compiler,
104102
gcx_cell: OnceCell::new(),
105-
queries: OnceCell::new(),
106103
arena: WorkerLocal::new(|_| Arena::default()),
107104
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
108105
dep_graph_future: Default::default(),
@@ -225,7 +222,6 @@ impl<'tcx> Queries<'tcx> {
225222
lint_store,
226223
self.dep_graph()?.steal(),
227224
untracked,
228-
&self.queries,
229225
&self.gcx_cell,
230226
&self.arena,
231227
&self.hir_arena,

compiler/rustc_interface/src/util.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
168168
) -> R {
169169
use rustc_data_structures::jobserver;
170170
use rustc_middle::ty::tls;
171-
use rustc_query_impl::{deadlock, QueryContext, QueryCtxt};
171+
use rustc_query_impl::QueryCtxt;
172+
use rustc_query_system::query::{deadlock, QueryContext};
172173

173174
let mut builder = rayon::ThreadPoolBuilder::new()
174175
.thread_name(|_| "rustc".to_string())
@@ -179,7 +180,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
179180
// On deadlock, creates a new thread and forwards information in thread
180181
// locals to it. The new thread runs the deadlock handler.
181182
let query_map = tls::with(|tcx| {
182-
QueryCtxt::from_tcx(tcx)
183+
QueryCtxt::new(tcx)
183184
.try_collect_active_jobs()
184185
.expect("active jobs shouldn't be locked in deadlock handler")
185186
});

compiler/rustc_middle/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ chalk-ir = "0.87.0"
1111
derive_more = "0.99.17"
1212
either = "1.5.0"
1313
gsgdt = "0.1.2"
14+
measureme = "10.0.0"
1415
polonius-engine = "0.13.0"
1516
rustc_apfloat = { path = "../rustc_apfloat" }
1617
rustc_arena = { path = "../rustc_arena" }

compiler/rustc_middle/src/mir/interpret/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>>(
227227
// References to statics doesn't need to know about their allocations,
228228
// just about its `DefId`.
229229
AllocDiscriminant::Static.encode(encoder);
230-
did.encode(encoder);
230+
// Cannot use `did.encode(encoder)` because of a bug around
231+
// specializations and method calls.
232+
Encodable::<E>::encode(&did, encoder);
231233
}
232234
}
233235
}

compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_span::def_id::LOCAL_CRATE;
99

1010
pub mod erase;
1111
mod keys;
12+
pub mod on_disk_cache;
1213
pub use keys::{AsLocalKey, Key, LocalCrate};
1314

1415
// Each of these queries corresponds to a function pointer field in the

compiler/rustc_middle/src/query/on_disk_cache.rs

+9-47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::QueryCtxt;
21
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
32
use rustc_data_structures::memmap::Mmap;
43
use rustc_data_structures::stable_hasher::Hash64;
@@ -13,8 +12,7 @@ use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
1312
use rustc_middle::mir::{self, interpret};
1413
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
1514
use rustc_middle::ty::{self, Ty, TyCtxt};
16-
use rustc_query_system::dep_graph::DepContext;
17-
use rustc_query_system::query::{QueryCache, QuerySideEffects};
15+
use rustc_query_system::query::QuerySideEffects;
1816
use rustc_serialize::{
1917
opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder},
2018
Decodable, Decoder, Encodable, Encoder,
@@ -123,7 +121,7 @@ struct SourceFileIndex(u32);
123121
pub struct AbsoluteBytePos(u64);
124122

125123
impl AbsoluteBytePos {
126-
fn new(pos: usize) -> AbsoluteBytePos {
124+
pub fn new(pos: usize) -> AbsoluteBytePos {
127125
AbsoluteBytePos(pos.try_into().expect("Incremental cache file size overflowed u64."))
128126
}
129127

@@ -158,9 +156,9 @@ impl EncodedSourceFileId {
158156
}
159157
}
160158

161-
impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
159+
impl<'sess> OnDiskCache<'sess> {
162160
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
163-
fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self {
161+
pub fn new(sess: &'sess Session, data: Mmap, start_pos: usize) -> Self {
164162
debug_assert!(sess.opts.incremental.is_some());
165163

166164
// Wrap in a scope so we can borrow `data`.
@@ -193,7 +191,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
193191
}
194192
}
195193

196-
fn new_empty(source_map: &'sess SourceMap) -> Self {
194+
pub fn new_empty(source_map: &'sess SourceMap) -> Self {
197195
Self {
198196
serialized_data: RwLock::new(None),
199197
file_index_to_stable_id: Default::default(),
@@ -215,7 +213,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
215213
/// Cache promotions require invoking queries, which needs to read the serialized data.
216214
/// In order to serialize the new on-disk cache, the former on-disk cache file needs to be
217215
/// deleted, hence we won't be able to refer to its memmapped data.
218-
fn drop_serialized_data(&self, tcx: TyCtxt<'_>) {
216+
pub fn drop_serialized_data(&self, tcx: TyCtxt<'_>) {
219217
// Load everything into memory so we can write it out to the on-disk
220218
// cache. The vast majority of cacheable query results should already
221219
// be in memory, so this should be a cheap operation.
@@ -227,7 +225,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
227225
*self.serialized_data.write() = None;
228226
}
229227

230-
fn serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult {
228+
pub fn serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult {
231229
// Serializing the `DepGraph` should not modify it.
232230
tcx.dep_graph.with_ignore(|| {
233231
// Allocate `SourceFileIndex`es.
@@ -269,7 +267,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
269267
tcx.sess.time("encode_query_results", || {
270268
let enc = &mut encoder;
271269
let qri = &mut query_result_index;
272-
QueryCtxt::from_tcx(tcx).encode_query_results(enc, qri);
270+
(tcx.query_system.fns.encode_query_results)(tcx, enc, qri);
273271
});
274272

275273
// Encode side effects.
@@ -358,12 +356,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
358356
encoder.finish()
359357
})
360358
}
361-
}
362-
363-
impl<'sess> OnDiskCache<'sess> {
364-
pub fn as_dyn(&self) -> &dyn rustc_middle::ty::OnDiskCache<'sess> {
365-
self as _
366-
}
367359

368360
/// Loads a `QuerySideEffects` created during the previous compilation session.
369361
pub fn load_side_effects(
@@ -855,7 +847,7 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
855847
/// encode the specified tag, then the given value, then the number of
856848
/// bytes taken up by tag and value. On decoding, we can then verify that
857849
/// we get the expected tag and read the expected number of bytes.
858-
fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V) {
850+
pub fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V) {
859851
let start_pos = self.position();
860852

861853
tag.encode(self);
@@ -1032,33 +1024,3 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
10321024
self.encode(&mut e.encoder);
10331025
}
10341026
}
1035-
1036-
pub(crate) fn encode_query_results<'a, 'tcx, Q>(
1037-
query: Q,
1038-
qcx: QueryCtxt<'tcx>,
1039-
encoder: &mut CacheEncoder<'a, 'tcx>,
1040-
query_result_index: &mut EncodedDepNodeIndex,
1041-
) where
1042-
Q: super::QueryConfigRestored<'tcx>,
1043-
Q::RestoredValue: Encodable<CacheEncoder<'a, 'tcx>>,
1044-
{
1045-
let _timer = qcx
1046-
.tcx
1047-
.profiler()
1048-
.verbose_generic_activity_with_arg("encode_query_results_for", query.name());
1049-
1050-
assert!(query.query_state(qcx).all_inactive());
1051-
let cache = query.query_cache(qcx);
1052-
cache.iter(&mut |key, value, dep_node| {
1053-
if query.cache_on_disk(qcx.tcx, &key) {
1054-
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
1055-
1056-
// Record position of the cache entry.
1057-
query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.encoder.position())));
1058-
1059-
// Encode the type check tables with the `SerializedDepNodeIndex`
1060-
// as tag.
1061-
encoder.encode_tagged(dep_node, &Q::restore(*value));
1062-
}
1063-
});
1064-
}

compiler/rustc_middle/src/ty/codec.rs

-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ impl_arena_copy_decoder! {<'tcx>
500500
macro_rules! implement_ty_decoder {
501501
($DecoderName:ident <$($typaram:tt),*>) => {
502502
mod __ty_decoder_impl {
503-
use std::borrow::Cow;
504503
use rustc_serialize::Decoder;
505504

506505
use super::$DecoderName;

0 commit comments

Comments
 (0)