Skip to content

Commit 53238c3

Browse files
committedJan 16, 2025
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 53238c3

File tree

7 files changed

+41
-0
lines changed

7 files changed

+41
-0
lines changed
 

Diff for: ‎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 =

Diff for: ‎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

Diff for: ‎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]

Diff for: ‎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);

Diff for: ‎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+
/// Whether 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,
+11
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+
}

Diff for: ‎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)