Skip to content

llvm-cov: [MCDC] Merge and recalculate independence pairs on template instantiations. #121196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: users/chapuni/cov/merge/merge-mcdc-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ struct MCDCRecord {
}
};

using BitmapByCondTy = std::array<BitVector, 2>;
using TestVectors = llvm::SmallVector<std::pair<TestVector, CondState>>;
using BoolVector = std::array<BitVector, 2>;
using TVRowPair = std::pair<unsigned, unsigned>;
Expand All @@ -490,24 +491,28 @@ struct MCDCRecord {
private:
CounterMappingRegion Region;
TestVectors TV;
BitmapByCondTy BitmapByCond;
std::optional<TVPairMap> IndependencePairs;
BoolVector Folded;
CondIDMap PosToID;
LineColPairMap CondLoc;

public:
MCDCRecord(const CounterMappingRegion &Region, TestVectors &&TV,
BoolVector &&Folded, CondIDMap &&PosToID, LineColPairMap &&CondLoc)
: Region(Region), TV(std::move(TV)), Folded(std::move(Folded)),
BitmapByCondTy &&BitmapByCond, BoolVector &&Folded,
CondIDMap &&PosToID, LineColPairMap &&CondLoc)
: Region(Region), TV(std::move(TV)),
BitmapByCond(std::move(BitmapByCond)), Folded(std::move(Folded)),
PosToID(std::move(PosToID)), CondLoc(std::move(CondLoc)) {
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);
return (this->viewLoc() == RHS.viewLoc() &&
this->BitmapByCond[false].size() == RHS.BitmapByCond[true].size() &&
this->PosToID == RHS.PosToID && this->CondLoc == RHS.CondLoc);
}

