diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h index 39acbedf0ca83..4f630188225d6 100644 --- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h +++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -60,6 +60,12 @@ namespace coverage { class CoverageMappingReader; struct CoverageMappingRecord; +enum class MergeStrategy { + Merge, + Any, + All, +}; + enum class coveragemap_error { success = 0, eof, @@ -384,6 +390,32 @@ struct CountedRegion : public CounterMappingRegion { : CounterMappingRegion(R), ExecutionCount(ExecutionCount), FalseExecutionCount(FalseExecutionCount), TrueFolded(false), FalseFolded(false) {} + + LineColPair viewLoc() const { return startLoc(); } + + bool isMergeable(const CountedRegion &RHS) const { + return (this->viewLoc() == RHS.viewLoc()); + } + + void merge(const CountedRegion &RHS, MergeStrategy Strategy); + + /// Returns comparable rank value in selecting a better Record for merging. + auto getMergeRank(MergeStrategy Strategy) const { + assert(isBranch() && "Dedicated to Branch"); + assert(Strategy == MergeStrategy::Any && "Dedicated to Any"); + unsigned m = 0; + // Prefer both Counts have values. + m = (m << 1) | (ExecutionCount != 0 && FalseExecutionCount != 0); + // Prefer both are unfolded. + m = (m << 1) | (!TrueFolded && !FalseFolded); + // Prefer either Count has value. + m = (m << 1) | (ExecutionCount != 0 || FalseExecutionCount != 0); + // Prefer either is unfolded. + m = (m << 1) | (!TrueFolded || !FalseFolded); + return std::make_pair(m, ExecutionCount + FalseExecutionCount); + } + + void commit() const {} }; /// MCDC Record grouping all information together. @@ -471,6 +503,19 @@ struct MCDCRecord { findIndependencePairs(); } + inline LineColPair viewLoc() const { return Region.endLoc(); } + + bool isMergeable(const MCDCRecord &RHS) const { + return (this->viewLoc() == RHS.viewLoc() && this->PosToID == RHS.PosToID && + this->CondLoc == RHS.CondLoc); + } + + // This may invalidate IndependencePairs + // MCDCRecord &operator+=(const MCDCRecord &RHS); + void merge(MCDCRecord &&RHS, MergeStrategy Strategy); + + void commit() { findIndependencePairs(); } + // Compare executed test vectors against each other to find an independence // pairs for each condition. This processing takes the most time. void findIndependencePairs(); @@ -523,15 +568,42 @@ struct MCDCRecord { return (*IndependencePairs)[PosToID[Condition]]; } - float getPercentCovered() const { - unsigned Folded = 0; + std::pair getCoveredCount() const { unsigned Covered = 0; + unsigned Folded = 0; for (unsigned C = 0; C < getNumConditions(); C++) { if (isCondFolded(C)) Folded++; else if (isConditionIndependencePairCovered(C)) Covered++; } + return {Covered, Folded}; + } + + /// Returns comparable rank value in selecting a better Record for merging. + std::tuple + getMergeRank(MergeStrategy Strategy) const { + auto [Covered, Folded] = getCoveredCount(); + auto NumTVs = getNumTestVectors(); + switch (Strategy) { + case MergeStrategy::Merge: + case MergeStrategy::Any: + return { + Covered, // The largest covered number + ~Folded, // Less folded is better + NumTVs, // Show more test vectors + }; + case MergeStrategy::All: + return { + ~Covered, // The smallest covered number + ~Folded, // Less folded is better + NumTVs, // Show more test vectors + }; + } + } + + float getPercentCovered() const { + auto [Covered, Folded] = getCoveredCount(); unsigned Total = getNumConditions() - Folded; if (Total == 0) @@ -1024,11 +1096,13 @@ class CoverageMapping { /// information. That is, only names returned from getUniqueSourceFiles will /// yield a result. CoverageData getCoverageForFile( - StringRef Filename, + StringRef Filename, MergeStrategy Strategy = MergeStrategy::Merge, const DenseSet &FilteredOutFunctions = {}) const; /// Get the coverage for a particular function. - CoverageData getCoverageForFunction(const FunctionRecord &Function) const; + CoverageData + getCoverageForFunction(const FunctionRecord &Function, + MergeStrategy Strategy = MergeStrategy::Merge) const; /// Get the coverage for an expansion within a coverage set. CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion) const; diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp index bff22e2d6be9a..4c99e81d4f6d0 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -246,6 +247,58 @@ Expected CounterMappingContext::evaluate(const Counter &C) const { return LastPoppedValue; } +void CountedRegion::merge(const CountedRegion &RHS, MergeStrategy Strategy) { + assert(this->isBranch() && RHS.isBranch()); + auto MergeCounts = [Strategy](uint64_t &LHSCount, bool &LHSFolded, + uint64_t RHSCount, bool RHSFolded) { + switch (Strategy) { + default: + llvm_unreachable("Don't perform by-parameter merging"); + case MergeStrategy::Merge: + LHSCount += RHSCount; + LHSFolded = (LHSFolded && !LHSCount && RHSFolded); + break; + case MergeStrategy::All: + LHSCount = (LHSFolded ? RHSCount + : RHSFolded ? LHSCount + : std::min(LHSCount, RHSCount)); + LHSFolded = (LHSFolded && RHSFolded); + break; + } + }; + + switch (Strategy) { + case MergeStrategy::Any: + // Take either better (more satisfied) hand side. + // FIXME: Really needed? Better just to merge? + if (this->getMergeRank(Strategy) < RHS.getMergeRank(Strategy)) + *this = RHS; + break; + case MergeStrategy::Merge: + case MergeStrategy::All: + // Able to merge by parameter. + MergeCounts(this->ExecutionCount, this->TrueFolded, RHS.ExecutionCount, + RHS.TrueFolded); + MergeCounts(this->FalseExecutionCount, this->FalseFolded, + RHS.FalseExecutionCount, RHS.FalseFolded); + break; + } +} + +void MCDCRecord::merge(MCDCRecord &&RHS, MergeStrategy Strategy) { + assert(this->PosToID == RHS.PosToID); + assert(this->CondLoc == RHS.CondLoc); + + switch (Strategy) { + case MergeStrategy::Merge: + case MergeStrategy::Any: + case MergeStrategy::All: + if (this->getMergeRank(Strategy) < RHS.getMergeRank(Strategy)) + *this = std::move(RHS); + return; + } +} + // Find an independence pair for each condition: // - The condition is true in one test and false in the other. // - The decision outcome is true one test and false in the other. @@ -1298,7 +1351,8 @@ class SegmentBuilder { /// Combine counts of regions which cover the same area. static ArrayRef - combineRegions(MutableArrayRef Regions) { + combineRegions(MutableArrayRef Regions, + MergeStrategy Strategy) { if (Regions.empty()) return Regions; auto Active = Regions.begin(); @@ -1324,8 +1378,22 @@ class SegmentBuilder { // value for that area. // We add counts of the regions of the same kind as the active region // to handle the both situations. - if (I->Kind == Active->Kind) + if (I->Kind != Active->Kind) + continue; + + switch (Strategy) { + case MergeStrategy::Merge: Active->ExecutionCount += I->ExecutionCount; + break; + case MergeStrategy::Any: + Active->ExecutionCount = + std::max(Active->ExecutionCount, I->ExecutionCount); + break; + case MergeStrategy::All: + Active->ExecutionCount = + std::min(Active->ExecutionCount, I->ExecutionCount); + break; + } } return Regions.drop_back(std::distance(++Active, End)); } @@ -1333,12 +1401,13 @@ class SegmentBuilder { public: /// Build a sorted list of CoverageSegments from a list of Regions. static std::vector - buildSegments(MutableArrayRef Regions) { + buildSegments(MutableArrayRef Regions, + MergeStrategy Strategy) { std::vector Segments; SegmentBuilder Builder(Segments); sortNestedRegions(Regions); - ArrayRef CombinedRegions = combineRegions(Regions); + ArrayRef CombinedRegions = combineRegions(Regions, Strategy); LLVM_DEBUG({ dbgs() << "Combined regions:\n"; @@ -1368,11 +1437,78 @@ class SegmentBuilder { } }; +template +static void mergeRecords(std::vector &Records, MergeStrategy Strategy) { + if (Records.empty()) + return; + + std::vector NewRecords; + + assert(Records.size() <= std::numeric_limits::max()); + + // Build up sorted indices (rather than pointers) of Records. + SmallVector BIdxs(Records.size()); + std::iota(BIdxs.begin(), BIdxs.end(), 0); + llvm::stable_sort(BIdxs, [&](auto A, auto B) { + auto StartA = Records[A].viewLoc(); + auto StartB = Records[B].viewLoc(); + return (std::tie(StartA, A) < std::tie(StartB, B)); + }); + + // 1st element should be stored into SubView. + auto I = BIdxs.begin(), E = BIdxs.end(); + SmallVector ViewRecords{Records[*I++]}; + + auto findMergeableInViewRecords = [&](const RecTy &Branch) { + auto I = ViewRecords.rbegin(), E = ViewRecords.rend(); + for (; I != E; ++I) + if (I->isMergeable(Branch)) + return I; + + // Not mergeable. + return E; + }; + + auto addRecordToSubView = [&] { + assert(!ViewRecords.empty() && "Should have the back"); + for (auto &Acc : ViewRecords) { + Acc.commit(); + NewRecords.push_back(std::move(Acc)); + } + }; + + for (; I != E; ++I) { + assert(!ViewRecords.empty() && "Should have the back in the loop"); + auto &AccB = ViewRecords.back(); + auto &Branch = Records[*I]; + + // Flush current and create the next SubView at the different line. + if (AccB.viewLoc().first != Branch.viewLoc().first) { + addRecordToSubView(); + ViewRecords = {Branch}; + } else if (auto AccI = findMergeableInViewRecords(Branch); + AccI != ViewRecords.rend()) { + // Merge the current Branch into the back of SubView. + AccI->merge(std::move(Branch), Strategy); + } else { + // Not mergeable. + ViewRecords.push_back(Branch); + } + } + + // Flush the last SubView. + addRecordToSubView(); + + // Replace + Records = std::move(NewRecords); +} + struct MergeableCoverageData : public CoverageData { std::vector CodeRegions; + MergeStrategy Strategy; - MergeableCoverageData(bool Single, StringRef Filename) - : CoverageData(Single, Filename) {} + MergeableCoverageData(MergeStrategy Strategy, bool Single, StringRef Filename) + : CoverageData(Single, Filename), Strategy(Strategy) {} void addFunctionRegions( const FunctionRecord &Function, @@ -1394,8 +1530,11 @@ struct MergeableCoverageData : public CoverageData { MCDCRecords.push_back(MR); } - CoverageData buildSegments() { - Segments = SegmentBuilder::buildSegments(CodeRegions); + CoverageData merge(MergeStrategy Strategy) { + mergeRecords(BranchRegions, Strategy); + mergeRecords(MCDCRecords, Strategy); + + Segments = SegmentBuilder::buildSegments(CodeRegions, Strategy); return CoverageData(std::move(*this)); } }; @@ -1449,10 +1588,10 @@ static bool isExpansion(const CountedRegion &R, unsigned FileID) { } CoverageData CoverageMapping::getCoverageForFile( - StringRef Filename, + StringRef Filename, MergeStrategy Strategy, const DenseSet &FilteredOutFunctions) const { assert(SingleByteCoverage); - MergeableCoverageData FileCoverage(*SingleByteCoverage, Filename); + MergeableCoverageData FileCoverage(Strategy, *SingleByteCoverage, Filename); // Look up the function records in the given file. Due to hash collisions on // the filename, we may get back some records that are not in the file. @@ -1471,7 +1610,7 @@ CoverageData CoverageMapping::getCoverageForFile( LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n"); - return FileCoverage.buildSegments(); + return FileCoverage.merge(Strategy); } std::vector @@ -1500,13 +1639,14 @@ CoverageMapping::getInstantiationGroups(StringRef Filename) const { } CoverageData -CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const { +CoverageMapping::getCoverageForFunction(const FunctionRecord &Function, + MergeStrategy Strategy) const { auto MainFileID = findMainViewFileID(Function); if (!MainFileID) return CoverageData(); assert(SingleByteCoverage); - MergeableCoverageData FunctionCoverage(*SingleByteCoverage, + MergeableCoverageData FunctionCoverage(Strategy, *SingleByteCoverage, Function.Filenames[*MainFileID]); FunctionCoverage.addFunctionRegions( Function, [&](auto &CR) { return (CR.FileID == *MainFileID); }, @@ -1515,7 +1655,7 @@ CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const { LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n"); - return FunctionCoverage.buildSegments(); + return FunctionCoverage.merge(Strategy); } CoverageData CoverageMapping::getCoverageForExpansion( @@ -1537,7 +1677,8 @@ CoverageData CoverageMapping::getCoverageForExpansion( LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID << "\n"); - ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions); + ExpansionCoverage.Segments = + SegmentBuilder::buildSegments(Regions, MergeStrategy::Merge); return ExpansionCoverage; } diff --git a/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp b/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp index 4d932eaf5944a..d8a7c056f3677 100644 --- a/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp +++ b/llvm/test/tools/llvm-cov/Inputs/branch-templates.cpp @@ -11,9 +11,9 @@ void unused(T x) { template int func(T x) { - if(x) // BRCOV: | Branch ([[@LINE]]:6): [True: 0, False: 1] - return 0; // BRCOV: | Branch ([[@LINE-1]]:6): [True: 1, False: 0] - else // BRCOV: | Branch ([[@LINE-2]]:6): [True: 0, False: 1] + if(x) // BRCOV: | Branch ([[@LINE]]:6): [True: 1, False: {{2|1}}] + return 0; + else return 1; int j = 1; } diff --git a/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp index 0abb308848b68..b2257f94d4b97 100644 --- a/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp +++ b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp @@ -4,11 +4,13 @@ template bool ab(Ty a, Ty b) { return (a && b); } -// CHECK: [[@LINE-2]]| 4| return +// MERGE: [[@LINE-2]]| 4| return +// ANY: [[@LINE-3]]| 2| return +// ALL: [[@LINE-4]]| 0| return -// CHECK: MC/DC Coverage for Decision{{[:]}} 50.00% -// CHECK: MC/DC Coverage for Decision{{[:]}} 50.00% -// CHECK: MC/DC Coverage for Decision{{[:]}} 0.00% +// MERGE: MC/DC Coverage for Decision{{[:]}} 50.00% +// ANY: MC/DC Coverage for Decision{{[:]}} 50.00% +// ALL: MC/DC Coverage for Decision{{[:]}} 0.00% // CHECK-NOT: MC/DC Coverage for Decision{{[:]}} // CHECK: _Z2abIbEbT_S0_{{[:]}} @@ -23,10 +25,13 @@ template bool Cab(bool a, bool b) { return (a && b && C); } -// CHECK: [[@LINE-2]]| 4| return +// MERGE: [[@LINE-2]]| 4| return +// ANY: [[@LINE-3]]| 2| return +// ALL: [[@LINE-4]]| 2| return -// CHECK: MC/DC Coverage for Decision{{[:]}} 0.00% -// CHECK: MC/DC Coverage for Decision{{[:]}} 50.00% +// MERGE: MC/DC Coverage for Decision{{[:]}} 50.00% +// ANY: MC/DC Coverage for Decision{{[:]}} 50.00% +// ALL: MC/DC Coverage for Decision{{[:]}} 0.00% // CHECK-NOT: MC/DC Coverage for Decision{{[:]}} // CHECK: _Z3CabILb0EEbbb{{[:]}} diff --git a/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.yaml b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.yaml index ba46319a63e8a..f017f75f24dce 100644 --- a/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.yaml +++ b/llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.yaml @@ -11,17 +11,17 @@ Sections: Type: SHT_PROGBITS Flags: [ SHF_GNU_RETAIN ] AddressAlign: 0x8 - Content: FAD58DE7366495DB2500000058247D0A00000000249EC986A505B62F0101010105050127210C020109070010200502000700100500110185808080080501050021 + Content: FAD58DE7366495DB2500000058247D0A00000000249EC986A505B62F010101010505012C210C020109070010200502000700100500110185808080080501050021 - Name: '__llvm_covfun (1)' Type: SHT_PROGBITS Flags: [ SHF_GNU_RETAIN ] AddressAlign: 0x8 - Content: 9CC3B348501DBFE8480000008E83010000000000249EC986A505B62F010103010D0D1105090901171A020201010B000C01000B0011280403000B0016300D02010300000B000C0D0010001130110603020000100011050015001630000A02000000150016 + Content: 9CC3B348501DBFE8480000008E83010000000000249EC986A505B62F010103010D0D1105090901191A020201010B000C01000B0011280403000B0016300D02010300000B000C0D0010001130110603020000100011050015001630000A02000000150016 - Name: '__llvm_covfun (2)' Type: SHT_PROGBITS Flags: [ SHF_GNU_RETAIN ] AddressAlign: 0x8 - Content: 9873627177A03F8E460000008E83010000000000249EC986A505B62F010102010D0D110901171A020201010B000C01000B0011280403000B0016300D02010300000B000C0D0010001130110603020000100011050015001630090002000000150016 + Content: 9873627177A03F8E460000008E83010000000000249EC986A505B62F010102010D0D110901191A020201010B000C01000B0011280403000B0016300D02010300000B000C0D0010001130110603020000100011050015001630090002000000150016 - Name: '__llvm_covfun (3)' Type: SHT_PROGBITS Flags: [ SHF_GNU_RETAIN ] diff --git a/llvm/test/tools/llvm-cov/branch-export-json.test b/llvm/test/tools/llvm-cov/branch-export-json.test index 4278482c6d870..77e2bab7d9eb3 100644 --- a/llvm/test/tools/llvm-cov/branch-export-json.test +++ b/llvm/test/tools/llvm-cov/branch-export-json.test @@ -46,4 +46,4 @@ // MACROS: 5,15,5,23,1,2,8,0,4 // MACROS: 6,15,6,23,0,1,9,0,4 // MACROS: 8,15,8,38,1,2,2,0,4 -// MACROS: {"count":40,"covered":24,"notcovered":16,"percent":60} +// MACROS: {"count":16,"covered":6,"notcovered":10,"percent":37.5} diff --git a/llvm/test/tools/llvm-cov/branch-export-lcov.test b/llvm/test/tools/llvm-cov/branch-export-lcov.test index fe43dd66de8d0..444277435d60d 100644 --- a/llvm/test/tools/llvm-cov/branch-export-lcov.test +++ b/llvm/test/tools/llvm-cov/branch-export-lcov.test @@ -71,8 +71,8 @@ // MACROS-COUNT-10: BRDA:37 // MACROS-NOT: BRDA:37 // MACROS-NOT: BRDA -// MACROS: BRF:40 -// MACROS: BRH:24 +// MACROS: BRF:16 +// MACROS: BRH:6 // NOBRANCH-NOT: BRDA // NOBRANCH-NOT: BRF diff --git a/llvm/test/tools/llvm-cov/branch-macros.test b/llvm/test/tools/llvm-cov/branch-macros.test index 469f24dbcd810..4c99cc7b81294 100644 --- a/llvm/test/tools/llvm-cov/branch-macros.test +++ b/llvm/test/tools/llvm-cov/branch-macros.test @@ -19,6 +19,6 @@ // FILE: Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover // FILE-NEXT: --- -// FILE-NEXT: branch-macros.cpp 28 5 82.14% 3 0 100.00% 39 0 100.00% 40 16 60.00% +// FILE-NEXT: branch-macros.cpp 28 5 82.14% 3 0 100.00% 39 0 100.00% 16 10 37.50% // FILE-NEXT: --- -// FILE-NEXT: TOTAL 28 5 82.14% 3 0 100.00% 39 0 100.00% 40 16 60.00% +// FILE-NEXT: TOTAL 28 5 82.14% 3 0 100.00% 39 0 100.00% 16 10 37.50% diff --git a/llvm/test/tools/llvm-cov/branch-templates.test b/llvm/test/tools/llvm-cov/branch-templates.test index 594a3ca533678..c4fc9cafbfd80 100644 --- a/llvm/test/tools/llvm-cov/branch-templates.test +++ b/llvm/test/tools/llvm-cov/branch-templates.test @@ -26,6 +26,6 @@ // REPORTFILE: Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover // REPORTFILE-NEXT: --- -// REPORTFILE-NEXT: branch-templates.cpp 12 2 83.33% 2 0 100.00% 17 3 82.35% 12 6 50.00% +// REPORTFILE-NEXT: branch-templates.cpp 12 2 83.33% 2 0 100.00% 17 3 82.35% 8 3 62.50% // REPORTFILE-NEXT: --- -// REPORTFILE-NEXT: TOTAL 12 2 83.33% 2 0 100.00% 17 3 82.35% 12 6 50.00% +// REPORTFILE-NEXT: TOTAL 12 2 83.33% 2 0 100.00% 17 3 82.35% 8 3 62.50% diff --git a/llvm/test/tools/llvm-cov/mcdc-templates-merge.test b/llvm/test/tools/llvm-cov/mcdc-templates-merge.test index 3ba8d159b9a19..21c5458edfa84 100644 --- a/llvm/test/tools/llvm-cov/mcdc-templates-merge.test +++ b/llvm/test/tools/llvm-cov/mcdc-templates-merge.test @@ -3,25 +3,39 @@ RUN: yaml2obj %S/Inputs/mcdc-templates-merge.yaml -o %t.o RUN: llvm-profdata merge %S/Inputs/mcdc-templates-merge.proftext -o %t.profdata -RUN: llvm-cov show --show-mcdc -show-instantiations=true %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %S/Inputs/mcdc-templates-merge.cpp +RUN: llvm-cov show -merge-instantiations=merge --show-mcdc -show-instantiations=true %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %S/Inputs/mcdc-templates-merge.cpp --check-prefixes=CHECK,MERGE +RUN: llvm-cov show -merge-instantiations=any --show-mcdc -show-instantiations=true %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %S/Inputs/mcdc-templates-merge.cpp --check-prefixes=CHECK,ANY +RUN: llvm-cov show -merge-instantiations=all --show-mcdc -show-instantiations=true %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %S/Inputs/mcdc-templates-merge.cpp --check-prefixes=CHECK,ALL -RUN: llvm-cov report --show-mcdc-summary %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %s +RUN: llvm-cov report -merge-instantiations=merge --show-mcdc-summary %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %s -check-prefixes=REPORT,MERGE +RUN: llvm-cov report -merge-instantiations=any --show-mcdc-summary %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %s -check-prefixes=REPORT,ANY +RUN: llvm-cov report -merge-instantiations=all --show-mcdc-summary %t.o -instr-profile %t.profdata -path-equivalence=.,%S/Inputs | FileCheck %s -check-prefixes=REPORT,ALL REPORT: mcdc-templates-merge.cpp # Regions -CHECK: 10 1 90.00% +MERGE: 10 1 90.00% +ANY: 10 1 90.00% +ALL: 10 4 60.00% # Functions -CHECK: 3 0 100.00% +MERGE: 3 0 100.00% +ANY: 3 0 100.00% +ALL: 3 0 100.00% # Lines -CHECK: 19 1 94.74% +MERGE: 19 1 94.74% +ANY: 19 1 94.74% +ALL: 19 4 78.95% # Branches -CHECK: 24 9 62.50% +MERGE: 12 1 91.67% +ANY: 11 1 90.91% +ALL: 12 7 41.67% # MC/DC Conditions -CHECK: 10 7 30.00% +MERGE: 4 2 50.00% +ANY: 4 2 50.00% +ALL: 4 4 0.00% REPORT: TOTAL diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp index 921f283deedc7..3794ff06f3bf6 100644 --- a/llvm/tools/llvm-cov/CodeCoverage.cpp +++ b/llvm/tools/llvm-cov/CodeCoverage.cpp @@ -395,7 +395,8 @@ CodeCoverageTool::createSourceFileView(StringRef SourceFile, auto SourceBuffer = getSourceFile(SourceFile); if (!SourceBuffer) return nullptr; - auto FileCoverage = Coverage.getCoverageForFile(SourceFile); + auto FileCoverage = + Coverage.getCoverageForFile(SourceFile, ViewOpts.MergeStrategyOpts); if (FileCoverage.empty()) return nullptr; @@ -795,6 +796,14 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { "check-binary-ids", cl::desc("Fail if an object couldn't be found for a " "binary ID in the profile")); + cl::opt MergeStrategyOpts( + "merge-instantiations", cl::desc("Merge instantiations"), + cl::values( + clEnumValN(MergeStrategy::Merge, "merge", "Merge entries by adding"), + clEnumValN(MergeStrategy::Any, "any", "Pick up any better entries"), + clEnumValN(MergeStrategy::All, "all", "Pick up the worst entries")), + cl::init(MergeStrategy::Merge)); + auto commandLineParser = [&, this](int argc, const char **argv) -> int { cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); ViewOpts.Debug = DebugDump; @@ -951,6 +960,7 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { ViewOpts.ExportSummaryOnly = SummaryOnly; ViewOpts.NumThreads = NumThreads; ViewOpts.CompilationDirectory = CompilationDirectory; + ViewOpts.MergeStrategyOpts = MergeStrategyOpts; return 0; }; diff --git a/llvm/tools/llvm-cov/CoverageReport.cpp b/llvm/tools/llvm-cov/CoverageReport.cpp index 0046b968756dd..9215e2421f514 100644 --- a/llvm/tools/llvm-cov/CoverageReport.cpp +++ b/llvm/tools/llvm-cov/CoverageReport.cpp @@ -480,8 +480,8 @@ void CoverageReport::prepareSingleFileReport(const StringRef Filename, /*Covered=*/Group.getTotalExecutionCount() > 0); } - auto FileCoverage = - Coverage->getCoverageForFile(Filename, FilteredOutFunctions); + auto FileCoverage = Coverage->getCoverageForFile( + Filename, Options.MergeStrategyOpts, FilteredOutFunctions); if (FileCoverage.empty()) return; diff --git a/llvm/tools/llvm-cov/CoverageViewOptions.h b/llvm/tools/llvm-cov/CoverageViewOptions.h index 81e69c3814e30..d2135a19e11f6 100644 --- a/llvm/tools/llvm-cov/CoverageViewOptions.h +++ b/llvm/tools/llvm-cov/CoverageViewOptions.h @@ -11,6 +11,7 @@ #include "RenderingSupport.h" #include "llvm/Config/llvm-config.h" +#include "llvm/ProfileData/Coverage/CoverageMapping.h" #include namespace llvm { @@ -48,6 +49,7 @@ struct CoverageViewOptions { bool BinaryCounters; OutputFormat Format; BranchOutputType ShowBranches; + coverage::MergeStrategy MergeStrategyOpts; std::string ShowOutputDirectory; std::vector DemanglerOpts; uint32_t TabSize; diff --git a/llvm/tools/llvm-cov/SourceCoverageView.cpp b/llvm/tools/llvm-cov/SourceCoverageView.cpp index dfecddfaf4143..1ef1952e691c9 100644 --- a/llvm/tools/llvm-cov/SourceCoverageView.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageView.cpp @@ -207,8 +207,8 @@ void SourceCoverageView::print(raw_ostream &OS, bool WholeFile, // through them while we iterate lines. llvm::stable_sort(ExpansionSubViews); llvm::stable_sort(InstantiationSubViews); - llvm::stable_sort(BranchSubViews); - llvm::stable_sort(MCDCSubViews); + // BranchSubViews is sorted. + // MCDCSubViews is sorted. auto NextESV = ExpansionSubViews.begin(); auto EndESV = ExpansionSubViews.end(); auto NextISV = InstantiationSubViews.begin();