Skip to content

[CMAKE] Enable FatLTO as a build option for LLVM #80480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2024

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Feb 2, 2024

Since LLVM supports -ffat-lto-objects we should enable this as an option in the LLVM build. FatLTO should improve the time it takes to build tests for LTO enabled builds of the compiler by not linking w/ the bitcode portion of the object files, which should speed up build times for LTO builds without disabling optimizations.

@ilovepi ilovepi requested a review from petrhosek February 2, 2024 19:34
@ilovepi
Copy link
Contributor Author

ilovepi commented Feb 2, 2024

I'm keeping this as a draft until I confirm the cmake config works as expected, especially in 2 stage builds.

@ilovepi ilovepi marked this pull request as ready for review February 2, 2024 20:48
@llvmbot llvmbot added cmake Build system in general and CMake in particular clang Clang issues not falling into any other category labels Feb 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 2, 2024

@llvm/pr-subscribers-clang

Author: Paul Kirth (ilovepi)

Changes

Since LLVM supports -ffat-lto-objects we should enable this as an option in the LLVM build. FatLTO should improve the time it takes to build tests for LTO enabled builds of the compiler by not linking w/ the bitcode portion of the object files, which should speed up build times for LTO builds without disabling optimizations.


Full diff: https://github.com/llvm/llvm-project/pull/80480.diff

3 Files Affected:

  • (modified) clang/cmake/caches/Fuchsia-stage2.cmake (+1)
  • (modified) llvm/cmake/modules/AddLLVM.cmake (+9-2)
  • (modified) llvm/cmake/modules/HandleLLVMOptions.cmake (+6)
diff --git a/clang/cmake/caches/Fuchsia-stage2.cmake b/clang/cmake/caches/Fuchsia-stage2.cmake
index eee37c5e7901f..d5a1662cbf4aa 100644
--- a/clang/cmake/caches/Fuchsia-stage2.cmake
+++ b/clang/cmake/caches/Fuchsia-stage2.cmake
@@ -11,6 +11,7 @@ set(LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind" CACHE STRING "
 
 set(LLVM_ENABLE_BACKTRACES OFF CACHE BOOL "")
 set(LLVM_ENABLE_DIA_SDK OFF CACHE BOOL "")
+set(LLVM_ENABLE_FATLTO ON CACHE BOOL "")
 set(LLVM_ENABLE_HTTPLIB ON CACHE BOOL "")
 set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
 set(LLVM_ENABLE_LIBEDIT OFF CACHE BOOL "")
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 5e98961855282..26ba092a82948 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1621,8 +1621,15 @@ function(add_unittest test_suite test_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)
-      set_property(TARGET ${test_name} APPEND_STRING PROPERTY
-                    LINK_FLAGS " -Wl,--lto-O0")
+      if(LLVM_ENABLE_FATLTO)
+        # When using FatLTO, just use relocatable linking.
+        set_property(TARGET ${test_name} APPEND_STRING PROPERTY
+                      LINK_FLAGS " -Wl,--no-fat-lto-objects")
+        set_property(TARGET ${test_name} APPEND_STRING PROPERTY COMPILE_FLAGS " -fno-lto")
+      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")
diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 0699a8586fcc7..05bbe98ef96f8 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -32,6 +32,8 @@ endif()
 set(LLVM_ENABLE_LTO OFF CACHE STRING "Build LLVM with LTO. May be specified as Thin or Full to use a particular kind of LTO")
 string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO)
 
+option(LLVM_ENABLE_FATLTO "Build LLVM with -ffat-lto-objects." OFF)
+
 # Ninja Job Pool support
 # The following only works with the Ninja generator in CMake >= 3.0.
 set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING
@@ -1251,6 +1253,10 @@ elseif(LLVM_ENABLE_LTO)
   endif()
 endif()
 
+if(LLVM_ENABLE_FATLTO AND (FUCHSIA OR UNIX))
+    append("-ffat-lto-objects" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
+endif()
+
 # Set an AIX default for LLVM_EXPORT_SYMBOLS_FOR_PLUGINS based on whether we are
 # doing dynamic linking (see below).
 set(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default OFF)

@ilovepi
Copy link
Contributor Author

ilovepi commented Feb 2, 2024

This speeds up a 2 stage build by about 3 minutes, tests time are still much closer than expected. Locally I saw a bout a 5-8% speedup to build clang unit tests, but maybe the unit tests are too small in the overall test regime to make a difference.

I'll try to collect more performance numbers locally w.r.t. the test times.

@ilovepi ilovepi force-pushed the llvm-fatlto-tests branch 2 times, most recently from 84c61d9 to 9c10222 Compare February 29, 2024 21:10
@ilovepi ilovepi force-pushed the llvm-fatlto-tests branch from 32be729 to d4d997a Compare March 5, 2024 18:14
@ilovepi
Copy link
Contributor Author

ilovepi commented Mar 15, 2024

Not sure what I've managed to do here w/ basic rebase ... Will remove unrelated folks and triage the commits

@ilovepi ilovepi force-pushed the llvm-fatlto-tests branch from d452022 to 10dceb4 Compare March 15, 2024 23:33
@ilovepi ilovepi merged commit 43fc921 into llvm:main Mar 15, 2024
@ilovepi ilovepi deleted the llvm-fatlto-tests branch March 15, 2024 23:35
@Endilll Endilll removed their request for review March 16, 2024 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants