1 //! Some facilities for tracking how codegen-units are reused during incremental
2 //! compilation. This is used for incremental compilation tests and debug
6 use rustc_data_structures::fx::FxHashMap;
8 use std::sync::{Arc, Mutex};
10 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
17 #[derive(Copy, Clone, Debug, PartialEq)]
18 pub enum ComparisonKind {
24 actual_reuse: FxHashMap<String, CguReuse>,
25 expected_reuse: FxHashMap<String, (String, SendSpan, CguReuse, ComparisonKind)>,
28 // Span does not implement `Send`, so we can't just store it in the shared
29 // `TrackerData` object. Instead of splitting up `TrackerData` into shared and
30 // non-shared parts (which would be complicated), we just mark the `Span` here
31 // explicitly as `Send`. That's safe because the span data here is only ever
32 // accessed from the main thread.
33 struct SendSpan(Span);
34 unsafe impl Send for SendSpan {}
37 pub struct CguReuseTracker {
38 data: Option<Arc<Mutex<TrackerData>>>,
41 impl CguReuseTracker {
42 pub fn new() -> CguReuseTracker {
44 TrackerData { actual_reuse: Default::default(), expected_reuse: Default::default() };
46 CguReuseTracker { data: Some(Arc::new(Mutex::new(data))) }
49 pub fn new_disabled() -> CguReuseTracker {
50 CguReuseTracker { data: None }
53 pub fn set_actual_reuse(&self, cgu_name: &str, kind: CguReuse) {
54 if let Some(ref data) = self.data {
55 debug!("set_actual_reuse({:?}, {:?})", cgu_name, kind);
57 let prev_reuse = data.lock().unwrap().actual_reuse.insert(cgu_name.to_string(), kind);
59 if let Some(prev_reuse) = prev_reuse {
60 // The only time it is legal to overwrite reuse state is when
61 // we discover during ThinLTO that we can actually reuse the
62 // post-LTO version of a CGU.
63 assert_eq!(prev_reuse, CguReuse::PreLto);
68 pub fn set_expectation(
73 expected_reuse: CguReuse,
74 comparison_kind: ComparisonKind,
76 if let Some(ref data) = self.data {
77 debug!("set_expectation({:?}, {:?}, {:?})", cgu_name, expected_reuse, comparison_kind);
78 let mut data = data.lock().unwrap();
80 data.expected_reuse.insert(
82 (cgu_user_name.to_string(), SendSpan(error_span), expected_reuse, comparison_kind),
87 pub fn check_expected_reuse(&self, diag: &rustc_errors::Handler) {
88 if let Some(ref data) = self.data {
89 let data = data.lock().unwrap();
91 for (cgu_name, &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind)) in
94 if let Some(&actual_reuse) = data.actual_reuse.get(cgu_name) {
95 let (error, at_least) = match comparison_kind {
96 ComparisonKind::Exact => (expected_reuse != actual_reuse, false),
97 ComparisonKind::AtLeast => (actual_reuse < expected_reuse, true),
101 let at_least = if at_least { "at least " } else { "" };
103 "CGU-reuse for `{}` is `{:?}` but \
105 cgu_user_name, actual_reuse, at_least, expected_reuse
107 diag.span_err(error_span.0, &msg);
111 "CGU-reuse for `{}` (mangled: `{}`) was \
113 cgu_user_name, cgu_name
115 diag.span_fatal(error_span.0, &msg).raise();