Skip to content

bootstrap: validate rust.codegen-backends & target.<triple>.codegen-backends #142212

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,14 +1534,13 @@ fn needs_codegen_config(run: &RunConfig<'_>) -> bool {
needs_codegen_cfg
}

pub(crate) const CODEGEN_BACKEND_PREFIX: &str = "rustc_codegen_";

fn is_codegen_cfg_needed(path: &TaskPath, run: &RunConfig<'_>) -> bool {
let path = path.path.to_str().unwrap();

let is_explicitly_called = |p| -> bool { run.builder.paths.contains(p) };
let should_enforce = run.builder.kind == Kind::Dist || run.builder.kind == Kind::Install;

const CODEGEN_BACKEND_PREFIX: &str = "rustc_codegen_";
if path.contains(CODEGEN_BACKEND_PREFIX) {
let mut needs_codegen_backend_config = true;
for backend in run.builder.config.codegen_backends(run.target) {
Expand Down
41 changes: 22 additions & 19 deletions src/bootstrap/src/core/config/toml/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::str::FromStr;

use serde::{Deserialize, Deserializer};

use crate::core::build_steps::compile::CODEGEN_BACKEND_PREFIX;
use crate::core::config::toml::TomlConfig;
use crate::core::config::{
DebuginfoLevel, Merge, ReplaceOpt, RustcLto, StringOrBool, set, threads_from_config,
Expand Down Expand Up @@ -394,6 +393,23 @@ pub fn check_incompatible_options_for_ci_rustc(
Ok(())
}

pub(crate) const VALID_CODEGEN_BACKENDS: &[&str] = &["llvm", "cranelift", "gcc"];

pub(crate) fn validate_codegen_backends(
backends: Vec<String>,
section: impl FnOnce() -> String,
) -> Vec<String> {
for backend in &backends {
if !VALID_CODEGEN_BACKENDS.contains(&&**backend) {
let section = section();
panic!(
"Invalid value '{backend}' for '{section}.codegen-backends'. Permitted values: {VALID_CODEGEN_BACKENDS:?}."
Comment on lines +403 to +406
Copy link
Member

Choose a reason for hiding this comment

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

mh, I remember a PR deliberately introduce the fallthrough to the previous else if this condition fails to obtain? it was about using an out-of-tree codegen backend, I believe.

cc @FractalFir @WaffleLapkin

Copy link
Member

Choose a reason for hiding this comment

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

I suspect the intent was to panic for codegen backend names starting with rustc_codegen_, while only warning for unknown backends. As written before this PR however it would only panic/warn when the name starts with rustc_codegen_ and don't check the name otherwise however.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not very familiar with this part of the compiler, but, to be honest, I don't see why we should panic on an unknown backend. If I am understanding things correctly, then this will warn people about unknown codegen backends anyway.

My backend in particular does not strictly need this to function(I can override the backend using RUSTFLAGS), but I still think a warning may be better than an outright error here.

Copy link
Member

Choose a reason for hiding this comment

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

The panic is for when we know for sure that the backend is specified wrong. Backends should never be specified with the rustc_codegen_ prefix. That is already implicitly added, so you would end up with a path like compiler/rustc_codegen_rustc_codegen_foo if you specified rustc_codegen_foo. Do note that we already panic on other invalid config options.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with everything above, I think. Panic on rustc_codegen_ prefix makes sense, but checking for known values (llvm/cranelift/gcc) is annoying for out-of-tree backends like my employer's.

);
}
}
backends
}

impl Config {
pub fn apply_rust_config(
&mut self,
Expand Down Expand Up @@ -572,24 +588,11 @@ impl Config {
set(&mut self.ehcont_guard, ehcont_guard);
self.llvm_libunwind_default =
llvm_libunwind.map(|v| v.parse().expect("failed to parse rust.llvm-libunwind"));

if let Some(ref backends) = codegen_backends {
let available_backends = ["llvm", "cranelift", "gcc"];

self.rust_codegen_backends = backends.iter().map(|s| {
if let Some(backend) = s.strip_prefix(CODEGEN_BACKEND_PREFIX) {
if available_backends.contains(&backend) {
panic!("Invalid value '{s}' for 'rust.codegen-backends'. Instead, please use '{backend}'.");
} else {
println!("HELP: '{s}' for 'rust.codegen-backends' might fail. \
Codegen backends are mostly defined without the '{CODEGEN_BACKEND_PREFIX}' prefix. \
In this case, it would be referred to as '{backend}'.");
}
}

s.clone()
}).collect();
}
set(
&mut self.rust_codegen_backends,
codegen_backends
.map(|backends| validate_codegen_backends(backends, || "rust".into())),
);

self.rust_codegen_units = codegen_units.map(threads_from_config);
self.rust_codegen_units_std = codegen_units_std.map(threads_from_config);
Expand Down
22 changes: 4 additions & 18 deletions src/bootstrap/src/core/config/toml/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::collections::HashMap;

use serde::{Deserialize, Deserializer};

use crate::core::build_steps::compile::CODEGEN_BACKEND_PREFIX;
use crate::core::config::toml::rust::validate_codegen_backends;
use crate::core::config::{LlvmLibunwind, Merge, ReplaceOpt, SplitDebuginfo, StringOrBool};
use crate::{Config, HashSet, PathBuf, TargetSelection, define_config, exit};

Expand Down Expand Up @@ -142,23 +142,9 @@ impl Config {
target.rpath = cfg.rpath;
target.optimized_compiler_builtins = cfg.optimized_compiler_builtins;
target.jemalloc = cfg.jemalloc;

if let Some(ref backends) = cfg.codegen_backends {
let available_backends = ["llvm", "cranelift", "gcc"];

target.codegen_backends = Some(backends.iter().map(|s| {
if let Some(backend) = s.strip_prefix(CODEGEN_BACKEND_PREFIX) {
if available_backends.contains(&backend) {
panic!("Invalid value '{s}' for 'target.{triple}.codegen-backends'. Instead, please use '{backend}'.");
} else {
println!("HELP: '{s}' for 'target.{triple}.codegen-backends' might fail. \
Codegen backends are mostly defined without the '{CODEGEN_BACKEND_PREFIX}' prefix. \
In this case, it would be referred to as '{backend}'.");
}
}

s.clone()
}).collect());
if let Some(backends) = cfg.codegen_backends {
target.codegen_backends =
Some(validate_codegen_backends(backends, || format!("target.{triple}")))
}

target.split_debuginfo = cfg.split_debuginfo.as_ref().map(|v| {
Expand Down
Loading