Skip to content

Commit e6a4afc

Browse files
committed
Auto merge of #95418 - cjgillot:more-disk, r=davidtwco
Cache more queries on disk One of the principles of incremental compilation is to allow saving results on disk to avoid recomputing them. This PR investigates persisting a lot of queries whose result are to be saved into metadata. Some of the queries are cheap reads from HIR, but we may also want to get rid of these reads for incremental lowering.
2 parents 536020c + 29f3b3f commit e6a4afc

File tree

15 files changed

+103
-83
lines changed

15 files changed

+103
-83
lines changed

compiler/rustc_hir/src/hir.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ pub struct FnSig<'hir> {
21422142
// The bodies for items are stored "out of line", in a separate
21432143
// hashmap in the `Crate`. Here we just record the hir-id of the item
21442144
// so it can fetched later.
2145-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
2145+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
21462146
pub struct TraitItemId {
21472147
pub def_id: LocalDefId,
21482148
}
@@ -2205,7 +2205,7 @@ pub enum TraitItemKind<'hir> {
22052205
// The bodies for items are stored "out of line", in a separate
22062206
// hashmap in the `Crate`. Here we just record the hir-id of the item
22072207
// so it can fetched later.
2208-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
2208+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
22092209
pub struct ImplItemId {
22102210
pub def_id: LocalDefId,
22112211
}
@@ -2800,7 +2800,7 @@ impl<'hir> VariantData<'hir> {
28002800
// The bodies for items are stored "out of line", in a separate
28012801
// hashmap in the `Crate`. Here we just record the hir-id of the item
28022802
// so it can fetched later.
2803-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, Hash, HashStable_Generic)]
2803+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, Hash, HashStable_Generic)]
28042804
pub struct ItemId {
28052805
pub def_id: LocalDefId,
28062806
}
@@ -3047,7 +3047,7 @@ pub enum AssocItemKind {
30473047
// The bodies for items are stored "out of line", in a separate
30483048
// hashmap in the `Crate`. Here we just record the hir-id of the item
30493049
// so it can fetched later.
3050-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
3050+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
30513051
pub struct ForeignItemId {
30523052
pub def_id: LocalDefId,
30533053
}

compiler/rustc_index/src/vec.rs

-6
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for IndexVec<I, T> {
6565
}
6666
}
6767

68-
impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for &IndexVec<I, T> {
69-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
70-
Encodable::encode(&self.raw, s)
71-
}
72-
}
73-
7468
impl<D: Decoder, I: Idx, T: Decodable<D>> Decodable<D> for IndexVec<I, T> {
7569
fn decode(d: &mut D) -> Self {
7670
IndexVec { raw: Decodable::decode(d), _marker: PhantomData }

compiler/rustc_metadata/src/rmeta/encoder.rs

-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_middle::middle::exported_symbols::{
2424
metadata_symbol_name, ExportedSymbol, SymbolExportInfo,
2525
};
2626
use rustc_middle::mir::interpret;
27-
use rustc_middle::thir;
2827
use rustc_middle::traits::specialization_graph;
2928
use rustc_middle::ty::codec::TyEncoder;
3029
use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
@@ -346,18 +345,6 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
346345
}
347346
}
348347

