Skip to content

Commit 7ff4d60

Browse files
committed
Remove legacy upstream_monomorphization queries
upstream_monomorphizatons required loading all exported symbols of all dependencies whenever any of them changed. This is problematic for large codebases like Zed.
1 parent a6dafc0 commit 7ff4d60

5 files changed

Lines changed: 22 additions & 181 deletions

File tree

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use std::collections::hash_map::Entry::*;
2-
31
use rustc_abi::{CanonAbi, X86Call};
42
use rustc_ast::expand::allocator::{AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, global_fn_name};
53
use rustc_data_structures::fx::FxIndexMap;
6-
use rustc_data_structures::unord::UnordMap;
74
use rustc_hashes::Hash128;
85
use rustc_hir::def::DefKind;
96
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId};
@@ -397,87 +394,6 @@ fn exported_generic_symbols_provider_local<'tcx>(
397394
tcx.arena.alloc_from_iter(symbols)
398395
}
399396

400-
fn upstream_monomorphizations_provider(
401-
tcx: TyCtxt<'_>,
402-
(): (),
403-
) -> DefIdMap<UnordMap<GenericArgsRef<'_>, CrateNum>> {
404-
let cnums = tcx.crates(());
405-
406-
let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();
407-
408-
let drop_glue_fn_def_id = tcx.lang_items().drop_glue_fn();
409-
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
410-
411-
for &cnum in cnums.iter() {
412-
for (exported_symbol, _) in tcx.exported_generic_symbols(cnum).iter() {
413-
let (def_id, args) = match *exported_symbol {
414-
ExportedSymbol::Generic(def_id, args) => (def_id, args),
415-
ExportedSymbol::DropGlue(ty) => {
416-
if let Some(drop_in_place_fn_def_id) = drop_glue_fn_def_id {
417-
(drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
418-
} else {
419-
// `drop_glue` does not exist, don't try to use it.
420-
continue;
421-
}
422-
}
423-
ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
424-
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
425-
(async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
426-
} else {
427-
continue;
428-
}
429-
}
430-
ExportedSymbol::AsyncDropGlue(def_id, ty) => (def_id, tcx.mk_args(&[ty.into()])),
431-
ExportedSymbol::NonGeneric(..)
432-
| ExportedSymbol::ThreadLocalShim(..)
433-
| ExportedSymbol::NoDefId(..) => unreachable!("{exported_symbol:?}"),
434-
};
435-
436-
let args_map = instances.entry(def_id).or_default();
437-
438-
match args_map.entry(args) {
439-
Occupied(mut e) => {
440-
// If there are multiple monomorphizations available,
441-
// we select one deterministically.
442-
let other_cnum = *e.get();
443-
if tcx.stable_crate_id(other_cnum) > tcx.stable_crate_id(cnum) {
444-
e.insert(cnum);
445-
}
446-
}
447-
Vacant(e) => {
448-
e.insert(cnum);
449-
}
450-
}
451-
}
452-
}
453-
454-
instances
455-
}
456-
457-
fn upstream_monomorphizations_for_provider(
458-
tcx: TyCtxt<'_>,
459-
def_id: DefId,
460-
) -> Option<&UnordMap<GenericArgsRef<'_>, CrateNum>> {
461-
assert!(!def_id.is_local());
462-
tcx.upstream_monomorphizations(()).get(&def_id)
463-
}
464-
465-
fn upstream_drop_glue_for_provider<'tcx>(
466-
tcx: TyCtxt<'tcx>,
467-
args: GenericArgsRef<'tcx>,
468-
) -> Option<CrateNum> {
469-
let def_id = tcx.lang_items().drop_glue_fn()?;
470-
tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
471-
}
472-
473-
fn upstream_async_drop_glue_for_provider<'tcx>(
474-
tcx: TyCtxt<'tcx>,
475-
args: GenericArgsRef<'tcx>,
476-
) -> Option<CrateNum> {
477-
let def_id = tcx.lang_items().async_drop_in_place_fn()?;
478-
tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
479-
}
480-
481397
fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
482398
!tcx.reachable_set(()).contains(&def_id)
483399
}
@@ -511,17 +427,12 @@ pub(crate) fn provide(providers: &mut Providers) {
511427
providers.queries.is_reachable_non_generic = is_reachable_non_generic_provider_local;
512428
providers.queries.exported_non_generic_symbols = exported_non_generic_symbols_provider_local;
513429
providers.queries.exported_generic_symbols = exported_generic_symbols_provider_local;
514-
providers.queries.upstream_monomorphizations = upstream_monomorphizations_provider;
515430
providers.queries.is_unreachable_local_definition = is_unreachable_local_definition_provider;
516-
providers.queries.upstream_drop_glue_for = upstream_drop_glue_for_provider;
517-
providers.queries.upstream_async_drop_glue_for = upstream_async_drop_glue_for_provider;
518431
providers.queries.upstream_monomorphization_hashes = upstream_monomorphization_hashes_provider;
519432
providers.queries.upstream_monomorphization_for_hash =
520433
upstream_monomorphization_for_hash_provider;
521434
providers.queries.wasm_import_module_map = wasm_import_module_map;
522435
providers.extern_queries.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
523-
providers.extern_queries.upstream_monomorphizations_for =
524-
upstream_monomorphizations_for_provider;
525436
}
526437

