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 rustc_data_structures::fx::FxHashMap;
6 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 {
43 let data = TrackerData {
44 actual_reuse: Default::default(),
45 expected_reuse: Default::default(),
49 data: Some(Arc::new(Mutex::new(data))),
53 pub fn new_disabled() -> CguReuseTracker {
59 pub fn set_actual_reuse(&self, cgu_name: &str, kind: CguReuse) {
60 if let Some(ref data) = self.data {
61 debug!("set_actual_reuse({:?}, {:?})", cgu_name, kind);
63 let prev_reuse = data.lock()
66 .insert(cgu_name.to_string(), kind);
68 if let Some(prev_reuse) = prev_reuse {
69 // The only time it is legal to overwrite reuse state is when
70 // we discover during ThinLTO that we can actually reuse the
71 // post-LTO version of a CGU.
72 assert_eq!(prev_reuse, CguReuse::PreLto);
77 pub fn set_expectation(&self,
81 expected_reuse: CguReuse,
82 comparison_kind: ComparisonKind) {
83 if let Some(ref data) = self.data {
84 debug!("set_expectation({:?}, {:?}, {:?})", cgu_name,
87 let mut data = data.lock().unwrap();
89 data.expected_reuse.insert(cgu_name.to_string(),
90 (cgu_user_name.to_string(),
97 pub fn check_expected_reuse(&self, diag: &rustc_errors::Handler) {
98 if let Some(ref data) = self.data {
99 let data = data.lock().unwrap();
101 for (cgu_name, &(ref cgu_user_name,
104 comparison_kind)) in &data.expected_reuse {
105 if let Some(&actual_reuse) = data.actual_reuse.get(cgu_name) {
106 let (error, at_least) = match comparison_kind {
107 ComparisonKind::Exact => {
108 (expected_reuse != actual_reuse, false)
110 ComparisonKind::AtLeast => {
111 (actual_reuse < expected_reuse, true)
116 let at_least = if at_least { "at least " } else { "" };
117 let msg = format!("CGU-reuse for `{}` is `{:?}` but \
123 diag.span_err(error_span.0, &msg);
126 let msg = format!("CGU-reuse for `{}` (mangled: `{}`) was \
130 diag.span_fatal(error_span.0, &msg).raise();