Skip to content

[MC/DC] Refactor: Introduce MCDCTypes.h for coverage::mcdc #81459

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

Merged
merged 3 commits into from
Feb 13, 2024
Merged
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
54 changes: 26 additions & 28 deletions clang/lib/CodeGen/CoverageMappingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ void CoverageSourceInfo::updateNextTokLoc(SourceLocation Loc) {
}

namespace {
using MCDCConditionID = CounterMappingRegion::MCDCConditionID;
using MCDCParameters = CounterMappingRegion::MCDCParameters;

/// A region of source code that can be mapped to a counter.
class SourceMappingRegion {
/// Primary Counter that is also used for Branch Regions for "True" branches.
Expand All @@ -107,7 +104,7 @@ class SourceMappingRegion {
std::optional<Counter> FalseCount;

/// Parameters used for Modified Condition/Decision Coverage
MCDCParameters MCDCParams;
mcdc::Parameters MCDCParams;

/// The region's starting location.
std::optional<SourceLocation> LocStart;
Expand All @@ -131,15 +128,15 @@ class SourceMappingRegion {
SkippedRegion(false) {}

SourceMappingRegion(Counter Count, std::optional<Counter> FalseCount,
MCDCParameters MCDCParams,
mcdc::Parameters MCDCParams,
std::optional<SourceLocation> LocStart,
std::optional<SourceLocation> LocEnd,
bool GapRegion = false)
: Count(Count), FalseCount(FalseCount), MCDCParams(MCDCParams),
LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
SkippedRegion(false) {}

SourceMappingRegion(MCDCParameters MCDCParams,
SourceMappingRegion(mcdc::Parameters MCDCParams,
std::optional<SourceLocation> LocStart,
std::optional<SourceLocation> LocEnd)
: MCDCParams(MCDCParams), LocStart(LocStart), LocEnd(LocEnd),
Expand Down Expand Up @@ -187,7 +184,7 @@ class SourceMappingRegion {

bool isMCDCDecision() const { return MCDCParams.NumConditions != 0; }

const MCDCParameters &getMCDCParams() const { return MCDCParams; }
const mcdc::Parameters &getMCDCParams() const { return MCDCParams; }
};

/// Spelling locations for the start and end of a source region.
Expand Down Expand Up @@ -587,8 +584,8 @@ struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
struct MCDCCoverageBuilder {

struct DecisionIDPair {
MCDCConditionID TrueID = 0;
MCDCConditionID FalseID = 0;
mcdc::ConditionID TrueID = 0;
mcdc::ConditionID FalseID = 0;
};

/// The AST walk recursively visits nested logical-AND or logical-OR binary
Expand Down Expand Up @@ -682,9 +679,9 @@ struct MCDCCoverageBuilder {
CodeGenModule &CGM;

llvm::SmallVector<DecisionIDPair> DecisionStack;
llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDs;
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDs;
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
MCDCConditionID NextID = 1;
mcdc::ConditionID NextID = 1;
bool NotMapped = false;

/// Represent a sentinel value of [0,0] for the bottom of DecisionStack.
Expand All @@ -696,9 +693,10 @@ struct MCDCCoverageBuilder {
}

public:
MCDCCoverageBuilder(CodeGenModule &CGM,
llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDMap,
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap)
MCDCCoverageBuilder(
CodeGenModule &CGM,
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDMap,
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap)
: CGM(CGM), DecisionStack(1, DecisionStackSentinel), CondIDs(CondIDMap),
MCDCBitmapMap(MCDCBitmapMap) {}

Expand All @@ -713,12 +711,12 @@ struct MCDCCoverageBuilder {
bool isBuilding() const { return (NextID > 1); }

/// Set the given condition's ID.
void setCondID(const Expr *Cond, MCDCConditionID ID) {
void setCondID(const Expr *Cond, mcdc::ConditionID ID) {
CondIDs[CodeGenFunction::stripCond(Cond)] = ID;
}

/// Return the ID of a given condition.
MCDCConditionID getCondID(const Expr *Cond) const {
mcdc::ConditionID getCondID(const Expr *Cond) const {
auto I = CondIDs.find(CodeGenFunction::stripCond(Cond));
if (I == CondIDs.end())
return 0;
Expand Down Expand Up @@ -755,7 +753,7 @@ struct MCDCCoverageBuilder {
setCondID(E->getLHS(), NextID++);

// Assign a ID+1 for the RHS.
MCDCConditionID RHSid = NextID++;
mcdc::ConditionID RHSid = NextID++;
setCondID(E->getRHS(), RHSid);

// Push the LHS decision IDs onto the DecisionStack.
Expand Down Expand Up @@ -865,8 +863,8 @@ struct CounterCoverageMappingBuilder
std::optional<SourceLocation> StartLoc = std::nullopt,
std::optional<SourceLocation> EndLoc = std::nullopt,
std::optional<Counter> FalseCount = std::nullopt,
MCDCConditionID ID = 0, MCDCConditionID TrueID = 0,
MCDCConditionID FalseID = 0) {
mcdc::ConditionID ID = 0, mcdc::ConditionID TrueID = 0,
mcdc::ConditionID FalseID = 0) {

if (StartLoc && !FalseCount) {
MostRecentLocation = *StartLoc;
Expand All @@ -886,7 +884,7 @@ struct CounterCoverageMappingBuilder
if (EndLoc && EndLoc->isInvalid())
EndLoc = std::nullopt;
RegionStack.emplace_back(Count, FalseCount,
MCDCParameters{0, 0, ID, TrueID, FalseID},
mcdc::Parameters{0, 0, ID, TrueID, FalseID},
StartLoc, EndLoc);

return RegionStack.size() - 1;
Expand All @@ -896,7 +894,7 @@ struct CounterCoverageMappingBuilder
std::optional<SourceLocation> StartLoc = std::nullopt,
std::optional<SourceLocation> EndLoc = std::nullopt) {

RegionStack.emplace_back(MCDCParameters{BitmapIdx, Conditions}, StartLoc,
RegionStack.emplace_back(mcdc::Parameters{BitmapIdx, Conditions}, StartLoc,
EndLoc);

return RegionStack.size() - 1;
Expand Down Expand Up @@ -1042,9 +1040,9 @@ struct CounterCoverageMappingBuilder
// function's SourceRegions) because it doesn't apply to any other source
// code other than the Condition.
if (CodeGenFunction::isInstrumentedCondition(C)) {
MCDCConditionID ID = MCDCBuilder.getCondID(C);
MCDCConditionID TrueID = IDPair.TrueID;
MCDCConditionID FalseID = IDPair.FalseID;
mcdc::ConditionID ID = MCDCBuilder.getCondID(C);
mcdc::ConditionID TrueID = IDPair.TrueID;
mcdc::ConditionID FalseID = IDPair.FalseID;

// If a condition can fold to true or false, the corresponding branch
// will be removed. Create a region with both counters hard-coded to
Expand Down Expand Up @@ -1151,9 +1149,9 @@ struct CounterCoverageMappingBuilder
if (I.isBranch())
SourceRegions.emplace_back(
I.getCounter(), I.getFalseCounter(),
MCDCParameters{0, 0, I.getMCDCParams().ID,
I.getMCDCParams().TrueID,
I.getMCDCParams().FalseID},
mcdc::Parameters{0, 0, I.getMCDCParams().ID,
I.getMCDCParams().TrueID,
I.getMCDCParams().FalseID},
Loc, getEndOfFileOrMacro(Loc), I.isBranch());
else
SourceRegions.emplace_back(I.getCounter(), Loc,
Expand Down Expand Up @@ -1338,7 +1336,7 @@ struct CounterCoverageMappingBuilder
CoverageMappingModuleGen &CVM,
llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap,
llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDMap,
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDMap,
SourceManager &SM, const LangOptions &LangOpts)
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
MCDCBitmapMap(MCDCBitmapMap),
Expand Down
33 changes: 11 additions & 22 deletions llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/ADT/iterator.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Object/BuildID.h"
#include "llvm/ProfileData/Coverage/MCDCTypes.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/Compiler.h"
Expand Down Expand Up @@ -249,27 +250,14 @@ struct CounterMappingRegion {
MCDCBranchRegion
};

using MCDCConditionID = unsigned int;
struct MCDCParameters {
/// Byte Index of Bitmap Coverage Object for a Decision Region.
unsigned BitmapIdx = 0;

/// Number of Conditions used for a Decision Region.
unsigned NumConditions = 0;

/// IDs used to represent a branch region and other branch regions
/// evaluated based on True and False branches.
MCDCConditionID ID = 0, TrueID = 0, FalseID = 0;
};

/// Primary Counter that is also used for Branch Regions (TrueCount).
Counter Count;

/// Secondary Counter used for Branch Regions (FalseCount).
Counter FalseCount;

/// Parameters used for Modified Condition/Decision Coverage
MCDCParameters MCDCParams;
mcdc::Parameters MCDCParams;

unsigned FileID = 0;
unsigned ExpandedFileID = 0;
Expand All @@ -285,7 +273,7 @@ struct CounterMappingRegion {
ColumnEnd(ColumnEnd), Kind(Kind) {}

CounterMappingRegion(Counter Count, Counter FalseCount,
MCDCParameters MCDCParams, unsigned FileID,
mcdc::Parameters MCDCParams, unsigned FileID,
unsigned ExpandedFileID, unsigned LineStart,
unsigned ColumnStart, unsigned LineEnd,
unsigned ColumnEnd, RegionKind Kind)
Expand All @@ -294,7 +282,7 @@ struct CounterMappingRegion {
ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd),
Kind(Kind) {}

CounterMappingRegion(MCDCParameters MCDCParams, unsigned FileID,
CounterMappingRegion(mcdc::Parameters MCDCParams, unsigned FileID,
unsigned LineStart, unsigned ColumnStart,
unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
: MCDCParams(MCDCParams), FileID(FileID), LineStart(LineStart),
Expand Down Expand Up @@ -334,23 +322,24 @@ struct CounterMappingRegion {
makeBranchRegion(Counter Count, Counter FalseCount, unsigned FileID,
unsigned LineStart, unsigned ColumnStart, unsigned LineEnd,
unsigned ColumnEnd) {
return CounterMappingRegion(Count, FalseCount, MCDCParameters(), FileID, 0,
LineStart, ColumnStart, LineEnd, ColumnEnd,
return CounterMappingRegion(Count, FalseCount, mcdc::Parameters(), FileID,
0, LineStart, ColumnStart, LineEnd, ColumnEnd,
BranchRegion);
}

static CounterMappingRegion
makeBranchRegion(Counter Count, Counter FalseCount, MCDCParameters MCDCParams,
unsigned FileID, unsigned LineStart, unsigned ColumnStart,
unsigned LineEnd, unsigned ColumnEnd) {
makeBranchRegion(Counter Count, Counter FalseCount,
mcdc::Parameters MCDCParams, unsigned FileID,
unsigned LineStart, unsigned ColumnStart, unsigned LineEnd,
unsigned ColumnEnd) {
return CounterMappingRegion(Count, FalseCount, MCDCParams, FileID, 0,
LineStart, ColumnStart, LineEnd, ColumnEnd,
MCDCParams.ID == 0 ? BranchRegion
: MCDCBranchRegion);
}

static CounterMappingRegion
makeDecisionRegion(MCDCParameters MCDCParams, unsigned FileID,
makeDecisionRegion(mcdc::Parameters MCDCParams, unsigned FileID,
unsigned LineStart, unsigned ColumnStart, unsigned LineEnd,
unsigned ColumnEnd) {
return CounterMappingRegion(MCDCParams, FileID, LineStart, ColumnStart,
Expand Down
36 changes: 36 additions & 0 deletions llvm/include/llvm/ProfileData/Coverage/MCDCTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===- MCDCTypes.h - Types related to MC/DC Coverage ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Types related to MC/DC Coverage.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_PROFILEDATA_COVERAGE_MCDCTYPES_H
#define LLVM_PROFILEDATA_COVERAGE_MCDCTYPES_H

namespace llvm::coverage::mcdc {

/// The ID for MCDCBranch.
using ConditionID = unsigned int;

/// MC/DC-specifig parameters
struct Parameters {
/// Byte Index of Bitmap Coverage Object for a Decision Region.
unsigned BitmapIdx = 0;

/// Number of Conditions used for a Decision Region.
unsigned NumConditions = 0;

/// IDs used to represent a branch region and other branch regions
/// evaluated based on True and False branches.
ConditionID ID = 0, TrueID = 0, FalseID = 0;
};

} // namespace llvm::coverage::mcdc

#endif // LLVM_PROFILEDATA_COVERAGE_MCDCTYPES_H
2 changes: 1 addition & 1 deletion llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ class MCDCDecisionRecorder {

/// IDs that are stored in MCDCBranches
/// Complete when all IDs (1 to NumConditions) are met.
DenseSet<CounterMappingRegion::MCDCConditionID> ConditionIDs;
DenseSet<mcdc::ConditionID> ConditionIDs;

/// Set of IDs of Expansion(s) that are relevant to DecisionRegion
/// and its children (via expansions).
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,9 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(

auto CMR = CounterMappingRegion(
C, C2,
CounterMappingRegion::MCDCParameters{
static_cast<unsigned>(BIDX), static_cast<unsigned>(NC),
static_cast<unsigned>(ID), static_cast<unsigned>(TID),
static_cast<unsigned>(FID)},
mcdc::Parameters{static_cast<unsigned>(BIDX), static_cast<unsigned>(NC),
static_cast<unsigned>(ID), static_cast<unsigned>(TID),
static_cast<unsigned>(FID)},
InferredFileID, ExpandedFileID, LineStart, ColumnStart,
LineStart + NumLines, ColumnEnd, Kind);
if (CMR.startLoc() > CMR.endLoc())
Expand Down
7 changes: 3 additions & 4 deletions llvm/unittests/ProfileData/CoverageMappingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
auto &Regions = InputFunctions.back().Regions;
unsigned FileID = getFileIndexForFunction(File);
Regions.push_back(CounterMappingRegion::makeDecisionRegion(
CounterMappingRegion::MCDCParameters{Mask, NC}, FileID, LS, CS, LE,
CE));
mcdc::Parameters{Mask, NC}, FileID, LS, CS, LE, CE));
}

void addMCDCBranchCMR(Counter C1, Counter C2, unsigned ID, unsigned TrueID,
Expand All @@ -207,8 +206,8 @@ struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
auto &Regions = InputFunctions.back().Regions;
unsigned FileID = getFileIndexForFunction(File);
Regions.push_back(CounterMappingRegion::makeBranchRegion(
C1, C2, CounterMappingRegion::MCDCParameters{0, 0, ID, TrueID, FalseID},
FileID, LS, CS, LE, CE));
C1, C2, mcdc::Parameters{0, 0, ID, TrueID, FalseID}, FileID, LS, CS, LE,
CE));
}

void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
Expand Down