Skip to content

Commit 6a409dd

Browse files
jyn514onur-ozkan
authored andcommitted
add a new optimized_compiler_builtins option
in particular, this makes the `c` feature for compiler-builtins an explicit opt-in, rather than silently detected by whether `llvm-project` is checked out on disk. exposing this is necessary because the `cc` crate doesn't support cross-compiling to MSVC, and we want people to be able to run `x check --target foo` regardless of whether they have a c toolchain available. this also uses the new option in CI, where we *do* want to optimize compiler_builtins. the new option is off by default for the `dev` channel and on otherwise.
1 parent 1490bba commit 6a409dd

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

config.example.toml

+8
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@
339339
# on this runtime, such as `-C profile-generate` or `-C instrument-coverage`).
340340
#profiler = false
341341

342+
# Use the optimized LLVM C intrinsics for `compiler_builtins`, rather than Rust intrinsics.
343+
# Requires the LLVM submodule to be managed by bootstrap (i.e. not external) so that `compiler-rt`
344+
# sources are available.
345+
#
346+
# Setting this to `false` generates slower code, but removes the requirement for a C toolchain in
347+
# order to run `x check`.
348+
#optimized-compiler-builtins = if rust.channel == "dev" { false } else { true }
349+
342350
# Indicates whether the native libraries linked into Cargo will be statically
343351
# linked or not.
344352
#cargo-native-static = false

src/bootstrap/src/core/build_steps/compile.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,7 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
382382

