]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_session/src/cgu_reuse_tracker.rs
Rollup merge of #105223 - lukas-code:(ExprWithBlock), r=petrochenkov
[rust.git] / compiler / rustc_session / src / cgu_reuse_tracker.rs
1 //! Some facilities for tracking how codegen-units are reused during incremental
2 //! compilation. This is used for incremental compilation tests and debug
3 //! output.
4
5 use crate::errors::{CguNotRecorded, IncorrectCguReuseType};
6 use crate::Session;
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
9 use rustc_span::{Span, Symbol};
10 use std::borrow::Cow;
11 use std::fmt::{self};
12 use std::sync::{Arc, Mutex};
13
14 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
15 pub enum CguReuse {
16     No,
17     PreLto,
18     PostLto,
19 }
20
21 impl fmt::Display for CguReuse {
22     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23         match *self {
24             CguReuse::No => write!(f, "No"),
25             CguReuse::PreLto => write!(f, "PreLto "),
26             CguReuse::PostLto => write!(f, "PostLto "),
27         }
28     }
29 }
30
31 impl IntoDiagnosticArg for CguReuse {
32     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
33         DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
34     }
35 }
36
37 #[derive(Copy, Clone, Debug, PartialEq)]
38 pub enum ComparisonKind {
39     Exact,
40     AtLeast,
41 }
42
43 struct TrackerData {
44     actual_reuse: FxHashMap<String, CguReuse>,
45     expected_reuse: FxHashMap<String, (String, SendSpan, CguReuse, ComparisonKind)>,
46 }
47
48 // Span does not implement `Send`, so we can't just store it in the shared
49 // `TrackerData` object. Instead of splitting up `TrackerData` into shared and
50 // non-shared parts (which would be complicated), we just mark the `Span` here
51 // explicitly as `Send`. That's safe because the span data here is only ever
52 // accessed from the main thread.
53 struct SendSpan(Span);
54 unsafe impl Send for SendSpan {}
55
56 #[derive(Clone)]
57 pub struct CguReuseTracker {
58     data: Option<Arc<Mutex<TrackerData>>>,
59 }
60
61 impl CguReuseTracker {
62     pub fn new() -> CguReuseTracker {
63         let data =
64             TrackerData { actual_reuse: Default::default(), expected_reuse: Default::default() };
65
66         CguReuseTracker { data: Some(Arc::new(Mutex::new(data))) }
67     }
68
69     pub fn new_disabled() -> CguReuseTracker {
70         CguReuseTracker { data: None }
71     }
72
73     pub fn set_actual_reuse(&self, cgu_name: &str, kind: CguReuse) {
74         if let Some(ref data) = self.data {
75             debug!("set_actual_reuse({cgu_name:?}, {kind:?})");
76
77             let prev_reuse = data.lock().unwrap().actual_reuse.insert(cgu_name.to_string(), kind);
78
79             if let Some(prev_reuse) = prev_reuse {
80                 // The only time it is legal to overwrite reuse state is when
81                 // we discover during ThinLTO that we can actually reuse the
82                 // post-LTO version of a CGU.
83                 assert_eq!(prev_reuse, CguReuse::PreLto);
84             }
85         }
86     }
87
88     pub fn set_expectation(
89         &self,
90         cgu_name: Symbol,
91         cgu_user_name: &str,
92         error_span: Span,
93         expected_reuse: CguReuse,
94         comparison_kind: ComparisonKind,
95     ) {
96         if let Some(ref data) = self.data {
97             debug!("set_expectation({cgu_name:?}, {expected_reuse:?}, {comparison_kind:?})");
98             let mut data = data.lock().unwrap();
99
100             data.expected_reuse.insert(
101                 cgu_name.to_string(),
102                 (cgu_user_name.to_string(), SendSpan(error_span), expected_reuse, comparison_kind),
103             );
104         }
105     }
106
107     pub fn check_expected_reuse(&self, sess: &Session) {
108         if let Some(ref data) = self.data {
109             let data = data.lock().unwrap();
110
111             for (cgu_name, &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind)) in
112                 &data.expected_reuse
113             {
114                 if let Some(&actual_reuse) = data.actual_reuse.get(cgu_name) {
115                     let (error, at_least) = match comparison_kind {
116                         ComparisonKind::Exact => (expected_reuse != actual_reuse, false),
117                         ComparisonKind::AtLeast => (actual_reuse < expected_reuse, true),
118                     };
119
120                     if error {
121                         let at_least = if at_least { 1 } else { 0 };
122                         IncorrectCguReuseType {
123                             span: error_span.0,
124                             cgu_user_name,
125                             actual_reuse,
126                             expected_reuse,
127                             at_least,
128                         };
129                     }
130                 } else {
131                     sess.emit_fatal(CguNotRecorded { cgu_user_name, cgu_name });
132                 }
133             }
134         }
135     }
136 }