Skip to content

Commit a730edc

Browse files
committed
Auto merge of rust-lang#135030 - Flakebi:require-cpu, r=workingjubilee
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. amdgpu tracking issue: rust-lang#135024 AVR MCP: rust-lang/compiler-team#800
2 parents 6ac8878 + 53238c3 commit a730edc

File tree

7 files changed

+41
-0
lines changed

7 files changed

+41
-0
lines changed

compiler/rustc_codegen_ssa/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ codegen_ssa_copy_path = could not copy {$from} to {$to}: {$error}
3030
3131
codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$error}
3232
33+
codegen_ssa_cpu_required = target requires explicitly specifying a cpu with `-C target-cpu`
34+
3335
codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error}
3436
3537
codegen_ssa_dlltool_fail_import_library =

compiler/rustc_codegen_ssa/src/base.rs

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

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

620625
// 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
@@ -523,6 +523,10 @@ pub(crate) struct CheckInstalledVisualStudio;
523523
#[diag(codegen_ssa_insufficient_vs_code_product)]
524524
pub(crate) struct InsufficientVSCodeProduct;
525525

526+
#[derive(Diagnostic)]
527+
#[diag(codegen_ssa_cpu_required)]
528+
pub(crate) struct CpuRequired;
529+
526530
#[derive(Diagnostic)]
527531
#[diag(codegen_ssa_processing_dymutil_failed)]
528532
#[note]

compiler/rustc_target/src/spec/json.rs

+2
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ impl Target {
546546
key!(link_env_remove, list);
547547
key!(asm_args, list);
548548
key!(cpu);
549+
key!(need_explicit_cpu, bool);
549550
key!(features);
550551
key!(dynamic_linking, bool);
551552
key!(direct_access_external_data, Option<bool>);
@@ -720,6 +721,7 @@ impl ToJson for Target {
720721
target_option_val!(link_env_remove);
721722
target_option_val!(asm_args);
722723
target_option_val!(cpu);
724+
target_option_val!(need_explicit_cpu);
723725
target_option_val!(features);
724726
target_option_val!(dynamic_linking);
725727
target_option_val!(direct_access_external_data);

compiler/rustc_target/src/spec/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,9 @@ pub struct TargetOptions {
22542254
/// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
22552255
/// to "generic".
22562256
pub cpu: StaticCow<str>,
2257+
/// Whether a cpu needs to be explicitly set.
2258+
/// Set to true if there is no default cpu. Defaults to false.
2259+
pub need_explicit_cpu: bool,
22572260
/// Default target features to pass to LLVM. These features overwrite
22582261
/// `-Ctarget-cpu` but can be overwritten with `-Ctarget-features`.
22592262
/// Corresponds to `llc -mattr=$features`.
@@ -2686,6 +2689,7 @@ impl Default for TargetOptions {
26862689
link_script: None,
26872690
asm_args: cvs![],
26882691
cpu: "generic".into(),
2692+
need_explicit_cpu: false,
26892693
features: "".into(),
26902694
direct_access_external_data: None,
26912695
dynamic_linking: false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"data-layout": "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128",
3+
"linker-flavor": "gcc",
4+
"llvm-target": "i686-unknown-linux-gnu",
5+
"target-endian": "little",
6+
"target-pointer-width": "32",
7+
"target-c-int-width": "32",
8+
"arch": "x86",
9+
"os": "linux",
10+
"need-explicit-cpu": true
11+
}

tests/run-make/target-specs/rmake.rs

+13
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,17 @@ fn main() {
6363
.crate_type("lib")
6464
.run_fail()
6565
.assert_stderr_contains("data-layout for target");
66+
rustc()
67+
.input("foo.rs")
68+
.target("require-explicit-cpu")
69+
.crate_type("lib")
70+
.run_fail()
71+
.assert_stderr_contains("target requires explicitly specifying a cpu");
72+
rustc()
73+
.input("foo.rs")
74+
.target("require-explicit-cpu")
75+
.crate_type("lib")
76+
.arg("-Ctarget-cpu=generic")
77+
.run();
78+
rustc().target("require-explicit-cpu").arg("--print=target-cpus").run();
6679
}

0 commit comments

Comments
 (0)