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};
15 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
22 impl fmt::Display for CguReuse {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 CguReuse::No => write!(f, "No"),
26 CguReuse::PreLto => write!(f, "PreLto "),
27 CguReuse::PostLto => write!(f, "PostLto "),
32 impl IntoDiagnosticArg for CguReuse {
33 fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
34 DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
38 #[derive(Copy, Clone, Debug, PartialEq)]
39 pub enum ComparisonKind {
45 actual_reuse: FxHashMap<String, CguReuse>,
46 expected_reuse: FxHashMap<String, (String, SendSpan, CguReuse, ComparisonKind)>,
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 {}
58 pub struct CguReuseTracker {
59 data: Option<Arc<Mutex<TrackerData>>>,
62 impl CguReuseTracker {
63 pub fn new() -> CguReuseTracker {
65 TrackerData { actual_reuse: Default::default(), expected_reuse: Default::default() };
67 CguReuseTracker { data: Some(Arc::new(Mutex::new(data))) }
70 pub fn new_disabled() -> CguReuseTracker {
71 CguReuseTracker { data: None }
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:?})");
78 let prev_reuse = data.lock().unwrap().actual_reuse.insert(cgu_name.to_string(), kind);
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);
89 pub fn set_expectation(
94 expected_reuse: CguReuse,
95 comparison_kind: ComparisonKind,
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();
101 data.expected_reuse.insert(
102 cgu_name.to_string(),
103 (cgu_user_name.to_string(), SendSpan(error_span), expected_reuse, comparison_kind),
108 pub fn check_expected_reuse(&self, sess: &Session) {
109 if let Some(ref data) = self.data {
110 let data = data.lock().unwrap();
112 for (cgu_name, &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind)) in
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),
122 let at_least = if at_least { 1 } else { 0 };
123 IncorrectCguReuseType {
125 cgu_user_name: &cgu_user_name,
132 sess.emit_fatal(CguNotRecorded { cgu_user_name, cgu_name });