Skip to content

Commit c5d942f

Browse files
Rollup merge of rust-lang#161332 - nnethercote:GlobalCtxt-Session-cleanups, r=Zalathar
Some `GlobalCtxt`/`Session` cleanups Details in individual commits. r? @Zalathar
2 parents 8260f1e + c55f5a3 commit c5d942f

15 files changed

Lines changed: 78 additions & 68 deletions

File tree

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> InferCtxt<'tcx> {
4646
V: TypeFoldable<TyCtxt<'tcx>>,
4747
{
4848
let ty::ParamEnvAnd { param_env, value } = value;
49-
let canonical_param_env = self.tcx.canonical_param_env_cache.get_or_insert(
49+
let canonical_param_env = self.tcx.caches.canonical_param_env_cache.get_or_insert(
5050
self.tcx,
5151
param_env,
5252
query_state,

compiler/rustc_infer/src/infer/canonical/instantiate.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,18 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for CanonicalInstantiator<'tcx> {
143143
// is both expensive (depending on the size of the clauses) and a pure function.
144144
let index = *self
145145
.tcx
146+
.caches
146147
.highest_var_in_clauses_cache
147148
.lock()
148149
.entry(c)
149150
.or_insert_with(|| highest_var_in_clauses(c));
150151
let c_args = &self.var_values[..=index];
151152

152-
if let Some(c) = self.tcx.clauses_cache.lock().get(&(c, c_args)) {
153+
if let Some(c) = self.tcx.caches.clauses_cache.lock().get(&(c, c_args)) {
153154
c
154155
} else {
155156
let folded = c.super_fold_with(self);
156-
self.tcx.clauses_cache.lock().insert((c, c_args), folded);
157+
self.tcx.caches.clauses_cache.lock().insert((c, c_args), folded);
157158
folded
158159
}
159160
}

compiler/rustc_interface/src/callbacks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use std::fmt;
1313

1414
use rustc_errors::DiagInner;
15-
use rustc_middle::dep_graph::{DepNodeIndex, QuerySideEffect, TaskDepsRef};
15+
use rustc_middle::dep_graph::{QuerySideEffect, TaskDepsRef};
1616
use rustc_middle::ty::tls;
1717
use rustc_span::Symbol;
1818

@@ -59,13 +59,13 @@ fn track_feature(feature: Symbol) {
5959
};
6060
let tcx = icx.tcx;
6161

62-
if let Some(dep_node_index) = tcx.sess.used_features.lock().get(&feature).copied() {
63-
tcx.dep_graph.read_index(DepNodeIndex::from_u32(dep_node_index));
62+
if let Some(dep_node_index) = tcx.query_system.used_features.lock().get(&feature).copied() {
63+
tcx.dep_graph.read_index(dep_node_index);
6464
} else {
6565
let dep_node_index = tcx
6666
.dep_graph
6767
.encode_side_effect(tcx, QuerySideEffect::CheckFeature { symbol: feature });
68-
tcx.sess.used_features.lock().insert(feature, dep_node_index.as_u32());
68+
tcx.query_system.used_features.lock().insert(feature, dep_node_index);
6969
tcx.dep_graph.read_index(dep_node_index);
7070
}
7171
})

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,8 +1011,8 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
10111011
untracked,
10121012
incr_comp_session.as_ref(),
10131013
dep_graph,
1014-
rustc_query_impl::make_dep_kind_vtables(&arena),
10151014
rustc_query_impl::query_system(
1015+
&arena,
10161016
providers.queries,
10171017
providers.extern_queries,
10181018
query_result_on_disk_cache,

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,12 @@ impl<'a, 'tcx> TyDecoder<'tcx> for MetadataDecodeContext<'a, 'tcx> {
411411

412412
let key = ty::CReaderCacheKey { cnum: Some(self.cdata.cnum), pos: shorthand };
413413

414-
if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) {
414+
if let Some(&ty) = tcx.caches.ty_rcache.borrow().get(&key) {
415415
return ty;
416416
}
417417

418418
let ty = or_insert_with(self);
419-
tcx.ty_rcache.borrow_mut().insert(key, ty);
419+
tcx.caches.ty_rcache.borrow_mut().insert(key, ty);
420420
ty
421421
}
422422

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum QuerySideEffect {
4848
/// effect dep node as a dependency.
4949
Diagnostic(DiagInner),
5050
/// Records the feature used during query execution.
51-
/// This feature will be inserted into `sess.used_features`
51+
/// This feature will be inserted into `query_system.used_features`
5252
/// if we mark the query as green, as that query will have
5353
/// the side effect dep node as a dependency.
5454
CheckFeature { symbol: Symbol },
@@ -779,7 +779,7 @@ impl DepGraphData {
779779
tcx.dcx().emit_diagnostic(diagnostic.clone());
780780
}
781781
QuerySideEffect::CheckFeature { symbol } => {
782-
tcx.sess.used_features.lock().insert(*symbol, dep_node_index.as_u32());
782+
tcx.query_system.used_features.lock().insert(*symbol, dep_node_index);
783783
}
784784
}
785785

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080
impl<'tcx> TyCtxt<'tcx> {
8181
#[inline]
8282
pub fn dep_kind_vtable(self, dk: DepKind) -> &'tcx DepKindVTable<'tcx> {
83-
&self.dep_kind_vtables[dk.as_usize()]
83+
&self.query_system.dep_kind_vtables[dk.as_usize()]
8484
}
8585

8686
#[inline(always)]

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,13 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
497497

498498
let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
499499

500-
if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
500+
if let Some(&ty) = tcx.caches.ty_rcache.borrow().get(&cache_key) {
501501
return ty;
502502
}
503503

504504
let ty = or_insert_with(self);
505505
// This may overwrite the entry, but it should overwrite with the same value.
506-
tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
506+
tcx.caches.ty_rcache.borrow_mut().insert_same(cache_key, ty);
507507
ty
508508
}
509509

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ use std::fmt;
22
use std::ops::Deref;
33

44
use rustc_data_structures::fingerprint::Fingerprint;
5-
use rustc_data_structures::fx::FxIndexMap;
5+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
66
use rustc_data_structures::hash_table::HashTable;
77
use rustc_data_structures::sharded::Sharded;
88
use rustc_data_structures::sync::{AtomicU64, Lock, WorkerLocal};
99
use rustc_errors::Diag;
1010
use rustc_hir::def_id::LocalDefId;
11-
use rustc_span::Span;
11+
use rustc_span::{Span, Symbol};
1212

13-
use crate::dep_graph::{DepKind, DepNodeIndex, QuerySideEffect, SerializedDepNodeIndex};
13+
use crate::dep_graph::{
14+
DepKind, DepKindVTable, DepNodeIndex, QuerySideEffect, SerializedDepNodeIndex,
15+
};
1416
use crate::ich::StableHashState;
1517
use crate::queries::{ExternProviders, Providers, QueryArenas, QueryVTables, TaggedQueryKey};
1618
use crate::query::on_disk_cache::OnDiskCache;
@@ -144,6 +146,7 @@ impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
144146

145147
pub struct QuerySystem<'tcx> {
146148
pub arenas: WorkerLocal<QueryArenas<'tcx>>,
149+
pub dep_kind_vtables: &'tcx [DepKindVTable<'tcx>],
147150
pub query_vtables: QueryVTables<'tcx>,
148151

149152
/// Side-effect associated with each [`DepKind::SideEffect`] node in the
@@ -153,6 +156,11 @@ pub struct QuerySystem<'tcx> {
153156
/// Always empty if incremental compilation is off.
154157
pub side_effects: Lock<FxIndexMap<DepNodeIndex, QuerySideEffect>>,
155158

159+
/// Enabled features that are used in the current compilation.
160+
///
161+
/// The value is the `DepNodeIndex` of the node that encodes the used feature.
162+
pub used_features: Lock<FxHashMap<Symbol, DepNodeIndex>>,
163+
156164
/// This provides access to the incremental compilation on-disk cache for query results.
157165
/// Do not access this directly. It is only meant to be used by
158166
/// `DepGraph::try_mark_green()` and the query infrastructure.

compiler/rustc_middle/src/ty/context.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use tracing::{debug, instrument};
4949

5050
use crate::arena::Arena;
5151
use crate::dep_graph::dep_node::make_metadata;
52-
use crate::dep_graph::{DepGraph, DepKindVTable, DepNodeIndex};
52+
use crate::dep_graph::{DepGraph, DepNodeIndex};
5353
use crate::hir::{ProjectedMaybeOwner, ProjectedOwnerInfo};
5454
use crate::ich::StableHashState;
5555
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind};
@@ -654,6 +654,38 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
654654
}
655655
}
656656

