Skip to content

Commit 7fb7a91

Browse files
committed
[CMAKE][llvm-libraries] Add Precompiled Headers option to improve build times
1 parent 597ac47 commit 7fb7a91

File tree

8 files changed

+194
-4
lines changed

8 files changed

+194
-4
lines changed

Diff for: llvm/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1410,3 +1410,6 @@ endif()
14101410
if (LLVM_INCLUDE_UTILS AND LLVM_INCLUDE_TOOLS)
14111411
add_subdirectory(utils/llvm-locstats)
14121412
endif()
1413+
1414+
include(LLVMPrecompiledHeaders)
1415+
llvm_lib_precompiled_headers()

Diff for: llvm/cmake/modules/HandleLLVMOptions.cmake

+10-1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ endif()
538538

539539
option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
540540

541+
option(LLVM_ENABLE_LIB_PRECOMPILED_HEADERS "Enable precompiled headers for LLVM Lib projects to improve build times." ON)
542+
543+
set(LLVM_LIB_DIRETORIES_FOR_PRECOMPILED_HEADERS ""
544+
CACHE STRING "Semicolon-separated list of lib subdirectories to use precompiled headers")
545+
541546
if( MSVC )
542547

543548
# Add definitions that make MSVC much less annoying.
@@ -593,7 +598,11 @@ if( MSVC )
593598
# PDBs without changing codegen.
594599
option(LLVM_ENABLE_PDB OFF)
595600
if (LLVM_ENABLE_PDB AND uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
596-
append("/Zi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
601+
if (LLVM_ENABLE_LIB_PRECOMPILED_HEADERS)
602+
append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
603+
else()
604+
append("/Zi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
605+
endif()
597606
# /DEBUG disables linker GC and ICF, but we want those in Release mode.
598607
append("/DEBUG /OPT:REF /OPT:ICF"
599608
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS

Diff for: llvm/cmake/modules/LLVMPrecompiledHeaders.cmake

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
macro(get_all_targets_recursive targets dir)
2+
get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
3+
foreach(subdir ${subdirectories})
4+
get_all_targets_recursive(${targets} ${subdir})
5+
endforeach()
6+
7+
get_property(current_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
8+
list(APPEND ${targets} ${current_targets})
9+
endmacro()
10+
11+
function(get_all_targets dir outvar)
12+
set(targets)
13+
get_all_targets_recursive(targets ${dir})
14+
set(${outvar} ${targets} PARENT_SCOPE)
15+
endfunction()
16+
17+
function(add_llvm_lib_precompiled_headers target)
18+
get_target_property(target_type ${target} TYPE)
19+
if (target_type STREQUAL "STATIC_LIBRARY")
20+
target_precompile_headers(${target} REUSE_FROM LLVMPchTarget)
21+
endif()
22+
endfunction()
23+
24+
function(add_llvm_subdir_pch subdir)
25+
get_all_targets("${LLVM_MAIN_SRC_DIR}/lib/${subdir}" lib_targets)
26+
foreach(target ${lib_targets})
27+
add_llvm_lib_precompiled_headers(${target})
28+
endforeach()
29+
endfunction()
30+
31+
function(llvm_lib_precompiled_headers)
32+
if (LLVM_ENABLE_LIB_PRECOMPILED_HEADERS)
33+
# Create a dummy source file to compile the PCH target.
34+
set(pch_dummy_cpp ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp)
35+
if (NOT EXISTS ${pch_dummy_cpp})
36+
FILE(TOUCH ${pch_dummy_cpp})
37+
endif()
38+
39+
set(precompiled_header_path ${LLVM_MAIN_INCLUDE_DIR}/llvm/PrecompiledHeaders.h)
40+
add_llvm_component_library(LLVMPchTarget ${pch_dummy_cpp} ${precompiled_header_path})
41+
target_precompile_headers(LLVMPchTarget PUBLIC "$<$<COMPILE_LANGUAGE:CXX>:${LLVM_MAIN_INCLUDE_DIR}/llvm/PrecompiledHeaders.h>")
42+
43+
if (NOT ${LLVM_LIB_DIRETORIES_FOR_PRECOMPILED_HEADERS})
44+
set(default_lib_dirs_for_pch
45+
"Analysis"
46+
"Codegen"
47+
"DebugInfo"
48+
"ExecutionEngine"
49+
"IR"
50+
"MC"
51+
"MCA"
52+
"ObjCopy"
53+
"Object"
54+
"Passes"
55+
"Support"
56+
"Target"
57+
"Transforms"
58+
)
59+
set(LLVM_LIB_DIRETORIES_FOR_PRECOMPILED_HEADERS ${default_lib_dirs_for_pch})
60+
endif()
61+
foreach(subdir ${LLVM_LIB_DIRETORIES_FOR_PRECOMPILED_HEADERS})
62+
add_llvm_subdir_pch(${subdir})
63+
endforeach()
64+
endif()
65+
endfunction()

Diff for: llvm/include/llvm/ADT/ConcurrentHashtable.h

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/Support/Allocator.h"
1717
#include "llvm/Support/Debug.h"
1818
#include "llvm/Support/Parallel.h"
19-
#include "llvm/Support/WithColor.h"
2019
#include "llvm/Support/xxhash.h"
2120
#include <atomic>
2221
#include <cstddef>

Diff for: llvm/include/llvm/PrecompiledHeaders.h

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//===-- llvm/PrecompiledHeaders.h - Precompiled Headers ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file contains common headers used within the LLVM library.
11+
/// It is intended to be used as a precompiled header.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_PRECOMPILEDHEADERS_H
16+
#define LLVM_PRECOMPILEDHEADERS_H
17+
18+
#include "llvm/IR/PassManager.h"
19+
#include "llvm/IR/Instructions.h"
20+
#include "llvm/IR/IntrinsicInst.h"
21+
#include "llvm/IR/DebugInfoMetadata.h"
22+
23+
#include "llvm/ADT/STLForwardCompat.h"
24+
#include "llvm/ADT/STLFunctionalExtras.h"
25+
#include "llvm/ADT/ADL.h"
26+
#include "llvm/ADT/iterator_range.h"
27+
#include "llvm/ADT/SmallVector.h"
28+
#include "llvm/ADT/DenseMapInfo.h"
29+
#include "llvm/ADT/bit.h"
30+
#include "llvm/ADT/StringRef.h"
31+
#include "llvm/ADT/Hashing.h"
32+
#include "llvm/ADT/iterator.h"
33+
#include "llvm/ADT/STLExtras.h"
34+
#include "llvm/ADT/ArrayRef.h"
35+
#include "llvm/ADT/Twine.h"
36+
#include "llvm/ADT/EpochTracker.h"
37+
#include "llvm/ADT/DenseMap.h"
38+
#include "llvm/ADT/StringMapEntry.h"
39+
#include "llvm/ADT/DenseSet.h"
40+
#include "llvm/ADT/StringMap.h"
41+
#include "llvm/ADT/PointerIntPair.h"
42+
#include "llvm/ADT/ArrayRef.h"
43+
#include "llvm/ADT/APInt.h"
44+
#include "llvm/ADT/ilist_node_base.h"
45+
#include "llvm/ADT/ilist_node.h"
46+
#include "llvm/ADT/ilist_node_options.h"
47+
#include "llvm/ADT/BitmaskEnum.h"
48+
49+
#include <algorithm>
50+
#include <utility>
51+
#include <optional>
52+
#include <vector>
53+
#include <string>
54+
#include <memory>
55+
#include <iterator>
56+
#include <map>
57+
#include <tuple>
58+
#include <limits>
59+
#include <set>
60+
#include <system_error>
61+
#include <functional>
62+
#include <numeric>
63+
#include <deque>
64+
#include <sstream>
65+
#include <queue>
66+
#include <type_traits>
67+
#include <mutex>
68+
#include <array>
69+
#include <list>
70+
#include <atomic>
71+
#include <unordered_map>
72+
#include <bitset>
73+
#include <new>
74+
#include <unordered_set>
75+
#include <iomanip>
76+
#include <thread>
77+
#include <variant>
78+
#include <future>
79+
#include <stack>
80+
#include <chrono>
81+
#include <initializer_list>
82+
#include <random>
83+
84+
#include <cctype>
85+
#include <cerrno>
86+
#include <cfloat>
87+
#include <cinttypes>
88+
#include <climits>
89+
#include <clocale>
90+
#include <cmath>
91+
#include <cstddef>
92+
#include <cstdint>
93+
#include <cstdio>
94+
#include <cstdlib>
95+
#include <cstring>
96+
#include <ctime>
97+
#include <cwchar>
98+
99+
#endif // LLVM_PRECOMPILEDHEADERS_H

Diff for: llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ DWARFAbbreviationDeclaration::extract(DataExtractor Data, uint64_t *OffsetPtr) {
6868

6969
// Read all of the abbreviation attributes and forms.
7070
while (Data.isValidOffset(*OffsetPtr)) {
71-
auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr, &Err));
71+
auto A = static_cast<dwarf::Attribute>(Data.getULEB128(OffsetPtr, &Err));
7272
if (Err)
7373
return std::move(Err);
7474

Diff for: llvm/lib/Object/Minidump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Type) const {
9292

9393
return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
9494
}
95-
template Expected<ArrayRef<Module>>
95+
template Expected<ArrayRef<minidump::Module>>
9696
MinidumpFile::getListStream(StreamType) const;
9797
template Expected<ArrayRef<Thread>>
9898
MinidumpFile::getListStream(StreamType) const;

Diff for: llvm/lib/Support/CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,21 @@ add_llvm_component_library(LLVMSupport
297297

298298
set(llvm_system_libs ${system_libs})
299299

300+
if (LLVM_ENABLE_LIB_PRECOMPILED_HEADERS)
301+
# CMake cannot use a C++ PCH in source targets that are non-C++
302+
# Ideally this would be handled by LLVMPrecompiledHeaders.cmake
303+
set(non_cpp_sources
304+
regcomp.c
305+
regerror.c
306+
regexec.c
307+
regfree.c
308+
regstrlcpy.c
309+
)
310+
foreach(source ${non_cpp_sources})
311+
set_source_files_properties(${source} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
312+
endforeach()
313+
endif()
314+
300315
# This block is only needed for llvm-config. When we deprecate llvm-config and
301316
# move to using CMake export, this block can be removed.
302317
if(LLVM_ENABLE_ZLIB)

0 commit comments

Comments
 (0)