Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,40 @@ fn debuginfo_map_cflags(builder: &Builder<'_>, target: TargetSelection) -> Vec<S
flags
}

/// Tries to reuse a locally built LLD as a linker.
/// If it is not available, uses a global LLD from PATH.
fn try_link_with_in_tree_lld(
builder: &Builder<'_>,
target: TargetSelection,
llvm_output: &LlvmOutput,
cfg: &mut cmake::Config,
ldflags: &mut LdFlags,
) {
// Apple has it's own ld64 linker, so don't use LLD on Darwin.
if target.contains("apple") {
return;
}

if builder.config.llvm_use_linker.is_some() || !builder.config.lld_enabled || target.is_msvc() {
// Logic derived from `configure_llvm`
// ThinLTO is only available when building with LLVM, enabling LLD is required.
if builder.config.llvm_thin_lto {
ldflags.push_all("-fuse-ld=lld");
}
return;
}

let lld_bin = builder.ensure(Lld { target }).join("bin");
ldflags.push_all(format!("-B{} -fuse-ld=lld", lld_bin.display()));

@Kobzol Kobzol Sep 16, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates the -fuse-ld=lld flag set on line 846, right?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I don't think it's harmful, but I moved it to avoid it.


if llvm_output.link_shared() {
// LLD in this case needs the LLVM lib, so tell where to look for it.
let mut dylib_path = helpers::dylib_path();
dylib_path.insert(0, llvm_output.root_dir().join("lib"));
cfg.env(helpers::dylib_path_var(), t!(env::join_paths(&dylib_path)));
}
}

fn configure_cmake(
builder: &Builder<'_>,
target: TargetSelection,
Expand Down Expand Up @@ -1180,13 +1214,8 @@ impl CommandLineStep for RustOffload {
let mut cfg =
cmake::Config::new(builder.src.join("compiler/rustc_llvm/llvm-wrapper/offload/"));

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

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

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

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

if let Some(dir) = &cxx_lib_dir {
ldflags.push_all(format!("-L{}", dir.display()));
Expand Down Expand Up @@ -1563,13 +1587,8 @@ impl CommandLineStep for Enzyme {
let mut cflags = CcFlags::default();
cflags.push_all("-Wno-deprecated");

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

configure_cmake(builder, target, &mut cfg, true, ldflags, cflags, &[]);

Expand Down
Loading