657+
/// An assortment of global caches used by various parts of the compiler.
658+
///
659+
/// The individual fields are mostly unrelated to each other, but have been grouped together to
660+
/// reduce the number of top-level fields in [`GlobalCtxt`].
661+
#[derive(Default)]
662+
pub struct GlobalCaches<'tcx> {
663+
// Internal caches for metadata decoding. No need to track deps on this.
664+
pub ty_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
665+
666+
/// Caches the results of trait selection. This cache is used
667+
/// for things that do not have to do with the parameters in scope.
668+
pub selection_cache: traits::SelectionCache<'tcx, ty::TypingEnv<'tcx>>,
669+
670+
/// Caches the results of trait evaluation. This cache is used
671+
/// for things that do not have to do with the parameters in scope.
672+
/// Merge this with `selection_cache`?
673+
pub evaluation_cache: traits::EvaluationCache<'tcx, ty::TypingEnv<'tcx>>,
674+
675+
/// Caches the results of goal evaluation in the new solver.
676+
new_solver_evaluation_cache: Lock<search_graph::GlobalCache<TyCtxt<'tcx>>>,
677+
new_solver_canonical_param_env_cache: Lock<ty::CanonicalParamEnvCache<TyCtxt<'tcx>>>,
678+
679+
pub canonical_param_env_cache: CanonicalParamEnvCache<'tcx>,
680+
681+
/// Caches the index of the highest bound var in clauses in a canonical binder.
682+
pub highest_var_in_clauses_cache: Lock<FxHashMap<ty::Clauses<'tcx>, usize>>,
683+
684+
/// Caches the instantiation of a canonical binder given a set of args.
685+
pub clauses_cache:
686+
Lock<FxHashMap<(ty::Clauses<'tcx>, &'tcx [ty::GenericArg<'tcx>]), ty::Clauses<'tcx>>>,
687+
}
688+
657689
/// The central data structure of the compiler. It stores references
658690
/// to the various **arenas** and also houses the results of the
659691
/// various **compiler queries** that have been performed. See the
@@ -715,6 +747,8 @@ pub struct GlobalCtxt<'tcx> {
715747
pub incr_comp_session: Option<&'tcx IncrCompSession>,
716748
pub dep_graph: DepGraph,
717749

750+
/// This duplicates `Session::prof` because this field is hot enough that accessing it via
751+
/// `self.sess.prof` is a measurable slowdown (see #161332).
718752
pub prof: SelfProfilerRef,
719753

720754
/// Common types, pre-interned for your convenience.
@@ -733,31 +767,8 @@ pub struct GlobalCtxt<'tcx> {
733767
untracked: Untracked,
734768

735769
pub query_system: QuerySystem<'tcx>,
736-
pub(crate) dep_kind_vtables: &'tcx [DepKindVTable<'tcx>],
737-
738-
// Internal caches for metadata decoding. No need to track deps on this.
739-
pub ty_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
740-
741-
/// Caches the results of trait selection. This cache is used
742-
/// for things that do not have to do with the parameters in scope.
743-
pub selection_cache: traits::SelectionCache<'tcx, ty::TypingEnv<'tcx>>,
744770

