Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_metadata: simplify the interactions between Lazy and Table. #66399

Merged
merged 4 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/librustc_metadata/rmeta/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Decoding metadata from a single crate's metadata

use crate::rmeta::*;
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};
use crate::rmeta::table::{FixedSizeEncoding, Table};

use rustc_index::vec::IndexVec;
use rustc_index::vec::{Idx, IndexVec};
use rustc_data_structures::sync::{Lrc, Lock, Once, AtomicCell};
use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash};
use rustc::hir::map::definitions::DefPathTable;
Expand Down Expand Up @@ -32,7 +32,7 @@ use std::mem;
use std::num::NonZeroUsize;
use std::u32;

use rustc_serialize::{Decodable, Decoder, Encodable, SpecializedDecoder, opaque};
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
use syntax::attr;
use syntax::ast::{self, Ident};
use syntax::source_map::{self, respan, Spanned};
Expand Down Expand Up @@ -217,15 +217,15 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
}
}

impl<'a, 'tcx, T: Encodable + Decodable> Lazy<T> {
impl<'a, 'tcx, T: Decodable> Lazy<T> {
fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T {
let mut dcx = metadata.decoder(self.position.get());
dcx.lazy_state = LazyState::NodeStart(self.position);
T::decode(&mut dcx).unwrap()
}
}

impl<'a: 'x, 'tcx: 'x, 'x, T: Encodable + Decodable> Lazy<[T]> {
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> {
fn decode<M: Metadata<'a, 'tcx>>(
self,
metadata: M,
Expand Down Expand Up @@ -324,13 +324,13 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
}
}

impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
self.read_lazy_with_meta(())
}
}

impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> {
let len = self.read_usize()?;
if len == 0 {
Expand All @@ -341,10 +341,10 @@ impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a,
}
}

impl<'a, 'tcx, T> SpecializedDecoder<Lazy<PerDefTable<T>>> for DecodeContext<'a, 'tcx>
impl<'a, 'tcx, I: Idx, T> SpecializedDecoder<Lazy<Table<I, T>>> for DecodeContext<'a, 'tcx>
where Option<T>: FixedSizeEncoding,
{
fn specialized_decode(&mut self) -> Result<Lazy<PerDefTable<T>>, Self::Error> {
fn specialized_decode(&mut self) -> Result<Lazy<Table<I, T>>, Self::Error> {
let len = self.read_usize()?;
self.read_lazy_with_meta(len)
}
Expand Down
67 changes: 11 additions & 56 deletions src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::rmeta::*;
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};
use crate::rmeta::table::FixedSizeEncoding;

use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
EncodedMetadata, ForeignModule};
Expand All @@ -8,7 +8,7 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId,
use rustc::hir::{GenericParamKind, AnonConst};
use rustc::hir::map::definitions::DefPathTable;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_index::vec::IndexVec;
use rustc_index::vec::Idx;
use rustc::middle::dependency_format::Linkage;
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel,
metadata_symbol_name};
Expand Down Expand Up @@ -47,7 +47,7 @@ struct EncodeContext<'tcx> {
opaque: opaque::Encoder,
tcx: TyCtxt<'tcx>,

per_def: PerDefTables<'tcx>,
per_def: PerDefTableBuilders<'tcx>,

lazy_state: LazyState,
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
Expand All @@ -60,30 +60,6 @@ struct EncodeContext<'tcx> {
source_file_cache: Lrc<SourceFile>,
}

#[derive(Default)]
struct PerDefTables<'tcx> {
kind: PerDefTable<Lazy<EntryKind<'tcx>>>,
visibility: PerDefTable<Lazy<ty::Visibility>>,
span: PerDefTable<Lazy<Span>>,
attributes: PerDefTable<Lazy<[ast::Attribute]>>,
children: PerDefTable<Lazy<[DefIndex]>>,
stability: PerDefTable<Lazy<attr::Stability>>,
deprecation: PerDefTable<Lazy<attr::Deprecation>>,

ty: PerDefTable<Lazy<Ty<'tcx>>>,
fn_sig: PerDefTable<Lazy<ty::PolyFnSig<'tcx>>>,
impl_trait_ref: PerDefTable<Lazy<ty::TraitRef<'tcx>>>,
inherent_impls: PerDefTable<Lazy<[DefIndex]>>,
variances: PerDefTable<Lazy<[ty::Variance]>>,
generics: PerDefTable<Lazy<ty::Generics>>,
explicit_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
inferred_outlives: PerDefTable<Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
super_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,

