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

Replace DepKind by trait objects #78314

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions compiler/rustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
if modifiers.eval_always {
attributes.push(quote! { eval_always });
};
// Pass on the cache modifier
if modifiers.cache.is_some() {
attributes.push(quote! { cached });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this attribute used? I can't find it.

EDIT: Fount it in encode_query_results. Can you add a comment documenting this dependency?

};

let attribute_stream = quote! {#(#attributes),*};
let doc_comments = query.doc_comments.iter();
Expand Down
68 changes: 67 additions & 1 deletion compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::definitions::DefPathHash;
use rustc_hir::HirId;
use rustc_query_system::query::QueryAccessors;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
use rustc_span::symbol::Symbol;
use std::hash::Hash;

Expand All @@ -84,6 +84,13 @@ pub trait DepKindTrait: std::fmt::Debug + Sync {

fn has_params(&self) -> bool;

fn encode_query_results<'a, 'tcx>(
&self,
tcx: TyCtxt<'tcx>,
encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
);

fn force_from_dep_node(&self, tcx: TyCtxt<'_>, dep_node: &DepNode) -> bool;

fn query_stats(&self, tcx: TyCtxt<'_>) -> Option<query::stats::QueryStats>;
Expand Down Expand Up @@ -124,6 +131,19 @@ macro_rules! contains_eval_always_attr {
($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
}

macro_rules! encode_query_results {
([][$variant:ident][$($args:expr),*]) => {{}};
([cached $($rest:tt)*][$variant:ident][$($args:expr),*]) => {{
let ret = query::on_disk_cache::encode_query_results::<
query::queries::$variant<'_>
>($($args),*);
match ret { Ok(()) => (), Err(_) => () }
}};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$variant:ident][$($args:expr),*]) => {
encode_query_results!([$($($modifiers)*)*][$variant][$($args),*])
};
}

macro_rules! define_dep_kinds {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this into a new file dep_kind.rs.

(<$tcx:tt>
$(
Expand Down Expand Up @@ -176,6 +196,16 @@ macro_rules! define_dep_kinds {
false
}

#[inline]
fn encode_query_results<'a, 'tcx>(
&self,
_tcx: TyCtxt<'tcx>,
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
) {
encode_query_results!([$($attrs)*][$variant][_tcx, _encoder, _query_result_index]);
}

#[inline]
fn force_from_dep_node(&self, tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool {
use rustc_query_system::query::force_query;
Expand Down Expand Up @@ -422,6 +452,15 @@ impl DepKindTrait for dep_kind::Null {
false
}

#[inline]
fn encode_query_results<'a, 'tcx>(
&self,
_tcx: TyCtxt<'tcx>,
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
) {
}

#[inline]
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
// Forcing this makes no sense.
Expand Down Expand Up @@ -463,6 +502,15 @@ impl DepKindTrait for dep_kind::CrateMetadata {
true
}

#[inline]
fn encode_query_results<'a, 'tcx>(
&self,
_tcx: TyCtxt<'tcx>,
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
) {
}

#[inline]
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
// These are inputs that are expected to be pre-allocated and that
Expand Down Expand Up @@ -509,6 +557,15 @@ impl DepKindTrait for dep_kind::TraitSelect {
false
}

#[inline]
fn encode_query_results<'a, 'tcx>(
&self,
_tcx: TyCtxt<'tcx>,
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
) {
}

#[inline]
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
// These are anonymous nodes.
Expand Down Expand Up @@ -555,6 +612,15 @@ impl DepKindTrait for dep_kind::CompileCodegenUnit {
true
}

#[inline]
fn encode_query_results<'a, 'tcx>(
&self,
_tcx: TyCtxt<'tcx>,
_encoder: &mut query::on_disk_cache::CacheEncoder<'a, 'tcx, opaque::Encoder>,
_query_result_index: &mut query::on_disk_cache::EncodedQueryResultIndex,
) {
}

#[inline]
fn force_from_dep_node(&self, _tcx: TyCtxt<'tcx>, _dep_node: &DepNode) -> bool {
// We don't have enough information to reconstruct the query key of these.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use rustc_query_system::query::QueryAccessors;
pub use rustc_query_system::query::QueryConfig;
pub(crate) use rustc_query_system::query::QueryDescription;

mod on_disk_cache;
pub mod on_disk_cache;
pub use self::on_disk_cache::OnDiskCache;

mod profiling_support;
Expand Down
28 changes: 8 additions & 20 deletions compiler/rustc_middle/src/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ struct Footer {
expn_data: FxHashMap<u32, AbsoluteBytePos>,
}

type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
pub type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
type EncodedDiagnostics = Vec<Diagnostic>;

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
struct SourceFileIndex(u32);

#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
struct AbsoluteBytePos(u32);
pub struct AbsoluteBytePos(u32);

impl AbsoluteBytePos {
fn new(pos: usize) -> AbsoluteBytePos {
Expand Down Expand Up @@ -226,22 +226,10 @@ impl<'sess> OnDiskCache<'sess> {
let enc = &mut encoder;
let qri = &mut query_result_index;

macro_rules! encode_queries {
($($query:ident,)*) => {
$(
encode_query_results::<ty::query::queries::$query<'_>>(
tcx,
enc,
qri
)?;
)*
}
for dk in rustc_middle::dep_graph::DEP_KINDS {
dk.encode_query_results(tcx, enc, qri)
}

rustc_cached_queries!(encode_queries!);

Ok(())
})?;
});

// Encode diagnostics.
let diagnostics_index: EncodedDiagnosticsIndex = self
Expand Down Expand Up @@ -765,7 +753,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Span] {
//- ENCODING -------------------------------------------------------------------

/// This trait is a hack to work around specialization bug #55243.
trait OpaqueEncoder: Encoder {
pub trait OpaqueEncoder: Encoder {
Copy link
Contributor

@Julian-Wollersberger Julian-Wollersberger Oct 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move the trait to make it private and then make it public again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trait is a hack. Ideally, all E: OpaqueEncoder should be E = opaque::Encoder. It needs to be public because it appears in CacheEncoder, which is public because it appears in DepKindTrait. Removing E: OpaqueEncoder from CacheEncoder definition breaks specialization (I don't know why).

fn opaque(&mut self) -> &mut opaque::Encoder;
fn encoder_position(&self) -> usize;
}
Expand All @@ -782,7 +770,7 @@ impl OpaqueEncoder for opaque::Encoder {
}

/// An encoder that can write the incr. comp. cache.
struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
tcx: TyCtxt<'tcx>,
encoder: &'a mut E,
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
Expand Down Expand Up @@ -1005,7 +993,7 @@ impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
}
}

fn encode_query_results<'a, 'tcx, Q>(
pub fn encode_query_results<'a, 'tcx, Q>(
tcx: TyCtxt<'tcx>,
encoder: &mut CacheEncoder<'a, 'tcx, opaque::Encoder>,
query_result_index: &mut EncodedQueryResultIndex,
Expand Down