Skip to content

Commit 8ce8e15

Browse files
Paul Murphypmur
authored andcommitted
Allow linking a prebuilt optimized compiler-rt builtins library
Extend the <target>.optimized-compiler-builtins bootstrap option to accept a path to a prebuilt compiler-rt builtins library, and update compiler-builtins to enable optimized builtins without building compiler-rt builtins.
1 parent 2e53675 commit 8ce8e15

File tree

6 files changed

+85
-34
lines changed

6 files changed

+85
-34
lines changed

bootstrap.example.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,9 @@
10301030
# sources are available.
10311031
#
10321032
# Setting this to `false` generates slower code, but removes the requirement for a C toolchain in
1033-
# order to run `x check`.
1033+
# order to run `x check`. This may also be given a path to an existing build of the builtins
1034+
# runtime library from LLVM's compiler-rt. This option will override the same option under [build]
1035+
# section.
10341036
#optimized-compiler-builtins = build.optimized-compiler-builtins (bool)
10351037

10361038
# Link the compiler and LLVM against `jemalloc` instead of the default libc allocator.

library/compiler-builtins/compiler-builtins/build.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,20 +547,28 @@ mod c {
547547
sources.extend(&[("__emutls_get_address", "emutls.c")]);
548548
}
549549

550+
// Optionally, link against a prebuilt compiler-rt library to supply
551+
// optimized intrinsics instead of compiling a subset of compiler-rt
552+
// from source.
553+
let link_against_prebuilt_rt = env::var_os("LLVM_COMPILER_RT_LIB").is_some();
554+
550555
// When compiling the C code we require the user to tell us where the
551556
// source code is, and this is largely done so when we're compiling as
552557
// part of rust-lang/rust we can use the same llvm-project repository as
553558
// rust-lang/rust.
554559
let root = match env::var_os("RUST_COMPILER_RT_ROOT") {
555560
Some(s) => PathBuf::from(s),
561+
// If a prebuild libcompiler-rt is provided, set a valid
562+
// path to simplify later logic. Nothing should be compiled.
563+
None if link_against_prebuilt_rt => PathBuf::new(),
556564
None => {
557565
panic!(
558566
"RUST_COMPILER_RT_ROOT is not set. You may need to run \
559567
`ci/download-compiler-rt.sh`."
560568
);
561569
}
562570
};
563-
if !root.exists() {
571+
if !link_against_prebuilt_rt && !root.exists() {
564572
panic!("RUST_COMPILER_RT_ROOT={} does not exist", root.display());
565573
}
566574

@@ -576,7 +584,7 @@ mod c {
576584
let src_dir = root.join("lib/builtins");
577585
if target.arch == "aarch64" && target.env != "msvc" && target.os != "uefi" {
578586
// See below for why we're building these as separate libraries.
579-
build_aarch64_out_of_line_atomics_libraries(&src_dir, cfg);
587+
build_aarch64_out_of_line_atomics_libraries(&src_dir, cfg, link_against_prebuilt_rt);
580588

581589
// Some run-time CPU feature detection is necessary, as well.
582590
let cpu_model_src = if src_dir.join("cpu_model.c").exists() {
@@ -590,20 +598,43 @@ mod c {
590598
let mut added_sources = HashSet::new();
591599
for (sym, src) in sources.map.iter() {
592600
let src = src_dir.join(src);
593-
if added_sources.insert(src.clone()) {
601+
if !link_against_prebuilt_rt && added_sources.insert(src.clone()) {
594602
cfg.file(&src);
595603
println!("cargo:rerun-if-changed={}", src.display());
596604
}
597605
println!("cargo:rustc-cfg={}=\"optimized-c\"", sym);
598606
}
599607

600-
cfg.compile("libcompiler-rt.a");
608+
if link_against_prebuilt_rt {
609+
let rt_builtins_ext = PathBuf::from(env::var_os("LLVM_COMPILER_RT_LIB").unwrap());
610+
if !rt_builtins_ext.exists() {
611+
panic!(
612+
"LLVM_COMPILER_RT_LIB={} does not exist",
613+
rt_builtins_ext.display()
614+
);
615+
}
616+
if let Some(dir) = rt_builtins_ext.parent() {
617+
println!("cargo::rustc-link-search=native={}", dir.display());
618+
}
619+
println!(
620+
"cargo::rustc-link-lib=static:+verbatim={}",
621+
rt_builtins_ext.to_str().unwrap()
622+
);
623+
} else {
624+
cfg.compile("libcompiler-rt.a");
625+
}
601626
}
602627

603-
fn build_aarch64_out_of_line_atomics_libraries(builtins_dir: &Path, cfg: &mut cc::Build) {
628+
fn build_aarch64_out_of_line_atomics_libraries(
629+
builtins_dir: &Path,
630+
cfg: &mut cc::Build,
631+
link_against_prebuilt_rt: bool,
632+
) {
604633
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
605634
let outlined_atomics_file = builtins_dir.join("aarch64").join("lse.S");
606-
println!("cargo:rerun-if-changed={}", outlined_atomics_file.display());
635+
if !link_against_prebuilt_rt {
636+
println!("cargo:rerun-if-changed={}", outlined_atomics_file.display());
637+
}
607638

608639
cfg.include(&builtins_dir);
609640

@@ -616,6 +647,13 @@ mod c {
616647
for (model_number, model_name) in
617648
&[(1, "relax"), (2, "acq"), (3, "rel"), (4, "acq_rel")]
618649
{
650+
let sym = format!("__aarch64_{}{}_{}", instruction_type, size, model_name);
651+
println!("cargo:rustc-cfg={}=\"optimized-c\"", sym);
652+
653+
if link_against_prebuilt_rt {
654+
continue;
655+
}
656+
619657
// The original compiler-rt build system compiles the same
620658
// source file multiple times with different compiler
621659
// options. Here we do something slightly different: we
@@ -639,9 +677,6 @@ mod c {
639677
.unwrap();
640678
drop(file);
641679
cfg.file(path);
642-
643-
let sym = format!("__aarch64_{}{}_{}", instruction_type, size, model_name);
644-
println!("cargo:rustc-cfg={}=\"optimized-c\"", sym);
645680
}
646681
}
647682
}

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -565,25 +565,31 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
565565
// `compiler-builtins` crate is enabled and it's configured to learn where
566566
// `compiler-rt` is located.
567567
let compiler_builtins_c_feature = if builder.config.optimized_compiler_builtins(target) {
568-
// NOTE: this interacts strangely with `llvm-has-rust-patches`. In that case, we enforce `submodules = false`, so this is a no-op.
569-
// But, the user could still decide to manually use an in-tree submodule.
570-
//
571-
// 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.
572-
// That's probably ok? At least, the difference wasn't enforced before. There's a comment in
573-
// the compiler_builtins build script that makes me nervous, though:
574-
// https://github.com/rust-lang/compiler-builtins/blob/31ee4544dbe47903ce771270d6e3bea8654e9e50/build.rs#L575-L579
575-
builder.require_submodule(
576-
"src/llvm-project",
577-
Some(
578-
"The `build.optimized-compiler-builtins` config option \
579-
requires `compiler-rt` sources from LLVM.",
580-
),
581-
);
582-
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
583-
assert!(compiler_builtins_root.exists());
584-
// The path to `compiler-rt` is also used by `profiler_builtins` (above),
585-
// so if you're changing something here please also change that as appropriate.
586-
cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);
568+
if let Some(path) = builder.config.optimized_compiler_builtins_path(target) {
569+
cargo.env("LLVM_COMPILER_RT_LIB", path);
570+
} else {
571+
// NOTE: this interacts strangely with `llvm-has-rust-patches`. In that case, we enforce
572+
// `submodules = false`, so this is a no-op. But, the user could still decide to
573+
// manually use an in-tree submodule.
574+
//
575+
// NOTE: if we're using system llvm, we'll end up building a version of `compiler-rt`
576+
// that doesn't match the LLVM we're linking to. That's probably ok? At least, the
577+
// difference wasn't enforced before. There's a comment in the compiler_builtins build
578+
// script that makes me nervous, though:
579+
// https://github.com/rust-lang/compiler-builtins/blob/31ee4544dbe47903ce771270d6e3bea8654e9e50/build.rs#L575-L579
580+
builder.require_submodule(
581+
"src/llvm-project",
582+
Some(
583+
"The `build.optimized-compiler-builtins` config option \
584+
requires `compiler-rt` sources from LLVM.",
585+
),
586+
);
587+
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
588+
assert!(compiler_builtins_root.exists());
589+
// The path to `compiler-rt` is also used by `profiler_builtins` (above),
590+
// so if you're changing something here please also change that as appropriate.
591+
cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);
592+
}
587593
" compiler-builtins-c"
588594
} else {
589595
""

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,10 +1748,18 @@ impl Config {
17481748
pub fn optimized_compiler_builtins(&self, target: TargetSelection) -> bool {
17491749
self.target_config
17501750
.get(&target)
1751-
.and_then(|t| t.optimized_compiler_builtins)
1751+
.and_then(|t| t.optimized_compiler_builtins.as_ref())
1752+
.map(StringOrBool::is_string_or_true)
17521753
.unwrap_or(self.optimized_compiler_builtins)
17531754
}
17541755

1756+
pub fn optimized_compiler_builtins_path(&self, target: TargetSelection) -> Option<&str> {
1757+
match self.target_config.get(&target)?.optimized_compiler_builtins.as_ref()? {
1758+
StringOrBool::String(s) => Some(s),
1759+
StringOrBool::Bool(_) => None,
1760+
}
1761+
}
1762+
17551763
pub fn llvm_enabled(&self, target: TargetSelection) -> bool {
17561764
self.codegen_backends(target).contains(&"llvm".to_owned())
17571765
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::core::build_steps::clippy::{LintConfig, get_clippy_rules_in_order};
1717
use crate::core::build_steps::llvm;
1818
use crate::core::build_steps::llvm::LLVM_INVALIDATION_PATHS;
1919
use crate::core::config::toml::TomlConfig;
20-
use crate::core::config::{LldMode, Target, TargetSelection};
20+
use crate::core::config::{LldMode, StringOrBool, Target, TargetSelection};
2121
use crate::utils::tests::git::git_test;
2222

2323
pub(crate) fn parse(config: &str) -> Config {
@@ -212,7 +212,7 @@ runner = "x86_64-runner"
212212
let darwin = TargetSelection::from_user("aarch64-apple-darwin");
213213
let darwin_values = Target {
214214
runner: Some("apple".into()),
215-
optimized_compiler_builtins: Some(false),
215+
optimized_compiler_builtins: Some(StringOrBool::Bool(false)),
216216
..Default::default()
217217
};
218218
assert_eq!(

src/bootstrap/src/core/config/toml/target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ define_config! {
4545
no_std: Option<bool> = "no-std",
4646
codegen_backends: Option<Vec<String>> = "codegen-backends",
4747
runner: Option<String> = "runner",
48-
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
48+
optimized_compiler_builtins: Option<StringOrBool> = "optimized-compiler-builtins",
4949
jemalloc: Option<bool> = "jemalloc",
5050
}
5151
}
@@ -77,7 +77,7 @@ pub struct Target {
7777
pub runner: Option<String>,
7878
pub no_std: bool,
7979
pub codegen_backends: Option<Vec<String>>,
80-
pub optimized_compiler_builtins: Option<bool>,
80+
pub optimized_compiler_builtins: Option<StringOrBool>,
8181
pub jemalloc: Option<bool>,
8282
}
8383

0 commit comments

Comments
 (0)