From 7f3e822efb1bee779f8a520c5efb48ce33d1bfef Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 9 Dec 2025 19:19:17 +0100 Subject: [PATCH] fix: Avoid hashing CWD parts of -march=native expansion for Clang Fixes #1658. --- src/ccache/ccache.cpp | 32 ++++++++++++++++++++--- test/fake-compilers/clang-march-native.sh | 2 +- test/suites/base.bash | 17 ++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index b44296db..7aa40c65 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -2045,10 +2045,34 @@ hash_native_args(Context& ctx, const util::Args& native_args, Hash& hash) hash.hash_delimiter(native_args.to_string()); - // We could potentially work out exactly which options -m*=native expand to - // and hash only those, but to keep things simple we include the full line - // where cc1 was found for now. - hash.hash(*line_to_hash); + if (ctx.config.is_compiler_group_clang()) { + // For Clang, extract and hash only architecture-related options to avoid + // hashing CWD-dependent paths like -fdebug-compilation-dir and + // -fcoverage-compilation-dir which cause cache misses when compiling from + // different directories. + // + // We specifically look for: + // + // - "-target-cpu" followed by CPU name (e.g., "alderlake") + // - "-tune-cpu" followed by CPU name + // - "-target-feature" followed by feature flag (e.g., "+avx2", "-avx512f") + bool hash_value = false; + for (const auto token : util::Tokenizer(*line_to_hash, " ")) { + if (hash_value) { + hash.hash(' '); + hash.hash(token); + hash_value = false; + } else if (token == "\"-target-cpu\"" || token == "\"-tune-cpu\"" + || token == "\"-target-feature\"") { + hash.hash(token); + hash_value = true; + } + } + } else { + // For GCC, the full line should be safe to hash as it doesn't contain + // CWD-dependent paths. + hash.hash(*line_to_hash); + } return {}; } diff --git a/test/fake-compilers/clang-march-native.sh b/test/fake-compilers/clang-march-native.sh index 32d6a7b5..e7f25776 100755 --- a/test/fake-compilers/clang-march-native.sh +++ b/test/fake-compilers/clang-march-native.sh @@ -7,7 +7,7 @@ if [ "$1" = "-###" ]; then ... InstalledDir: /usr/bin (in-process) - "/example/bin/clang" "-cc1" $CC1_ARGS + "/example/bin/clang" "-cc1" "-target-cpu" "$CC1_ARGS" EOF echo "bin/cc1" elif [ "$1" = "-E" ]; then diff --git a/test/suites/base.bash b/test/suites/base.bash index cdb7daff..e6ed77e9 100644 --- a/test/suites/base.bash +++ b/test/suites/base.bash @@ -1702,6 +1702,23 @@ EOF expect_stat preprocessed_cache_hit 2 expect_stat cache_miss 2 + # ------------------------------------------------------------------------- + if $COMPILER -march=native -c test1.c 2>/dev/null; then + TEST "-march=native, no CWD in input hash" + mkdir a b + cp test1.c a + cp test1.c b + cd a + $CCACHE_COMPILE -march=native -c test1.c + expect_stat preprocessed_cache_hit 0 + expect_stat cache_miss 1 + cd ../b + $CCACHE_COMPILE -march=native -c test1.c + cd .. + expect_stat preprocessed_cache_hit 1 + expect_stat cache_miss 1 + fi + # ------------------------------------------------------------------------- TEST "Handling of compiler-only arguments" -- 2.47.3