Skip to content

Commit 84f962a

Browse files
committed
Auto merge of #91924 - Aaron1011:serialize-adt-def, r=michaelwoerister
Fully serialize AdtDef This avoids needing to invoke the `adt_def` query during the decoding of another query's result. Split out from #91919 See #91696 (comment)
2 parents 60f3bd7 + 00ce6dc commit 84f962a

File tree

6 files changed

+23
-28
lines changed

6 files changed

+23
-28
lines changed

compiler/rustc_middle/src/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! arena_types {
99
[] layout: rustc_target::abi::Layout,
1010
[] fn_abi: rustc_target::abi::call::FnAbi<'tcx, rustc_middle::ty::Ty<'tcx>>,
1111
// AdtDef are interned and compared by address
12-
[] adt_def: rustc_middle::ty::AdtDef,
12+
[decode] adt_def: rustc_middle::ty::AdtDef,
1313
[] steal_thir: rustc_data_structures::steal::Steal<rustc_middle::thir::Thir<'tcx>>,
1414
[] steal_mir: rustc_data_structures::steal::Steal<rustc_middle::mir::Body<'tcx>>,
1515
[decode] mir: rustc_middle::mir::Body<'tcx>,

compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ rustc_queries! {
522522
}
523523
query adt_def(key: DefId) -> &'tcx ty::AdtDef {
524524
desc { |tcx| "computing ADT definition for `{}`", tcx.def_path_str(key) }
525+
cache_on_disk_if { key.is_local() }
525526
separate_provide_extern
526527
}
527528
query adt_destructor(key: DefId) -> Option<ty::Destructor> {

compiler/rustc_middle/src/ty/adt.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
1111
use rustc_hir::def_id::DefId;
1212
use rustc_index::vec::{Idx, IndexVec};
1313
use rustc_query_system::ich::StableHashingContext;
14-
use rustc_serialize::{self, Encodable, Encoder};
1514
use rustc_session::DataTypeKind;
1615
use rustc_span::symbol::sym;
1716
use rustc_target::abi::VariantIdx;
@@ -20,7 +19,7 @@ use std::cell::RefCell;
2019
use std::cmp::Ordering;
2120
use std::hash::{Hash, Hasher};
2221
use std::ops::Range;
23-
use std::{ptr, str};
22+
use std::str;
2423

2524
use super::{
2625
Destructor, FieldDef, GenericPredicates, ReprOptions, Ty, TyCtxt, VariantDef, VariantDiscr,
@@ -30,7 +29,7 @@ use super::{
3029
pub struct AdtSizedConstraint<'tcx>(pub &'tcx [Ty<'tcx>]);
3130

3231
bitflags! {
33-
#[derive(HashStable)]
32+
#[derive(HashStable, TyEncodable, TyDecodable)]
3433
pub struct AdtFlags: u32 {
3534
const NO_ADT_FLAGS = 0;
3635
/// Indicates whether the ADT is an enum.
@@ -88,6 +87,7 @@ bitflags! {
8887
///
8988
/// where `x` here represents the `DefId` of `S.x`. Then, the `DefId`
9089
/// can be used with [`TyCtxt::type_of()`] to get the type of the field.
90+
#[derive(TyEncodable, TyDecodable)]
9191
pub struct AdtDef {
9292
/// The `DefId` of the struct, enum or union item.
9393
pub did: DefId,
@@ -113,26 +113,23 @@ impl Ord for AdtDef {
113113
}
114114
}
115115

116+
/// There should be only one AdtDef for each `did`, therefore
117+
/// it is fine to implement `PartialEq` only based on `did`.
116118
impl PartialEq for AdtDef {
117-
// `AdtDef`s are always interned, and this is part of `TyS` equality.
118119
#[inline]
119120
fn eq(&self, other: &Self) -> bool {
120-
ptr::eq(self, other)
121+
self.did == other.did
121122
}
122123
}
123124

124125
impl Eq for AdtDef {}
125126

127+
/// There should be only one AdtDef for each `did`, therefore
128+
/// it is fine to implement `Hash` only based on `did`.
126129
impl Hash for AdtDef {
127130
#[inline]
128131
fn hash<H: Hasher>(&self, s: &mut H) {
129-
(self as *const AdtDef).hash(s)
130-
}
131-
}
132-
133-
impl<S: Encoder> Encodable<S> for AdtDef {
134-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
135-
self.did.encode(s)
132+
self.did.hash(s)
136133
}
137134
}
138135

@@ -161,7 +158,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for AdtDef {
161158
}
162159
}
163160

164-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
161+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
165162
pub enum AdtKind {
166163
Struct,
167164
Union,

compiler/rustc_middle/src/ty/codec.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::thir;
1616
use crate::ty::subst::SubstsRef;
1717
use crate::ty::{self, List, Ty, TyCtxt};
1818
use rustc_data_structures::fx::FxHashMap;
19-
use rustc_hir::def_id::DefId;
2019
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2120
use rustc_span::Span;
2221
use std::hash::Hash;
@@ -160,7 +159,8 @@ encodable_via_deref! {
160159
&'tcx mir::Body<'tcx>,
161160
&'tcx mir::UnsafetyCheckResult,
162161
&'tcx mir::BorrowCheckResult<'tcx>,
163-
&'tcx mir::coverage::CodeRegion
162+
&'tcx mir::coverage::CodeRegion,
163+
&'tcx ty::AdtDef
164164
}
165165

166166
pub trait TyDecoder<'tcx>: Decoder {
@@ -312,13 +312,6 @@ macro_rules! impl_decodable_via_ref {
312312
}
313313
}
314314

315-
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::AdtDef {
316-
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
317-
let def_id = <DefId as Decodable<D>>::decode(decoder)?;
318-
Ok(decoder.tcx().adt_def(def_id))
319-
}
320-
}
321-
322315
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<Ty<'tcx>> {
323316
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
324317
let len = decoder.read_usize()?;
@@ -403,7 +396,8 @@ impl_decodable_via_ref! {
403396
&'tcx mir::UnsafetyCheckResult,
404397
&'tcx mir::BorrowCheckResult<'tcx>,
405398
&'tcx mir::coverage::CodeRegion,
406-
&'tcx ty::List<ty::BoundVariableKind>
399+
&'tcx ty::List<ty::BoundVariableKind>,
400+
&'tcx ty::AdtDef
407401
}
408402

409403
#[macro_export]

compiler/rustc_middle/src/ty/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub struct CtxtInterners<'tcx> {
112112
const_allocation: InternedSet<'tcx, Allocation>,
113113
bound_variable_kinds: InternedSet<'tcx, List<ty::BoundVariableKind>>,
114114
layout: InternedSet<'tcx, Layout>,
115+
adt_def: InternedSet<'tcx, AdtDef>,
115116
}
116117

117118
impl<'tcx> CtxtInterners<'tcx> {
@@ -132,6 +133,7 @@ impl<'tcx> CtxtInterners<'tcx> {
132133
const_allocation: Default::default(),
133134
bound_variable_kinds: Default::default(),
134135
layout: Default::default(),
136+
adt_def: Default::default(),
135137
}
136138
}
137139

@@ -1078,7 +1080,7 @@ impl<'tcx> TyCtxt<'tcx> {
10781080
variants: IndexVec<VariantIdx, ty::VariantDef>,
10791081
repr: ReprOptions,
10801082
) -> &'tcx ty::AdtDef {
1081-
self.arena.alloc(ty::AdtDef::new(self, did, kind, variants, repr))
1083+
self.intern_adt_def(ty::AdtDef::new(self, did, kind, variants, repr))
10821084
}
10831085

10841086
/// Allocates a read-only byte or string literal for `mir::interpret`.
@@ -2057,6 +2059,7 @@ direct_interners! {
20572059
const_: mk_const(Const<'tcx>),
20582060
const_allocation: intern_const_alloc(Allocation),
20592061
layout: intern_layout(Layout),
2062+
adt_def: intern_adt_def(AdtDef),
20602063
}
20612064

20622065
macro_rules! slice_interners {

compiler/rustc_middle/src/ty/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ pub struct Destructor {
14721472
}
14731473

14741474
bitflags! {
1475-
#[derive(HashStable)]
1475+
#[derive(HashStable, TyEncodable, TyDecodable)]
14761476
pub struct VariantFlags: u32 {
14771477
const NO_VARIANT_FLAGS = 0;
14781478
/// Indicates whether the field list of this variant is `#[non_exhaustive]`.
@@ -1484,7 +1484,7 @@ bitflags! {
14841484
}
14851485

14861486
/// Definition of a variant -- a struct's fields or an enum variant.
1487-
#[derive(Debug, HashStable)]
1487+
#[derive(Debug, HashStable, TyEncodable, TyDecodable)]
14881488
pub struct VariantDef {
14891489
/// `DefId` that identifies the variant itself.
14901490
/// If this variant belongs to a struct or union, then this is a copy of its `DefId`.
@@ -1586,7 +1586,7 @@ pub enum VariantDiscr {
15861586
Relative(u32),
15871587
}
15881588

1589-
#[derive(Debug, HashStable)]
1589+
#[derive(Debug, HashStable, TyEncodable, TyDecodable)]
15901590
pub struct FieldDef {
15911591
pub did: DefId,
15921592
#[stable_hasher(project(name))]

0 commit comments

Comments
 (0)