Skip to content

Commit 7256855

Browse files
committed
Auto merge of rust-lang#85941 - cjgillot:qresolve, r=Aaron1011
Reduce the amount of untracked state in TyCtxt -- Take 2 Main part of rust-lang#85153 The offending line (rust-lang#85153 (comment)) is replaced by a FIXME until the possible bug and the perf concern are both resolved. r? `@Aaron1011`
2 parents e9a387d + 9f6d7e7 commit 7256855

File tree

15 files changed

+139
-122
lines changed

15 files changed

+139
-122
lines changed

compiler/rustc_hir/src/definitions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use tracing::debug;
2323
/// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey`
2424
/// stores the `DefIndex` of its parent.
2525
/// There is one `DefPathTable` for each crate.
26-
#[derive(Clone, Default)]
26+
#[derive(Clone, Default, Debug)]
2727
pub struct DefPathTable {
2828
index_to_key: IndexVec<DefIndex, DefKey>,
2929
def_path_hashes: IndexVec<DefIndex, DefPathHash>,
@@ -96,7 +96,7 @@ impl DefPathTable {
9696
/// The definition table containing node definitions.
9797
/// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s.
9898
/// It also stores mappings to convert `LocalDefId`s to/from `HirId`s.
99-
#[derive(Clone)]
99+
#[derive(Clone, Debug)]
100100
pub struct Definitions {
101101
table: DefPathTable,
102102

compiler/rustc_metadata/src/creader.rs

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ pub struct CStore {
5151
unused_externs: Vec<Symbol>,
5252
}
5353

54+
impl std::fmt::Debug for CStore {
55+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56+
f.debug_struct("CStore").finish_non_exhaustive()
57+
}
58+
}
59+
5460
pub struct CrateLoader<'a> {
5561
// Immutable configuration.
5662
sess: &'a Session,

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
445445
}
446446

447447
fn encode_def_path_table(&mut self) {
448-
let table = self.tcx.hir().definitions().def_path_table();
448+
let table = self.tcx.resolutions(()).definitions.def_path_table();
449449
if self.is_proc_macro {
450450
for def_index in std::iter::once(CRATE_DEF_INDEX)
451451
.chain(self.tcx.hir().krate().proc_macros.iter().map(|p| p.owner.local_def_index))
@@ -1062,7 +1062,7 @@ impl EncodeContext<'a, 'tcx> {
10621062

10631063
let data = ModData {
10641064
reexports,
1065-
expansion: tcx.hir().definitions().expansion_that_defined(local_def_id),
1065+
expansion: tcx.resolutions(()).definitions.expansion_that_defined(local_def_id),
10661066
};
10671067

10681068
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
@@ -1759,7 +1759,7 @@ impl EncodeContext<'a, 'tcx> {
17591759
.map(|(trait_def_id, mut impls)| {
17601760
// Bring everything into deterministic order for hashing
17611761
impls.sort_by_cached_key(|&(index, _)| {
1762-
tcx.hir().definitions().def_path_hash(LocalDefId { local_def_index: index })
1762+
tcx.hir().def_path_hash(LocalDefId { local_def_index: index })
17631763
});
17641764

17651765
TraitImpls {

compiler/rustc_middle/src/hir/map/mod.rs

+30-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
99
use rustc_data_structures::svh::Svh;
1010
use rustc_hir::def::{DefKind, Res};
1111
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
12-
use rustc_hir::definitions::{DefKey, DefPath, Definitions};
12+
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1313
use rustc_hir::intravisit;
1414
use rustc_hir::intravisit::Visitor;
1515
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@@ -154,21 +154,24 @@ impl<'hir> Map<'hir> {
154154
self.tcx.hir_crate(())
155155
}
156156

157-
#[inline]
158-
pub fn definitions(&self) -> &'hir Definitions {
159-
&self.tcx.definitions
160-
}
161-
162157
pub fn def_key(&self, def_id: LocalDefId) -> DefKey {
163-
self.tcx.definitions.def_key(def_id)
158+
// Accessing the DefKey is ok, since it is part of DefPathHash.
159+
self.tcx.untracked_resolutions.definitions.def_key(def_id)
164160
}
165161

166162
pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
167163
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
168164
}
169165

170166
pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
171-
self.tcx.definitions.def_path(def_id)
167+
// Accessing the DefPath is ok, since it is part of DefPathHash.
168+
self.tcx.untracked_resolutions.definitions.def_path(def_id)
169+
}
170+
171+
#[inline]
172+
pub fn def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
173+
// Accessing the DefPathHash is ok, it is incr. comp. stable.
174+
self.tcx.untracked_resolutions.definitions.def_path_hash(def_id)
172175
}
173176

174177
#[inline]
@@ -184,16 +187,21 @@ impl<'hir> Map<'hir> {
184187

185188
#[inline]
186189
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<LocalDefId> {
187-
self.tcx.definitions.opt_hir_id_to_local_def_id(hir_id)
190+
// FIXME(#85914) is this access safe for incr. comp.?
191+
self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id)
188192
}
189193

190194
#[inline]
191195
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
192-
self.tcx.definitions.local_def_id_to_hir_id(def_id)
196+
// FIXME(#85914) is this access safe for incr. comp.?
197+
self.tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id)
193198
}
194199

195200
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
196-
self.tcx.definitions.iter_local_def_id()
201+
// Create a dependency to the crate to be sure we reexcute this when the amount of
202+
// definitions change.
203+
self.tcx.ensure().hir_crate(());
204+
self.tcx.untracked_resolutions.definitions.iter_local_def_id()
197205
}
198206

199207
pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
@@ -932,9 +940,15 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> {
932940
pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx IndexedHir<'tcx> {
933941
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
934942

943+
// We can access untracked state since we are an eval_always query.
935944
let hcx = tcx.create_stable_hashing_context();
936-
let mut collector =
937-
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
945+
let mut collector = NodeCollector::root(
946+
tcx.sess,
947+
&**tcx.arena,
948+
tcx.untracked_crate,
949+
&tcx.untracked_resolutions.definitions,
950+
hcx,
951+
);
938952
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
939953

940954
let map = collector.finalize_and_compute_crate_hash();
@@ -944,14 +958,15 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx IndexedHir<'tc
944958
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
945959
assert_eq!(crate_num, LOCAL_CRATE);
946960

961+
// We can access untracked state since we are an eval_always query.
947962
let mut hcx = tcx.create_stable_hashing_context();
948963

949964
let mut hir_body_nodes: Vec<_> = tcx
950965
.index_hir(())
951966
.map
952967
.iter_enumerated()
953968
.filter_map(|(def_id, hod)| {
954-
let def_path_hash = tcx.definitions.def_path_hash(def_id);
969+
let def_path_hash = tcx.untracked_resolutions.definitions.def_path_hash(def_id);
955970
let mut hasher = StableHasher::new();
956971
hod.as_ref()?.hash_stable(&mut hcx, &mut hasher);
957972
AttributeMap { map: &tcx.untracked_crate.attrs, prefix: def_id }
@@ -968,7 +983,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
968983
},
969984
);
970985

971-
let upstream_crates = upstream_crates(&*tcx.cstore);
986+
let upstream_crates = upstream_crates(&*tcx.untracked_resolutions.cstore);
972987

973988
// We hash the final, remapped names of all local source files so we
974989
// don't have to include the path prefix remapping commandline args.

compiler/rustc_middle/src/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,6 @@ pub fn provide(providers: &mut Providers) {
169169
providers.all_local_trait_impls = |tcx, ()| &tcx.hir_crate(()).trait_impls;
170170
providers.expn_that_defined = |tcx, id| {
171171
let id = id.expect_local();
172-
tcx.definitions.expansion_that_defined(id)
172+
tcx.resolutions(()).definitions.expansion_that_defined(id)
173173
};
174174
}

compiler/rustc_middle/src/middle/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
187187
/// that it's *not* tracked for dependency information throughout compilation
188188
/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
189189
/// during resolve)
190-
pub trait CrateStore {
190+
pub trait CrateStore: std::fmt::Debug {
191191
fn as_any(&self) -> &dyn Any;
192192

193193
// resolve

compiler/rustc_middle/src/query/mod.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ rustc_queries! {
1414
desc { "trigger a delay span bug" }
1515
}
1616

17+
query resolutions(_: ()) -> &'tcx ty::ResolverOutputs {
18+
eval_always
19+
no_hash
20+
desc { "get the resolver outputs" }
21+
}
22+
1723
/// Represents crate as a whole (as distinct from the top-level crate module).
1824
/// If you call `hir_crate` (e.g., indirectly by calling `tcx.hir().krate()`),
1925
/// we will have to assume that any change means that you need to be recompiled.
@@ -207,7 +213,6 @@ rustc_queries! {
207213
}
208214

209215
query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
210-
eval_always
211216
desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) }
212217
}
213218

