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

Cache expansion hash globally #87044

Merged
merged 2 commits into from
Jul 14, 2021
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
46 changes: 43 additions & 3 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_ID};
use rustc_hir::def_id::{DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_ID};
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
use rustc_hir::intravisit;
use rustc_hir::{ConstArg, GenericArg, ParamName};
Expand All @@ -59,7 +59,7 @@ use rustc_session::utils::{FlattenNonterminals, NtToTokenstream};
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{respan, DesugaringKind};
use rustc_span::source_map::{respan, CachingSourceMapView, DesugaringKind};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_target::spec::abi::Abi;
Expand Down Expand Up @@ -204,6 +204,8 @@ pub trait ResolverAstLowering {

fn local_def_id(&self, node: NodeId) -> LocalDefId;

fn def_path_hash(&self, def_id: DefId) -> DefPathHash;

fn create_def(
&mut self,
parent: LocalDefId,
Expand All @@ -214,6 +216,32 @@ pub trait ResolverAstLowering {
) -> LocalDefId;
}

struct LoweringHasher<'a> {
source_map: CachingSourceMapView<'a>,
resolver: &'a dyn ResolverAstLowering,
}

impl<'a> rustc_span::HashStableContext for LoweringHasher<'a> {
#[inline]
fn hash_spans(&self) -> bool {
true
}

#[inline]
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
self.resolver.def_path_hash(def_id)
}

#[inline]
fn span_data_to_lines_and_cols(
&mut self,
span: &rustc_span::SpanData,
) -> Option<(Lrc<rustc_span::SourceFile>, usize, rustc_span::BytePos, usize, rustc_span::BytePos)>
{
self.source_map.span_data_to_lines_and_cols(span)
}
}

/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
/// and if so, what meaning it has.
#[derive(Debug)]
Expand Down Expand Up @@ -565,6 +593,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lowered
}

fn create_stable_hashing_context(&self) -> LoweringHasher<'_> {
LoweringHasher {
source_map: CachingSourceMapView::new(self.sess.source_map()),
resolver: self.resolver,
}
}

fn lower_node_id_generic(
&mut self,
ast_node_id: NodeId,
Expand Down Expand Up @@ -684,7 +719,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span: Span,
allow_internal_unstable: Option<Lrc<[Symbol]>>,
) -> Span {
span.mark_with_reason(allow_internal_unstable, reason, self.sess.edition())
span.mark_with_reason(
allow_internal_unstable,
reason,
self.sess.edition(),
self.create_stable_hashing_context(),
)
}

