Skip to content

Commit e94eaa6

Browse files
committed
Auto merge of #70674 - cjgillot:query-arena-all, r=matthewjasper
Have the per-query caches store the results on arenas This PR leverages the cache for each query to serve as storage area for the query results. It introduces a new cache `ArenaCache`, which moves the result to an arena, and only stores the reference in the hash map. This allows to remove a sizeable part of the usage of the global `TyCtxt` arena. I only migrated queries that already used arenas before.
2 parents 614f273 + d7d2185 commit e94eaa6

File tree

37 files changed

+313
-256
lines changed

37 files changed

+313
-256
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4126,6 +4126,7 @@ dependencies = [
41264126
name = "rustc_query_system"
41274127
version = "0.0.0"
41284128
dependencies = [
4129+
"arena",
41294130
"log",
41304131
"parking_lot 0.10.2",
41314132
"rustc-rayon-core",

src/librustc_codegen_llvm/attributes.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
253253
inline(cx, llfn, attributes::InlineAttr::Hint);
254254
}
255255

256-
inline(cx, llfn, codegen_fn_attrs.inline);
256+
inline(cx, llfn, codegen_fn_attrs.inline.clone());
257257

258258
// The `uwtable` attribute according to LLVM is:
259259
//
@@ -350,15 +350,12 @@ pub fn provide(providers: &mut Providers<'_>) {
350350
if tcx.sess.opts.actually_rustdoc {
351351
// rustdoc needs to be able to document functions that use all the features, so
352352
// whitelist them all
353-
tcx.arena
354-
.alloc(llvm_util::all_known_features().map(|(a, b)| (a.to_string(), b)).collect())
353+
llvm_util::all_known_features().map(|(a, b)| (a.to_string(), b)).collect()
355354
} else {
356-
tcx.arena.alloc(
357-
llvm_util::target_feature_whitelist(tcx.sess)
358-
.iter()
359-
.map(|&(a, b)| (a.to_string(), b))
360-
.collect(),
361-
)
355+
llvm_util::target_feature_whitelist(tcx.sess)
356+
.iter()
357+
.map(|&(a, b)| (a.to_string(), b))
358+
.collect()
362359
}
363360
};
364361

@@ -390,7 +387,7 @@ pub fn provide_extern(providers: &mut Providers<'_>) {
390387
}));
391388
}
392389

393-
tcx.arena.alloc(ret)
390+
ret
394391
};
395392
}
396393

src/librustc_codegen_ssa/back/symbol_export.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@ pub fn crates_export_threshold(crate_types: &[config::CrateType]) -> SymbolExpor
4242
}
4343
}
4444

45-
fn reachable_non_generics_provider(
46-
tcx: TyCtxt<'_>,
47-
cnum: CrateNum,
48-
) -> &DefIdMap<SymbolExportLevel> {
45+
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportLevel> {
4946
assert_eq!(cnum, LOCAL_CRATE);
5047

5148
if !tcx.sess.opts.output_types.should_codegen() {
52-
return tcx.arena.alloc(Default::default());
49+
return Default::default();
5350
}
5451

5552
// Check to see if this crate is a "special runtime crate". These
@@ -145,7 +142,7 @@ fn reachable_non_generics_provider(
145142
reachable_non_generics.insert(id, SymbolExportLevel::C);
146143
}
147144

148-
tcx.arena.alloc(reachable_non_generics)
145+
reachable_non_generics
149146
}
150147

151148
fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
@@ -281,7 +278,7 @@ fn exported_symbols_provider_local(
281278
fn upstream_monomorphizations_provider(
282279
tcx: TyCtxt<'_>,
283280
cnum: CrateNum,
284-
) -> &DefIdMap<FxHashMap<SubstsRef<'_>, CrateNum>> {
281+
) -> DefIdMap<FxHashMap<SubstsRef<'_>, CrateNum>> {
285282
debug_assert!(cnum == LOCAL_CRATE);
286283

287284
let cnums = tcx.all_crate_nums(LOCAL_CRATE);
@@ -338,7 +335,7 @@ fn upstream_monomorphizations_provider(
338335
}
339336
}
340337

341-
tcx.arena.alloc(instances)
338+
instances
342339
}
343340

