From 2c286f1ff2987b8887fab22473331801b5de0809 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 27 Jun 2021 20:34:06 +0200 Subject: [PATCH] Include $PWD in hash for -fprofile-generate with relative directory For a relative profile directory D, GCC stores $PWD/D as part of the profile filename in an object file generated with -fprofile-generate, so we need to include the same information in the hash. Fixes #872. --- src/ccache.cpp | 12 +++++++-- test/suites/profiling.bash | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/ccache.cpp b/src/ccache.cpp index 4f81c63b0..0b3d23f76 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1648,9 +1648,17 @@ calculate_result_and_manifest_key(Context& ctx, if (ctx.args_info.profile_generate) { ASSERT(!ctx.args_info.profile_path.empty()); - LOG("Adding profile directory {} to our hash", ctx.args_info.profile_path); + + // For a relative profile directory D the compiler stores $PWD/D as part of + // the profile filename so we need to include the same information in the + // hash. + const std::string profile_path = + Util::is_absolute_path(ctx.args_info.profile_path) + ? ctx.args_info.profile_path + : FMT("{}/{}", ctx.apparent_cwd, ctx.args_info.profile_path); + LOG("Adding profile directory {} to our hash", profile_path); hash.hash_delimiter("-fprofile-dir"); - hash.hash(ctx.args_info.profile_path); + hash.hash(profile_path); } if (ctx.args_info.profile_use && !hash_profile_data_file(ctx, hash)) { diff --git a/test/suites/profiling.bash b/test/suites/profiling.bash index dbe36d41f..f9e0cba3b 100644 --- a/test/suites/profiling.bash +++ b/test/suites/profiling.bash @@ -97,6 +97,57 @@ SUITE_profiling() { expect_stat 'cache hit (direct)' 1 expect_stat 'cache miss' 3 + # ------------------------------------------------------------------------- + TEST "-fprofile-generate=dir in different directories" + + mkdir -p dir1/data dir2/data + + cd dir1 + + $CCACHE_COMPILE -Werror -fprofile-generate=data -c ../test.c \ + || test_failed "compilation error" + expect_stat 'cache hit (direct)' 0 + expect_stat 'cache miss' 1 + + $CCACHE_COMPILE -Werror -fprofile-generate=data -c ../test.c \ + || test_failed "compilation error" + expect_stat 'cache hit (direct)' 1 + expect_stat 'cache miss' 1 + + $COMPILER -Werror -fprofile-generate test.o -o test \ + || test_failed "compilation error" + + ./test || test_failed "execution error" + merge_profiling_data data + + $CCACHE_COMPILE -Werror -fprofile-use=data -c ../test.c \ + || test_failed "compilation error" + expect_stat 'cache hit (direct)' 1 + expect_stat 'cache miss' 2 + + cd ../dir2 + + $CCACHE_COMPILE -Werror -fprofile-generate=data -c ../test.c \ + || test_failed "compilation error" + expect_stat 'cache hit (direct)' 1 + expect_stat 'cache miss' 3 + + $CCACHE_COMPILE -Werror -fprofile-generate=data -c ../test.c \ + || test_failed "compilation error" + expect_stat 'cache hit (direct)' 2 + expect_stat 'cache miss' 3 + + $COMPILER -Werror -fprofile-generate test.o -o test \ + || test_failed "compilation error" + + ./test || test_failed "execution error" + merge_profiling_data data + + $CCACHE_COMPILE -Werror -fprofile-use=data -c ../test.c \ + || test_failed "compilation error" + # Note: No expect_stat here since GCC and Clang behave differently – just + # check that the compiler doesn't warn about not finding the profile data. + # ------------------------------------------------------------------------- TEST "-ftest-coverage with -fprofile-dir" -- 2.47.3