Skip to content

Commit 244da22

Browse files
committed
Auto merge of rust-lang#123126 - oli-obk:feed_crate_num, r=davidtwco
Enable `CrateNum` query feeding via `TyCtxt` Instead of having a magic function that violates some `TyCtxtFeed` invariants, add a `create_def` equivalent for `CrateNum`s. Note that this still isn't tracked by the query system (unlike `create_def`), and that feeding most `CrateNum` queries for crates other than the local one will likely cause performance regressions. These things should be attempted on their own separately, but this PR should stand on its own
2 parents 40dcd79 + e9a2f8f commit 244da22

File tree

5 files changed

+61
-48
lines changed

5 files changed

+61
-48
lines changed

compiler/rustc_interface/src/queries.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_codegen_ssa::CodegenResults;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
1010
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
11-
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
11+
use rustc_hir::def_id::{StableCrateId, StableCrateIdMap, LOCAL_CRATE};
1212
use rustc_hir::definitions::Definitions;
1313
use rustc_incremental::setup_dep_graph;
1414
use rustc_metadata::creader::CStore;
@@ -140,11 +140,16 @@ impl<'tcx> Queries<'tcx> {
140140

141141
let cstore = FreezeLock::new(Box::new(CStore::new(
142142
self.compiler.codegen_backend.metadata_loader(),
143-
stable_crate_id,
144143
)) as _);
145144
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
146-
let untracked =
147-
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions };
145+
146+
let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
147+
let untracked = Untracked {
148+
cstore,
149+
source_span: AppendOnlyIndexVec::new(),
150+
definitions,
151+
stable_crate_ids,
152+
};
148153

149154
let qcx = passes::create_global_ctxt(
150155
self.compiler,
@@ -158,7 +163,8 @@ impl<'tcx> Queries<'tcx> {
158163
);
159164

160165
qcx.enter(|tcx| {
161-
let feed = tcx.feed_local_crate();
166+
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
167+
assert_eq!(feed.key(), LOCAL_CRATE);
162168
feed.crate_name(crate_name);
163169

164170
let feed = tcx.feed_unit_query();

compiler/rustc_metadata/src/creader.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
1313
use rustc_errors::DiagCtxt;
1414
use rustc_expand::base::SyntaxExtension;
1515
use rustc_fs_util::try_canonicalize;
16-
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE};
16+
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
1717
use rustc_hir::definitions::Definitions;
1818
use rustc_index::IndexVec;
19-
use rustc_middle::ty::TyCtxt;
19+
use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
2020
use rustc_session::config::{self, CrateType, ExternLocation};
2121
use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource};
2222
use rustc_session::lint;
@@ -62,9 +62,6 @@ pub struct CStore {
6262
/// This crate has a `#[alloc_error_handler]` item.
6363
has_alloc_error_handler: bool,
6464

65-
/// The interned [StableCrateId]s.
66-
pub(crate) stable_crate_ids: StableCrateIdMap,
67-
6865
/// Unused externs of the crate
6966
unused_externs: Vec<Symbol>,
7067
}
@@ -165,25 +162,27 @@ impl CStore {
165162
})
166163
}
167164

168-
fn intern_stable_crate_id(&mut self, root: &CrateRoot) -> Result<CrateNum, CrateError> {
169-
assert_eq!(self.metas.len(), self.stable_crate_ids.len());
170-
let num = CrateNum::new(self.stable_crate_ids.len());
171-
if let Some(&existing) = self.stable_crate_ids.get(&root.stable_crate_id()) {
165+
fn intern_stable_crate_id<'tcx>(
166+
&mut self,
167+
root: &CrateRoot,
168+
tcx: TyCtxt<'tcx>,
169+
) -> Result<TyCtxtFeed<'tcx, CrateNum>, CrateError> {
170+
assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
171+
let num = tcx.create_crate_num(root.stable_crate_id()).map_err(|existing| {
172172
// Check for (potential) conflicts with the local crate
173173
if existing == LOCAL_CRATE {
174-
Err(CrateError::SymbolConflictsCurrent(root.name()))
174+
CrateError::SymbolConflictsCurrent(root.name())
175175
} else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name())
176176
{
177177
let crate_name0 = root.name();
178-
Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1))
178+
CrateError::StableCrateIdCollision(crate_name0, crate_name1)
179179
} else {
180-
Err(CrateError::NotFound(root.name()))
180+
CrateError::NotFound(root.name())
181181
}
182-
} else {
183-
self.metas.push(None);
184-
self.stable_crate_ids.insert(root.stable_crate_id(), num);
185-
Ok(num)
186-
}
182+
})?;
183+
184+
self.metas.push(None);
185+
Ok(num)
187186
}
188187

189188
pub fn has_crate_data(&self, cnum: CrateNum) -> bool {
@@ -289,12 +288,7 @@ impl CStore {
289288
}
290289
}
291290