344341
fn upstream_monomorphizations_for_provider(

src/librustc_codegen_ssa/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ pub fn provide_both(providers: &mut Providers<'_>) {
908908
.map(|id| &module_map[&id])
909909
.flat_map(|module| module.foreign_items.iter().cloned())
910910
.collect();
911-
tcx.arena.alloc(dllimports)
911+
dllimports
912912
};
913913

914914
providers.is_dllimport_foreign_item =

src/librustc_lint/levels.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::symbol::{sym, Symbol};
2222

2323
use std::cmp;
2424

25-
fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
25+
fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> LintLevelMap {
2626
assert_eq!(cnum, LOCAL_CRATE);
2727
let store = unerased_lint_store(tcx);
2828
let levels = LintLevelsBuilder::new(tcx.sess, false, &store);
@@ -37,7 +37,7 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
3737
intravisit::walk_crate(&mut builder, krate);
3838
builder.levels.pop(push);
3939

40-
tcx.arena.alloc(builder.levels.build_map())
40+
builder.levels.build_map()
4141
}
4242

4343
pub struct LintLevelsBuilder<'s> {

src/librustc_macros/src/query.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ impl Parse for QueryModifier {
107107
let block = input.parse()?;
108108
Ok(QueryModifier::LoadCached(tcx, id, block))
109109
} else if modifier == "storage" {
110-
let ty = input.parse()?;
110+
let args;
111+
parenthesized!(args in input);
112+
let ty = args.parse()?;
111113
Ok(QueryModifier::Storage(ty))
112114
} else if modifier == "fatal_cycle" {
113115
Ok(QueryModifier::FatalCycle)

src/librustc_metadata/rmeta/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
939939
}
940940

941941
/// Iterates over the diagnostic items in the given crate.
942-
fn get_diagnostic_items(&self, tcx: TyCtxt<'tcx>) -> &'tcx FxHashMap<Symbol, DefId> {
943-
tcx.arena.alloc(if self.root.is_proc_macro_crate() {
942+
fn get_diagnostic_items(&self) -> FxHashMap<Symbol, DefId> {
943+
if self.root.is_proc_macro_crate() {
944944
// Proc macro crates do not export any diagnostic-items to the target.
945945
Default::default()
946946
} else {
@@ -949,7 +949,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
949949
.decode(self)
950950
.map(|(name, def_index)| (name, self.local_def_id(def_index)))
951951
.collect()
952-
})
952+
}
953953
}
954954

955955
/// Iterates over each child of the given item.

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ impl IntoArgs for (CrateNum, DefId) {
8888

8989
provide! { <'tcx> tcx, def_id, other, cdata,
9090
type_of => { cdata.get_type(def_id.index, tcx) }
91-
generics_of => {
92-
tcx.arena.alloc(cdata.get_generics(def_id.index, tcx.sess))
93-
}
91+
generics_of => { cdata.get_generics(def_id.index, tcx.sess) }
9492
explicit_predicates_of => { cdata.get_explicit_predicates(def_id.index, tcx) }
9593
inferred_outlives_of => { cdata.get_inferred_outlives(def_id.index, tcx) }
9694
super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) }
97-
trait_def => {
98-
tcx.arena.alloc(cdata.get_trait_def(def_id.index, tcx.sess))
99-
}
95+
trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) }
10096
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
10197
adt_destructor => {
10298
let _ = cdata;
@@ -117,8 +113,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
117113
bug!("coerce_unsized_info: `{:?}` is missing its info", def_id);
118114
})
119115
}
120-
optimized_mir => { tcx.arena.alloc(cdata.get_optimized_mir(tcx, def_id.index)) }
121-
promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) }
116+
optimized_mir => { cdata.get_optimized_mir(tcx, def_id.index) }
117+
promoted_mir => { cdata.get_promoted_mir(tcx, def_id.index) }
122118
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
123119
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
124120
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
@@ -178,7 +174,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
178174
})
179175
.collect();
180176

181-
tcx.arena.alloc(reachable_non_generics)
177+
reachable_non_generics
182178
}
183179
native_libraries => { Lrc::new(cdata.get_native_libraries(tcx.sess)) }
184180
foreign_modules => { cdata.get_foreign_modules(tcx) }
@@ -220,7 +216,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
220216
}
221217
defined_lib_features => { cdata.get_lib_features(tcx) }
222218
defined_lang_items => { cdata.get_lang_items(tcx) }
223-
diagnostic_items => { cdata.get_diagnostic_items(tcx) }
219+
diagnostic_items => { cdata.get_diagnostic_items() }
224220
missing_lang_items => { cdata.get_missing_lang_items(tcx) }
225221

226222
missing_extern_crate_item => {
@@ -363,7 +359,7 @@ pub fn provide(providers: &mut Providers<'_>) {
363359
}
364360
}
365361

366-
tcx.arena.alloc(visible_parent_map)
362+
visible_parent_map
367363
},
368364

