Skip to content

Commit 7daca2c

Browse files
committed
Extract suitable code from rustc_query_impl into a new crate rustc_query_misc
1 parent e172351 commit 7daca2c

File tree

10 files changed

+315
-185
lines changed

10 files changed

+315
-185
lines changed

Cargo.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -3897,6 +3897,7 @@ dependencies = [
38973897
"rustc_plugin_impl",
38983898
"rustc_privacy",
38993899
"rustc_query_impl",
3900+
"rustc_query_misc",
39003901
"rustc_query_system",
39013902
"rustc_resolve",
39023903
"rustc_session",
@@ -4242,7 +4243,6 @@ name = "rustc_query_impl"
42424243
version = "0.0.0"
42434244
dependencies = [
42444245
"field-offset",
4245-
"measureme",
42464246
"memoffset",
42474247
"rustc-rayon-core",
42484248
"rustc_data_structures",
@@ -4251,6 +4251,7 @@ dependencies = [
42514251
"rustc_index",
42524252
"rustc_macros",
42534253
"rustc_middle",
4254+
"rustc_query_misc",
42544255
"rustc_query_system",
42554256
"rustc_serialize",
42564257
"rustc_session",
@@ -4259,6 +4260,20 @@ dependencies = [
42594260
"tracing",
42604261
]
42614262

4263+
[[package]]
4264+
name = "rustc_query_misc"
4265+
version = "0.0.0"
4266+
dependencies = [
4267+
"measureme",
4268+
"rustc_data_structures",
4269+
"rustc_hir",
4270+
"rustc_index",
4271+
"rustc_middle",
4272+
"rustc_query_system",
4273+
"rustc_serialize",
4274+
"rustc_span",
4275+
]
4276+
42624277
[[package]]
42634278
name = "rustc_query_system"
42644279
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
@@ -381,7 +381,7 @@ impl Compiler {
381381
{
382382
let _prof_timer =
383383
queries.session().prof.generic_activity("self_profile_alloc_query_strings");
384-
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
384+
gcx.enter(rustc_query_misc::alloc_self_profile_query_strings);
385385
}
386386

387387
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,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,33 +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 = qcx.profiler().generic_activity_with_arg("encode_query_results_for", query.name());
352-
353-
assert!(query.query_state(qcx).all_inactive());
354-
let cache = query.query_cache(qcx);
355-
cache.iter(&mut |key, value, dep_node| {
356-
if query.cache_on_disk(qcx.tcx, &key) {
357-
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
358-
359-
// Record position of the cache entry.
360-
query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.position())));
361-
362-
// Encode the type check tables with the `SerializedDepNodeIndex`
363-
// as tag.
364-
encoder.encode_tagged(dep_node, &Q::restore(*value));
365-
}
366-
});
367-
}
368-
369324
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
370325
where
371326
Q: QueryConfig<QueryCtxt<'tcx>>,
@@ -380,38 +335,6 @@ where
380335
}
381336
}
382337

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

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

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

690540
pub(crate) fn engine(incremental: bool) -> QueryEngine {
@@ -707,23 +557,11 @@ macro_rules! define_queries {
707557
}
708558
}
709559

710-
// 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`.
711561

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

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

0 commit comments

Comments
 (0)