Skip to content

Commit 78dae5e

Browse files
committed
Target option to require explicit cpu
Some targets have many different CPUs and no generic CPU that can be used as a default. For these targets, the user needs to explicitly specify a CPU through `-C target-cpu=`. Add an option for targets and an error message if no CPU is set. This affects the proposed amdgpu and avr targets.
1 parent bf6f8a4 commit 78dae5e

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

compiler/rustc_codegen_ssa/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ codegen_ssa_incorrect_cgu_reuse_type =
8484
8585
codegen_ssa_insufficient_vs_code_product = VS Code is a different product, and is not sufficient.
8686
87+
codegen_ssa_cpu_required = target requires to explicitly specify a cpu with `-C target-cpu`
88+
8789
codegen_ssa_invalid_link_ordinal_nargs = incorrect number of arguments to `#[link_ordinal]`
8890
.note = the attribute requires exactly one argument
8991

compiler/rustc_codegen_ssa/src/base.rs

+5
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,11 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
616616
return ongoing_codegen;
617617
}
618618

619+
if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() {
620+
// The target has no default cpu, but none is set explicitly
621+
tcx.dcx().emit_fatal(errors::CpuRequired);
622+
}
623+
619624
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
620625

621626
// Run the monomorphization collector and partition the collected items into

compiler/rustc_codegen_ssa/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ pub(crate) struct CheckInstalledVisualStudio;
491491
#[diag(codegen_ssa_insufficient_vs_code_product)]
492492
pub(crate) struct InsufficientVSCodeProduct;
493493

494+
#[derive(Diagnostic)]
495+
#[diag(codegen_ssa_cpu_required)]
496+
pub(crate) struct CpuRequired;
497+
494498
#[derive(Diagnostic)]
495499
#[diag(codegen_ssa_processing_dymutil_failed)]
496500
#[note]

compiler/rustc_target/src/spec/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,9 @@ pub struct TargetOptions {
22402240
/// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
22412241
/// to "generic".
22422242
pub cpu: StaticCow<str>,
2243+
/// Wether a cpu needs to be explicitly set.
2244+
/// Set to true if there is no default cpu. Defaults to false.
2245+
pub need_explicit_cpu: bool,
22432246
/// Default target features to pass to LLVM. These features overwrite
22442247
/// `-Ctarget-cpu` but can be overwritten with `-Ctarget-features`.
22452248
/// Corresponds to `llc -mattr=$features`.
@@ -2676,6 +2679,7 @@ impl Default for TargetOptions {
26762679
link_script: None,
26772680
asm_args: cvs![],
26782681
cpu: "generic".into(),
2682+
need_explicit_cpu: false,
26792683
features: "".into(),
26802684
direct_access_external_data: None,
26812685
dynamic_linking: false,

0 commit comments

Comments
 (0)