Skip to content

Commit bb1d854

Browse files
committed
Extract suitable code from rustc_query_impl into a new crate rustc_query_misc
1 parent ec08a03 commit bb1d854

File tree

10 files changed

+315
-186
lines changed

10 files changed

+315
-186
lines changed

Cargo.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -3880,6 +3880,7 @@ dependencies = [
38803880
"rustc_plugin_impl",
38813881
"rustc_privacy",
38823882
"rustc_query_impl",
3883+
"rustc_query_misc",
38833884
"rustc_query_system",
38843885
"rustc_resolve",
38853886
"rustc_session",
@@ -4225,7 +4226,6 @@ name = "rustc_query_impl"
42254226
version = "0.0.0"
42264227
dependencies = [
42274228
"field-offset",
4228-
"measureme",
42294229
"memoffset",
42304230
"rustc-rayon-core",
42314231
"rustc_data_structures",
@@ -4234,6 +4234,7 @@ dependencies = [
42344234
"rustc_index",
42354235
"rustc_macros",
42364236
"rustc_middle",
4237+
"rustc_query_misc",
42374238
"rustc_query_system",
42384239
"rustc_serialize",
42394240
"rustc_session",
@@ -4242,6 +4243,20 @@ dependencies = [
42424243
"tracing",
42434244
]
42444245

4246+
[[package]]
4247+
name = "rustc_query_misc"
4248+
version = "0.0.0"
4249+
dependencies = [
4250+
"measureme",
4251+
"rustc_data_structures",
4252+
"rustc_hir",
4253+
"rustc_index",
4254+
"rustc_middle",
4255+
"rustc_query_system",
4256+
"rustc_serialize",
4257+
"rustc_span",
4258+
]
4259+
42454260
[[package]]
42464261
name = "rustc_query_system"
42474262
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
@@ -379,7 +379,7 @@ impl Compiler {
379379
{
380380
let _prof_timer =
381381
queries.session().prof.generic_activity("self_profile_alloc_query_strings");
382-
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
382+
gcx.enter(rustc_query_misc::alloc_self_profile_query_strings);
383383
}
384384

385385
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-173
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,22 @@
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;
11-
use rustc_index::Idx;
129
use rustc_middle::dep_graph::{
1310
self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex,
1411
};
15-
use rustc_middle::query::on_disk_cache::AbsoluteBytePos;
16-
use rustc_middle::query::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex};
1712
use rustc_middle::query::Key;
1813
use rustc_middle::ty::tls::{self, ImplicitCtxt};
1914
use rustc_middle::ty::{self, print::with_no_queries, TyCtxt};
2015
use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
2116
use rustc_query_system::ich::StableHashingContext;
2217
use rustc_query_system::query::{
23-
force_query, QueryCache, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects,
24-
QueryStackFrame,
18+
force_query, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffects, QueryStackFrame,
2519
};
2620
use rustc_query_system::{LayoutOfDepth, QueryOverflow};
27-
use rustc_serialize::Decodable;
28-
use rustc_serialize::Encodable;
2921
use rustc_session::Limit;
3022
use rustc_span::def_id::LOCAL_CRATE;
3123
use std::num::NonZeroU64;
@@ -178,16 +170,6 @@ pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepN
178170
tcx.dep_graph.try_mark_green(QueryCtxt::new(tcx), dep_node).is_some()
179171
}
180172

181-
pub(super) fn encode_all_query_results<'tcx>(
182-
tcx: TyCtxt<'tcx>,
183-
encoder: &mut CacheEncoder<'_, 'tcx>,
184-
query_result_index: &mut EncodedDepNodeIndex,
185-
) {
186-
for encode in super::ENCODE_QUERY_RESULTS.iter().copied().flatten() {
187-
encode(tcx, encoder, query_result_index);
188-
}
189-
}
190-
191173
macro_rules! handle_cycle_error {
192174
([]) => {{
193175
rustc_query_system::HandleCycleError::Error
@@ -252,10 +234,10 @@ macro_rules! feedable {
252234
}
253235

254236
macro_rules! hash_result {
255-
([][$V:ty]) => {{
256-
Some(|hcx, result| dep_graph::hash_result(hcx, &restore::<$V>(*result)))
237+
([][$f:path]) => {{
238+
Some($f)
257239
}};
258-
([(no_hash) $($rest:tt)*][$V:ty]) => {{
240+
([(no_hash) $($rest:tt)*]$args:tt) => {{
259241
None
260242
}};
261243
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
@@ -339,34 +321,6 @@ pub(crate) fn create_query_frame<
339321
QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash)
340322
}
341323

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

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

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

@@ -662,30 +535,6 @@ macro_rules! define_queries {
662535
qmap,
663536
).unwrap();
664537
}
665-
666-
pub fn alloc_self_profile_query_strings<'tcx>(tcx: TyCtxt<'tcx>, string_cache: &mut QueryKeyStringCache) {
667-
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
668-
tcx,
669-
stringify!($name),
670-
&tcx.query_system.caches.$name,
671-
string_cache,
672-
)
673-
}
674-
675-
item_if_cached! { [$($modifiers)*] {
676-
pub fn encode_query_results<'tcx>(
677-
tcx: TyCtxt<'tcx>,
678-
encoder: &mut CacheEncoder<'_, 'tcx>,
679-
query_result_index: &mut EncodedDepNodeIndex
680-
) {
681-
$crate::plumbing::encode_query_results::<query_impl::$name::QueryType<'tcx>>(
682-
query_impl::$name::QueryType::config(tcx),
683-
QueryCtxt::new(tcx),
684-
encoder,
685-
query_result_index,
686-
)
687-
}
688-
}}
689538
})*}
690539

691540
pub(crate) fn engine(incremental: bool) -> QueryEngine {
@@ -708,23 +557,11 @@ macro_rules! define_queries {
708557
}
709558
}
710559

711-
// These arrays are used for iteration and can't be indexed by `DepKind`.
560+
// This array is used for iteration and can't be indexed by `DepKind`.
712561

713562
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<DepKind>)] =
714563
&[$(query_impl::$name::try_collect_active_jobs),*];
715564

716-
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[
717-
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryKeyStringCache)
718-
] = &[$(query_impl::$name::alloc_self_profile_query_strings),*];
719-
720-
const ENCODE_QUERY_RESULTS: &[
721-
Option<for<'tcx> fn(
722-
TyCtxt<'tcx>,
723-
&mut CacheEncoder<'_, 'tcx>,
724-
&mut EncodedDepNodeIndex)
725-
>
726-
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
727-
728565
#[allow(nonstandard_style)]
729566
mod query_callbacks {
730567
use super::*;

0 commit comments

Comments
 (0)