Skip to content

Commit f8de502

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 e1b9081 commit f8de502

File tree

6 files changed

+87
-34
lines changed

6 files changed

+87
-34
lines changed

bootstrap.example.toml

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

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

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

Lines changed: 46 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,45 @@ 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+
if let Some(lib) = rt_builtins_ext.file_name() {
620+
println!(
621+
"cargo::rustc-link-lib=static:+verbatim={}",
622+
lib.to_str().unwrap()
623+
);
624+
}
625+
} else {
626+
cfg.compile("libcompiler-rt.a");
627+
}
601628
}
602629

603-
fn build_aarch64_out_of_line_atomics_libraries(builtins_dir: &Path, cfg: &mut cc::Build) {
630+
fn build_aarch64_out_of_line_atomics_libraries(
631+
builtins_dir: &Path,
632+
cfg: &mut cc::Build,
633+
link_against_prebuilt_rt: bool,
634+
) {
604635
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
605636
let outlined_atomics_file = builtins_dir.join("aarch64").join("lse.S");
606-
println!("cargo:rerun-if-changed={}", outlined_atomics_file.display());
637+
if !link_against_prebuilt_rt {
638+
println!("cargo:rerun-if-changed={}", outlined_atomics_file.display());
639+
}
607640

608641
cfg.include(&builtins_dir);
609642

@@ -616,6 +649,13 @@ mod c {
616649
for (model_number, model_name) in
617650
&[(1, "relax"), (2, "acq"), (3, "rel"), (4, "acq_rel")]
618651
{
652+
let sym = format!("__aarch64_{}{}_{}", instruction_type, size, model_name);
653+
println!("cargo:rustc-cfg={}=\"optimized-c\"", sym);
654+
655+
if link_against_prebuilt_rt {
656+
continue;
657+
}
658+
619659
// The original compiler-rt build system compiles the same
620660
// source file multiple times with different compiler
621661
// options. Here we do something slightly different: we
@@ -639,9 +679,6 @@ mod c {
639679
.unwrap();
640680
drop(file);
641681
cfg.file(path);
642-
643-
let sym = format!("__aarch64_{}{}_{}", instruction_type, size, model_name);
644-
println!("cargo:rustc-cfg={}=\"optimized-c\"", sym);
645682
}
646683
}
647684
}

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

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

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,10 +1769,18 @@ impl Config {
17691769
pub fn optimized_compiler_builtins(&self, target: TargetSelection) -> bool {
17701770
self.target_config
17711771
.get(&target)
1772-
.and_then(|t| t.optimized_compiler_builtins)
1772+
.and_then(|t| t.optimized_compiler_builtins.as_ref())
1773+
.map(StringOrBool::is_string_or_true)
17731774
.unwrap_or(self.optimized_compiler_builtins)
17741775
}
17751776

1777+
pub fn optimized_compiler_builtins_path(&self, target: TargetSelection) -> Option<&str> {
1778+
match self.target_config.get(&target)?.optimized_compiler_builtins.as_ref()? {
1779+
StringOrBool::String(s) => Some(s),
1780+
StringOrBool::Bool(_) => None,
1781+
}
1782+
}
1783+
17761784
pub fn llvm_enabled(&self, target: TargetSelection) -> bool {
17771785
self.codegen_backends(target).contains(&CodegenBackendKind::Llvm)
17781786
}

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<CodegenBackendKind>>,
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)