Skip to content

Commit 3d352fd

Browse files
committed
Auto merge of #115118 - Zoxc:rustc-query-encode, r=<try>
Extract suitable code from rustc_query_impl into a new crate rustc_query_misc This extracts code from `rustc_query_impl` into a new crate `rustc_query_misc` in order to reduce the compile time of `rustc_query_impl`. The compile time of the new crate is roughly 60% of the remaining `rustc_query_impl` . I've picked code which should not impact performance or result in duplicate generic code generation. Encoding and decoding of query results and profiling handling is moved along with some other minor methods which use dynamic dispatch. r? `@cjgillot`
2 parents 90f3a6f + c9ce621 commit 3d352fd

File tree

10 files changed

+314
-185
lines changed

10 files changed

+314
-185
lines changed

Cargo.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -3959,6 +3959,7 @@ dependencies = [
39593959
"rustc_plugin_impl",
39603960
"rustc_privacy",
39613961
"rustc_query_impl",
3962+
"rustc_query_misc",
39623963
"rustc_query_system",
39633964
"rustc_resolve",
39643965
"rustc_session",
@@ -4304,7 +4305,6 @@ name = "rustc_query_impl"
43044305
version = "0.0.0"
43054306
dependencies = [
43064307
"field-offset",
4307-
"measureme",
43084308
"memoffset",
43094309
"rustc-rayon-core",
43104310
"rustc_data_structures",
@@ -4313,6 +4313,7 @@ dependencies = [
43134313
"rustc_index",
43144314
"rustc_macros",
43154315
"rustc_middle",
4316+
"rustc_query_misc",
43164317
"rustc_query_system",
43174318
"rustc_serialize",
43184319
"rustc_session",
@@ -4321,6 +4322,20 @@ dependencies = [
43214322
"tracing",
43224323
]
43234324

4325+
[[package]]
4326+
name = "rustc_query_misc"
4327+
version = "0.0.0"
4328+
dependencies = [
4329+
"measureme",
4330+
"rustc_data_structures",
4331+
"rustc_hir",
4332+
"rustc_index",
4333+
"rustc_middle",
4334+
"rustc_query_system",
4335+
"rustc_serialize",
4336+
"rustc_span",
4337+
]
4338+
43244339
[[package]]
43254340
name = "rustc_query_system"
43264341
version = "0.0.0"

compiler/rustc_interface/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ rustc_plugin_impl = { path = "../rustc_plugin_impl" }
4747
rustc_privacy = { path = "../rustc_privacy" }
4848
rustc_query_system = { path = "../rustc_query_system" }
4949
rustc_query_impl = { path = "../rustc_query_impl" }
50+
rustc_query_misc = { path = "../rustc_query_misc" }
5051
rustc_resolve = { path = "../rustc_resolve" }
5152
rustc_target = { path = "../rustc_target" }
5253
rustc_trait_selection = { path = "../rustc_trait_selection" }

compiler/rustc_interface/src/queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl Compiler {
340340
{
341341
let _prof_timer =
342342
queries.session().prof.generic_activity("self_profile_alloc_query_strings");
343-
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
343+
gcx.enter(rustc_query_misc::alloc_self_profile_query_strings);
344344
}
345345

346346
self.session()

compiler/rustc_query_impl/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ edition = "2021"
88

99
[dependencies]
1010
field-offset = "0.3.5"
11-
measureme = "10.0.0"
1211
rustc_data_structures = { path = "../rustc_data_structures" }
1312
rustc_errors = { path = "../rustc_errors" }
1413
rustc_hir = { path = "../rustc_hir" }
1514
rustc_index = { path = "../rustc_index" }
1615
rustc_macros = { path = "../rustc_macros" }
1716
rustc_middle = { path = "../rustc_middle" }
1817
rustc_query_system = { path = "../rustc_query_system" }
18+
rustc_query_misc = { path = "../rustc_query_misc" }
1919
rustc-rayon-core = { version = "0.5.0", optional = true }
2020
rustc_serialize = { path = "../rustc_serialize" }
2121
rustc_session = { path = "../rustc_session" }

compiler/rustc_query_impl/src/lib.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// this shouldn't be necessary, but the check for `&mut _` is too naive and denies returning a function pointer that takes a mut ref
55
#![feature(const_mut_refs)]
66
#![feature(const_refs_to_cell)]
7-
#![feature(min_specialization)]
87
#![feature(never_type)]
98
#![feature(rustc_attrs)]
109
#![recursion_limit = "256"]
@@ -16,40 +15,35 @@
1615
#[macro_use]
1716
extern crate rustc_middle;
1817

19-
use crate::plumbing::{__rust_begin_short_backtrace, encode_all_query_results, try_mark_green};
18+
use crate::plumbing::{__rust_begin_short_backtrace, try_mark_green};
2019
use field_offset::offset_of;
2120
use rustc_data_structures::stable_hasher::HashStable;
2221
use rustc_data_structures::sync::AtomicU64;
2322
use rustc_middle::arena::Arena;
2423
use rustc_middle::dep_graph::DepNodeIndex;
2524
use rustc_middle::dep_graph::{self, DepKind, DepKindStruct};
2625
use rustc_middle::query::erase::{erase, restore, Erase};
27-
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
28-
use rustc_middle::query::plumbing::{
29-
DynamicQuery, QueryKeyStringCache, QuerySystem, QuerySystemFns,
30-
};
26+
use rustc_middle::query::on_disk_cache::OnDiskCache;
27+
use rustc_middle::query::plumbing::{DynamicQuery, QuerySystem, QuerySystemFns};
3128
use rustc_middle::query::AsLocalKey;
3229
use rustc_middle::query::{
3330
queries, DynamicQueries, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
3431
};
3532
use rustc_middle::ty::TyCtxt;
33+
use rustc_query_misc::{encode_all_query_results, query_utils};
3634
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
3735
use rustc_query_system::ich::StableHashingContext;
3836
use rustc_query_system::query::{
3937
get_query_incr, get_query_non_incr, HashResult, QueryCache, QueryConfig, QueryInfo, QueryMap,
4038
QueryMode, QueryState,
4139
};
4240
use rustc_query_system::HandleCycleError;
43-
use rustc_query_system::Value;
4441
use rustc_span::{ErrorGuaranteed, Span};
4542

4643
#[macro_use]
4744
mod plumbing;
4845
pub use crate::plumbing::QueryCtxt;
4946

50-
mod profiling_support;
51-
pub use self::profiling_support::alloc_self_profile_query_strings;
52-
5347
struct DynamicConfig<
5448
'tcx,
5549
C: QueryCache,

compiler/rustc_query_impl/src/plumbing.rs

+10-172
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,24 @@
22
//! generate the actual methods on tcx which find and execute the provider,
33
//! manage the caches, and so forth.
44
5-
use crate::rustc_middle::dep_graph::DepContext;
6-
use crate::rustc_middle::ty::TyEncoder;
75
use crate::QueryConfigRestored;
86
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
97
use rustc_data_structures::sync::Lock;
108
use rustc_errors::Diagnostic;
119

12-
use rustc_index::Idx;
1310
use rustc_middle::dep_graph::dep_kinds;
1411
use rustc_middle::dep_graph::{
1512
self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex,
1613
};
17-
use rustc_middle::query::on_disk_cache::AbsoluteBytePos;
18-
use rustc_middle::query::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex};
1914
use rustc_middle::query::Key;
2015
use rustc_middle::ty::tls::{self, ImplicitCtxt};
2116
use rustc_middle::ty::{self, print::with_no_queries, TyCtxt};
2217
use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
2318
use rustc_query_system::ich::StableHashingContext;
2419
use rustc_query_system::query::{
25-
force_query, QueryCache, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects,
26-
QueryStackFrame,
20+
force_query, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame,
2721
};
2822
use rustc_query_system::{LayoutOfDepth, QueryOverflow};
29-
use rustc_serialize::Decodable;
30-
use rustc_serialize::Encodable;
3123
use rustc_session::Limit;
3224
use rustc_span::def_id::LOCAL_CRATE;
3325
use std::num::NonZeroU64;
@@ -180,16 +172,6 @@ pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepN
180172
tcx.dep_graph.try_mark_green(QueryCtxt::new(tcx), dep_node).is_some()
181173
}
182174

183-
pub(super) fn encode_all_query_results<'tcx>(
184-
tcx: TyCtxt<'tcx>,
185-
encoder: &mut CacheEncoder<'_, 'tcx>,
186-
query_result_index: &mut EncodedDepNodeIndex,
187-
) {
188-
for encode in super::ENCODE_QUERY_RESULTS.iter().copied().flatten() {
189-
encode(tcx, encoder, query_result_index);
190-
}
191-
}
192-
193175
macro_rules! handle_cycle_error {
194176
([]) => {{
195177
rustc_query_system::HandleCycleError::Error
@@ -254,10 +236,10 @@ macro_rules! feedable {
254236
}
255237

256238
macro_rules! hash_result {
257-
([][$V:ty]) => {{
258-
Some(|hcx, result| dep_graph::hash_result(hcx, &restore::<$V>(*result)))
239+
([][$f:path]) => {{
240+
Some($f)
259241
}};
260-
([(no_hash) $($rest:tt)*][$V:ty]) => {{
242+
([(no_hash) $($rest:tt)*]$args:tt) => {{
261243
None
262244
}};
263245
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
@@ -341,33 +323,6 @@ pub(crate) fn create_query_frame<
341323
QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash)
342324
}
343325

344-
pub(crate) fn encode_query_results<'a, 'tcx, Q>(
345-
query: Q::Config,
346-
qcx: QueryCtxt<'tcx>,
347-
encoder: &mut CacheEncoder<'a, 'tcx>,
348-
query_result_index: &mut EncodedDepNodeIndex,
349-
) where
350-
Q: super::QueryConfigRestored<'tcx>,
351-
Q::RestoredValue: Encodable<CacheEncoder<'a, 'tcx>>,
352-
{
353-
let _timer = qcx.profiler().generic_activity_with_arg("encode_query_results_for", query.name());
354-
355-
assert!(query.query_state(qcx).all_inactive());
356-
let cache = query.query_cache(qcx);
357-
cache.iter(&mut |key, value, dep_node| {
358-
if query.cache_on_disk(qcx.tcx, &key) {
359-
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
360-
361-
// Record position of the cache entry.
362-
query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.position())));
363-
364-
// Encode the type check tables with the `SerializedDepNodeIndex`
365-
// as tag.
366-
encoder.encode_tagged(dep_node, &Q::restore(*value));
367-
}
368-
});
369-
}
370-
371326
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
372327
where
373328
Q: QueryConfig<QueryCtxt<'tcx>>,
@@ -382,38 +337,6 @@ where
382337
}
383338
}
384339

385-
pub(crate) fn loadable_from_disk<'tcx>(tcx: TyCtxt<'tcx>, id: SerializedDepNodeIndex) -> bool {
386-
if let Some(cache) = tcx.query_system.on_disk_cache.as_ref() {
387-
cache.loadable_from_disk(id)
388-
} else {
389-
false
390-
}
391-
}
392-
393-
pub(crate) fn try_load_from_disk<'tcx, V>(
394-
tcx: TyCtxt<'tcx>,
395-
prev_index: SerializedDepNodeIndex,
396-
index: DepNodeIndex,
397-
) -> Option<V>
398-
where
399-
V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
400-
{
401-
let on_disk_cache = tcx.query_system.on_disk_cache.as_ref()?;
402-
403-
let prof_timer = tcx.prof.incr_cache_loading();
404-
405-
// The call to `with_query_deserialization` enforces that no new `DepNodes`
406-
// are created during deserialization. See the docs of that method for more
407-
// details.
408-
let value = tcx
409-
.dep_graph
410-
.with_query_deserialization(|| on_disk_cache.try_load_query_result(tcx, prev_index));
411-
412-
prof_timer.finish_with_query_invocation_id(index.into());
413-
414-
value
415-
}
416-
417340
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
418341
where
419342
Q: QueryConfig<QueryCtxt<'tcx>>,
@@ -477,28 +400,6 @@ where
477400
}
478401
}
479402

