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

redesign stage 0 std #119899

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 1 addition & 3 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(exhaustive_patterns))]
#![cfg_attr(bootstrap, feature(platform_intrinsics))]
#![cfg_attr(not(bootstrap), feature(min_exhaustive_patterns))]
#![feature(min_exhaustive_patterns)]
#![feature(alloc_error_handler)]
#![feature(allocator_internals)]
#![feature(allow_internal_unsafe)]
Expand Down
5 changes: 2 additions & 3 deletions library/std/src/sys/thread_local/static_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ pub macro thread_local_inner {
(@key $t:ty, const $init:expr) => {{
#[inline] // see comments below
#[deny(unsafe_op_in_unsafe_fn)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[cfg_attr(bootstrap, allow(static_mut_ref))]
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
Copy link
Contributor

Choose a reason for hiding this comment

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

typo nit:

Suggested change
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint

#[allow(static_mut_refs)]
unsafe fn __getit(
_init: $crate::option::Option<&mut $crate::option::Option<$t>>,
) -> $crate::option::Option<&'static $t> {
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/defaults/config.compiler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ lto = "off"
frame-pointers = true
# Build the llvm-bitcode-linker as it is required for running nvptx tests
llvm-bitcode-linker = true
# Download rustc from CI instead of building it from source.
# This cuts compile times by almost 60x, but means there is a sharp "cliff" in compile times when modifying the compiler.
# It also takes slightly longer to start an initial build, since the toolchain has to be re-downloaded after each rebase.
download-rustc = "if-unchanged"

[llvm]
# Having this set to true disrupts compiler development workflows for people who use `llvm.download-ci-llvm = true`
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/defaults/config.library.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ incremental = true
lto = "off"
# Build the llvm-bitcode-linker as it is required for running nvptx tests
llvm-bitcode-linker = true
# Download rustc from CI instead of building it from source.
# This cuts compile times by almost 60x, but means there is a sharp "cliff" in compile times when modifying the compiler.
# It also takes slightly longer to start an initial build, since the toolchain has to be re-downloaded after each rebase.
download-rustc = "if-unchanged"

[llvm]
# Will download LLVM from CI if available on your platform.
Expand Down
19 changes: 18 additions & 1 deletion src/bootstrap/src/core/build_steps/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Implementation of compiling the compiler and standard library, in "check"-based modes.

use super::compile;
use crate::core::build_steps::compile::{
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo,
};
Expand Down Expand Up @@ -96,10 +97,18 @@ impl Step for Std {
const DEFAULT: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.crate_or_deps("sysroot").path("library")
let stage = run.builder.top_stage;
run.crate_or_deps("sysroot").path("library").default_condition(stage != 0)
}

fn make_run(run: RunConfig<'_>) {
if run.builder.top_stage == 0 {
eprintln!(
"check --stage 0 std is no longer supported - try --stage 1 or enabling rust.download-rustc"
);
build_helper::util::detail_exit(1, false);
}

let crates = run.make_run_crates(Alias::Library);
run.builder.ensure(Std { target: run.target, crates });
}
Expand All @@ -110,6 +119,14 @@ impl Step for Std {
let target = self.target;
let compiler = builder.compiler(builder.top_stage, builder.config.build);

if builder.top_stage == 0 {
// Reuse the beta compiler's libstd
builder.ensure(compile::Std::new(compiler, target));
return;
}

builder.update_submodule(&Path::new("library").join("stdarch"));

let mut cargo = builder::Cargo::new(
builder,
compiler,
Expand Down
61 changes: 35 additions & 26 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ impl Step for Std {
compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
target: run.target,
crates,
force_recompile: false,
// If someone explicitly says `x build std`, recompile from source so they see their changes
force_recompile: true,
extra_rust_args: &[],
is_for_mir_opt_tests: false,
});
Expand All @@ -151,6 +152,13 @@ impl Step for Std {
let target = self.target;
let compiler = self.compiler;

// We already have std ready to be used for stage 0.
if compiler.stage == 0 {
builder.ensure(StdLink::from_std(self, compiler));

return;
}

// When using `download-rustc`, we already have artifacts for the host available. Don't
// recompile them.
if builder.download_rustc() && target == builder.build.build
Expand Down Expand Up @@ -190,8 +198,19 @@ impl Step for Std {

let mut target_deps = builder.ensure(StartupObjects { compiler, target });

let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
if compiler_to_use != compiler {
let mut compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);

if compiler_to_use != compiler
// Never uplift std unless we have compiled stage 2; if stage 2 is compiled,
// uplift it from there.
//
// FIXME: improve `fn compiler_for` to avoid adding stage condition here.
&& compiler.stage > 2
{
if compiler.stage > 2 {
compiler_to_use.stage = 2;
}

builder.ensure(Std::new(compiler_to_use, target));
let msg = if compiler_to_use.host == target {
format!(
Expand Down Expand Up @@ -618,18 +637,16 @@ impl Step for StdLink {
(libdir, hostdir)
};

add_to_sysroot(builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
let is_downloaded_beta_stage0 = builder
.build
.config
.initial_rustc
.starts_with(builder.out.join(&compiler.host.triple).join("stage0/bin"));

// Special case for stage0, to make `rustup toolchain link` and `x dist --stage 0`
// work for stage0-sysroot. We only do this if the stage0 compiler comes from beta,
// and is not set to a custom path.
if compiler.stage == 0
&& builder
.build
.config
.initial_rustc
.starts_with(builder.out.join(compiler.host.triple).join("stage0/bin"))
{
if compiler.stage == 0 && is_downloaded_beta_stage0 {
// Copy bin files from stage0/bin to stage0-sysroot/bin
let sysroot = builder.out.join(compiler.host.triple).join("stage0-sysroot");

Expand All @@ -640,17 +657,8 @@ impl Step for StdLink {
builder.cp_link_r(&stage0_bin_dir, &sysroot_bin_dir);

// Copy all *.so files from stage0/lib to stage0-sysroot/lib
let stage0_lib_dir = builder.out.join(host).join("stage0/lib");
if let Ok(files) = fs::read_dir(stage0_lib_dir) {
for file in files {
let file = t!(file);
let path = file.path();
if path.is_file() && is_dylib(&file.file_name().into_string().unwrap()) {
builder
.copy_link(&path, &sysroot.join("lib").join(path.file_name().unwrap()));
}
}
}
let stage0_lib_dir = builder.out.join(&host).join("stage0/lib");
builder.cp_link_r(&stage0_lib_dir, &sysroot.join("lib"));

// Copy codegen-backends from stage0
let sysroot_codegen_backends = builder.sysroot_codegen_backends(compiler);
Expand All @@ -664,6 +672,11 @@ impl Step for StdLink {
if stage0_codegen_backends.exists() {
builder.cp_link_r(&stage0_codegen_backends, &sysroot_codegen_backends);
}
} else if compiler.stage == 0 {
let sysroot = builder.out.join(&compiler.host.triple).join("stage0-sysroot");
builder.cp_link_r(&builder.initial_sysroot.join("lib"), &sysroot.join("lib"));
} else {
add_to_sysroot(builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
}
}
}
Expand Down Expand Up @@ -776,10 +789,6 @@ impl Step for StartupObjects {
if !up_to_date(src_file, dst_file) {
let mut cmd = Command::new(&builder.initial_rustc);
cmd.env("RUSTC_BOOTSTRAP", "1");
if !builder.local_rebuild {
// a local_rebuild compiler already has stage1 features
cmd.arg("--cfg").arg("bootstrap");
}
builder.run(
cmd.arg("--target")
.arg(target.rustc_target_arg())
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ impl Step for Tidy {
if builder.config.channel == "dev" || builder.config.channel == "nightly" {
builder.info("fmt check");
if builder.initial_rustfmt().is_none() {
let inferred_rustfmt_dir = builder.initial_rustc.parent().unwrap();
let inferred_rustfmt_dir = builder.initial_sysroot.join("bin");
eprintln!(
"\
ERROR: no `rustfmt` binary found in {PATH}
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ impl<'a> Builder<'a> {
}

pub fn cargo_clippy_cmd(&self, run_compiler: Compiler) -> Command {
let initial_sysroot_bin = self.initial_rustc.parent().unwrap();
let initial_sysroot_bin = self.initial_sysroot.join("bin");
// Set PATH to include the sysroot bin dir so clippy can find cargo.
// FIXME: once rust-clippy#11944 lands on beta, set `CARGO` directly instead.
let path = t!(env::join_paths(
Expand Down
5 changes: 4 additions & 1 deletion src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,10 @@ impl Config {
let download_rustc = config.download_rustc_commit.is_some();
// See https://github.com/rust-lang/compiler-team/issues/326
config.stage = match config.cmd {
Subcommand::Check { .. } => flags.stage.or(check_stage).unwrap_or(0),
// `x check std` does nothing in stage 0; use a higher stage by default if it's fast
Subcommand::Check { .. } => {
flags.stage.or(check_stage).unwrap_or(if download_rustc { 2 } else { 0 })
}
// `download-rustc` only has a speed-up for stage2 builds. Default to stage2 unless explicitly overridden.
Subcommand::Doc { .. } => {
flags.stage.or(doc_stage).unwrap_or(if download_rustc { 2 } else { 0 })
Expand Down
2 changes: 1 addition & 1 deletion src/ci/docker/host-x86_64/i686-gnu-nopt/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ RUN echo "[rust]" > /config/nopt-std-config.toml
RUN echo "optimize = false" >> /config/nopt-std-config.toml

ENV RUST_CONFIGURE_ARGS --build=i686-unknown-linux-gnu --disable-optimize-tests
ENV SCRIPT python3 ../x.py test --stage 0 --config /config/nopt-std-config.toml library/std \
ENV SCRIPT python3 ../x.py test --stage 1 --config /config/nopt-std-config.toml library/std \
&& python3 ../x.py --stage 2 test
2 changes: 1 addition & 1 deletion src/ci/docker/host-x86_64/mingw-check/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \
python3 ../x.py clippy compiler -Aclippy::all -Dclippy::correctness && \
python3 ../x.py build --stage 0 src/tools/build-manifest && \
python3 ../x.py test --stage 0 src/tools/compiletest && \
python3 ../x.py test --stage 0 core alloc std test proc_macro && \
python3 ../x.py test --stage 1 core alloc std test proc_macro && \
# Build both public and internal documentation.
RUSTDOCFLAGS=\"--document-private-items --document-hidden-items\" python3 ../x.py doc --stage 0 library && \
mkdir -p /checkout/obj/staging/doc && \
Expand Down
2 changes: 1 addition & 1 deletion src/ci/docker/host-x86_64/x86_64-gnu-nopt/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ RUN echo "optimize = false" >> /config/nopt-std-config.toml
ENV RUST_CONFIGURE_ARGS --build=x86_64-unknown-linux-gnu \
--disable-optimize-tests \
--set rust.test-compare-mode
ENV SCRIPT python3 ../x.py test --stage 0 --config /config/nopt-std-config.toml library/std \
ENV SCRIPT python3 ../x.py test --stage 1 --config /config/nopt-std-config.toml library/std \
&& python3 ../x.py --stage 2 test
Loading