[clang] [CMake] Add a cache file for building a highly-optimized LLVM toolchain (PR #117802)
Tom Stellard via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 26 14:26:18 PST 2024
https://github.com/tstellar updated https://github.com/llvm/llvm-project/pull/117802
>From 3bddb3c6d25efbfcc901a42a8367be85599d1f7e Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Mon, 23 Sep 2024 13:34:46 +0000
Subject: [PATCH 1/2] [CMake] Add a cache file for building a highly-optimized
LLVM toolchain
The goal of these cache files is to provide a toolchain that:
1. Relies on only LLVM components (as much as possible).
2. Is highly optimized.
These cache files will produce a full toolchain
(clang/compiler-rt/libcxx/lld) where clang is built with LTO, PGO, and
BOLT optimizations, and is statically linked with an LTO optimized
libc++ and compiler-rt.
I would eventually like to use these as the basis for the release
builds.
---
.../cmake/caches/llvm-toolchain/stage1.cmake | 13 ++++++++++++
.../cmake/caches/llvm-toolchain/stage2.cmake | 20 +++++++++++++++++++
.../llvm-toolchain/stage3-instrumented.cmake | 10 ++++++++++
.../cmake/caches/llvm-toolchain/stage3.cmake | 16 +++++++++++++++
clang/cmake/caches/llvm-toolchain/usage.rst | 20 +++++++++++++++++++
5 files changed, 79 insertions(+)
create mode 100644 clang/cmake/caches/llvm-toolchain/stage1.cmake
create mode 100644 clang/cmake/caches/llvm-toolchain/stage2.cmake
create mode 100644 clang/cmake/caches/llvm-toolchain/stage3-instrumented.cmake
create mode 100644 clang/cmake/caches/llvm-toolchain/stage3.cmake
create mode 100644 clang/cmake/caches/llvm-toolchain/usage.rst
diff --git a/clang/cmake/caches/llvm-toolchain/stage1.cmake b/clang/cmake/caches/llvm-toolchain/stage1.cmake
new file mode 100644
index 00000000000000..13a0a7871dcd26
--- /dev/null
+++ b/clang/cmake/caches/llvm-toolchain/stage1.cmake
@@ -0,0 +1,13 @@
+# Stage 1:
+# * Build the prerequisites for stage 2.
+# * We will be building an LTO optimized libcxx in stage 2, so we need to
+# build clang and lld.
+
+
+set(CMAKE_BUILD_TYPE Release CACHE STRING "")
+set(LLVM_TARGETS_TO_BUILD Native 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.cmake CACHE BOOL "")
+set(CLANG_BOOTSTRAP_TARGETS stage3-check-all stage3-distribution stage3-install-distribution stage3-clang stage3-clang-bolt CACHE BOOL "")
diff --git a/clang/cmake/caches/llvm-toolchain/stage2.cmake b/clang/cmake/caches/llvm-toolchain/stage2.cmake
new file mode 100644
index 00000000000000..87ac7899b205ef
--- /dev/null
+++ b/clang/cmake/caches/llvm-toolchain/stage2.cmake
@@ -0,0 +1,20 @@
+# Stage 2:
+# * Build an LTO optimized libcxx, so we can staticially link it into stage 3
+# clang.
+# * Stage 3 will be PGO optimized, so we need to build clang, lld, and
+# compiler-rt in stage 2.
+
+
+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 "" FORCE)
+set(LLVM_ENABLE_PROJECTS "clang;lld" CACHE STRING "" FORCE)
+
+set(CLANG_ENABLE_BOOTSTRAP ON CACHE BOOL "")
+set(CLANG_BOOTSTRAP_CMAKE_ARGS -C ${CMAKE_CURRENT_LIST_DIR}/stage3-instrumented.cmake CACHE BOOL "")
+set(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED IR CACHE BOOL "")
+set(CLANG_BOOTSTRAP_TARGETS stage3-check-all stage3-distribution stage3-install-distribution stage3-clang stage3-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 "")
+set(LLVM_ENABLE_LLD ON CACHE STRING "")
+#set(CLANG_DEFAULT_RTLIB compiler-rt CACHE STRING "")
diff --git a/clang/cmake/caches/llvm-toolchain/stage3-instrumented.cmake b/clang/cmake/caches/llvm-toolchain/stage3-instrumented.cmake
new file mode 100644
index 00000000000000..4570a6c758ccda
--- /dev/null
+++ b/clang/cmake/caches/llvm-toolchain/stage3-instrumented.cmake
@@ -0,0 +1,10 @@
+# Stage 3 instrumented:
+# * Build an instrumented clang, so we can generate profile data for stage 3.
+
+
+set(CLANG_ENABLE_BOOTSTRAP ON CACHE BOOL "")
+set(CLANG_BOOTSTRAP_CMAKE_ARGS -C ${CMAKE_CURRENT_LIST_DIR}/stage3.cmake CACHE BOOL "")
+set(CLANG_BOOTSTRAP_TARGETS clang check-all distribution install-distribution clang-bolt CACHE BOOL "")
+set(CLANG_BOLT OFF CACHE STRING "")
+
+include(${CMAKE_CURRENT_LIST_DIR}/stage3.cmake)
diff --git a/clang/cmake/caches/llvm-toolchain/stage3.cmake b/clang/cmake/caches/llvm-toolchain/stage3.cmake
new file mode 100644
index 00000000000000..a9050a64d8f633
--- /dev/null
+++ b/clang/cmake/caches/llvm-toolchain/stage3.cmake
@@ -0,0 +1,16 @@
+# Stage 3:
+# * This is the final stage.
+# * The goals 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 STRING "")
+set(LLVM_ENABLE_LTO THIN CACHE STRING "")
+set(LLVM_ENABLE_LIBCXX ON CACHE STRING "")
+set(LLVM_STATIC_LINK_CXX_STDLIB ON CACHE STRING "")
+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 "")
+set(LLVM_DISTRIBUTION_COMPONENTS "clang;lld;runtimes;clang-resource-headers" CACHE STRING "")
diff --git a/clang/cmake/caches/llvm-toolchain/usage.rst b/clang/cmake/caches/llvm-toolchain/usage.rst
new file mode 100644
index 00000000000000..82b6d678669643
--- /dev/null
+++ b/clang/cmake/caches/llvm-toolchain/usage.rst
@@ -0,0 +1,20 @@
+# 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 4 stages:
+
+* Stage 1: Build clang/lld.
+* Stage 2: Build an LTO optimized libc++ with Stage 1 clang/lld.
+* Stage 3 Instrumented: Build clang with instrumentation in order to generate
+ profile data for PGO.
+* Stage 3: 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 stage3-install-distribution
>From d188a03dee75b3e9964785e11d7edecbbad7ad0e Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Tue, 26 Nov 2024 14:26:09 -0800
Subject: [PATCH 2/2] Fix documentation
---
clang/cmake/caches/llvm-toolchain/usage.rst | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/cmake/caches/llvm-toolchain/usage.rst b/clang/cmake/caches/llvm-toolchain/usage.rst
index 82b6d678669643..739e2326273906 100644
--- a/clang/cmake/caches/llvm-toolchain/usage.rst
+++ b/clang/cmake/caches/llvm-toolchain/usage.rst
@@ -15,6 +15,7 @@ The build is done in 4 stages:
## Usage
-..
- cmake -S llvm -B build -C clang/cmake/caches/llvm-toolchain/stage1.cmake
- ninja stage3-install-distribution
+::
+
+ $ cmake -S llvm -B build -C clang/cmake/caches/llvm-toolchain/stage1.cmake
+ $ ninja stage3-install-distribution
More information about the cfe-commits
mailing list