diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c --- a/compiler-rt/lib/profile/InstrProfilingFile.c +++ b/compiler-rt/lib/profile/InstrProfilingFile.c @@ -993,6 +993,12 @@ */ COMPILER_RT_VISIBILITY void __llvm_profile_initialize(void) { + const uint64_t NumCounters = __llvm_profile_get_num_counters( + __llvm_profile_begin_counters(), __llvm_profile_end_counters()); + // If there are no counters, do not initialize profile runtime. + if (!NumCounters) + return; + __llvm_profile_initialize_file(); if (!__llvm_profile_is_continuous_mode_enabled()) __llvm_profile_register_write_file_atexit(); diff --git a/compiler-rt/test/profile/Linux/instrprof-profile-filter-no-profile.c b/compiler-rt/test/profile/Linux/instrprof-profile-filter-no-profile.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/profile/Linux/instrprof-profile-filter-no-profile.c @@ -0,0 +1,8 @@ +// Test that no profile is generated when a src file is not in the +// selected files list provided via -fprofile-list. +// RUN: echo "src:other.c" | sed -e 's/\\/\\\\/g' > %t-file.list +// RUN: %clang_profgen -fprofile-list=%t-file.list -o %t %s +// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t +// RUN: not test -f %t.profraw + +int main() { return 0; }