rustc_query_system/dep_graph/
serialized.rs1use std::iter;
39use std::marker::PhantomData;
40use std::sync::Arc;
41
42use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
43use rustc_data_structures::fx::FxHashMap;
44use rustc_data_structures::outline;
45use rustc_data_structures::profiling::SelfProfilerRef;
46use rustc_data_structures::sync::Lock;
47use rustc_data_structures::unhash::UnhashMap;
48use rustc_index::{Idx, IndexVec};
49use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder};
50use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
51use tracing::{debug, instrument};
52
53use super::query::DepGraphQuery;
54use super::{DepKind, DepNode, DepNodeIndex, Deps};
55use crate::dep_graph::edges::EdgesVec;
56
57rustc_index::newtype_index! {
61 #[encodable]
62 #[max = 0x7FFF_FFFF]
63 pub struct SerializedDepNodeIndex {}
64}
65
66const DEP_NODE_SIZE: usize = size_of::<SerializedDepNodeIndex>();
67const DEP_NODE_PAD: usize = DEP_NODE_SIZE - 1;
70const DEP_NODE_WIDTH_BITS: usize = DEP_NODE_SIZE / 2;
75
76#[derive(Debug, Default)]
78pub struct SerializedDepGraph {
79 nodes: IndexVec<SerializedDepNodeIndex, DepNode>,
81 fingerprints: IndexVec<SerializedDepNodeIndex, Fingerprint>,
84 edge_list_indices: IndexVec<SerializedDepNodeIndex, EdgeHeader>,
88 edge_list_data: Vec<u8>,
91 index: Vec<UnhashMap<PackedFingerprint, SerializedDepNodeIndex>>,
94}
95
96impl SerializedDepGraph {
97 #[inline]
98 pub fn edge_targets_from(
99 &self,
100 source: SerializedDepNodeIndex,
101 ) -> impl Iterator<Item = SerializedDepNodeIndex> + Clone {
102 let header = self.edge_list_indices[source];
103 let mut raw = &self.edge_list_data[header.start()..];
104 let end = self
107 .edge_list_indices
108 .get(source + 1)
109 .map(|h| h.start())
110 .unwrap_or_else(|| self.edge_list_data.len() - DEP_NODE_PAD);
111
112 let bytes_per_index = header.bytes_per_index();
115 let len = (end - header.start()) / bytes_per_index;
116
117 let mask = header.mask();
119 (0..len).map(move |_| {
120 let index = &raw[..DEP_NODE_SIZE];
123 raw = &raw[bytes_per_index..];
124 let index = u32::from_le_bytes(index.try_into().unwrap()) & mask;
125 SerializedDepNodeIndex::from_u32(index)
126 })
127 }
128
129 #[inline]
130 pub fn index_to_node(&self, dep_node_index: SerializedDepNodeIndex) -> DepNode {
131 self.nodes[dep_node_index]
132 }
133
134 #[inline]
135 pub fn node_to_index_opt(&self, dep_node: &DepNode) -> Option<SerializedDepNodeIndex> {
136 self.index.get(dep_node.kind.as_usize())?.get(&dep_node.hash).cloned()
137 }
138
139 #[inline]
140 pub fn fingerprint_by_index(&self, dep_node_index: SerializedDepNodeIndex) -> Fingerprint {
141 self.fingerprints[dep_node_index]
142 }
143
144 #[inline]
145 pub fn node_count(&self) -> usize {
146 self.nodes.len()
147 }
148}
149
150#[derive(Debug, Clone, Copy)]
155struct EdgeHeader {
156 repr: usize,
157}
158
159impl EdgeHeader {
160 #[inline]
161 fn start(self) -> usize {
162 self.repr >> DEP_NODE_WIDTH_BITS
163 }
164
165 #[inline]
166 fn bytes_per_index(self) -> usize {
167 (self.repr & mask(DEP_NODE_WIDTH_BITS)) + 1
168 }
169
170 #[inline]
171 fn mask(self) -> u32 {
172 mask(self.bytes_per_index() * 8) as u32
173 }
174}
175
176#[inline]
177fn mask(bits: usize) -> usize {
178 usize::MAX >> ((size_of::<usize>() * 8) - bits)
179}
180
181impl SerializedDepGraph {
182 #[instrument(level = "debug", skip(d, deps))]
183 pub fn decode<D: Deps>(d: &mut MemDecoder<'_>, deps: &D) -> Arc<SerializedDepGraph> {
184 debug!("position: {:?}", d.position());
186 let (node_count, edge_count) =
187 d.with_position(d.len() - 2 * IntEncodedWithFixedSize::ENCODED_SIZE, |d| {
188 debug!("position: {:?}", d.position());
189 let node_count = IntEncodedWithFixedSize::decode(d).0 as usize;
190 let edge_count = IntEncodedWithFixedSize::decode(d).0 as usize;
191 (node_count, edge_count)
192 });
193 debug!("position: {:?}", d.position());
194
195 debug!(?node_count, ?edge_count);
196
197 let graph_bytes = d.len() - (2 * IntEncodedWithFixedSize::ENCODED_SIZE) - d.position();
198
199 let mut nodes = IndexVec::with_capacity(node_count);
200 let mut fingerprints = IndexVec::with_capacity(node_count);
201 let mut edge_list_indices = IndexVec::with_capacity(node_count);
202 let mut edge_list_data =
212 Vec::with_capacity(graph_bytes - node_count * size_of::<SerializedNodeHeader<D>>());
213
214 for _index in 0..node_count {
215 let node_header =
218 SerializedNodeHeader::<D> { bytes: d.read_array(), _marker: PhantomData };
219
220 let _i: SerializedDepNodeIndex = nodes.push(node_header.node());
221 debug_assert_eq!(_i.index(), _index);
222
223 let _i: SerializedDepNodeIndex = fingerprints.push(node_header.fingerprint());
224 debug_assert_eq!(_i.index(), _index);
225
226 let num_edges = node_header.len().unwrap_or_else(|| d.read_usize());
229
230 let edges_len_bytes = node_header.bytes_per_index() * num_edges;
234 let edges_header = node_header.edges_header(&edge_list_data);
237
238 edge_list_data.extend(d.read_raw_bytes(edges_len_bytes));
239
240 let _i: SerializedDepNodeIndex = edge_list_indices.push(edges_header);
241 debug_assert_eq!(_i.index(), _index);
242 }
243
244 edge_list_data.extend(&[0u8; DEP_NODE_PAD]);
248
249 let mut index: Vec<_> = (0..(D::DEP_KIND_MAX + 1))
251 .map(|_| UnhashMap::with_capacity_and_hasher(d.read_u32() as usize, Default::default()))
252 .collect();
253
254 for (idx, node) in nodes.iter_enumerated() {
255 if index[node.kind.as_usize()].insert(node.hash, idx).is_some() {
256 if node.kind != D::DEP_KIND_SIDE_EFFECT {
258 let name = deps.name(node.kind);
259 panic!(
260 "Error: A dep graph node ({name}) does not have an unique index. \
261 Running a clean build on a nightly compiler with `-Z incremental-verify-ich` \
262 can help narrow down the issue for reporting. A clean build may also work around the issue.\n
263 DepNode: {node:?}"
264 )
265 }
266 }
267 }
268
269 Arc::new(SerializedDepGraph {
270 nodes,
271 fingerprints,
272 edge_list_indices,
273 edge_list_data,
274 index,
275 })
276 }
277}
278
279struct SerializedNodeHeader<D> {
288 bytes: [u8; 34],
292 _marker: PhantomData<D>,
293}
294
295struct Unpacked {
298 len: Option<usize>,
299 bytes_per_index: usize,
300 kind: DepKind,
301 hash: PackedFingerprint,
302 fingerprint: Fingerprint,
303}
304
305impl<D: Deps> SerializedNodeHeader<D> {
313 const TOTAL_BITS: usize = size_of::<DepKind>() * 8;
314 const LEN_BITS: usize = Self::TOTAL_BITS - Self::KIND_BITS - Self::WIDTH_BITS;
315 const WIDTH_BITS: usize = DEP_NODE_WIDTH_BITS;
316 const KIND_BITS: usize = Self::TOTAL_BITS - D::DEP_KIND_MAX.leading_zeros() as usize;
317 const MAX_INLINE_LEN: usize = (u16::MAX as usize >> (Self::TOTAL_BITS - Self::LEN_BITS)) - 1;
318
319 #[inline]
320 fn new(
321 node: DepNode,
322 fingerprint: Fingerprint,
323 edge_max_index: u32,
324 edge_count: usize,
325 ) -> Self {
326 debug_assert_eq!(Self::TOTAL_BITS, Self::LEN_BITS + Self::WIDTH_BITS + Self::KIND_BITS);
327
328 let mut head = node.kind.as_inner();
329
330 let free_bytes = edge_max_index.leading_zeros() as usize / 8;
331 let bytes_per_index = (DEP_NODE_SIZE - free_bytes).saturating_sub(1);
332 head |= (bytes_per_index as u16) << Self::KIND_BITS;
333
334 if edge_count <= Self::MAX_INLINE_LEN {
337 head |= (edge_count as u16 + 1) << (Self::KIND_BITS + Self::WIDTH_BITS);
338 }
339
340 let hash: Fingerprint = node.hash.into();
341
342 let mut bytes = [0u8; 34];
344 bytes[..2].copy_from_slice(&head.to_le_bytes());
345 bytes[2..18].copy_from_slice(&hash.to_le_bytes());
346 bytes[18..].copy_from_slice(&fingerprint.to_le_bytes());
347
348 #[cfg(debug_assertions)]
349 {
350 let res = Self { bytes, _marker: PhantomData };
351 assert_eq!(fingerprint, res.fingerprint());
352 assert_eq!(node, res.node());
353 if let Some(len) = res.len() {
354 assert_eq!(edge_count, len);
355 }
356 }
357 Self { bytes, _marker: PhantomData }
358 }
359
360 #[inline]
361 fn unpack(&self) -> Unpacked {
362 let head = u16::from_le_bytes(self.bytes[..2].try_into().unwrap());
363 let hash = self.bytes[2..18].try_into().unwrap();
364 let fingerprint = self.bytes[18..].try_into().unwrap();
365
366 let kind = head & mask(Self::KIND_BITS) as u16;
367 let bytes_per_index = (head >> Self::KIND_BITS) & mask(Self::WIDTH_BITS) as u16;
368 let len = (head as usize) >> (Self::WIDTH_BITS + Self::KIND_BITS);
369
370 Unpacked {
371 len: len.checked_sub(1),
372 bytes_per_index: bytes_per_index as usize + 1,
373 kind: DepKind::new(kind),
374 hash: Fingerprint::from_le_bytes(hash).into(),
375 fingerprint: Fingerprint::from_le_bytes(fingerprint),
376 }
377 }
378
379 #[inline]
380 fn len(&self) -> Option<usize> {
381 self.unpack().len
382 }
383
384 #[inline]
385 fn bytes_per_index(&self) -> usize {
386 self.unpack().bytes_per_index
387 }
388
389 #[inline]
390 fn fingerprint(&self) -> Fingerprint {
391 self.unpack().fingerprint
392 }
393
394 #[inline]
395 fn node(&self) -> DepNode {
396 let Unpacked { kind, hash, .. } = self.unpack();
397 DepNode { kind, hash }
398 }
399
400 #[inline]
401 fn edges_header(&self, edge_list_data: &[u8]) -> EdgeHeader {
402 EdgeHeader {
403 repr: (edge_list_data.len() << DEP_NODE_WIDTH_BITS) | (self.bytes_per_index() - 1),
404 }
405 }
406}
407
408#[derive(Debug)]
409struct NodeInfo {
410 node: DepNode,
411 fingerprint: Fingerprint,
412 edges: EdgesVec,
413}
414
415impl NodeInfo {
416 fn encode<D: Deps>(&self, e: &mut FileEncoder) {
417 let NodeInfo { node, fingerprint, ref edges } = *self;
418 let header =
419 SerializedNodeHeader::<D>::new(node, fingerprint, edges.max_index(), edges.len());
420 e.write_array(header.bytes);
421
422 if header.len().is_none() {
423 e.emit_usize(edges.len());
424 }
425
426 let bytes_per_index = header.bytes_per_index();
427 for node_index in edges.iter() {
428 e.write_with(|dest| {
429 *dest = node_index.as_u32().to_le_bytes();
430 bytes_per_index
431 });
432 }
433 }
434
435 #[inline]
439 fn encode_promoted<D: Deps>(
440 e: &mut FileEncoder,
441 node: DepNode,
442 fingerprint: Fingerprint,
443 prev_index: SerializedDepNodeIndex,
444 prev_index_to_index: &IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>,
445 previous: &SerializedDepGraph,
446 ) -> usize {
447 let edges = previous.edge_targets_from(prev_index);
448 let edge_count = edges.size_hint().0;
449
450 let edge_max =
452 edges.clone().map(|i| prev_index_to_index[i].unwrap().as_u32()).max().unwrap_or(0);
453
454 let header = SerializedNodeHeader::<D>::new(node, fingerprint, edge_max, edge_count);
455 e.write_array(header.bytes);
456
457 if header.len().is_none() {
458 e.emit_usize(edge_count);
459 }
460
461 let bytes_per_index = header.bytes_per_index();
462 for node_index in edges {
463 let node_index = prev_index_to_index[node_index].unwrap();
464 e.write_with(|dest| {
465 *dest = node_index.as_u32().to_le_bytes();
466 bytes_per_index
467 });
468 }
469
470 edge_count
471 }
472}
473
474struct Stat {
475 kind: DepKind,
476 node_counter: u64,
477 edge_counter: u64,
478}
479
480struct EncoderState<D: Deps> {
481 previous: Arc<SerializedDepGraph>,
482 encoder: FileEncoder,
483 total_node_count: usize,
484 total_edge_count: usize,
485 stats: Option<FxHashMap<DepKind, Stat>>,
486
487 kind_stats: Vec<u32>,
489 marker: PhantomData<D>,
490}
491
492impl<D: Deps> EncoderState<D> {
493 fn new(encoder: FileEncoder, record_stats: bool, previous: Arc<SerializedDepGraph>) -> Self {
494 Self {
495 previous,
496 encoder,
497 total_edge_count: 0,
498 total_node_count: 0,
499 stats: record_stats.then(FxHashMap::default),
500 kind_stats: iter::repeat(0).take(D::DEP_KIND_MAX as usize + 1).collect(),
501 marker: PhantomData,
502 }
503 }
504
505 #[inline]
506 fn record(
507 &mut self,
508 node: DepNode,
509 edge_count: usize,
510 edges: impl FnOnce(&mut Self) -> Vec<DepNodeIndex>,
511 record_graph: &Option<Lock<DepGraphQuery>>,
512 ) -> DepNodeIndex {
513 let index = DepNodeIndex::new(self.total_node_count);
514
515 self.total_node_count += 1;
516 self.kind_stats[node.kind.as_usize()] += 1;
517 self.total_edge_count += edge_count;
518
519 if let Some(record_graph) = &record_graph {
520 let edges = edges(self);
522
523 outline(move || {
525 if let Some(record_graph) = &mut record_graph.try_lock() {
527 record_graph.push(index, node, &edges);
528 }
529 });
530 }
531
532 if let Some(stats) = &mut self.stats {
533 let kind = node.kind;
534
535 outline(move || {
537 let stat =
538 stats.entry(kind).or_insert(Stat { kind, node_counter: 0, edge_counter: 0 });
539 stat.node_counter += 1;
540 stat.edge_counter += edge_count as u64;
541 });
542 }
543
544 index
545 }
546
547 fn encode_node(
549 &mut self,
550 node: &NodeInfo,
551 record_graph: &Option<Lock<DepGraphQuery>>,
552 ) -> DepNodeIndex {
553 node.encode::<D>(&mut self.encoder);
554 self.record(node.node, node.edges.len(), |_| node.edges[..].to_vec(), record_graph)
555 }
556
557 #[inline]
564 fn encode_promoted_node(
565 &mut self,
566 prev_index: SerializedDepNodeIndex,
567 record_graph: &Option<Lock<DepGraphQuery>>,
568 prev_index_to_index: &IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>,
569 ) -> DepNodeIndex {
570 let node = self.previous.index_to_node(prev_index);
571
572 let fingerprint = self.previous.fingerprint_by_index(prev_index);
573 let edge_count = NodeInfo::encode_promoted::<D>(
574 &mut self.encoder,
575 node,
576 fingerprint,
577 prev_index,
578 prev_index_to_index,
579 &self.previous,
580 );
581
582 self.record(
583 node,
584 edge_count,
585 |this| {
586 this.previous
587 .edge_targets_from(prev_index)
588 .map(|i| prev_index_to_index[i].unwrap())
589 .collect()
590 },
591 record_graph,
592 )
593 }
594
595 fn finish(self, profiler: &SelfProfilerRef) -> FileEncodeResult {
596 let Self {
597 mut encoder,
598 total_node_count,
599 total_edge_count,
600 stats: _,
601 kind_stats,
602 marker: _,
603 previous: _,
604 } = self;
605
606 let node_count = total_node_count.try_into().unwrap();
607 let edge_count = total_edge_count.try_into().unwrap();
608
609 for count in kind_stats.iter() {
611 count.encode(&mut encoder);
612 }
613
614 debug!(?node_count, ?edge_count);
615 debug!("position: {:?}", encoder.position());
616 IntEncodedWithFixedSize(node_count).encode(&mut encoder);
617 IntEncodedWithFixedSize(edge_count).encode(&mut encoder);
618 debug!("position: {:?}", encoder.position());
619 let result = encoder.finish();
621 if let Ok(position) = result {
622 profiler.artifact_size("dep_graph", "dep-graph.bin", position as u64);
625 }
626 result
627 }
628}
629
630pub(crate) struct GraphEncoder<D: Deps> {
631 profiler: SelfProfilerRef,
632 status: Lock<Option<EncoderState<D>>>,
633 record_graph: Option<Lock<DepGraphQuery>>,
634}
635
636impl<D: Deps> GraphEncoder<D> {
637 pub(crate) fn new(
638 encoder: FileEncoder,
639 prev_node_count: usize,
640 record_graph: bool,
641 record_stats: bool,
642 profiler: &SelfProfilerRef,
643 previous: Arc<SerializedDepGraph>,
644 ) -> Self {
645 let record_graph = record_graph.then(|| Lock::new(DepGraphQuery::new(prev_node_count)));
646 let status = Lock::new(Some(EncoderState::new(encoder, record_stats, previous)));
647 GraphEncoder { status, record_graph, profiler: profiler.clone() }
648 }
649
650 pub(crate) fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
651 if let Some(record_graph) = &self.record_graph {
652 f(&record_graph.lock())
653 }
654 }
655
656 pub(crate) fn print_incremental_info(
657 &self,
658 total_read_count: u64,
659 total_duplicate_read_count: u64,
660 ) {
661 let mut status = self.status.lock();
662 let status = status.as_mut().unwrap();
663 if let Some(record_stats) = &status.stats {
664 let mut stats: Vec<_> = record_stats.values().collect();
665 stats.sort_by_key(|s| -(s.node_counter as i64));
666
667 const SEPARATOR: &str = "[incremental] --------------------------------\
668 ----------------------------------------------\
669 ------------";
670
671 eprintln!("[incremental]");
672 eprintln!("[incremental] DepGraph Statistics");
673 eprintln!("{SEPARATOR}");
674 eprintln!("[incremental]");
675 eprintln!("[incremental] Total Node Count: {}", status.total_node_count);
676 eprintln!("[incremental] Total Edge Count: {}", status.total_edge_count);
677
678 if cfg!(debug_assertions) {
679 eprintln!("[incremental] Total Edge Reads: {total_read_count}");
680 eprintln!("[incremental] Total Duplicate Edge Reads: {total_duplicate_read_count}");
681 }
682
683 eprintln!("[incremental]");
684 eprintln!(
685 "[incremental] {:<36}| {:<17}| {:<12}| {:<17}|",
686 "Node Kind", "Node Frequency", "Node Count", "Avg. Edge Count"
687 );
688 eprintln!("{SEPARATOR}");
689
690 for stat in stats {
691 let node_kind_ratio =
692 (100.0 * (stat.node_counter as f64)) / (status.total_node_count as f64);
693 let node_kind_avg_edges = (stat.edge_counter as f64) / (stat.node_counter as f64);
694
695 eprintln!(
696 "[incremental] {:<36}|{:>16.1}% |{:>12} |{:>17.1} |",
697 format!("{:?}", stat.kind),
698 node_kind_ratio,
699 stat.node_counter,
700 node_kind_avg_edges,
701 );
702 }
703
704 eprintln!("{SEPARATOR}");
705 eprintln!("[incremental]");
706 }
707 }
708
709 pub(crate) fn send(
710 &self,
711 node: DepNode,
712 fingerprint: Fingerprint,
713 edges: EdgesVec,
714 ) -> DepNodeIndex {
715 let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
716 let node = NodeInfo { node, fingerprint, edges };
717 self.status.lock().as_mut().unwrap().encode_node(&node, &self.record_graph)
718 }
719
720 #[inline]
723 pub(crate) fn send_promoted(
724 &self,
725 prev_index: SerializedDepNodeIndex,
726 prev_index_to_index: &IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>,
727 ) -> DepNodeIndex {
728 let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
729 self.status.lock().as_mut().unwrap().encode_promoted_node(
730 prev_index,
731 &self.record_graph,
732 prev_index_to_index,
733 )
734 }
735
736 pub(crate) fn finish(&self) -> FileEncodeResult {
737 let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph_finish");
738
739 self.status.lock().take().unwrap().finish(&self.profiler)
740 }
741}