527438
pub(crate) fn allocator_shim_symbols(

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
772772
stable_order_of_exportable_impls,
773773
exported_non_generic_symbols,
774774
exported_generic_symbols,
775-
exported_generic_symbol_hashes,
775+
exported_generic_symbol_hashes,
776776
interpret_alloc_index,
777777
tables,
778778
syntax_contexts,
@@ -2274,7 +2274,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
22742274
self.lazy_array(exported_symbols.iter().cloned())
22752275
}
22762276

2277-
22782277
fn encode_exported_generic_symbol_hashes(
22792278
&mut self,
22802279
exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)],

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub(crate) struct CrateRoot {
279279
stable_order_of_exportable_impls: LazyArray<(DefIndex, usize)>,
280280
exported_non_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
281281
exported_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
282-
exported_generic_symbol_hashes: LazyValue<ExportedSymbolHashTableRef>,
282+
exported_generic_symbol_hashes: LazyValue<ExportedSymbolHashTableRef>,
283283

284284
syntax_contexts: SyntaxContextTable,
285285
expn_data: ExpnDataTable,

compiler/rustc_middle/src/queries.rs

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use rustc_data_structures::steal::Steal;
6161
use rustc_data_structures::svh::Svh;
6262
use rustc_data_structures::unord::{UnordMap, UnordSet};
6363
use rustc_errors::{ErrorGuaranteed, catch_fatal_errors};
64+
use rustc_hashes::Hash128;
6465
use rustc_hir as hir;
6566
use rustc_hir::attrs::{EiiDecl, EiiImpl, StrippedCfgItem};
6667
use rustc_hir::def::{DefKind, DocLinkResMap};
@@ -69,7 +70,6 @@ use rustc_hir::lang_items::{LangItem, LanguageItems};
6970
use rustc_hir::{ItemLocalId, ItemLocalMap, PreciseCapturingArgKind, TraitCandidate};
7071
use rustc_index::IndexVec;
7172
use rustc_lint_defs::LintId;
72-
use rustc_hashes::Hash128;
7373
use rustc_macros::rustc_queries;
7474
use rustc_session::Limits;
7575
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
@@ -1939,75 +1939,6 @@ rustc_queries! {
19391939
}
19401940
}
19411941

