[llvm] [CMake] Support per-target linker flags (PR #68393)

Vincent Lee via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 6 00:38:01 PDT 2023


https://github.com/thevinster created https://github.com/llvm/llvm-project/pull/68393

`CMAKE_{C/CXX}_FLAGS` affects all targets in LLVM. This can
be undesirable in situations, like the case of enabling thinLTO,
where `-flto` is added to every source file. In reality, we only 
care about optimizing a select few of binaries, such as clang or lld,
that dominate the compilation pipeline. Auxiliary binaries in a 
distribution and not on the critical path can be kept non-optimized. 
This PR adds support of per-target linker flags, which can solve the
thinLTO problem by negating the effects of LTO via targeted linker 
flags on the targets. The pattern follows a similar approach to how 
multi-distributions are created. The example of negating thinLTO 
above can be done by doing the following:

```
set(LLVM_CUSTOM_BINARIES llvm-dwarfdump lldb CACHE STRING "List of targets to apply custom flags")
set(LLVM_llvm-dwarfdump_EXE_LINKER_FLAGS "-Wl,--lto-O0" CACHE STRING "Custom linker flags to llvm-dwarfdump")
set(LLVM_lldb_EXE_LINKER_FLAGS "-Wl,--lto-O0" CACHE STRING "Custom linker flags to lldb")
```

There's other applications where this could be used (e.g. avoid
optimizing host tools for build speed improvement etc.). 
I've generalized this so that users can apply their desired flags to
targets that are generated by `llvm_add_library` or `add_llvm_executable`. 

Internally, our toolchain builds were on average 1.4x faster when
selectively choosing the binaries that we want optimized. 

>From cc83200af3bd64f41195b6a11480578370faf52b Mon Sep 17 00:00:00 2001
From: Vincent Lee <leevince at fb.com>
Date: Thu, 5 Oct 2023 23:18:09 -0700
Subject: [PATCH] [CMake] Support per-target linker flags

---
 llvm/cmake/modules/AddLLVM.cmake | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 93011522e498e6e..4075e0bb9fe4bc4 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -726,6 +726,20 @@ function(llvm_add_library name)
     endforeach()
   endif()
 
+  if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
+    if (NOT LLVM_${name}_EXE_LINKER_FLAGS AND NOT LLVM_${name}_SHARED_LINKER_FLAGS AND NOT LLVM_${name}_MODULE_LINKER_FLAGS)
+      message(FATAL_ERROR "Linker flags not provided for ${name} in LLVM_CUSTOM_BINARIES")
+    endif()
+    if (ARG_SHARED AND LLVM_${name}_SHARED_LINKER_FLAGS)
+      message(STATUS "Applying ${LLVM_${name}_SHARED_LINKER_FLAGS} to ${name}")
+      target_link_options(${name} PRIVATE ${LLVM_${name}_SHARED_LINKER_FLAGS})
+    endif()
+    if (ARG_MODULE AND LLVM_${name}_MODULE_LINKER_FLAGS)
+      message(STATUS "Applying ${LLVM_${name}_MODULE_LINKER_FLAGS} to ${name}")
+      target_link_options(${name} PRIVATE ${LLVM_${name}_MODULE_LINKER_FLAGS})
+    endif()
+  endif()
+
   if(ARG_SHARED OR ARG_MODULE)
     llvm_externalize_debuginfo(${name})
     llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
@@ -1019,6 +1033,16 @@ macro(add_llvm_executable name)
     endforeach()
   endif( LLVM_COMMON_DEPENDS )
 
+  if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
+    if (NOT LLVM_${name}_EXE_LINKER_FLAGS AND NOT LLVM_${name}_SHARED_LINKER_FLAGS AND NOT LLVM_${name}_MODULE_LINKER_FLAGS)
+      message(FATAL_ERROR "Linker flags not provided for ${name} in LLVM_CUSTOM_BINARIES")
+    endif()
+    if (LLVM_${name}_EXE_LINKER_FLAGS)
+      message(STATUS "Applying ${LLVM_${name}_EXE_LINKER_FLAGS} to ${name}")
+      target_link_options(${name} PRIVATE ${LLVM_${name}_EXE_LINKER_FLAGS})
+    endif()
+  endif()
+
   if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
     llvm_externalize_debuginfo(${name})
   endif()



More information about the llvm-commits mailing list