Skip to content

Commit 5e46059

Browse files
committed
[Coverage] Make additional counters available for BranchRegion. NFC.
`getBranchCounterPair()` allocates an additional Counter to SkipPath in `SingleByteCoverage`. `IsCounterEqual()` calculates the comparison with rewinding counter replacements. `NumRegionCounters` is updated to take additional counters in account. `incrementProfileCounter()` has a few additiona arguments. - `UseSkipPath=true`, to specify setting counters for SkipPath. It assumes `UseSkipPath=false` is used together. - `UseBoth` may be specified for marking another path. It introduces the same effect as issueing `markStmtAsUsed(!SkipPath, S)`. `llvm-cov` discovers counters in `FalseCount` to allocate `MaxCounterID` for empty profile data.
1 parent 12abd89 commit 5e46059

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1629,11 +1629,17 @@ class CodeGenFunction : public CodeGenTypeCache {
16291629
/// Increment the profiler's counter for the given statement by \p StepV.
16301630
/// If \p StepV is null, the default increment is 1.
16311631
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) {
1632+
incrementProfileCounter(false, S, false, StepV);
1633+
}
1634+
1635+
void incrementProfileCounter(bool UseSkipPath, const Stmt *S,
1636+
bool UseBoth = false,
1637+
llvm::Value *StepV = nullptr) {
16321638
if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
16331639
!CurFn->hasFnAttribute(llvm::Attribute::NoProfile) &&
16341640
!CurFn->hasFnAttribute(llvm::Attribute::SkipProfile)) {
16351641
auto AL = ApplyDebugLocation::CreateArtificial(*this);
1636-
PGO.emitCounterSetOrIncrement(Builder, S, StepV);
1642+
PGO.emitCounterSetOrIncrement(Builder, S, UseSkipPath, UseBoth, StepV);
16371643
}
16381644
PGO.setCurrentStmt(S);
16391645
}

clang/lib/CodeGen/CodeGenPGO.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,19 @@ void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
11381138
if (CoverageMapping.empty())
11391139
return;
11401140

1141+
// Scan max(FalseCnt) and update NumRegionCounters.
1142+
unsigned MaxNumCounters = NumRegionCounters;
1143+
for (const auto [_, V] : *RegionCounterMap) {
1144+
auto HasCounters = V.getIsCounterPair();
1145+
assert((!HasCounters.first ||
1146+
MaxNumCounters > (V.first & CounterPair::Mask)) &&
1147+
"TrueCnt should not be reassigned");
1148+
if (HasCounters.second)
1149+
MaxNumCounters =
1150+
std::max(MaxNumCounters, (V.second & CounterPair::Mask) + 1);
1151+
}
1152+
NumRegionCounters = MaxNumCounters;
1153+
11411154
CGM.getCoverageMapping()->addFunctionMappingRecord(
11421155
FuncNameVar, FuncName, FunctionHash, CoverageMapping);
11431156
}
@@ -1193,11 +1206,25 @@ std::pair<bool, bool> CodeGenPGO::getIsCounterPair(const Stmt *S) const {
11931206
}
11941207

