[libcxx-commits] [PATCH] D152967: [runtimes] Enable LTO when supported

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 14 15:16:44 PDT 2023


ldionne created this revision.
Herald added subscribers: ormris, steven_wu, hiraditya, inglorion.
Herald added a project: All.
ldionne requested review of this revision.
Herald added projects: libc++, libc++abi, libunwind.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.
Herald added a reviewer: libc++abi.
Herald added a reviewer: libunwind.

We were not using LTO to build the various runtime libraries. Enabling
this is a no-brainer.

I did measure the code size changes in the hopes that it would provide
an improvement in the size of the dylib. However, after stripping the
dylibs, there is no improvement:

File                      Without LTO (-O2 -g)      With Thin LTO (-flto=thin -O2 -g)
=====================================================================================

libc++.1.0.dylib          1.0M                      1.0M
libc++.a                  9.2M                      12M
libc++abi.1.0.dylib       252K                      252K
libc++abi.a               1.5M                      2.0M

The static archives become larger, however this is expected since we
are now including more information that would allow LTO to happen when
users produce their final linked image.

Note that this patch does not enable LTO for libunwind, because this
currently crashes when trying to link libunwind.dylib. I followed up
with linker folks to investigate the issue.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152967

Files:
  libcxx/CMakeLists.txt
  libcxxabi/src/CMakeLists.txt
  runtimes/cmake/Modules/LTO.cmake


Index: runtimes/cmake/Modules/LTO.cmake
===================================================================
--- /dev/null
+++ runtimes/cmake/Modules/LTO.cmake
@@ -0,0 +1,8 @@
+include(CheckIPOSupported)
+check_ipo_supported(RESULT _ipo_supported LANGUAGES C CXX)
+
+function(cxx_enable_lto target)
+  if (_ipo_supported)
+    set_property(TARGET ${target} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
+  endif()
+endfunction()
Index: libcxxabi/src/CMakeLists.txt
===================================================================
--- libcxxabi/src/CMakeLists.txt
+++ libcxxabi/src/CMakeLists.txt
@@ -154,11 +154,13 @@
   string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
 endif()
 
+include(LTO)
 include(WarningFlags)
 
 # Build the shared library.
 add_library(cxxabi_shared_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
 cxx_add_warning_flags(cxxabi_shared_objects ${LIBCXXABI_ENABLE_WERROR} ${LIBCXXABI_ENABLE_PEDANTIC})
+cxx_enable_lto(cxxabi_shared_objects)
 if (LIBCXXABI_USE_LLVM_UNWINDER)
   if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY)
     target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared_objects) # propagate usage requirements
@@ -243,6 +245,7 @@
 # Build the static library.
 add_library(cxxabi_static_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
 cxx_add_warning_flags(cxxabi_static_objects ${LIBCXXABI_ENABLE_WERROR} ${LIBCXXABI_ENABLE_PEDANTIC})
+cxx_enable_lto(cxxabi_static_objects)
 if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
   target_link_libraries(cxxabi_static_objects PUBLIC unwind_static_objects) # propagate usage requirements
   target_sources(cxxabi_static_objects PUBLIC $<TARGET_OBJECTS:unwind_static_objects>)
Index: libcxx/CMakeLists.txt
===================================================================
--- libcxx/CMakeLists.txt
+++ libcxx/CMakeLists.txt
@@ -24,7 +24,6 @@
 set(LIBCXX_BINARY_INCLUDE_DIR "${LIBCXX_BINARY_DIR}/include/c++build")
 
 include(GNUInstallDirs)
-include(WarningFlags)
 
 # Require out of source build.
 include(MacroEnsureOutOfSourceBuild)
@@ -840,6 +839,8 @@
 endif()
 
 # Setup all common build flags =================================================
+include(LTO)
+include(WarningFlags)
 function(cxx_add_common_build_flags target)
   cxx_add_basic_build_flags(${target})
   cxx_add_warning_flags(${target} ${LIBCXX_ENABLE_WERROR} ${LIBCXX_ENABLE_PEDANTIC})
@@ -848,6 +849,7 @@
   cxx_add_rtti_flags(${target})
   cxx_add_module_flags(${target})
   cxx_link_system_libraries(${target})
+  cxx_enable_lto(${target})
 endfunction()
 
 #===============================================================================


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152967.531533.patch
Type: text/x-patch
Size: 2755 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230614/296042db/attachment.bin>


More information about the libcxx-commits mailing list