2016-07-21 16:50:15 +00:00
|
|
|
//! This pass is only used for UNIT TESTS related to incremental
|
|
|
|
//! compilation. It tests whether a particular `.o` file will be re-used
|
|
|
|
//! from a previous compilation or whether it must be regenerated.
|
|
|
|
//!
|
|
|
|
//! The user adds annotations to the crate of the following form:
|
|
|
|
//!
|
|
|
|
//! ```
|
2022-04-15 22:04:34 +00:00
|
|
|
//! # #![feature(rustc_attrs)]
|
2023-03-09 20:54:53 +00:00
|
|
|
//! # #![allow(internal_features)]
|
2016-07-21 16:50:15 +00:00
|
|
|
//! #![rustc_partition_reused(module="spike", cfg="rpass2")]
|
2018-05-08 13:10:16 +00:00
|
|
|
//! #![rustc_partition_codegened(module="spike-x", cfg="rpass2")]
|
2016-07-21 16:50:15 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The first indicates (in the cfg `rpass2`) that `spike.o` will be
|
|
|
|
//! reused, the second that `spike-x.o` will be recreated. If these
|
|
|
|
//! annotations are inaccurate, errors are reported.
|
|
|
|
//!
|
|
|
|
//! The reason that we use `cfg=...` and not `#[cfg_attr]` is so that
|
|
|
|
//! the HIR doesn't change as a result of the annotations, which might
|
|
|
|
//! perturb the reuse results.
|
2018-09-18 14:33:24 +00:00
|
|
|
//!
|
2023-03-03 03:10:46 +00:00
|
|
|
//! `#![rustc_expected_cgu_reuse(module="spike", cfg="rpass2", kind="post-lto")]`
|
2018-09-18 14:33:24 +00:00
|
|
|
//! allows for doing a more fine-grained check to see if pre- or post-lto data
|
|
|
|
//! was re-used.
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2022-09-26 14:42:12 +00:00
|
|
|
use std::borrow::Cow;
|
2023-09-19 11:26:46 +00:00
|
|
|
use std::fmt;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2023-05-07 23:52:19 +00:00
|
|
|
use rustc_data_structures::unord::{UnordMap, UnordSet};
|
2024-03-05 05:53:24 +00:00
|
|
|
use rustc_errors::{DiagArgValue, IntoDiagArg};
|
2024-10-16 23:14:01 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2023-09-19 11:23:35 +00:00
|
|
|
use rustc_session::Session;
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::{Span, Symbol, sym};
|
2022-11-23 00:55:16 +00:00
|
|
|
use thin_vec::ThinVec;
|
2024-05-22 05:08:26 +00:00
|
|
|
use tracing::debug;
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2023-12-18 11:21:37 +00:00
|
|
|
use crate::errors;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2021-10-29 17:14:17 +00:00
|
|
|
#[allow(missing_docs)]
|
2023-09-19 11:26:46 +00:00
|
|
|
pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&mut CguReuseTracker)) {
|
2017-12-28 05:05:45 +00:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
|
|
|
if tcx.sess.opts.incremental.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2025-01-27 08:11:02 +00:00
|
|
|
let available_cgus = tcx
|
|
|
|
.collect_and_partition_mono_items(())
|
|
|
|
.codegen_units
|
|
|
|
.iter()
|
|
|
|
.map(|cgu| cgu.name())
|
|
|
|
.collect();
|
2018-09-18 14:33:24 +00:00
|
|
|
|
2023-09-19 11:26:46 +00:00
|
|
|
let mut ams = AssertModuleSource {
|
2023-09-19 11:23:35 +00:00
|
|
|
tcx,
|
|
|
|
available_cgus,
|
|
|
|
cgu_reuse_tracker: if tcx.sess.opts.unstable_opts.query_dep_graph {
|
|
|
|
CguReuseTracker::new()
|
|
|
|
} else {
|
|
|
|
CguReuseTracker::new_disabled()
|
|
|
|
},
|
|
|
|
};
|
2018-09-18 14:33:24 +00:00
|
|
|
|
2020-11-26 22:38:53 +00:00
|
|
|
for attr in tcx.hir().attrs(rustc_hir::CRATE_HIR_ID) {
|
2017-12-28 05:05:45 +00:00
|
|
|
ams.check_attr(attr);
|
|
|
|
}
|
2023-09-19 11:23:35 +00:00
|
|
|
|
2023-09-19 11:26:46 +00:00
|
|
|
set_reuse(&mut ams.cgu_reuse_tracker);
|
2023-09-19 11:23:35 +00:00
|
|
|
|
|
|
|
ams.cgu_reuse_tracker.check_expected_reuse(tcx.sess);
|
|
|
|
});
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct AssertModuleSource<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-05-07 23:52:19 +00:00
|
|
|
available_cgus: UnordSet<Symbol>,
|
2023-09-19 11:23:35 +00:00
|
|
|
cgu_reuse_tracker: CguReuseTracker,
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 04:49:21 +00:00
|
|
|
impl<'tcx> AssertModuleSource<'tcx> {
|
2024-10-16 23:14:01 +00:00
|
|
|
fn check_attr(&mut self, attr: &hir::Attribute) {
|
2021-07-29 17:00:41 +00:00
|
|
|
let (expected_reuse, comp_kind) = if attr.has_name(sym::rustc_partition_reused) {
|
|
|
|
(CguReuse::PreLto, ComparisonKind::AtLeast)
|
|
|
|
} else if attr.has_name(sym::rustc_partition_codegened) {
|
|
|
|
(CguReuse::No, ComparisonKind::Exact)
|
|
|
|
} else if attr.has_name(sym::rustc_expected_cgu_reuse) {
|
|
|
|
match self.field(attr, sym::kind) {
|
|
|
|
sym::no => (CguReuse::No, ComparisonKind::Exact),
|
|
|
|
sym::pre_dash_lto => (CguReuse::PreLto, ComparisonKind::Exact),
|
|
|
|
sym::post_dash_lto => (CguReuse::PostLto, ComparisonKind::Exact),
|
|
|
|
sym::any => (CguReuse::PreLto, ComparisonKind::AtLeast),
|
|
|
|
other => {
|
2022-09-26 14:42:12 +00:00
|
|
|
self.tcx
|
2023-12-18 11:21:37 +00:00
|
|
|
.dcx()
|
2022-09-26 14:42:12 +00:00
|
|
|
.emit_fatal(errors::UnknownReuseKind { span: attr.span, kind: other });
|
2018-09-18 14:33:24 +00:00
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if !self.tcx.sess.opts.unstable_opts.query_dep_graph {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_fatal(errors::MissingQueryDepGraph { span: attr.span });
|
2018-09-18 14:33:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 16:50:15 +00:00
|
|
|
if !self.check_config(attr) {
|
|
|
|
debug!("check_attr: config does not match, ignoring attr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-21 21:31:37 +00:00
|
|
|
let user_path = self.field(attr, sym::module).to_string();
|
|
|
|
let crate_name = self.tcx.crate_name(LOCAL_CRATE).to_string();
|
2018-07-10 17:16:22 +00:00
|
|
|
|
|
|
|
if !user_path.starts_with(&crate_name) {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_fatal(errors::MalformedCguName {
|
2022-09-26 14:42:12 +00:00
|
|
|
span: attr.span,
|
|
|
|
user_path,
|
|
|
|
crate_name,
|
|
|
|
});
|
2018-07-10 17:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split of the "special suffix" if there is one.
|
2020-02-26 12:03:46 +00:00
|
|
|
let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind('.') {
|
2018-07-10 17:16:22 +00:00
|
|
|
(&user_path[..index], Some(&user_path[index + 1..]))
|
|
|
|
} else {
|
|
|
|
(&user_path[..], None)
|
|
|
|
};
|
|
|
|
|
2020-10-28 16:05:49 +00:00
|
|
|
let mut iter = user_path.split('-');
|
2018-07-10 17:16:22 +00:00
|
|
|
|
|
|
|
// Remove the crate name
|
2020-10-28 16:05:49 +00:00
|
|
|
assert_eq!(iter.next().unwrap(), crate_name);
|
|
|
|
|
|
|
|
let cgu_path_components = iter.collect::<Vec<_>>();
|
2018-07-10 17:16:22 +00:00
|
|
|
|
2018-08-14 15:55:22 +00:00
|
|
|
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
|
|
|
|
let cgu_name =
|
|
|
|
cgu_name_builder.build_cgu_name(LOCAL_CRATE, cgu_path_components, cgu_special_suffix);
|
2018-07-10 17:16:22 +00:00
|
|
|
|
2019-10-21 21:22:23 +00:00
|
|
|
debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name);
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
if !self.available_cgus.contains(&cgu_name) {
|
2023-06-05 04:16:20 +00:00
|
|
|
let cgu_names: Vec<&str> =
|
2023-06-08 04:38:50 +00:00
|
|
|
self.available_cgus.items().map(|cgu| cgu.as_str()).into_sorted_stable_ord();
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_err(errors::NoModuleNamed {
|
2022-09-26 14:42:12 +00:00
|
|
|
span: attr.span,
|
|
|
|
user_path,
|
|
|
|
cgu_name,
|
|
|
|
cgu_names: cgu_names.join(", "),
|
|
|
|
});
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
2018-09-18 14:33:24 +00:00
|
|
|
|
2023-09-19 11:23:35 +00:00
|
|
|
self.cgu_reuse_tracker.set_expectation(
|
2020-07-08 10:03:37 +00:00
|
|
|
cgu_name,
|
2023-11-21 19:07:32 +00:00
|
|
|
user_path,
|
2018-09-18 14:33:24 +00:00
|
|
|
attr.span,
|
|
|
|
expected_reuse,
|
|
|
|
comp_kind,
|
|
|
|
);
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 23:14:01 +00:00
|
|
|
fn field(&self, attr: &hir::Attribute, name: Symbol) -> Symbol {
|
2022-11-23 00:55:16 +00:00
|
|
|
for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
|
2020-08-02 10:17:20 +00:00
|
|
|
if item.has_name(name) {
|
2016-07-21 16:50:15 +00:00
|
|
|
if let Some(value) = item.value_str() {
|
2016-11-16 10:52:37 +00:00
|
|
|
return value;
|
2016-07-21 16:50:15 +00:00
|
|
|
} else {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_fatal(errors::FieldAssociatedValueExpected {
|
2022-09-26 14:42:12 +00:00
|
|
|
span: item.span(),
|
|
|
|
name,
|
|
|
|
});
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_fatal(errors::NoField { span: attr.span, name });
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scan for a `cfg="foo"` attribute and check whether we have a
|
|
|
|
/// cfg flag called `foo`.
|
2024-10-16 23:14:01 +00:00
|
|
|
fn check_config(&self, attr: &hir::Attribute) -> bool {
|
2024-03-04 05:31:49 +00:00
|
|
|
let config = &self.tcx.sess.psess.config;
|
2019-10-21 21:22:23 +00:00
|
|
|
let value = self.field(attr, sym::cfg);
|
2016-07-21 16:50:15 +00:00
|
|
|
debug!("check_config(config={:?}, value={:?})", config, value);
|
2016-11-15 08:54:27 +00:00
|
|
|
if config.iter().any(|&(name, _)| name == value) {
|
2016-07-21 16:50:15 +00:00
|
|
|
debug!("check_config: matched");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
debug!("check_config: no match found");
|
2020-03-20 14:03:11 +00:00
|
|
|
false
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-19 11:23:35 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
|
|
|
|
pub enum CguReuse {
|
|
|
|
No,
|
|
|
|
PreLto,
|
|
|
|
PostLto,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CguReuse {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
CguReuse::No => write!(f, "No"),
|
2023-12-12 15:09:50 +00:00
|
|
|
CguReuse::PreLto => write!(f, "PreLto"),
|
|
|
|
CguReuse::PostLto => write!(f, "PostLto"),
|
2023-09-19 11:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 05:53:24 +00:00
|
|
|
impl IntoDiagArg for CguReuse {
|
|
|
|
fn into_diag_arg(self) -> DiagArgValue {
|
2024-02-23 03:37:48 +00:00
|
|
|
DiagArgValue::Str(Cow::Owned(self.to_string()))
|
2023-09-19 11:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
pub enum ComparisonKind {
|
|
|
|
Exact,
|
|
|
|
AtLeast,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TrackerData {
|
2024-02-11 11:50:50 +00:00
|
|
|
actual_reuse: UnordMap<String, CguReuse>,
|
|
|
|
expected_reuse: UnordMap<String, (String, Span, CguReuse, ComparisonKind)>,
|
2023-09-19 11:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CguReuseTracker {
|
2023-09-19 11:26:46 +00:00
|
|
|
data: Option<TrackerData>,
|
2023-09-19 11:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CguReuseTracker {
|
2023-10-05 09:49:10 +00:00
|
|
|
fn new() -> CguReuseTracker {
|
2023-09-19 11:23:35 +00:00
|
|
|
let data =
|
|
|
|
TrackerData { actual_reuse: Default::default(), expected_reuse: Default::default() };
|
|
|
|
|
2023-09-19 11:26:46 +00:00
|
|
|
CguReuseTracker { data: Some(data) }
|
2023-09-19 11:23:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 09:49:10 +00:00
|
|
|
fn new_disabled() -> CguReuseTracker {
|
2023-09-19 11:23:35 +00:00
|
|
|
CguReuseTracker { data: None }
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:26:46 +00:00
|
|
|
pub fn set_actual_reuse(&mut self, cgu_name: &str, kind: CguReuse) {
|
|
|
|
if let Some(data) = &mut self.data {
|
2023-09-19 11:23:35 +00:00
|
|
|
debug!("set_actual_reuse({cgu_name:?}, {kind:?})");
|
|
|
|
|
2023-09-19 11:26:46 +00:00
|
|
|
let prev_reuse = data.actual_reuse.insert(cgu_name.to_string(), kind);
|
2023-09-19 11:23:35 +00:00
|
|
|
assert!(prev_reuse.is_none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 09:49:10 +00:00
|
|
|
fn set_expectation(
|
2023-09-19 11:26:46 +00:00
|
|
|
&mut self,
|
2023-09-19 11:23:35 +00:00
|
|
|
cgu_name: Symbol,
|
|
|
|
cgu_user_name: &str,
|
|
|
|
error_span: Span,
|
|
|
|
expected_reuse: CguReuse,
|
|
|
|
comparison_kind: ComparisonKind,
|
|
|
|
) {
|
2023-09-19 11:26:46 +00:00
|
|
|
if let Some(data) = &mut self.data {
|
2023-09-19 11:23:35 +00:00
|
|
|
debug!("set_expectation({cgu_name:?}, {expected_reuse:?}, {comparison_kind:?})");
|
|
|
|
|
|
|
|
data.expected_reuse.insert(
|
|
|
|
cgu_name.to_string(),
|
2023-09-19 11:26:46 +00:00
|
|
|
(cgu_user_name.to_string(), error_span, expected_reuse, comparison_kind),
|
2023-09-19 11:23:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 09:49:10 +00:00
|
|
|
fn check_expected_reuse(&self, sess: &Session) {
|
2023-09-19 11:23:35 +00:00
|
|
|
if let Some(ref data) = self.data {
|
2024-02-11 11:50:50 +00:00
|
|
|
let keys = data.expected_reuse.keys().into_sorted_stable_ord();
|
2023-12-29 12:41:35 +00:00
|
|
|
for cgu_name in keys {
|
|
|
|
let &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind) =
|
|
|
|
data.expected_reuse.get(cgu_name).unwrap();
|
|
|
|
|
2023-09-19 11:23:35 +00:00
|
|
|
if let Some(&actual_reuse) = data.actual_reuse.get(cgu_name) {
|
|
|
|
let (error, at_least) = match comparison_kind {
|
|
|
|
ComparisonKind::Exact => (expected_reuse != actual_reuse, false),
|
|
|
|
ComparisonKind::AtLeast => (actual_reuse < expected_reuse, true),
|
|
|
|
};
|
|
|
|
|
|
|
|
if error {
|
|
|
|
let at_least = if at_least { 1 } else { 0 };
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(errors::IncorrectCguReuseType {
|
2023-09-19 11:26:46 +00:00
|
|
|
span: *error_span,
|
2023-09-19 11:23:35 +00:00
|
|
|
cgu_user_name,
|
|
|
|
actual_reuse,
|
|
|
|
expected_reuse,
|
|
|
|
at_least,
|
2023-12-12 15:47:32 +00:00
|
|
|
});
|
2023-09-19 11:23:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(errors::CguNotRecorded { cgu_user_name, cgu_name });
|
2023-09-19 11:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|