Index: include/llvm/ProfileData/Coverage/CoverageMappingReader.h =================================================================== --- include/llvm/ProfileData/Coverage/CoverageMappingReader.h +++ include/llvm/ProfileData/Coverage/CoverageMappingReader.h @@ -103,6 +103,16 @@ std::error_code read(); }; +/// \brief Checks if the given coverage mapping data is exported for +/// an unused function. +class RawCoverageMappingDummyChecker : public RawCoverageReader { +public: + RawCoverageMappingDummyChecker(StringRef MappingData) + : RawCoverageReader(MappingData) {} + + ErrorOr isDummy(); +}; + /// \brief Reader for the raw coverage mapping data. class RawCoverageMappingReader : public RawCoverageReader { ArrayRef TranslationUnitFilenames; 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" @@ -290,6 +290,39 @@ return std::error_code(); } +// A dummy coverage mapping data consists from only one region record +// with zero count. +ErrorOr RawCoverageMappingDummyChecker::isDummy() { + uint64_t NumFileMappings; + if (auto Err = readSize(NumFileMappings)) + return Err; + if (NumFileMappings != 1) + return false; + // We don't expect any specific value for the filename index, just skip it. + uint64_t FilenameIndex; + if (auto Err = + readIntMax(FilenameIndex, std::numeric_limits::max())) + return Err; + uint64_t NumExpressions; + if (auto Err = readSize(NumExpressions)) + return Err; + if (NumExpressions != 0) + return false; + uint64_t NumRegions; + if (auto Err = readSize(NumRegions)) + return Err; + if (NumRegions != 1) + return false; + uint64_t EncodedCounterAndRegion; + if (auto Err = readIntMax(EncodedCounterAndRegion, + std::numeric_limits::max())) + return Err; + unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask; + if (Tag != Counter::Zero) + return false; + return true; +} + std::error_code InstrProfSymtab::create(SectionRef &Section) { if (auto Err = Section.getContents(Data)) return Err; @@ -331,7 +364,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; @@ -381,8 +416,8 @@ // before reading the next map. Buf += alignmentAdjustment(Buf, 8); - auto CFR = reinterpret_cast(FunBuf); - while ((const char *)CFR < FunEnd) { + for (auto CFR = reinterpret_cast(FunBuf); + (const char *)CFR < FunEnd; CFR++) { // Read the function information uint32_t DataSize = CFR->template getDataSize(); uint64_t FuncHash = CFR->template getFuncHash(); @@ -396,20 +431,51 @@ // 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 real coverage mapping data to dummy + // records, which were emitted for inline functions which were seen but + // not used in the corresponding translation unit. NameRefType NameRef = CFR->template getFuncNameRef(); - if (!UniqueFunctionMappingData.insert(NameRef).second) { - CFR++; - continue; + auto InsertResult = + FunctionRecords.insert(std::make_pair(NameRef, Records.size())); + bool AlreadySeen = !InsertResult.second; + size_t RecordIndex = InsertResult.first->second; + if (AlreadySeen) { + // If the hash value for the old record is not zero, it's definitely + // not a dummy record and we don't need to replace it. + if (Records[RecordIndex].FunctionHash) + continue; + if (!FuncHash) { + // The hash value for a used function might be zero, for example, + // if the function is very simple. We need to analyse the mapping + // data itself in order to distinguish real records from dummy ones. + auto NewIsDummyOrErr = + RawCoverageMappingDummyChecker(Mapping).isDummy(); + if (auto EC = NewIsDummyOrErr.getError()) + return EC; + if (NewIsDummyOrErr.get()) + continue; + auto OldIsDummyOrErr = RawCoverageMappingDummyChecker( + Records[RecordIndex].CoverageMapping) + .isDummy(); + if (auto EC = OldIsDummyOrErr.getError()) + return EC; + if (!OldIsDummyOrErr.get()) + continue; + } } StringRef FuncName; 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)); - CFR++; + Filenames.size() - FilenamesBegin); + if (AlreadySeen) { + Records[RecordIndex] = FunctionRecord; + } else { + Records.push_back(FunctionRecord); + } } 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 sampleFunc(5) + simpleFunc(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,25 @@ +_Z10sampleFunci +# Func Hash: +10 +# Num Counters: +2 +# Counter Values: +1 +1 + +main +# Func Hash: +0 +# Num Counters: +1 +# Counter Values: +1 + +_Z10simpleFunci +# 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,22 @@ +// 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 + +// Coverage data for this function has a non-zero hash value if it is used in the translation unit. +inline int sampleFunc(int A) { // CHECK: 1| [[@LINE]]|inline int sampleFunc(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]]|} + +// The hash for this function is zero in both cases, either it is used in the translation unit or not. +inline int simpleFunc(int A) { // CHECK: 1| [[@LINE]]|inline int simpleFunc(int A) { + return A; // CHECK-NEXT: 1| [[@LINE]]| return A; +} // CHECK-NEXT: 1| [[@LINE]]|}