292-
pub fn new(
293-
metadata_loader: Box<MetadataLoaderDyn>,
294-
local_stable_crate_id: StableCrateId,
295-
) -> CStore {
296-
let mut stable_crate_ids = StableCrateIdMap::default();
297-
stable_crate_ids.insert(local_stable_crate_id, LOCAL_CRATE);
291+
pub fn new(metadata_loader: Box<MetadataLoaderDyn>) -> CStore {
298292
CStore {
299293
metadata_loader,
300294
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
@@ -307,7 +301,6 @@ impl CStore {
307301
alloc_error_handler_kind: None,
308302
has_global_allocator: false,
309303
has_alloc_error_handler: false,
310-
stable_crate_ids,
311304
unused_externs: Vec::new(),
312305
}
313306
}
@@ -416,7 +409,8 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
416409
let private_dep = self.is_private_dep(name.as_str(), private_dep);
417410

418411
// Claim this crate number and cache it
419-
let cnum = self.cstore.intern_stable_crate_id(&crate_root)?;
412+
let feed = self.cstore.intern_stable_crate_id(&crate_root, self.tcx)?;
413+
let cnum = feed.key();
420414

421415
info!(
422416
"register crate `{}` (cnum = {}. private_dep = {})",

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,6 @@ impl CrateStore for CStore {
629629
self.get_crate_data(cnum).root.stable_crate_id
630630
}
631631

632-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum {
633-
*self
634-
.stable_crate_ids
635-
.get(&stable_crate_id)
636-
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
637-
}
638-
639632
/// Returns the `DefKey` for a given `DefId`. This indicates the
640633
/// parent `DefId` as well as some idea of what kind of data the
641634
/// `DefId` refers to.
@@ -657,7 +650,13 @@ fn provide_cstore_hooks(providers: &mut Providers) {
657650
// If this is a DefPathHash from an upstream crate, let the CrateStore map
658651
// it to a DefId.
659652
let cstore = CStore::from_tcx(tcx.tcx);
660-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
653+
let cnum = *tcx
654+
.untracked()
655+
.stable_crate_ids
656+
.read()
657+
.get(&stable_crate_id)
658+
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"));
659+
assert_ne!(cnum, LOCAL_CRATE);
661660
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
662661
DefId { krate: cnum, index: def_index }
663662
};

compiler/rustc_middle/src/ty/context.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,6 @@ impl<'tcx> TyCtxt<'tcx> {
565565
TyCtxtFeed { tcx: self, key: () }
566566
}
567567

568-
/// Can only be fed before queries are run, and is thus exempt from any
569-
/// incremental issues. Do not use except for the initial query feeding.
570-
pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> {
571-
self.dep_graph.assert_ignored();
572-
TyCtxtFeed { tcx: self, key: LOCAL_CRATE }
573-
}
574-
575568
/// Only used in the resolver to register the `CRATE_DEF_ID` `DefId` and feed
576569
/// some queries for it. It will panic if used twice.
577570
pub fn create_local_crate_def_id(self, span: Span) -> TyCtxtFeed<'tcx, LocalDefId> {
@@ -1151,7 +1144,12 @@ impl<'tcx> TyCtxt<'tcx> {
11511144
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
11521145
LOCAL_CRATE
11531146
} else {
1154-
self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id)
1147+
*self
1148+
.untracked()
1149+
.stable_crate_ids
1150+
.read()
1151+
.get(&stable_crate_id)
1152+
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
11551153
}
11561154
}
11571155

@@ -1267,6 +1265,19 @@ impl<'tcx> TyCtxt<'tcx> {
12671265
feed
12681266
}
12691267

1268+
pub fn create_crate_num(
1269+
self,
1270+
stable_crate_id: StableCrateId,
1271+
) -> Result<TyCtxtFeed<'tcx, CrateNum>, CrateNum> {
1272+
if let Some(&existing) = self.untracked().stable_crate_ids.read().get(&stable_crate_id) {
1273+
return Err(existing);
1274+
}
1275+
1276+
let num = CrateNum::new(self.untracked().stable_crate_ids.read().len());
1277+
self.untracked().stable_crate_ids.write().insert(stable_crate_id, num);
1278+
Ok(TyCtxtFeed { key: num, tcx: self })
1279+
}
1280+
12701281
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
12711282
// Create a dependency to the red node to be sure we re-execute this when the amount of
12721283
// definitions change.

compiler/rustc_session/src/cstore.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use crate::search_paths::PathKind;
66
use crate::utils::NativeLibKind;
77
use rustc_ast as ast;
88
use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
9-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
9+
use rustc_hir::def_id::{
10+
CrateNum, DefId, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE,
11+
};
1012
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
1113
use rustc_span::symbol::Symbol;
1214
use rustc_span::Span;
@@ -217,7 +219,6 @@ pub trait CrateStore: std::fmt::Debug {
217219
// incr. comp. uses to identify a CrateNum.
218220
fn crate_name(&self, cnum: CrateNum) -> Symbol;
219221
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
220-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
221222
}
222223

223224
pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;
@@ -227,4 +228,6 @@ pub struct Untracked {
227228
/// Reference span for definitions.
228229
pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
229230
pub definitions: FreezeLock<Definitions>,
231+
/// The interned [StableCrateId]s.
232+
pub stable_crate_ids: FreezeLock<StableCrateIdMap>,
230233
}

0 commit comments

Comments
 (0)