349-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
350-
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
351-
(**self).encode(s)
352-
}
353-
}
354-
355-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] {
356-
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
357-
(**self).encode(s)
358-
}
359-
}
360-
361348
/// Helper trait to allow overloading `EncodeContext::lazy` for iterators.
362349
trait EncodeContentsForLazy<'a, 'tcx, T: ?Sized + LazyMeta> {
363350
fn encode_contents_for_lazy(self, ecx: &mut EncodeContext<'a, 'tcx>) -> T::Meta;

compiler/rustc_middle/src/arena.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ macro_rules! arena_types {
8282
[] upvars_mentioned: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
8383
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
8484
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<'tcx>,
85-
[] attribute: rustc_ast::Attribute,
85+
[decode] attribute: rustc_ast::Attribute,
8686
[] name_set: rustc_data_structures::fx::FxHashSet<rustc_span::symbol::Symbol>,
8787
[] hir_id_set: rustc_hir::HirIdSet,
8888

@@ -95,9 +95,6 @@ macro_rules! arena_types {
9595
// since we need to allocate this type on both the `rustc_hir` arena
9696
// (during lowering) and the `librustc_middle` arena (for decoding MIR)
9797
[decode] asm_template: rustc_ast::InlineAsmTemplatePiece,
98-
99-
// This is used to decode the &'tcx [Span] for InlineAsm's line_spans.
100-
[decode] span: rustc_span::Span,
10198
[decode] used_trait_imports: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>,
10299
[decode] impl_source: rustc_middle::traits::ImplSource<'tcx, ()>,
103100

compiler/rustc_middle/src/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
3636

3737
/// Gather the LocalDefId for each item-like within a module, including items contained within
3838
/// bodies. The Ids are in visitor order. This is used to partition a pass between modules.
39-
#[derive(Debug, HashStable)]
39+
#[derive(Debug, HashStable, Encodable, Decodable)]
4040
pub struct ModuleItems {
4141
submodules: Box<[LocalDefId]>,
4242
items: Box<[ItemId]>,

compiler/rustc_middle/src/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub enum StabilityLevel {
2929
}
3030

3131
/// An entry in the `depr_map`.
32-
#[derive(Copy, Clone, HashStable, Debug)]
32+
#[derive(Copy, Clone, HashStable, Debug, Encodable, Decodable)]
3333
pub struct DeprecationEntry {
3434
/// The metadata of the attribute associated with this entry.
3535
pub attr: Deprecation,

compiler/rustc_middle/src/query/mod.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ rustc_queries! {
5959
query hir_module_items(key: LocalDefId) -> rustc_middle::hir::ModuleItems {
6060
storage(ArenaCacheSelector<'tcx>)
6161
desc { |tcx| "HIR module items in `{}`", tcx.def_path_str(key.to_def_id()) }
62+
cache_on_disk_if { true }
6263
}
6364

6465
/// Gives access to the HIR node for the HIR owner `key`.
@@ -128,6 +129,7 @@ rustc_queries! {
128129
/// parameter. e.g. `fn example<const N: usize=3>` called on `N` would return `3`.
129130
query const_param_default(param: DefId) -> ty::Const<'tcx> {
130131
desc { |tcx| "compute const default for a given parameter `{}`", tcx.def_path_str(param) }
132+
cache_on_disk_if { param.is_local() }
131133
separate_provide_extern
132134
}
133135

@@ -223,6 +225,7 @@ rustc_queries! {
223225
/// Bounds from the parent (e.g. with nested impl trait) are not included.
224226
query explicit_item_bounds(key: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
225227
desc { |tcx| "finding item bounds for `{}`", tcx.def_path_str(key) }
228+
cache_on_disk_if { key.is_local() }
226229
separate_provide_extern
227230
}
228231

@@ -508,13 +511,15 @@ rustc_queries! {
508511
/// Returns the predicates written explicitly by the user.
509512
query explicit_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
510513
desc { |tcx| "computing explicit predicates of `{}`", tcx.def_path_str(key) }
514+
cache_on_disk_if { key.is_local() }
511515
separate_provide_extern
512516
}
513517

514518
/// Returns the inferred outlives predicates (e.g., for `struct
515519
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
516520
query inferred_outlives_of(key: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
517521
desc { |tcx| "computing inferred outlives predicates of `{}`", tcx.def_path_str(key) }
522+
cache_on_disk_if { key.is_local() }
518523
separate_provide_extern
519524
}
520525

@@ -526,6 +531,7 @@ rustc_queries! {
526531
/// additional acyclicity requirements).
527532
query super_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
528533
desc { |tcx| "computing the super predicates of `{}`", tcx.def_path_str(key) }
534+
cache_on_disk_if { key.is_local() }
529535
separate_provide_extern
530536
}
531537

@@ -549,6 +555,7 @@ rustc_queries! {
549555
query trait_def(key: DefId) -> ty::TraitDef {
550556
desc { |tcx| "computing trait definition for `{}`", tcx.def_path_str(key) }
551557
storage(ArenaCacheSelector<'tcx>)
558+
cache_on_disk_if { key.is_local() }
552559
separate_provide_extern
553560
}
554561
query adt_def(key: DefId) -> ty::AdtDef<'tcx> {
@@ -558,6 +565,7 @@ rustc_queries! {
558565
}
559566
query adt_destructor(key: DefId) -> Option<ty::Destructor> {
560567
desc { |tcx| "computing `Drop` impl for `{}`", tcx.def_path_str(key) }
568+
cache_on_disk_if { key.is_local() }
561569
separate_provide_extern
562570
}
563571

@@ -587,11 +595,13 @@ rustc_queries! {
587595
/// `is_const_fn` function.
588596
query impl_constness(key: DefId) -> hir::Constness {
589597
desc { |tcx| "checking if item is const fn: `{}`", tcx.def_path_str(key) }
598+
cache_on_disk_if { key.is_local() }
590599
separate_provide_extern
591600
}
592601

593602
query asyncness(key: DefId) -> hir::IsAsync {
594603
desc { |tcx| "checking if the function is async: `{}`", tcx.def_path_str(key) }
604+
cache_on_disk_if { key.is_local() }
595605
separate_provide_extern
596606
}
597607

@@ -609,12 +619,14 @@ rustc_queries! {
609619
/// Returns `true` if this is a foreign item (i.e., linked via `extern { ... }`).
610620
query is_foreign_item(key: DefId) -> bool {
611621
desc { |tcx| "checking if `{}` is a foreign item", tcx.def_path_str(key) }
622+
cache_on_disk_if { key.is_local() }
612623
separate_provide_extern
613624
}
614625

615626
/// Returns `Some(generator_kind)` if the node pointed to by `def_id` is a generator.
616627
query generator_kind(def_id: DefId) -> Option<hir::GeneratorKind> {
617628
desc { |tcx| "looking up generator kind of `{}`", tcx.def_path_str(def_id) }
629+
cache_on_disk_if { def_id.is_local() }
618630
separate_provide_extern
619631
}
620632

@@ -627,6 +639,7 @@ rustc_queries! {
627639
/// Maps from the `DefId` of a type or region parameter to its (inferred) variance.
628640
query variances_of(def_id: DefId) -> &'tcx [ty::Variance] {
629641
desc { |tcx| "computing the variances of `{}`", tcx.def_path_str(def_id) }
642+
cache_on_disk_if { def_id.is_local() }
630643
separate_provide_extern
631644
}
632645

@@ -639,13 +652,15 @@ rustc_queries! {
639652
/// Maps from an impl/trait `DefId` to a list of the `DefId`s of its items.
640653
query associated_item_def_ids(key: DefId) -> &'tcx [DefId] {
641654
desc { |tcx| "collecting associated items of `{}`", tcx.def_path_str(key) }
655+
cache_on_disk_if { key.is_local() }
642656
separate_provide_extern
643657
}
644658

645659
/// Maps from a trait item to the trait item "descriptor".
646660
query associated_item(key: DefId) -> ty::AssocItem {
647661
desc { |tcx| "computing associated item data for `{}`", tcx.def_path_str(key) }
648662
storage(ArenaCacheSelector<'tcx>)
663+
cache_on_disk_if { key.is_local() }
649664
separate_provide_extern
650665
}
651666

@@ -685,10 +700,12 @@ rustc_queries! {
685700
/// Return `None` if this is an inherent impl.
686701
query impl_trait_ref(impl_id: DefId) -> Option<ty::TraitRef<'tcx>> {
687702
desc { |tcx| "computing trait implemented by `{}`", tcx.def_path_str(impl_id) }
703+
cache_on_disk_if { impl_id.is_local() }
688704
separate_provide_extern
689705
}
690706
query impl_polarity(impl_id: DefId) -> ty::ImplPolarity {
691707
desc { |tcx| "computing implementation polarity of `{}`", tcx.def_path_str(impl_id) }
708+
cache_on_disk_if { impl_id.is_local() }
692709
separate_provide_extern
693710
}
694711

@@ -701,6 +718,7 @@ rustc_queries! {
701718
/// Methods in these implementations don't need to be exported.
702719
query inherent_impls(key: DefId) -> &'tcx [DefId] {
703720
desc { |tcx| "collecting inherent impls for `{}`", tcx.def_path_str(key) }
721+
cache_on_disk_if { key.is_local() }
704722
separate_provide_extern
705723
}
706724

@@ -745,6 +763,7 @@ rustc_queries! {
745763
/// Computes the signature of the function.
746764
query fn_sig(key: DefId) -> ty::PolyFnSig<'tcx> {
747765
desc { |tcx| "computing function signature of `{}`", tcx.def_path_str(key) }
766+
cache_on_disk_if { key.is_local() }
748767
separate_provide_extern
749768
}
750769

@@ -820,6 +839,7 @@ rustc_queries! {
820839
/// Caches `CoerceUnsized` kinds for impls on custom types.
821840
query coerce_unsized_info(key: DefId) -> ty::adjustment::CoerceUnsizedInfo {
822841
desc { |tcx| "computing CoerceUnsized info for `{}`", tcx.def_path_str(key) }
842+
cache_on_disk_if { key.is_local() }
823843
separate_provide_extern
824844
}
825845

@@ -1050,28 +1070,33 @@ rustc_queries! {
10501070

10511071
query opt_def_kind(def_id: DefId) -> Option<DefKind> {
10521072
desc { |tcx| "looking up definition kind of `{}`", tcx.def_path_str(def_id) }
1073+
cache_on_disk_if { def_id.is_local() }
10531074
separate_provide_extern
10541075
}
10551076

10561077
/// Gets the span for the definition.
10571078
query def_span(def_id: DefId) -> Span {
10581079
desc { |tcx| "looking up span for `{}`", tcx.def_path_str(def_id) }
1080+
cache_on_disk_if { def_id.is_local() }
10591081
separate_provide_extern
10601082
}
10611083

10621084
/// Gets the span for the identifier of the definition.
10631085
query def_ident_span(def_id: DefId) -> Option<Span> {
10641086
desc { |tcx| "looking up span for `{}`'s identifier", tcx.def_path_str(def_id) }
1087+
cache_on_disk_if { def_id.is_local() }
10651088
separate_provide_extern
10661089
}
10671090

10681091
query lookup_stability(def_id: DefId) -> Option<attr::Stability> {
10691092
desc { |tcx| "looking up stability of `{}`", tcx.def_path_str(def_id) }
1093+
cache_on_disk_if { def_id.is_local() }
10701094
separate_provide_extern
10711095
}
10721096

10731097
query lookup_const_stability(def_id: DefId) -> Option<attr::ConstStability> {
10741098
desc { |tcx| "looking up const stability of `{}`", tcx.def_path_str(def_id) }
1099+
cache_on_disk_if { def_id.is_local() }
10751100
separate_provide_extern
10761101
}
10771102

@@ -1081,6 +1106,7 @@ rustc_queries! {
10811106

10821107
query lookup_deprecation_entry(def_id: DefId) -> Option<DeprecationEntry> {
10831108
desc { |tcx| "checking whether `{}` is deprecated", tcx.def_path_str(def_id) }
1109+
cache_on_disk_if { def_id.is_local() }
10841110
separate_provide_extern
10851111
}
10861112

@@ -1110,33 +1136,39 @@ rustc_queries! {
11101136

11111137
query fn_arg_names(def_id: DefId) -> &'tcx [rustc_span::symbol::Ident] {
11121138
desc { |tcx| "looking up function parameter names for `{}`", tcx.def_path_str(def_id) }
1139+
cache_on_disk_if { def_id.is_local() }
11131140
separate_provide_extern
11141141
}
11151142
/// Gets the rendered value of the specified constant or associated constant.
11161143
/// Used by rustdoc.
11171144
query rendered_const(def_id: DefId) -> String {
11181145
storage(ArenaCacheSelector<'tcx>)
11191146
desc { |tcx| "rendering constant intializer of `{}`", tcx.def_path_str(def_id) }
1147+
cache_on_disk_if { def_id.is_local() }
11201148
separate_provide_extern
11211149
}
11221150
query impl_parent(def_id: DefId) -> Option<DefId> {
11231151
desc { |tcx| "computing specialization parent impl of `{}`", tcx.def_path_str(def_id) }
1152+
cache_on_disk_if { def_id.is_local() }
11241153
separate_provide_extern
11251154
}
11261155

11271156
/// Given an `associated_item`, find the trait it belongs to.
11281157
/// Return `None` if the `DefId` is not an associated item.
11291158
query trait_of_item(associated_item: DefId) -> Option<DefId> {
11301159
desc { |tcx| "finding trait defining `{}`", tcx.def_path_str(associated_item) }
1160+
cache_on_disk_if { associated_item.is_local() }
11311161
separate_provide_extern
11321162
}
11331163

11341164
query is_ctfe_mir_available(key: DefId) -> bool {
11351165
desc { |tcx| "checking if item has ctfe mir available: `{}`", tcx.def_path_str(key) }
1166+
cache_on_disk_if { key.is_local() }
11361167
separate_provide_extern
11371168
}
11381169
query is_mir_available(key: DefId) -> bool {
11391170
desc { |tcx| "checking if item has mir available: `{}`", tcx.def_path_str(key) }
1171+
cache_on_disk_if { key.is_local() }
11401172
separate_provide_extern
11411173
}
11421174

@@ -1378,6 +1410,7 @@ rustc_queries! {
13781410

13791411
query impl_defaultness(def_id: DefId) -> hir::Defaultness {
13801412
desc { |tcx| "looking up whether `{}` is a default impl", tcx.def_path_str(def_id) }
1413+
cache_on_disk_if { def_id.is_local() }
13811414
separate_provide_extern
13821415
}
13831416

@@ -1411,6 +1444,7 @@ rustc_queries! {
14111444
}
14121445
query is_reachable_non_generic(def_id: DefId) -> bool {
14131446
desc { |tcx| "checking whether `{}` is an exported symbol", tcx.def_path_str(def_id) }
1447+
cache_on_disk_if { def_id.is_local() }
14141448
separate_provide_extern
14151449
}
14161450
query is_unreachable_local_definition(def_id: LocalDefId) -> bool {
@@ -1730,9 +1764,9 @@ rustc_queries! {
17301764
/// - All names contained in `exported_symbols(cnum)` are guaranteed to
17311765
/// correspond to a publicly visible symbol in `cnum` machine code.
17321766
/// - The `exported_symbols` sets of different crates do not intersect.
1733-
query exported_symbols(_: CrateNum)
1734-
-> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
1767+
query exported_symbols(cnum: CrateNum) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
17351768
desc { "exported_symbols" }
1769+
cache_on_disk_if { *cnum == LOCAL_CRATE }
17361770
separate_provide_extern
17371771
}
17381772

0 commit comments

Comments
 (0)