// This may invalidate IndependencePairs
Expand Down Expand Up @@ -586,7 +591,8 @@ struct MCDCRecord {
auto [Covered, Folded] = getCoveredCount();
auto NumTVs = getNumTestVectors();
switch (Strategy) {
case MergeStrategy::Merge:
default:
llvm_unreachable("Not supported");
case MergeStrategy::Any:
return {
Covered, // The largest covered number
Expand Down
57 changes: 48 additions & 9 deletions llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,55 @@ void CountedRegion::merge(const CountedRegion &RHS, MergeStrategy Strategy) {
}

void MCDCRecord::merge(MCDCRecord &&RHS, MergeStrategy Strategy) {
assert(this->TV.size() ==
this->BitmapByCond[false].count() + this->BitmapByCond[true].count());
assert(RHS.TV.size() ==
RHS.BitmapByCond[false].count() + RHS.BitmapByCond[true].count());
assert(this->PosToID == RHS.PosToID);
assert(this->CondLoc == RHS.CondLoc);

switch (Strategy) {
case MergeStrategy::Merge:
break;
case MergeStrategy::Any:
case MergeStrategy::All:
if (this->getMergeRank(Strategy) < RHS.getMergeRank(Strategy))
*this = std::move(RHS);
return;
}

std::array<TestVectors, 2> LHSTV;
auto LHSI = this->TV.begin();
auto RHSI = RHS.TV.begin();
bool Merged = false;
for (auto MCDCCond : {MCDCRecord::MCDC_False, MCDCRecord::MCDC_True}) {
auto &LHSBitmap = this->BitmapByCond[MCDCCond];
auto &RHSBitmap = RHS.BitmapByCond[MCDCCond];
for (unsigned I = 0, E = LHSBitmap.size(); I != E; ++I) {
if (LHSBitmap[I]) {
if (RHSBitmap[I])
++RHSI;
LHSTV[LHSI->second].push_back(std::move(*LHSI++));
} else if (RHSBitmap[I]) {
LHSTV[RHSI->second].push_back(std::move(*RHSI++));
LHSBitmap[I] = true;
Merged = true;
}
}

this->Folded[MCDCCond] &= RHS.Folded[MCDCCond];
}

if (Merged)
IndependencePairs.reset();

assert(LHSI == this->TV.end());
assert(RHSI == RHS.TV.end());
this->TV = std::move(LHSTV[false]);
this->TV.append(std::make_move_iterator(LHSTV[true].begin()),
std::make_move_iterator(LHSTV[true].end()));
assert(this->TV.size() ==
this->BitmapByCond[false].count() + this->BitmapByCond[true].count());
}

// Find an independence pair for each condition:
Expand All @@ -310,13 +348,7 @@ void MCDCRecord::findIndependencePairs() {
IndependencePairs.emplace();

unsigned NumTVs = TV.size();
// Will be replaced to shorter expr.
unsigned TVTrueIdx = std::distance(
TV.begin(),
std::find_if(TV.begin(), TV.end(),
[&](auto I) { return (I.second == MCDCRecord::MCDC_True); })

);
unsigned TVTrueIdx = BitmapByCond[false].count();
for (unsigned I = TVTrueIdx; I < NumTVs; ++I) {
const auto &[A, ACond] = TV[I];
assert(ACond == MCDCRecord::MCDC_True);
Expand Down Expand Up @@ -461,6 +493,7 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
/// with a bit value of '1' indicates that the corresponding Test Vector
/// identified by that index was executed.
const BitVector &Bitmap;
MCDCRecord::BitmapByCondTy BitmapByCond;

/// Decision Region to which the ExecutedTestVectorBitmap applies.
const CounterMappingRegion &Region;
Expand Down Expand Up @@ -513,6 +546,7 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
ArrayRef<const CounterMappingRegion *> Branches,
bool IsVersion11)
: NextIDsBuilder(Branches), TVIdxBuilder(this->NextIDs), Bitmap(Bitmap),
BitmapByCond{{BitVector(NumTestVectors), BitVector(NumTestVectors)}},
Region(Region), DecisionParams(Region.getDecisionParams()),
Branches(Branches), NumConditions(DecisionParams.NumConditions),
Folded{{BitVector(NumConditions), BitVector(NumConditions)}},
Expand Down Expand Up @@ -544,6 +578,8 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
: DecisionParams.BitmapIdx - NumTestVectors + NextTVIdx])
continue;

assert(!BitmapByCond[MCDCCond][NextTVIdx]);
BitmapByCond[MCDCCond][NextTVIdx] = true;
ExecVectorIdxs.emplace_back(MCDCCond, NextTVIdx, ExecVectors.size());

// Copy the completed test vector to the vector of testvectors.
Expand Down Expand Up @@ -573,6 +609,8 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
for (const auto &IdxTuple : ExecVectorIdxs)
NewTestVectors.push_back(std::move(ExecVectors[IdxTuple.Ord]));
ExecVectors = std::move(NewTestVectors);

assert(!BitmapByCond[false].anyCommon(BitmapByCond[true]));
}

public:
Expand Down Expand Up @@ -611,8 +649,9 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
findExecutedTestVectors();

// Record Test vectors, executed vectors, and independence pairs.
return MCDCRecord(Region, std::move(ExecVectors), std::move(Folded),
std::move(PosToID), std::move(CondLoc));
return MCDCRecord(Region, std::move(ExecVectors), std::move(BitmapByCond),
std::move(Folded), std::move(PosToID),
std::move(CondLoc));
}
};

Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-cov/Inputs/mcdc-templates-merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bool ab(Ty a, Ty b) {
// ANY: [[@LINE-3]]| 2| return
// ALL: [[@LINE-4]]| 0| return

// MERGE: MC/DC Coverage for Decision{{[:]}} 50.00%
// MERGE: MC/DC Coverage for Decision{{[:]}} 100.00%
// ANY: MC/DC Coverage for Decision{{[:]}} 50.00%
// ALL: MC/DC Coverage for Decision{{[:]}} 0.00%
// CHECK-NOT: MC/DC Coverage for Decision{{[:]}}
Expand All @@ -29,7 +29,7 @@ bool Cab(bool a, bool b) {
// ANY: [[@LINE-3]]| 2| return
// ALL: [[@LINE-4]]| 2| return

// MERGE: MC/DC Coverage for Decision{{[:]}} 50.00%
// MERGE: MC/DC Coverage for Decision{{[:]}} 100.00%
// ANY: MC/DC Coverage for Decision{{[:]}} 50.00%
// ALL: MC/DC Coverage for Decision{{[:]}} 0.00%
// CHECK-NOT: MC/DC Coverage for Decision{{[:]}}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-cov/mcdc-templates-merge.test
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ANY: 11 1 90.91%
ALL: 12 7 41.67%

# MC/DC Conditions
MERGE: 4 2 50.00%
MERGE: 5 0 100.00%
ANY: 4 2 50.00%
ALL: 4 4 0.00%

Expand Down
Loading