Auto merge of #148035 - bjorn3:check_skip_codegen_crate, r=madsmtm

Skip codegen_crate call in check mode

This way we don't have to spawn the coordinator thread. Some errors will no longer be emitted with this in check mode. For example the check that `-Ctarget-cpu` is passed on targets that need this.

Suggested by `@saethlin`
This commit is contained in:
bors
2025-10-27 17:24:08 +00:00
6 changed files with 28 additions and 35 deletions

View File

@@ -671,18 +671,7 @@ pub(crate) fn run_aot(tcx: TyCtxt<'_>) -> Box<OngoingCodegen> {
} }
.to_owned(); .to_owned();
let cgus = if tcx.sess.opts.output_types.should_codegen() { let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
tcx.collect_and_partition_mono_items(()).codegen_units
} else {
// If only `--emit metadata` is used, we shouldn't perform any codegen.
// Also `tcx.collect_and_partition_mono_items` may panic in that case.
return Box::new(OngoingCodegen {
modules: vec![],
allocator_module: None,
crate_info: CrateInfo::new(tcx, target_cpu),
concurrency_limiter: ConcurrencyLimiter::new(0),
});
};
if tcx.dep_graph.is_fully_enabled() { if tcx.dep_graph.is_fully_enabled() {
for cgu in cgus { for cgu in cgus {

View File

@@ -33,9 +33,7 @@ fn create_jit_module(tcx: TyCtxt<'_>) -> (UnwindModule<JITModule>, CodegenCx) {
} }
pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! { pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
if !tcx.sess.opts.output_types.should_codegen() { // FIXME error on check mode or crate types other than bin in CodegenBackend::init()
tcx.dcx().fatal("JIT mode doesn't work with `cargo check`");
}
if !tcx.crate_types().contains(&rustc_session::config::CrateType::Executable) { if !tcx.crate_types().contains(&rustc_session::config::CrateType::Executable) {
tcx.dcx().fatal("can't jit non-executable crate"); tcx.dcx().fatal("can't jit non-executable crate");

View File

@@ -1274,13 +1274,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
}) })
.expect("failed to spawn helper thread"); .expect("failed to spawn helper thread");
let ol = let ol = tcx.backend_optimization_level(());
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
// If we know that we wont be doing codegen, create target machines without optimisation.
config::OptLevel::No
} else {
tcx.backend_optimization_level(())
};
let backend_features = tcx.global_backend_features(()); let backend_features = tcx.global_backend_features(());
let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir { let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {

View File

@@ -684,17 +684,6 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
target_cpu: String, target_cpu: String,
) -> OngoingCodegen<B> { ) -> OngoingCodegen<B> {
// Skip crate items and just output metadata in -Z no-codegen mode.
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, None);
ongoing_codegen.codegen_finished(tcx);
ongoing_codegen.check_for_errors(tcx.sess);
return ongoing_codegen;
}
if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() { if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() {
// The target has no default cpu, but none is set explicitly // The target has no default cpu, but none is set explicitly
tcx.dcx().emit_fatal(errors::CpuRequired); tcx.dcx().emit_fatal(errors::CpuRequired);

View File

@@ -8,6 +8,7 @@ use std::{env, fs, iter};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_attr_parsing::{AttributeParser, ShouldEmit}; use rustc_attr_parsing::{AttributeParser, ShouldEmit};
use rustc_codegen_ssa::traits::CodegenBackend; use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_codegen_ssa::{CodegenResults, CrateInfo};
use rustc_data_structures::jobserver::Proxy; use rustc_data_structures::jobserver::Proxy;
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal}; use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal};
@@ -1244,7 +1245,21 @@ pub(crate) fn start_codegen<'tcx>(
let metadata = rustc_metadata::fs::encode_and_write_metadata(tcx); let metadata = rustc_metadata::fs::encode_and_write_metadata(tcx);
let codegen = tcx.sess.time("codegen_crate", move || codegen_backend.codegen_crate(tcx)); let codegen = tcx.sess.time("codegen_crate", move || {
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
// Skip crate items and just output metadata in -Z no-codegen mode.
tcx.sess.dcx().abort_if_errors();
// Linker::link will skip join_codegen in case of a CodegenResults Any value.
Box::new(CodegenResults {
modules: vec![],
allocator_module: None,
crate_info: CrateInfo::new(tcx, "<dummy cpu>".to_owned()),
})
} else {
codegen_backend.codegen_crate(tcx)
}
});
info!("Post-codegen\n{:?}", tcx.debug_stats()); info!("Post-codegen\n{:?}", tcx.debug_stats());

View File

@@ -3,6 +3,7 @@ use std::sync::Arc;
use rustc_codegen_ssa::CodegenResults; use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::traits::CodegenBackend; use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::indexmap::IndexMap;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_errors::timings::TimingSection; use rustc_errors::timings::TimingSection;
use rustc_hir::def_id::LOCAL_CRATE; use rustc_hir::def_id::LOCAL_CRATE;
@@ -46,7 +47,14 @@ impl Linker {
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) { pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) {
let (codegen_results, mut work_products) = sess.time("finish_ongoing_codegen", || { let (codegen_results, mut work_products) = sess.time("finish_ongoing_codegen", || {
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames) match self.ongoing_codegen.downcast::<CodegenResults>() {
// This was a check only build
Ok(codegen_results) => (*codegen_results, IndexMap::default()),
Err(ongoing_codegen) => {
codegen_backend.join_codegen(ongoing_codegen, sess, &self.output_filenames)
}
}
}); });
sess.timings.end_section(sess.dcx(), TimingSection::Codegen); sess.timings.end_section(sess.dcx(), TimingSection::Codegen);