mir: PerDefTable<Lazy<mir::Body<'tcx>>>,
promoted_mir: PerDefTable<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
}

macro_rules! encoder_methods {
($($name:ident($ty:ty);)*) => {
$(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -122,13 +98,13 @@ impl<'tcx> Encoder for EncodeContext<'tcx> {
}
}

impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
impl<'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> {
self.emit_lazy_distance(*lazy)
}
}

impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
fn specialized_encode(&mut self, lazy: &Lazy<[T]>) -> Result<(), Self::Error> {
self.emit_usize(lazy.meta)?;
if lazy.meta == 0 {
Expand All @@ -138,10 +114,10 @@ impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
}
}

impl<'tcx, T> SpecializedEncoder<Lazy<PerDefTable<T>>> for EncodeContext<'tcx>
impl<'tcx, I: Idx, T> SpecializedEncoder<Lazy<Table<I, T>>> for EncodeContext<'tcx>
where Option<T>: FixedSizeEncoding,
{
fn specialized_encode(&mut self, lazy: &Lazy<PerDefTable<T>>) -> Result<(), Self::Error> {
fn specialized_encode(&mut self, lazy: &Lazy<Table<I, T>>) -> Result<(), Self::Error> {
self.emit_usize(lazy.meta)?;
self.emit_lazy_distance(*lazy)
}
Expand Down Expand Up @@ -307,14 +283,14 @@ impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I
}
}

