Skip to content
Open
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
16 changes: 9 additions & 7 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::fs::File;
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::Arc;

use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
Expand Down Expand Up @@ -66,7 +68,7 @@ pub(super) struct EncodeContext<'a, 'tcx> {
// order of `SourceFiles`, and encoded inside `Span`s.
required_source_files: Option<FxIndexSet<usize>>,
is_proc_macro: bool,
hygiene_ctxt: &'a HygieneEncodeContext,
hygiene_ctxt: Rc<RefCell<HygieneEncodeContext>>,
// Used for both `Symbol`s and `ByteSymbol`s.
symbol_index_table: FxHashMap<u32, usize>,
}
Expand Down Expand Up @@ -156,7 +158,8 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
}

fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_ctxt, self);
let idx = self.hygiene_ctxt.borrow_mut().raw_encode_syntax_context(syntax_context);
idx.encode(self);
}

fn encode_expn_id(&mut self, expn_id: ExpnId) {
Expand All @@ -165,7 +168,7 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
// data from the corresponding crate's metadata.
// FIXME(#43047) FIXME(#74731) We may eventually want to avoid relying on external
// metadata from proc-macro crates.
self.hygiene_ctxt.schedule_expn_data_for_encoding(expn_id);
self.hygiene_ctxt.borrow_mut().schedule_expn_data_for_encoding(expn_id);
}
expn_id.krate.encode(self);
expn_id.local_id.encode(self);
Expand Down Expand Up @@ -1955,7 +1958,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let mut expn_data_table: TableBuilder<_, _> = Default::default();
let mut expn_hash_table: TableBuilder<_, _> = Default::default();

