rustc_query_system/dep_graph/
serialized.rs

1//! The data that we will serialize and deserialize.
2//!
3//! Notionally, the dep-graph is a sequence of NodeInfo with the dependencies
4//! specified inline. The total number of nodes and edges are stored as the last
5//! 16 bytes of the file, so we can find them easily at decoding time.
6//!
7//! The serialisation is performed on-demand when each node is emitted. Using this
8//! scheme, we do not need to keep the current graph in memory.
9//!
10//! The deserialization is performed manually, in order to convert from the stored
11//! sequence of NodeInfos to the different arrays in SerializedDepGraph. Since the
12//! node and edge count are stored at the end of the file, all the arrays can be
13//! pre-allocated with the right length.
14//!
15//! The encoding of the dep-graph is generally designed around the fact that fixed-size
16//! reads of encoded data are generally faster than variable-sized reads. Ergo we adopt
17//! essentially the same varint encoding scheme used in the rmeta format; the edge lists
18//! for each node on the graph store a 2-bit integer which is the number of bytes per edge
19//! index in that node's edge list. We effectively ignore that an edge index of 0 could be
20//! encoded with 0 bytes in order to not require 3 bits to store the byte width of the edges.
21//! The overhead of calculating the correct byte width for each edge is mitigated by
22//! building edge lists with [`EdgesVec`] which keeps a running max of the edges in a node.
23//!
24//! When we decode this data, we do not immediately create [`SerializedDepNodeIndex`] and
25//! instead keep the data in its denser serialized form which lets us turn our on-disk size
26//! efficiency directly into a peak memory reduction. When we convert these encoded-in-memory
27//! values into their fully-deserialized type, we use a fixed-size read of the encoded array
28//! then mask off any errant bytes we read. The array of edge index bytes is padded to permit this.
29//!
30//! We also encode and decode the entire rest of each node using [`SerializedNodeHeader`]
31//! to let this encoding and decoding be done in one fixed-size operation. These headers contain
32//! two [`Fingerprint`]s along with the serialized [`DepKind`], and the number of edge indices
33//! in the node and the number of bytes used to encode the edge indices for this node. The
34//! [`DepKind`], number of edges, and bytes per edge are all bit-packed together, if they fit.
35//! If the number of edges in this node does not fit in the bits available in the header, we
36//! store it directly after the header with leb128.
37//!
38//! Dep-graph indices are bulk allocated to threads inside `LocalEncoderState`. Having threads
39//! own these indices helps avoid races when they are conditionally used when marking nodes green.
40//! It also reduces congestion on the shared index count.
41
42use std::cell::RefCell;
43use std::cmp::max;
44use std::marker::PhantomData;
45use std::sync::Arc;
46use std::sync::atomic::Ordering;
47use std::{iter, mem, u64};
48
49use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
50use rustc_data_structures::fx::FxHashMap;
51use rustc_data_structures::outline;
52use rustc_data_structures::profiling::SelfProfilerRef;
53use rustc_data_structures::sync::{AtomicU64, Lock, WorkerLocal, broadcast};
54use rustc_data_structures::unhash::UnhashMap;
55use rustc_index::IndexVec;
56use rustc_serialize::opaque::mem_encoder::MemEncoder;
57use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder};
58use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
59use rustc_session::Session;
60use tracing::{debug, instrument};
61
62use super::graph::{CurrentDepGraph, DepNodeColor, DepNodeColorMap};
63use super::query::DepGraphQuery;
64use super::{DepKind, DepNode, DepNodeIndex, Deps};
65use crate::dep_graph::edges::EdgesVec;
66
67// The maximum value of `SerializedDepNodeIndex` leaves the upper two bits
68// unused so that we can store multiple index types in `CompressedHybridIndex`,
69// and use those bits to encode which index type it contains.
70rustc_index::newtype_index! {
71    #[encodable]
72    #[max = 0x7FFF_FFFF]
73    pub struct SerializedDepNodeIndex {}
74}
75
76const DEP_NODE_SIZE: usize = size_of::<SerializedDepNodeIndex>();
77/// Amount of padding we need to add to the edge list data so that we can retrieve every
78/// SerializedDepNodeIndex with a fixed-size read then mask.
79const DEP_NODE_PAD: usize = DEP_NODE_SIZE - 1;
80/// Number of bits we need to store the number of used bytes in a SerializedDepNodeIndex.
81/// Note that wherever we encode byte widths like this we actually store the number of bytes used
82/// minus 1; for a 4-byte value we technically would have 5 widths to store, but using one byte to
83/// store zeroes (which are relatively rare) is a decent tradeoff to save a bit in our bitfields.
84const DEP_NODE_WIDTH_BITS: usize = DEP_NODE_SIZE / 2;
85
86/// Data for use when recompiling the **current crate**.
87///
88/// There may be unused indices with DEP_KIND_NULL in this graph due to batch allocation of
89/// indices to threads.
90#[derive(Debug, Default)]
91pub struct SerializedDepGraph {
92    /// The set of all DepNodes in the graph
93    nodes: IndexVec<SerializedDepNodeIndex, DepNode>,
94    /// The set of all Fingerprints in the graph. Each Fingerprint corresponds to
95    /// the DepNode at the same index in the nodes vector.
96    fingerprints: IndexVec<SerializedDepNodeIndex, Fingerprint>,
97    /// For each DepNode, stores the list of edges originating from that
98    /// DepNode. Encoded as a [start, end) pair indexing into edge_list_data,
99    /// which holds the actual DepNodeIndices of the target nodes.
100    edge_list_indices: IndexVec<SerializedDepNodeIndex, EdgeHeader>,
101    /// A flattened list of all edge targets in the graph, stored in the same
102    /// varint encoding that we use on disk. Edge sources are implicit in edge_list_indices.
103    edge_list_data: Vec<u8>,
104    /// Stores a map from fingerprints to nodes per dep node kind.
105    /// This is the reciprocal of `nodes`.
106    index: Vec<UnhashMap<PackedFingerprint, SerializedDepNodeIndex>>,
107    /// The number of previous compilation sessions. This is used to generate
108    /// unique anon dep nodes per session.
109    session_count: u64,
110}
111
112impl SerializedDepGraph {
113    #[inline]
114    pub fn edge_targets_from(
115        &self,
116        source: SerializedDepNodeIndex,
117    ) -> impl Iterator<Item = SerializedDepNodeIndex> + Clone {
118        let header = self.edge_list_indices[source];
119        let mut raw = &self.edge_list_data[header.start()..];
120
121        let bytes_per_index = header.bytes_per_index();
122
123        // LLVM doesn't hoist EdgeHeader::mask so we do it ourselves.
124        let mask = header.mask();
125        (0..header.num_edges).map(move |_| {
126            // Doing this slicing in this order ensures that the first bounds check suffices for
127            // all the others.
128            let index = &raw[..DEP_NODE_SIZE];
129            raw = &raw[bytes_per_index..];
130            let index = u32::from_le_bytes(index.try_into().unwrap()) & mask;
131            SerializedDepNodeIndex::from_u32(index)
132        })
133    }
134
135    #[inline]
136    pub fn index_to_node(&self, dep_node_index: SerializedDepNodeIndex) -> DepNode {
137        self.nodes[dep_node_index]
138    }
139
140    #[inline]
141    pub fn node_to_index_opt(&self, dep_node: &DepNode) -> Option<SerializedDepNodeIndex> {
142        self.index.get(dep_node.kind.as_usize())?.get(&dep_node.hash).cloned()
143    }
144
145    #[inline]
146    pub fn fingerprint_by_index(&self, dep_node_index: SerializedDepNodeIndex) -> Fingerprint {
147        self.fingerprints[dep_node_index]
148    }
149
150    #[inline]
151    pub fn node_count(&self) -> usize {
152        self.nodes.len()
153    }
154
155    #[inline]
156    pub fn session_count(&self) -> u64 {
157        self.session_count
158    }
159}
160
161/// A packed representation of an edge's start index and byte width.
162///
163/// This is packed by stealing 2 bits from the start index, which means we only accommodate edge
164/// data arrays up to a quarter of our address space. Which seems fine.
165#[derive(Debug, Clone, Copy)]
166struct EdgeHeader {
167    repr: usize,
168    num_edges: u32,
169}
170
171impl EdgeHeader {
172    #[inline]
173    fn start(self) -> usize {
174        self.repr >> DEP_NODE_WIDTH_BITS
175    }
176
177    #[inline]
178    fn bytes_per_index(self) -> usize {
179        (self.repr & mask(DEP_NODE_WIDTH_BITS)) + 1
180    }
181
182    #[inline]
183    fn mask(self) -> u32 {
184        mask(self.bytes_per_index() * 8) as u32
185    }
186}
187
188#[inline]
189fn mask(bits: usize) -> usize {
190    usize::MAX >> ((size_of::<usize>() * 8) - bits)
191}
192
193impl SerializedDepGraph {
194    #[instrument(level = "debug", skip(d, deps))]
195    pub fn decode<D: Deps>(d: &mut MemDecoder<'_>, deps: &D) -> Arc<SerializedDepGraph> {
196        // The last 16 bytes are the node count and edge count.
197        debug!("position: {:?}", d.position());
198
199        // `node_max` is the number of indices including empty nodes while `node_count`
200        // is the number of actually encoded nodes.
201        let (node_max, node_count, edge_count) =
202            d.with_position(d.len() - 3 * IntEncodedWithFixedSize::ENCODED_SIZE, |d| {
203                debug!("position: {:?}", d.position());
204                let node_max = IntEncodedWithFixedSize::decode(d).0 as usize;
205                let node_count = IntEncodedWithFixedSize::decode(d).0 as usize;
206                let edge_count = IntEncodedWithFixedSize::decode(d).0 as usize;
207                (node_max, node_count, edge_count)
208            });
209        debug!("position: {:?}", d.position());
210
211        debug!(?node_count, ?edge_count);
212
213        let graph_bytes = d.len() - (3 * IntEncodedWithFixedSize::ENCODED_SIZE) - d.position();
214
215        let mut nodes = IndexVec::from_elem_n(
216            DepNode { kind: D::DEP_KIND_NULL, hash: PackedFingerprint::from(Fingerprint::ZERO) },
217            node_max,
218        );
219        let mut fingerprints = IndexVec::from_elem_n(Fingerprint::ZERO, node_max);
220        let mut edge_list_indices =
221            IndexVec::from_elem_n(EdgeHeader { repr: 0, num_edges: 0 }, node_max);
222
223        // This estimation assumes that all of the encoded bytes are for the edge lists or for the
224        // fixed-size node headers. But that's not necessarily true; if any edge list has a length
225        // that spills out of the size we can bit-pack into SerializedNodeHeader then some of the
226        // total serialized size is also used by leb128-encoded edge list lengths. Neglecting that
227        // contribution to graph_bytes means our estimation of the bytes needed for edge_list_data
228        // slightly overshoots. But it cannot overshoot by much; consider that the worse case is
229        // for a node with length 64, which means the spilled 1-byte leb128 length is 1 byte of at
230        // least (34 byte header + 1 byte len + 64 bytes edge data), which is ~1%. A 2-byte leb128
231        // length is about the same fractional overhead and it amortizes for yet greater lengths.
232        let mut edge_list_data =
233            Vec::with_capacity(graph_bytes - node_count * size_of::<SerializedNodeHeader<D>>());
234
235        for _ in 0..node_count {
236            // Decode the header for this edge; the header packs together as many of the fixed-size
237            // fields as possible to limit the number of times we update decoder state.
238            let node_header =
239                SerializedNodeHeader::<D> { bytes: d.read_array(), _marker: PhantomData };
240
241            let index = node_header.index();
242
243            let node = &mut nodes[index];
244            // Make sure there's no duplicate indices in the dep graph.
245            assert!(node_header.node().kind != D::DEP_KIND_NULL && node.kind == D::DEP_KIND_NULL);
246            *node = node_header.node();
247
248            fingerprints[index] = node_header.fingerprint();
249
250            // If the length of this node's edge list is small, the length is stored in the header.
251            // If it is not, we fall back to another decoder call.
252            let num_edges = node_header.len().unwrap_or_else(|| d.read_u32());
253
254            // The edges index list uses the same varint strategy as rmeta tables; we select the
255            // number of byte elements per-array not per-element. This lets us read the whole edge
256            // list for a node with one decoder call and also use the on-disk format in memory.
257            let edges_len_bytes = node_header.bytes_per_index() * (num_edges as usize);
258            // The in-memory structure for the edges list stores the byte width of the edges on
259            // this node with the offset into the global edge data array.
260            let edges_header = node_header.edges_header(&edge_list_data, num_edges);
261
262            edge_list_data.extend(d.read_raw_bytes(edges_len_bytes));
263
264            edge_list_indices[index] = edges_header;
265        }
266
267        // When we access the edge list data, we do a fixed-size read from the edge list data then
268        // mask off the bytes that aren't for that edge index, so the last read may dangle off the
269        // end of the array. This padding ensure it doesn't.
270        edge_list_data.extend(&[0u8; DEP_NODE_PAD]);
271
272        // Read the number of each dep kind and use it to create an hash map with a suitable size.
273        let mut index: Vec<_> = (0..(D::DEP_KIND_MAX + 1))
274            .map(|_| UnhashMap::with_capacity_and_hasher(d.read_u32() as usize, Default::default()))
275            .collect();
276
277        let session_count = d.read_u64();
278
279        for (idx, node) in nodes.iter_enumerated() {
280            if index[node.kind.as_usize()].insert(node.hash, idx).is_some() {
281                // Empty nodes and side effect nodes can have duplicates
282                if node.kind != D::DEP_KIND_NULL && node.kind != D::DEP_KIND_SIDE_EFFECT {
283                    let name = deps.name(node.kind);
284                    panic!(
285                    "Error: A dep graph node ({name}) does not have an unique index. \
286                     Running a clean build on a nightly compiler with `-Z incremental-verify-ich` \
287                     can help narrow down the issue for reporting. A clean build may also work around the issue.\n
288                     DepNode: {node:?}"
289                )
290                }
291            }
292        }
293
294        Arc::new(SerializedDepGraph {
295            nodes,
296            fingerprints,
297            edge_list_indices,
298            edge_list_data,
299            index,
300            session_count,
301        })
302    }
303}
304
305/// A packed representation of all the fixed-size fields in a `NodeInfo`.
306///
307/// This stores in one byte array:
308/// * The `Fingerprint` in the `NodeInfo`
309/// * The `Fingerprint` in `DepNode` that is in this `NodeInfo`
310/// * The `DepKind`'s discriminant (a u16, but not all bits are used...)
311/// * The byte width of the encoded edges for this node
312/// * In whatever bits remain, the length of the edge list for this node, if it fits
313struct SerializedNodeHeader<D> {
314    // 2 bytes for the DepNode
315    // 4 bytes for the index
316    // 16 for Fingerprint in DepNode
317    // 16 for Fingerprint in NodeInfo
318    bytes: [u8; 38],
319    _marker: PhantomData<D>,
320}
321
322// The fields of a `SerializedNodeHeader`, this struct is an implementation detail and exists only
323// to make the implementation of `SerializedNodeHeader` simpler.
324struct Unpacked {
325    len: Option<u32>,
326    bytes_per_index: usize,
327    kind: DepKind,
328    index: SerializedDepNodeIndex,
329    hash: PackedFingerprint,
330    fingerprint: Fingerprint,
331}
332
333// Bit fields, where
334// M: bits used to store the length of a node's edge list
335// N: bits used to store the byte width of elements of the edge list
336// are
337// 0..M    length of the edge
338// M..M+N  bytes per index
339// M+N..16 kind
340impl<D: Deps> SerializedNodeHeader<D> {
341    const TOTAL_BITS: usize = size_of::<DepKind>() * 8;
342    const LEN_BITS: usize = Self::TOTAL_BITS - Self::KIND_BITS - Self::WIDTH_BITS;
343    const WIDTH_BITS: usize = DEP_NODE_WIDTH_BITS;
344    const KIND_BITS: usize = Self::TOTAL_BITS - D::DEP_KIND_MAX.leading_zeros() as usize;
345    const MAX_INLINE_LEN: usize = (u16::MAX as usize >> (Self::TOTAL_BITS - Self::LEN_BITS)) - 1;
346
347    #[inline]
348    fn new(
349        node: DepNode,
350        index: DepNodeIndex,
351        fingerprint: Fingerprint,
352        edge_max_index: u32,
353        edge_count: usize,
354    ) -> Self {
355        debug_assert_eq!(Self::TOTAL_BITS, Self::LEN_BITS + Self::WIDTH_BITS + Self::KIND_BITS);
356
357        let mut head = node.kind.as_inner();
358
359        let free_bytes = edge_max_index.leading_zeros() as usize / 8;
360        let bytes_per_index = (DEP_NODE_SIZE - free_bytes).saturating_sub(1);
361        head |= (bytes_per_index as u16) << Self::KIND_BITS;
362
363        // Encode number of edges + 1 so that we can reserve 0 to indicate that the len doesn't fit
364        // in this bitfield.
365        if edge_count <= Self::MAX_INLINE_LEN {
366            head |= (edge_count as u16 + 1) << (Self::KIND_BITS + Self::WIDTH_BITS);
367        }
368
369        let hash: Fingerprint = node.hash.into();
370
371        // Using half-open ranges ensures an unconditional panic if we get the magic numbers wrong.
372        let mut bytes = [0u8; 38];
373        bytes[..2].copy_from_slice(&head.to_le_bytes());
374        bytes[2..6].copy_from_slice(&index.as_u32().to_le_bytes());
375        bytes[6..22].copy_from_slice(&hash.to_le_bytes());
376        bytes[22..].copy_from_slice(&fingerprint.to_le_bytes());
377
378        #[cfg(debug_assertions)]
379        {
380            let res = Self { bytes, _marker: PhantomData };
381            assert_eq!(fingerprint, res.fingerprint());
382            assert_eq!(node, res.node());
383            if let Some(len) = res.len() {
384                assert_eq!(edge_count, len as usize);
385            }
386        }
387        Self { bytes, _marker: PhantomData }
388    }
389
390    #[inline]
391    fn unpack(&self) -> Unpacked {
392        let head = u16::from_le_bytes(self.bytes[..2].try_into().unwrap());
393        let index = u32::from_le_bytes(self.bytes[2..6].try_into().unwrap());
394        let hash = self.bytes[6..22].try_into().unwrap();
395        let fingerprint = self.bytes[22..].try_into().unwrap();
396
397        let kind = head & mask(Self::KIND_BITS) as u16;
398        let bytes_per_index = (head >> Self::KIND_BITS) & mask(Self::WIDTH_BITS) as u16;
399        let len = (head as u32) >> (Self::WIDTH_BITS + Self::KIND_BITS);
400
401        Unpacked {
402            len: len.checked_sub(1),
403            bytes_per_index: bytes_per_index as usize + 1,
404            kind: DepKind::new(kind),
405            index: SerializedDepNodeIndex::from_u32(index),
406            hash: Fingerprint::from_le_bytes(hash).into(),
407            fingerprint: Fingerprint::from_le_bytes(fingerprint),
408        }
409    }
410
411    #[inline]
412    fn len(&self) -> Option<u32> {
413        self.unpack().len
414    }
415
416    #[inline]
417    fn bytes_per_index(&self) -> usize {
418        self.unpack().bytes_per_index
419    }
420
421    #[inline]
422    fn index(&self) -> SerializedDepNodeIndex {
423        self.unpack().index
424    }
425
426    #[inline]
427    fn fingerprint(&self) -> Fingerprint {
428        self.unpack().fingerprint
429    }
430
431    #[inline]
432    fn node(&self) -> DepNode {
433        let Unpacked { kind, hash, .. } = self.unpack();
434        DepNode { kind, hash }
435    }
436
437    #[inline]
438    fn edges_header(&self, edge_list_data: &[u8], num_edges: u32) -> EdgeHeader {
439        EdgeHeader {
440            repr: (edge_list_data.len() << DEP_NODE_WIDTH_BITS) | (self.bytes_per_index() - 1),
441            num_edges,
442        }
443    }
444}
445
446#[derive(Debug)]
447struct NodeInfo {
448    node: DepNode,
449    fingerprint: Fingerprint,
450    edges: EdgesVec,
451}
452
453impl NodeInfo {
454    fn encode<D: Deps>(&self, e: &mut MemEncoder, index: DepNodeIndex) {
455        let NodeInfo { node, fingerprint, ref edges } = *self;
456        let header = SerializedNodeHeader::<D>::new(
457            node,
458            index,
459            fingerprint,
460            edges.max_index(),
461            edges.len(),
462        );
463        e.write_array(header.bytes);
464
465        if header.len().is_none() {
466            // The edges are all unique and the number of unique indices is less than u32::MAX.
467            e.emit_u32(edges.len().try_into().unwrap());
468        }
469
470        let bytes_per_index = header.bytes_per_index();
471        for node_index in edges.iter() {
472            e.write_with(|dest| {
473                *dest = node_index.as_u32().to_le_bytes();
474                bytes_per_index
475            });
476        }
477    }
478
479    /// Encode a node that was promoted from the previous graph. It reads the edges directly from
480    /// the previous dep graph and expects all edges to already have a new dep node index assigned.
481    /// This avoids the overhead of constructing `EdgesVec`, which would be needed to call `encode`.
482    #[inline]
483    fn encode_promoted<D: Deps>(
484        e: &mut MemEncoder,
485        node: DepNode,
486        index: DepNodeIndex,
487        fingerprint: Fingerprint,
488        prev_index: SerializedDepNodeIndex,
489        colors: &DepNodeColorMap,
490        previous: &SerializedDepGraph,
491    ) -> usize {
492        let edges = previous.edge_targets_from(prev_index);
493        let edge_count = edges.size_hint().0;
494
495        // Find the highest edge in the new dep node indices
496        let edge_max =
497            edges.clone().map(|i| colors.current(i).unwrap().as_u32()).max().unwrap_or(0);
498
499        let header = SerializedNodeHeader::<D>::new(node, index, fingerprint, edge_max, edge_count);
500        e.write_array(header.bytes);
501
502        if header.len().is_none() {
503            // The edges are all unique and the number of unique indices is less than u32::MAX.
504            e.emit_u32(edge_count.try_into().unwrap());
505        }
506
507        let bytes_per_index = header.bytes_per_index();
508        for node_index in edges {
509            let node_index = colors.current(node_index).unwrap();
510            e.write_with(|dest| {
511                *dest = node_index.as_u32().to_le_bytes();
512                bytes_per_index
513            });
514        }
515
516        edge_count
517    }
518}
519
520struct Stat {
521    kind: DepKind,
522    node_counter: u64,
523    edge_counter: u64,
524}
525
526struct LocalEncoderState {
527    next_node_index: u32,
528    remaining_node_index: u32,
529    encoder: MemEncoder,
530    node_count: usize,
531    edge_count: usize,
532
533    /// Stores the number of times we've encoded each dep kind.
534    kind_stats: Vec<u32>,
535}
536
537struct LocalEncoderResult {
538    node_max: u32,
539    node_count: usize,
540    edge_count: usize,
541
542    /// Stores the number of times we've encoded each dep kind.
543    kind_stats: Vec<u32>,
544}
545
546struct EncoderState<D: Deps> {
547    next_node_index: AtomicU64,
548    previous: Arc<SerializedDepGraph>,
549    file: Lock<Option<FileEncoder>>,
550    local: WorkerLocal<RefCell<LocalEncoderState>>,
551    stats: Option<Lock<FxHashMap<DepKind, Stat>>>,
552    marker: PhantomData<D>,
553}
554
555impl<D: Deps> EncoderState<D> {
556    fn new(encoder: FileEncoder, record_stats: bool, previous: Arc<SerializedDepGraph>) -> Self {
557        Self {
558            previous,
559            next_node_index: AtomicU64::new(0),
560            stats: record_stats.then(|| Lock::new(FxHashMap::default())),
561            file: Lock::new(Some(encoder)),
562            local: WorkerLocal::new(|_| {
563                RefCell::new(LocalEncoderState {
564                    next_node_index: 0,
565                    remaining_node_index: 0,
566                    edge_count: 0,
567                    node_count: 0,
568                    encoder: MemEncoder::new(),
569                    kind_stats: iter::repeat(0).take(D::DEP_KIND_MAX as usize + 1).collect(),
570                })
571            }),
572            marker: PhantomData,
573        }
574    }
575
576    #[inline]
577    fn next_index(&self, local: &mut LocalEncoderState) -> DepNodeIndex {
578        if local.remaining_node_index == 0 {
579            const COUNT: u32 = 256;
580
581            // We assume that there won't be enough active threads to overflow `u64` from `u32::MAX` here.
582            // This can exceed u32::MAX by at most `N` * `COUNT` where `N` is the thread pool count since
583            // `try_into().unwrap()` will make threads panic when `self.next_node_index` exceeds u32::MAX.
584            local.next_node_index =
585                self.next_node_index.fetch_add(COUNT as u64, Ordering::Relaxed).try_into().unwrap();
586
587            // Check that we'll stay within `u32`
588            local.next_node_index.checked_add(COUNT).unwrap();
589
590            local.remaining_node_index = COUNT;
591        }
592
593        DepNodeIndex::from_u32(local.next_node_index)
594    }
595
596    /// Marks the index previously returned by `next_index` as used.
597    #[inline]
598    fn bump_index(&self, local: &mut LocalEncoderState) {
599        local.remaining_node_index -= 1;
600        local.next_node_index += 1;
601        local.node_count += 1;
602    }
603
604    #[inline]
605    fn record(
606        &self,
607        node: DepNode,
608        index: DepNodeIndex,
609        edge_count: usize,
610        edges: impl FnOnce(&Self) -> Vec<DepNodeIndex>,
611        record_graph: &Option<Lock<DepGraphQuery>>,
612        local: &mut LocalEncoderState,
613    ) {
614        local.kind_stats[node.kind.as_usize()] += 1;
615        local.edge_count += edge_count;
616
617        if let Some(record_graph) = &record_graph {
618            // Call `edges` before the outlined code to allow the closure to be optimized out.
619            let edges = edges(self);
620
621            // Outline the build of the full dep graph as it's typically disabled and cold.
622            outline(move || {
623                // Do not ICE when a query is called from within `with_query`.
624                if let Some(record_graph) = &mut record_graph.try_lock() {
625                    record_graph.push(index, node, &edges);
626                }
627            });
628        }
629
630        if let Some(stats) = &self.stats {
631            let kind = node.kind;
632
633            // Outline the stats code as it's typically disabled and cold.
634            outline(move || {
635                let mut stats = stats.lock();
636                let stat =
637                    stats.entry(kind).or_insert(Stat { kind, node_counter: 0, edge_counter: 0 });
638                stat.node_counter += 1;
639                stat.edge_counter += edge_count as u64;
640            });
641        }
642    }
643
644    #[inline]
645    fn flush_mem_encoder(&self, local: &mut LocalEncoderState) {
646        let data = &mut local.encoder.data;
647        if data.len() > 64 * 1024 {
648            self.file.lock().as_mut().unwrap().emit_raw_bytes(&data[..]);
649            data.clear();
650        }
651    }
652
653    /// Encodes a node to the current graph.
654    fn encode_node(
655        &self,
656        index: DepNodeIndex,
657        node: &NodeInfo,
658        record_graph: &Option<Lock<DepGraphQuery>>,
659        local: &mut LocalEncoderState,
660    ) {
661        node.encode::<D>(&mut local.encoder, index);
662        self.flush_mem_encoder(&mut *local);
663        self.record(
664            node.node,
665            index,
666            node.edges.len(),
667            |_| node.edges[..].to_vec(),
668            record_graph,
669            &mut *local,
670        );
671    }
672
673    /// Encodes a node that was promoted from the previous graph. It reads the information directly from
674    /// the previous dep graph for performance reasons.
675    ///
676    /// This differs from `encode_node` where you have to explicitly provide the relevant `NodeInfo`.
677    ///
678    /// It expects all edges to already have a new dep node index assigned.
679    #[inline]
680    fn encode_promoted_node(
681        &self,
682        index: DepNodeIndex,
683        prev_index: SerializedDepNodeIndex,
684        record_graph: &Option<Lock<DepGraphQuery>>,
685        colors: &DepNodeColorMap,
686        local: &mut LocalEncoderState,
687    ) {
688        let node = self.previous.index_to_node(prev_index);
689        let fingerprint = self.previous.fingerprint_by_index(prev_index);
690        let edge_count = NodeInfo::encode_promoted::<D>(
691            &mut local.encoder,
692            node,
693            index,
694            fingerprint,
695            prev_index,
696            colors,
697            &self.previous,
698        );
699        self.flush_mem_encoder(&mut *local);
700        self.record(
701            node,
702            index,
703            edge_count,
704            |this| {
705                this.previous
706                    .edge_targets_from(prev_index)
707                    .map(|i| colors.current(i).unwrap())
708                    .collect()
709            },
710            record_graph,
711            &mut *local,
712        );
713    }
714
715    fn finish(&self, profiler: &SelfProfilerRef, current: &CurrentDepGraph<D>) -> FileEncodeResult {
716        // Prevent more indices from being allocated.
717        self.next_node_index.store(u32::MAX as u64 + 1, Ordering::SeqCst);
718
719        let results = broadcast(|_| {
720            let mut local = self.local.borrow_mut();
721
722            // Prevent more indices from being allocated on this thread.
723            local.remaining_node_index = 0;
724
725            let data = mem::replace(&mut local.encoder.data, Vec::new());
726            self.file.lock().as_mut().unwrap().emit_raw_bytes(&data);
727
728            LocalEncoderResult {
729                kind_stats: local.kind_stats.clone(),
730                node_max: local.next_node_index,
731                node_count: local.node_count,
732                edge_count: local.edge_count,
733            }
734        });
735
736        let mut encoder = self.file.lock().take().unwrap();
737
738        let mut kind_stats: Vec<u32> = iter::repeat(0).take(D::DEP_KIND_MAX as usize + 1).collect();
739
740        let mut node_max = 0;
741        let mut node_count = 0;
742        let mut edge_count = 0;
743
744        for result in results {
745            node_max = max(node_max, result.node_max);
746            node_count += result.node_count;
747            edge_count += result.edge_count;
748            for (i, stat) in result.kind_stats.iter().enumerate() {
749                kind_stats[i] += stat;
750            }
751        }
752
753        // Encode the number of each dep kind encountered
754        for count in kind_stats.iter() {
755            count.encode(&mut encoder);
756        }
757
758        self.previous.session_count.checked_add(1).unwrap().encode(&mut encoder);
759
760        debug!(?node_max, ?node_count, ?edge_count);
761        debug!("position: {:?}", encoder.position());
762        IntEncodedWithFixedSize(node_max.try_into().unwrap()).encode(&mut encoder);
763        IntEncodedWithFixedSize(node_count.try_into().unwrap()).encode(&mut encoder);
764        IntEncodedWithFixedSize(edge_count.try_into().unwrap()).encode(&mut encoder);
765        debug!("position: {:?}", encoder.position());
766        // Drop the encoder so that nothing is written after the counts.
767        let result = encoder.finish();
768        if let Ok(position) = result {
769            // FIXME(rylev): we hardcode the dep graph file name so we
770            // don't need a dependency on rustc_incremental just for that.
771            profiler.artifact_size("dep_graph", "dep-graph.bin", position as u64);
772        }
773
774        self.print_incremental_info(current, node_count, edge_count);
775
776        result
777    }
778
779    fn print_incremental_info(
780        &self,
781        current: &CurrentDepGraph<D>,
782        total_node_count: usize,
783        total_edge_count: usize,
784    ) {
785        if let Some(record_stats) = &self.stats {
786            let record_stats = record_stats.lock();
787            let mut stats: Vec<_> = record_stats.values().collect();
788            stats.sort_by_key(|s| -(s.node_counter as i64));
789
790            const SEPARATOR: &str = "[incremental] --------------------------------\
791                                     ----------------------------------------------\
792                                     ------------";
793
794            eprintln!("[incremental]");
795            eprintln!("[incremental] DepGraph Statistics");
796            eprintln!("{SEPARATOR}");
797            eprintln!("[incremental]");
798            eprintln!("[incremental] Total Node Count: {}", total_node_count);
799            eprintln!("[incremental] Total Edge Count: {}", total_edge_count);
800
801            if cfg!(debug_assertions) {
802                let total_read_count = current.total_read_count.load(Ordering::Relaxed);
803                let total_duplicate_read_count =
804                    current.total_duplicate_read_count.load(Ordering::Relaxed);
805                eprintln!("[incremental] Total Edge Reads: {total_read_count}");
806                eprintln!("[incremental] Total Duplicate Edge Reads: {total_duplicate_read_count}");
807            }
808
809            eprintln!("[incremental]");
810            eprintln!(
811                "[incremental]  {:<36}| {:<17}| {:<12}| {:<17}|",
812                "Node Kind", "Node Frequency", "Node Count", "Avg. Edge Count"
813            );
814            eprintln!("{SEPARATOR}");
815
816            for stat in stats {
817                let node_kind_ratio =
818                    (100.0 * (stat.node_counter as f64)) / (total_node_count as f64);
819                let node_kind_avg_edges = (stat.edge_counter as f64) / (stat.node_counter as f64);
820
821                eprintln!(
822                    "[incremental]  {:<36}|{:>16.1}% |{:>12} |{:>17.1} |",
823                    format!("{:?}", stat.kind),
824                    node_kind_ratio,
825                    stat.node_counter,
826                    node_kind_avg_edges,
827                );
828            }
829
830            eprintln!("{SEPARATOR}");
831            eprintln!("[incremental]");
832        }
833    }
834}
835
836pub(crate) struct GraphEncoder<D: Deps> {
837    profiler: SelfProfilerRef,
838    status: EncoderState<D>,
839    record_graph: Option<Lock<DepGraphQuery>>,
840}
841
842impl<D: Deps> GraphEncoder<D> {
843    pub(crate) fn new(
844        sess: &Session,
845        encoder: FileEncoder,
846        prev_node_count: usize,
847        previous: Arc<SerializedDepGraph>,
848    ) -> Self {
849        let record_graph = sess
850            .opts
851            .unstable_opts
852            .query_dep_graph
853            .then(|| Lock::new(DepGraphQuery::new(prev_node_count)));
854        let status = EncoderState::new(encoder, sess.opts.unstable_opts.incremental_info, previous);
855        GraphEncoder { status, record_graph, profiler: sess.prof.clone() }
856    }
857
858    pub(crate) fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
859        if let Some(record_graph) = &self.record_graph {
860            f(&record_graph.lock())
861        }
862    }
863
864    /// Encodes a node that does not exists in the previous graph.
865    pub(crate) fn send_new(
866        &self,
867        node: DepNode,
868        fingerprint: Fingerprint,
869        edges: EdgesVec,
870    ) -> DepNodeIndex {
871        let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
872        let node = NodeInfo { node, fingerprint, edges };
873        let mut local = self.status.local.borrow_mut();
874        let index = self.status.next_index(&mut *local);
875        self.status.bump_index(&mut *local);
876        self.status.encode_node(index, &node, &self.record_graph, &mut *local);
877        index
878    }
879
880    /// Encodes a node that exists in the previous graph, but was re-executed.
881    ///
882    /// This will also ensure the dep node is colored either red or green.
883    pub(crate) fn send_and_color(
884        &self,
885        prev_index: SerializedDepNodeIndex,
886        colors: &DepNodeColorMap,
887        node: DepNode,
888        fingerprint: Fingerprint,
889        edges: EdgesVec,
890        is_green: bool,
891    ) -> DepNodeIndex {
892        let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
893        let node = NodeInfo { node, fingerprint, edges };
894
895        let mut local = self.status.local.borrow_mut();
896
897        let index = self.status.next_index(&mut *local);
898
899        if is_green {
900            // Use `try_mark_green` to avoid racing when `send_promoted` is called concurrently
901            // on the same index.
902            match colors.try_mark_green(prev_index, index) {
903                Ok(()) => (),
904                Err(dep_node_index) => return dep_node_index,
905            }
906        } else {
907            colors.insert(prev_index, DepNodeColor::Red);
908        }
909
910        self.status.bump_index(&mut *local);
911        self.status.encode_node(index, &node, &self.record_graph, &mut *local);
912        index
913    }
914
915    /// Encodes a node that was promoted from the previous graph. It reads the information directly from
916    /// the previous dep graph and expects all edges to already have a new dep node index assigned.
917    ///
918    /// This will also ensure the dep node is marked green.
919    #[inline]
920    pub(crate) fn send_promoted(
921        &self,
922        prev_index: SerializedDepNodeIndex,
923        colors: &DepNodeColorMap,
924    ) -> DepNodeIndex {
925        let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
926
927        let mut local = self.status.local.borrow_mut();
928        let index = self.status.next_index(&mut *local);
929
930        // Use `try_mark_green` to avoid racing when `send_promoted` or `send_and_color`
931        // is called concurrently on the same index.
932        match colors.try_mark_green(prev_index, index) {
933            Ok(()) => {
934                self.status.bump_index(&mut *local);
935                self.status.encode_promoted_node(
936                    index,
937                    prev_index,
938                    &self.record_graph,
939                    colors,
940                    &mut *local,
941                );
942                index
943            }
944            Err(dep_node_index) => dep_node_index,
945        }
946    }
947
948    pub(crate) fn finish(&self, current: &CurrentDepGraph<D>) -> FileEncodeResult {
949        let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph_finish");
950
951        self.status.finish(&self.profiler, current)
952    }
953}