// Shorthand for `$self.$tables.$table.set($key, $self.lazy($value))`, which would
// Shorthand for `$self.$tables.$table.set($def_id.index, $self.lazy($value))`, which would
// normally need extra variables to avoid errors about multiple mutable borrows.
macro_rules! record {
($self:ident.$tables:ident.$table:ident[$key:expr] <- $value:expr) => {{
($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
{
let value = $value;
let lazy = $self.lazy(value);
$self.$tables.$table.set($key, lazy);
$self.$tables.$table.set($def_id.index, lazy);
}
}}
}
Expand Down Expand Up @@ -509,28 +485,7 @@ impl<'tcx> EncodeContext<'tcx> {


i = self.position();
let per_def = LazyPerDefTables {
kind: self.per_def.kind.encode(&mut self.opaque),
visibility: self.per_def.visibility.encode(&mut self.opaque),
span: self.per_def.span.encode(&mut self.opaque),
attributes: self.per_def.attributes.encode(&mut self.opaque),
children: self.per_def.children.encode(&mut self.opaque),
stability: self.per_def.stability.encode(&mut self.opaque),
deprecation: self.per_def.deprecation.encode(&mut self.opaque),

ty: self.per_def.ty.encode(&mut self.opaque),
fn_sig: self.per_def.fn_sig.encode(&mut self.opaque),
impl_trait_ref: self.per_def.impl_trait_ref.encode(&mut self.opaque),
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
variances: self.per_def.variances.encode(&mut self.opaque),
generics: self.per_def.generics.encode(&mut self.opaque),
explicit_predicates: self.per_def.explicit_predicates.encode(&mut self.opaque),
inferred_outlives: self.per_def.inferred_outlives.encode(&mut self.opaque),
super_predicates: self.per_def.super_predicates.encode(&mut self.opaque),

mir: self.per_def.mir.encode(&mut self.opaque),
promoted_mir: self.per_def.promoted_mir.encode(&mut self.opaque),
};
let per_def = self.per_def.encode(&mut self.opaque);
let per_def_bytes = self.position() - i;

// Encode the proc macro data
Expand Down
77 changes: 49 additions & 28 deletions src/librustc_metadata/rmeta/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use decoder::Metadata;
use table::PerDefTable;
use table::{Table, TableBuilder};

use rustc::hir;
use rustc::hir::def::{self, CtorKind};
Expand All @@ -15,7 +15,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple};
use rustc_index::vec::IndexVec;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef;
use rustc_serialize::Encodable;
use rustc_serialize::opaque::Encoder;
use syntax::{ast, attr};
use syntax::edition::Edition;
use syntax::symbol::Symbol;
Expand Down Expand Up @@ -59,7 +59,7 @@ trait LazyMeta {
fn min_size(meta: Self::Meta) -> usize;
}

impl<T: Encodable> LazyMeta for T {
impl<T> LazyMeta for T {
type Meta = ();

fn min_size(_: ()) -> usize {
Expand All @@ -68,7 +68,7 @@ impl<T: Encodable> LazyMeta for T {
}
}

impl<T: Encodable> LazyMeta for [T] {
impl<T> LazyMeta for [T] {
type Meta = usize;

fn min_size(len: usize) -> usize {
Expand Down Expand Up @@ -124,13 +124,13 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
}
}

impl<T: Encodable> Lazy<T> {
impl<T> Lazy<T> {
fn from_position(position: NonZeroUsize) -> Lazy<T> {
Lazy::from_position_and_meta(position, ())
}
}

impl<T: Encodable> Lazy<[T]> {
impl<T> Lazy<[T]> {
fn empty() -> Lazy<[T]> {
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
}
Expand Down Expand Up @@ -166,8 +166,7 @@ enum LazyState {
// manually, instead of relying on the default, to get the correct variance.
// Only needed when `T` itself contains a parameter (e.g. `'tcx`).
macro_rules! Lazy {
(Table<$T:ty>) => {Lazy<Table<$T>, usize>};
(PerDefTable<$T:ty>) => {Lazy<PerDefTable<$T>, usize>};
(Table<$I:ty, $T:ty>) => {Lazy<Table<$I, $T>, usize>};
([$T:ty]) => {Lazy<[$T], usize>};
($T:ty) => {Lazy<$T, ()>};
}
Expand Down Expand Up @@ -232,31 +231,53 @@ crate struct TraitImpls {
impls: Lazy<[DefIndex]>,
}

#[derive(RustcEncodable, RustcDecodable)]
crate struct LazyPerDefTables<'tcx> {
kind: Lazy!(PerDefTable<Lazy!(EntryKind<'tcx>)>),
visibility: Lazy!(PerDefTable<Lazy<ty::Visibility>>),
span: Lazy!(PerDefTable<Lazy<Span>>),
attributes: Lazy!(PerDefTable<Lazy<[ast::Attribute]>>),
children: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
stability: Lazy!(PerDefTable<Lazy<attr::Stability>>),
deprecation: Lazy!(PerDefTable<Lazy<attr::Deprecation>>),
ty: Lazy!(PerDefTable<Lazy!(Ty<'tcx>)>),
fn_sig: Lazy!(PerDefTable<Lazy!(ty::PolyFnSig<'tcx>)>),
impl_trait_ref: Lazy!(PerDefTable<Lazy!(ty::TraitRef<'tcx>)>),
inherent_impls: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
variances: Lazy!(PerDefTable<Lazy<[ty::Variance]>>),
generics: Lazy!(PerDefTable<Lazy<ty::Generics>>),
explicit_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
/// Define `LazyPerDefTables` and `PerDefTableBuilders` at the same time.
macro_rules! define_per_def_tables {
($($name:ident: Table<DefIndex, $T:ty>),+ $(,)?) => {
#[derive(RustcEncodable, RustcDecodable)]
crate struct LazyPerDefTables<'tcx> {
$($name: Lazy!(Table<DefIndex, $T>)),+
}

#[derive(Default)]
struct PerDefTableBuilders<'tcx> {
$($name: TableBuilder<DefIndex, $T>),+
}

impl PerDefTableBuilders<'tcx> {
fn encode(&self, buf: &mut Encoder) -> LazyPerDefTables<'tcx> {
LazyPerDefTables {
$($name: self.$name.encode(buf)),+
}
}
}
}
}

define_per_def_tables! {
kind: Table<DefIndex, Lazy!(EntryKind<'tcx>)>,
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
span: Table<DefIndex, Lazy<Span>>,
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
children: Table<DefIndex, Lazy<[DefIndex]>>,
stability: Table<DefIndex, Lazy<attr::Stability>>,
deprecation: Table<DefIndex, Lazy<attr::Deprecation>>,
ty: Table<DefIndex, Lazy!(Ty<'tcx>)>,
fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>,
impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>,
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
variances: Table<DefIndex, Lazy<[ty::Variance]>>,
generics: Table<DefIndex, Lazy<ty::Generics>>,
explicit_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
// FIXME(eddyb) this would ideally be `Lazy<[...]>` but `ty::Predicate`
// doesn't handle shorthands in its own (de)serialization impls,
// as it's an `enum` for which we want to derive (de)serialization,
// so the `ty::codec` APIs handle the whole `&'tcx [...]` at once.
// Also, as an optimization, a missing entry indicates an empty `&[]`.
inferred_outlives: Lazy!(PerDefTable<Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>),
super_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
mir: Lazy!(PerDefTable<Lazy!(mir::Body<'tcx>)>),
promoted_mir: Lazy!(PerDefTable<Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>),
inferred_outlives: Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>,
super_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
}

#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
Expand Down
Loading