369365
dependency_formats: |tcx, cnum| {

src/librustc_middle/arena.rs

+3-58
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,13 @@ macro_rules! arena_types {
1212
($macro:path, $args:tt, $tcx:lifetime) => (
1313
$macro!($args, [
1414
[] layouts: rustc_target::abi::Layout,
15-
[] generics: rustc_middle::ty::Generics,
16-
[] trait_def: rustc_middle::ty::TraitDef,
15+
// AdtDef are interned and compared by address
1716
[] adt_def: rustc_middle::ty::AdtDef,
18-
[] steal_mir: rustc_middle::ty::steal::Steal<rustc_middle::mir::Body<$tcx>>,
19-
[] mir: rustc_middle::mir::Body<$tcx>,
20-
[] steal_promoted: rustc_middle::ty::steal::Steal<
21-
rustc_index::vec::IndexVec<
22-
rustc_middle::mir::Promoted,
23-
rustc_middle::mir::Body<$tcx>
24-
>
25-
>,
26-
[] promoted: rustc_index::vec::IndexVec<
27-
rustc_middle::mir::Promoted,
28-
rustc_middle::mir::Body<$tcx>
29-
>,
3017
[decode] tables: rustc_middle::ty::TypeckTables<$tcx>,
31-
[decode] borrowck_result: rustc_middle::mir::BorrowCheckResult<$tcx>,
3218
[] const_allocs: rustc_middle::mir::interpret::Allocation,
33-
[] vtable_method: Option<(
34-
rustc_hir::def_id::DefId,
35-
rustc_middle::ty::subst::SubstsRef<$tcx>
36-
)>,
37-
[few, decode] collect_and_partition_mono_items: rustc_hir::def_id::DefIdSet,
38-
[few, decode] mir_keys: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>,
39-
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
19+
// Required for the incremental on-disk cache
20+
[few, decode] mir_keys: rustc_hir::def_id::DefIdSet,
4021
[] region_scope_tree: rustc_middle::middle::region::ScopeTree,
41-
[] item_local_set: rustc_hir::ItemLocalSet,
42-
[decode] mir_const_qualif: rustc_index::bit_set::BitSet<rustc_middle::mir::Local>,
43-
[] trait_impls_of: rustc_middle::ty::trait_def::TraitImpls,
44-
[] associated_items: rustc_middle::ty::AssociatedItems,
4522
[] dropck_outlives:
4623
rustc_middle::infer::canonical::Canonical<'tcx,
4724
rustc_middle::infer::canonical::QueryResponse<'tcx,
@@ -80,42 +57,10 @@ macro_rules! arena_types {
8057
rustc_middle::infer::canonical::Canonical<'tcx,
8158
rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::Ty<'tcx>>
8259
>,
83-
[few] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls,
84-
[few] upstream_monomorphizations:
85-
rustc_hir::def_id::DefIdMap<
86-
rustc_data_structures::fx::FxHashMap<
87-
rustc_middle::ty::subst::SubstsRef<'tcx>,
88-
rustc_hir::def_id::CrateNum
89-
>
90-
>,
91-
[few] diagnostic_items: rustc_data_structures::fx::FxHashMap<
92-
rustc_span::symbol::Symbol,
93-
rustc_hir::def_id::DefId,
94-
>,
95-
[few] resolve_lifetimes: rustc_middle::middle::resolve_lifetime::ResolveLifetimes,
96-
[few] lint_levels: rustc_middle::lint::LintLevelMap,
97-
[few] stability_index: rustc_middle::middle::stability::Index<'tcx>,
98-
[few] features: rustc_feature::Features,
9960
[few] all_traits: Vec<rustc_hir::def_id::DefId>,
10061
[few] privacy_access_levels: rustc_middle::middle::privacy::AccessLevels,
101-
[few] target_features_whitelist: rustc_data_structures::fx::FxHashMap<
102-
String,
103-
Option<rustc_span::symbol::Symbol>
104-
>,
105-
[few] wasm_import_module_map: rustc_data_structures::fx::FxHashMap<
106-
rustc_hir::def_id::DefId,
107-
String
108-
>,
109-
[few] get_lib_features: rustc_middle::middle::lib_features::LibFeatures,
110-
[few] defined_lib_features: rustc_hir::lang_items::LanguageItems,
111-
[few] visible_parent_map: rustc_hir::def_id::DefIdMap<rustc_hir::def_id::DefId>,
11262
[few] foreign_module: rustc_middle::middle::cstore::ForeignModule,
11363
[few] foreign_modules: Vec<rustc_middle::middle::cstore::ForeignModule>,
114-
[few] reachable_non_generics: rustc_hir::def_id::DefIdMap<
115-
rustc_middle::middle::exported_symbols::SymbolExportLevel
116-
>,
117-
[few] crate_variances: rustc_middle::ty::CrateVariancesMap<'tcx>,
118-
[few] inferred_outlives_crate: rustc_middle::ty::CratePredicatesMap<'tcx>,
11964
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
12065
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
12166
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<$tcx>,

0 commit comments

Comments
 (0)