Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target option to require explicit cpu #135030

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_ssa/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ codegen_ssa_copy_path = could not copy {$from} to {$to}: {$error}

codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$error}

codegen_ssa_cpu_required = target requires explicitly specifying a cpu with `-C target-cpu`

codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error}

codegen_ssa_dlltool_fail_import_library =
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,11 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
return ongoing_codegen;
}

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
tcx.dcx().emit_fatal(errors::CpuRequired);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should instead error when creating the Session? If target features depend on -Ctarget-cpu, the current code would allow a check build to be done with the wrong target features available through #[cfg(target_feature)] if -Ctarget-cpu isn't used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I tried moving the code into validate_commandline_args_with_session_available, but that makes basic commands fail that don’t try to compile any code.

$ rustc --print target-cpus --target amdgcn-amd-amdhsa
error: target requires to explicitly specify a cpu with `-C target-cpu`

and for cargo

$ cargo check
error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target amdgcn-amd-amdhsa --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg` (exit status: 1)
  --- stderr
  error: target requires to explicitly specify a cpu with `-C target-cpu`

(I also tried running cargo check without moving the code and it failed without -Ctarget-cpu.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. Checking it when building the Session would prevent anything that internally uses the Session from working without -Ctarget-cpu, including those that don't need to know about the target cpu.


let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);

// Run the monomorphization collector and partition the collected items into
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ pub(crate) struct CheckInstalledVisualStudio;
#[diag(codegen_ssa_insufficient_vs_code_product)]
pub(crate) struct InsufficientVSCodeProduct;

#[derive(Diagnostic)]
#[diag(codegen_ssa_cpu_required)]
pub(crate) struct CpuRequired;

#[derive(Diagnostic)]
#[diag(codegen_ssa_processing_dymutil_failed)]
#[note]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ impl Target {
key!(link_env_remove, list);
key!(asm_args, list);
key!(cpu);
key!(need_explicit_cpu, bool);
key!(features);
key!(dynamic_linking, bool);
key!(direct_access_external_data, Option<bool>);
Expand Down Expand Up @@ -720,6 +721,7 @@ impl ToJson for Target {
target_option_val!(link_env_remove);
target_option_val!(asm_args);
target_option_val!(cpu);
target_option_val!(need_explicit_cpu);
target_option_val!(features);
target_option_val!(dynamic_linking);
target_option_val!(direct_access_external_data);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,9 @@ pub struct TargetOptions {
/// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
/// to "generic".
pub cpu: StaticCow<str>,
/// Whether a cpu needs to be explicitly set.
/// Set to true if there is no default cpu. Defaults to false.
pub need_explicit_cpu: bool,
/// Default target features to pass to LLVM. These features overwrite
/// `-Ctarget-cpu` but can be overwritten with `-Ctarget-features`.
/// Corresponds to `llc -mattr=$features`.
Expand Down Expand Up @@ -2676,6 +2679,7 @@ impl Default for TargetOptions {
link_script: None,
asm_args: cvs![],
cpu: "generic".into(),
need_explicit_cpu: false,
features: "".into(),
direct_access_external_data: None,
dynamic_linking: false,
Expand Down
11 changes: 11 additions & 0 deletions tests/run-make/target-specs/require-explicit-cpu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"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",
"linker-flavor": "gcc",
"llvm-target": "i686-unknown-linux-gnu",
"target-endian": "little",
"target-pointer-width": "32",
"target-c-int-width": "32",
"arch": "x86",
"os": "linux",
"need-explicit-cpu": true
}
13 changes: 13 additions & 0 deletions tests/run-make/target-specs/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,17 @@ fn main() {
.crate_type("lib")
.run_fail()
.assert_stderr_contains("data-layout for target");
rustc()
.input("foo.rs")
.target("require-explicit-cpu")
.crate_type("lib")
.run_fail()
.assert_stderr_contains("target requires explicitly specifying a cpu");
rustc()
.input("foo.rs")
.target("require-explicit-cpu")
.crate_type("lib")
.arg("-Ctarget-cpu=generic")
.run();
rustc().target("require-explicit-cpu").arg("--print=target-cpus").run();
}
Loading