diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index e7a57a7f3755b..56e7582a6ffda 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1830,10 +1830,27 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the cmd.arg("--host").arg(&*compiler.host.triple); cmd.arg("--llvm-filecheck").arg(builder.llvm_filecheck(builder.config.host_target)); - if let Some(codegen_backend) = builder.config.default_codegen_backend(compiler.host) { - // Tells compiletest which codegen backend is used by default by the compiler. + if let Some(codegen_backend) = builder.config.cmd.test_codegen_backend() { + if !builder.config.enabled_codegen_backends(compiler.host).contains(codegen_backend) { + eprintln!( + "\ +ERROR: No configured backend named `{name}` +HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?}]`", + name = codegen_backend.name(), + ); + crate::exit!(1); + } + // Tells compiletest that we want to use this codegen in particular and to override + // the default one. + cmd.arg("--override-codegen-backend").arg(codegen_backend.name()); + // Tells compiletest which codegen backend to use. + // It is used to e.g. ignore tests that don't support that codegen backend. + cmd.arg("--default-codegen-backend").arg(codegen_backend.name()); + } else { + // Tells compiletest which codegen backend to use. // It is used to e.g. ignore tests that don't support that codegen backend. - cmd.arg("--codegen-backend").arg(codegen_backend.name()); + cmd.arg("--default-codegen-backend") + .arg(builder.config.default_codegen_backend(compiler.host).unwrap().name()); } if builder.build.config.llvm_enzyme { diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index 69a744a86cb85..721924034123e 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -1326,7 +1326,12 @@ impl Builder<'_> { if let Some(limit) = limit && (build_compiler_stage == 0 - || self.config.default_codegen_backend(target).unwrap_or_default().is_llvm()) + || self + .config + .default_codegen_backend(target) + .cloned() + .unwrap_or_default() + .is_llvm()) { rustflags.arg(&format!("-Cllvm-args=-import-instr-limit={limit}")); } diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 5eea54360238d..d0647537e5668 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1755,8 +1755,8 @@ impl Config { /// Returns the codegen backend that should be configured as the *default* codegen backend /// for a rustc compiled by bootstrap. - pub fn default_codegen_backend(&self, target: TargetSelection) -> Option { - self.enabled_codegen_backends(target).first().cloned() + pub fn default_codegen_backend(&self, target: TargetSelection) -> Option<&CodegenBackendKind> { + self.enabled_codegen_backends(target).first() } pub fn jemalloc(&self, target: TargetSelection) -> bool { diff --git a/src/bootstrap/src/core/config/flags.rs b/src/bootstrap/src/core/config/flags.rs index 17bfb388280a9..c01b71b926068 100644 --- a/src/bootstrap/src/core/config/flags.rs +++ b/src/bootstrap/src/core/config/flags.rs @@ -15,7 +15,7 @@ use crate::core::build_steps::setup::Profile; use crate::core::builder::{Builder, Kind}; use crate::core::config::Config; use crate::core::config::target_selection::{TargetSelectionList, target_selection_list}; -use crate::{Build, DocTests}; +use crate::{Build, CodegenBackendKind, DocTests}; #[derive(Copy, Clone, Default, Debug, ValueEnum)] pub enum Color { @@ -419,6 +419,9 @@ pub enum Subcommand { #[arg(long)] /// don't capture stdout/stderr of tests no_capture: bool, + #[arg(long)] + /// Use a different codegen backend when running tests. + test_codegen_backend: Option, }, /// Build and run some test suites *in Miri* Miri { @@ -658,6 +661,13 @@ impl Subcommand { _ => vec![], } } + + pub fn test_codegen_backend(&self) -> Option<&CodegenBackendKind> { + match self { + Subcommand::Test { test_codegen_backend, .. } => test_codegen_backend.as_ref(), + _ => None, + } + } } /// Returns the shell completion for a given shell, if the result differs from the current diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 8f766ed00a5be..ec7edbf753129 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -165,6 +165,20 @@ impl CodegenBackendKind { } } +impl std::str::FromStr for CodegenBackendKind { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "" => Err("Invalid empty backend name"), + "gcc" => Ok(Self::Gcc), + "llvm" => Ok(Self::Llvm), + "cranelift" => Ok(Self::Cranelift), + _ => Ok(Self::Custom(s.to_string())), + } + } +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub enum DocTests { /// Run normal tests and doc tests (default). diff --git a/src/bootstrap/src/utils/change_tracker.rs b/src/bootstrap/src/utils/change_tracker.rs index b2f9960a4492a..4fb5891ed181a 100644 --- a/src/bootstrap/src/utils/change_tracker.rs +++ b/src/bootstrap/src/utils/change_tracker.rs @@ -506,6 +506,11 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[ severity: ChangeSeverity::Warning, summary: "It is no longer possible to `x clippy` with stage 0. All clippy commands have to be on stage 1+.", }, + ChangeInfo { + change_id: 145256, + severity: ChangeSeverity::Info, + summary: "Added `--test-codegen-backend` CLI option for tests", + }, ChangeInfo { change_id: 145379, severity: ChangeSeverity::Info, diff --git a/src/etc/completions/x.fish b/src/etc/completions/x.fish index 0b9af33421461..544f9b97237cb 100644 --- a/src/etc/completions/x.fish +++ b/src/etc/completions/x.fish @@ -305,6 +305,7 @@ complete -c x -n "__fish_x_using_subcommand test" -l extra-checks -d 'comma-sepa complete -c x -n "__fish_x_using_subcommand test" -l compare-mode -d 'mode describing what file the actual ui output will be compared to' -r complete -c x -n "__fish_x_using_subcommand test" -l pass -d 'force {check,build,run}-pass tests to this mode' -r complete -c x -n "__fish_x_using_subcommand test" -l run -d 'whether to execute run-* tests' -r +complete -c x -n "__fish_x_using_subcommand test" -l test-codegen-backend -d 'Use a different codegen backend when running tests' -r complete -c x -n "__fish_x_using_subcommand test" -l config -d 'TOML configuration file for build' -r -F complete -c x -n "__fish_x_using_subcommand test" -l build-dir -d 'Build directory, overrides `build.build-dir` in `bootstrap.toml`' -r -f -a "(__fish_complete_directories)" complete -c x -n "__fish_x_using_subcommand test" -l build -d 'host target of the stage0 compiler' -r -f diff --git a/src/etc/completions/x.ps1 b/src/etc/completions/x.ps1 index 95cee4b633625..b03acf930f70d 100644 --- a/src/etc/completions/x.ps1 +++ b/src/etc/completions/x.ps1 @@ -351,6 +351,7 @@ Register-ArgumentCompleter -Native -CommandName 'x' -ScriptBlock { [CompletionResult]::new('--compare-mode', '--compare-mode', [CompletionResultType]::ParameterName, 'mode describing what file the actual ui output will be compared to') [CompletionResult]::new('--pass', '--pass', [CompletionResultType]::ParameterName, 'force {check,build,run}-pass tests to this mode') [CompletionResult]::new('--run', '--run', [CompletionResultType]::ParameterName, 'whether to execute run-* tests') + [CompletionResult]::new('--test-codegen-backend', '--test-codegen-backend', [CompletionResultType]::ParameterName, 'Use a different codegen backend when running tests') [CompletionResult]::new('--config', '--config', [CompletionResultType]::ParameterName, 'TOML configuration file for build') [CompletionResult]::new('--build-dir', '--build-dir', [CompletionResultType]::ParameterName, 'Build directory, overrides `build.build-dir` in `bootstrap.toml`') [CompletionResult]::new('--build', '--build', [CompletionResultType]::ParameterName, 'host target of the stage0 compiler') diff --git a/src/etc/completions/x.py.fish b/src/etc/completions/x.py.fish index 6fba6a4562368..08e4cd26ce887 100644 --- a/src/etc/completions/x.py.fish +++ b/src/etc/completions/x.py.fish @@ -305,6 +305,7 @@ complete -c x.py -n "__fish_x.py_using_subcommand test" -l extra-checks -d 'comm complete -c x.py -n "__fish_x.py_using_subcommand test" -l compare-mode -d 'mode describing what file the actual ui output will be compared to' -r complete -c x.py -n "__fish_x.py_using_subcommand test" -l pass -d 'force {check,build,run}-pass tests to this mode' -r complete -c x.py -n "__fish_x.py_using_subcommand test" -l run -d 'whether to execute run-* tests' -r +complete -c x.py -n "__fish_x.py_using_subcommand test" -l test-codegen-backend -d 'Use a different codegen backend when running tests' -r complete -c x.py -n "__fish_x.py_using_subcommand test" -l config -d 'TOML configuration file for build' -r -F complete -c x.py -n "__fish_x.py_using_subcommand test" -l build-dir -d 'Build directory, overrides `build.build-dir` in `bootstrap.toml`' -r -f -a "(__fish_complete_directories)" complete -c x.py -n "__fish_x.py_using_subcommand test" -l build -d 'host target of the stage0 compiler' -r -f diff --git a/src/etc/completions/x.py.ps1 b/src/etc/completions/x.py.ps1 index 458879a17a7b9..3d95d88af4955 100644 --- a/src/etc/completions/x.py.ps1 +++ b/src/etc/completions/x.py.ps1 @@ -351,6 +351,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock { [CompletionResult]::new('--compare-mode', '--compare-mode', [CompletionResultType]::ParameterName, 'mode describing what file the actual ui output will be compared to') [CompletionResult]::new('--pass', '--pass', [CompletionResultType]::ParameterName, 'force {check,build,run}-pass tests to this mode') [CompletionResult]::new('--run', '--run', [CompletionResultType]::ParameterName, 'whether to execute run-* tests') + [CompletionResult]::new('--test-codegen-backend', '--test-codegen-backend', [CompletionResultType]::ParameterName, 'Use a different codegen backend when running tests') [CompletionResult]::new('--config', '--config', [CompletionResultType]::ParameterName, 'TOML configuration file for build') [CompletionResult]::new('--build-dir', '--build-dir', [CompletionResultType]::ParameterName, 'Build directory, overrides `build.build-dir` in `bootstrap.toml`') [CompletionResult]::new('--build', '--build', [CompletionResultType]::ParameterName, 'host target of the stage0 compiler') diff --git a/src/etc/completions/x.py.sh b/src/etc/completions/x.py.sh index e003bf7fd0b43..8ff0eaf35c89a 100644 --- a/src/etc/completions/x.py.sh +++ b/src/etc/completions/x.py.sh @@ -3875,7 +3875,7 @@ _x.py() { return 0 ;; x.py__test) - opts="-v -i -j -h --no-fail-fast --test-args --compiletest-rustc-args --no-doc --doc --bless --extra-checks --force-rerun --only-modified --compare-mode --pass --run --rustfix-coverage --no-capture --verbose --incremental --config --build-dir --build --host --target --exclude --skip --include-default-paths --rustc-error-format --on-fail --dry-run --dump-bootstrap-shims --stage --keep-stage --keep-stage-std --src --jobs --warnings --json-output --compile-time-deps --color --bypass-bootstrap-lock --rust-profile-generate --rust-profile-use --llvm-profile-use --llvm-profile-generate --enable-bolt-settings --skip-stage0-validation --reproducible-artifact --set --ci --skip-std-check-if-no-download-rustc --help [PATHS]... [ARGS]..." + opts="-v -i -j -h --no-fail-fast --test-args --compiletest-rustc-args --no-doc --doc --bless --extra-checks --force-rerun --only-modified --compare-mode --pass --run --rustfix-coverage --no-capture --test-codegen-backend --verbose --incremental --config --build-dir --build --host --target --exclude --skip --include-default-paths --rustc-error-format --on-fail --dry-run --dump-bootstrap-shims --stage --keep-stage --keep-stage-std --src --jobs --warnings --json-output --compile-time-deps --color --bypass-bootstrap-lock --rust-profile-generate --rust-profile-use --llvm-profile-use --llvm-profile-generate --enable-bolt-settings --skip-stage0-validation --reproducible-artifact --set --ci --skip-std-check-if-no-download-rustc --help [PATHS]... [ARGS]..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -3905,6 +3905,10 @@ _x.py() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; + --test-codegen-backend) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; --config) local oldifs if [ -n "${IFS+x}" ]; then diff --git a/src/etc/completions/x.py.zsh b/src/etc/completions/x.py.zsh index b82c2d65e8694..9d2d73e582ec6 100644 --- a/src/etc/completions/x.py.zsh +++ b/src/etc/completions/x.py.zsh @@ -351,6 +351,7 @@ _arguments "${_arguments_options[@]}" : \ '--compare-mode=[mode describing what file the actual ui output will be compared to]:COMPARE MODE:_default' \ '--pass=[force {check,build,run}-pass tests to this mode]:check | build | run:_default' \ '--run=[whether to execute run-* tests]:auto | always | never:_default' \ +'--test-codegen-backend=[Use a different codegen backend when running tests]:TEST_CODEGEN_BACKEND:_default' \ '--config=[TOML configuration file for build]:FILE:_files' \ '--build-dir=[Build directory, overrides \`build.build-dir\` in \`bootstrap.toml\`]:DIR:_files -/' \ '--build=[host target of the stage0 compiler]:BUILD:' \ diff --git a/src/etc/completions/x.sh b/src/etc/completions/x.sh index c2cb771002017..c1b73fb7c9e34 100644 --- a/src/etc/completions/x.sh +++ b/src/etc/completions/x.sh @@ -3875,7 +3875,7 @@ _x() { return 0 ;; x__test) - opts="-v -i -j -h --no-fail-fast --test-args --compiletest-rustc-args --no-doc --doc --bless --extra-checks --force-rerun --only-modified --compare-mode --pass --run --rustfix-coverage --no-capture --verbose --incremental --config --build-dir --build --host --target --exclude --skip --include-default-paths --rustc-error-format --on-fail --dry-run --dump-bootstrap-shims --stage --keep-stage --keep-stage-std --src --jobs --warnings --json-output --compile-time-deps --color --bypass-bootstrap-lock --rust-profile-generate --rust-profile-use --llvm-profile-use --llvm-profile-generate --enable-bolt-settings --skip-stage0-validation --reproducible-artifact --set --ci --skip-std-check-if-no-download-rustc --help [PATHS]... [ARGS]..." + opts="-v -i -j -h --no-fail-fast --test-args --compiletest-rustc-args --no-doc --doc --bless --extra-checks --force-rerun --only-modified --compare-mode --pass --run --rustfix-coverage --no-capture --test-codegen-backend --verbose --incremental --config --build-dir --build --host --target --exclude --skip --include-default-paths --rustc-error-format --on-fail --dry-run --dump-bootstrap-shims --stage --keep-stage --keep-stage-std --src --jobs --warnings --json-output --compile-time-deps --color --bypass-bootstrap-lock --rust-profile-generate --rust-profile-use --llvm-profile-use --llvm-profile-generate --enable-bolt-settings --skip-stage0-validation --reproducible-artifact --set --ci --skip-std-check-if-no-download-rustc --help [PATHS]... [ARGS]..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -3905,6 +3905,10 @@ _x() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; + --test-codegen-backend) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; --config) local oldifs if [ -n "${IFS+x}" ]; then diff --git a/src/etc/completions/x.zsh b/src/etc/completions/x.zsh index 49139e70f7fb2..29237ef9bf8fd 100644 --- a/src/etc/completions/x.zsh +++ b/src/etc/completions/x.zsh @@ -351,6 +351,7 @@ _arguments "${_arguments_options[@]}" : \ '--compare-mode=[mode describing what file the actual ui output will be compared to]:COMPARE MODE:_default' \ '--pass=[force {check,build,run}-pass tests to this mode]:check | build | run:_default' \ '--run=[whether to execute run-* tests]:auto | always | never:_default' \ +'--test-codegen-backend=[Use a different codegen backend when running tests]:TEST_CODEGEN_BACKEND:_default' \ '--config=[TOML configuration file for build]:FILE:_files' \ '--build-dir=[Build directory, overrides \`build.build-dir\` in \`bootstrap.toml\`]:DIR:_files -/' \ '--build=[host target of the stage0 compiler]:BUILD:' \ diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index 2d49b1a7097d0..7fc80c1edb158 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -692,7 +692,9 @@ pub struct Config { pub minicore_path: Utf8PathBuf, /// Current codegen backend used. - pub codegen_backend: CodegenBackend, + pub default_codegen_backend: CodegenBackend, + /// Name/path of the backend to use instead of `default_codegen_backend`. + pub override_codegen_backend: Option, } impl Config { @@ -796,7 +798,8 @@ impl Config { profiler_runtime: Default::default(), diff_command: Default::default(), minicore_path: Default::default(), - codegen_backend: CodegenBackend::Llvm, + default_codegen_backend: CodegenBackend::Llvm, + override_codegen_backend: None, } } diff --git a/src/tools/compiletest/src/directives.rs b/src/tools/compiletest/src/directives.rs index 13e694b7f03ca..00007aa1d6683 100644 --- a/src/tools/compiletest/src/directives.rs +++ b/src/tools/compiletest/src/directives.rs @@ -1624,7 +1624,7 @@ fn ignore_backends( } } }) { - if config.codegen_backend == backend { + if config.default_codegen_backend == backend { return IgnoreDecision::Ignore { reason: format!("{} backend is marked as ignore", backend.as_str()), }; @@ -1651,12 +1651,12 @@ fn needs_backends( panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}") } }) - .any(|backend| config.codegen_backend == backend) + .any(|backend| config.default_codegen_backend == backend) { return IgnoreDecision::Ignore { reason: format!( "{} backend is not part of required backends", - config.codegen_backend.as_str() + config.default_codegen_backend.as_str() ), }; } diff --git a/src/tools/compiletest/src/lib.rs b/src/tools/compiletest/src/lib.rs index f5409e783418b..469dd68207edc 100644 --- a/src/tools/compiletest/src/lib.rs +++ b/src/tools/compiletest/src/lib.rs @@ -212,9 +212,15 @@ pub fn parse_config(args: Vec) -> Config { ) .optopt( "", - "codegen-backend", + "default-codegen-backend", "the codegen backend currently used", "CODEGEN BACKEND NAME", + ) + .optopt( + "", + "override-codegen-backend", + "the codegen backend to use instead of the default one", + "CODEGEN BACKEND [NAME | PATH]", ); let (argv0, args_) = args.split_first().unwrap(); @@ -276,14 +282,17 @@ pub fn parse_config(args: Vec) -> Config { || directives::extract_llvm_version_from_binary(&matches.opt_str("llvm-filecheck")?), ); - let codegen_backend = match matches.opt_str("codegen-backend").as_deref() { + let default_codegen_backend = match matches.opt_str("default-codegen-backend").as_deref() { Some(backend) => match CodegenBackend::try_from(backend) { Ok(backend) => backend, - Err(error) => panic!("invalid value `{backend}` for `--codegen-backend`: {error}"), + Err(error) => { + panic!("invalid value `{backend}` for `--defalt-codegen-backend`: {error}") + } }, // By default, it's always llvm. None => CodegenBackend::Llvm, }; + let override_codegen_backend = matches.opt_str("override-codegen-backend"); let run_ignored = matches.opt_present("ignored"); let with_rustc_debug_assertions = matches.opt_present("with-rustc-debug-assertions"); @@ -472,7 +481,8 @@ pub fn parse_config(args: Vec) -> Config { minicore_path: opt_path(matches, "minicore-path"), - codegen_backend, + default_codegen_backend, + override_codegen_backend, } } @@ -812,13 +822,13 @@ fn collect_tests_from_dir( && let Some(Utf8Component::Normal(parent)) = components.next() && parent == "tests" && let Ok(backend) = CodegenBackend::try_from(backend) - && backend != cx.config.codegen_backend + && backend != cx.config.default_codegen_backend { // We ignore asm tests which don't match the current codegen backend. warning!( "Ignoring tests in `{dir}` because they don't match the configured codegen \ backend (`{}`)", - cx.config.codegen_backend.as_str(), + cx.config.default_codegen_backend.as_str(), ); return Ok(TestCollector::new()); } diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 821cb12864764..2402ed9a950f2 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1558,6 +1558,11 @@ impl<'test> TestCx<'test> { rustc.arg("--sysroot").arg(&self.config.sysroot_base); } + // If the provided codegen backend is not LLVM, we need to pass it. + if let Some(ref backend) = self.config.override_codegen_backend { + rustc.arg(format!("-Zcodegen-backend={}", backend)); + } + // Optionally prevent default --target if specified in test compile-flags. let custom_target = self.props.compile_flags.iter().any(|x| x.starts_with("--target"));