diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -931,6 +931,8 @@ include(AddLLVM) include(TableGen) +include(LLVMDistributionSupport) + if( MINGW AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) # People report that -O3 is unreliable on MinGW. The traditional # build also uses -O2 for that reason: @@ -1119,7 +1121,6 @@ # This must be at the end of the LLVM root CMakeLists file because it must run # after all targets are created. -include(LLVMDistributionSupport) llvm_distribution_add_targets() process_llvm_pass_plugins(GEN_CONFIG) diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -218,6 +218,25 @@ endif() function(add_link_opts target_name) + if(LLVM_DISTRIBUTION_COMPONENTS OR LLVM_DISTRIBUTIONS) + get_property(distribution GLOBAL PROPERTY LLVM_DISTRIBUTION_FOR_${target_name}) + if(NOT distribution) + # Don't LTO optimize targets that aren't part of any distribution. + if(LLVM_ENABLE_LTO) + # We may consider avoiding LTO altogether by using -fembed-bitcode + # and teaching the linker to select machine code from .o files, see + # https://lists.llvm.org/pipermail/llvm-dev/2021-April/149843.html + if((UNIX OR MINGW) AND LLVM_USE_LINKER STREQUAL "lld") + set_property(TARGET ${target_name} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,--lto-O0") + elseif(LINKER_IS_LLD_LINK) + set_property(TARGET ${target_name} APPEND_STRING PROPERTY + LINK_FLAGS " /opt:lldlto=0") + endif() + endif() + endif() + endif() + # Don't use linker optimizations in debug builds since it slows down the # linker in a context where the optimizations are not important. if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")