480-
macro_rules! item_if_cached {
481-
([] $tokens:tt) => {};
482-
([(cache) $($rest:tt)*] { $($tokens:tt)* }) => {
483-
$($tokens)*
484-
};
485-
([$other:tt $($modifiers:tt)*] $tokens:tt) => {
486-
item_if_cached! { [$($modifiers)*] $tokens }
487-
};
488-
}
489-
490-
macro_rules! expand_if_cached {
491-
([], $tokens:expr) => {{
492-
None
493-
}};
494-
([(cache) $($rest:tt)*], $tokens:expr) => {{
495-
Some($tokens)
496-
}};
497-
([$other:tt $($modifiers:tt)*], $tokens:expr) => {
498-
expand_if_cached!([$($modifiers)*], $tokens)
499-
};
500-
}
501-
502403
/// Don't show the backtrace for query system by default
503404
/// use `RUST_BACKTRACE=full` to show all the backtraces
504405
#[inline(never)]
@@ -590,38 +491,11 @@ macro_rules! define_queries {
590491
)
591492
},
592493
can_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] true false),
593-
try_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] {
594-
|tcx, key, prev_index, index| {
595-
if ::rustc_middle::query::cached::$name(tcx, key) {
596-
let value = $crate::plumbing::try_load_from_disk::<
597-
queries::$name::ProvidedValue<'tcx>
598-
>(
599-
tcx,
600-
prev_index,
601-
index,
602-
);
603-
value.map(|value| queries::$name::provided_to_erased(tcx, value))
604-
} else {
605-
None
606-
}
607-
}
608-
} {
609-
|_tcx, _key, _prev_index, _index| None
610-
}),
611-
value_from_cycle_error: |tcx, cycle, guar| {
612-
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
613-
erase(result)
614-
},
615-
loadable_from_disk: |_tcx, _key, _index| {
616-
should_ever_cache_on_disk!([$($modifiers)*] {
617-
::rustc_middle::query::cached::$name(_tcx, _key) &&
618-
$crate::plumbing::loadable_from_disk(_tcx, _index)
619-
} {
620-
false
621-
})
622-
},
623-
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),
624-
format_value: |value| format!("{:?}", restore::<queries::$name::Value<'tcx>>(*value)),
494+
try_load_from_disk: query_utils::$name::try_load_from_disk,
495+
loadable_from_disk: query_utils::$name::loadable_from_disk,
496+
value_from_cycle_error: query_utils::$name::value_from_cycle_error,
497+
hash_result: hash_result!([$($modifiers)*][query_utils::$name::hash_result]),
498+
format_value: query_utils::$name::format_value,
625499
}
626500
}
627501

