[flang-commits] [flang] [llvm] [flang][cmake] Set the usual linker flags for non-gtest unit tests (PR #165256)
Nikita Popov via flang-commits
flang-commits at lists.llvm.org
Mon Oct 27 07:12:56 PDT 2025
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/165256
Flang also uses non-gtest based unittests, which don't go through the usual add_unittest() helper. These currently do not use the usual linker flags for unit tests. This means that in LTO builds, they do not disable LTO when building unit tests, which increases the build time.
>From e1eb4dd876c629e585b949989547d9905b197eeb Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 27 Oct 2025 15:09:35 +0100
Subject: [PATCH] [flang][cmake] Set the usual linker flags for non-gtest unit
tests
Flang also uses non-gtest based unittests, which don't go through
the usual add_unittest() helper. These currently do not use the
usual linker flags for unit tests. This means that in LTO builds,
they do not disable LTO when building unit tests, which increases
the build time.
---
flang/unittests/CMakeLists.txt | 1 +
llvm/cmake/modules/AddLLVM.cmake | 47 ++++++++++++++++++--------------
2 files changed, 27 insertions(+), 21 deletions(-)
diff --git a/flang/unittests/CMakeLists.txt b/flang/unittests/CMakeLists.txt
index db04923e2943a..2d612e58dae24 100644
--- a/flang/unittests/CMakeLists.txt
+++ b/flang/unittests/CMakeLists.txt
@@ -48,6 +48,7 @@ function(add_flang_nongtest_unittest test_name)
llvm_map_components_to_libnames(llvm_libs Support)
endif()
target_link_libraries(${test_name}${suffix} ${llvm_libs} ${ARG_UNPARSED_ARGUMENTS})
+ set_unittest_link_flags(${test_name}${suffix})
if(NOT ARG_SLOW_TEST)
add_dependencies(FlangUnitTests ${test_name}${suffix})
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 80e59a4df2433..7d40d309d538e 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1747,6 +1747,31 @@ function(add_llvm_implicit_projects)
llvm_add_implicit_projects(LLVM)
endfunction(add_llvm_implicit_projects)
+function(set_unittest_link_flags target_name)
+ # The runtime benefits of LTO don't outweight the compile time costs for
+ # tests.
+ if(LLVM_ENABLE_LTO)
+ if((UNIX OR MINGW) AND LINKER_IS_LLD)
+ if(LLVM_ENABLE_FATLTO AND NOT APPLE)
+ # When using FatLTO, just use relocatable linking.
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,--no-fat-lto-objects")
+ else()
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,--lto-O0")
+ endif()
+ elseif(LINKER_IS_LLD_LINK)
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " /opt:lldlto=0")
+ elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,-mllvm,-O0")
+ endif()
+ endif()
+
+ target_link_options(${target_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")
+endfunction(set_unittest_link_flags)
+
# Generic support for adding a unittest.
function(add_unittest test_suite test_name)
if( NOT LLVM_BUILD_TESTS )
@@ -1770,27 +1795,7 @@ function(add_unittest test_suite test_name)
get_subproject_title(subproject_title)
set_target_properties(${test_name} PROPERTIES FOLDER "${subproject_title}/Tests/Unit")
- # The runtime benefits of LTO don't outweight the compile time costs for tests.
- if(LLVM_ENABLE_LTO)
- if((UNIX OR MINGW) AND LINKER_IS_LLD)
- if(LLVM_ENABLE_FATLTO AND NOT APPLE)
- # When using FatLTO, just use relocatable linking.
- set_property(TARGET ${test_name} APPEND_STRING PROPERTY
- LINK_FLAGS " -Wl,--no-fat-lto-objects")
- else()
- set_property(TARGET ${test_name} APPEND_STRING PROPERTY
- LINK_FLAGS " -Wl,--lto-O0")
- endif()
- elseif(LINKER_IS_LLD_LINK)
- set_property(TARGET ${test_name} APPEND_STRING PROPERTY
- LINK_FLAGS " /opt:lldlto=0")
- elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
- set_property(TARGET ${target_name} APPEND_STRING PROPERTY
- LINK_FLAGS " -Wl,-mllvm,-O0")
- endif()
- endif()
-
- target_link_options(${test_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")
+ set_unittest_link_flags(${test_name})
set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
More information about the flang-commits
mailing list