fn with_anonymous_lifetime_mode<R>(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
.map(|(path, item, _exts)| {
// FIXME: Consider using the derive resolutions (`_exts`)
// instead of enqueuing the derives to be resolved again later.
let expn_id = ExpnId::fresh(None);
let expn_id = ExpnId::fresh_empty();
derive_invocations.push((
Invocation {
kind: InvocationKind::Derive { path, item },
Expand Down Expand Up @@ -989,7 +989,7 @@ struct InvocationCollector<'a, 'b> {

impl<'a, 'b> InvocationCollector<'a, 'b> {
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
let expn_id = ExpnId::fresh(None);
let expn_id = ExpnId::fresh_empty();
let vis = kind.placeholder_visibility();
self.invocations.push((
Invocation {
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,19 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
} else {
local_cdata.cstore.get_crate_data(cnum)
};
Ok(crate_data
let expn_data = crate_data
.root
.expn_data
.get(&crate_data, index)
.unwrap()
.decode((&crate_data, sess)))
.decode((&crate_data, sess));
let expn_hash = crate_data
.root
.expn_hashes
.get(&crate_data, index)
.unwrap()
.decode((&crate_data, sess));
Ok((expn_data, expn_hash))
},
)
}
Expand Down
19 changes: 13 additions & 6 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// Therefore, we need to encode the hygiene data last to ensure that we encode
// any `SyntaxContext`s that might be used.
i = self.position();
let (syntax_contexts, expn_data) = self.encode_hygiene();
let (syntax_contexts, expn_data, expn_hashes) = self.encode_hygiene();
let hygiene_bytes = self.position() - i;

// Encode source_map. This needs to be done last,
Expand Down Expand Up @@ -701,6 +701,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
tables,
syntax_contexts,
expn_data,
expn_hashes,
});

let total_bytes = self.position();
Expand Down Expand Up @@ -1578,23 +1579,29 @@ impl EncodeContext<'a, 'tcx> {
self.lazy(foreign_modules.iter().map(|(_, m)| m).cloned())
}

fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable) {
fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
let mut syntax_contexts: TableBuilder<_, _> = Default::default();
let mut expn_data_table: TableBuilder<_, _> = Default::default();
let mut expn_hash_table: TableBuilder<_, _> = Default::default();

let _: Result<(), !> = self.hygiene_ctxt.encode(
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table),
|(this, syntax_contexts, _), index, ctxt_data| {
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
|(this, syntax_contexts, _, _), index, ctxt_data| {
syntax_contexts.set(index, this.lazy(ctxt_data));
Ok(())
},
|(this, _, expn_data_table), index, expn_data| {
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
expn_data_table.set(index, this.lazy(expn_data));
expn_hash_table.set(index, this.lazy(hash));
Ok(())
},
);

(syntax_contexts.encode(&mut self.opaque), expn_data_table.encode(&mut self.opaque))
(
syntax_contexts.encode(&mut self.opaque),
expn_data_table.encode(&mut self.opaque),
expn_hash_table.encode(&mut self.opaque),
)
}

fn encode_proc_macros(&mut self) -> Option<ProcMacroData> {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_session::config::SymbolManglingVersion;
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::{self, ExpnData, ExpnId, Span};
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
use rustc_target::spec::{PanicStrategy, TargetTriple};

use std::marker::PhantomData;
Expand Down Expand Up @@ -171,6 +171,7 @@ macro_rules! Lazy {

type SyntaxContextTable = Lazy<Table<u32, Lazy<SyntaxContextData>>>;
type ExpnDataTable = Lazy<Table<u32, Lazy<ExpnData>>>;
type ExpnHashTable = Lazy<Table<u32, Lazy<ExpnHash>>>;

#[derive(MetadataEncodable, MetadataDecodable)]
crate struct ProcMacroData {
Expand Down Expand Up @@ -226,6 +227,7 @@ crate struct CrateRoot<'tcx> {

syntax_contexts: SyntaxContextTable,
expn_data: ExpnDataTable,
expn_hashes: ExpnHashTable,

source_map: Lazy<[rustc_span::SourceFile]>,

Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_middle/src/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rustc_span::{BytePos, CachingSourceMapView, SourceFile, SpanData};

use smallvec::SmallVec;
use std::cmp::Ord;
use std::thread::LocalKey;

fn compute_ignored_attr_names() -> FxHashSet<Symbol> {
debug_assert!(!ich::IGNORED_ATTRIBUTES.is_empty());
Expand Down Expand Up @@ -230,13 +229,6 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
self.def_path_hash(def_id)
}

fn expn_id_cache() -> &'static LocalKey<rustc_span::ExpnIdCache> {
thread_local! {
static CACHE: rustc_span::ExpnIdCache = Default::default();
}
&CACHE
}

fn span_data_to_lines_and_cols(
&mut self,
span: &SpanData,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_span::hygiene::{
};
use rustc_span::source_map::{SourceMap, StableSourceFileId};
use rustc_span::CachingSourceMapView;
use rustc_span::{BytePos, ExpnData, SourceFile, Span, DUMMY_SP};
use rustc_span::{BytePos, ExpnData, ExpnHash, SourceFile, Span, DUMMY_SP};
use std::collections::hash_map::Entry;
use std::mem;

Expand Down Expand Up @@ -364,9 +364,9 @@ impl<'sess> OnDiskCache<'sess> {
syntax_contexts.insert(index, pos);
Ok(())
},
|encoder, index, expn_data| -> FileEncodeResult {
|encoder, index, expn_data, hash| -> FileEncodeResult {
let pos = AbsoluteBytePos::new(encoder.position());
encoder.encode_tagged(TAG_EXPN_DATA, expn_data)?;
encoder.encode_tagged(TAG_EXPN_DATA, &(expn_data, hash))?;
expn_ids.insert(index, pos);
Ok(())
},
Expand Down Expand Up @@ -804,7 +804,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
.unwrap_or_else(|| panic!("Bad index {:?} (map {:?})", index, expn_data));

this.with_position(pos.to_usize(), |decoder| {
let data: ExpnData = decode_tagged(decoder, TAG_EXPN_DATA)?;
let data: (ExpnData, ExpnHash) = decode_tagged(decoder, TAG_EXPN_DATA)?;
Ok(data)
})
},
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir/src/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,8 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None, None);
expn_data.def_site = self.body_span;
// Make sure that all spans track the fact that they were inlined.
*span = self.callsite_span.fresh_expansion(expn_data);
*span =
self.callsite_span.fresh_expansion(expn_data, self.tcx.create_stable_hashing_context());
}

fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
Expand Down
44 changes: 42 additions & 2 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
use rustc_hir::def::Namespace::*;
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
use rustc_hir::TraitCandidate;
use rustc_index::vec::IndexVec;
Expand All @@ -54,7 +54,7 @@ use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::hygiene::{ExpnId, ExpnKind, MacroKind, SyntaxContext, Transparency};
use rustc_span::source_map::Spanned;
use rustc_span::source_map::{CachingSourceMapView, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};

Expand Down Expand Up @@ -1149,6 +1149,13 @@ impl ResolverAstLowering for Resolver<'_> {
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
}

fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
match def_id.as_local() {
Some(def_id) => self.definitions.def_path_hash(def_id),
None => self.cstore().def_path_hash(def_id),
}
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
}

/// Adds a definition with a parent definition.
fn create_def(
&mut self,
Expand Down Expand Up @@ -1192,6 +1199,32 @@ impl ResolverAstLowering for Resolver<'_> {
}
}

struct ExpandHasher<'a, 'b> {
source_map: CachingSourceMapView<'a>,
resolver: &'a Resolver<'b>,
}

impl<'a, 'b> rustc_span::HashStableContext for ExpandHasher<'a, 'b> {
#[inline]
fn hash_spans(&self) -> bool {
true
}

#[inline]
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
self.resolver.def_path_hash(def_id)
}

#[inline]
fn span_data_to_lines_and_cols(
&mut self,
span: &rustc_span::SpanData,
) -> Option<(Lrc<rustc_span::SourceFile>, usize, rustc_span::BytePos, usize, rustc_span::BytePos)>
{
self.source_map.span_data_to_lines_and_cols(span)
}
}

impl<'a> Resolver<'a> {
pub fn new(
session: &'a Session,
Expand Down Expand Up @@ -1364,6 +1397,13 @@ impl<'a> Resolver<'a> {
resolver
}

fn create_stable_hashing_context(&self) -> ExpandHasher<'_, 'a> {
ExpandHasher {
source_map: CachingSourceMapView::new(self.session.source_map()),
resolver: self,
}
}

pub fn next_node_id(&mut self) -> NodeId {
let next = self
.next_node_id
Expand Down
40 changes: 23 additions & 17 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,17 @@ impl<'a> ResolverExpand for Resolver<'a> {
parent_module_id: Option<NodeId>,
) -> ExpnId {
let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
ExpnKind::AstPass(pass),
call_site,
self.session.edition(),
features.into(),
None,
parent_module.map(LocalDefId::to_def_id),
)));
let expn_id = ExpnId::fresh(
ExpnData::allow_unstable(
ExpnKind::AstPass(pass),
call_site,
self.session.edition(),
features.into(),
None,
parent_module.map(LocalDefId::to_def_id),
),
self.create_stable_hashing_context(),
);

let parent_scope = parent_module
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
Expand Down Expand Up @@ -287,15 +290,18 @@ impl<'a> ResolverExpand for Resolver<'a> {
)?;

let span = invoc.span();
invoc_id.set_expn_data(ext.expn_data(
parent_scope.expansion,
span,
fast_print_path(path),
res.opt_def_id(),
res.opt_def_id().map(|macro_def_id| {
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
}),
));
invoc_id.set_expn_data(
ext.expn_data(
parent_scope.expansion,
span,
fast_print_path(path),
res.opt_def_id(),
res.opt_def_id().map(|macro_def_id| {
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
}),
),
self.create_stable_hashing_context(),
);

if let Res::Def(_, _) = res {
// Gate macro attributes in `#[derive]` output.
Expand Down
Loading