self.hygiene_ctxt.encode(
HygieneEncodeContext::encode(
&Rc::clone(&self.hygiene_ctxt),
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
|(this, syntax_contexts, _, _), index, ctxt_data| {
syntax_contexts.set_some(index, this.lazy(ctxt_data));
Expand Down Expand Up @@ -2548,8 +2552,6 @@ fn with_encode_metadata_header(
let required_source_files = Some(FxIndexSet::default());
drop(source_map_files);

let hygiene_ctxt = HygieneEncodeContext::default();

let mut ecx = EncodeContext {
opaque: encoder,
tcx,
Expand All @@ -2563,7 +2565,7 @@ fn with_encode_metadata_header(
interpret_allocs: Default::default(),
required_source_files,
is_proc_macro: tcx.crate_types().contains(&CrateType::ProcMacro),
hygiene_ctxt: &hygiene_ctxt,
hygiene_ctxt: Default::default(),
symbol_index_table: Default::default(),
};

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ declare_hooks! {
hook build_mir_inner_impl(def: LocalDefId) -> mir::Body<'tcx>;

/// Serializes all eligible query return values into the on-disk cache.
hook encode_query_values(encoder: &mut CacheEncoder<'_, 'tcx>) -> ();
hook encode_query_values(encoder: &mut CacheEncoder<'tcx>) -> ();
}

#[cold]
Expand Down
32 changes: 17 additions & 15 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::rc::Rc;
use std::sync::Arc;
use std::{fmt, mem};

Expand Down Expand Up @@ -223,8 +225,6 @@ impl OnDiskCache {
(file_to_file_index, file_index_to_stable_id)
};

let hygiene_encode_context = HygieneEncodeContext::default();

let mut encoder = CacheEncoder {
tcx,
encoder,
Expand All @@ -233,7 +233,7 @@ impl OnDiskCache {
interpret_allocs: Default::default(),
caching_source_map_view: CachingSourceMapView::new(tcx.sess.source_map()),
file_to_file_index,
hygiene_context: &hygiene_encode_context,
hygiene_context: Default::default(),
symbol_index_table: Default::default(),
query_values_index: Default::default(),
side_effects_index: Default::default(),
Expand Down Expand Up @@ -278,7 +278,8 @@ impl OnDiskCache {
// Encode all hygiene data (`SyntaxContextData` and `ExpnData`) from the current
// session.

hygiene_encode_context.encode(
HygieneEncodeContext::encode(
Rc::clone(&encoder.hygiene_context).as_ref(),
&mut encoder,
|encoder, index, ctxt_data| {
let pos = AbsoluteBytePos::new(encoder.position());
Expand Down Expand Up @@ -774,30 +775,30 @@ impl_ref_decoder! {<'tcx>
//- ENCODING -------------------------------------------------------------------

/// An encoder that can write to the incremental compilation cache.
pub struct CacheEncoder<'a, 'tcx> {
pub struct CacheEncoder<'tcx> {
tcx: TyCtxt<'tcx>,
encoder: FileEncoder<'static>,
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
interpret_allocs: FxIndexSet<interpret::AllocId>,
caching_source_map_view: CachingSourceMapView<'tcx>,
file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
hygiene_context: &'a HygieneEncodeContext,
hygiene_context: Rc<RefCell<HygieneEncodeContext>>,
// Used for both `Symbol`s and `ByteSymbol`s.
symbol_index_table: FxHashMap<u32, usize>,

query_values_index: Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>,
side_effects_index: Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>,
}

impl<'a, 'tcx> fmt::Debug for CacheEncoder<'a, 'tcx> {
impl<'tcx> fmt::Debug for CacheEncoder<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Add more details here if/when necessary.
f.write_str("CacheEncoder")
}
}

impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
impl<'tcx> CacheEncoder<'tcx> {
#[inline]
fn source_file_index(&mut self, source_file: Arc<SourceFile>) -> SourceFileIndex {
self.file_to_file_index[&(&raw const *source_file)]
Expand Down Expand Up @@ -866,13 +867,14 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
}
}

impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
impl<'tcx> SpanEncoder for CacheEncoder<'tcx> {
fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_context, self);
let idx = self.hygiene_context.borrow_mut().raw_encode_syntax_context(syntax_context);
idx.encode(self);
}

fn encode_expn_id(&mut self, expn_id: ExpnId) {
self.hygiene_context.schedule_expn_data_for_encoding(expn_id);
self.hygiene_context.borrow_mut().schedule_expn_data_for_encoding(expn_id);
expn_id.expn_hash().encode(self);
}

Expand Down Expand Up @@ -944,7 +946,7 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
}
}

impl<'a, 'tcx> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx> {
impl<'tcx> TyEncoder<'tcx> for CacheEncoder<'tcx> {
const CLEAR_CROSS_CRATE: bool = false;

#[inline]
Expand Down Expand Up @@ -976,7 +978,7 @@ macro_rules! encoder_methods {
}
}

impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
impl<'tcx> Encoder for CacheEncoder<'tcx> {
encoder_methods! {
emit_usize(usize);
emit_u128(u128);
Expand All @@ -999,8 +1001,8 @@ impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
// is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
// and the encoding traits currently work.
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) {
impl<'tcx> Encodable<CacheEncoder<'tcx>> for [u8] {
fn encode(&self, e: &mut CacheEncoder<'tcx>) {
self.encode(&mut e.encoder);
}
}
8 changes: 4 additions & 4 deletions compiler/rustc_query_impl/src/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ fn all_inactive<'tcx, K>(state: &QueryState<'tcx, K>) -> bool {
state.active.lock_shards().all(|shard| shard.is_empty())
}

pub(crate) fn encode_query_values<'tcx>(tcx: TyCtxt<'tcx>, encoder: &mut CacheEncoder<'_, 'tcx>) {
pub(crate) fn encode_query_values<'tcx>(tcx: TyCtxt<'tcx>, encoder: &mut CacheEncoder<'tcx>) {
for_each_query_vtable!(CACHE_ON_DISK, tcx, |query| {
encode_query_values_inner(tcx, query, encoder)
});
}

fn encode_query_values_inner<'a, 'tcx, C, V>(
fn encode_query_values_inner<'tcx, C, V>(
tcx: TyCtxt<'tcx>,
query: &'tcx QueryVTable<'tcx, C>,
encoder: &mut CacheEncoder<'a, 'tcx>,
encoder: &mut CacheEncoder<'tcx>,
) where
C: QueryCache<Value = Erased<V>>,
V: Erasable + Encodable<CacheEncoder<'a, 'tcx>>,
V: Erasable + Encodable<CacheEncoder<'tcx>>,
{
let _timer = tcx.prof.generic_activity_with_arg("encode_query_results_for", query.name);

Expand Down
Loading
Loading