Skip to content

Commit 1ad6b5e

Browse files
committed
Auto merge of #68623 - Zoxc:lld, r=Mark-Simulacrum
Add an option to use LLD to link the compiler on Windows platforms Based on #68609. Using LLD is good way to improve compile times on Windows since `link.exe` is quite slow. The time for `x.py build --stage 1 src/libtest` goes from 0:12:00 to 0:08:29. Compile time for `rustc_driver` goes from 226.34s to 18.5s. `rustc_macros` goes from 28.69s to 7.7s. The size of `rustc_driver` is also reduced from 83.3 MB to 78.7 MB. r? @Mark-Simulacrum
2 parents 6dff769 + d304cd0 commit 1ad6b5e

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

config.toml.example

+10
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@
395395
# rustc to execute.
396396
#lld = false
397397

398+
# Indicates whether LLD will be used to link Rust crates during bootstrap on
399+
# supported platforms. The LLD from the bootstrap distribution will be used
400+
# and not the LLD compiled during the bootstrap.
401+
#
402+
# LLD will not be used if we're cross linking or running tests.
403+
#
404+
# Explicitly setting the linker for a target will override this option.
405+
#use-lld = false
406+
398407
# Indicates whether some LLVM tools, like llvm-objdump, will be made available in the
399408
# sysroot.
400409
#llvm-tools = false
@@ -463,6 +472,7 @@
463472
# Linker to be used to link Rust code. Note that the
464473
# default value is platform specific, and if not specified it may also depend on
465474
# what platform is crossing to what platform.
475+
# Setting this will override the `use-lld` option for Rust code.
466476
#linker = "cc"
467477

468478
# Path to the `llvm-config` binary of the installation of a custom LLVM to link

src/bootstrap/bin/rustc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ fn main() {
134134
cmd.arg(format!("-Clinker={}", host_linker));
135135
}
136136

