From bdb4929ebdc6260719411b8aa8ec4063acba6cd1 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 30 Jun 2023 12:27:42 +0200 Subject: [PATCH] Use LLVM_UNITTEST_LINK_FLAGS to reduce link times for unit tests We currently spend a lot of time performing LTO on unit tests, which is largely wasted effort. We can't disable LTO entirely, because we don't have fat objects, but at least we can disable post-link optimization. For my local build, this reduces time in %build by about 30%. --- ...-Add-LLVM_UNITTEST_LINK_FLAGS-option.patch | 39 +++++++++++++++++++ llvm.spec | 6 ++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 0001-cmake-Add-LLVM_UNITTEST_LINK_FLAGS-option.patch diff --git a/0001-cmake-Add-LLVM_UNITTEST_LINK_FLAGS-option.patch b/0001-cmake-Add-LLVM_UNITTEST_LINK_FLAGS-option.patch new file mode 100644 index 0000000..192ca92 --- /dev/null +++ b/0001-cmake-Add-LLVM_UNITTEST_LINK_FLAGS-option.patch @@ -0,0 +1,39 @@ +From cf35b4622d31701d0c79e0441931518726f997f2 Mon Sep 17 00:00:00 2001 +From: Nikita Popov +Date: Fri, 30 Jun 2023 12:25:09 +0200 +Subject: [PATCH] Add LLVM_UNITTEST_LINK_FLAGS option + +This allows specifying linker flags that should only be applied +to unit tests, e.g. to disable LTO optimization for them. +--- + llvm/cmake/modules/AddLLVM.cmake | 2 ++ + llvm/cmake/modules/HandleLLVMOptions.cmake | 3 +++ + 2 files changed, 5 insertions(+) + +diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake +index 94fc83db9344..7cf75223ce38 100644 +--- a/llvm/cmake/modules/AddLLVM.cmake ++++ b/llvm/cmake/modules/AddLLVM.cmake +@@ -1592,6 +1592,8 @@ function(add_unittest test_suite test_name) + endif() + endif() + ++ target_link_options(${test_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}") ++ + set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) + set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) + # libpthreads overrides some standard library symbols, so main +diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake +index 6119ecdce0f4..eec98162c2d7 100644 +--- a/llvm/cmake/modules/HandleLLVMOptions.cmake ++++ b/llvm/cmake/modules/HandleLLVMOptions.cmake +@@ -1274,3 +1274,6 @@ endif() + + set(LLVM_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../third-party CACHE STRING + "Directory containing third party software used by LLVM (e.g. googletest)") ++ ++set(LLVM_UNITTEST_LINK_FLAGS "" CACHE STRING ++ "Additional linker flags for unit tests") +-- +2.40.1 + diff --git a/llvm.spec b/llvm.spec index c8001fc..0c52b70 100644 --- a/llvm.spec +++ b/llvm.spec @@ -94,6 +94,9 @@ Source6: release-keys.asc Patch1: 0001-SystemZ-Improve-error-messages-for-unsupported-reloc.patch # See https://reviews.llvm.org/D137890 for the next two patches Patch2: 0001-llvm-Add-install-targets-for-gtest.patch +# Backport of https://reviews.llvm.org/D154212 from LLVM 17. +Patch3: 0001-cmake-Add-LLVM_UNITTEST_LINK_FLAGS-option.patch + # RHEL-specific patch to avoid unwanted recommonmark dep Patch101: 0101-Deactivate-markdown-doc.patch # Patching third-party dir with a 200 offset in patch number @@ -323,7 +326,8 @@ export ASMFLAGS=$CFLAGS -DCMAKE_INSTALL_PREFIX=%{install_prefix} \ -DLLVM_INSTALL_SPHINX_HTML_DIR=%{_pkgdocdir}/html \ -DSPHINX_EXECUTABLE=%{_bindir}/sphinx-build-3 \ - -DLLVM_INCLUDE_BENCHMARKS=OFF + -DLLVM_INCLUDE_BENCHMARKS=OFF \ + -DLLVM_UNITTEST_LINK_FLAGS="-Wl,-plugin-opt=O0" # Build libLLVM.so first. This ensures that when libLLVM.so is linking, there # are no other compile jobs running. This will help reduce OOM errors on the