Skip to content

Add new --test-codegen-backend bootstrap option #145256

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 2 commits 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
15 changes: 14 additions & 1 deletion src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tracing::{instrument, span};

use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags};
use crate::core::build_steps::tool::{RustcPrivateCompilers, SourceType, copy_lld_artifacts};
use crate::core::build_steps::{dist, llvm};
use crate::core::build_steps::{dist, gcc, llvm};
use crate::core::builder;
use crate::core::builder::{
Builder, Cargo, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description,
Expand Down Expand Up @@ -2348,6 +2348,19 @@ impl Step for Assemble {
dist::maybe_install_llvm_runtime(builder, target_compiler.host, &sysroot);
dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);

if builder.config.gcc_enabled(target_compiler.host) {
let gcc::GccOutput { mut libgccjit } =
builder.ensure(gcc::Gcc { target: target_compiler.host });
let dst_libdir = sysroot.join("lib");
builder.install(&libgccjit, &dst_libdir, FileType::NativeLibrary);
if let Some(file_name) = libgccjit.file_name() {
let mut file_name = file_name.to_os_string();
file_name.push(".0");
libgccjit.set_file_name(file_name);
builder.install(&libgccjit, &dst_libdir, FileType::NativeLibrary);
}
}

// Link the compiler binary itself into place
let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host);
let rustc = out_dir.join(exe("rustc-main", host));
Expand Down
16 changes: 15 additions & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,21 @@ 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) {
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 which codegen backend is used by default by the compiler.
// It is used to e.g. ignore tests that don't support that codegen backend.
cmd.arg("--codegen-backend").arg(codegen_backend.name());
} else if let Some(codegen_backend) = builder.config.default_codegen_backend(compiler.host)
{
// Tells compiletest which codegen backend is used by default by the compiler.
// It is used to e.g. ignore tests that don't support that codegen backend.
cmd.arg("--codegen-backend").arg(codegen_backend.name());
Expand Down
17 changes: 16 additions & 1 deletion src/bootstrap/src/core/builder/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,16 @@ impl Builder<'_> {
rustflags.arg("--cfg=bootstrap");
}

if self
.config
.default_codegen_backend(compiler.host)
.is_some_and(|backend| backend.is_gcc())
{
let sysroot = sysroot.join("lib");
let sysroot_str = sysroot.as_os_str().to_str().expect("sysroot should be UTF-8");
cargo.env("LD_LIBRARY_PATH", sysroot_str);
Copy link
Member Author

Choose a reason for hiding this comment

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

Sadly I still need to change this env variable to be able to build the compiler with the GCC backend. However, no need anymore for the tests. \o/

}

if cmd_kind == Kind::Clippy {
// clippy overwrites sysroot if we pass it to cargo.
// Pass it directly to clippy instead.
Expand Down Expand Up @@ -1292,7 +1302,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}"));
}
Expand Down
8 changes: 6 additions & 2 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1751,8 +1751,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<CodegenBackendKind> {
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 {
Expand All @@ -1774,6 +1774,10 @@ impl Config {
self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Llvm)
}

pub fn gcc_enabled(&self, target: TargetSelection) -> bool {
self.enabled_codegen_backends(target).contains(&CodegenBackendKind::Gcc)
}

pub fn llvm_libunwind(&self, target: TargetSelection) -> LlvmLibunwind {
self.target_config
.get(&target)
Expand Down
12 changes: 11 additions & 1 deletion src/bootstrap/src/core/config/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -413,6 +413,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<CodegenBackendKind>,
},
/// Build and run some test suites *in Miri*
Miri {
Expand Down Expand Up @@ -639,6 +642,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
Expand Down
14 changes: 14 additions & 0 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ impl CodegenBackendKind {
}
}

impl std::str::FromStr for CodegenBackendKind {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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).
Expand Down
5 changes: 5 additions & 0 deletions src/bootstrap/src/utils/change_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
severity: ChangeSeverity::Warning,
summary: "It is no longer possible to `x doc` with stage 0. All doc commands have to be on stage 1+.",
},
ChangeInfo {
change_id: 145256,
severity: ChangeSeverity::Info,
summary: "Added `--test-codegen-backend` CLI option for tests",
},
];
1 change: 1 addition & 0 deletions src/etc/completions/x.fish
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,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
Expand Down
1 change: 1 addition & 0 deletions src/etc/completions/x.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,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')
Expand Down
1 change: 1 addition & 0 deletions src/etc/completions/x.py.fish
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,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
Expand Down
1 change: 1 addition & 0 deletions src/etc/completions/x.py.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,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')
Expand Down
6 changes: 5 additions & 1 deletion src/etc/completions/x.py.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/etc/completions/x.py.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,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:' \
Expand Down
6 changes: 5 additions & 1 deletion src/etc/completions/x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/etc/completions/x.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,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:' \
Expand Down
Loading