137+
// Override linker flavor if necessary.
138+
if let Ok(host_linker_flavor) = env::var("RUSTC_HOST_LINKER_FLAVOR") {
139+
cmd.arg(format!("-Clinker-flavor={}", host_linker_flavor));
140+
}
141+
137142
if let Ok(s) = env::var("RUSTC_HOST_CRT_STATIC") {
138143
if s == "true" {
139144
cmd.arg("-C").arg("target-feature=+crt-static");

src/bootstrap/builder.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl<'a> Builder<'a> {
692692
cmd.env_remove("MAKEFLAGS");
693693
cmd.env_remove("MFLAGS");
694694

695-
if let Some(linker) = self.linker(compiler.host) {
695+
if let Some(linker) = self.linker(compiler.host, true) {
696696
cmd.env("RUSTC_TARGET_LINKER", linker);
697697
}
698698
cmd
@@ -952,10 +952,31 @@ impl<'a> Builder<'a> {
952952
}
953953
}
954954

955-
if let Some(host_linker) = self.linker(compiler.host) {
955+
// FIXME: Don't use LLD if we're compiling libtest, since it fails to link it.
956+
// See https://github.com/rust-lang/rust/issues/68647.
957+
let can_use_lld = mode != Mode::Std;
958+
959+
// FIXME: The beta compiler doesn't pick the `lld-link` flavor for `*-pc-windows-msvc`
960+
// Remove `RUSTC_HOST_LINKER_FLAVOR` when this is fixed
961+
let lld_linker_flavor = |linker: &Path, target: Interned<String>| {
962+
compiler.stage == 0
963+
&& linker.file_name() == Some(OsStr::new("rust-lld"))
964+
&& target.contains("pc-windows-msvc")
965+
};
966+
967+
if let Some(host_linker) = self.linker(compiler.host, can_use_lld) {
968+
if lld_linker_flavor(host_linker, compiler.host) {
969+
cargo.env("RUSTC_HOST_LINKER_FLAVOR", "lld-link");
970+
}
971+
956972
cargo.env("RUSTC_HOST_LINKER", host_linker);
957973
}
958-
if let Some(target_linker) = self.linker(target) {
974+
975+
if let Some(target_linker) = self.linker(target, can_use_lld) {
976+
if lld_linker_flavor(target_linker, target) {
977+
rustflags.arg("-Clinker-flavor=lld-link");
978+
}
979+
959980
let target = crate::envify(&target);
960981
cargo.env(&format!("CARGO_TARGET_{}_LINKER", target), target_linker);
961982
}

src/bootstrap/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct Config {
8282
pub llvm_use_linker: Option<String>,
8383
pub llvm_allow_old_toolchain: Option<bool>,
8484

85+
pub use_lld: bool,
8586
pub lld_enabled: bool,
8687
pub lldb_enabled: bool,
8788
pub llvm_tools_enabled: bool,
@@ -321,6 +322,7 @@ struct Rust {
321322
save_toolstates: Option<String>,
322323
codegen_backends: Option<Vec<String>>,
323324
lld: Option<bool>,
325+
use_lld: Option<bool>,
324326
llvm_tools: Option<bool>,
325327
lldb: Option<bool>,
326328
deny_warnings: Option<bool>,
@@ -565,6 +567,7 @@ impl Config {
565567
if let Some(true) = rust.incremental {
566568
config.incremental = true;
567569
}
570+
set(&mut config.use_lld, rust.use_lld);
568571
set(&mut config.lld_enabled, rust.lld);
569572
set(&mut config.lldb_enabled, rust.lldb);
570573
set(&mut config.llvm_tools_enabled, rust.llvm_tools);

src/bootstrap/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,10 @@ pub struct Build {
239239
hosts: Vec<Interned<String>>,
240240
targets: Vec<Interned<String>>,
241241

242-
// Stage 0 (downloaded) compiler and cargo or their local rust equivalents
242+
// Stage 0 (downloaded) compiler, lld and cargo or their local rust equivalents
243243
initial_rustc: PathBuf,
244244
initial_cargo: PathBuf,
245+
initial_lld: PathBuf,
245246

246247
// Runtime state filled in later on
247248
// C/C++ compilers and archiver for all targets
@@ -343,9 +344,18 @@ impl Build {
343344
// we always try to use git for LLVM builds
344345
let in_tree_llvm_info = channel::GitInfo::new(false, &src.join("src/llvm-project"));
345346

347+
let initial_sysroot = config.initial_rustc.parent().unwrap().parent().unwrap();
348+
let initial_lld = initial_sysroot
349+
.join("lib")
350+
.join("rustlib")
351+
.join(config.build)
352+
.join("bin")
353+
.join("rust-lld");
354+
346355
let mut build = Build {
347356
initial_rustc: config.initial_rustc.clone(),
348357
initial_cargo: config.initial_cargo.clone(),
358+
initial_lld,
349359
local_rebuild: config.local_rebuild,
350360
fail_fast: config.cmd.fail_fast(),
351361
doc_tests: config.cmd.doc_tests(),
@@ -810,7 +820,7 @@ impl Build {
810820
}
811821

812822
/// Returns the path to the linker for the given target if it needs to be overridden.
813-
fn linker(&self, target: Interned<String>) -> Option<&Path> {
823+
fn linker(&self, target: Interned<String>, can_use_lld: bool) -> Option<&Path> {
814824
if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.as_ref())
815825
{
816826
Some(linker)
@@ -819,6 +829,8 @@ impl Build {
819829
&& !target.contains("msvc")
820830
{
821831
Some(self.cc(target))
832+
} else if can_use_lld && self.config.use_lld && self.build == target {
833+
Some(&self.initial_lld)
822834
} else {
823835
None
824836
}

src/bootstrap/test.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ impl Step for RustdocTheme {
596596
.env("RUSTDOC_REAL", builder.rustdoc(self.compiler))
597597
.env("RUSTDOC_CRATE_VERSION", builder.rust_version())
598598
.env("RUSTC_BOOTSTRAP", "1");
599-
if let Some(linker) = builder.linker(self.compiler.host) {
599+
if let Some(linker) = builder.linker(self.compiler.host, true) {
600600
cmd.env("RUSTC_TARGET_LINKER", linker);
601601
}
602602
try_run(builder, &mut cmd);
@@ -1035,7 +1035,8 @@ impl Step for Compiletest {
10351035
flags.push("-Zunstable-options".to_string());
10361036
flags.push(builder.config.cmd.rustc_args().join(" "));
10371037

1038-
if let Some(linker) = builder.linker(target) {
1038+
// Don't use LLD here since we want to test that rustc finds and uses a linker by itself.
1039+
if let Some(linker) = builder.linker(target, false) {
10391040
cmd.arg("--linker").arg(linker);
10401041
}
10411042

0 commit comments

Comments
 (0)