[Build] set CCACHE_SLOPPINESS to enable ccache with PCH (#24757)
A recent LLVM bump enables precompiled headers by default; ccache can't
cache PCH compilations, reducing the cache hit rate. Set
CCACHE_SLOPPINESS to enable ccache with PCH
---------
Signed-off-by: Jelle Schühmacher <schuehmacher@roofline.ai>
diff --git a/build_tools/cmake/setup_ccache.sh b/build_tools/cmake/setup_ccache.sh
index 4743d95..97f485e 100644
--- a/build_tools/cmake/setup_ccache.sh
+++ b/build_tools/cmake/setup_ccache.sh
@@ -40,6 +40,11 @@
export IREE_USE_CCACHE=1
export CMAKE_C_COMPILER_LAUNCHER="$(which ccache)"
export CMAKE_CXX_COMPILER_LAUNCHER="$(which ccache)"
+
+ # LLVM builds with precompiled headers by default when using Clang >= 18.
+ # ccache refuses to cache any compilation that uses a PCH
+ export CCACHE_SLOPPINESS="${CCACHE_SLOPPINESS:+${CCACHE_SLOPPINESS},}pch_defines,time_macros"
+
ccache --zero-stats
ccache --show-stats
else
diff --git a/docs/website/docs/developers/building/cmake-with-ccache.md b/docs/website/docs/developers/building/cmake-with-ccache.md
index bfba13e..f1d60bf 100644
--- a/docs/website/docs/developers/building/cmake-with-ccache.md
+++ b/docs/website/docs/developers/building/cmake-with-ccache.md
@@ -60,7 +60,8 @@
Use the CMake
[COMPILER_LAUNCHER functionality](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_LAUNCHER.html)
by setting `CMAKE_C_COMPILER_LAUNCHER=ccache` and
-`CMAKE_CXX_COMPILER_LAUNCHER=ccache` in your
+`CMAKE_CXX_COMPILER_LAUNCHER=ccache` in your CMake configure command or in your
+environment.
Notes:
@@ -69,6 +70,20 @@
based on wrapping the compiler in a script that prepends `ccache`. See this
[article](https://crascit.com/2016/04/09/using-ccache-with-cmake/).
+## `ccache` and precompiled headers
+
+`third_party/llvm-project` builds with precompiled headers by default when
+using Clang 18 or newer. `ccache` will not cache a compilation that uses a
+precompiled header unless it is configured to be sloppy about `#define`
+directives and time macros.
+
+`build_tools/cmake/setup_ccache.sh` sets this for you. If you configure
+`ccache` yourself, set it once as persistent `ccache` configuration:
+
+```shell
+ccache --set-config=sloppiness=pch_defines,time_macros
+```
+
## Ensuring that `ccache` is used and monitoring cache hits
The `ccache -s` command dumps statistics, including a cache hit count and ratio.