Index: lib/ProfileData/Coverage/CoverageMappingReader.cpp =================================================================== --- lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -13,7 +13,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ProfileData/Coverage/CoverageMappingReader.h" -#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/Object/MachOUniversal.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Debug.h" @@ -331,7 +331,9 @@ typedef typename coverage::CovMapTraits::NameRefType NameRefType; - llvm::DenseSet UniqueFunctionMappingData; + // Maps function's name references to the indexes of their records + // in \c Records. + llvm::DenseMap FunctionRecords; InstrProfSymtab &ProfileNames; std::vector &Filenames; std::vector &Records; @@ -396,8 +398,14 @@ // Ignore this record if we already have a record that points to the same // function name. This is useful to ignore the redundant records for the // functions with ODR linkage. + // In addition, prefer records with non-zero hash values because they were + // emitted for real functions, whereas records with zero hashes are likely + // just placeholders for unused inline functions. NameRefType NameRef = CFR->template getFuncNameRef(); - if (!UniqueFunctionMappingData.insert(NameRef).second) { + auto InsertResult = + FunctionRecords.insert(std::make_pair(NameRef, Records.size())); + if (!InsertResult.second && + (!FuncHash || Records[InsertResult.first->second].FunctionHash)) { CFR++; continue; } @@ -406,9 +414,14 @@ if (std::error_code EC = CFR->template getFuncName(ProfileNames, FuncName)) return EC; - Records.push_back(BinaryCoverageReader::ProfileMappingRecord( + BinaryCoverageReader::ProfileMappingRecord FunctionRecord( Version, FuncName, FuncHash, Mapping, FilenamesBegin, - Filenames.size() - FilenamesBegin)); + Filenames.size() - FilenamesBegin); + if (InsertResult.second) { + Records.push_back(FunctionRecord); + } else { + Records[InsertResult.first->second] = FunctionRecord; + } CFR++; } return std::error_code(); Index: test/tools/llvm-cov/Inputs/prefer_used_to_unused.cpp =================================================================== --- /dev/null +++ test/tools/llvm-cov/Inputs/prefer_used_to_unused.cpp @@ -0,0 +1,5 @@ +#include "prefer_used_to_unused.h" + +int main() { + return sample_func(5); +} Index: test/tools/llvm-cov/Inputs/prefer_used_to_unused.proftext =================================================================== --- /dev/null +++ test/tools/llvm-cov/Inputs/prefer_used_to_unused.proftext @@ -0,0 +1,17 @@ +_Z11sample_funci +# Func Hash: +10 +# Num Counters: +2 +# Counter Values: +1 +1 + +main +# Func Hash: +0 +# Num Counters: +1 +# Counter Values: +1 + Index: test/tools/llvm-cov/prefer_used_to_unused.h =================================================================== --- /dev/null +++ test/tools/llvm-cov/prefer_used_to_unused.h @@ -0,0 +1,16 @@ +// Check that llvm-cov loads coverage mapping data for real function even though +// an unused function comes first. + +// If you need to rebuild the 'covmapping' file for this test, please use +// the following commands: +// clang++ -fprofile-instr-generate -fcoverage-mapping -o tmp -x c++ prefer_used_to_unused.h prefer_used_to_unused.cpp +// llvm-cov convert-for-testing -o prefer_used_to_unused.covmapping tmp + +// RUN: llvm-profdata merge %S/Inputs/prefer_used_to_unused.proftext -o %t.profdata +// RUN: llvm-cov show %S/Inputs/prefer_used_to_unused.covmapping -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s + +inline int sample_func(int A) { // CHECK: 1| [[@LINE]]|inline int sample_func(int A) { + if (A > 0) // CHECK-NEXT: 1| [[@LINE]]| if (A > 0) + return A; // CHECK-NEXT: 1| [[@LINE]]| return A; + return 0; // CHECK-NEXT: 0| [[@LINE]]| return 0; +} // CHECK-NEXT: 1| [[@LINE]]|}