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