@@ -1133,7 +1138,6 @@ rustc_queries! {
11331138

11341139
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export<LocalDefId>]> {
11351140
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
1136-
eval_always
11371141
}
11381142

11391143
query impl_defaultness(def_id: DefId) -> hir::Defaultness {
@@ -1323,7 +1327,6 @@ rustc_queries! {
13231327
}
13241328

13251329
query visibility(def_id: DefId) -> ty::Visibility {
1326-
eval_always
13271330
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
13281331
}
13291332

@@ -1348,8 +1351,6 @@ rustc_queries! {
13481351
desc { |tcx| "collecting child items of `{}`", tcx.def_path_str(def_id) }
13491352
}
13501353
query extern_mod_stmt_cnum(def_id: LocalDefId) -> Option<CrateNum> {
1351-
// This depends on untracked global state (`tcx.extern_crate_map`)
1352-
eval_always
13531354
desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
13541355
}
13551356

@@ -1426,16 +1427,12 @@ rustc_queries! {
14261427
eval_always
14271428
}
14281429
query maybe_unused_trait_import(def_id: LocalDefId) -> bool {
1429-
eval_always
14301430
desc { |tcx| "maybe_unused_trait_import for `{}`", tcx.def_path_str(def_id.to_def_id()) }
14311431
}
14321432
query maybe_unused_extern_crates(_: ()) -> &'tcx [(LocalDefId, Span)] {
1433-
eval_always
14341433
desc { "looking up all possibly unused extern crates" }
14351434
}
1436-
query names_imported_by_glob_use(def_id: LocalDefId)
1437-
-> &'tcx FxHashSet<Symbol> {
1438-
eval_always
1435+
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxHashSet<Symbol> {
14391436
desc { |tcx| "names_imported_by_glob_use for `{}`", tcx.def_path_str(def_id.to_def_id()) }
14401437
}
14411438

@@ -1445,7 +1442,6 @@ rustc_queries! {
14451442
desc { "calculating the stability index for the local crate" }
14461443
}
14471444
query crates(_: ()) -> &'tcx [CrateNum] {
1448-
eval_always
14491445
desc { "fetching all foreign CrateNum instances" }
14501446
}
14511447

0 commit comments

Comments
 (0)