@@ -667,30 +541,6 @@ macro_rules! define_queries {
667541
qmap,
668542
).unwrap();
669543
}
670-
671-
pub fn alloc_self_profile_query_strings<'tcx>(tcx: TyCtxt<'tcx>, string_cache: &mut QueryKeyStringCache) {
672-
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
673-
tcx,
674-
stringify!($name),
675-
&tcx.query_system.caches.$name,
676-
string_cache,
677-
)
678-
}
679-
680-
item_if_cached! { [$($modifiers)*] {
681-
pub fn encode_query_results<'tcx>(
682-
tcx: TyCtxt<'tcx>,
683-
encoder: &mut CacheEncoder<'_, 'tcx>,
684-
query_result_index: &mut EncodedDepNodeIndex
685-
) {
686-
$crate::plumbing::encode_query_results::<query_impl::$name::QueryType<'tcx>>(
687-
query_impl::$name::QueryType::config(tcx),
688-
QueryCtxt::new(tcx),
689-
encoder,
690-
query_result_index,
691-
)
692-
}
693-
}}
694544
})*}
695545

696546
pub(crate) fn engine(incremental: bool) -> QueryEngine {
@@ -713,23 +563,11 @@ macro_rules! define_queries {
713563
}
714564
}
715565

716-
// These arrays are used for iteration and can't be indexed by `DepKind`.
566+
// This array is used for iteration and can't be indexed by `DepKind`.
717567

718568
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap)] =
719569
&[$(query_impl::$name::try_collect_active_jobs),*];
720570

721-
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[
722-
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryKeyStringCache)
723-
] = &[$(query_impl::$name::alloc_self_profile_query_strings),*];
724-
725-
const ENCODE_QUERY_RESULTS: &[
726-
Option<for<'tcx> fn(
727-
TyCtxt<'tcx>,
728-
&mut CacheEncoder<'_, 'tcx>,
729-
&mut EncodedDepNodeIndex)
730-
>
731-
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
732-
733571
#[allow(nonstandard_style)]
734572
mod query_callbacks {
735573
use super::*;

0 commit comments

Comments
 (0)