383383
// Determine if we're going to compile in optimized C intrinsics to
384384
// the `compiler-builtins` crate. These intrinsics live in LLVM's
385-
// `compiler-rt` repository, but our `src/llvm-project` submodule isn't
386-
// always checked out, so we need to conditionally look for this. (e.g. if
387-
// an external LLVM is used we skip the LLVM submodule checkout).
385+
// `compiler-rt` repository.
388386
//
389387
// Note that this shouldn't affect the correctness of `compiler-builtins`,
390388
// but only its speed. Some intrinsics in C haven't been translated to Rust
@@ -395,8 +393,21 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
395393
// If `compiler-rt` is available ensure that the `c` feature of the
396394
// `compiler-builtins` crate is enabled and it's configured to learn where
397395
// `compiler-rt` is located.
398-
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
399-
let compiler_builtins_c_feature = if compiler_builtins_root.exists() {
396+
let compiler_builtins_c_feature = if builder.config.optimized_compiler_builtins {
397+
// NOTE: this interacts strangely with `llvm-has-rust-patches`. In that case, we enforce `submodules = false`, so this is a no-op.
398+
// But, the user could still decide to manually use an in-tree submodule.
399+
//
400+
// NOTE: if we're using system llvm, we'll end up building a version of `compiler-rt` that doesn't match the LLVM we're linking to.
401+
// That's probably ok? At least, the difference wasn't enforced before. There's a comment in
402+
// the compiler_builtins build script that makes me nervous, though:
403+
// https://github.com/rust-lang/compiler-builtins/blob/31ee4544dbe47903ce771270d6e3bea8654e9e50/build.rs#L575-L579
404+
builder.update_submodule(&Path::new("src").join("llvm-project"));
405+
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
406+
if !compiler_builtins_root.exists() {
407+
panic!(
408+
"need LLVM sources available to build `compiler-rt`, but they weren't present; consider enabling `build.submodules = true` or disabling `optimized-compiler-builtins`"
409+
);
410+
}
400411
// Note that `libprofiler_builtins/build.rs` also computes this so if
401412
// you're changing something here please also change that.
402413
cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);

src/bootstrap/src/core/config/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ pub struct Config {
178178
pub patch_binaries_for_nix: Option<bool>,
179179
pub stage0_metadata: Stage0Metadata,
180180
pub android_ndk: Option<PathBuf>,
181+
/// Whether to use the `c` feature of the `compiler_builtins` crate.
182+
pub optimized_compiler_builtins: bool,
181183

182184
pub stdout_is_tty: bool,
183185
pub stderr_is_tty: bool,
@@ -848,6 +850,7 @@ define_config! {
848850
// NOTE: only parsed by bootstrap.py, `--feature build-metrics` enables metrics unconditionally
849851
metrics: Option<bool> = "metrics",
850852
android_ndk: Option<PathBuf> = "android-ndk",
853+
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
851854
}
852855
}
853856

@@ -1396,6 +1399,7 @@ impl Config {
13961399
// This field is only used by bootstrap.py
13971400
metrics: _,
13981401
android_ndk,
1402+
optimized_compiler_builtins,
13991403
} = toml.build.unwrap_or_default();
14001404

14011405
if let Some(file_build) = build {
@@ -1916,6 +1920,8 @@ impl Config {
19161920
config.rust_debuginfo_level_std = with_defaults(debuginfo_level_std);
19171921
config.rust_debuginfo_level_tools = with_defaults(debuginfo_level_tools);
19181922
config.rust_debuginfo_level_tests = debuginfo_level_tests.unwrap_or(DebuginfoLevel::None);
1923+
config.optimized_compiler_builtins =
1924+
optimized_compiler_builtins.unwrap_or(config.channel != "dev");
19191925

19201926
let download_rustc = config.download_rustc_commit.is_some();
19211927
// See https://github.com/rust-lang/compiler-team/issues/326

src/bootstrap/src/utils/change_tracker.rs

+5
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
106106
severity: ChangeSeverity::Info,
107107
summary: "The dist.missing-tools config option was deprecated, as it was unused. If you are using it, remove it from your config, it will be removed soon.",
108108
},
109+
ChangeInfo {
110+
change_id: 102579,
111+
severity: ChangeSeverity::Warning,
112+
summary: "A new `optimized-compiler-builtins` option has been introduced. Whether to build llvm's `compiler-rt` from source is no longer implicitly controlled by git state. See the PR for more details.",
113+
},
109114
];

src/ci/docker/host-x86_64/disabled/dist-x86_64-haiku/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ ENV RUST_CONFIGURE_ARGS --disable-jemalloc \
4747
--set=$TARGET.cc=x86_64-unknown-haiku-gcc \
4848
--set=$TARGET.cxx=x86_64-unknown-haiku-g++ \
4949
--set=$TARGET.llvm-config=/bin/llvm-config-haiku
50+
ENV EXTERNAL_LLVM 1
51+
5052
ENV SCRIPT python3 ../x.py dist --host=$HOST --target=$HOST

src/ci/docker/host-x86_64/x86_64-gnu-llvm-17/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ RUN sh /scripts/sccache.sh
4040
# We are disabling CI LLVM since this builder is intentionally using a host
4141
# LLVM, rather than the typical src/llvm-project LLVM.
4242
ENV NO_DOWNLOAD_CI_LLVM 1
43+
ENV EXTERNAL_LLVM 1
4344

4445
# Using llvm-link-shared due to libffi issues -- see #34486
4546
ENV RUST_CONFIGURE_ARGS \

src/ci/run.sh

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ fi
8080
# space required for CI artifacts.
8181
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --dist-compression-formats=xz"
8282

83+
# Enable the `c` feature for compiler_builtins, but only when the `compiler-rt` source is available
84+
# (to avoid spending a lot of time cloning llvm)
85+
if [ "$EXTERNAL_LLVM" = "" ]; then
86+
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set build.optimized-compiler-builtins"
87+
elif [ "$DEPLOY$DEPLOY_ALT" = "1" ]; then
88+
echo "error: dist builds should always use optimized compiler-rt!" >&2
89+
exit 1
90+
fi
91+
8392
if [ "$DIST_SRC" = "" ]; then
8493
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-dist-src"
8594
fi

0 commit comments

Comments
 (0)