Skip to content

Commit f5adff6

Browse files
committed
Auto merge of #109611 - Zoxc:query-engine-rem, r=cjgillot
Remove `QueryEngine` trait This removes the `QueryEngine` trait and `Queries` from `rustc_query_impl` and replaced them with function pointers and fields in `QuerySystem`. As a side effect `OnDiskCache` is moved back into `rustc_middle` and the `OnDiskCache` trait is also removed. This has a couple of benefits. - `TyCtxt` is used in the query system instead of the removed `QueryCtxt` which is larger. - Function pointers are more flexible to work with. A variant of #107802 is included which avoids the double indirection. For #108938 we can name entry point `__rust_end_short_backtrace` to avoid some overhead. For #108062 it avoids the duplicate `QueryEngine` structs. - `QueryContext` now implements `DepContext` which avoids many `dep_context()` calls in `rustc_query_system`. - The `rustc_driver` size is reduced by 0.33%, hopefully that means some bootstrap improvements. - This avoids the unsafe code around the `QueryEngine` trait. r? `@cjgillot`
2 parents 87b1f89 + b694373 commit f5adff6

File tree

21 files changed

+344
-356
lines changed

21 files changed

+344
-356
lines changed

Diff for: 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",

Diff for: 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
}

Diff for: 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

Diff for: 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" }

Diff for: 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
}

Diff for: 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
})

Diff for: 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,

Diff for: compiler/rustc_interface/src/util.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
170170
) -> R {
171171
use rustc_data_structures::jobserver;
172172
use rustc_middle::ty::tls;
173-
use rustc_query_impl::{deadlock, QueryContext, QueryCtxt};
173+
use rustc_query_impl::QueryCtxt;
174+
use rustc_query_system::query::{deadlock, QueryContext};
174175

175176
let registry = sync::Registry::new(threads);
176177
let mut builder = rayon::ThreadPoolBuilder::new()
@@ -182,7 +183,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
182183
// On deadlock, creates a new thread and forwards information in thread
183184
// locals to it. The new thread runs the deadlock handler.
184185
let query_map = tls::with(|tcx| {
185-
QueryCtxt::from_tcx(tcx)
186+
QueryCtxt::new(tcx)
186187
.try_collect_active_jobs()
187188
.expect("active jobs shouldn't be locked in deadlock handler")
188189
});

Diff for: 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" }

Diff for: 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
}

Diff for: 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

0 commit comments

Comments
 (0)