From b82c7fc65229c8b2b6a964f023f6ec59b3cf9210 Mon Sep 17 00:00:00 2001 From: Alexis Engelke Date: Fri, 20 Feb 2026 12:07:18 +0100 Subject: [PATCH] [CMake][LLVM] Add PCH infrastructure and LLVMSupport PCH (#176420) This patch implements PCH support. PCH is enabled by default, unless noted below, and can be disabled with -DCMAKE_DISABLE_PRECOMPILE_HEADERS=ON. * Libraries can define precompiled headers using a newly added PRECOMPILE_HEADERS keyword. If specified, the listed headers will be compiled into a pre-compiled header using standard CMake mechanisms. * Libraries that don't define their own PRECOMPILE_HEADERS but directly depend on a library or component that defines its own PCH will reuse that PCH. This reuse is not transitive to prevent excessive use of unrelated headers. If multiple dependencies provide a reusable PCH, the first one with the longest dependency chain (stored in the CMake target property LLVM_PCH_PRIORITY) is used. However, due to CMake limitations, only PCH from targets that are already defined can be reused; therefore libraries that should reuse a PCH must be defined later in the CMake file (=> add_subdirectory order matters). * Libraries and executables can prevent PCH reuse with the keyword DISABLE_PCH_REUSE. This both prevents reuse from dependencies and reuse by other dependants. This is useful when, e.g., internal headers are used in the PCH or the used headers are unlikely to provide benefits for dependants. * Precompiled headers are only used for C++ sources, not for C. * With GCC, PCH provide very little benefits (tested with GCC 14 and 15) due to increased template instatiation costs, but substantially increase max-rss and build directory size. Therefore, disable PCH with GCC by default; this can be explicitly overridden on the command line with -DCMAKE_DISABLE_PRECOMPILE_HEADERS=OFF. * With ccache and non-Clang compilers, changes in macro definitions are not always accurately forwarded with ccache's preprocessed mode. To be on the safe side, when ccache is enabled, disable PCH with all non-Clang compilers; this can be explicitly overridden. * With sccache, changes in macro definitions are not identified, which in some cases can lead to false positive cache hits. Conservatively disable PCH with sccache by default. * Add a base PCH to LLVMSupport, which includes widely used standard library and Support+ADT headers. The pch.h is placed in include so that later PCH headers can extend that list of headers. * Flang PCH use is ported to the general mechanism. Addition of PCH headers for other components (e.g., IR, CodeGen) will be posted as separate PRs. RFC: https://discourse.llvm.org/t/rfc-use-pre-compiled-headers-to-speed-up-llvm-build-by-1-5-2x/89345 --- flang/CMakeLists.txt | 10 -- flang/lib/Evaluate/CMakeLists.txt | 18 ++-- flang/lib/Frontend/CMakeLists.txt | 16 ++-- flang/lib/Lower/CMakeLists.txt | 22 ++--- flang/lib/Parser/CMakeLists.txt | 16 ++-- flang/lib/Semantics/CMakeLists.txt | 18 ++-- llvm/CMakeLists.txt | 16 ++++ llvm/cmake/modules/AddLLVM.cmake | 102 +++++++++++++++++++-- llvm/cmake/modules/HandleLLVMOptions.cmake | 48 ++++++++++ llvm/include/llvm/Support/pch.h | 75 +++++++++++++++ llvm/lib/CMakeLists.txt | 2 +- llvm/lib/Support/CMakeLists.txt | 7 +- polly/lib/CMakeLists.txt | 4 + 13 files changed, 289 insertions(+), 65 deletions(-) create mode 100644 llvm/include/llvm/Support/pch.h diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt index c01eb56d5e49..e21304d2e4da 100644 --- a/flang/CMakeLists.txt +++ b/flang/CMakeLists.txt @@ -441,11 +441,6 @@ if (LLVM_COMPILER_IS_GCC_COMPATIBLE) if (BUILD_SHARED_LIBS AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-semantic-interposition") endif() - - # GCC requires this flag in order for precompiled headers to work with ccache - if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_DISABLE_PRECOMPILE_HEADERS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpch-preprocess") - endif() endif() # Clang on Darwin enables non-POSIX extensions by default, which allows the @@ -456,11 +451,6 @@ if (APPLE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_POSIX_C_SOURCE=200809") endif() -# Clang requires this flag in order for precompiled headers to work with ccache -if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_DISABLE_PRECOMPILE_HEADERS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -fno-pch-timestamp") -endif() - list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS) # Determine HOST_LINK_VERSION on Darwin. diff --git a/flang/lib/Evaluate/CMakeLists.txt b/flang/lib/Evaluate/CMakeLists.txt index 24a1c9004bc3..472ecb6d8d07 100644 --- a/flang/lib/Evaluate/CMakeLists.txt +++ b/flang/lib/Evaluate/CMakeLists.txt @@ -66,15 +66,8 @@ add_flang_library(FortranEvaluate ${LIBPGMATH} ${QUADMATHLIB} - LINK_COMPONENTS - Support - - DEPENDS - acc_gen - omp_gen -) - -target_precompile_headers(FortranEvaluate PRIVATE + DISABLE_PCH_REUSE + PRECOMPILE_HEADERS [["flang/Evaluate/common.h"]] [["flang/Evaluate/call.h"]] [["flang/Evaluate/traverse.h"]] @@ -86,4 +79,11 @@ target_precompile_headers(FortranEvaluate PRIVATE [["flang/Evaluate/integer.h"]] [["flang/Evaluate/expression.h"]] [["flang/Evaluate/tools.h"]] + + LINK_COMPONENTS + Support + + DEPENDS + acc_gen + omp_gen ) diff --git a/flang/lib/Frontend/CMakeLists.txt b/flang/lib/Frontend/CMakeLists.txt index 4ebe497e6567..bd67ec444f35 100644 --- a/flang/lib/Frontend/CMakeLists.txt +++ b/flang/lib/Frontend/CMakeLists.txt @@ -13,6 +13,14 @@ add_flang_library(flangFrontend TextDiagnosticBuffer.cpp TextDiagnostic.cpp + DISABLE_PCH_REUSE + PRECOMPILE_HEADERS + [["flang/Parser/parsing.h"]] + [["flang/Parser/parse-tree.h"]] + [["flang/Parser/dump-parse-tree.h"]] + [["flang/Lower/PFTBuilder.h"]] + [["flang/Lower/Bridge.h"]] + DEPENDS CUFDialect FIRDialect @@ -78,11 +86,3 @@ add_flang_library(flangFrontend clangBasic clangOptions ) - -target_precompile_headers(flangFrontend PRIVATE - [["flang/Parser/parsing.h"]] - [["flang/Parser/parse-tree.h"]] - [["flang/Parser/dump-parse-tree.h"]] - [["flang/Lower/PFTBuilder.h"]] - [["flang/Lower/Bridge.h"]] -) diff --git a/flang/lib/Lower/CMakeLists.txt b/flang/lib/Lower/CMakeLists.txt index 230a56ab66ec..f5424c78b9a9 100644 --- a/flang/lib/Lower/CMakeLists.txt +++ b/flang/lib/Lower/CMakeLists.txt @@ -38,6 +38,17 @@ add_flang_library(FortranLower Support/Utils.cpp SymbolMap.cpp VectorSubscripts.cpp + + DISABLE_PCH_REUSE + PRECOMPILE_HEADERS + [["flang/Lower/ConvertExpr.h"]] + [["flang/Lower/SymbolMap.h"]] + [["flang/Lower/AbstractConverter.h"]] + [["flang/Lower/IterationSpace.h"]] + [["flang/Lower/CallInterface.h"]] + [["flang/Lower/BoxAnalyzer.h"]] + [["flang/Lower/PFTBuilder.h"]] + [["flang/Lower/DirectivesCommon.h"]] DEPENDS CUFAttrs @@ -79,14 +90,3 @@ add_flang_library(FortranLower MLIRLLVMDialect MLIRSCFToControlFlow ) - -target_precompile_headers(FortranLower PRIVATE - [["flang/Lower/ConvertExpr.h"]] - [["flang/Lower/SymbolMap.h"]] - [["flang/Lower/AbstractConverter.h"]] - [["flang/Lower/IterationSpace.h"]] - [["flang/Lower/CallInterface.h"]] - [["flang/Lower/BoxAnalyzer.h"]] - [["flang/Lower/PFTBuilder.h"]] - [["flang/Lower/DirectivesCommon.h"]] -) diff --git a/flang/lib/Parser/CMakeLists.txt b/flang/lib/Parser/CMakeLists.txt index 20c6c2a7c8f8..e0479b0da3eb 100644 --- a/flang/lib/Parser/CMakeLists.txt +++ b/flang/lib/Parser/CMakeLists.txt @@ -25,6 +25,14 @@ add_flang_library(FortranParser unparse.cpp user-state.cpp + DISABLE_PCH_REUSE + PRECOMPILE_HEADERS + [["flang/Parser/parsing.h"]] + [["flang/Parser/parse-tree.h"]] + [["flang/Parser/provenance.h"]] + [["flang/Parser/message.h"]] + [["flang/Parser/parse-tree-visitor.h"]] + LINK_LIBS FortranSupport @@ -37,11 +45,3 @@ add_flang_library(FortranParser omp_gen acc_gen ) - -target_precompile_headers(FortranParser PRIVATE - [["flang/Parser/parsing.h"]] - [["flang/Parser/parse-tree.h"]] - [["flang/Parser/provenance.h"]] - [["flang/Parser/message.h"]] - [["flang/Parser/parse-tree-visitor.h"]] -) diff --git a/flang/lib/Semantics/CMakeLists.txt b/flang/lib/Semantics/CMakeLists.txt index 109bc2dbb856..44e6dfb4dd09 100644 --- a/flang/lib/Semantics/CMakeLists.txt +++ b/flang/lib/Semantics/CMakeLists.txt @@ -53,6 +53,15 @@ add_flang_library(FortranSemantics type.cpp unparse-with-symbols.cpp + DISABLE_PCH_REUSE + PRECOMPILE_HEADERS + [["flang/Semantics/semantics.h"]] + [["flang/Semantics/type.h"]] + [["flang/Semantics/openmp-modifiers.h"]] + [["flang/Semantics/expression.h"]] + [["flang/Semantics/tools.h"]] + [["flang/Semantics/symbol.h"]] + DEPENDS acc_gen omp_gen @@ -68,12 +77,3 @@ add_flang_library(FortranSemantics FrontendOpenACC TargetParser ) - -target_precompile_headers(FortranSemantics PRIVATE - [["flang/Semantics/semantics.h"]] - [["flang/Semantics/type.h"]] - [["flang/Semantics/openmp-modifiers.h"]] - [["flang/Semantics/expression.h"]] - [["flang/Semantics/tools.h"]] - [["flang/Semantics/symbol.h"]] -) diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index 1f88c84f2c60..dbf8b9e4aa66 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -310,6 +310,22 @@ if(LLVM_CCACHE_BUILD) set(CCACHE_PROGRAM "CCACHE_DIR=${LLVM_CCACHE_DIR} ${CCACHE_PROGRAM}") endif() set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM}) + + if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # ccache with PCH can lead to false-positives when only a macro + # definition changes with non-Clang compilers, because macro definitions + # are not compared in preprocessed mode. + # See: https://github.com/ccache/ccache/issues/1668 + if(NOT DEFINED CMAKE_DISABLE_PRECOMPILE_HEADERS) + message(NOTICE "Using ccache with precompiled headers with non-Clang " + "compilers is not supported. CMAKE_DISABLE_PRECOMPILE_HEADERS will be set to ON. " + "Pass -DCMAKE_DISABLE_PRECOMPILE_HEADERS=OFF to override this.") + set(CMAKE_DISABLE_PRECOMPILE_HEADERS "ON") + elseif(NOT CMAKE_DISABLE_PRECOMPILE_HEADERS) + message(WARNING "Using ccache with precompiled headers with non-Clang " + "compilers is not supported.") + endif() + endif() else() # Until a way to reliably configure ccache on Windows is found, # disable precompiled headers for Windows + ccache builds diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index 81aaf6034cca..8738ef2bf788 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -79,6 +79,64 @@ function(llvm_update_compile_flags name) target_compile_definitions(${name} PRIVATE ${LLVM_COMPILE_DEFINITIONS}) endfunction() +function(llvm_update_pch name) + if(LLVM_REQUIRES_RTTI OR LLVM_REQUIRES_EH) + # Non-default RTTI/EH results in incompatible flags, precluding PCH reuse. + set(ARG_DISABLE_PCH_REUSE ON) + endif() + + if(NOT ARG_PRECOMPILE_HEADERS) + # We only use PCH for C++. For targets with C source files that re-use a + # precompiled headers from another target, CMake complains that there is no + # PCH for C. There doesn't seem to be a disable PCH reuse for C files only, + # so disable PCH reuse for targets that contain C sources. + get_property(srcs TARGET ${name} PROPERTY SOURCES) + foreach(src ${srcs}) + get_filename_component(extension ${src} EXT) + if(extension STREQUAL ".c") + message(DEBUG "Disable PCH for ${name} due to C file ${src}") + set(ARG_DISABLE_PCH_REUSE ON) + break() + endif() + endforeach() + endif() + + # Find PCH with highest priority from dependencies. We reuse the first PCH + # with the highest priority. If the target has its own set of PCH, we give it + # a higher priority so that dependents will prefer the new PCH. We don't do + # transitive PCH reuse, because this causes too many unrelated naming + # collisions (e.g., in A -> B -> C{pch}, only B would reuse the PCH of C). + set(pch_priority 0) + llvm_map_components_to_libnames(libs + ${LLVM_LINK_COMPONENTS} + ) + list(APPEND libs ${ARG_LINK_LIBS}) + foreach(lib ${libs}) + if(TARGET ${lib}) + get_target_property(lib_pch_priority ${lib} LLVM_PCH_PRIORITY) + if(${lib_pch_priority} GREATER ${pch_priority}) + set(pch_priority ${lib_pch_priority}) + set(pch_reuse ${lib}) + endif() + endif() + endforeach() + + if(ARG_PRECOMPILE_HEADERS) + message(DEBUG "Adding PCH ${ARG_PRECOMPILE_HEADERS} for ${name} (prio ${pch_priority})") + target_precompile_headers(${name} PRIVATE $<$:${ARG_PRECOMPILE_HEADERS}>) + if(NOT ARG_DISABLE_PCH_REUSE) + # Set priority so that dependants can reuse the PCH. + math(EXPR pch_priority "${pch_priority} + 1") + set_target_properties(${name} PROPERTIES LLVM_PCH_PRIORITY ${pch_priority}) + endif() + elseif(pch_reuse AND NOT ARG_DISABLE_PCH_REUSE) + message(DEBUG "Using PCH ${pch_reuse} for ${name} (prio ${pch_priority})") + target_precompile_headers(${name} REUSE_FROM ${pch_reuse}) + else() + message(DEBUG "Using NO PCH for ${name}") + endif() +endfunction() + function(add_llvm_symbol_exports target_name export_file) if("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin") set(native_export_file "${target_name}.exports") @@ -485,6 +543,13 @@ endfunction(set_windows_version_resource_properties) # Corresponds to OUTPUT_NAME in target properties. # DEPENDS targets... # Same semantics as add_dependencies(). +# PRECOMPILE_HEADERS include_directives... +# Pre-compiled C++ headers to use. PCH can be reused by dependants. If +# specified, no PCHs from dependencies will be reused. +# DISABLE_PCH_REUSE +# Disable reuse of pre-compiled headers in both directions: the library will +# not reuse the PCH of a dependency and a defined PCH will not be offered +# for reuse by dependants. # LINK_COMPONENTS components... # Same as the variable LLVM_LINK_COMPONENTS. # LINK_LIBS lib_targets... @@ -504,11 +569,12 @@ endfunction(set_windows_version_resource_properties) # ) function(llvm_add_library name) cmake_parse_arguments(ARG - "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB" + "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB;DISABLE_PCH_REUSE" "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH" - "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS" + "ADDITIONAL_HEADERS;PRECOMPILE_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS" ${ARGN}) list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS}) + list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS}) if(ARG_ADDITIONAL_HEADERS) # Pass through ADDITIONAL_HEADERS. set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS}) @@ -550,6 +616,7 @@ function(llvm_add_library name) ${ALL_FILES} ) llvm_update_compile_flags(${obj_name}) + llvm_update_pch(${obj_name}) if(CMAKE_GENERATOR STREQUAL "Xcode") set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c) file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n") @@ -604,7 +671,7 @@ function(llvm_add_library name) ${output_name} OBJLIBS ${ALL_FILES} # objlib LINK_LIBS ${ARG_LINK_LIBS} - LINK_COMPONENTS ${ARG_LINK_COMPONENTS} + LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} ) set_target_properties(${name_static} PROPERTIES FOLDER "${subproject_title}/Libraries") @@ -676,6 +743,11 @@ function(llvm_add_library name) # $ doesn't require compile flags. if(NOT obj_name) llvm_update_compile_flags(${name}) + llvm_update_pch(${name}) + else() + target_precompile_headers(${name} REUSE_FROM ${obj_name}) + get_target_property(pch_priority ${obj_name} LLVM_PCH_PRIORITY) + set_target_properties(${name} PROPERTIES LLVM_PCH_PRIORITY ${pch_priority}) endif() add_link_opts( ${name} ) if(ARG_OUTPUT_NAME) @@ -758,8 +830,7 @@ function(llvm_add_library name) target_compile_definitions(${name} PRIVATE LLVM_BUILD_STATIC) endif() llvm_map_components_to_libnames(llvm_libs - ${ARG_LINK_COMPONENTS} - ${LLVM_LINK_COMPONENTS} + ${LLVM_LINK_COMPONENTS} ) endif() else() @@ -770,7 +841,7 @@ function(llvm_add_library name) # It would be nice to verify that we have the dependencies for this library # name, but using get_property(... SET) doesn't suffice to determine if a # property has been set to an empty value. - set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS}) + set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS}) # This property is an internal property only used to make sure the # link step applied in LLVMBuildResolveComponentsLink uses the same @@ -993,6 +1064,7 @@ macro(generate_llvm_objects name) ${ALL_FILES} ) llvm_update_compile_flags(${obj_name}) + llvm_update_pch(${obj_name}) set(ALL_FILES "$") if(ARG_DEPENDS) add_dependencies(${obj_name} ${ARG_DEPENDS}) @@ -1039,7 +1111,7 @@ endmacro() macro(add_llvm_executable name) cmake_parse_arguments(ARG - "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS;EXPORT_SYMBOLS" + "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS;EXPORT_SYMBOLS;DISABLE_PCH_REUSE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN}) @@ -1077,9 +1149,23 @@ macro(add_llvm_executable name) set_windows_version_resource_properties(${name} ${windows_resource_file}) endif() + # CMake position-independent code sets -fPIE for source files in executables. + # With LLVM_ENABLE_PIC, we add -fPIC to all source files, but -fPIE is added + # later and therefore wins. To avoid option mismatch between the PCH (-fPIC) + # and the executable (-fPIE), disable PCH reuse for PIE. + get_target_property(cmake_pie ${name} POSITION_INDEPENDENT_CODE) + if(${cmake_pie}) + set(ARG_DISABLE_PCH_REUSE ON) + endif() + # $ doesn't require compile flags. if(NOT LLVM_ENABLE_OBJLIB) llvm_update_compile_flags(${name}) + llvm_update_pch(${name}) + elseif(NOT ARG_DISABLE_PCH_REUSE) + target_precompile_headers(${name} REUSE_FROM ${obj_name}) + get_target_property(pch_priority ${obj_name} LLVM_PCH_PRIORITY) + set_target_properties(${name} PROPERTIES LLVM_PCH_PRIORITY ${pch_priority}) endif() if (ARG_SUPPORT_PLUGINS AND NOT "${CMAKE_SYSTEM_NAME}" MATCHES "AIX") @@ -1787,7 +1873,7 @@ function(add_unittest test_suite test_name) endif() list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream - add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) + add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH DISABLE_PCH_REUSE ${ARGN}) get_subproject_title(subproject_title) set_target_properties(${test_name} PROPERTIES FOLDER "${subproject_title}/Tests/Unit") diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake index b5372eea259f..35ab06fd9674 100644 --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -1303,11 +1303,59 @@ if (LLVM_BUILD_INSTRUMENTED AND LLVM_BUILD_INSTRUMENTED_COVERAGE) message(FATAL_ERROR "LLVM_BUILD_INSTRUMENTED and LLVM_BUILD_INSTRUMENTED_COVERAGE cannot both be specified") endif() +if(NOT DEFINED CMAKE_DISABLE_PRECOMPILE_HEADERS) + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + # Pre-compiled headers with GCC (tested versions 14+15) provide very little + # compile-time improvements, but substantially increase the build dir size. + # Therefore, disable PCH with GCC by default. + message(NOTICE "Precompiled headers are disabled by default with GCC. " + "Pass -DCMAKE_DISABLE_PRECOMPILE_HEADERS=OFF to override.") + set(CMAKE_DISABLE_PRECOMPILE_HEADERS ON) + endif() + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 18) + # Clang before version 18 has problems with mixed default visibility. + # Therefore, disable PCH with older Clang versions by default. + message(NOTICE "Precompiled headers are disabled by default with Clang <18. " + "Pass -DCMAKE_DISABLE_PRECOMPILE_HEADERS=OFF to override.") + set(CMAKE_DISABLE_PRECOMPILE_HEADERS ON) + endif() + + # Warn on possibly unintended interactions with ccache/sccache if the user + # sets this via CMAKE_CXX_COMPILER_LAUNCHER (and not using LLVM_CCACHE_BUILD). + if(CMAKE_CXX_COMPILER_LAUNCHER MATCHES "sccache") + # sccache doesn't support PCH, this can lead to false-positives when only + # a macro definition changes. For Clang, this sometimes even works + # correctly, but not always, so disable by default to be safe. + # See: https://github.com/mozilla/sccache/issues/615 + # See: https://github.com/mozilla/sccache/issues/2562 + message(NOTICE "Precompiled headers are disabled by default with sccache. " + "Pass -DCMAKE_DISABLE_PRECOMPILE_HEADERS=OFF to override.") + set(CMAKE_DISABLE_PRECOMPILE_HEADERS ON) + elseif(CMAKE_CXX_COMPILER_LAUNCHER MATCHES "ccache" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # ccache with PCH can lead to false-positives when only a macro + # definition changes with non-Clang compilers, because macro definitions + # are not compared in preprocessed mode. + # See: https://github.com/ccache/ccache/issues/1668 + message(WARNING "Using ccache with precompiled headers with non-Clang " + "compilers is not supported and may lead to false positives. " + "Set CMAKE_DISABLE_PRECOMPILE_HEADERS to ON/OFF to silence this warning.") + endif() +endif() if(NOT CMAKE_DISABLE_PRECOMPILE_HEADERS) + message(STATUS "Precompiled headers enabled.") # CMake weirdly marks all PCH as system headers. This undocumented variable # can be used to suppress the "#pragma clang system_header". # See: https://gitlab.kitware.com/cmake/cmake/-/issues/21219 set(CMAKE_PCH_PROLOGUE "") + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # Clang requires this flag in order for precompiled headers to work with ccache. + append("-Xclang -fno-pch-timestamp" CMAKE_CXX_FLAGS) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + # GCC requires this flag in order for precompiled headers to work with ccache. + append("-fpch-preprocess" CMAKE_CXX_FLAGS) + endif() +else() + message(STATUS "Precompiled headers disabled.") endif() set(LLVM_THINLTO_CACHE_PATH "${PROJECT_BINARY_DIR}/lto.cache" CACHE STRING "Set ThinLTO cache path. This can be used when building LLVM from several different directiories.") diff --git a/llvm/include/llvm/Support/pch.h b/llvm/include/llvm/Support/pch.h new file mode 100644 index 000000000000..ccfeceb231f6 --- /dev/null +++ b/llvm/include/llvm/Support/pch.h @@ -0,0 +1,75 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Precompiled header for LLVMSupport. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/ADL.h" +#include "llvm/ADT/APFloat.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/APSInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/BitVector.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Hashing.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/Statistic.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/JSON.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/VersionTuple.h" +#include "llvm/Support/YAMLTraits.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/llvm/lib/CMakeLists.txt b/llvm/lib/CMakeLists.txt index 9af185485243..f0faaff30799 100644 --- a/llvm/lib/CMakeLists.txt +++ b/llvm/lib/CMakeLists.txt @@ -3,8 +3,8 @@ include(LLVM-Build) # `Demangle', `Support' and `TableGen' libraries are added on the top-level # CMakeLists.txt -add_subdirectory(ABI) add_subdirectory(IR) +add_subdirectory(ABI) add_subdirectory(FuzzMutate) add_subdirectory(FileCheck) add_subdirectory(InterfaceStub) diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index 099b0e55579b..b4e39fced385 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -135,7 +135,6 @@ if (UNIX AND "${CMAKE_SYSTEM_NAME}" MATCHES "AIX") endif() add_subdirectory(BLAKE3) -add_subdirectory(LSP) add_llvm_component_library(LLVMSupport ABIBreak.cpp @@ -322,6 +321,9 @@ add_llvm_component_library(LLVMSupport ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support ${Backtrace_INCLUDE_DIRS} + PRECOMPILE_HEADERS + [["llvm/Support/pch.h"]] + LINK_LIBS ${system_libs} ${imported_libs} ${delayload_flags} @@ -396,3 +398,6 @@ target_include_directories(LLVMSupport PRIVATE ${LLVM_THIRD_PARTY_DIR}/siphash/include ) + +# SupportLSP depends on Support and therefore must be included afterwards. +add_subdirectory(LSP) diff --git a/polly/lib/CMakeLists.txt b/polly/lib/CMakeLists.txt index 41c3b97764ac..6f6e4d5782df 100644 --- a/polly/lib/CMakeLists.txt +++ b/polly/lib/CMakeLists.txt @@ -91,6 +91,10 @@ add_llvm_pass_plugin(Polly Transform/MatmulOptimizer.cpp ${POLLY_HEADER_FILES} + # Polly unconditionally uses -fno-rtti -fno-exceptions. This can be + # incompatible with PCH if LLVM is built with RTTI or exceptions. + DISABLE_PCH_REUSE + LINK_COMPONENTS ${POLLY_COMPONENTS} )