rustc_middle/query/
on_disk_cache.rs

1use std::collections::hash_map::Entry;
2use std::mem;
3use std::sync::Arc;
4
5use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
6use rustc_data_structures::memmap::Mmap;
7use rustc_data_structures::sync::{HashMapExt, Lock, RwLock};
8use rustc_data_structures::unhash::UnhashMap;
9use rustc_data_structures::unord::{UnordMap, UnordSet};
10use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, LocalDefId, StableCrateId};
11use rustc_hir::definitions::DefPathHash;
12use rustc_index::{Idx, IndexVec};
13use rustc_macros::{Decodable, Encodable};
14use rustc_query_system::query::QuerySideEffect;
15use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder};
16use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
17use rustc_session::Session;
18use rustc_span::hygiene::{
19    ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextKey,
20};
21use rustc_span::source_map::Spanned;
22use rustc_span::{
23    BytePos, ByteSymbol, CachingSourceMapView, ExpnData, ExpnHash, Pos, RelativeBytePos,
24    SourceFile, Span, SpanDecoder, SpanEncoder, StableSourceFileId, Symbol,
25};
26
27use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
28use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
29use crate::mir::mono::MonoItem;
30use crate::mir::{self, interpret};
31use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
32use crate::ty::{self, Ty, TyCtxt};
33
34const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
35
36// A normal span encoded with both location information and a `SyntaxContext`
37const TAG_FULL_SPAN: u8 = 0;
38// A partial span with no location information, encoded only with a `SyntaxContext`
39const TAG_PARTIAL_SPAN: u8 = 1;
40const TAG_RELATIVE_SPAN: u8 = 2;
41
42const TAG_SYNTAX_CONTEXT: u8 = 0;
43const TAG_EXPN_DATA: u8 = 1;
44
45// Tags for encoding Symbols and ByteSymbols
46const SYMBOL_STR: u8 = 0;
47const SYMBOL_OFFSET: u8 = 1;
48const SYMBOL_PREDEFINED: u8 = 2;
49
50/// Provides an interface to incremental compilation data cached from the
51/// previous compilation session. This data will eventually include the results
52/// of a few selected queries (like `typeck` and `mir_optimized`) and
53/// any side effects that have been emitted during a query.
54pub struct OnDiskCache {
55    // The complete cache data in serialized form.
56    serialized_data: RwLock<Option<Mmap>>,
57
58    // Collects all `QuerySideEffect` created during the current compilation
59    // session.
60    current_side_effects: Lock<FxIndexMap<DepNodeIndex, QuerySideEffect>>,
61
62    file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
63
64    // Caches that are populated lazily during decoding.
65    file_index_to_file: Lock<FxHashMap<SourceFileIndex, Arc<SourceFile>>>,
66
67    // A map from dep-node to the position of the cached query result in
68    // `serialized_data`.
69    query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
70
71    // A map from dep-node to the position of any associated `QuerySideEffect` in
72    // `serialized_data`.
73    prev_side_effects_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
74
75    alloc_decoding_state: AllocDecodingState,
76
77    // A map from syntax context ids to the position of their associated
78    // `SyntaxContextData`. We use a `u32` instead of a `SyntaxContext`
79    // to represent the fact that we are storing *encoded* ids. When we decode
80    // a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
81    // which will almost certainly be different than the serialized id.
82    syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
83    // A map from the `DefPathHash` of an `ExpnId` to the position
84    // of their associated `ExpnData`. Ideally, we would store a `DefId`,
85    // but we need to decode this before we've constructed a `TyCtxt` (which
86    // makes it difficult to decode a `DefId`).
87
88    // Note that these `DefPathHashes` correspond to both local and foreign
89    // `ExpnData` (e.g `ExpnData.krate` may not be `LOCAL_CRATE`). Alternatively,
90    // we could look up the `ExpnData` from the metadata of foreign crates,
91    // but it seemed easier to have `OnDiskCache` be independent of the `CStore`.
92    expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
93    // Additional information used when decoding hygiene data.
94    hygiene_context: HygieneDecodeContext,
95    // Maps `ExpnHash`es to their raw value from the *previous*
96    // compilation session. This is used as an initial 'guess' when
97    // we try to map an `ExpnHash` to its value in the current
98    // compilation session.
99    foreign_expn_data: UnhashMap<ExpnHash, u32>,
100}
101
102// This type is used only for serialization and deserialization.
103#[derive(Encodable, Decodable)]
104struct Footer {
105    file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
106    query_result_index: EncodedDepNodeIndex,
107    side_effects_index: EncodedDepNodeIndex,
108    // The location of all allocations.
109    // Most uses only need values up to u32::MAX, but benchmarking indicates that we can use a u64
110    // without measurable overhead. This permits larger const allocations without ICEing.
111    interpret_alloc_index: Vec<u64>,
112    // See `OnDiskCache.syntax_contexts`
113    syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
114    // See `OnDiskCache.expn_data`
115    expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
116    foreign_expn_data: UnhashMap<ExpnHash, u32>,
117}
118
119pub type EncodedDepNodeIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
120
121#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
122struct SourceFileIndex(u32);
123
124#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
125pub struct AbsoluteBytePos(u64);
126
127impl AbsoluteBytePos {
128    #[inline]
129    pub fn new(pos: usize) -> AbsoluteBytePos {
130        AbsoluteBytePos(pos.try_into().expect("Incremental cache file size overflowed u64."))
131    }
132
133    #[inline]
134    fn to_usize(self) -> usize {
135        self.0 as usize
136    }
137}
138
139#[derive(Encodable, Decodable, Clone, Debug)]
140struct EncodedSourceFileId {
141    stable_source_file_id: StableSourceFileId,
142    stable_crate_id: StableCrateId,
143}
144
145impl EncodedSourceFileId {
146    #[inline]
147    fn new(tcx: TyCtxt<'_>, file: &SourceFile) -> EncodedSourceFileId {
148        EncodedSourceFileId {
149            stable_source_file_id: file.stable_id,
150            stable_crate_id: tcx.stable_crate_id(file.cnum),
151        }
152    }
153}
154
155impl OnDiskCache {
156    /// Creates a new `OnDiskCache` instance from the serialized data in `data`.
157    ///
158    /// The serialized cache has some basic integrity checks, if those checks indicate that the
159    /// on-disk data is corrupt, an error is returned.
160    pub fn new(sess: &Session, data: Mmap, start_pos: usize) -> Result<Self, ()> {
161        assert!(sess.opts.incremental.is_some());
162
163        let mut decoder = MemDecoder::new(&data, start_pos)?;
164
165        // Decode the *position* of the footer, which can be found in the
166        // last 8 bytes of the file.
167        let footer_pos = decoder
168            .with_position(decoder.len() - IntEncodedWithFixedSize::ENCODED_SIZE, |decoder| {
169                IntEncodedWithFixedSize::decode(decoder).0 as usize
170            });
171        // Decode the file footer, which contains all the lookup tables, etc.
172        let footer: Footer =
173            decoder.with_position(footer_pos, |decoder| decode_tagged(decoder, TAG_FILE_FOOTER));
174
175        Ok(Self {
176            serialized_data: RwLock::new(Some(data)),
177            file_index_to_stable_id: footer.file_index_to_stable_id,
178            file_index_to_file: Default::default(),
179            current_side_effects: Default::default(),
180            query_result_index: footer.query_result_index.into_iter().collect(),
181            prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
182            alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
183            syntax_contexts: footer.syntax_contexts,
184            expn_data: footer.expn_data,
185            foreign_expn_data: footer.foreign_expn_data,
186            hygiene_context: Default::default(),
187        })
188    }
189
190    pub fn new_empty() -> Self {
191        Self {
192            serialized_data: RwLock::new(None),
193            file_index_to_stable_id: Default::default(),
194            file_index_to_file: Default::default(),
195            current_side_effects: Default::default(),
196            query_result_index: Default::default(),
197            prev_side_effects_index: Default::default(),
198            alloc_decoding_state: AllocDecodingState::new(Vec::new()),
199            syntax_contexts: FxHashMap::default(),
200            expn_data: UnhashMap::default(),
201            foreign_expn_data: UnhashMap::default(),
202            hygiene_context: Default::default(),
203        }
204    }
205
206    /// Execute all cache promotions and release the serialized backing Mmap.
207    ///
208    /// Cache promotions require invoking queries, which needs to read the serialized data.
209    /// In order to serialize the new on-disk cache, the former on-disk cache file needs to be
210    /// deleted, hence we won't be able to refer to its memmapped data.
211    pub fn drop_serialized_data(&self, tcx: TyCtxt<'_>) {
212        // Load everything into memory so we can write it out to the on-disk
213        // cache. The vast majority of cacheable query results should already
214        // be in memory, so this should be a cheap operation.
215        // Do this *before* we clone 'latest_foreign_def_path_hashes', since
216        // loading existing queries may cause us to create new DepNodes, which
217        // may in turn end up invoking `store_foreign_def_id_hash`
218        tcx.dep_graph.exec_cache_promotions(tcx);
219
220        *self.serialized_data.write() = None;
221    }
222
223    pub fn serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult {
224        // Serializing the `DepGraph` should not modify it.
225        tcx.dep_graph.with_ignore(|| {
226            // Allocate `SourceFileIndex`es.
227            let (file_to_file_index, file_index_to_stable_id) = {
228                let files = tcx.sess.source_map().files();
229                let mut file_to_file_index =
230                    FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
231                let mut file_index_to_stable_id =
232                    FxHashMap::with_capacity_and_hasher(files.len(), Default::default());
233
234                for (index, file) in files.iter().enumerate() {
235                    let index = SourceFileIndex(index as u32);
236                    let file_ptr: *const SourceFile = &raw const **file;
237                    file_to_file_index.insert(file_ptr, index);
238                    let source_file_id = EncodedSourceFileId::new(tcx, file);
239                    file_index_to_stable_id.insert(index, source_file_id);
240                }
241
242                (file_to_file_index, file_index_to_stable_id)
243            };
244
245            let hygiene_encode_context = HygieneEncodeContext::default();
246
247            let mut encoder = CacheEncoder {
248                tcx,
249                encoder,
250                type_shorthands: Default::default(),
251                predicate_shorthands: Default::default(),
252                interpret_allocs: Default::default(),
253                source_map: CachingSourceMapView::new(tcx.sess.source_map()),
254                file_to_file_index,
255                hygiene_context: &hygiene_encode_context,
256                symbol_index_table: Default::default(),
257            };
258
259            // Encode query results.
260            let mut query_result_index = EncodedDepNodeIndex::new();
261
262            tcx.sess.time("encode_query_results", || {
263                let enc = &mut encoder;
264                let qri = &mut query_result_index;
265                (tcx.query_system.fns.encode_query_results)(tcx, enc, qri);
266            });
267
268            // Encode side effects.
269            let side_effects_index: EncodedDepNodeIndex = self
270                .current_side_effects
271                .borrow()
272                .iter()
273                .map(|(dep_node_index, side_effect)| {
274                    let pos = AbsoluteBytePos::new(encoder.position());
275                    let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
276                    encoder.encode_tagged(dep_node_index, side_effect);
277
278                    (dep_node_index, pos)
279                })
280                .collect();
281
282            let interpret_alloc_index = {
283                let mut interpret_alloc_index = Vec::new();
284                let mut n = 0;
285                loop {
286                    let new_n = encoder.interpret_allocs.len();
287                    // If we have found new IDs, serialize those too.
288                    if n == new_n {
289                        // Otherwise, abort.
290                        break;
291                    }
292                    interpret_alloc_index.reserve(new_n - n);
293                    for idx in n..new_n {
294                        let id = encoder.interpret_allocs[idx];
295                        let pos: u64 = encoder.position().try_into().unwrap();
296                        interpret_alloc_index.push(pos);
297                        interpret::specialized_encode_alloc_id(&mut encoder, tcx, id);
298                    }
299                    n = new_n;
300                }
301                interpret_alloc_index
302            };
303
304            let mut syntax_contexts = FxHashMap::default();
305            let mut expn_data = UnhashMap::default();
306            let mut foreign_expn_data = UnhashMap::default();
307
308            // Encode all hygiene data (`SyntaxContextData` and `ExpnData`) from the current
309            // session.
310
311            hygiene_encode_context.encode(
312                &mut encoder,
313                |encoder, index, ctxt_data| {
314                    let pos = AbsoluteBytePos::new(encoder.position());
315                    encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data);
316                    syntax_contexts.insert(index, pos);
317                },
318                |encoder, expn_id, data, hash| {
319                    if expn_id.krate == LOCAL_CRATE {
320                        let pos = AbsoluteBytePos::new(encoder.position());
321                        encoder.encode_tagged(TAG_EXPN_DATA, data);
322                        expn_data.insert(hash, pos);
323                    } else {
324                        foreign_expn_data.insert(hash, expn_id.local_id.as_u32());
325                    }
326                },
327            );
328
329            // Encode the file footer.
330            let footer_pos = encoder.position() as u64;
331            encoder.encode_tagged(
332                TAG_FILE_FOOTER,
333                &Footer {
334                    file_index_to_stable_id,
335                    query_result_index,
336                    side_effects_index,
337                    interpret_alloc_index,
338                    syntax_contexts,
339                    expn_data,
340                    foreign_expn_data,
341                },
342            );
343
344            // Encode the position of the footer as the last 8 bytes of the
345            // file so we know where to look for it.
346            IntEncodedWithFixedSize(footer_pos).encode(&mut encoder.encoder);
347
348            // DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
349            // of the footer must be the last thing in the data stream.
350
351            encoder.finish()
352        })
353    }
354
355    /// Loads a `QuerySideEffect` created during the previous compilation session.
356    pub fn load_side_effect(
357        &self,
358        tcx: TyCtxt<'_>,
359        dep_node_index: SerializedDepNodeIndex,
360    ) -> Option<QuerySideEffect> {
361        let side_effect: Option<QuerySideEffect> =
362            self.load_indexed(tcx, dep_node_index, &self.prev_side_effects_index);
363        side_effect
364    }
365
366    /// Stores a `QuerySideEffect` emitted during the current compilation session.
367    /// Anything stored like this will be available via `load_side_effect` in
368    /// the next compilation session.
369    pub fn store_side_effect(&self, dep_node_index: DepNodeIndex, side_effect: QuerySideEffect) {
370        let mut current_side_effects = self.current_side_effects.borrow_mut();
371        let prev = current_side_effects.insert(dep_node_index, side_effect);
372        debug_assert!(prev.is_none());
373    }
374
375    /// Return whether the cached query result can be decoded.
376    #[inline]
377    pub fn loadable_from_disk(&self, dep_node_index: SerializedDepNodeIndex) -> bool {
378        self.query_result_index.contains_key(&dep_node_index)
379        // with_decoder is infallible, so we can stop here
380    }
381
382    /// Returns the cached query result if there is something in the cache for
383    /// the given `SerializedDepNodeIndex`; otherwise returns `None`.
384    pub fn try_load_query_result<'tcx, T>(
385        &self,
386        tcx: TyCtxt<'tcx>,
387        dep_node_index: SerializedDepNodeIndex,
388    ) -> Option<T>
389    where
390        T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
391    {
392        let opt_value = self.load_indexed(tcx, dep_node_index, &self.query_result_index);
393        debug_assert_eq!(opt_value.is_some(), self.loadable_from_disk(dep_node_index));
394        opt_value
395    }
396
397    fn load_indexed<'tcx, T>(
398        &self,
399        tcx: TyCtxt<'tcx>,
400        dep_node_index: SerializedDepNodeIndex,
401        index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
402    ) -> Option<T>
403    where
404        T: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
405    {
406        let pos = index.get(&dep_node_index).cloned()?;
407        let value = self.with_decoder(tcx, pos, |decoder| decode_tagged(decoder, dep_node_index));
408        Some(value)
409    }
410
411    fn with_decoder<'a, 'tcx, T, F: for<'s> FnOnce(&mut CacheDecoder<'s, 'tcx>) -> T>(
412        &self,
413        tcx: TyCtxt<'tcx>,
414        pos: AbsoluteBytePos,
415        f: F,
416    ) -> T
417    where
418        T: Decodable<CacheDecoder<'a, 'tcx>>,
419    {
420        let serialized_data = self.serialized_data.read();
421        let mut decoder = CacheDecoder {
422            tcx,
423            opaque: MemDecoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize())
424                .unwrap(),
425            file_index_to_file: &self.file_index_to_file,
426            file_index_to_stable_id: &self.file_index_to_stable_id,
427            alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
428            syntax_contexts: &self.syntax_contexts,
429            expn_data: &self.expn_data,
430            foreign_expn_data: &self.foreign_expn_data,
431            hygiene_context: &self.hygiene_context,
432        };
433        f(&mut decoder)
434    }
435}
436
437//- DECODING -------------------------------------------------------------------
438
439/// A decoder that can read from the incremental compilation cache. It is similar to the one
440/// we use for crate metadata decoding in that it can rebase spans and eventually
441/// will also handle things that contain `Ty` instances.
442pub struct CacheDecoder<'a, 'tcx> {
443    tcx: TyCtxt<'tcx>,
444    opaque: MemDecoder<'a>,
445    file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Arc<SourceFile>>>,
446    file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
447    alloc_decoding_session: AllocDecodingSession<'a>,
448    syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
449    expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
450    foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
451    hygiene_context: &'a HygieneDecodeContext,
452}
453
454impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
455    #[inline]
456    fn file_index_to_file(&self, index: SourceFileIndex) -> Arc<SourceFile> {
457        let CacheDecoder { tcx, file_index_to_file, file_index_to_stable_id, .. } = *self;
458
459        Arc::clone(file_index_to_file.borrow_mut().entry(index).or_insert_with(|| {
460            let source_file_id = &file_index_to_stable_id[&index];
461            let source_file_cnum = tcx.stable_crate_id_to_crate_num(source_file_id.stable_crate_id);
462
463            // If this `SourceFile` is from a foreign crate, then make sure
464            // that we've imported all of the source files from that crate.
465            // This has usually already been done during macro invocation.
466            // However, when encoding query results like `TypeckResults`,
467            // we might encode an `AdtDef` for a foreign type (because it
468            // was referenced in the body of the function). There is no guarantee
469            // that we will load the source files from that crate during macro
470            // expansion, so we use `import_source_files` to ensure that the foreign
471            // source files are actually imported before we call `source_file_by_stable_id`.
472            if source_file_cnum != LOCAL_CRATE {
473                self.tcx.import_source_files(source_file_cnum);
474            }
475
476            tcx.sess
477                .source_map()
478                .source_file_by_stable_id(source_file_id.stable_source_file_id)
479                .expect("failed to lookup `SourceFile` in new context")
480        }))
481    }
482
483    // copy&paste impl from rustc_metadata
484    #[inline]
485    fn decode_symbol_or_byte_symbol<S>(
486        &mut self,
487        new_from_index: impl Fn(u32) -> S,
488        read_and_intern_str_or_byte_str_this: impl Fn(&mut Self) -> S,
489        read_and_intern_str_or_byte_str_opaque: impl Fn(&mut MemDecoder<'a>) -> S,
490    ) -> S {
491        let tag = self.read_u8();
492
493        match tag {
494            SYMBOL_STR => read_and_intern_str_or_byte_str_this(self),
495            SYMBOL_OFFSET => {
496                // read str offset
497                let pos = self.read_usize();
498
499                // move to str offset and read
500                self.opaque.with_position(pos, |d| read_and_intern_str_or_byte_str_opaque(d))
501            }
502            SYMBOL_PREDEFINED => new_from_index(self.read_u32()),
503            _ => unreachable!(),
504        }
505    }
506}
507
508// Decodes something that was encoded with `encode_tagged()` and verify that the
509// tag matches and the correct amount of bytes was read.
510fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> V
511where
512    T: Decodable<D> + Eq + std::fmt::Debug,
513    V: Decodable<D>,
514    D: Decoder,
515{
516    let start_pos = decoder.position();
517
518    let actual_tag = T::decode(decoder);
519    assert_eq!(actual_tag, expected_tag);
520    let value = V::decode(decoder);
521    let end_pos = decoder.position();
522
523    let expected_len: u64 = Decodable::decode(decoder);
524    assert_eq!((end_pos - start_pos) as u64, expected_len);
525
526    value
527}
528
529impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
530    const CLEAR_CROSS_CRATE: bool = false;
531
532    #[inline]
533    fn interner(&self) -> TyCtxt<'tcx> {
534        self.tcx
535    }
536
537    fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
538    where
539        F: FnOnce(&mut Self) -> Ty<'tcx>,
540    {
541        let tcx = self.tcx;
542
543        let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand };
544
545        if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) {
546            return ty;
547        }
548
549        let ty = or_insert_with(self);
550        // This may overwrite the entry, but it should overwrite with the same value.
551        tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty);
552        ty
553    }
554
555    fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
556    where
557        F: FnOnce(&mut Self) -> R,
558    {
559        debug_assert!(pos < self.opaque.len());
560
561        let new_opaque = self.opaque.split_at(pos);
562        let old_opaque = mem::replace(&mut self.opaque, new_opaque);
563        let r = f(self);
564        self.opaque = old_opaque;
565        r
566    }
567
568    fn decode_alloc_id(&mut self) -> interpret::AllocId {
569        let alloc_decoding_session = self.alloc_decoding_session;
570        alloc_decoding_session.decode_alloc_id(self)
571    }
572}
573
574crate::implement_ty_decoder!(CacheDecoder<'a, 'tcx>);
575
576// This ensures that the `Decodable<opaque::Decoder>::decode` specialization for `Vec<u8>` is used
577// when a `CacheDecoder` is passed to `Decodable::decode`. Unfortunately, we have to manually opt
578// into specializations this way, given how `CacheDecoder` and the decoding traits currently work.
579impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
580    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
581        Decodable::decode(&mut d.opaque)
582    }
583}
584
585impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
586    fn decode_syntax_context(&mut self) -> SyntaxContext {
587        let syntax_contexts = self.syntax_contexts;
588        rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| {
589            // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
590            // We look up the position of the associated `SyntaxData` and decode it.
591            let pos = syntax_contexts.get(&id).unwrap();
592            this.with_position(pos.to_usize(), |decoder| {
593                let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
594                data
595            })
596        })
597    }
598
599    fn decode_expn_id(&mut self) -> ExpnId {
600        let hash = ExpnHash::decode(self);
601        if hash.is_root() {
602            return ExpnId::root();
603        }
604
605        if let Some(expn_id) = ExpnId::from_hash(hash) {
606            return expn_id;
607        }
608
609        let krate = self.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id());
610
611        let expn_id = if krate == LOCAL_CRATE {
612            // We look up the position of the associated `ExpnData` and decode it.
613            let pos = self
614                .expn_data
615                .get(&hash)
616                .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, self.expn_data));
617
618            let data: ExpnData =
619                self.with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA));
620            let expn_id = rustc_span::hygiene::register_local_expn_id(data, hash);
621
622            #[cfg(debug_assertions)]
623            {
624                use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
625                let local_hash = self.tcx.with_stable_hashing_context(|mut hcx| {
626                    let mut hasher = StableHasher::new();
627                    expn_id.expn_data().hash_stable(&mut hcx, &mut hasher);
628                    hasher.finish()
629                });
630                debug_assert_eq!(hash.local_hash(), local_hash);
631            }
632
633            expn_id
634        } else {
635            let index_guess = self.foreign_expn_data[&hash];
636            self.tcx.expn_hash_to_expn_id(krate, index_guess, hash)
637        };
638
639        debug_assert_eq!(expn_id.krate, krate);
640        expn_id
641    }
642
643    fn decode_span(&mut self) -> Span {
644        let ctxt = SyntaxContext::decode(self);
645        let parent = Option::<LocalDefId>::decode(self);
646        let tag: u8 = Decodable::decode(self);
647
648        if tag == TAG_PARTIAL_SPAN {
649            return Span::new(BytePos(0), BytePos(0), ctxt, parent);
650        } else if tag == TAG_RELATIVE_SPAN {
651            let dlo = u32::decode(self);
652            let dto = u32::decode(self);
653
654            let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
655            let span = Span::new(
656                enclosing.lo + BytePos::from_u32(dlo),
657                enclosing.lo + BytePos::from_u32(dto),
658                ctxt,
659                parent,
660            );
661
662            return span;
663        } else {
664            debug_assert_eq!(tag, TAG_FULL_SPAN);
665        }
666
667        let file_lo_index = SourceFileIndex::decode(self);
668        let line_lo = usize::decode(self);
669        let col_lo = RelativeBytePos::decode(self);
670        let len = BytePos::decode(self);
671
672        let file_lo = self.file_index_to_file(file_lo_index);
673        let lo = file_lo.lines()[line_lo - 1] + col_lo;
674        let lo = file_lo.absolute_position(lo);
675        let hi = lo + len;
676
677        Span::new(lo, hi, ctxt, parent)
678    }
679
680    fn decode_symbol(&mut self) -> Symbol {
681        self.decode_symbol_or_byte_symbol(
682            Symbol::new,
683            |this| Symbol::intern(this.read_str()),
684            |opaque| Symbol::intern(opaque.read_str()),
685        )
686    }
687
688    fn decode_byte_symbol(&mut self) -> ByteSymbol {
689        self.decode_symbol_or_byte_symbol(
690            ByteSymbol::new,
691            |this| ByteSymbol::intern(this.read_byte_str()),
692            |opaque| ByteSymbol::intern(opaque.read_byte_str()),
693        )
694    }
695
696    fn decode_crate_num(&mut self) -> CrateNum {
697        let stable_id = StableCrateId::decode(self);
698        let cnum = self.tcx.stable_crate_id_to_crate_num(stable_id);
699        cnum
700    }
701
702    // This impl makes sure that we get a runtime error when we try decode a
703    // `DefIndex` that is not contained in a `DefId`. Such a case would be problematic
704    // because we would not know how to transform the `DefIndex` to the current
705    // context.
706    fn decode_def_index(&mut self) -> DefIndex {
707        panic!("trying to decode `DefIndex` outside the context of a `DefId`")
708    }
709
710    // Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
711    // compilation sessions. We use the `DefPathHash`, which is stable across
712    // sessions, to map the old `DefId` to the new one.
713    fn decode_def_id(&mut self) -> DefId {
714        // Load the `DefPathHash` which is was we encoded the `DefId` as.
715        let def_path_hash = DefPathHash::decode(self);
716
717        // Using the `DefPathHash`, we can lookup the new `DefId`.
718        // Subtle: We only encode a `DefId` as part of a query result.
719        // If we get to this point, then all of the query inputs were green,
720        // which means that the definition with this hash is guaranteed to
721        // still exist in the current compilation session.
722        match self.tcx.def_path_hash_to_def_id(def_path_hash) {
723            Some(r) => r,
724            None => panic!("Failed to convert DefPathHash {def_path_hash:?}"),
725        }
726    }
727
728    fn decode_attr_id(&mut self) -> rustc_span::AttrId {
729        panic!("cannot decode `AttrId` with `CacheDecoder`");
730    }
731}
732
733impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx UnordSet<LocalDefId> {
734    #[inline]
735    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
736        RefDecodable::decode(d)
737    }
738}
739
740impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
741    for &'tcx UnordMap<DefId, ty::EarlyBinder<'tcx, Ty<'tcx>>>
742{
743    #[inline]
744    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
745        RefDecodable::decode(d)
746    }
747}
748
749impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
750    for &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>>
751{
752    #[inline]
753    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
754        RefDecodable::decode(d)
755    }
756}
757
758impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
759    #[inline]
760    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
761        RefDecodable::decode(d)
762    }
763}
764
765impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
766    #[inline]
767    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
768        RefDecodable::decode(d)
769    }
770}
771
772impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Spanned<MonoItem<'tcx>>] {
773    #[inline]
774    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
775        RefDecodable::decode(d)
776    }
777}
778
779impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
780    for &'tcx crate::traits::specialization_graph::Graph
781{
782    #[inline]
783    fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
784        RefDecodable::decode(d)
785    }
786}
787
788macro_rules! impl_ref_decoder {
789    (<$tcx:tt> $($ty:ty,)*) => {
790        $(impl<'a, $tcx> Decodable<CacheDecoder<'a, $tcx>> for &$tcx [$ty] {
791            #[inline]
792            fn decode(d: &mut CacheDecoder<'a, $tcx>) -> Self {
793                RefDecodable::decode(d)
794            }
795        })*
796    };
797}
798
799impl_ref_decoder! {<'tcx>
800    Span,
801    rustc_hir::Attribute,
802    rustc_span::Ident,
803    ty::Variance,
804    rustc_span::def_id::DefId,
805    rustc_span::def_id::LocalDefId,
806    (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
807    ty::DeducedParamAttrs,
808}
809
810//- ENCODING -------------------------------------------------------------------
811
812/// An encoder that can write to the incremental compilation cache.
813pub struct CacheEncoder<'a, 'tcx> {
814    tcx: TyCtxt<'tcx>,
815    encoder: FileEncoder,
816    type_shorthands: FxHashMap<Ty<'tcx>, usize>,
817    predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
818    interpret_allocs: FxIndexSet<interpret::AllocId>,
819    source_map: CachingSourceMapView<'tcx>,
820    file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
821    hygiene_context: &'a HygieneEncodeContext,
822    // Used for both `Symbol`s and `ByteSymbol`s.
823    symbol_index_table: FxHashMap<u32, usize>,
824}
825
826impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
827    #[inline]
828    fn source_file_index(&mut self, source_file: Arc<SourceFile>) -> SourceFileIndex {
829        self.file_to_file_index[&(&raw const *source_file)]
830    }
831
832    /// Encode something with additional information that allows to do some
833    /// sanity checks when decoding the data again. This method will first
834    /// encode the specified tag, then the given value, then the number of
835    /// bytes taken up by tag and value. On decoding, we can then verify that
836    /// we get the expected tag and read the expected number of bytes.
837    pub fn encode_tagged<T: Encodable<Self>, V: Encodable<Self>>(&mut self, tag: T, value: &V) {
838        let start_pos = self.position();
839
840        tag.encode(self);
841        value.encode(self);
842
843        let end_pos = self.position();
844        ((end_pos - start_pos) as u64).encode(self);
845    }
846
847    // copy&paste impl from rustc_metadata
848    fn encode_symbol_or_byte_symbol(
849        &mut self,
850        index: u32,
851        emit_str_or_byte_str: impl Fn(&mut Self),
852    ) {
853        // if symbol/byte symbol is predefined, emit tag and symbol index
854        if Symbol::is_predefined(index) {
855            self.encoder.emit_u8(SYMBOL_PREDEFINED);
856            self.encoder.emit_u32(index);
857        } else {
858            // otherwise write it as string or as offset to it
859            match self.symbol_index_table.entry(index) {
860                Entry::Vacant(o) => {
861                    self.encoder.emit_u8(SYMBOL_STR);
862                    let pos = self.encoder.position();
863                    o.insert(pos);
864                    emit_str_or_byte_str(self);
865                }
866                Entry::Occupied(o) => {
867                    let x = *o.get();
868                    self.emit_u8(SYMBOL_OFFSET);
869                    self.emit_usize(x);
870                }
871            }
872        }
873    }
874
875    #[inline]
876    fn finish(mut self) -> FileEncodeResult {
877        self.encoder.finish()
878    }
879}
880
881impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
882    fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
883        rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_context, self);
884    }
885
886    fn encode_expn_id(&mut self, expn_id: ExpnId) {
887        self.hygiene_context.schedule_expn_data_for_encoding(expn_id);
888        expn_id.expn_hash().encode(self);
889    }
890
891    fn encode_span(&mut self, span: Span) {
892        let span_data = span.data_untracked();
893        span_data.ctxt.encode(self);
894        span_data.parent.encode(self);
895
896        if span_data.is_dummy() {
897            return TAG_PARTIAL_SPAN.encode(self);
898        }
899
900        if let Some(parent) = span_data.parent {
901            let enclosing = self.tcx.source_span_untracked(parent).data_untracked();
902            if enclosing.contains(span_data) {
903                TAG_RELATIVE_SPAN.encode(self);
904                (span_data.lo - enclosing.lo).to_u32().encode(self);
905                (span_data.hi - enclosing.lo).to_u32().encode(self);
906                return;
907            }
908        }
909
910        let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo);
911        let partial_span = match &pos {
912            Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
913            None => true,
914        };
915
916        if partial_span {
917            return TAG_PARTIAL_SPAN.encode(self);
918        }
919
920        let (file_lo, line_lo, col_lo) = pos.unwrap();
921
922        let len = span_data.hi - span_data.lo;
923
924        let source_file_index = self.source_file_index(file_lo);
925
926        TAG_FULL_SPAN.encode(self);
927        source_file_index.encode(self);
928        line_lo.encode(self);
929        col_lo.encode(self);
930        len.encode(self);
931    }
932
933    fn encode_symbol(&mut self, sym: Symbol) {
934        self.encode_symbol_or_byte_symbol(sym.as_u32(), |this| this.emit_str(sym.as_str()));
935    }
936
937    fn encode_byte_symbol(&mut self, byte_sym: ByteSymbol) {
938        self.encode_symbol_or_byte_symbol(byte_sym.as_u32(), |this| {
939            this.emit_byte_str(byte_sym.as_byte_str())
940        });
941    }
942
943    fn encode_crate_num(&mut self, crate_num: CrateNum) {
944        self.tcx.stable_crate_id(crate_num).encode(self);
945    }
946
947    fn encode_def_id(&mut self, def_id: DefId) {
948        self.tcx.def_path_hash(def_id).encode(self);
949    }
950
951    fn encode_def_index(&mut self, _def_index: DefIndex) {
952        bug!("encoding `DefIndex` without context");
953    }
954}
955
956impl<'a, 'tcx> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx> {
957    const CLEAR_CROSS_CRATE: bool = false;
958
959    #[inline]
960    fn position(&self) -> usize {
961        self.encoder.position()
962    }
963    #[inline]
964    fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
965        &mut self.type_shorthands
966    }
967    #[inline]
968    fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
969        &mut self.predicate_shorthands
970    }
971    #[inline]
972    fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) {
973        let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
974
975        index.encode(self);
976    }
977}
978
979macro_rules! encoder_methods {
980    ($($name:ident($ty:ty);)*) => {
981        #[inline]
982        $(fn $name(&mut self, value: $ty) {
983            self.encoder.$name(value)
984        })*
985    }
986}
987
988impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
989    encoder_methods! {
990        emit_usize(usize);
991        emit_u128(u128);
992        emit_u64(u64);
993        emit_u32(u32);
994        emit_u16(u16);
995        emit_u8(u8);
996
997        emit_isize(isize);
998        emit_i128(i128);
999        emit_i64(i64);
1000        emit_i32(i32);
1001        emit_i16(i16);
1002
1003        emit_raw_bytes(&[u8]);
1004    }
1005}
1006
1007// This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
1008// is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
1009// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
1010// and the encoding traits currently work.
1011impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
1012    fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) {
1013        self.encode(&mut e.encoder);
1014    }
1015}