[PATCH] D102732: [CMake] Don't LTO optimize targets that aren't part of any distribution

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 18 16:17:40 PDT 2021


phosek created this revision.
phosek added a reviewer: smeenai.
Herald added subscribers: mstorsjo, inglorion, mgorny.
phosek requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When using distributions, targets that aren't included in any
distribution don't need to be as optimized as targets that are
included since those targets are typically only used for tests.

We might consider avoiding LTO for these targets altogether, see
https://lists.llvm.org/pipermail/llvm-dev/2021-April/149843.html


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102732

Files:
  llvm/CMakeLists.txt
  llvm/cmake/modules/AddLLVM.cmake


Index: llvm/cmake/modules/AddLLVM.cmake
===================================================================
--- llvm/cmake/modules/AddLLVM.cmake
+++ 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")
Index: llvm/CMakeLists.txt
===================================================================
--- llvm/CMakeLists.txt
+++ 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)
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102732.346296.patch
Type: text/x-patch
Size: 2011 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210518/2dbb6587/attachment.bin>


More information about the llvm-commits mailing list