Remove interior mutability from CguReuseTracker

This commit is contained in:
bjorn3 2023-09-19 11:26:46 +00:00
parent e9fa2ca6ad
commit 7f5af72e64

View File

@ -35,12 +35,11 @@ use rustc_session::Session;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt::{self}; use std::fmt;
use std::sync::{Arc, Mutex};
use thin_vec::ThinVec; use thin_vec::ThinVec;
#[allow(missing_docs)] #[allow(missing_docs)]
pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&CguReuseTracker)) { pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&mut CguReuseTracker)) {
tcx.dep_graph.with_ignore(|| { tcx.dep_graph.with_ignore(|| {
if tcx.sess.opts.incremental.is_none() { if tcx.sess.opts.incremental.is_none() {
return; return;
@ -49,7 +48,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&CguReuseTracke
let available_cgus = let available_cgus =
tcx.collect_and_partition_mono_items(()).1.iter().map(|cgu| cgu.name()).collect(); tcx.collect_and_partition_mono_items(()).1.iter().map(|cgu| cgu.name()).collect();
let ams = AssertModuleSource { let mut ams = AssertModuleSource {
tcx, tcx,
available_cgus, available_cgus,
cgu_reuse_tracker: if tcx.sess.opts.unstable_opts.query_dep_graph { cgu_reuse_tracker: if tcx.sess.opts.unstable_opts.query_dep_graph {
@ -63,7 +62,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&CguReuseTracke
ams.check_attr(attr); ams.check_attr(attr);
} }
set_reuse(&ams.cgu_reuse_tracker); set_reuse(&mut ams.cgu_reuse_tracker);
ams.cgu_reuse_tracker.check_expected_reuse(tcx.sess); ams.cgu_reuse_tracker.check_expected_reuse(tcx.sess);
}); });
@ -76,7 +75,7 @@ struct AssertModuleSource<'tcx> {
} }
impl<'tcx> AssertModuleSource<'tcx> { impl<'tcx> AssertModuleSource<'tcx> {
fn check_attr(&self, attr: &ast::Attribute) { fn check_attr(&mut self, attr: &ast::Attribute) {
let (expected_reuse, comp_kind) = if attr.has_name(sym::rustc_partition_reused) { let (expected_reuse, comp_kind) = if attr.has_name(sym::rustc_partition_reused) {
(CguReuse::PreLto, ComparisonKind::AtLeast) (CguReuse::PreLto, ComparisonKind::AtLeast)
} else if attr.has_name(sym::rustc_partition_codegened) { } else if attr.has_name(sym::rustc_partition_codegened) {
@ -220,20 +219,11 @@ pub enum ComparisonKind {
struct TrackerData { struct TrackerData {
actual_reuse: FxHashMap<String, CguReuse>, actual_reuse: FxHashMap<String, CguReuse>,
expected_reuse: FxHashMap<String, (String, SendSpan, CguReuse, ComparisonKind)>, expected_reuse: FxHashMap<String, (String, Span, CguReuse, ComparisonKind)>,
} }
// Span does not implement `Send`, so we can't just store it in the shared
// `TrackerData` object. Instead of splitting up `TrackerData` into shared and
// non-shared parts (which would be complicated), we just mark the `Span` here
// explicitly as `Send`. That's safe because the span data here is only ever
// accessed from the main thread.
struct SendSpan(Span);
unsafe impl Send for SendSpan {}
#[derive(Clone)]
pub struct CguReuseTracker { pub struct CguReuseTracker {
data: Option<Arc<Mutex<TrackerData>>>, data: Option<TrackerData>,
} }
impl CguReuseTracker { impl CguReuseTracker {
@ -241,45 +231,42 @@ impl CguReuseTracker {
let data = let data =
TrackerData { actual_reuse: Default::default(), expected_reuse: Default::default() }; TrackerData { actual_reuse: Default::default(), expected_reuse: Default::default() };
CguReuseTracker { data: Some(Arc::new(Mutex::new(data))) } CguReuseTracker { data: Some(data) }
} }
pub fn new_disabled() -> CguReuseTracker { pub fn new_disabled() -> CguReuseTracker {
CguReuseTracker { data: None } CguReuseTracker { data: None }
} }
pub fn set_actual_reuse(&self, cgu_name: &str, kind: CguReuse) { pub fn set_actual_reuse(&mut self, cgu_name: &str, kind: CguReuse) {
if let Some(ref data) = self.data { if let Some(data) = &mut self.data {
debug!("set_actual_reuse({cgu_name:?}, {kind:?})"); debug!("set_actual_reuse({cgu_name:?}, {kind:?})");
let prev_reuse = data.lock().unwrap().actual_reuse.insert(cgu_name.to_string(), kind); let prev_reuse = data.actual_reuse.insert(cgu_name.to_string(), kind);
assert!(prev_reuse.is_none()); assert!(prev_reuse.is_none());
} }
} }
pub fn set_expectation( pub fn set_expectation(
&self, &mut self,
cgu_name: Symbol, cgu_name: Symbol,
cgu_user_name: &str, cgu_user_name: &str,
error_span: Span, error_span: Span,
expected_reuse: CguReuse, expected_reuse: CguReuse,
comparison_kind: ComparisonKind, comparison_kind: ComparisonKind,
) { ) {
if let Some(ref data) = self.data { if let Some(data) = &mut self.data {
debug!("set_expectation({cgu_name:?}, {expected_reuse:?}, {comparison_kind:?})"); debug!("set_expectation({cgu_name:?}, {expected_reuse:?}, {comparison_kind:?})");
let mut data = data.lock().unwrap();
data.expected_reuse.insert( data.expected_reuse.insert(
cgu_name.to_string(), cgu_name.to_string(),
(cgu_user_name.to_string(), SendSpan(error_span), expected_reuse, comparison_kind), (cgu_user_name.to_string(), error_span, expected_reuse, comparison_kind),
); );
} }
} }
pub fn check_expected_reuse(&self, sess: &Session) { pub fn check_expected_reuse(&self, sess: &Session) {
if let Some(ref data) = self.data { if let Some(ref data) = self.data {
let data = data.lock().unwrap();
for (cgu_name, &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind)) in for (cgu_name, &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind)) in
&data.expected_reuse &data.expected_reuse
{ {
@ -292,7 +279,7 @@ impl CguReuseTracker {
if error { if error {
let at_least = if at_least { 1 } else { 0 }; let at_least = if at_least { 1 } else { 0 };
errors::IncorrectCguReuseType { errors::IncorrectCguReuseType {
span: error_span.0, span: *error_span,
cgu_user_name, cgu_user_name,
actual_reuse, actual_reuse,
expected_reuse, expected_reuse,