Skip to content

[CMake] Add a cache file for building a highly-optimized LLVM toolchain #117802

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions clang/cmake/caches/llvm-toolchain/stage1.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Stage 1
# * Build an LTO optimized libcxx, so we can staticially link it into stage 2
# clang.


set(CMAKE_BUILD_TYPE Release CACHE STRING "")
set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "")
set(LLVM_ENABLE_RUNTIMES compiler-rt libcxx libcxxabi libunwind CACHE STRING "")
set(LLVM_ENABLE_PROJECTS clang lld CACHE STRING "")

set(CLANG_ENABLE_BOOTSTRAP ON CACHE BOOL "")
set(CLANG_BOOTSTRAP_CMAKE_ARGS -C ${CMAKE_CURRENT_LIST_DIR}/stage2-instrumented.cmake CACHE BOOL "")
set(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED IR CACHE BOOL "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to set this in stage2-instrumented instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not. This needs to be set in the stage1 config: https://github.com/llvm/llvm-project/blob/main/clang/CMakeLists.txt#L597

set(CLANG_BOOTSTRAP_TARGETS stage2-check-all stage2-distribution stage2-install-distribution stage2-clang stage2-clang-bolt CACHE BOOL "")
set(LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY ON CACHE STRING "")
set(RUNTIMES_CMAKE_ARGS "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON" CACHE STRING "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this use FullLTO or ThinLTO? We're explicitly building the binaries with ThinLTO, so I'd assumed libc++ would also need to be ThinLTO to be able to be statically linked into them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set(LLVM_ENABLE_LLD ON CACHE STRING "")
11 changes: 11 additions & 0 deletions clang/cmake/caches/llvm-toolchain/stage2-instrumented.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Stage 2 instrumented:
# * Build an instrumented clang, so we can generate profile data for stage 2.


set(CLANG_ENABLE_BOOTSTRAP ON CACHE BOOL "")
set(LLVM_BUILD_INSTRUMENTED IR CACHE STRING "")
set(CLANG_BOOTSTRAP_CMAKE_ARGS -C ${CMAKE_CURRENT_LIST_DIR}/stage2.cmake CACHE STRING "")
set(CLANG_BOOTSTRAP_TARGETS clang check-all distribution install-distribution clang-bolt CACHE STRING "")
set(CLANG_BOLT OFF CACHE STRING "")

include(${CMAKE_CURRENT_LIST_DIR}/stage2.cmake)
17 changes: 17 additions & 0 deletions clang/cmake/caches/llvm-toolchain/stage2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Stage 2:
# * This is the final stage.
# * The goal is to have a clang that is LTO, PGO, and bolt optimized and also
# statically linked to libcxx and compiler-rt.

set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "")
set(LLVM_ENABLE_RUNTIMES compiler-rt libcxx libcxxabi libunwind CACHE STRING "" FORCE)
set(LLVM_ENABLE_PROJECTS clang lld bolt CACHE STRING "" FORCE)
set(LLVM_ENABLE_LLD ON CACHE BOOL "")
set(LLVM_ENABLE_LTO THIN CACHE STRING "")
set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
set(LLVM_STATIC_LINK_CXX_STDLIB ON CACHE BOOL "")
set(CLANG_BOLT "INSTRUMENT" CACHE STRING "")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--emit-relocs,-znow -rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" CACHE STRING "")
set(CMAKE_SHARED_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" CACHE STRING "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had some issues in the past (that I can't remember the details of) if I didn't also set CMAKE_MODULE_LINKER_FLAGS.

set(CMAKE_MODULE_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" CACHE STRING "")
set(LLVM_DISTRIBUTION_COMPONENTS clang lld runtimes clang-resource-headers CACHE STRING "")
19 changes: 19 additions & 0 deletions clang/cmake/caches/llvm-toolchain/usage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# LLVM Toolchain Build

This directory contains cache files for building a complete LLVM-based toolchain.
The resulting clang build will be LTO, PGO, and BOLT optimized and statically
linked against libc++ and compiler-rt.

The build is done in 3 stages:

* Stage 1: Build an LTO optimized libc++ with Stage 1 clang/lld.
* Stage 2 Instrumented: Build clang with instrumentation in order to generate
profile data for PGO.
* Stage 2: Build clang with LTO, PGO, and BOLT optimizations and statically link
with stage2 libc++ and compiler-rt.

## Usage

::
$ cmake -S llvm -B build -C clang/cmake/caches/llvm-toolchain/stage1.cmake
$ ninja stage2-install-distribution
Loading