|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Some facilities for tracking how codegen-units are reused during incremental |
| 12 | +//! compilition. This is used for incremental compiliation tests and debug |
| 13 | +//! output. |
| 14 | +
|
| 15 | +use session::Session; |
| 16 | +use rustc_data_structures::fx::FxHashMap; |
| 17 | +use std::sync::{Arc, Mutex}; |
| 18 | +use syntax_pos::Span; |
| 19 | + |
| 20 | +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)] |
| 21 | +pub enum CguReuse { |
| 22 | + No, |
| 23 | + PreLto, |
| 24 | + PostLto, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Copy, Clone, Debug, PartialEq)] |
| 28 | +pub enum ComparisonKind { |
| 29 | + Exact, |
| 30 | + AtLeast, |
| 31 | +} |
| 32 | + |
| 33 | +struct TrackerData { |
| 34 | + actual_reuse: FxHashMap<String, CguReuse>, |
| 35 | + expected_reuse: FxHashMap<String, (String, SendSpan, CguReuse, ComparisonKind)>, |
| 36 | +} |
| 37 | + |
| 38 | +// Span does not implement `Send`, so we can't just store it in the shared |
| 39 | +// `TrackerData` object. Instead of splitting up `TrackerData` into shared and |
| 40 | +// non-shared parts (which would be complicated), we just mark the `Span` here |
| 41 | +// explicitly as `Send`. That's safe because the span data here is only ever |
| 42 | +// accessed from the main thread. |
| 43 | +struct SendSpan(Span); |
| 44 | +unsafe impl Send for SendSpan {} |
| 45 | + |
| 46 | +#[derive(Clone)] |
| 47 | +pub struct CguReuseTracker { |
| 48 | + data: Option<Arc<Mutex<TrackerData>>>, |
| 49 | +} |
| 50 | + |
| 51 | +impl CguReuseTracker { |
| 52 | + pub fn new() -> CguReuseTracker { |
| 53 | + let data = TrackerData { |
| 54 | + actual_reuse: FxHashMap(), |
| 55 | + expected_reuse: FxHashMap(), |
| 56 | + }; |
| 57 | + |
| 58 | + CguReuseTracker { |
| 59 | + data: Some(Arc::new(Mutex::new(data))), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + pub fn new_disabled() -> CguReuseTracker { |
| 64 | + CguReuseTracker { |
| 65 | + data: None, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + pub fn set_actual_reuse(&self, cgu_name: &str, kind: CguReuse) { |
| 70 | + if let Some(ref data) = self.data { |
| 71 | + debug!("set_actual_reuse({:?}, {:?})", cgu_name, kind); |
| 72 | + |
| 73 | + let prev_reuse = data.lock() |
| 74 | + .unwrap() |
| 75 | + .actual_reuse |
| 76 | + .insert(cgu_name.to_string(), kind); |
| 77 | + |
| 78 | + if let Some(prev_reuse) = prev_reuse { |
| 79 | + // The only time it is legal to overwrite reuse state is when |
| 80 | + // we discover during ThinLTO that we can actually reuse the |
| 81 | + // post-LTO version of a CGU. |
| 82 | + assert_eq!(prev_reuse, CguReuse::PreLto); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + pub fn set_expectation(&self, |
| 88 | + cgu_name: &str, |
| 89 | + cgu_user_name: &str, |
| 90 | + error_span: Span, |
| 91 | + expected_reuse: CguReuse, |
| 92 | + comparison_kind: ComparisonKind) { |
| 93 | + if let Some(ref data) = self.data { |
| 94 | + debug!("set_expectation({:?}, {:?}, {:?})", cgu_name, |
| 95 | + expected_reuse, |
| 96 | + comparison_kind); |
| 97 | + let mut data = data.lock().unwrap(); |
| 98 | + |
| 99 | + data.expected_reuse.insert(cgu_name.to_string(), |
| 100 | + (cgu_user_name.to_string(), |
| 101 | + SendSpan(error_span), |
| 102 | + expected_reuse, |
| 103 | + comparison_kind)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + pub fn check_expected_reuse(&self, sess: &Session) { |
| 108 | + if let Some(ref data) = self.data { |
| 109 | + let data = data.lock().unwrap(); |
| 110 | + |
| 111 | + for (cgu_name, &(ref cgu_user_name, |
| 112 | + ref error_span, |
| 113 | + expected_reuse, |
| 114 | + comparison_kind)) in &data.expected_reuse { |
| 115 | + if let Some(&actual_reuse) = data.actual_reuse.get(cgu_name) { |
| 116 | + let (error, at_least) = match comparison_kind { |
| 117 | + ComparisonKind::Exact => { |
| 118 | + (expected_reuse != actual_reuse, false) |
| 119 | + } |
| 120 | + ComparisonKind::AtLeast => { |
| 121 | + (actual_reuse < expected_reuse, true) |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + if error { |
| 126 | + let at_least = if at_least { "at least " } else { "" }; |
| 127 | + let msg = format!("CGU-reuse for `{}` is `{:?}` but \ |
| 128 | + should be {}`{:?}`", |
| 129 | + cgu_user_name, |
| 130 | + actual_reuse, |
| 131 | + at_least, |
| 132 | + expected_reuse); |
| 133 | + sess.span_err(error_span.0, &msg); |
| 134 | + } |
| 135 | + } else { |
| 136 | + let msg = format!("CGU-reuse for `{}` (mangled: `{}`) was \ |
| 137 | + not recorded", |
| 138 | + cgu_user_name, |
| 139 | + cgu_name); |
| 140 | + sess.span_fatal(error_span.0, &msg); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments