Skip to content

Commit 81f6a6a

Browse files
authored
Rollup merge of #162824 - ZuseZ4:use-lld-more, r=Kobzol
link the offload with in-tree lld if possible I have multiple LLVM builds on all my computers, and often enough ended up with an older lld version on the path, which often breaks Offload builds. I've also seen autodiff builds take forever since some of it's files are huge, and builds somehow end up using ld. This fixes both by just using our up-to-date in-tree lld (if available). I used an LLM to implement a couple of bootstrap fixes, including this one. I then split this one out and cleaned it up. r? kobzol closes: #160584
2 parents fd959c9 + d296cf2 commit 81f6a6a

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

  • src/bootstrap/src/core/build_steps

‎src/bootstrap/src/core/build_steps/llvm.rs‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,40 @@ fn debuginfo_map_cflags(builder: &Builder<'_>, target: TargetSelection) -> Vec<S
832832
flags
833833
}
834834

835+
/// Tries to reuse a locally built LLD as a linker.
836+
/// If it is not available, uses a global LLD from PATH.
837+
fn try_link_with_in_tree_lld(
838+
builder: &Builder<'_>,
839+
target: TargetSelection,
840+
llvm_output: &LlvmOutput,
841+
cfg: &mut cmake::Config,
842+
ldflags: &mut LdFlags,
843+
) {
844+
// Apple has it's own ld64 linker, so don't use LLD on Darwin.
845+
if target.contains("apple") {
846+
return;
847+
}
848+
849+
if builder.config.llvm_use_linker.is_some() || !builder.config.lld_enabled || target.is_msvc() {
850+
// Logic derived from `configure_llvm`
851+
// ThinLTO is only available when building with LLVM, enabling LLD is required.
852+
if builder.config.llvm_thin_lto {
853+
ldflags.push_all("-fuse-ld=lld");
854+
}
855+
return;
856+
}
857+
858+
let lld_bin = builder.ensure(Lld { target }).join("bin");
859+
ldflags.push_all(format!("-B{} -fuse-ld=lld", lld_bin.display()));
860+
861+
if llvm_output.link_shared() {
862+
// LLD in this case needs the LLVM lib, so tell where to look for it.
863+
let mut dylib_path = helpers::dylib_path();
864+
dylib_path.insert(0, llvm_output.root_dir().join("lib"));
865+
cfg.env(helpers::dylib_path_var(), t!(env::join_paths(&dylib_path)));
866+
}
867+
}
868+
835869
fn configure_cmake(
836870
builder: &Builder<'_>,
837871
target: TargetSelection,
@@ -1180,13 +1214,8 @@ impl CommandLineStep for RustOffload {
11801214
let mut cfg =
11811215
cmake::Config::new(builder.src.join("compiler/rustc_llvm/llvm-wrapper/offload/"));
11821216

1183-
// Logic copied from `configure_llvm`
1184-
// ThinLTO is only available when building with LLVM, enabling LLD is required.
1185-
// Apple's linker ld64 supports ThinLTO out of the box though, so don't use LLD on Darwin.
11861217
let mut ldflags = LdFlags::default();
1187-
if builder.config.llvm_thin_lto && !target.contains("apple") {
1188-
ldflags.push_all("-fuse-ld=lld");
1189-
}
1218+
try_link_with_in_tree_lld(builder, target, &llvm_output, &mut cfg, &mut ldflags);
11901219

11911220
configure_cmake(builder, target, &mut cfg, true, ldflags, CcFlags::default(), &[]);
11921221

@@ -1397,13 +1426,8 @@ impl CommandLineStep for OmpOffload {
13971426
cflags.push_all(format!(" -I {inc_dir}"));
13981427
}
13991428

1400-
// Logic copied from `configure_llvm`
1401-
// ThinLTO is only available when building with LLVM, enabling LLD is required.
1402-
// Apple's linker ld64 supports ThinLTO out of the box though, so don't use LLD on Darwin.
14031429
let mut ldflags = LdFlags::default();
1404-
if builder.config.llvm_thin_lto && !target.contains("apple") {
1405-
ldflags.push_all("-fuse-ld=lld");
1406-
}
1430+
try_link_with_in_tree_lld(builder, target, &llvm_output, &mut cfg, &mut ldflags);
14071431

14081432
if let Some(dir) = &cxx_lib_dir {
14091433
ldflags.push_all(format!("-L{}", dir.display()));

0 commit comments

Comments
 (0)