Skip to content

Commit adaa838

Browse files
committed
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`
2 parents 9ea8d67 + 5a8ffa4 commit adaa838

File tree

6 files changed

+28
-35
lines changed

6 files changed

+28
-35
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -671,18 +671,7 @@ pub(crate) fn run_aot(tcx: TyCtxt<'_>) -> Box<OngoingCodegen> {
671671
}
672672
.to_owned();
673673

674-
let cgus = if tcx.sess.opts.output_types.should_codegen() {
675-
tcx.collect_and_partition_mono_items(()).codegen_units
676-
} else {
677-
// If only `--emit metadata` is used, we shouldn't perform any codegen.
678-
// Also `tcx.collect_and_partition_mono_items` may panic in that case.
679-
return Box::new(OngoingCodegen {
680-
modules: vec![],
681-
allocator_module: None,
682-
crate_info: CrateInfo::new(tcx, target_cpu),
683-
concurrency_limiter: ConcurrencyLimiter::new(0),
684-
});
685-
};
674+
let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
686675

687676
if tcx.dep_graph.is_fully_enabled() {
688677
for cgu in cgus {

compiler/rustc_codegen_cranelift/src/driver/jit.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ fn create_jit_module(tcx: TyCtxt<'_>) -> (UnwindModule<JITModule>, CodegenCx) {
3333
}
3434

3535
pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
36-
if !tcx.sess.opts.output_types.should_codegen() {
37-
tcx.dcx().fatal("JIT mode doesn't work with `cargo check`");
38-
}
36+
// FIXME error on check mode or crate types other than bin in CodegenBackend::init()
3937

4038
if !tcx.crate_types().contains(&rustc_session::config::CrateType::Executable) {
4139
tcx.dcx().fatal("can't jit non-executable crate");

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,13 +1274,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
12741274
})
12751275
.expect("failed to spawn helper thread");
12761276

1277-
let ol =
1278-
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
1279-
// If we know that we won’t be doing codegen, create target machines without optimisation.
1280-
config::OptLevel::No
1281-
} else {
1282-
tcx.backend_optimization_level(())
1283-
};
1277+
let ol = tcx.backend_optimization_level(());
12841278
let backend_features = tcx.global_backend_features(());
12851279

12861280
let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -684,17 +684,6 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
684684
tcx: TyCtxt<'_>,
685685
target_cpu: String,
686686
) -> OngoingCodegen<B> {
687-
// Skip crate items and just output metadata in -Z no-codegen mode.
688-
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
689-
let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, None);
690-
691-
ongoing_codegen.codegen_finished(tcx);
692-
693-
ongoing_codegen.check_for_errors(tcx.sess);
694-
695-
return ongoing_codegen;
696-
}
697-
698687
if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() {
699688
// The target has no default cpu, but none is set explicitly
700689
tcx.dcx().emit_fatal(errors::CpuRequired);

compiler/rustc_interface/src/passes.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{env, fs, iter};
88
use rustc_ast as ast;
99
use rustc_attr_parsing::{AttributeParser, ShouldEmit};
1010
use rustc_codegen_ssa::traits::CodegenBackend;
11+
use rustc_codegen_ssa::{CodegenResults, CrateInfo};
1112
use rustc_data_structures::jobserver::Proxy;
1213
use rustc_data_structures::steal::Steal;
1314
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal};
@@ -1244,7 +1245,21 @@ pub(crate) fn start_codegen<'tcx>(
12441245

12451246
let metadata = rustc_metadata::fs::encode_and_write_metadata(tcx);
12461247

1247-
let codegen = tcx.sess.time("codegen_crate", move || codegen_backend.codegen_crate(tcx));
1248+
let codegen = tcx.sess.time("codegen_crate", move || {
1249+
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
1250+
// Skip crate items and just output metadata in -Z no-codegen mode.
1251+
tcx.sess.dcx().abort_if_errors();
1252+
1253+
// Linker::link will skip join_codegen in case of a CodegenResults Any value.
1254+
Box::new(CodegenResults {
1255+
modules: vec![],
1256+
allocator_module: None,
1257+
crate_info: CrateInfo::new(tcx, "<dummy cpu>".to_owned()),
1258+
})
1259+
} else {
1260+
codegen_backend.codegen_crate(tcx)
1261+
}
1262+
});
12481263

12491264
info!("Post-codegen\n{:?}", tcx.debug_stats());
12501265

compiler/rustc_interface/src/queries.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::Arc;
33

44
use rustc_codegen_ssa::CodegenResults;
55
use rustc_codegen_ssa::traits::CodegenBackend;
6+
use rustc_data_structures::indexmap::IndexMap;
67
use rustc_data_structures::svh::Svh;
78
use rustc_errors::timings::TimingSection;
89
use rustc_hir::def_id::LOCAL_CRATE;
@@ -46,7 +47,14 @@ impl Linker {
4647

4748
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) {
4849
let (codegen_results, mut work_products) = sess.time("finish_ongoing_codegen", || {
49-
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames)
50+
match self.ongoing_codegen.downcast::<CodegenResults>() {
51+
// This was a check only build
52+
Ok(codegen_results) => (*codegen_results, IndexMap::default()),
53+
54+
Err(ongoing_codegen) => {
55+
codegen_backend.join_codegen(ongoing_codegen, sess, &self.output_filenames)
56+
}
57+
}
5058
});
5159
sess.timings.end_section(sess.dcx(), TimingSection::Codegen);
5260

0 commit comments

Comments
 (0)