Skip to content

Commit 40b0619

Browse files
authored
[FatLTO] Detect LLD linker more reliably (#128285)
It's possible to have an `ld-path` point to a linker that doesn't have the `ld.lld` filename (e.g. linker wrapper that may emit telemetry before invoking the linker). This was causing mis-compilations with fatLTO since the check couldn't reliably detect that it was using lld. Instead, rely on the value from `-fuse-ld` to determine whether lld is enabled.
1 parent 14f33c6 commit 40b0619

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,13 +862,15 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
862862
const llvm::Triple &Triple = ToolChain.getTriple();
863863
const bool IsOSAIX = Triple.isOSAIX();
864864
const bool IsAMDGCN = Triple.isAMDGCN();
865-
const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
865+
StringRef Linker = Args.getLastArgValue(options::OPT_fuse_ld_EQ);
866+
const char *LinkerPath = Args.MakeArgString(ToolChain.GetLinkerPath());
866867
const Driver &D = ToolChain.getDriver();
867868
const bool IsFatLTO = Args.hasFlag(options::OPT_ffat_lto_objects,
868869
options::OPT_fno_fat_lto_objects, false);
869870
const bool IsUnifiedLTO = Args.hasArg(options::OPT_funified_lto);
870-
if (llvm::sys::path::filename(Linker) != "ld.lld" &&
871-
llvm::sys::path::stem(Linker) != "ld.lld" && !Triple.isOSOpenBSD()) {
871+
if (Linker != "lld" && Linker != "lld-link" &&
872+
llvm::sys::path::filename(LinkerPath) != "ld.lld" &&
873+
llvm::sys::path::stem(LinkerPath) != "ld.lld" && !Triple.isOSOpenBSD()) {
872874
// Tell the linker to load the plugin. This has to come before
873875
// AddLinkerInputs as gold requires -plugin and AIX ld requires -bplugin to
874876
// come before any -plugin-opt/-bplugin_opt that -Wl might forward.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ld.lld

clang/test/Driver/fat-lto-objects.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@
4949
// RUN: -fuse-ld=lld -flto -ffat-lto-objects -### 2>&1 | FileCheck --check-prefix=LTO %s
5050
// RUN: %clang --target=x86_64-unknown-linux-gnu --sysroot=%S/Inputs/basic_cross_linux_tree %s \
5151
// RUN: -fuse-ld=lld -fno-lto -ffat-lto-objects -### 2>&1 | FileCheck --check-prefix=NOLTO %s
52+
// RUN: %clang --target=x86_64-unknown-linux-gnu --sysroot=%S/Inputs/basic_cross_linux_tree %s \
53+
// RUN: -fuse-ld=lld --ld-path=%S/Inputs/basic_cross_linux_tree/usr/x86_64-unknown-linux-gnu/bin/lld-wrapper \
54+
// RUN: -flto -ffat-lto-objects -### 2>&1 | FileCheck --check-prefix=LTO %s
5255
// LTO: "--fat-lto-objects"
5356
// NOLTO-NOT: "--fat-lto-objects"

0 commit comments

Comments
 (0)