745-
/// Caches the results of trait evaluation. This cache is used
746-
/// for things that do not have to do with the parameters in scope.
747-
/// Merge this with `selection_cache`?
748-
pub evaluation_cache: traits::EvaluationCache<'tcx, ty::TypingEnv<'tcx>>,
749-
750-
/// Caches the results of goal evaluation in the new solver.
751-
pub new_solver_evaluation_cache: Lock<search_graph::GlobalCache<TyCtxt<'tcx>>>,
752-
pub new_solver_canonical_param_env_cache: Lock<ty::CanonicalParamEnvCache<TyCtxt<'tcx>>>,
753-
754-
pub canonical_param_env_cache: CanonicalParamEnvCache<'tcx>,
755-
756-
/// Caches the index of the highest bound var in clauses in a canonical binder.
757-
pub highest_var_in_clauses_cache: Lock<FxHashMap<ty::Clauses<'tcx>, usize>>,
758-
/// Caches the instantiation of a canonical binder given a set of args.
759-
pub clauses_cache:
760-
Lock<FxHashMap<(ty::Clauses<'tcx>, &'tcx [ty::GenericArg<'tcx>]), ty::Clauses<'tcx>>>,
771+
pub caches: GlobalCaches<'tcx>,
761772

762773
/// Data layout specification for the current target.
763774
pub data_layout: TargetDataLayout,
@@ -937,7 +948,6 @@ impl<'tcx> TyCtxt<'tcx> {
937948
untracked: Untracked,
938949
incr_comp_session: Option<&'tcx IncrCompSession>,
939950
dep_graph: DepGraph,
940-
dep_kind_vtables: &'tcx [DepKindVTable<'tcx>],
941951
query_system: QuerySystem<'tcx>,
942952
hooks: crate::hooks::Providers,
943953
current_gcx: CurrentGcx,
@@ -967,15 +977,7 @@ impl<'tcx> TyCtxt<'tcx> {
967977
consts: common_consts,
968978
untracked,
969979
query_system,
970-
dep_kind_vtables,
971-
ty_rcache: Default::default(),
972-
selection_cache: Default::default(),
973-
evaluation_cache: Default::default(),
974-
new_solver_evaluation_cache: Default::default(),
975-
new_solver_canonical_param_env_cache: Default::default(),
976-
canonical_param_env_cache: Default::default(),
977-
highest_var_in_clauses_cache: Default::default(),
978-
clauses_cache: Default::default(),
980+
caches: Default::default(),
979981
data_layout,
980982
alloc_map: interpret::AllocMap::new(),
981983
current_gcx,
@@ -1612,7 +1614,7 @@ impl<'tcx> TyCtxt<'tcx> {
16121614
}
16131615

16141616
// Collect first to avoid holding the lock while linting.
1615-
let used_features = self.sess.used_features.lock();
1617+
let used_features = self.query_system.used_features.lock();
16161618
let unused_features = self
16171619
.features()
16181620
.enabled_features_iter_stable_order()

0 commit comments

Comments
 (0)