1942-
/// The entire set of monomorphizations the local crate can safely
1943-
/// link to because they are exported from upstream crates. Do
1944-
/// not depend on this directly, as its value changes anytime
1945-
/// a monomorphization gets added or removed in any upstream
1946-
/// crate. Instead use the narrower `upstream_monomorphizations_for`,
1947-
/// `upstream_drop_glue_for`, `upstream_async_drop_glue_for`, or,
1948-
/// even better, `Instance::upstream_monomorphization()`.
1949-
query upstream_monomorphizations(_: ()) -> &'tcx DefIdMap<UnordMap<GenericArgsRef<'tcx>, CrateNum>> {
1950-
arena_cache
1951-
desc { "collecting available upstream monomorphizations" }
1952-
}
1953-
1954-
/// Returns the set of upstream monomorphizations available for the
1955-
/// generic function identified by the given `def_id`. The query makes
1956-
/// sure to make a stable selection if the same monomorphization is
1957-
/// available in multiple upstream crates.
1958-
///
1959-
/// You likely want to call `Instance::upstream_monomorphization()`
1960-
/// instead of invoking this query directly.
1961-
query upstream_monomorphizations_for(def_id: DefId)
1962-
-> Option<&'tcx UnordMap<GenericArgsRef<'tcx>, CrateNum>>
1963-
{
1964-
desc {
1965-
"collecting available upstream monomorphizations for `{}`",
1966-
tcx.def_path_str(def_id),
1967-
}
1968-
separate_provide_extern
1969-
}
1970-
1971-
/// Returns the upstream crate that exports drop-glue for the given
1972-
/// type (`args` is expected to be a single-item list containing the
1973-
/// type one wants drop-glue for).
1974-
///
1975-
/// This is a subset of `upstream_monomorphizations_for` in order to
1976-
/// increase dep-tracking granularity. Otherwise adding or removing any
1977-
/// type with drop-glue in any upstream crate would invalidate all
1978-
/// functions calling drop-glue of an upstream type.
1979-
///
1980-
/// You likely want to call `Instance::upstream_monomorphization()`
1981-
/// instead of invoking this query directly.
1982-
///
1983-
/// NOTE: This query could easily be extended to also support other
1984-
/// common functions that have are large set of monomorphizations
1985-
/// (like `Clone::clone` for example).
1986-
query upstream_drop_glue_for(args: GenericArgsRef<'tcx>) -> Option<CrateNum> {
1987-
desc { "available upstream drop-glue for `{:?}`", args }
1988-
}
1989-
1990-
/// Returns the upstream crate that exports async-drop-glue for
1991-
/// the given type (`args` is expected to be a single-item list
1992-
/// containing the type one wants async-drop-glue for).
1993-
///
1994-
/// This is a subset of `upstream_monomorphizations_for` in order
1995-
/// to increase dep-tracking granularity. Otherwise adding or
1996-
/// removing any type with async-drop-glue in any upstream crate
1997-
/// would invalidate all functions calling async-drop-glue of an
1998-
/// upstream type.
1999-
///
2000-
/// You likely want to call `Instance::upstream_monomorphization()`
2001-
/// instead of invoking this query directly.
2002-
///
2003-
/// NOTE: This query could easily be extended to also support other
2004-
/// common functions that have are large set of monomorphizations
2005-
/// (like `Clone::clone` for example).
2006-
query upstream_async_drop_glue_for(args: GenericArgsRef<'tcx>) -> Option<CrateNum> {
2007-
desc { "available upstream async-drop-glue for `{:?}`", args }
2008-
}
2009-
2010-
20111942
/// Set of all fingerprints of generic items exported by `cnum`.
20121943
query exported_generic_symbol_hashes(cnum: CrateNum) -> &'tcx [Hash128] {
20131944
desc { "getting exported generic symbol hashes for crate `{}`", cnum }

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{assert_matches, fmt};
22

33
use rustc_data_structures::fx::FxHashMap;
4-
use rustc_data_structures::stable_hash::{StableHasher, StableHash};
4+
use rustc_data_structures::stable_hash::{StableHash, StableHasher};
55
use rustc_errors::ErrorGuaranteed;
66
use rustc_hashes::Hash128;
77
use rustc_hir as hir;
@@ -225,24 +225,24 @@ impl<'tcx> Instance<'tcx> {
225225

226226
match self.def {
227227
InstanceKind::Item(_)
228-
| InstanceKind::DropGlue(_, Some(_))
229-
| InstanceKind::AsyncDropGlueCtorShim(_, _) => {
230-
let hash: Hash128 = tcx.with_stable_hashing_context(|mut hcx| {
231-
let mut hasher = StableHasher::new();
232-
self.stable_hash(&mut hcx, &mut hasher);
233-
hasher.finish()
234-
});
235-
let candidates = tcx.upstream_monomorphization_for_hash(hash)?;
236-
// Candidates are sorted by StableCrateId. First verified match wins.
237-
for &cnum in candidates.iter() {
238-
for (sym, _) in tcx.exported_generic_symbols(cnum).iter() {
239-
if sym.to_instance(tcx).as_ref() == Some(self) {
240-
return Some(cnum);
241-
}
242-
}
243-
}
244-
None
245-
}
228+
| InstanceKind::DropGlue(_, Some(_))
229+
| InstanceKind::AsyncDropGlueCtorShim(_, _) => {
230+
let hash: Hash128 = tcx.with_stable_hashing_context(|mut hcx| {
231+
let mut hasher = StableHasher::new();
232+
self.stable_hash(&mut hcx, &mut hasher);
233+
hasher.finish()
234+
});
235+
let candidates = tcx.upstream_monomorphization_for_hash(hash)?;
236+
// Candidates are sorted by StableCrateId. First verified match wins.
237+
for &cnum in candidates.iter() {
238+
for (sym, _) in tcx.exported_generic_symbols(cnum).iter() {
239+
if sym.to_instance(tcx).as_ref() == Some(self) {
240+
return Some(cnum);
241+
}
242+
}
243+
}
244+
None
245+
}
246246
InstanceKind::AsyncDropGlue(_, _) => None,
247247
InstanceKind::FutureDropPollShim(_, _, _) => None,
248248

0 commit comments

Comments
 (0)