11951208
void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
1209+
bool UseSkipPath, bool UseBoth,
11961210
llvm::Value *StepV) {
1197-
if (!RegionCounterMap || !Builder.GetInsertBlock())
1211+
if (!RegionCounterMap)
11981212
return;
11991213

1200-
unsigned Counter = (*RegionCounterMap)[S].first;
1214+
unsigned Counter;
1215+
auto &TheMap = (*RegionCounterMap)[S];
1216+
auto IsCounter = TheMap.getIsCounterPair();
1217+
if (!UseSkipPath) {
1218+
assert(IsCounter.first);
1219+
Counter = (TheMap.first & CounterPair::Mask);
1220+
} else {
1221+
if (!IsCounter.second)
1222+
return;
1223+
Counter = (TheMap.second & CounterPair::Mask);
1224+
}
1225+
1226+
if (!Builder.GetInsertBlock())
1227+
return;
12011228

12021229
// Make sure that pointer to global is passed in with zero addrspace
12031230
// This is relevant during GPU profiling

clang/lib/CodeGen/CodeGenPGO.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class CodeGenPGO {
112112
public:
113113
std::pair<bool, bool> getIsCounterPair(const Stmt *S) const;
114114
void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
115+
bool UseFalsePath, bool UseBoth,
115116
llvm::Value *StepV);
116117
void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
117118
Address MCDCCondBitmapAddr,

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,9 @@ struct CounterCoverageMappingBuilder
887887
/// The map of statements to count values.
888888
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;
889889

890+
CounterExpressionBuilder::ReplaceMap MapToExpand;
891+
unsigned NextCounterNum;
892+
890893
MCDC::State &MCDCState;
891894

892895
/// A stack of currently live regions.
@@ -922,15 +925,11 @@ struct CounterCoverageMappingBuilder
922925

923926
/// Return a counter for the sum of \c LHS and \c RHS.
924927
Counter addCounters(Counter LHS, Counter RHS, bool Simplify = true) {
925-
assert(!llvm::EnableSingleByteCoverage &&
926-
"cannot add counters when single byte coverage mode is enabled");
927928
return Builder.add(LHS, RHS, Simplify);
928929
}
929930

930931
Counter addCounters(Counter C1, Counter C2, Counter C3,
931932
bool Simplify = true) {
932-
assert(!llvm::EnableSingleByteCoverage &&
933-
"cannot add counters when single byte coverage mode is enabled");
934933
return addCounters(addCounters(C1, C2, Simplify), C3, Simplify);
935934
}
936935

@@ -943,14 +942,31 @@ struct CounterCoverageMappingBuilder
943942

944943
std::pair<Counter, Counter> getBranchCounterPair(const Stmt *S,
945944
Counter ParentCnt) {
946-
Counter ExecCnt = getRegionCounter(S);
947-
return {ExecCnt, Builder.subtract(ParentCnt, ExecCnt)};
945+
auto &TheMap = CounterMap[S];
946+
auto ExecCnt = Counter::getCounter(TheMap.first);
947+
auto SkipExpr = Builder.subtract(ParentCnt, ExecCnt);
948+
949+
if (!llvm::EnableSingleByteCoverage)
950+
return {ExecCnt, SkipExpr};
951+
952+
// Assign second if second is not assigned yet.
953+
if (!TheMap.getIsCounterPair().second)
954+
TheMap.second = NextCounterNum++;
955+
956+
Counter SkipCnt = Counter::getCounter(TheMap.second);
957+
MapToExpand[SkipCnt] = SkipExpr;
958+
return {ExecCnt, SkipCnt};
948959
}
949960

950961
bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
951962
if (OutCount == ParentCount)
952963
return true;
953964

965+
// Try comaparison with pre-replaced expressions.
966+
if (Builder.replace(Builder.subtract(OutCount, ParentCount), MapToExpand)
967+
.isZero())
968+
return true;
969+
954970
return false;
955971
}
956972

@@ -1437,7 +1453,8 @@ struct CounterCoverageMappingBuilder
14371453
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap,
14381454
MCDC::State &MCDCState, SourceManager &SM, const LangOptions &LangOpts)
14391455
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
1440-
MCDCState(MCDCState), MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {}
1456+
NextCounterNum(CounterMap.size()), MCDCState(MCDCState),
1457+
MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {}
14411458

14421459
/// Write the mapping data to the output stream
14431460
void write(llvm::raw_ostream &OS) {

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
640640
unsigned MaxCounterID = 0;
641641
for (const auto &Region : Record.MappingRegions) {
642642
MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
643+
if (Region.Kind == CounterMappingRegion::BranchRegion ||
644+
Region.Kind == CounterMappingRegion::MCDCBranchRegion)
645+
MaxCounterID =
646+
std::max(MaxCounterID, Ctx.getMaxCounterID(Region.FalseCount));
643647
}
644648
return MaxCounterID;
645649
}

0 commit comments

Comments
 (0)