Skip to content

Commit 96b0892

Browse files
committed
[3.15] gh-161821: Fall back to lld for Clang LTO builds (gh-161822)
Clang LTO with a GNU linker needs the LLVMgold.so plugin, which some packages (e.g. LLVM for Debian 13) don't ship. When using Clang with --with-lto on non-macOS platforms, configure now test-links an LTO program and, if that fails, retries with -fuse-ld=lld. (cherry picked from commit eb7cd6e)
1 parent 1ce067d commit 96b0892

4 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎Doc/using/configure.rst‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,14 @@ also be used to improve performance.
728728
Enable Link Time Optimization (LTO) in any build (disabled by default).
729729

730730
The C compiler Clang requires ``llvm-ar`` for LTO (``ar`` on macOS), as well
731-
as an LTO-aware linker (``ld.gold`` or ``lld``).
731+
as an LTO-aware linker (``ld.gold`` or ``lld``). GNU linkers require the
732+
``LLVMgold.so`` plugin for Clang LTO; ``lld`` does not. On platforms other
733+
than macOS, configure tries ``-fuse-ld=lld`` if the configured linker cannot
734+
link a Clang LTO program.
735+
736+
.. versionchanged:: next
737+
Fall back to ``lld`` when the configured linker cannot link Clang LTO
738+
programs.
732739

733740
.. versionadded:: 3.6
734741

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
When building with ``--with-lto`` and Clang, :program:`configure` now checks
2+
that the linker can link LTO objects and falls back to ``lld``
3+
(``-fuse-ld=lld``) if it cannot. This fixes LTO builds on systems where the
4+
default GNU linker lacks the ``LLVMgold.so`` plugin, such as Debian 13.

‎configure‎

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure.ac‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,32 @@ if test "$Py_LTO" = 'true' ; then
20452045
else
20462046
LTOFLAGS="-flto=${Py_LTO_POLICY}"
20472047
fi
2048+
LTOCFLAGS="$LTOFLAGS"
2049+
# GNU ld needs LLVMgold.so for Clang LTO. If the configured linker
2050+
# cannot link LTO objects, try lld, which does not need that plugin.
2051+
WITH_SAVE_ENV([
2052+
LDFLAGS="$LDFLAGS $LDFLAGS_NODIST $LTOFLAGS"
2053+
AC_MSG_CHECKING([whether Clang LTO works with the configured linker])
2054+
AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [clang_lto_link=yes], [clang_lto_link=no])
2055+
AC_MSG_RESULT([$clang_lto_link])
2056+
])
2057+
if test "$clang_lto_link" = no; then
2058+
WITH_SAVE_ENV([
2059+
LDFLAGS="$LDFLAGS -fuse-ld=lld"
2060+
AC_MSG_CHECKING([whether Clang LTO works with lld])
2061+
AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [clang_lto_link=yes], [clang_lto_link=no])
2062+
AC_MSG_RESULT([$clang_lto_link])
2063+
])
2064+
if test "$clang_lto_link" = no; then
2065+
AC_MSG_ERROR([m4_normalize([
2066+
Clang LTO requires a working LTO-aware linker.
2067+
Install lld or the LLVMgold.so plugin, select a linker
2068+
with LDFLAGS, or disable LTO with --without-lto.
2069+
])])
2070+
fi
2071+
LTOFLAGS="$LTOFLAGS -fuse-ld=lld"
2072+
LDFLAGS_NOLTO="$LDFLAGS_NOLTO -fuse-ld=lld"
2073+
fi
20482074
;;
20492075
esac
20502076
;;

0 commit comments

Comments
 (0)