1 //! Some facilities for tracking how codegen-units are reused during incremental
2 //! compilation. This is used for incremental compilation tests and debug
5 use crate::errors::{CguNotRecorded, IncorrectCguReuseType};
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
9 use rustc_span::{Span, Symbol};
12 use std::sync::{Arc, Mutex};
14 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
21 impl fmt::Display for CguReuse {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 CguReuse::No => write!(f, "No"),
25 CguReuse::PreLto => write!(f, "PreLto "),
26 CguReuse::PostLto => write!(f, "PostLto "),
31 impl IntoDiagnosticArg for CguReuse {
32 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
33 DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
37 #[derive(Copy, Clone, Debug, PartialEq)]
38 pub enum ComparisonKind {
44 actual_reuse: FxHashMap<String, CguReuse>,
45 expected_reuse: FxHashMap<String, (String, SendSpan, CguReuse, ComparisonKind)>,
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 {}
57 pub struct CguReuseTracker {
58 data: Option<Arc<Mutex<TrackerData>>>,
61 impl CguReuseTracker {
62 pub fn new() -> CguReuseTracker {
64 TrackerData { actual_reuse: Default::default(), expected_reuse: Default::default() };
66 CguReuseTracker { data: Some(Arc::new(Mutex::new(data))) }
69 pub fn new_disabled() -> CguReuseTracker {
70 CguReuseTracker { data: None }
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:?})");
77 let prev_reuse = data.lock().unwrap().actual_reuse.insert(cgu_name.to_string(), kind);
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);
88 pub fn set_expectation(
93 expected_reuse: CguReuse,
94 comparison_kind: ComparisonKind,
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();
100 data.expected_reuse.insert(
101 cgu_name.to_string(),
102 (cgu_user_name.to_string(), SendSpan(error_span), expected_reuse, comparison_kind),
107 pub fn check_expected_reuse(&self, sess: &Session) {
108 if let Some(ref data) = self.data {
109 let data = data.lock().unwrap();
111 for (cgu_name, &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind)) in
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),
121 let at_least = if at_least { 1 } else { 0 };
122 IncorrectCguReuseType {
131 sess.emit_fatal(CguNotRecorded { cgu_user_name, cgu_name });