[clang] [compiler-rt] [llvm] Single byte coverage with branch coverage (PR #113115)
NAKAMURA Takumi via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 27 10:52:47 PDT 2024
https://github.com/chapuni updated https://github.com/llvm/llvm-project/pull/113115
>From 178b57cbbcb2b21b7d13f90ffd75903417913438 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 2 Oct 2024 23:11:36 +0900
Subject: [PATCH 01/24] [Coverage] Move SingleByteCoverage out of CountedRegion
`SingleByteCoverage` is not per-region attribute at least.
At the moment, this change moves it into `FunctionRecord`.
---
.../ProfileData/Coverage/CoverageMapping.h | 27 +++++++-------
.../ProfileData/Coverage/CoverageMapping.cpp | 36 ++++++++++++-------
2 files changed, 36 insertions(+), 27 deletions(-)
diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
index fa07b3a9e8b14f..77c447efe1a7c9 100644
--- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -359,19 +359,15 @@ struct CountedRegion : public CounterMappingRegion {
uint64_t ExecutionCount;
uint64_t FalseExecutionCount;
bool Folded;
- bool HasSingleByteCoverage;
- CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount,
- bool HasSingleByteCoverage)
+ CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
: CounterMappingRegion(R), ExecutionCount(ExecutionCount),
- FalseExecutionCount(0), Folded(false),
- HasSingleByteCoverage(HasSingleByteCoverage) {}
+ FalseExecutionCount(0), Folded(false) {}
CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount,
- uint64_t FalseExecutionCount, bool HasSingleByteCoverage)
+ uint64_t FalseExecutionCount)
: CounterMappingRegion(R), ExecutionCount(ExecutionCount),
- FalseExecutionCount(FalseExecutionCount), Folded(false),
- HasSingleByteCoverage(HasSingleByteCoverage) {}
+ FalseExecutionCount(FalseExecutionCount), Folded(false) {}
};
/// MCDC Record grouping all information together.
@@ -702,9 +698,12 @@ struct FunctionRecord {
std::vector<MCDCRecord> MCDCRecords;
/// The number of times this function was executed.
uint64_t ExecutionCount = 0;
+ bool SingleByteCoverage;
- FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames)
- : Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
+ FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames,
+ bool SingleByteCoverage)
+ : Name(Name), Filenames(Filenames.begin(), Filenames.end()),
+ SingleByteCoverage(SingleByteCoverage) {}
FunctionRecord(FunctionRecord &&FR) = default;
FunctionRecord &operator=(FunctionRecord &&) = default;
@@ -714,11 +713,10 @@ struct FunctionRecord {
}
void pushRegion(CounterMappingRegion Region, uint64_t Count,
- uint64_t FalseCount, bool HasSingleByteCoverage) {
+ uint64_t FalseCount) {
if (Region.Kind == CounterMappingRegion::BranchRegion ||
Region.Kind == CounterMappingRegion::MCDCBranchRegion) {
- CountedBranchRegions.emplace_back(Region, Count, FalseCount,
- HasSingleByteCoverage);
+ CountedBranchRegions.emplace_back(Region, Count, FalseCount);
// If both counters are hard-coded to zero, then this region represents a
// constant-folded branch.
if (Region.Count.isZero() && Region.FalseCount.isZero())
@@ -727,8 +725,7 @@ struct FunctionRecord {
}
if (CountedRegions.empty())
ExecutionCount = Count;
- CountedRegions.emplace_back(Region, Count, FalseCount,
- HasSingleByteCoverage);
+ CountedRegions.emplace_back(Region, Count, FalseCount);
}
};
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index 18643c6b44485e..a02136d5b0386d 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -808,6 +808,7 @@ Error CoverageMapping::loadFunctionRecord(
else
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
+ bool SingleByteCoverage = ProfileReader.hasSingleByteCoverage();
CounterMappingContext Ctx(Record.Expressions);
std::vector<uint64_t> Counts;
@@ -855,7 +856,7 @@ Error CoverageMapping::loadFunctionRecord(
return Error::success();
MCDCDecisionRecorder MCDCDecisions;
- FunctionRecord Function(OrigFuncName, Record.Filenames);
+ FunctionRecord Function(OrigFuncName, Record.Filenames, SingleByteCoverage);
for (const auto &Region : Record.MappingRegions) {
// MCDCDecisionRegion should be handled first since it overlaps with
// others inside.
@@ -873,8 +874,7 @@ Error CoverageMapping::loadFunctionRecord(
consumeError(std::move(E));
return Error::success();
}
- Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount,
- ProfileReader.hasSingleByteCoverage());
+ Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
// Record ExpansionRegion.
if (Region.Kind == CounterMappingRegion::ExpansionRegion) {
@@ -1270,7 +1270,8 @@ class SegmentBuilder {
/// Combine counts of regions which cover the same area.
static ArrayRef<CountedRegion>
- combineRegions(MutableArrayRef<CountedRegion> Regions) {
+ combineRegions(MutableArrayRef<CountedRegion> Regions,
+ bool SingleByteCoverage) {
if (Regions.empty())
return Regions;
auto Active = Regions.begin();
@@ -1297,9 +1298,7 @@ class SegmentBuilder {
// We add counts of the regions of the same kind as the active region
// to handle the both situations.
if (I->Kind == Active->Kind) {
- assert(I->HasSingleByteCoverage == Active->HasSingleByteCoverage &&
- "Regions are generated in different coverage modes");
- if (I->HasSingleByteCoverage)
+ if (SingleByteCoverage)
Active->ExecutionCount = Active->ExecutionCount || I->ExecutionCount;
else
Active->ExecutionCount += I->ExecutionCount;
@@ -1311,12 +1310,14 @@ class SegmentBuilder {
public:
/// Build a sorted list of CoverageSegments from a list of Regions.
static std::vector<CoverageSegment>
- buildSegments(MutableArrayRef<CountedRegion> Regions) {
+ buildSegments(MutableArrayRef<CountedRegion> Regions,
+ bool SingleByteCoverage) {
std::vector<CoverageSegment> Segments;
SegmentBuilder Builder(Segments);
sortNestedRegions(Regions);
- ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
+ ArrayRef<CountedRegion> CombinedRegions =
+ combineRegions(Regions, SingleByteCoverage);
LLVM_DEBUG({
dbgs() << "Combined regions:\n";
@@ -1403,10 +1404,14 @@ CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
// the filename, we may get back some records that are not in the file.
ArrayRef<unsigned> RecordIndices =
getImpreciseRecordIndicesForFilename(Filename);
+ std::optional<bool> SingleByteCoverage;
for (unsigned RecordIndex : RecordIndices) {
const FunctionRecord &Function = Functions[RecordIndex];
auto MainFileID = findMainViewFileID(Filename, Function);
auto FileIDs = gatherFileIDs(Filename, Function);
+ assert(!SingleByteCoverage ||
+ *SingleByteCoverage == Function.SingleByteCoverage);
+ SingleByteCoverage = Function.SingleByteCoverage;
for (const auto &CR : Function.CountedRegions)
if (FileIDs.test(CR.FileID)) {
Regions.push_back(CR);
@@ -1424,7 +1429,8 @@ CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
}
LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
- FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
+ FileCoverage.Segments =
+ SegmentBuilder::buildSegments(Regions, *SingleByteCoverage);
return FileCoverage;
}
@@ -1480,7 +1486,8 @@ CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
<< "\n");
- FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
+ FunctionCoverage.Segments =
+ SegmentBuilder::buildSegments(Regions, Function.SingleByteCoverage);
return FunctionCoverage;
}
@@ -1490,8 +1497,12 @@ CoverageData CoverageMapping::getCoverageForExpansion(
CoverageData ExpansionCoverage(
Expansion.Function.Filenames[Expansion.FileID]);
std::vector<CountedRegion> Regions;
+ std::optional<bool> SingleByteCoverage;
for (const auto &CR : Expansion.Function.CountedRegions)
if (CR.FileID == Expansion.FileID) {
+ assert(!SingleByteCoverage ||
+ *SingleByteCoverage == Expansion.Function.SingleByteCoverage);
+ SingleByteCoverage = Expansion.Function.SingleByteCoverage;
Regions.push_back(CR);
if (isExpansion(CR, Expansion.FileID))
ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
@@ -1503,7 +1514,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, *SingleByteCoverage);
return ExpansionCoverage;
}
>From aacb50ddf87d96b4a0644c7ef5d0a86dc94f069b Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 2 Oct 2024 23:25:52 +0900
Subject: [PATCH 02/24] [Coverage] Make SingleByteCoverage work consistent to
merging
- Round `Counts` as 1/0
- Confirm both `ExecutionCount` and `AltExecutionCount` are in range.
---
compiler-rt/test/profile/instrprof-block-coverage.c | 2 +-
compiler-rt/test/profile/instrprof-entry-coverage.c | 2 +-
llvm/include/llvm/ProfileData/InstrProf.h | 5 ++++-
llvm/lib/ProfileData/Coverage/CoverageMapping.cpp | 3 +++
llvm/lib/ProfileData/InstrProf.cpp | 2 +-
llvm/lib/ProfileData/InstrProfReader.cpp | 1 +
6 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/compiler-rt/test/profile/instrprof-block-coverage.c b/compiler-rt/test/profile/instrprof-block-coverage.c
index 829d5af8dc3f9e..8d924e1cac64d8 100644
--- a/compiler-rt/test/profile/instrprof-block-coverage.c
+++ b/compiler-rt/test/profile/instrprof-block-coverage.c
@@ -49,4 +49,4 @@ int main(int argc, char *argv[]) {
// CHECK-ERROR-NOT: warning: {{.*}}: Found inconsistent block coverage
-// COUNTS: Maximum function count: 4
+// COUNTS: Maximum function count: 1
diff --git a/compiler-rt/test/profile/instrprof-entry-coverage.c b/compiler-rt/test/profile/instrprof-entry-coverage.c
index 1c6816ba01964b..b93a4e0c43ccd6 100644
--- a/compiler-rt/test/profile/instrprof-entry-coverage.c
+++ b/compiler-rt/test/profile/instrprof-entry-coverage.c
@@ -36,4 +36,4 @@ int main(int argc, char *argv[]) {
// CHECK-DAG: foo
// CHECK-DAG: bar
-// COUNTS: Maximum function count: 2
+// COUNTS: Maximum function count: 1
diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h
index b0b2258735e2ae..df9e76966bf42b 100644
--- a/llvm/include/llvm/ProfileData/InstrProf.h
+++ b/llvm/include/llvm/ProfileData/InstrProf.h
@@ -830,6 +830,7 @@ struct InstrProfValueSiteRecord {
/// Profiling information for a single function.
struct InstrProfRecord {
std::vector<uint64_t> Counts;
+ bool SingleByteCoverage = false;
std::vector<uint8_t> BitmapBytes;
InstrProfRecord() = default;
@@ -839,13 +840,15 @@ struct InstrProfRecord {
: Counts(std::move(Counts)), BitmapBytes(std::move(BitmapBytes)) {}
InstrProfRecord(InstrProfRecord &&) = default;
InstrProfRecord(const InstrProfRecord &RHS)
- : Counts(RHS.Counts), BitmapBytes(RHS.BitmapBytes),
+ : Counts(RHS.Counts), SingleByteCoverage(RHS.SingleByteCoverage),
+ BitmapBytes(RHS.BitmapBytes),
ValueData(RHS.ValueData
? std::make_unique<ValueProfData>(*RHS.ValueData)
: nullptr) {}
InstrProfRecord &operator=(InstrProfRecord &&) = default;
InstrProfRecord &operator=(const InstrProfRecord &RHS) {
Counts = RHS.Counts;
+ SingleByteCoverage = RHS.SingleByteCoverage;
BitmapBytes = RHS.BitmapBytes;
if (!RHS.ValueData) {
ValueData = nullptr;
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index a02136d5b0386d..bc765c59381718 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -874,6 +874,9 @@ Error CoverageMapping::loadFunctionRecord(
consumeError(std::move(E));
return Error::success();
}
+ assert(!SingleByteCoverage ||
+ (0 <= *ExecutionCount && *ExecutionCount <= 1 &&
+ 0 <= *AltExecutionCount && *AltExecutionCount <= 1));
Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
// Record ExpansionRegion.
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index b9937c9429b77d..0f6677b4d35718 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -952,7 +952,7 @@ void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight,
Value = getInstrMaxCountValue();
Overflowed = true;
}
- Counts[I] = Value;
+ Counts[I] = (SingleByteCoverage && Value != 0 ? 1 : Value);
if (Overflowed)
Warn(instrprof_error::counter_overflow);
}
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index b90617c74f6d13..a07d7f573275ba 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -743,6 +743,7 @@ Error RawInstrProfReader<IntPtrT>::readRawCounts(
Record.Counts.clear();
Record.Counts.reserve(NumCounters);
+ Record.SingleByteCoverage = hasSingleByteCoverage();
for (uint32_t I = 0; I < NumCounters; I++) {
const char *Ptr =
CountersStart + CounterBaseOffset + I * getCounterTypeSize();
>From b9bbc7cac3076594cd326ffa7f2d4fc4a92fabb9 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sat, 5 Oct 2024 10:43:26 +0900
Subject: [PATCH 03/24] Rework. (Also reverts "[Coverage] Move
SingleByteCoverage out of CountedRegion")
---
.../test/profile/instrprof-block-coverage.c | 2 +-
.../test/profile/instrprof-entry-coverage.c | 2 +-
.../ProfileData/Coverage/CoverageMapping.h | 27 +++++++------
llvm/include/llvm/ProfileData/InstrProf.h | 5 +--
.../ProfileData/Coverage/CoverageMapping.cpp | 40 +++++++------------
llvm/lib/ProfileData/InstrProf.cpp | 2 +-
llvm/lib/ProfileData/InstrProfReader.cpp | 1 -
7 files changed, 33 insertions(+), 46 deletions(-)
diff --git a/compiler-rt/test/profile/instrprof-block-coverage.c b/compiler-rt/test/profile/instrprof-block-coverage.c
index 8d924e1cac64d8..829d5af8dc3f9e 100644
--- a/compiler-rt/test/profile/instrprof-block-coverage.c
+++ b/compiler-rt/test/profile/instrprof-block-coverage.c
@@ -49,4 +49,4 @@ int main(int argc, char *argv[]) {
// CHECK-ERROR-NOT: warning: {{.*}}: Found inconsistent block coverage
-// COUNTS: Maximum function count: 1
+// COUNTS: Maximum function count: 4
diff --git a/compiler-rt/test/profile/instrprof-entry-coverage.c b/compiler-rt/test/profile/instrprof-entry-coverage.c
index b93a4e0c43ccd6..1c6816ba01964b 100644
--- a/compiler-rt/test/profile/instrprof-entry-coverage.c
+++ b/compiler-rt/test/profile/instrprof-entry-coverage.c
@@ -36,4 +36,4 @@ int main(int argc, char *argv[]) {
// CHECK-DAG: foo
// CHECK-DAG: bar
-// COUNTS: Maximum function count: 1
+// COUNTS: Maximum function count: 2
diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
index 77c447efe1a7c9..fa07b3a9e8b14f 100644
--- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -359,15 +359,19 @@ struct CountedRegion : public CounterMappingRegion {
uint64_t ExecutionCount;
uint64_t FalseExecutionCount;
bool Folded;
+ bool HasSingleByteCoverage;
- CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
+ CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount,
+ bool HasSingleByteCoverage)
: CounterMappingRegion(R), ExecutionCount(ExecutionCount),
- FalseExecutionCount(0), Folded(false) {}
+ FalseExecutionCount(0), Folded(false),
+ HasSingleByteCoverage(HasSingleByteCoverage) {}
CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount,
- uint64_t FalseExecutionCount)
+ uint64_t FalseExecutionCount, bool HasSingleByteCoverage)
: CounterMappingRegion(R), ExecutionCount(ExecutionCount),
- FalseExecutionCount(FalseExecutionCount), Folded(false) {}
+ FalseExecutionCount(FalseExecutionCount), Folded(false),
+ HasSingleByteCoverage(HasSingleByteCoverage) {}
};
/// MCDC Record grouping all information together.
@@ -698,12 +702,9 @@ struct FunctionRecord {
std::vector<MCDCRecord> MCDCRecords;
/// The number of times this function was executed.
uint64_t ExecutionCount = 0;
- bool SingleByteCoverage;
- FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames,
- bool SingleByteCoverage)
- : Name(Name), Filenames(Filenames.begin(), Filenames.end()),
- SingleByteCoverage(SingleByteCoverage) {}
+ FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames)
+ : Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
FunctionRecord(FunctionRecord &&FR) = default;
FunctionRecord &operator=(FunctionRecord &&) = default;
@@ -713,10 +714,11 @@ struct FunctionRecord {
}
void pushRegion(CounterMappingRegion Region, uint64_t Count,
- uint64_t FalseCount) {
+ uint64_t FalseCount, bool HasSingleByteCoverage) {
if (Region.Kind == CounterMappingRegion::BranchRegion ||
Region.Kind == CounterMappingRegion::MCDCBranchRegion) {
- CountedBranchRegions.emplace_back(Region, Count, FalseCount);
+ CountedBranchRegions.emplace_back(Region, Count, FalseCount,
+ HasSingleByteCoverage);
// If both counters are hard-coded to zero, then this region represents a
// constant-folded branch.
if (Region.Count.isZero() && Region.FalseCount.isZero())
@@ -725,7 +727,8 @@ struct FunctionRecord {
}
if (CountedRegions.empty())
ExecutionCount = Count;
- CountedRegions.emplace_back(Region, Count, FalseCount);
+ CountedRegions.emplace_back(Region, Count, FalseCount,
+ HasSingleByteCoverage);
}
};
diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h
index df9e76966bf42b..b0b2258735e2ae 100644
--- a/llvm/include/llvm/ProfileData/InstrProf.h
+++ b/llvm/include/llvm/ProfileData/InstrProf.h
@@ -830,7 +830,6 @@ struct InstrProfValueSiteRecord {
/// Profiling information for a single function.
struct InstrProfRecord {
std::vector<uint64_t> Counts;
- bool SingleByteCoverage = false;
std::vector<uint8_t> BitmapBytes;
InstrProfRecord() = default;
@@ -840,15 +839,13 @@ struct InstrProfRecord {
: Counts(std::move(Counts)), BitmapBytes(std::move(BitmapBytes)) {}
InstrProfRecord(InstrProfRecord &&) = default;
InstrProfRecord(const InstrProfRecord &RHS)
- : Counts(RHS.Counts), SingleByteCoverage(RHS.SingleByteCoverage),
- BitmapBytes(RHS.BitmapBytes),
+ : Counts(RHS.Counts), BitmapBytes(RHS.BitmapBytes),
ValueData(RHS.ValueData
? std::make_unique<ValueProfData>(*RHS.ValueData)
: nullptr) {}
InstrProfRecord &operator=(InstrProfRecord &&) = default;
InstrProfRecord &operator=(const InstrProfRecord &RHS) {
Counts = RHS.Counts;
- SingleByteCoverage = RHS.SingleByteCoverage;
BitmapBytes = RHS.BitmapBytes;
if (!RHS.ValueData) {
ValueData = nullptr;
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index bc765c59381718..9a03dede84b6df 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -856,7 +856,7 @@ Error CoverageMapping::loadFunctionRecord(
return Error::success();
MCDCDecisionRecorder MCDCDecisions;
- FunctionRecord Function(OrigFuncName, Record.Filenames, SingleByteCoverage);
+ FunctionRecord Function(OrigFuncName, Record.Filenames);
for (const auto &Region : Record.MappingRegions) {
// MCDCDecisionRegion should be handled first since it overlaps with
// others inside.
@@ -874,10 +874,10 @@ Error CoverageMapping::loadFunctionRecord(
consumeError(std::move(E));
return Error::success();
}
- assert(!SingleByteCoverage ||
- (0 <= *ExecutionCount && *ExecutionCount <= 1 &&
- 0 <= *AltExecutionCount && *AltExecutionCount <= 1));
- Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
+ Function.pushRegion(
+ Region, (SingleByteCoverage && *ExecutionCount ? 1 : *ExecutionCount),
+ (SingleByteCoverage && *AltExecutionCount ? 1 : *AltExecutionCount),
+ SingleByteCoverage);
// Record ExpansionRegion.
if (Region.Kind == CounterMappingRegion::ExpansionRegion) {
@@ -1273,8 +1273,7 @@ class SegmentBuilder {
/// Combine counts of regions which cover the same area.
static ArrayRef<CountedRegion>
- combineRegions(MutableArrayRef<CountedRegion> Regions,
- bool SingleByteCoverage) {
+ combineRegions(MutableArrayRef<CountedRegion> Regions) {
if (Regions.empty())
return Regions;
auto Active = Regions.begin();
@@ -1301,7 +1300,9 @@ class SegmentBuilder {
// 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 (SingleByteCoverage)
+ assert(I->HasSingleByteCoverage == Active->HasSingleByteCoverage &&
+ "Regions are generated in different coverage modes");
+ if (I->HasSingleByteCoverage)
Active->ExecutionCount = Active->ExecutionCount || I->ExecutionCount;
else
Active->ExecutionCount += I->ExecutionCount;
@@ -1313,14 +1314,12 @@ class SegmentBuilder {
public:
/// Build a sorted list of CoverageSegments from a list of Regions.
static std::vector<CoverageSegment>
- buildSegments(MutableArrayRef<CountedRegion> Regions,
- bool SingleByteCoverage) {
+ buildSegments(MutableArrayRef<CountedRegion> Regions) {
std::vector<CoverageSegment> Segments;
SegmentBuilder Builder(Segments);
sortNestedRegions(Regions);
- ArrayRef<CountedRegion> CombinedRegions =
- combineRegions(Regions, SingleByteCoverage);
+ ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
LLVM_DEBUG({
dbgs() << "Combined regions:\n";
@@ -1407,14 +1406,10 @@ CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
// the filename, we may get back some records that are not in the file.
ArrayRef<unsigned> RecordIndices =
getImpreciseRecordIndicesForFilename(Filename);
- std::optional<bool> SingleByteCoverage;
for (unsigned RecordIndex : RecordIndices) {
const FunctionRecord &Function = Functions[RecordIndex];
auto MainFileID = findMainViewFileID(Filename, Function);
auto FileIDs = gatherFileIDs(Filename, Function);
- assert(!SingleByteCoverage ||
- *SingleByteCoverage == Function.SingleByteCoverage);
- SingleByteCoverage = Function.SingleByteCoverage;
for (const auto &CR : Function.CountedRegions)
if (FileIDs.test(CR.FileID)) {
Regions.push_back(CR);
@@ -1432,8 +1427,7 @@ CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
}
LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
- FileCoverage.Segments =
- SegmentBuilder::buildSegments(Regions, *SingleByteCoverage);
+ FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
return FileCoverage;
}
@@ -1489,8 +1483,7 @@ CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
<< "\n");
- FunctionCoverage.Segments =
- SegmentBuilder::buildSegments(Regions, Function.SingleByteCoverage);
+ FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
return FunctionCoverage;
}
@@ -1500,12 +1493,8 @@ CoverageData CoverageMapping::getCoverageForExpansion(
CoverageData ExpansionCoverage(
Expansion.Function.Filenames[Expansion.FileID]);
std::vector<CountedRegion> Regions;
- std::optional<bool> SingleByteCoverage;
for (const auto &CR : Expansion.Function.CountedRegions)
if (CR.FileID == Expansion.FileID) {
- assert(!SingleByteCoverage ||
- *SingleByteCoverage == Expansion.Function.SingleByteCoverage);
- SingleByteCoverage = Expansion.Function.SingleByteCoverage;
Regions.push_back(CR);
if (isExpansion(CR, Expansion.FileID))
ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
@@ -1517,8 +1506,7 @@ CoverageData CoverageMapping::getCoverageForExpansion(
LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file "
<< Expansion.FileID << "\n");
- ExpansionCoverage.Segments =
- SegmentBuilder::buildSegments(Regions, *SingleByteCoverage);
+ ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
return ExpansionCoverage;
}
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index 0f6677b4d35718..b9937c9429b77d 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -952,7 +952,7 @@ void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight,
Value = getInstrMaxCountValue();
Overflowed = true;
}
- Counts[I] = (SingleByteCoverage && Value != 0 ? 1 : Value);
+ Counts[I] = Value;
if (Overflowed)
Warn(instrprof_error::counter_overflow);
}
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index a07d7f573275ba..b90617c74f6d13 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -743,7 +743,6 @@ Error RawInstrProfReader<IntPtrT>::readRawCounts(
Record.Counts.clear();
Record.Counts.reserve(NumCounters);
- Record.SingleByteCoverage = hasSingleByteCoverage();
for (uint32_t I = 0; I < NumCounters; I++) {
const char *Ptr =
CountersStart + CounterBaseOffset + I * getCounterTypeSize();
>From 618e63946923a460b2684738e636c77b1706322a Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Thu, 17 Oct 2024 01:22:54 +0900
Subject: [PATCH 04/24] Introduce CounterExpressionBuilder::replace(C, Map)
This return a counter for each term in the expression replaced by
ReplaceMap.
At the moment, this doesn't update the Map, so Map is marked as `const`.
---
.../ProfileData/Coverage/CoverageMapping.h | 6 ++++
.../ProfileData/Coverage/CoverageMapping.cpp | 32 +++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
index fa07b3a9e8b14f..d6528bd407ef34 100644
--- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -34,6 +34,7 @@
#include <cassert>
#include <cstdint>
#include <iterator>
+#include <map>
#include <memory>
#include <sstream>
#include <string>
@@ -213,6 +214,11 @@ class CounterExpressionBuilder {
/// Return a counter that represents the expression that subtracts RHS from
/// LHS.
Counter subtract(Counter LHS, Counter RHS, bool Simplify = true);
+
+ using ReplaceMap = std::map<Counter, Counter>;
+
+ /// Return a counter for each term in the expression replaced by ReplaceMap.
+ Counter replace(Counter C, const ReplaceMap &Map);
};
using LineColPair = std::pair<unsigned, unsigned>;
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index c713371da81e40..b50f025d261e13 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -135,6 +135,38 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
return Simplify ? simplify(Cnt) : Cnt;
}
+Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) {
+ auto I = Map.find(C);
+
+ // Replace C with the Map even if C is Expression.
+ if (I != Map.end())
+ return I->second;
+
+ // Traverse only Expression.
+ if (!C.isExpression())
+ return C;
+
+ auto CE = Expressions[C.getExpressionID()];
+ auto NewLHS = replace(CE.LHS, Map);
+ auto NewRHS = replace(CE.RHS, Map);
+
+ // Reconstruct Expression with induced subexpressions.
+ switch (CE.Kind) {
+ case CounterExpression::Add:
+ C = add(NewLHS, NewRHS);
+ break;
+ case CounterExpression::Subtract:
+ C = subtract(NewLHS, NewRHS);
+ break;
+ }
+
+ // Reconfirm if the reconstructed expression would hit the Map.
+ if ((I = Map.find(C)) != Map.end())
+ return I->second;
+
+ return C;
+}
+
void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
switch (C.getKind()) {
case Counter::Zero:
>From fc697f04fd6c9f3c217ce04e3f1dd082c1f1a705 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 16 Oct 2024 23:16:53 +0900
Subject: [PATCH 05/24] [Coverage] Introduce `getBranchCounterPair()`. NFC.
This aggregates the generation of branch counter pair as `ExecCnt` and
`SkipCnt`, to aggregate `CounterExpr::subtract`. At the moment:
- This change preserves the behavior of
`llvm::EnableSingleByteCoverage`. Almost of SingleByteCoverage will
be cleaned up by coming commits.
- `getBranchCounterPair()` is not called in
`llvm::EnableSingleByteCoverage`. I will implement the new behavior
of SingleByteCoverage in it.
- `IsCounterEqual(Out, Par)` is introduced instead of
`Counter::operator==`. Tweaks would be required for the comparison
for additional counters.
- Braces around `assert()` is intentional. I will add a statement there.
https://discourse.llvm.org/t/rfc-integrating-singlebytecoverage-with-branch-coverage/82492
---
clang/lib/CodeGen/CoverageMappingGen.cpp | 177 +++++++++++++----------
1 file changed, 102 insertions(+), 75 deletions(-)
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 07015834bc84f3..0bfad9cbcbe12b 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -941,6 +941,19 @@ struct CounterCoverageMappingBuilder
return Counter::getCounter(CounterMap[S]);
}
+ std::pair<Counter, Counter> getBranchCounterPair(const Stmt *S,
+ Counter ParentCnt) {
+ Counter ExecCnt = getRegionCounter(S);
+ return {ExecCnt, Builder.subtract(ParentCnt, ExecCnt)};
+ }
+
+ bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
+ if (OutCount == ParentCount)
+ return true;
+
+ return false;
+ }
+
/// Push a region onto the stack.
///
/// Returns the index on the stack where the region was pushed. This can be
@@ -1592,6 +1605,13 @@ struct CounterCoverageMappingBuilder
llvm::EnableSingleByteCoverage
? getRegionCounter(S->getCond())
: addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
+ auto [ExecCount, ExitCount] =
+ (llvm::EnableSingleByteCoverage
+ ? std::make_pair(getRegionCounter(S), Counter::getZero())
+ : getBranchCounterPair(S, CondCount));
+ if (!llvm::EnableSingleByteCoverage) {
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+ }
propagateCounts(CondCount, S->getCond());
adjustForOutOfOrderTraversal(getEnd(S));
@@ -1600,13 +1620,11 @@ struct CounterCoverageMappingBuilder
if (Gap)
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
- Counter OutCount =
- llvm::EnableSingleByteCoverage
- ? getRegionCounter(S)
- : addCounters(BC.BreakCount,
- subtractCounters(CondCount, BodyCount));
+ Counter OutCount = llvm::EnableSingleByteCoverage
+ ? getRegionCounter(S)
+ : addCounters(BC.BreakCount, ExitCount);
- if (OutCount != ParentCount) {
+ if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
if (BodyHasTerminateStmt)
@@ -1615,8 +1633,7 @@ struct CounterCoverageMappingBuilder
// Create Branch Region around condition.
if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount,
- subtractCounters(CondCount, BodyCount));
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
}
void VisitDoStmt(const DoStmt *S) {
@@ -1645,22 +1662,26 @@ struct CounterCoverageMappingBuilder
Counter CondCount = llvm::EnableSingleByteCoverage
? getRegionCounter(S->getCond())
: addCounters(BackedgeCount, BC.ContinueCount);
+ auto [ExecCount, ExitCount] =
+ (llvm::EnableSingleByteCoverage
+ ? std::make_pair(getRegionCounter(S), Counter::getZero())
+ : getBranchCounterPair(S, CondCount));
+ if (!llvm::EnableSingleByteCoverage) {
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+ }
propagateCounts(CondCount, S->getCond());
- Counter OutCount =
- llvm::EnableSingleByteCoverage
- ? getRegionCounter(S)
- : addCounters(BC.BreakCount,
- subtractCounters(CondCount, BodyCount));
- if (OutCount != ParentCount) {
+ Counter OutCount = llvm::EnableSingleByteCoverage
+ ? getRegionCounter(S)
+ : addCounters(BC.BreakCount, ExitCount);
+ if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
}
// Create Branch Region around condition.
if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount,
- subtractCounters(CondCount, BodyCount));
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
if (BodyHasTerminateStmt)
HasTerminateStmt = true;
@@ -1709,6 +1730,13 @@ struct CounterCoverageMappingBuilder
: addCounters(
addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
IncrementBC.ContinueCount);
+ auto [ExecCount, ExitCount] =
+ (llvm::EnableSingleByteCoverage
+ ? std::make_pair(getRegionCounter(S), Counter::getZero())
+ : getBranchCounterPair(S, CondCount));
+ if (!llvm::EnableSingleByteCoverage) {
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+ }
if (const Expr *Cond = S->getCond()) {
propagateCounts(CondCount, Cond);
@@ -1723,9 +1751,8 @@ struct CounterCoverageMappingBuilder
Counter OutCount =
llvm::EnableSingleByteCoverage
? getRegionCounter(S)
- : addCounters(BodyBC.BreakCount, IncrementBC.BreakCount,
- subtractCounters(CondCount, BodyCount));
- if (OutCount != ParentCount) {
+ : addCounters(BodyBC.BreakCount, IncrementBC.BreakCount, ExitCount);
+ if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
if (BodyHasTerminateStmt)
@@ -1734,8 +1761,7 @@ struct CounterCoverageMappingBuilder
// Create Branch Region around condition.
if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount,
- subtractCounters(CondCount, BodyCount));
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
}
void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
@@ -1764,15 +1790,21 @@ struct CounterCoverageMappingBuilder
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
Counter OutCount;
+ Counter ExitCount;
Counter LoopCount;
if (llvm::EnableSingleByteCoverage)
OutCount = getRegionCounter(S);
else {
- LoopCount = addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
- OutCount =
- addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
+ LoopCount =
+ (ParentCount.isZero()
+ ? ParentCount
+ : addCounters(ParentCount, BackedgeCount, BC.ContinueCount));
+ auto [ExecCount, SkipCount] = getBranchCounterPair(S, LoopCount);
+ ExitCount = SkipCount;
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+ OutCount = addCounters(BC.BreakCount, ExitCount);
}
- if (OutCount != ParentCount) {
+ if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
if (BodyHasTerminateStmt)
@@ -1781,8 +1813,7 @@ struct CounterCoverageMappingBuilder
// Create Branch Region around condition.
if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount,
- subtractCounters(LoopCount, BodyCount));
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
}
void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
@@ -1803,10 +1834,13 @@ struct CounterCoverageMappingBuilder
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
Counter LoopCount =
- addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
- Counter OutCount =
- addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
- if (OutCount != ParentCount) {
+ (ParentCount.isZero()
+ ? ParentCount
+ : addCounters(ParentCount, BackedgeCount, BC.ContinueCount));
+ auto [ExecCount, ExitCount] = getBranchCounterPair(S, LoopCount);
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+ Counter OutCount = addCounters(BC.BreakCount, ExitCount);
+ if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
}
@@ -2016,9 +2050,12 @@ struct CounterCoverageMappingBuilder
extendRegion(S->getCond());
Counter ParentCount = getRegion().getCounter();
- Counter ThenCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getThen())
- : getRegionCounter(S);
+ auto [ThenCount, ElseCount] =
+ (llvm::EnableSingleByteCoverage
+ ? std::make_pair(getRegionCounter(S->getThen()),
+ (S->getElse() ? getRegionCounter(S->getElse())
+ : Counter::getZero()))
+ : getBranchCounterPair(S, ParentCount));
// Emitting a counter for the condition makes it easier to interpret the
// counter for the body when looking at the coverage.
@@ -2033,12 +2070,6 @@ struct CounterCoverageMappingBuilder
extendRegion(S->getThen());
Counter OutCount = propagateCounts(ThenCount, S->getThen());
- Counter ElseCount;
- if (!llvm::EnableSingleByteCoverage)
- ElseCount = subtractCounters(ParentCount, ThenCount);
- else if (S->getElse())
- ElseCount = getRegionCounter(S->getElse());
-
if (const Stmt *Else = S->getElse()) {
bool ThenHasTerminateStmt = HasTerminateStmt;
HasTerminateStmt = false;
@@ -2061,15 +2092,14 @@ struct CounterCoverageMappingBuilder
if (llvm::EnableSingleByteCoverage)
OutCount = getRegionCounter(S);
- if (OutCount != ParentCount) {
+ if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
}
if (!S->isConsteval() && !llvm::EnableSingleByteCoverage)
// Create Branch Region around condition.
- createBranchRegion(S->getCond(), ThenCount,
- subtractCounters(ParentCount, ThenCount));
+ createBranchRegion(S->getCond(), ThenCount, ElseCount);
}
void VisitCXXTryStmt(const CXXTryStmt *S) {
@@ -2095,9 +2125,11 @@ struct CounterCoverageMappingBuilder
extendRegion(E);
Counter ParentCount = getRegion().getCounter();
- Counter TrueCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(E->getTrueExpr())
- : getRegionCounter(E);
+ auto [TrueCount, FalseCount] =
+ (llvm::EnableSingleByteCoverage
+ ? std::make_pair(getRegionCounter(E->getTrueExpr()),
+ getRegionCounter(E->getFalseExpr()))
+ : getBranchCounterPair(E, ParentCount));
Counter OutCount;
if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) {
@@ -2116,25 +2148,20 @@ struct CounterCoverageMappingBuilder
}
extendRegion(E->getFalseExpr());
- Counter FalseCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(E->getFalseExpr())
- : subtractCounters(ParentCount, TrueCount);
-
Counter FalseOutCount = propagateCounts(FalseCount, E->getFalseExpr());
if (llvm::EnableSingleByteCoverage)
OutCount = getRegionCounter(E);
else
OutCount = addCounters(OutCount, FalseOutCount);
- if (OutCount != ParentCount) {
+ if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
}
// Create Branch Region around condition.
if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(E->getCond(), TrueCount,
- subtractCounters(ParentCount, TrueCount));
+ createBranchRegion(E->getCond(), TrueCount, FalseCount);
}
void createOrCancelDecision(const BinaryOperator *E, unsigned Since) {
@@ -2233,27 +2260,27 @@ struct CounterCoverageMappingBuilder
extendRegion(E->getRHS());
propagateCounts(getRegionCounter(E), E->getRHS());
+ if (llvm::EnableSingleByteCoverage)
+ return;
+
// Track RHS True/False Decision.
const auto DecisionRHS = MCDCBuilder.back();
+ // Extract the Parent Region Counter.
+ Counter ParentCnt = getRegion().getCounter();
+
// Extract the RHS's Execution Counter.
- Counter RHSExecCnt = getRegionCounter(E);
+ auto [RHSExecCnt, LHSExitCnt] = getBranchCounterPair(E, ParentCnt);
// Extract the RHS's "True" Instance Counter.
- Counter RHSTrueCnt = getRegionCounter(E->getRHS());
-
- // Extract the Parent Region Counter.
- Counter ParentCnt = getRegion().getCounter();
+ auto [RHSTrueCnt, RHSExitCnt] =
+ getBranchCounterPair(E->getRHS(), RHSExecCnt);
// Create Branch Region around LHS condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(E->getLHS(), RHSExecCnt,
- subtractCounters(ParentCnt, RHSExecCnt), DecisionLHS);
+ createBranchRegion(E->getLHS(), RHSExecCnt, LHSExitCnt, DecisionLHS);
// Create Branch Region around RHS condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(E->getRHS(), RHSTrueCnt,
- subtractCounters(RHSExecCnt, RHSTrueCnt), DecisionRHS);
+ createBranchRegion(E->getRHS(), RHSTrueCnt, RHSExitCnt, DecisionRHS);
// Create MCDC Decision Region if at top-level (root).
if (IsRootNode)
@@ -2294,31 +2321,31 @@ struct CounterCoverageMappingBuilder
extendRegion(E->getRHS());
propagateCounts(getRegionCounter(E), E->getRHS());
+ if (llvm::EnableSingleByteCoverage)
+ return;
+
// Track RHS True/False Decision.
const auto DecisionRHS = MCDCBuilder.back();
+ // Extract the Parent Region Counter.
+ Counter ParentCnt = getRegion().getCounter();
+
// Extract the RHS's Execution Counter.
- Counter RHSExecCnt = getRegionCounter(E);
+ auto [RHSExecCnt, LHSExitCnt] = getBranchCounterPair(E, ParentCnt);
// Extract the RHS's "False" Instance Counter.
- Counter RHSFalseCnt = getRegionCounter(E->getRHS());
+ auto [RHSFalseCnt, RHSExitCnt] =
+ getBranchCounterPair(E->getRHS(), RHSExecCnt);
if (!shouldVisitRHS(E->getLHS())) {
GapRegionCounter = OutCount;
}
- // Extract the Parent Region Counter.
- Counter ParentCnt = getRegion().getCounter();
-
// Create Branch Region around LHS condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(E->getLHS(), subtractCounters(ParentCnt, RHSExecCnt),
- RHSExecCnt, DecisionLHS);
+ createBranchRegion(E->getLHS(), LHSExitCnt, RHSExecCnt, DecisionLHS);
// Create Branch Region around RHS condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(E->getRHS(), subtractCounters(RHSExecCnt, RHSFalseCnt),
- RHSFalseCnt, DecisionRHS);
+ createBranchRegion(E->getRHS(), RHSExitCnt, RHSFalseCnt, DecisionRHS);
// Create MCDC Decision Region if at top-level (root).
if (IsRootNode)
>From e4172ca273a6fdfcbfc4662c9e37276ef34c2df4 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Thu, 17 Oct 2024 00:32:26 +0900
Subject: [PATCH 06/24] Introduce the type `CounterPair` for RegionCounterMap
`CounterPair` can hold `<uint32_t, uint32_t>` instead of current
`unsigned`, to hold also the counter number of SkipPath. For now, this
change provides the skeleton and only `CounterPair::first` is used.
Each counter number can have `None` to suppress emitting counter
increment. `second` is initialized as `None` by default, since most
`Stmt*` don't have a pair of counters.
This change also provides stubs for the verifyer. I'll provide the
impl of verifier for `+Asserts` later.
`markStmtAsUsed(bool, Stmt*)` may be used to inform that other side
counter may not emitted.
`markStmtMaybeUsed(S)` may be used for the `Stmt` and its inner will
be excluded for emission in the case of skipping by constant
folding. I put it into places where I found.
`verifyCounterMap()` will check the coverage map the counter map and
can be used to report inconsistency.
These verifier methods shall be eliminated in `-Asserts`.
https://discourse.llvm.org/t/rfc-integrating-singlebytecoverage-with-branch-coverage/82492
---
clang/lib/CodeGen/CGDecl.cpp | 9 ++++++++-
clang/lib/CodeGen/CGExpr.cpp | 1 +
clang/lib/CodeGen/CGExprScalar.cpp | 9 +++++++--
clang/lib/CodeGen/CGStmt.cpp | 3 +++
clang/lib/CodeGen/CodeGenFunction.cpp | 3 +++
clang/lib/CodeGen/CodeGenFunction.h | 6 ++++++
clang/lib/CodeGen/CodeGenModule.h | 19 +++++++++++++++++++
clang/lib/CodeGen/CodeGenPGO.cpp | 14 ++++++++++----
clang/lib/CodeGen/CodeGenPGO.h | 17 +++++++++++++++--
clang/lib/CodeGen/CoverageMappingGen.cpp | 6 +++---
clang/lib/CodeGen/CoverageMappingGen.h | 5 +++--
11 files changed, 78 insertions(+), 14 deletions(-)
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 563f728e29d781..ed5f41b624b62b 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -362,6 +362,8 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
return GV;
}
+ PGO.markStmtMaybeUsed(D.getInit()); // FIXME: Too lazy
+
#ifndef NDEBUG
CharUnits VarSize = CGM.getContext().getTypeSizeInChars(D.getType()) +
D.getFlexibleArrayInitChars(getContext());
@@ -1869,7 +1871,10 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
// If we are at an unreachable point, we don't need to emit the initializer
// unless it contains a label.
if (!HaveInsertPoint()) {
- if (!Init || !ContainsLabel(Init)) return;
+ if (!Init || !ContainsLabel(Init)) {
+ PGO.markStmtMaybeUsed(Init);
+ return;
+ }
EnsureInsertPoint();
}
@@ -1978,6 +1983,8 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
return EmitExprAsInit(Init, &D, lv, capturedByInit);
}
+ PGO.markStmtMaybeUsed(Init);
+
if (!emission.IsConstantAggregate) {
// For simple scalar/complex initialization, store the value directly.
LValue lv = MakeAddrLValue(Loc, type);
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 52d2f6d52abf94..2fd6b02a3395ee 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -5134,6 +5134,7 @@ std::optional<LValue> HandleConditionalOperatorLValueSimpleCase(
// If the true case is live, we need to track its region.
if (CondExprBool)
CGF.incrementProfileCounter(E);
+ CGF.markStmtMaybeUsed(Dead);
// If a throw expression we emit it and return an undefined lvalue
// because it can't be used.
if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Live->IgnoreParens())) {
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index b7f5b932c56b6f..74e93f889f4261 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4982,8 +4982,10 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
}
// 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
- if (!CGF.ContainsLabel(E->getRHS()))
+ if (!CGF.ContainsLabel(E->getRHS())) {
+ CGF.markStmtMaybeUsed(E->getRHS());
return llvm::Constant::getNullValue(ResTy);
+ }
}
// If the top of the logical operator nest, reset the MCDC temp to 0.
@@ -5122,8 +5124,10 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
}
// 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
- if (!CGF.ContainsLabel(E->getRHS()))
+ if (!CGF.ContainsLabel(E->getRHS())) {
+ CGF.markStmtMaybeUsed(E->getRHS());
return llvm::ConstantInt::get(ResTy, 1);
+ }
}
// If the top of the logical operator nest, reset the MCDC temp to 0.
@@ -5247,6 +5251,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
CGF.incrementProfileCounter(E);
}
Value *Result = Visit(live);
+ CGF.markStmtMaybeUsed(dead);
// If the live part is a throw expression, it acts like it has a void
// type, so evaluating it returns a null Value*. However, a conditional
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 41dc91c578c800..dbc1ce9bf993cd 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -76,6 +76,7 @@ void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
// Verify that any decl statements were handled as simple, they may be in
// scope of subsequent reachable statements.
assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
+ PGO.markStmtMaybeUsed(S);
return;
}
@@ -845,6 +846,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
RunCleanupsScope ExecutedScope(*this);
EmitStmt(Executed);
}
+ PGO.markStmtMaybeUsed(Skipped);
return;
}
}
@@ -2170,6 +2172,7 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
EmitStmt(CaseStmts[i]);
incrementProfileCounter(&S);
+ PGO.markStmtMaybeUsed(S.getBody());
// Now we want to restore the saved switch instance so that nested
// switches continue to function properly
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 24723e392c2a3a..371aa494e014bd 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1606,6 +1606,8 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
// Emit the standard function epilogue.
FinishFunction(BodyRange.getEnd());
+ PGO.verifyCounterMap();
+
// If we haven't marked the function nothrow through other means, do
// a quick pass now to see if we can.
if (!CurFn->doesNotThrow())
@@ -1728,6 +1730,7 @@ bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond))
return false; // Contains a label.
+ PGO.markStmtMaybeUsed(Cond);
ResultInt = Int;
return true;
}
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 9ba0ed02a564dd..89ac3b342d0a7c 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -1620,6 +1620,12 @@ class CodeGenFunction : public CodeGenTypeCache {
uint64_t LoopCount) const;
public:
+ std::pair<bool, bool> getIsCounterPair(const Stmt *S) const {
+ return PGO.getIsCounterPair(S);
+ }
+
+ void markStmtMaybeUsed(const Stmt *S) { PGO.markStmtMaybeUsed(S); }
+
/// Increment the profiler's counter for the given statement by \p StepV.
/// If \p StepV is null, the default increment is 1.
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) {
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index c58bb88035ca8a..9dc497321b42a6 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -101,6 +101,25 @@ enum ForDefinition_t : bool {
ForDefinition = true
};
+class CounterPair : public std::pair<uint32_t, uint32_t> {
+private:
+ static constexpr uint32_t None = (1u << 31); /// None is set
+
+public:
+ static constexpr uint32_t Mask = None - 1;
+
+public:
+ CounterPair(unsigned Val = 0) {
+ assert(!(Val & ~Mask));
+ first = Val;
+ second = None;
+ }
+
+ std::pair<bool, bool> getIsCounterPair() const {
+ return {!(first & None), !(second & None)};
+ }
+};
+
struct OrderGlobalInitsOrStermFinalizers {
unsigned int priority;
unsigned int lex_order;
diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp
index 820bb521ccf850..069469e3de856b 100644
--- a/clang/lib/CodeGen/CodeGenPGO.cpp
+++ b/clang/lib/CodeGen/CodeGenPGO.cpp
@@ -164,7 +164,7 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
/// The function hash.
PGOHash Hash;
/// The map of statements to counters.
- llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
+ llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;
/// The state of MC/DC Coverage in this function.
MCDC::State &MCDCState;
/// Maximum number of supported MC/DC conditions in a boolean expression.
@@ -175,7 +175,7 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
DiagnosticsEngine &Diag;
MapRegionCounters(PGOHashVersion HashVersion, uint64_t ProfileVersion,
- llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
+ llvm::DenseMap<const Stmt *, CounterPair> &CounterMap,
MCDC::State &MCDCState, unsigned MCDCMaxCond,
DiagnosticsEngine &Diag)
: NextCounter(0), Hash(HashVersion), CounterMap(CounterMap),
@@ -1084,7 +1084,7 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
(CGM.getCodeGenOpts().MCDCCoverage ? CGM.getCodeGenOpts().MCDCMaxConds
: 0);
- RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
+ RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, CounterPair>);
RegionMCDCState.reset(new MCDC::State);
MapRegionCounters Walker(HashVersion, ProfileVersion, *RegionCounterMap,
*RegionMCDCState, MCDCMaxConditions, CGM.getDiags());
@@ -1186,12 +1186,18 @@ CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
Fn->setEntryCount(FunctionCount);
}
+std::pair<bool, bool> CodeGenPGO::getIsCounterPair(const Stmt *S) const {
+ if (!RegionCounterMap || RegionCounterMap->count(S) == 0)
+ return {false, false};
+ return (*RegionCounterMap)[S].getIsCounterPair();
+}
+
void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
llvm::Value *StepV) {
if (!RegionCounterMap || !Builder.GetInsertBlock())
return;
- unsigned Counter = (*RegionCounterMap)[S];
+ unsigned Counter = (*RegionCounterMap)[S].first;
// Make sure that pointer to global is passed in with zero addrspace
// This is relevant during GPU profiling
diff --git a/clang/lib/CodeGen/CodeGenPGO.h b/clang/lib/CodeGen/CodeGenPGO.h
index 9d66ffad6f4350..83f35785e5327d 100644
--- a/clang/lib/CodeGen/CodeGenPGO.h
+++ b/clang/lib/CodeGen/CodeGenPGO.h
@@ -35,7 +35,7 @@ class CodeGenPGO {
std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
unsigned NumRegionCounters;
uint64_t FunctionHash;
- std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
+ std::unique_ptr<llvm::DenseMap<const Stmt *, CounterPair>> RegionCounterMap;
std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
std::unique_ptr<MCDC::State> RegionMCDCState;
@@ -110,6 +110,7 @@ class CodeGenPGO {
bool canEmitMCDCCoverage(const CGBuilderTy &Builder);
public:
+ std::pair<bool, bool> getIsCounterPair(const Stmt *S) const;
void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
llvm::Value *StepV);
void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
@@ -122,6 +123,18 @@ class CodeGenPGO {
Address MCDCCondBitmapAddr, llvm::Value *Val,
CodeGenFunction &CGF);
+ void markStmtAsUsed(bool Skipped, const Stmt *S) {
+ // Do nothing.
+ }
+
+ void markStmtMaybeUsed(const Stmt *S) {
+ // Do nothing.
+ }
+
+ void verifyCounterMap() {
+ // Do nothing.
+ }
+
/// Return the region count for the counter at the given index.
uint64_t getRegionCount(const Stmt *S) {
if (!RegionCounterMap)
@@ -130,7 +143,7 @@ class CodeGenPGO {
return 0;
// With profiles from a differing version of clang we can have mismatched
// decl counts. Don't crash in such a case.
- auto Index = (*RegionCounterMap)[S];
+ auto Index = (*RegionCounterMap)[S].first;
if (Index >= RegionCounts.size())
return 0;
return RegionCounts[Index];
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 07015834bc84f3..08e4ac80e0e87c 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -885,7 +885,7 @@ struct CounterCoverageMappingBuilder
: public CoverageMappingBuilder,
public ConstStmtVisitor<CounterCoverageMappingBuilder> {
/// The map of statements to count values.
- llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
+ llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;
MCDC::State &MCDCState;
@@ -938,7 +938,7 @@ struct CounterCoverageMappingBuilder
///
/// This should only be called on statements that have a dedicated counter.
Counter getRegionCounter(const Stmt *S) {
- return Counter::getCounter(CounterMap[S]);
+ return Counter::getCounter(CounterMap[S].first);
}
/// Push a region onto the stack.
@@ -1421,7 +1421,7 @@ struct CounterCoverageMappingBuilder
CounterCoverageMappingBuilder(
CoverageMappingModuleGen &CVM,
- llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
+ llvm::DenseMap<const Stmt *, CounterPair> &CounterMap,
MCDC::State &MCDCState, SourceManager &SM, const LangOptions &LangOpts)
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
MCDCState(MCDCState), MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {}
diff --git a/clang/lib/CodeGen/CoverageMappingGen.h b/clang/lib/CodeGen/CoverageMappingGen.h
index fe4b93f3af8561..0ed50597e1dc3e 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.h
+++ b/clang/lib/CodeGen/CoverageMappingGen.h
@@ -95,6 +95,7 @@ class CoverageSourceInfo : public PPCallbacks,
namespace CodeGen {
class CodeGenModule;
+class CounterPair;
namespace MCDC {
struct State;
@@ -158,7 +159,7 @@ class CoverageMappingGen {
CoverageMappingModuleGen &CVM;
SourceManager &SM;
const LangOptions &LangOpts;
- llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
+ llvm::DenseMap<const Stmt *, CounterPair> *CounterMap;
MCDC::State *MCDCState;
public:
@@ -169,7 +170,7 @@ class CoverageMappingGen {
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
const LangOptions &LangOpts,
- llvm::DenseMap<const Stmt *, unsigned> *CounterMap,
+ llvm::DenseMap<const Stmt *, CounterPair> *CounterMap,
MCDC::State *MCDCState)
: CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap),
MCDCState(MCDCState) {}
>From 5e460594c8a2550c38c759b2e6f1c5dc4152f820 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Thu, 17 Oct 2024 22:15:12 +0900
Subject: [PATCH 07/24] [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.
---
clang/lib/CodeGen/CodeGenFunction.h | 8 ++++-
clang/lib/CodeGen/CodeGenPGO.cpp | 31 +++++++++++++++++--
clang/lib/CodeGen/CodeGenPGO.h | 1 +
clang/lib/CodeGen/CoverageMappingGen.cpp | 31 ++++++++++++++-----
.../ProfileData/Coverage/CoverageMapping.cpp | 4 +++
5 files changed, 65 insertions(+), 10 deletions(-)
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 89ac3b342d0a7c..cb1192bf6e11fe 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -1629,11 +1629,17 @@ class CodeGenFunction : public CodeGenTypeCache {
/// Increment the profiler's counter for the given statement by \p StepV.
/// If \p StepV is null, the default increment is 1.
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) {
+ incrementProfileCounter(false, S, false, StepV);
+ }
+
+ void incrementProfileCounter(bool UseSkipPath, const Stmt *S,
+ bool UseBoth = false,
+ llvm::Value *StepV = nullptr) {
if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
!CurFn->hasFnAttribute(llvm::Attribute::NoProfile) &&
!CurFn->hasFnAttribute(llvm::Attribute::SkipProfile)) {
auto AL = ApplyDebugLocation::CreateArtificial(*this);
- PGO.emitCounterSetOrIncrement(Builder, S, StepV);
+ PGO.emitCounterSetOrIncrement(Builder, S, UseSkipPath, UseBoth, StepV);
}
PGO.setCurrentStmt(S);
}
diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp
index 069469e3de856b..aefd53e12088b4 100644
--- a/clang/lib/CodeGen/CodeGenPGO.cpp
+++ b/clang/lib/CodeGen/CodeGenPGO.cpp
@@ -1138,6 +1138,19 @@ void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
if (CoverageMapping.empty())
return;
+ // Scan max(FalseCnt) and update NumRegionCounters.
+ unsigned MaxNumCounters = NumRegionCounters;
+ for (const auto [_, V] : *RegionCounterMap) {
+ auto HasCounters = V.getIsCounterPair();
+ assert((!HasCounters.first ||
+ MaxNumCounters > (V.first & CounterPair::Mask)) &&
+ "TrueCnt should not be reassigned");
+ if (HasCounters.second)
+ MaxNumCounters =
+ std::max(MaxNumCounters, (V.second & CounterPair::Mask) + 1);
+ }
+ NumRegionCounters = MaxNumCounters;
+
CGM.getCoverageMapping()->addFunctionMappingRecord(
FuncNameVar, FuncName, FunctionHash, CoverageMapping);
}
@@ -1193,11 +1206,25 @@ std::pair<bool, bool> CodeGenPGO::getIsCounterPair(const Stmt *S) const {
}
void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
+ bool UseSkipPath, bool UseBoth,
llvm::Value *StepV) {
- if (!RegionCounterMap || !Builder.GetInsertBlock())
+ if (!RegionCounterMap)
return;
- unsigned Counter = (*RegionCounterMap)[S].first;
+ unsigned Counter;
+ auto &TheMap = (*RegionCounterMap)[S];
+ auto IsCounter = TheMap.getIsCounterPair();
+ if (!UseSkipPath) {
+ assert(IsCounter.first);
+ Counter = (TheMap.first & CounterPair::Mask);
+ } else {
+ if (!IsCounter.second)
+ return;
+ Counter = (TheMap.second & CounterPair::Mask);
+ }
+
+ if (!Builder.GetInsertBlock())
+ return;
// Make sure that pointer to global is passed in with zero addrspace
// This is relevant during GPU profiling
diff --git a/clang/lib/CodeGen/CodeGenPGO.h b/clang/lib/CodeGen/CodeGenPGO.h
index 83f35785e5327d..8b769dd88d7f1e 100644
--- a/clang/lib/CodeGen/CodeGenPGO.h
+++ b/clang/lib/CodeGen/CodeGenPGO.h
@@ -112,6 +112,7 @@ class CodeGenPGO {
public:
std::pair<bool, bool> getIsCounterPair(const Stmt *S) const;
void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
+ bool UseFalsePath, bool UseBoth,
llvm::Value *StepV);
void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
Address MCDCCondBitmapAddr,
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index a5d83e7a743bbd..0bcbd20593ae22 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -887,6 +887,9 @@ struct CounterCoverageMappingBuilder
/// The map of statements to count values.
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;
+ CounterExpressionBuilder::ReplaceMap MapToExpand;
+ unsigned NextCounterNum;
+
MCDC::State &MCDCState;
/// A stack of currently live regions.
@@ -922,15 +925,11 @@ struct CounterCoverageMappingBuilder
/// Return a counter for the sum of \c LHS and \c RHS.
Counter addCounters(Counter LHS, Counter RHS, bool Simplify = true) {
- assert(!llvm::EnableSingleByteCoverage &&
- "cannot add counters when single byte coverage mode is enabled");
return Builder.add(LHS, RHS, Simplify);
}
Counter addCounters(Counter C1, Counter C2, Counter C3,
bool Simplify = true) {
- assert(!llvm::EnableSingleByteCoverage &&
- "cannot add counters when single byte coverage mode is enabled");
return addCounters(addCounters(C1, C2, Simplify), C3, Simplify);
}
@@ -943,14 +942,31 @@ struct CounterCoverageMappingBuilder
std::pair<Counter, Counter> getBranchCounterPair(const Stmt *S,
Counter ParentCnt) {
- Counter ExecCnt = getRegionCounter(S);
- return {ExecCnt, Builder.subtract(ParentCnt, ExecCnt)};
+ auto &TheMap = CounterMap[S];
+ auto ExecCnt = Counter::getCounter(TheMap.first);
+ auto SkipExpr = Builder.subtract(ParentCnt, ExecCnt);
+
+ if (!llvm::EnableSingleByteCoverage)
+ return {ExecCnt, SkipExpr};
+
+ // Assign second if second is not assigned yet.
+ if (!TheMap.getIsCounterPair().second)
+ TheMap.second = NextCounterNum++;
+
+ Counter SkipCnt = Counter::getCounter(TheMap.second);
+ MapToExpand[SkipCnt] = SkipExpr;
+ return {ExecCnt, SkipCnt};
}
bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
if (OutCount == ParentCount)
return true;
+ // Try comaparison with pre-replaced expressions.
+ if (Builder.replace(Builder.subtract(OutCount, ParentCount), MapToExpand)
+ .isZero())
+ return true;
+
return false;
}
@@ -1437,7 +1453,8 @@ struct CounterCoverageMappingBuilder
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap,
MCDC::State &MCDCState, SourceManager &SM, const LangOptions &LangOpts)
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
- MCDCState(MCDCState), MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {}
+ NextCounterNum(CounterMap.size()), MCDCState(MCDCState),
+ MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {}
/// Write the mapping data to the output stream
void write(llvm::raw_ostream &OS) {
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index b50f025d261e13..fc7f36c8599f54 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -640,6 +640,10 @@ static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
unsigned MaxCounterID = 0;
for (const auto &Region : Record.MappingRegions) {
MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
+ if (Region.Kind == CounterMappingRegion::BranchRegion ||
+ Region.Kind == CounterMappingRegion::MCDCBranchRegion)
+ MaxCounterID =
+ std::max(MaxCounterID, Ctx.getMaxCounterID(Region.FalseCount));
}
return MaxCounterID;
}
>From ad136910aad1c8e53a8c6091999ad2f90d180761 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Fri, 18 Oct 2024 09:37:18 +0900
Subject: [PATCH 08/24] Rewind changes for folding
---
clang/lib/CodeGen/CoverageMappingGen.cpp | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 0bfad9cbcbe12b..8bd9ab402f4e59 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1795,10 +1795,7 @@ struct CounterCoverageMappingBuilder
if (llvm::EnableSingleByteCoverage)
OutCount = getRegionCounter(S);
else {
- LoopCount =
- (ParentCount.isZero()
- ? ParentCount
- : addCounters(ParentCount, BackedgeCount, BC.ContinueCount));
+ LoopCount = addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
auto [ExecCount, SkipCount] = getBranchCounterPair(S, LoopCount);
ExitCount = SkipCount;
assert(ExecCount.isZero() || ExecCount == BodyCount);
@@ -1834,9 +1831,7 @@ struct CounterCoverageMappingBuilder
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
Counter LoopCount =
- (ParentCount.isZero()
- ? ParentCount
- : addCounters(ParentCount, BackedgeCount, BC.ContinueCount));
+ addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
auto [ExecCount, ExitCount] = getBranchCounterPair(S, LoopCount);
assert(ExecCount.isZero() || ExecCount == BodyCount);
Counter OutCount = addCounters(BC.BreakCount, ExitCount);
>From 209ea4cfdb4f93d3c8fc60dc9af29af39fd24758 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 20 Oct 2024 11:58:16 +0900
Subject: [PATCH 09/24] Update comments
---
llvm/lib/ProfileData/Coverage/CoverageMapping.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index b50f025d261e13..6f44797be20e32 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -138,14 +138,14 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) {
auto I = Map.find(C);
- // Replace C with the Map even if C is Expression.
+ // Replace C with the value found in Map even if C is Expression.
if (I != Map.end())
return I->second;
- // Traverse only Expression.
if (!C.isExpression())
return C;
+ // Traverse both sides of Expression.
auto CE = Expressions[C.getExpressionID()];
auto NewLHS = replace(CE.LHS, Map);
auto NewRHS = replace(CE.RHS, Map);
>From f0afd04dd86573f1e7d868cc6e1c677d1779aa5f Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 20 Oct 2024 12:00:23 +0900
Subject: [PATCH 10/24] Use initializer statements
---
llvm/lib/ProfileData/Coverage/CoverageMapping.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index 6f44797be20e32..7ff2e5ba69e19d 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -136,10 +136,8 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
}
Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) {
- auto I = Map.find(C);
-
// Replace C with the value found in Map even if C is Expression.
- if (I != Map.end())
+ if (auto I = Map.find(C); I != Map.end())
return I->second;
if (!C.isExpression())
@@ -161,7 +159,7 @@ Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) {
}
// Reconfirm if the reconstructed expression would hit the Map.
- if ((I = Map.find(C)) != Map.end())
+ if (auto I = Map.find(C); I != Map.end())
return I->second;
return C;
>From be516faa39e1152d637ac425229f8a88480ba41b Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 20 Oct 2024 11:57:03 +0900
Subject: [PATCH 11/24] `first` may be cancelled.
Currently `first` is not None by default.
---
clang/lib/CodeGen/CodeGenPGO.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp
index aefd53e12088b4..0f2090da47a374 100644
--- a/clang/lib/CodeGen/CodeGenPGO.cpp
+++ b/clang/lib/CodeGen/CodeGenPGO.cpp
@@ -1215,7 +1215,8 @@ void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
auto &TheMap = (*RegionCounterMap)[S];
auto IsCounter = TheMap.getIsCounterPair();
if (!UseSkipPath) {
- assert(IsCounter.first);
+ if (!IsCounter.first)
+ return;
Counter = (TheMap.first & CounterPair::Mask);
} else {
if (!IsCounter.second)
>From 52f072e5058267660aa8c8fbb00c5d09634f22b3 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Fri, 18 Oct 2024 08:32:39 +0900
Subject: [PATCH 12/24] clang/test/CoverageMapping/single-byte-counters.cpp:
Rewrite counter matches
---
.../CoverageMapping/single-byte-counters.cpp | 163 +++++++-----------
1 file changed, 65 insertions(+), 98 deletions(-)
diff --git a/clang/test/CoverageMapping/single-byte-counters.cpp b/clang/test/CoverageMapping/single-byte-counters.cpp
index 8e9b613dcc68f7..d20b695bc2636a 100644
--- a/clang/test/CoverageMapping/single-byte-counters.cpp
+++ b/clang/test/CoverageMapping/single-byte-counters.cpp
@@ -1,169 +1,136 @@
// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -mllvm -enable-single-byte-coverage=true -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name single-byte-counters.cpp %s | FileCheck %s
// CHECK: testIf
-int testIf(int x) { // CHECK-NEXT: File 0, [[@LINE]]:19 -> [[@LINE+10]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+5]]:7 -> [[@LINE+5]]:13 = #0
- // CHECK-NEXT: Gap,File 0, [[@LINE+4]]:14 -> [[@LINE+5]]:5 = #1
- // CHECK-NEXT: File 0, [[@LINE+4]]:5 -> [[@LINE+4]]:16 = #1
- // CHECK-NEXT: File 0, [[@LINE+5]]:3 -> [[@LINE+5]]:16 = #2
+int testIf(int x) { // CHECK-NEXT: File 0, [[@LINE]]:19 -> [[@LINE+7]]:2 = [[C00:#0]]
int result = 0;
- if (x == 0)
- result = -1;
+ if (x == 0) // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:13 = [[C00]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+1]]:5 = [[C0T:#1]]
+ result = -1; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:16 = [[C0T]]
- return result;
+ return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C0E:#2]]
}
// CHECK-NEXT: testIfElse
-int testIfElse(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+13]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+7]]:7 -> [[@LINE+7]]:12 = #0
- // CHECK-NEXT: Gap,File 0, [[@LINE+6]]:13 -> [[@LINE+7]]:5 = #1
- // CHECK-NEXT: File 0, [[@LINE+6]]:5 -> [[@LINE+6]]:15 = #1
- // CHECK-NEXT: Gap,File 0, [[@LINE+5]]:16 -> [[@LINE+7]]:5 = #2
- // CHECK-NEXT: File 0, [[@LINE+6]]:5 -> [[@LINE+6]]:19 = #2
- // CHECK-NEXT: File 0, [[@LINE+6]]:3 -> [[@LINE+6]]:16 = #3
+int testIfElse(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+8]]:2 = [[C10:#0]]
int result = 0;
- if (x < 0)
- result = 0;
- else
- result = x * x;
- return result;
+ if (x < 0) // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:12 = [[C10]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:13 -> [[@LINE+1]]:5 = [[C1T:#1]]
+ result = 0; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:15 = [[C1T]]
+ else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C1F:#2]]
+ result = x * x; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:19 = [[C1F]]
+ return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C1E:#3]]
}
// CHECK-NEXT: testIfElseReturn
-int testIfElseReturn(int x) { // CHECK-NEXT: File 0, [[@LINE]]:29 -> [[@LINE+14]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+8]]:7 -> [[@LINE+8]]:12 = #0
- // CHECK-NEXT: Gap,File 0, [[@LINE+7]]:13 -> [[@LINE+8]]:5 = #1
- // CHECK-NEXT: File 0, [[@LINE+7]]:5 -> [[@LINE+7]]:19 = #1
- // CHECK-NEXT: Gap,File 0, [[@LINE+6]]:20 -> [[@LINE+8]]:5 = #2
- // CHECK-NEXT: File 0, [[@LINE+7]]:5 -> [[@LINE+7]]:13 = #2
- // CHECK-NEXT: Gap,File 0, [[@LINE+6]]:14 -> [[@LINE+7]]:3 = #3
- // CHECK-NEXT: File 0, [[@LINE+6]]:3 -> [[@LINE+6]]:16 = #3
+int testIfElseReturn(int x) { // CHECK-NEXT: File 0, [[@LINE]]:29 -> [[@LINE+9]]:2 = [[C20:#0]]
int result = 0;
- if (x > 0)
- result = x * x;
- else
- return 0;
- return result;
+ if (x > 0) // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:12 = [[C20]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:13 -> [[@LINE+1]]:5 = [[C2T:#1]]
+ result = x * x; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:19 = [[C2T]]
+ else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:20 -> [[@LINE+1]]:5 = [[C2F:#2]]
+ return 0; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:13 = [[C2F]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+1]]:3 = [[C2E:#3]]
+ return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C2E:#3]]
}
// CHECK-NEXT: testSwitch
-int testSwitch(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+22]]:2 = #0
- // CHECK-NEXT: Gap,File 0, [[@LINE+9]]:14 -> [[@LINE+17]]:15 = 0
- // CHECK-NEXT: File 0, [[@LINE+9]]:3 -> [[@LINE+11]]:10 = #2
- // CHECK-NEXT: Gap,File 0, [[@LINE+10]]:11 -> [[@LINE+11]]:3 = 0
- // CHECK-NEXT: File 0, [[@LINE+10]]:3 -> [[@LINE+12]]:10 = #3
- // CHECK-NEXT: Gap,File 0, [[@LINE+11]]:11 -> [[@LINE+12]]:3 = 0
- // CHECK-NEXT: File 0, [[@LINE+11]]:3 -> [[@LINE+12]]:15 = #4
- // CHECK-NEXT: Gap,File 0, [[@LINE+12]]:4 -> [[@LINE+14]]:3 = #1
- // CHECK-NEXT: File 0, [[@LINE+13]]:3 -> [[@LINE+13]]:16 = #1
+int testSwitch(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+17]]:2 = [[C30:#0]]
int result;
switch (x) {
- case 1:
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+10]]:15 = 0
+ case 1: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+2]]:10 = [[C31:#2]]
result = 1;
break;
- case 2:
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:11 -> [[@LINE+1]]:3 = 0
+ case 2: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+2]]:10 = [[C32:#3]]
result = 2;
break;
- default:
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:11 -> [[@LINE+1]]:3 = 0
+ default: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+1]]:15 = [[C3D:#4]]
result = 0;
}
-
- return result;
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:4 -> [[@LINE+1]]:3 = [[C3E:#1]]
+ return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C3E]]
}
// CHECK-NEXT: testWhile
-int testWhile() { // CHECK-NEXT: File 0, [[@LINE]]:17 -> [[@LINE+13]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+6]]:10 -> [[@LINE+6]]:16 = #1
- // CHECK-NEXT: Gap,File 0, [[@LINE+5]]:17 -> [[@LINE+5]]:18 = #2
- // CHECK-NEXT: File 0, [[@LINE+4]]:18 -> [[@LINE+7]]:4 = #2
- // CHECK-NEXT: File 0, [[@LINE+8]]:3 -> [[@LINE+8]]:13 = #3
+int testWhile() { // CHECK-NEXT: File 0, [[@LINE]]:17 -> [[@LINE+11]]:2 = [[C40:#0]]
int i = 0;
int sum = 0;
- while (i < 10) {
+ while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = [[C4C:#1]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:17 -> [[@LINE-1]]:18 = [[C4T:#2]]
+ // CHECK-NEXT: File 0, [[@LINE-2]]:18 -> [[@LINE+3]]:4 = [[C4T]]
sum += i;
i++;
}
- return sum;
+ return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C4E:#3]]
}
// CHECK-NEXT: testContinue
-int testContinue() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+21]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+12]]:10 -> [[@LINE+12]]:16 = #1
- // CHECK-NEXT: Gap,File 0, [[@LINE+11]]:17 -> [[@LINE+11]]:18 = #2
- // CHECK-NEXT: File 0, [[@LINE+10]]:18 -> [[@LINE+15]]:4 = #2
- // CHECK-NEXT: File 0, [[@LINE+10]]:9 -> [[@LINE+10]]:15 = #2
- // CHECK-NEXT: Gap,File 0, [[@LINE+9]]:16 -> [[@LINE+10]]:7 = #4
- // CHECK-NEXT: File 0, [[@LINE+9]]:7 -> [[@LINE+9]]:15 = #4
- // CHECK-NEXT: Gap,File 0, [[@LINE+8]]:16 -> [[@LINE+9]]:5 = #5
- // CHECK-NEXT: File 0, [[@LINE+8]]:5 -> [[@LINE+10]]:4 = #5
- // CHECK-NEXT: Gap,File 0, [[@LINE+9]]:4 -> [[@LINE+11]]:3 = #3
- // CHECK-NEXT: File 0, [[@LINE+10]]:3 -> [[@LINE+10]]:13 = #3
+int testContinue() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+15]]:2 = [[C50:#0]]
int i = 0;
int sum = 0;
- while (i < 10) {
- if (i == 4)
- continue;
- sum += i;
+ while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = [[C5C:#1]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:17 -> [[@LINE-1]]:18 = [[C5B:#2]]
+ // CHECK-NEXT: File 0, [[@LINE-2]]:18 -> [[@LINE+7]]:4 = [[C5B]]
+ if (i == 4) // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:15 = [[C5B]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:7 = [[C5T:#4]]
+ continue; // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:15 = [[C5T]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C5F:#5]]
+ sum += i; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE+2]]:4 = [[C5F]]
i++;
}
-
- return sum;
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:4 -> [[@LINE+1]]:3 = [[C5E:#3]]
+ return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C5E]]
}
// CHECK-NEXT: testFor
-int testFor() { // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE+13]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+7]]:19 -> [[@LINE+7]]:25 = #1
- // CHECK-NEXT: File 0, [[@LINE+6]]:27 -> [[@LINE+6]]:30 = #2
- // CHECK-NEXT: Gap,File 0, [[@LINE+5]]:31 -> [[@LINE+5]]:32 = #3
- // CHECK-NEXT: File 0, [[@LINE+4]]:32 -> [[@LINE+6]]:4 = #3
- // CHECK-NEXT: File 0, [[@LINE+7]]:3 -> [[@LINE+7]]:13 = #4
+int testFor() { // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE+12]]:2 = [[C60:#0]]
int i;
int sum = 0;
+ // CHECK-NEXT: File 0, [[@LINE+2]]:19 -> [[@LINE+2]]:25 = [[C61:#1]]
+ // CHECK-NEXT: File 0, [[@LINE+1]]:27 -> [[@LINE+1]]:30 = [[C6C:#2]]
for (int i = 0; i < 10; i++) {
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:31 -> [[@LINE-1]]:32 = [[C6B:#3]]
+ // CHECK-NEXT: File 0, [[@LINE-2]]:32 -> [[@LINE+2]]:4 = [[C6B]]
sum += i;
}
- return sum;
+ return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C6E:#4]]
}
// CHECK-NEXT: testForRange
-int testForRange() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+12]]:2 = #0
- // CHECK-NEXT: Gap,File 0, [[@LINE+6]]:28 -> [[@LINE+6]]:29 = #1
- // CHECK-NEXT: File 0, [[@LINE+5]]:29 -> [[@LINE+7]]:4 = #1
- // CHECK-NEXT: File 0, [[@LINE+8]]:3 -> [[@LINE+8]]:13 = #2
+int testForRange() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+11]]:2 = [[C70:#0]]
int sum = 0;
int array[] = {1, 2, 3, 4, 5};
for (int element : array) {
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:28 -> [[@LINE-1]]:29 = [[C7B:#1]]
+ // CHECK-NEXT: File 0, [[@LINE-2]]:29 -> [[@LINE+2]]:4 = [[C7B]]
sum += element;
}
- return sum;
+ return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C7E:#2]]
}
// CHECK-NEXT: testDo
-int testDo() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+12]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+5]]:6 -> [[@LINE+8]]:4 = #1
- // CHECK-NEXT: File 0, [[@LINE+7]]:12 -> [[@LINE+7]]:17 = #2
- // CHECK-NEXT: File 0, [[@LINE+8]]:3 -> [[@LINE+8]]:13 = #3
+int testDo() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+9]]:2 = [[C80:#0]]
int i = 0;
int sum = 0;
- do {
+ do { // CHECK-NEXT: File 0, [[@LINE]]:6 -> [[@LINE+3]]:4 = [[C8B:#1]]
sum += i;
i++;
- } while (i < 5);
+ } while (i < 5); // CHECK-NEXT: File 0, [[@LINE]]:12 -> [[@LINE]]:17 = [[C8C:#2]]
- return sum;
+ return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C8E:#3]]
}
// CHECK-NEXT: testConditional
-int testConditional(int x) { // CHECK-NEXT: File 0, [[@LINE]]:28 -> [[@LINE+8]]:2 = #0
- // CHECK-NEXT: File 0, [[@LINE+5]]:15 -> [[@LINE+5]]:22 = #0
- // CHECK-NEXT: Gap,File 0, [[@LINE+4]]:24 -> [[@LINE+4]]:25 = #2
- // CHECK-NEXT: File 0, [[@LINE+3]]:25 -> [[@LINE+3]]:26 = #2
- // CHECK-NEXT: File 0, [[@LINE+2]]:29 -> [[@LINE+2]]:31 = #3
- // CHECK-NEXT: File 0, [[@LINE+2]]:2 -> [[@LINE+2]]:15 = #1
- int result = (x > 0) ? 1 : -1;
- return result;
+int testConditional(int x) { // CHECK-NEXT: File 0, [[@LINE]]:28 -> [[@LINE+6]]:2 = [[C90:#0]]
+ int result = (x > 0) ? 1 : -1; // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE]]:22 = [[C90]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:24 -> [[@LINE-1]]:25 = [[C9T:#2]]
+ // CHECK-NEXT: File 0, [[@LINE-2]]:25 -> [[@LINE-2]]:26 = [[C9T]]
+ // CHECK-NEXT: File 0, [[@LINE-3]]:29 -> [[@LINE-3]]:31 = [[C9F:#3]]
+ return result; // CHECK-NEXT: File 0, [[@LINE]]:2 -> [[@LINE]]:15 = [[C9E:#1]]
}
>From 5d19c77551c6fc585d1b15c4c2a71c3c3f99ef8a Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Fri, 18 Oct 2024 09:33:51 +0900
Subject: [PATCH 13/24] [Coverage][Single] Enable Branch coverage for loop
statements
---
clang/lib/CodeGen/CGStmt.cpp | 82 ++++-------
clang/lib/CodeGen/CodeGenFunction.cpp | 11 +-
clang/lib/CodeGen/CodeGenPGO.cpp | 79 +----------
clang/lib/CodeGen/CoverageMappingGen.cpp | 130 +++++-------------
.../CoverageMapping/single-byte-counters.cpp | 53 +++----
5 files changed, 97 insertions(+), 258 deletions(-)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index dbc1ce9bf993cd..7d778ce58a1487 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -1039,15 +1039,11 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
SourceLocToDebugLoc(R.getEnd()),
checkIfLoopMustProgress(S.getCond(), hasEmptyLoopBody(S)));
- // When single byte coverage mode is enabled, add a counter to loop condition.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getCond());
-
// As long as the condition is true, go to the loop body.
llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
if (EmitBoolCondBranch) {
llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
- if (ConditionScope.requiresCleanups())
+ if (getIsCounterPair(&S).second || ConditionScope.requiresCleanups())
ExitBlock = createBasicBlock("while.exit");
llvm::MDNode *Weights =
createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()));
@@ -1058,6 +1054,7 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
+ incrementProfileCounter(true, &S);
EmitBranchThroughCleanup(LoopExit);
}
} else if (const Attr *A = Stmt::getLikelihoodAttr(S.getBody())) {
@@ -1075,11 +1072,7 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
{
RunCleanupsScope BodyScope(*this);
EmitBlock(LoopBody);
- // When single byte coverage mode is enabled, add a counter to the body.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getBody());
- else
- incrementProfileCounter(&S);
+ incrementProfileCounter(false, &S);
EmitStmt(S.getBody());
}
@@ -1099,13 +1092,10 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
// The LoopHeader typically is just a branch if we skipped emitting
// a branch, try to erase it.
- if (!EmitBoolCondBranch)
+ if (!EmitBoolCondBranch) {
SimplifyForwardingBlocks(LoopHeader.getBlock());
-
- // When single byte coverage mode is enabled, add a counter to continuation
- // block.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(&S);
+ PGO.markStmtAsUsed(true, &S);
+ }
if (CGM.shouldEmitConvergenceTokens())
ConvergenceTokenStack.pop_back();
@@ -1124,10 +1114,7 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
// Emit the body of the loop.
llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
- if (llvm::EnableSingleByteCoverage)
- EmitBlockWithFallThrough(LoopBody, S.getBody());
- else
- EmitBlockWithFallThrough(LoopBody, &S);
+ EmitBlockWithFallThrough(LoopBody, &S);
if (CGM.shouldEmitConvergenceTokens())
ConvergenceTokenStack.push_back(
@@ -1139,9 +1126,6 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
}
EmitBlock(LoopCond.getBlock());
- // When single byte coverage mode is enabled, add a counter to loop condition.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getCond());
// C99 6.8.5.2: "The evaluation of the controlling expression takes place
// after each execution of the loop body."
@@ -1164,16 +1148,25 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
SourceLocToDebugLoc(R.getEnd()),
checkIfLoopMustProgress(S.getCond(), hasEmptyLoopBody(S)));
+ auto *LoopFalse =
+ (getIsCounterPair(&S).second ? createBasicBlock("do.loopfalse")
+ : LoopExit.getBlock());
+
// As long as the condition is true, iterate the loop.
if (EmitBoolCondBranch) {
uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount;
Builder.CreateCondBr(
- BoolCondVal, LoopBody, LoopExit.getBlock(),
+ BoolCondVal, LoopBody, LoopFalse,
createProfileWeightsForLoop(S.getCond(), BackedgeCount));
}
LoopStack.pop();
+ if (LoopFalse != LoopExit.getBlock()) {
+ EmitBlock(LoopFalse);
+ incrementProfileCounter(true, &S, true);
+ }
+
// Emit the exit block.
EmitBlock(LoopExit.getBlock());
@@ -1182,11 +1175,6 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
if (!EmitBoolCondBranch)
SimplifyForwardingBlocks(LoopCond.getBlock());
- // When single byte coverage mode is enabled, add a counter to continuation
- // block.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(&S);
-
if (CGM.shouldEmitConvergenceTokens())
ConvergenceTokenStack.pop_back();
}
@@ -1247,15 +1235,10 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
BreakContinueStack.back().ContinueBlock = Continue;
}
- // When single byte coverage mode is enabled, add a counter to loop
- // condition.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getCond());
-
llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
// If there are any cleanups between here and the loop-exit scope,
// create a block to stage a loop exit along.
- if (ForScope.requiresCleanups())
+ if (getIsCounterPair(&S).second || ForScope.requiresCleanups())
ExitBlock = createBasicBlock("for.cond.cleanup");
// As long as the condition is true, iterate the loop.
@@ -1274,6 +1257,7 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
+ incrementProfileCounter(true, &S);
EmitBranchThroughCleanup(LoopExit);
}
@@ -1281,13 +1265,11 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
} else {
// Treat it as a non-zero constant. Don't even create a new block for the
// body, just fall into it.
+ PGO.markStmtAsUsed(true, &S);
}
- // When single byte coverage mode is enabled, add a counter to the body.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getBody());
- else
- incrementProfileCounter(&S);
+ incrementProfileCounter(false, &S);
+
{
// Create a separate cleanup scope for the body, in case it is not
// a compound statement.
@@ -1299,8 +1281,6 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
if (S.getInc()) {
EmitBlock(Continue.getBlock());
EmitStmt(S.getInc());
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getInc());
}
BreakContinueStack.pop_back();
@@ -1317,11 +1297,6 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
// Emit the fall-through block.
EmitBlock(LoopExit.getBlock(), true);
- // When single byte coverage mode is enabled, add a counter to continuation
- // block.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(&S);
-
if (CGM.shouldEmitConvergenceTokens())
ConvergenceTokenStack.pop_back();
}
@@ -1358,7 +1333,7 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
// If there are any cleanups between here and the loop-exit scope,
// create a block to stage a loop exit along.
llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
- if (ForScope.requiresCleanups())
+ if (getIsCounterPair(&S).second || ForScope.requiresCleanups())
ExitBlock = createBasicBlock("for.cond.cleanup");
// The loop body, consisting of the specified body and the loop variable.
@@ -1376,14 +1351,12 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
if (ExitBlock != LoopExit.getBlock()) {
EmitBlock(ExitBlock);
+ incrementProfileCounter(true, &S);
EmitBranchThroughCleanup(LoopExit);
}
EmitBlock(ForBody);
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getBody());
- else
- incrementProfileCounter(&S);
+ incrementProfileCounter(false, &S);
// Create a block for the increment. In case of a 'continue', we jump there.
JumpDest Continue = getJumpDestInCurrentScope("for.inc");
@@ -1414,11 +1387,6 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
// Emit the fall-through block.
EmitBlock(LoopExit.getBlock(), true);
- // When single byte coverage mode is enabled, add a counter to continuation
- // block.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(&S);
-
if (CGM.shouldEmitConvergenceTokens())
ConvergenceTokenStack.pop_back();
}
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index df15d09276c2fb..848f8d1a69c107 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -53,10 +53,6 @@
using namespace clang;
using namespace CodeGen;
-namespace llvm {
-extern cl::opt<bool> EnableSingleByteCoverage;
-} // namespace llvm
-
/// shouldEmitLifetimeMarkers - Decide whether we need emit the life-time
/// markers.
static bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts,
@@ -1361,10 +1357,7 @@ void CodeGenFunction::EmitFunctionBody(const Stmt *Body) {
void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
const Stmt *S) {
llvm::BasicBlock *SkipCountBB = nullptr;
- // Do not skip over the instrumentation when single byte coverage mode is
- // enabled.
- if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr() &&
- !llvm::EnableSingleByteCoverage) {
+ if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) {
// When instrumenting for profiling, the fallthrough to certain
// statements needs to skip over the instrumentation code so that we
// get an accurate count.
@@ -1373,7 +1366,7 @@ void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
}
EmitBlock(BB);
uint64_t CurrentCount = getCurrentProfileCount();
- incrementProfileCounter(S);
+ incrementProfileCounter(false, S);
setCurrentProfileCount(getCurrentProfileCount() + CurrentCount);
if (SkipCountBB)
EmitBlock(SkipCountBB);
diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp
index 0f2090da47a374..6020a611d1a576 100644
--- a/clang/lib/CodeGen/CodeGenPGO.cpp
+++ b/clang/lib/CodeGen/CodeGenPGO.cpp
@@ -394,81 +394,6 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
return true;
}
- bool TraverseWhileStmt(WhileStmt *While) {
- // When single byte coverage mode is enabled, add a counter to condition and
- // body.
- bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage;
- for (Stmt *CS : While->children()) {
- if (!CS || NoSingleByteCoverage)
- continue;
- if (CS == While->getCond())
- CounterMap[While->getCond()] = NextCounter++;
- else if (CS == While->getBody())
- CounterMap[While->getBody()] = NextCounter++;
- }
-
- Base::TraverseWhileStmt(While);
- if (Hash.getHashVersion() != PGO_HASH_V1)
- Hash.combine(PGOHash::EndOfScope);
- return true;
- }
-
- bool TraverseDoStmt(DoStmt *Do) {
- // When single byte coverage mode is enabled, add a counter to condition and
- // body.
- bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage;
- for (Stmt *CS : Do->children()) {
- if (!CS || NoSingleByteCoverage)
- continue;
- if (CS == Do->getCond())
- CounterMap[Do->getCond()] = NextCounter++;
- else if (CS == Do->getBody())
- CounterMap[Do->getBody()] = NextCounter++;
- }
-
- Base::TraverseDoStmt(Do);
- if (Hash.getHashVersion() != PGO_HASH_V1)
- Hash.combine(PGOHash::EndOfScope);
- return true;
- }
-
- bool TraverseForStmt(ForStmt *For) {
- // When single byte coverage mode is enabled, add a counter to condition,
- // increment and body.
- bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage;
- for (Stmt *CS : For->children()) {
- if (!CS || NoSingleByteCoverage)
- continue;
- if (CS == For->getCond())
- CounterMap[For->getCond()] = NextCounter++;
- else if (CS == For->getInc())
- CounterMap[For->getInc()] = NextCounter++;
- else if (CS == For->getBody())
- CounterMap[For->getBody()] = NextCounter++;
- }
-
- Base::TraverseForStmt(For);
- if (Hash.getHashVersion() != PGO_HASH_V1)
- Hash.combine(PGOHash::EndOfScope);
- return true;
- }
-
- bool TraverseCXXForRangeStmt(CXXForRangeStmt *ForRange) {
- // When single byte coverage mode is enabled, add a counter to body.
- bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage;
- for (Stmt *CS : ForRange->children()) {
- if (!CS || NoSingleByteCoverage)
- continue;
- if (CS == ForRange->getBody())
- CounterMap[ForRange->getBody()] = NextCounter++;
- }
-
- Base::TraverseCXXForRangeStmt(ForRange);
- if (Hash.getHashVersion() != PGO_HASH_V1)
- Hash.combine(PGOHash::EndOfScope);
- return true;
- }
-
// If the statement type \p N is nestable, and its nesting impacts profile
// stability, define a custom traversal which tracks the end of the statement
// in the hash (provided we're not using the V1 hash).
@@ -480,6 +405,10 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
return true; \
}
+ DEFINE_NESTABLE_TRAVERSAL(WhileStmt)
+ DEFINE_NESTABLE_TRAVERSAL(DoStmt)
+ DEFINE_NESTABLE_TRAVERSAL(ForStmt)
+ DEFINE_NESTABLE_TRAVERSAL(CXXForRangeStmt)
DEFINE_NESTABLE_TRAVERSAL(ObjCForCollectionStmt)
DEFINE_NESTABLE_TRAVERSAL(CXXTryStmt)
DEFINE_NESTABLE_TRAVERSAL(CXXCatchStmt)
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index a331d5bc68286b..4062c531d0b797 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1570,9 +1570,8 @@ struct CounterCoverageMappingBuilder
void VisitBreakStmt(const BreakStmt *S) {
assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
- if (!llvm::EnableSingleByteCoverage)
- BreakContinueStack.back().BreakCount = addCounters(
- BreakContinueStack.back().BreakCount, getRegion().getCounter());
+ BreakContinueStack.back().BreakCount = addCounters(
+ BreakContinueStack.back().BreakCount, getRegion().getCounter());
// FIXME: a break in a switch should terminate regions for all preceding
// case statements, not just the most recent one.
terminateRegion(S);
@@ -1580,9 +1579,8 @@ struct CounterCoverageMappingBuilder
void VisitContinueStmt(const ContinueStmt *S) {
assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
- if (!llvm::EnableSingleByteCoverage)
- BreakContinueStack.back().ContinueCount = addCounters(
- BreakContinueStack.back().ContinueCount, getRegion().getCounter());
+ BreakContinueStack.back().ContinueCount = addCounters(
+ BreakContinueStack.back().ContinueCount, getRegion().getCounter());
terminateRegion(S);
}
@@ -1600,9 +1598,7 @@ struct CounterCoverageMappingBuilder
extendRegion(S);
Counter ParentCount = getRegion().getCounter();
- Counter BodyCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getBody())
- : getRegionCounter(S);
+ Counter BodyCount = getRegionCounter(S);
// Handle the body first so that we can get the backedge count.
BreakContinueStack.push_back(BreakContinue());
@@ -1615,16 +1611,10 @@ struct CounterCoverageMappingBuilder
// Go back to handle the condition.
Counter CondCount =
- llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getCond())
- : addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
- auto [ExecCount, ExitCount] =
- (llvm::EnableSingleByteCoverage
- ? std::make_pair(getRegionCounter(S), Counter::getZero())
- : getBranchCounterPair(S, CondCount));
- if (!llvm::EnableSingleByteCoverage) {
- assert(ExecCount.isZero() || ExecCount == BodyCount);
- }
+ addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
+ auto [ExecCount, ExitCount] = getBranchCounterPair(S, CondCount);
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+
propagateCounts(CondCount, S->getCond());
adjustForOutOfOrderTraversal(getEnd(S));
@@ -1633,10 +1623,7 @@ struct CounterCoverageMappingBuilder
if (Gap)
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
- Counter OutCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S)
- : addCounters(BC.BreakCount, ExitCount);
-
+ Counter OutCount = addCounters(BC.BreakCount, ExitCount);
if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
@@ -1645,56 +1632,40 @@ struct CounterCoverageMappingBuilder
}
// Create Branch Region around condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount, ExitCount);
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
}
void VisitDoStmt(const DoStmt *S) {
extendRegion(S);
Counter ParentCount = getRegion().getCounter();
- Counter BodyCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getBody())
- : getRegionCounter(S);
+ Counter BodyCount = getRegionCounter(S);
BreakContinueStack.push_back(BreakContinue());
extendRegion(S->getBody());
- Counter BackedgeCount;
- if (llvm::EnableSingleByteCoverage)
- propagateCounts(BodyCount, S->getBody());
- else
- BackedgeCount =
- propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
+ Counter BackedgeCount =
+ propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
BreakContinue BC = BreakContinueStack.pop_back_val();
bool BodyHasTerminateStmt = HasTerminateStmt;
HasTerminateStmt = false;
- Counter CondCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getCond())
- : addCounters(BackedgeCount, BC.ContinueCount);
- auto [ExecCount, ExitCount] =
- (llvm::EnableSingleByteCoverage
- ? std::make_pair(getRegionCounter(S), Counter::getZero())
- : getBranchCounterPair(S, CondCount));
- if (!llvm::EnableSingleByteCoverage) {
- assert(ExecCount.isZero() || ExecCount == BodyCount);
- }
+ Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
+ auto [ExecCount, ExitCount] = getBranchCounterPair(S, CondCount);
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+
propagateCounts(CondCount, S->getCond());
- Counter OutCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S)
- : addCounters(BC.BreakCount, ExitCount);
+ Counter OutCount = addCounters(BC.BreakCount, ExitCount);
if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
}
// Create Branch Region around condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount, ExitCount);
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
if (BodyHasTerminateStmt)
HasTerminateStmt = true;
@@ -1706,9 +1677,7 @@ struct CounterCoverageMappingBuilder
Visit(S->getInit());
Counter ParentCount = getRegion().getCounter();
- Counter BodyCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getBody())
- : getRegionCounter(S);
+ Counter BodyCount = getRegionCounter(S);
// The loop increment may contain a break or continue.
if (S->getInc())
@@ -1727,29 +1696,16 @@ struct CounterCoverageMappingBuilder
// the count for all the continue statements.
BreakContinue IncrementBC;
if (const Stmt *Inc = S->getInc()) {
- Counter IncCount;
- if (llvm::EnableSingleByteCoverage)
- IncCount = getRegionCounter(S->getInc());
- else
- IncCount = addCounters(BackedgeCount, BodyBC.ContinueCount);
- propagateCounts(IncCount, Inc);
+ propagateCounts(addCounters(BackedgeCount, BodyBC.ContinueCount), Inc);
IncrementBC = BreakContinueStack.pop_back_val();
}
// Go back to handle the condition.
- Counter CondCount =
- llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getCond())
- : addCounters(
- addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
- IncrementBC.ContinueCount);
- auto [ExecCount, ExitCount] =
- (llvm::EnableSingleByteCoverage
- ? std::make_pair(getRegionCounter(S), Counter::getZero())
- : getBranchCounterPair(S, CondCount));
- if (!llvm::EnableSingleByteCoverage) {
- assert(ExecCount.isZero() || ExecCount == BodyCount);
- }
+ Counter CondCount = addCounters(
+ addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
+ IncrementBC.ContinueCount);
+ auto [ExecCount, ExitCount] = getBranchCounterPair(S, CondCount);
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
if (const Expr *Cond = S->getCond()) {
propagateCounts(CondCount, Cond);
@@ -1762,9 +1718,7 @@ struct CounterCoverageMappingBuilder
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
Counter OutCount =
- llvm::EnableSingleByteCoverage
- ? getRegionCounter(S)
- : addCounters(BodyBC.BreakCount, IncrementBC.BreakCount, ExitCount);
+ addCounters(BodyBC.BreakCount, IncrementBC.BreakCount, ExitCount);
if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
@@ -1773,8 +1727,7 @@ struct CounterCoverageMappingBuilder
}
// Create Branch Region around condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount, ExitCount);
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
}
void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
@@ -1785,9 +1738,7 @@ struct CounterCoverageMappingBuilder
Visit(S->getRangeStmt());
Counter ParentCount = getRegion().getCounter();
- Counter BodyCount = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S->getBody())
- : getRegionCounter(S);
+ Counter BodyCount = getRegionCounter(S);
BreakContinueStack.push_back(BreakContinue());
extendRegion(S->getBody());
@@ -1802,18 +1753,12 @@ struct CounterCoverageMappingBuilder
if (Gap)
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
- Counter OutCount;
- Counter ExitCount;
- Counter LoopCount;
- if (llvm::EnableSingleByteCoverage)
- OutCount = getRegionCounter(S);
- else {
- LoopCount = addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
- auto [ExecCount, SkipCount] = getBranchCounterPair(S, LoopCount);
- ExitCount = SkipCount;
- assert(ExecCount.isZero() || ExecCount == BodyCount);
- OutCount = addCounters(BC.BreakCount, ExitCount);
- }
+ Counter LoopCount =
+ addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
+ auto [ExecCount, ExitCount] = getBranchCounterPair(S, LoopCount);
+ assert(ExecCount.isZero() || ExecCount == BodyCount);
+
+ Counter OutCount = addCounters(BC.BreakCount, ExitCount);
if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
@@ -1822,8 +1767,7 @@ struct CounterCoverageMappingBuilder
}
// Create Branch Region around condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(S->getCond(), BodyCount, ExitCount);
+ createBranchRegion(S->getCond(), BodyCount, ExitCount);
}
void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
diff --git a/clang/test/CoverageMapping/single-byte-counters.cpp b/clang/test/CoverageMapping/single-byte-counters.cpp
index d20b695bc2636a..401b5d7dd8b84d 100644
--- a/clang/test/CoverageMapping/single-byte-counters.cpp
+++ b/clang/test/CoverageMapping/single-byte-counters.cpp
@@ -54,76 +54,81 @@ int testSwitch(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+17]]:2 =
}
// CHECK-NEXT: testWhile
-int testWhile() { // CHECK-NEXT: File 0, [[@LINE]]:17 -> [[@LINE+11]]:2 = [[C40:#0]]
+int testWhile() { // CHECK-NEXT: File 0, [[@LINE]]:17 -> [[@LINE+12]]:2 = [[C40:#0]]
int i = 0;
int sum = 0;
- while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = [[C4C:#1]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:17 -> [[@LINE-1]]:18 = [[C4T:#2]]
- // CHECK-NEXT: File 0, [[@LINE-2]]:18 -> [[@LINE+3]]:4 = [[C4T]]
+ while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = ([[C40]] + [[C4T:#1]])
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:10 -> [[@LINE-1]]:16 = [[C4T]], [[C4F:#2]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:17 -> [[@LINE-2]]:18 = [[C4T]]
+ // CHECK-NEXT: File 0, [[@LINE-3]]:18 -> [[@LINE+3]]:4 = [[C4T]]
sum += i;
i++;
}
- return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C4E:#3]]
+ return sum; // #0
}
// CHECK-NEXT: testContinue
-int testContinue() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+15]]:2 = [[C50:#0]]
+int testContinue() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+16]]:2 = [[C50:#0]]
int i = 0;
int sum = 0;
- while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = [[C5C:#1]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:17 -> [[@LINE-1]]:18 = [[C5B:#2]]
- // CHECK-NEXT: File 0, [[@LINE-2]]:18 -> [[@LINE+7]]:4 = [[C5B]]
+ while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = (([[C50]] + [[C5T:#2]]) + [[C5F:#3]])
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:10 -> [[@LINE-1]]:16 = [[C5B:#1]], [[C5E:#4]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:17 -> [[@LINE-2]]:18 = [[C5B]]
+ // CHECK-NEXT: File 0, [[@LINE-3]]:18 -> [[@LINE+7]]:4 = [[C5B]]
if (i == 4) // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:15 = [[C5B]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:7 = [[C5T:#4]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:7 = [[C5T]]
continue; // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:15 = [[C5T]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C5F:#5]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C5F]]
sum += i; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE+2]]:4 = [[C5F]]
i++;
}
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:4 -> [[@LINE+1]]:3 = [[C5E:#3]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:4 -> [[@LINE+1]]:3 = [[C5E]]
return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C5E]]
}
// CHECK-NEXT: testFor
-int testFor() { // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE+12]]:2 = [[C60:#0]]
+int testFor() { // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE+13]]:2 = [[C60:#0]]
int i;
int sum = 0;
- // CHECK-NEXT: File 0, [[@LINE+2]]:19 -> [[@LINE+2]]:25 = [[C61:#1]]
- // CHECK-NEXT: File 0, [[@LINE+1]]:27 -> [[@LINE+1]]:30 = [[C6C:#2]]
+ // CHECK-NEXT: File 0, [[@LINE+3]]:19 -> [[@LINE+3]]:25 = ([[C60]] + [[C6B:#1]])
+ // CHECK-NEXT: Branch,File 0, [[@LINE+2]]:19 -> [[@LINE+2]]:25 = [[C6B]], [[C6E:#2]]
+ // CHECK-NEXT: File 0, [[@LINE+1]]:27 -> [[@LINE+1]]:30 = [[C6B]]
for (int i = 0; i < 10; i++) {
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:31 -> [[@LINE-1]]:32 = [[C6B:#3]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:31 -> [[@LINE-1]]:32 = [[C6B]]
// CHECK-NEXT: File 0, [[@LINE-2]]:32 -> [[@LINE+2]]:4 = [[C6B]]
sum += i;
}
- return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C6E:#4]]
+ return sum; // #0
}
// CHECK-NEXT: testForRange
-int testForRange() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+11]]:2 = [[C70:#0]]
+int testForRange() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+12]]:2 = [[C70:#0]]
int sum = 0;
int array[] = {1, 2, 3, 4, 5};
+ // CHECK-NEXT: Branch,File 0, [[@LINE+1]]:20 -> [[@LINE+1]]:21 = [[C7B:#1]], [[C7E:#2]]
for (int element : array) {
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:28 -> [[@LINE-1]]:29 = [[C7B:#1]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:28 -> [[@LINE-1]]:29 = [[C7B]]
// CHECK-NEXT: File 0, [[@LINE-2]]:29 -> [[@LINE+2]]:4 = [[C7B]]
sum += element;
}
- return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C7E:#2]]
+ return sum; // #0
}
// CHECK-NEXT: testDo
-int testDo() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+9]]:2 = [[C80:#0]]
+int testDo() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+10]]:2 = [[C80:#0]]
int i = 0;
int sum = 0;
- do { // CHECK-NEXT: File 0, [[@LINE]]:6 -> [[@LINE+3]]:4 = [[C8B:#1]]
+ do { // CHECK-NEXT: File 0, [[@LINE]]:6 -> [[@LINE+3]]:4 = ([[C80]] + [[C8B:#1]])
sum += i;
i++;
- } while (i < 5); // CHECK-NEXT: File 0, [[@LINE]]:12 -> [[@LINE]]:17 = [[C8C:#2]]
+ } while (i < 5); // CHECK-NEXT: File 0, [[@LINE]]:12 -> [[@LINE]]:17 = ([[C80]] + [[C8B]])
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:12 -> [[@LINE-1]]:17 = [[C8B]], [[C8E:#2]]
- return sum; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:13 = [[C8E:#3]]
+ return sum; // #0
}
// CHECK-NEXT: testConditional
>From 744c5b634de08f9214c82d6fcfde7179bc4edfb0 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 20 Oct 2024 14:46:07 +0900
Subject: [PATCH 14/24] [Coverage][Single] Enable Branch coverage for CondOp
---
clang/lib/CodeGen/CGExpr.cpp | 6 +--
clang/lib/CodeGen/CGExprAgg.cpp | 14 +------
clang/lib/CodeGen/CGExprComplex.cpp | 15 +-------
clang/lib/CodeGen/CGExprScalar.cpp | 37 +++----------------
clang/lib/CodeGen/CodeGenFunction.cpp | 3 +-
clang/lib/CodeGen/CodeGenPGO.cpp | 8 ----
clang/lib/CodeGen/CoverageMappingGen.cpp | 16 ++------
.../CoverageMapping/single-byte-counters.cpp | 11 +++---
8 files changed, 25 insertions(+), 85 deletions(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index cc85f05ad9f70c..67e3a1de17e679 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -5137,8 +5137,7 @@ std::optional<LValue> HandleConditionalOperatorLValueSimpleCase(
if (!CGF.ContainsLabel(Dead)) {
// If the true case is live, we need to track its region.
- if (CondExprBool)
- CGF.incrementProfileCounter(E);
+ CGF.incrementProfileCounter(!CondExprBool, E, true);
CGF.markStmtMaybeUsed(Dead);
// If a throw expression we emit it and return an undefined lvalue
// because it can't be used.
@@ -5177,7 +5176,7 @@ ConditionalInfo EmitConditionalBlocks(CodeGenFunction &CGF,
// Any temporaries created here are conditional.
CGF.EmitBlock(Info.lhsBlock);
- CGF.incrementProfileCounter(E);
+ CGF.incrementProfileCounter(false, E);
eval.begin(CGF);
Info.LHS = BranchGenFunc(CGF, E->getTrueExpr());
eval.end(CGF);
@@ -5188,6 +5187,7 @@ ConditionalInfo EmitConditionalBlocks(CodeGenFunction &CGF,
// Any temporaries created here are conditional.
CGF.EmitBlock(Info.rhsBlock);
+ CGF.incrementProfileCounter(true, E);
eval.begin(CGF);
Info.RHS = BranchGenFunc(CGF, E->getFalseExpr());
eval.end(CGF);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..0c778ef185532f 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -36,10 +36,6 @@ using namespace CodeGen;
// Aggregate Expression Emitter
//===----------------------------------------------------------------------===//
-namespace llvm {
-extern cl::opt<bool> EnableSingleByteCoverage;
-} // namespace llvm
-
namespace {
class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
CodeGenFunction &CGF;
@@ -1293,10 +1289,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
eval.begin(CGF);
CGF.EmitBlock(LHSBlock);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(E->getTrueExpr());
- else
- CGF.incrementProfileCounter(E);
+ CGF.incrementProfileCounter(false, E);
Visit(E->getTrueExpr());
eval.end(CGF);
@@ -1311,8 +1304,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
eval.begin(CGF);
CGF.EmitBlock(RHSBlock);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(E->getFalseExpr());
+ CGF.incrementProfileCounter(true, E);
Visit(E->getFalseExpr());
eval.end(CGF);
@@ -1321,8 +1313,6 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
E->getType());
CGF.EmitBlock(ContBlock);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(E);
}
void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index fef26e7b4ccdbd..bcece9431de764 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -28,10 +28,6 @@ using namespace CodeGen;
// Complex Expression Emitter
//===----------------------------------------------------------------------===//
-namespace llvm {
-extern cl::opt<bool> EnableSingleByteCoverage;
-} // namespace llvm
-
typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
/// Return the complex type that we are meant to emit.
@@ -1381,11 +1377,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
eval.begin(CGF);
CGF.EmitBlock(LHSBlock);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(E->getTrueExpr());
- else
- CGF.incrementProfileCounter(E);
-
+ CGF.incrementProfileCounter(false, E);
ComplexPairTy LHS = Visit(E->getTrueExpr());
LHSBlock = Builder.GetInsertBlock();
CGF.EmitBranch(ContBlock);
@@ -1393,13 +1385,10 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
eval.begin(CGF);
CGF.EmitBlock(RHSBlock);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(E->getFalseExpr());
+ CGF.incrementProfileCounter(true, E);
ComplexPairTy RHS = Visit(E->getFalseExpr());
RHSBlock = Builder.GetInsertBlock();
CGF.EmitBlock(ContBlock);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(E);
eval.end(CGF);
// Create a PHI node for the real part.
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index ca9ab6025128f0..11d4ec8a267605 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -55,10 +55,6 @@ using llvm::Value;
// Scalar Expression Emitter
//===----------------------------------------------------------------------===//
-namespace llvm {
-extern cl::opt<bool> EnableSingleByteCoverage;
-} // namespace llvm
-
namespace {
/// Determine whether the given binary operation may overflow.
@@ -5247,13 +5243,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
// If the dead side doesn't have labels we need, just emit the Live part.
if (!CGF.ContainsLabel(dead)) {
- if (CondExprBool) {
- if (llvm::EnableSingleByteCoverage) {
- CGF.incrementProfileCounter(lhsExpr);
- CGF.incrementProfileCounter(rhsExpr);
- }
- CGF.incrementProfileCounter(E);
- }
+ CGF.incrementProfileCounter(!CondExprBool, E, true);
Value *Result = Visit(live);
CGF.markStmtMaybeUsed(dead);
@@ -5328,17 +5318,13 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
// If this is a really simple expression (like x ? 4 : 5), emit this as a
// select instead of as control flow. We can only do this if it is cheap and
// safe to evaluate the LHS and RHS unconditionally.
- if (isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) &&
+ if (!CGF.getIsCounterPair(E).second &&
+ isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) &&
isCheapEnoughToEvaluateUnconditionally(rhsExpr, CGF)) {
llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
llvm::Value *StepV = Builder.CreateZExtOrBitCast(CondV, CGF.Int64Ty);
- if (llvm::EnableSingleByteCoverage) {
- CGF.incrementProfileCounter(lhsExpr);
- CGF.incrementProfileCounter(rhsExpr);
- CGF.incrementProfileCounter(E);
- } else
- CGF.incrementProfileCounter(E, StepV);
+ CGF.incrementProfileCounter(E, StepV);
llvm::Value *LHS = Visit(lhsExpr);
llvm::Value *RHS = Visit(rhsExpr);
@@ -5370,11 +5356,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
if (CGF.MCDCLogOpStack.empty())
CGF.maybeUpdateMCDCTestVectorBitmap(condExpr);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(lhsExpr);
- else
- CGF.incrementProfileCounter(E);
-
+ CGF.incrementProfileCounter(false, E);
eval.begin(CGF);
Value *LHS = Visit(lhsExpr);
eval.end(CGF);
@@ -5390,9 +5372,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
if (CGF.MCDCLogOpStack.empty())
CGF.maybeUpdateMCDCTestVectorBitmap(condExpr);
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(rhsExpr);
-
+ CGF.incrementProfileCounter(true, E);
eval.begin(CGF);
Value *RHS = Visit(rhsExpr);
eval.end(CGF);
@@ -5411,11 +5391,6 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
PN->addIncoming(LHS, LHSBlock);
PN->addIncoming(RHS, RHSBlock);
- // When single byte coverage mode is enabled, add a counter to continuation
- // block.
- if (llvm::EnableSingleByteCoverage)
- CGF.incrementProfileCounter(E);
-
return PN;
}
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index df15d09276c2fb..2bcba9bef2628c 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -2004,7 +2004,7 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
cond.begin(*this);
EmitBlock(LHSBlock);
- incrementProfileCounter(CondOp);
+ incrementProfileCounter(false, CondOp);
{
ApplyDebugLocation DL(*this, Cond);
EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock,
@@ -2014,6 +2014,7 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
cond.begin(*this);
EmitBlock(RHSBlock);
+ incrementProfileCounter(true, CondOp);
EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock,
TrueCount - LHSScaledTrueCount, LH, CondOp);
cond.end(*this);
diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp
index 0f2090da47a374..69f66290979840 100644
--- a/clang/lib/CodeGen/CodeGenPGO.cpp
+++ b/clang/lib/CodeGen/CodeGenPGO.cpp
@@ -343,14 +343,6 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
return Base::VisitBinaryOperator(S);
}
- bool VisitConditionalOperator(ConditionalOperator *S) {
- if (llvm::EnableSingleByteCoverage && S->getTrueExpr())
- CounterMap[S->getTrueExpr()] = NextCounter++;
- if (llvm::EnableSingleByteCoverage && S->getFalseExpr())
- CounterMap[S->getFalseExpr()] = NextCounter++;
- return Base::VisitConditionalOperator(S);
- }
-
/// Include \p S in the function hash.
bool VisitStmt(Stmt *S) {
auto Type = updateCounterMappings(S);
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index a331d5bc68286b..77e73992098061 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -2125,11 +2125,7 @@ struct CounterCoverageMappingBuilder
extendRegion(E);
Counter ParentCount = getRegion().getCounter();
- auto [TrueCount, FalseCount] =
- (llvm::EnableSingleByteCoverage
- ? std::make_pair(getRegionCounter(E->getTrueExpr()),
- getRegionCounter(E->getFalseExpr()))
- : getBranchCounterPair(E, ParentCount));
+ auto [TrueCount, FalseCount] = getBranchCounterPair(E, ParentCount);
Counter OutCount;
if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) {
@@ -2148,11 +2144,8 @@ struct CounterCoverageMappingBuilder
}
extendRegion(E->getFalseExpr());
- Counter FalseOutCount = propagateCounts(FalseCount, E->getFalseExpr());
- if (llvm::EnableSingleByteCoverage)
- OutCount = getRegionCounter(E);
- else
- OutCount = addCounters(OutCount, FalseOutCount);
+ OutCount =
+ addCounters(OutCount, propagateCounts(FalseCount, E->getFalseExpr()));
if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
@@ -2160,8 +2153,7 @@ struct CounterCoverageMappingBuilder
}
// Create Branch Region around condition.
- if (!llvm::EnableSingleByteCoverage)
- createBranchRegion(E->getCond(), TrueCount, FalseCount);
+ createBranchRegion(E->getCond(), TrueCount, FalseCount);
}
void createOrCancelDecision(const BinaryOperator *E, unsigned Since) {
diff --git a/clang/test/CoverageMapping/single-byte-counters.cpp b/clang/test/CoverageMapping/single-byte-counters.cpp
index d20b695bc2636a..be0454df002bd0 100644
--- a/clang/test/CoverageMapping/single-byte-counters.cpp
+++ b/clang/test/CoverageMapping/single-byte-counters.cpp
@@ -127,10 +127,11 @@ int testDo() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+9]]:2 = [
}
// CHECK-NEXT: testConditional
-int testConditional(int x) { // CHECK-NEXT: File 0, [[@LINE]]:28 -> [[@LINE+6]]:2 = [[C90:#0]]
+int testConditional(int x) { // CHECK-NEXT: File 0, [[@LINE]]:28 -> [[@LINE+7]]:2 = [[C90:#0]]
int result = (x > 0) ? 1 : -1; // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE]]:22 = [[C90]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:24 -> [[@LINE-1]]:25 = [[C9T:#2]]
- // CHECK-NEXT: File 0, [[@LINE-2]]:25 -> [[@LINE-2]]:26 = [[C9T]]
- // CHECK-NEXT: File 0, [[@LINE-3]]:29 -> [[@LINE-3]]:31 = [[C9F:#3]]
- return result; // CHECK-NEXT: File 0, [[@LINE]]:2 -> [[@LINE]]:15 = [[C9E:#1]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:22 = [[C9T:#1]], [[C9F:#2]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:24 -> [[@LINE-2]]:25 = [[C9T]]
+ // CHECK-NEXT: File 0, [[@LINE-3]]:25 -> [[@LINE-3]]:26 = [[C9T]]
+ // CHECK-NEXT: File 0, [[@LINE-4]]:29 -> [[@LINE-4]]:31 = [[C9F]]
+ return result; // #0
}
>From 3ea6383e2142889550f37389dfaaee81e5ae7d9c Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 20 Oct 2024 15:15:03 +0900
Subject: [PATCH 15/24] [Coverage][Single] Enable Branch coverage for IfStmt
---
clang/lib/CodeGen/CGStmt.cpp | 31 +++++++---------
clang/lib/CodeGen/CodeGenPGO.cpp | 12 -------
clang/lib/CodeGen/CoverageMappingGen.cpp | 21 +++--------
.../CoverageMapping/single-byte-counters.cpp | 36 ++++++++++---------
4 files changed, 38 insertions(+), 62 deletions(-)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index dbc1ce9bf993cd..c511e5f4f4213a 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -840,8 +840,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
// If the skipped block has no labels in it, just emit the executed block.
// This avoids emitting dead code and simplifies the CFG substantially.
if (S.isConstexpr() || !ContainsLabel(Skipped)) {
- if (CondConstant)
- incrementProfileCounter(&S);
+ incrementProfileCounter(!CondConstant, &S, true);
if (Executed) {
RunCleanupsScope ExecutedScope(*this);
EmitStmt(Executed);
@@ -851,14 +850,14 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
}
}
+ auto HasSkip = getIsCounterPair(&S);
+
// Otherwise, the condition did not fold, or we couldn't elide it. Just emit
// the conditional branch.
llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
- llvm::BasicBlock *ElseBlock = ContBlock;
- if (Else)
- ElseBlock = createBasicBlock("if.else");
-
+ llvm::BasicBlock *ElseBlock =
+ (Else || HasSkip.second ? createBasicBlock("if.else") : ContBlock);
// Prefer the PGO based weights over the likelihood attribute.
// When the build isn't optimized the metadata isn't used, so don't generate
// it.
@@ -891,10 +890,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
// Emit the 'then' code.
EmitBlock(ThenBlock);
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(S.getThen());
- else
- incrementProfileCounter(&S);
+ incrementProfileCounter(false, &S);
{
RunCleanupsScope ThenScope(*this);
EmitStmt(S.getThen());
@@ -908,9 +904,9 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
auto NL = ApplyDebugLocation::CreateEmpty(*this);
EmitBlock(ElseBlock);
}
- // When single byte coverage mode is enabled, add a counter to else block.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(Else);
+ // Add a counter to else block unless it has CounterExpr.
+ if (HasSkip.second)
+ incrementProfileCounter(true, &S);
{
RunCleanupsScope ElseScope(*this);
EmitStmt(Else);
@@ -920,15 +916,14 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
auto NL = ApplyDebugLocation::CreateEmpty(*this);
EmitBranch(ContBlock);
}
+ } else if (HasSkip.second) {
+ EmitBlock(ElseBlock);
+ incrementProfileCounter(true, &S);
+ EmitBranch(ContBlock);
}
// Emit the continuation block for code after the if.
EmitBlock(ContBlock, true);
-
- // When single byte coverage mode is enabled, add a counter to continuation
- // block.
- if (llvm::EnableSingleByteCoverage)
- incrementProfileCounter(&S);
}
bool CodeGenFunction::checkIfLoopMustProgress(const Expr *ControllingExpression,
diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp
index 0f2090da47a374..f6b9b5c82952c4 100644
--- a/clang/lib/CodeGen/CodeGenPGO.cpp
+++ b/clang/lib/CodeGen/CodeGenPGO.cpp
@@ -366,18 +366,6 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
if (Hash.getHashVersion() == PGO_HASH_V1)
return Base::TraverseIfStmt(If);
- // When single byte coverage mode is enabled, add a counter to then and
- // else.
- bool NoSingleByteCoverage = !llvm::EnableSingleByteCoverage;
- for (Stmt *CS : If->children()) {
- if (!CS || NoSingleByteCoverage)
- continue;
- if (CS == If->getThen())
- CounterMap[If->getThen()] = NextCounter++;
- else if (CS == If->getElse())
- CounterMap[If->getElse()] = NextCounter++;
- }
-
// Otherwise, keep track of which branch we're in while traversing.
VisitStmt(If);
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index a331d5bc68286b..6c6aecb9994c6b 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -2050,12 +2050,7 @@ struct CounterCoverageMappingBuilder
extendRegion(S->getCond());
Counter ParentCount = getRegion().getCounter();
- auto [ThenCount, ElseCount] =
- (llvm::EnableSingleByteCoverage
- ? std::make_pair(getRegionCounter(S->getThen()),
- (S->getElse() ? getRegionCounter(S->getElse())
- : Counter::getZero()))
- : getBranchCounterPair(S, ParentCount));
+ auto [ThenCount, ElseCount] = getBranchCounterPair(S, ParentCount);
// Emitting a counter for the condition makes it easier to interpret the
// counter for the body when looking at the coverage.
@@ -2080,26 +2075,20 @@ struct CounterCoverageMappingBuilder
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ElseCount);
extendRegion(Else);
- Counter ElseOutCount = propagateCounts(ElseCount, Else);
- if (!llvm::EnableSingleByteCoverage)
- OutCount = addCounters(OutCount, ElseOutCount);
+ OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
if (ThenHasTerminateStmt)
HasTerminateStmt = true;
- } else if (!llvm::EnableSingleByteCoverage)
+ } else
OutCount = addCounters(OutCount, ElseCount);
- if (llvm::EnableSingleByteCoverage)
- OutCount = getRegionCounter(S);
-
if (!IsCounterEqual(OutCount, ParentCount)) {
pushRegion(OutCount);
GapRegionCounter = OutCount;
}
- if (!llvm::EnableSingleByteCoverage)
- // Create Branch Region around condition.
- createBranchRegion(S->getCond(), ThenCount, ElseCount);
+ // Create Branch Region around condition.
+ createBranchRegion(S->getCond(), ThenCount, ElseCount);
}
void VisitCXXTryStmt(const CXXTryStmt *S) {
diff --git a/clang/test/CoverageMapping/single-byte-counters.cpp b/clang/test/CoverageMapping/single-byte-counters.cpp
index d20b695bc2636a..533f791eee19e0 100644
--- a/clang/test/CoverageMapping/single-byte-counters.cpp
+++ b/clang/test/CoverageMapping/single-byte-counters.cpp
@@ -1,36 +1,39 @@
// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -mllvm -enable-single-byte-coverage=true -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name single-byte-counters.cpp %s | FileCheck %s
// CHECK: testIf
-int testIf(int x) { // CHECK-NEXT: File 0, [[@LINE]]:19 -> [[@LINE+7]]:2 = [[C00:#0]]
+int testIf(int x) { // CHECK-NEXT: File 0, [[@LINE]]:19 -> [[@LINE+8]]:2 = [[C00:#0]]
int result = 0;
if (x == 0) // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:13 = [[C00]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+1]]:5 = [[C0T:#1]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:7 -> [[@LINE-1]]:13 = [[C0T:#1]], [[C0F:#2]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:14 -> [[@LINE+1]]:5 = [[C0T]]
result = -1; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:16 = [[C0T]]
- return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C0E:#2]]
+ return result; // #0
}
// CHECK-NEXT: testIfElse
-int testIfElse(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+8]]:2 = [[C10:#0]]
+int testIfElse(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+9]]:2 = [[C10:#0]]
int result = 0;
if (x < 0) // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:12 = [[C10]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:13 -> [[@LINE+1]]:5 = [[C1T:#1]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:7 -> [[@LINE-1]]:12 = [[C1T:#1]], [[C1F:#2]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:13 -> [[@LINE+1]]:5 = [[C1T]]
result = 0; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:15 = [[C1T]]
- else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C1F:#2]]
+ else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C1F]]
result = x * x; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:19 = [[C1F]]
- return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C1E:#3]]
+ return result; // #0
}
// CHECK-NEXT: testIfElseReturn
-int testIfElseReturn(int x) { // CHECK-NEXT: File 0, [[@LINE]]:29 -> [[@LINE+9]]:2 = [[C20:#0]]
+int testIfElseReturn(int x) { // CHECK-NEXT: File 0, [[@LINE]]:29 -> [[@LINE+10]]:2 = [[C20:#0]]
int result = 0;
if (x > 0) // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:12 = [[C20]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:13 -> [[@LINE+1]]:5 = [[C2T:#1]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:7 -> [[@LINE-1]]:12 = [[C2T:#1]], [[C2F:#2]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:13 -> [[@LINE+1]]:5 = [[C2T]]
result = x * x; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:19 = [[C2T]]
- else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:20 -> [[@LINE+1]]:5 = [[C2F:#2]]
+ else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:20 -> [[@LINE+1]]:5 = [[C2F]]
return 0; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:13 = [[C2F]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+1]]:3 = [[C2E:#3]]
- return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C2E:#3]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+1]]:3 = [[C2T]]
+ return result; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:16 = [[C2T]]
}
// CHECK-NEXT: testSwitch
@@ -68,16 +71,17 @@ int testWhile() { // CHECK-NEXT: File 0, [[@LINE]]:17 -> [[@LINE+11]]:2 =
}
// CHECK-NEXT: testContinue
-int testContinue() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+15]]:2 = [[C50:#0]]
+int testContinue() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+16]]:2 = [[C50:#0]]
int i = 0;
int sum = 0;
while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = [[C5C:#1]]
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:17 -> [[@LINE-1]]:18 = [[C5B:#2]]
- // CHECK-NEXT: File 0, [[@LINE-2]]:18 -> [[@LINE+7]]:4 = [[C5B]]
+ // CHECK-NEXT: File 0, [[@LINE-2]]:18 -> [[@LINE+8]]:4 = [[C5B]]
if (i == 4) // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:15 = [[C5B]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:7 = [[C5T:#4]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:9 -> [[@LINE-1]]:15 = [[C5T:#4]], [[C5F:#5]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-2]]:16 -> [[@LINE+1]]:7 = [[C5T]]
continue; // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:15 = [[C5T]]
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C5F:#5]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+1]]:5 = [[C5F]]
sum += i; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE+2]]:4 = [[C5F]]
i++;
}
>From ec05cc37e1177f06c9a44a1e39dadc9306cc5c68 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Mon, 21 Oct 2024 08:09:31 +0900
Subject: [PATCH 16/24] [Coverage][Single] Enable Branch coverage for
SwitchStmt
---
clang/lib/CodeGen/CGStmt.cpp | 12 ++++++++++
clang/lib/CodeGen/CoverageMappingGen.cpp | 22 ++++++++++---------
.../CoverageMapping/single-byte-counters.cpp | 13 ++++++-----
3 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index dbc1ce9bf993cd..80fe5cf183de16 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2259,6 +2259,18 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
ConditionScope.ForceCleanup();
+ // Close the last case (or DefaultBlock).
+ EmitBranch(SwitchExit.getBlock());
+
+ // Insert a False Counter if SwitchStmt doesn't have DefaultStmt.
+ if (getIsCounterPair(S.getCond()).second) {
+ auto *ImplicitDefaultBlock = createBasicBlock("sw.false");
+ EmitBlock(ImplicitDefaultBlock);
+ incrementProfileCounter(true, S.getCond());
+ Builder.CreateBr(SwitchInsn->getDefaultDest());
+ SwitchInsn->setDefaultDest(ImplicitDefaultBlock);
+ }
+
// Emit continuation.
EmitBlock(SwitchExit.getBlock(), true);
incrementProfileCounter(&S);
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index a331d5bc68286b..c5fdf23299e4e9 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -958,6 +958,14 @@ struct CounterCoverageMappingBuilder
return {ExecCnt, SkipCnt};
}
+ Counter getSwitchImplicitDefaultCounter(const Stmt *Cond, Counter ParentCount,
+ Counter CaseCountSum) {
+ return (
+ llvm::EnableSingleByteCoverage
+ ? Counter::getCounter(CounterMap[Cond].second = NextCounterNum++)
+ : subtractCounters(ParentCount, CaseCountSum));
+ }
+
bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
if (OutCount == ParentCount)
return true;
@@ -1885,7 +1893,7 @@ struct CounterCoverageMappingBuilder
propagateCounts(Counter::getZero(), Body);
BreakContinue BC = BreakContinueStack.pop_back_val();
- if (!BreakContinueStack.empty() && !llvm::EnableSingleByteCoverage)
+ if (!BreakContinueStack.empty())
BreakContinueStack.back().ContinueCount = addCounters(
BreakContinueStack.back().ContinueCount, BC.ContinueCount);
@@ -1900,11 +1908,6 @@ struct CounterCoverageMappingBuilder
MostRecentLocation = getStart(S);
handleFileExit(ExitLoc);
- // When single byte coverage mode is enabled, do not create branch region by
- // early returning.
- if (llvm::EnableSingleByteCoverage)
- return;
-
// Create a Branch Region around each Case. Subtract the case's
// counter from the Parent counter to track the "False" branch count.
Counter CaseCountSum;
@@ -1920,7 +1923,8 @@ struct CounterCoverageMappingBuilder
// the hidden branch, which will be added later by the CodeGen. This region
// will be associated with the switch statement's condition.
if (!HasDefaultCase) {
- Counter DefaultCount = subtractCounters(ParentCount, CaseCountSum);
+ Counter DefaultCount = getSwitchImplicitDefaultCounter(
+ S->getCond(), ParentCount, CaseCountSum);
createBranchRegion(S->getCond(), Counter::getZero(), DefaultCount);
}
}
@@ -1929,9 +1933,7 @@ struct CounterCoverageMappingBuilder
extendRegion(S);
SourceMappingRegion &Parent = getRegion();
- Counter Count = llvm::EnableSingleByteCoverage
- ? getRegionCounter(S)
- : addCounters(Parent.getCounter(), getRegionCounter(S));
+ Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
// Reuse the existing region if it starts at our label. This is typical of
// the first case in a switch.
diff --git a/clang/test/CoverageMapping/single-byte-counters.cpp b/clang/test/CoverageMapping/single-byte-counters.cpp
index d20b695bc2636a..464fa370d86f09 100644
--- a/clang/test/CoverageMapping/single-byte-counters.cpp
+++ b/clang/test/CoverageMapping/single-byte-counters.cpp
@@ -34,19 +34,22 @@ int testIfElseReturn(int x) { // CHECK-NEXT: File 0, [[@LINE]]:29 -> [[@LINE+9]]
}
// CHECK-NEXT: testSwitch
-int testSwitch(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+17]]:2 = [[C30:#0]]
+int testSwitch(int x) { // CHECK-NEXT: File 0, [[@LINE]]:23 -> [[@LINE+20]]:2 = [[C30:#0]]
int result;
switch (x) {
- // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+10]]:15 = 0
- case 1: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+2]]:10 = [[C31:#2]]
+ // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+13]]:15 = 0
+ case 1: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+3]]:10 = [[C31:#2]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:3 -> [[@LINE-1]]:9 = [[C31]], 0
result = 1;
break;
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:11 -> [[@LINE+1]]:3 = 0
- case 2: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+2]]:10 = [[C32:#3]]
+ case 2: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+3]]:10 = [[C32:#3]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:3 -> [[@LINE-1]]:9 = [[C32]], 0
result = 2;
break;
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:11 -> [[@LINE+1]]:3 = 0
- default: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+1]]:15 = [[C3D:#4]]
+ default: // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE+2]]:15 = [[C3D:#4]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:3 -> [[@LINE-1]]:10 = [[C3D]], 0
result = 0;
}
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:4 -> [[@LINE+1]]:3 = [[C3E:#1]]
>From 16e2bb8b73bcde1c2618bb358a905a9f463c1217 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 20 Oct 2024 16:24:26 +0900
Subject: [PATCH 17/24] [Coverage][Single] Enable Branch coverage for `BinLAnd`
and `BinLOr`
---
clang/lib/CodeGen/CGExprScalar.cpp | 83 +++++++++++++++++++-----
clang/lib/CodeGen/CGStmt.cpp | 4 --
clang/lib/CodeGen/CodeGenFunction.cpp | 43 ++++++++++--
clang/lib/CodeGen/CoverageMappingGen.cpp | 6 --
4 files changed, 104 insertions(+), 32 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 11d4ec8a267605..83962ba96aa484 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4918,6 +4918,9 @@ Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
}
Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
+ auto HasLHSSkip = CGF.getIsCounterPair(E);
+ auto HasRHSSkip = CGF.getIsCounterPair(E->getRHS());
+
// Perform vector logical and on comparisons with zero vectors.
if (E->getType()->isVectorType()) {
CGF.incrementProfileCounter(E);
@@ -4964,11 +4967,17 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
CodeGenFunction::isInstrumentedCondition(E->getRHS())) {
CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
llvm::BasicBlock *FBlock = CGF.createBasicBlock("land.end");
+ llvm::BasicBlock *RHSSkip =
+ (HasRHSSkip.second ? CGF.createBasicBlock("land.rhsskip") : FBlock);
llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("land.rhscnt");
- Builder.CreateCondBr(RHSCond, RHSBlockCnt, FBlock);
+ Builder.CreateCondBr(RHSCond, RHSBlockCnt, RHSSkip);
CGF.EmitBlock(RHSBlockCnt);
- CGF.incrementProfileCounter(E->getRHS());
+ CGF.incrementProfileCounter(false, E->getRHS());
CGF.EmitBranch(FBlock);
+ if (HasRHSSkip.second) {
+ CGF.EmitBlock(RHSSkip);
+ CGF.incrementProfileCounter(true, E->getRHS());
+ }
CGF.EmitBlock(FBlock);
}
@@ -4997,12 +5006,21 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
+ llvm::BasicBlock *LHSFalseBlock =
+ (HasLHSSkip.second ? CGF.createBasicBlock("land.lhsskip") : ContBlock);
+
CodeGenFunction::ConditionalEvaluation eval(CGF);
// Branch on the LHS first. If it is false, go to the failure (cont) block.
- CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock,
+ CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, LHSFalseBlock,
CGF.getProfileCount(E->getRHS()));
+ if (HasLHSSkip.second) {
+ CGF.EmitBlock(LHSFalseBlock);
+ CGF.incrementProfileCounter(true, E);
+ CGF.EmitBranch(ContBlock);
+ }
+
// Any edges into the ContBlock are now from an (indeterminate number of)
// edges from this first condition. All of these values will be false. Start
// setting up the PHI node in the Cont Block for this.
@@ -5014,7 +5032,7 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
eval.begin(CGF);
CGF.EmitBlock(RHSBlock);
- CGF.incrementProfileCounter(E);
+ CGF.incrementProfileCounter(false, E);
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
eval.end(CGF);
@@ -5024,15 +5042,24 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
// If we're generating for profiling or coverage, generate a branch on the
// RHS to a block that increments the RHS true counter needed to track branch
// condition coverage.
+ llvm::BasicBlock *ContIncoming = RHSBlock;
if (InstrumentRegions &&
CodeGenFunction::isInstrumentedCondition(E->getRHS())) {
CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("land.rhscnt");
- Builder.CreateCondBr(RHSCond, RHSBlockCnt, ContBlock);
+ llvm::BasicBlock *RHSBlockSkip =
+ (HasRHSSkip.second ? CGF.createBasicBlock("land.rhsskip") : ContBlock);
+ Builder.CreateCondBr(RHSCond, RHSBlockCnt, RHSBlockSkip);
CGF.EmitBlock(RHSBlockCnt);
- CGF.incrementProfileCounter(E->getRHS());
+ CGF.incrementProfileCounter(false, E->getRHS());
CGF.EmitBranch(ContBlock);
PN->addIncoming(RHSCond, RHSBlockCnt);
+ if (HasRHSSkip.second) {
+ CGF.EmitBlock(RHSBlockSkip);
+ CGF.incrementProfileCounter(true, E->getRHS());
+ CGF.EmitBranch(ContBlock);
+ ContIncoming = RHSBlockSkip;
+ }
}
// Emit an unconditional branch from this block to ContBlock.
@@ -5042,7 +5069,7 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
CGF.EmitBlock(ContBlock);
}
// Insert an entry into the phi node for the edge with the value of RHSCond.
- PN->addIncoming(RHSCond, RHSBlock);
+ PN->addIncoming(RHSCond, ContIncoming);
CGF.MCDCLogOpStack.pop_back();
// If the top of the logical operator nest, update the MCDC bitmap.
@@ -5060,6 +5087,9 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
}
Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
+ auto HasLHSSkip = CGF.getIsCounterPair(E);
+ auto HasRHSSkip = CGF.getIsCounterPair(E->getRHS());
+
// Perform vector logical or on comparisons with zero vectors.
if (E->getType()->isVectorType()) {
CGF.incrementProfileCounter(E);
@@ -5088,7 +5118,7 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
bool LHSCondVal;
if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
if (!LHSCondVal) { // If we have 0 || X, just emit X.
- CGF.incrementProfileCounter(E);
+ CGF.incrementProfileCounter(false, E);
// If the top of the logical operator nest, reset the MCDC temp to 0.
if (CGF.MCDCLogOpStack.empty())
@@ -5106,11 +5136,17 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
CodeGenFunction::isInstrumentedCondition(E->getRHS())) {
CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
llvm::BasicBlock *FBlock = CGF.createBasicBlock("lor.end");
+ llvm::BasicBlock *RHSSkip =
+ (HasRHSSkip.second ? CGF.createBasicBlock("lor.rhsskip") : FBlock);
llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("lor.rhscnt");
- Builder.CreateCondBr(RHSCond, FBlock, RHSBlockCnt);
+ Builder.CreateCondBr(RHSCond, RHSSkip, RHSBlockCnt);
CGF.EmitBlock(RHSBlockCnt);
- CGF.incrementProfileCounter(E->getRHS());
+ CGF.incrementProfileCounter(false, E->getRHS());
CGF.EmitBranch(FBlock);
+ if (HasRHSSkip.second) {
+ CGF.EmitBlock(RHSSkip);
+ CGF.incrementProfileCounter(true, E->getRHS());
+ }
CGF.EmitBlock(FBlock);
}
@@ -5138,14 +5174,22 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
+ llvm::BasicBlock *LHSTrueBlock =
+ (HasLHSSkip.second ? CGF.createBasicBlock("lor.lhsskip") : ContBlock);
CodeGenFunction::ConditionalEvaluation eval(CGF);
// Branch on the LHS first. If it is true, go to the success (cont) block.
- CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock,
+ CGF.EmitBranchOnBoolExpr(E->getLHS(), LHSTrueBlock, RHSBlock,
CGF.getCurrentProfileCount() -
CGF.getProfileCount(E->getRHS()));
+ if (HasLHSSkip.second) {
+ CGF.EmitBlock(LHSTrueBlock);
+ CGF.incrementProfileCounter(true, E);
+ CGF.EmitBranch(ContBlock);
+ }
+
// Any edges into the ContBlock are now from an (indeterminate number of)
// edges from this first condition. All of these values will be true. Start
// setting up the PHI node in the Cont Block for this.
@@ -5159,7 +5203,7 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
// Emit the RHS condition as a bool value.
CGF.EmitBlock(RHSBlock);
- CGF.incrementProfileCounter(E);
+ CGF.incrementProfileCounter(false, E);
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
eval.end(CGF);
@@ -5170,21 +5214,30 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
// If we're generating for profiling or coverage, generate a branch on the
// RHS to a block that increments the RHS true counter needed to track branch
// condition coverage.
+ llvm::BasicBlock *ContIncoming = RHSBlock;
if (InstrumentRegions &&
CodeGenFunction::isInstrumentedCondition(E->getRHS())) {
CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("lor.rhscnt");
- Builder.CreateCondBr(RHSCond, ContBlock, RHSBlockCnt);
+ llvm::BasicBlock *RHSTrueBlock =
+ (HasRHSSkip.second ? CGF.createBasicBlock("lor.rhsskip") : ContBlock);
+ Builder.CreateCondBr(RHSCond, RHSTrueBlock, RHSBlockCnt);
CGF.EmitBlock(RHSBlockCnt);
- CGF.incrementProfileCounter(E->getRHS());
+ CGF.incrementProfileCounter(false, E->getRHS());
CGF.EmitBranch(ContBlock);
PN->addIncoming(RHSCond, RHSBlockCnt);
+ if (HasRHSSkip.second) {
+ CGF.EmitBlock(RHSTrueBlock);
+ CGF.incrementProfileCounter(true, E->getRHS());
+ CGF.EmitBranch(ContBlock);
+ ContIncoming = RHSTrueBlock;
+ }
}
// Emit an unconditional branch from this block to ContBlock. Insert an entry
// into the phi node for the edge with the value of RHSCond.
CGF.EmitBlock(ContBlock);
- PN->addIncoming(RHSCond, RHSBlock);
+ PN->addIncoming(RHSCond, ContIncoming);
CGF.MCDCLogOpStack.pop_back();
// If the top of the logical operator nest, update the MCDC bitmap.
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index e28048d0ec4d90..ee42560b8870dc 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -43,10 +43,6 @@ using namespace CodeGen;
// Statement Emission
//===----------------------------------------------------------------------===//
-namespace llvm {
-extern cl::opt<bool> EnableSingleByteCoverage;
-} // namespace llvm
-
void CodeGenFunction::EmitStopPoint(const Stmt *S) {
if (CGDebugInfo *DI = getDebugInfo()) {
SourceLocation Loc;
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index fcd225b0dc7f45..7f3f4bdbdbbc1d 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1762,6 +1762,7 @@ void CodeGenFunction::EmitBranchToCounterBlock(
return EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount, LH);
const Stmt *CntrStmt = (CntrIdx ? CntrIdx : Cond);
+ auto HasSkip = getIsCounterPair(CntrStmt);
llvm::BasicBlock *ThenBlock = nullptr;
llvm::BasicBlock *ElseBlock = nullptr;
@@ -1770,6 +1771,10 @@ void CodeGenFunction::EmitBranchToCounterBlock(
// Create the block we'll use to increment the appropriate counter.
llvm::BasicBlock *CounterIncrBlock = createBasicBlock("lop.rhscnt");
+ llvm::BasicBlock *SkipIncrBlock =
+ (HasSkip.second ? createBasicBlock("lop.rhsskip") : nullptr);
+ llvm::BasicBlock *SkipNextBlock = nullptr;
+
// Set block pointers according to Logical-AND (BO_LAnd) semantics. This
// means we need to evaluate the condition and increment the counter on TRUE:
//
@@ -1783,8 +1788,9 @@ void CodeGenFunction::EmitBranchToCounterBlock(
// goto TrueBlock;
if (LOp == BO_LAnd) {
+ SkipNextBlock = FalseBlock;
ThenBlock = CounterIncrBlock;
- ElseBlock = FalseBlock;
+ ElseBlock = (SkipIncrBlock ? SkipIncrBlock : SkipNextBlock);
NextBlock = TrueBlock;
}
@@ -1801,7 +1807,8 @@ void CodeGenFunction::EmitBranchToCounterBlock(
// goto FalseBlock;
else if (LOp == BO_LOr) {
- ThenBlock = TrueBlock;
+ SkipNextBlock = TrueBlock;
+ ThenBlock = (SkipIncrBlock ? SkipIncrBlock : SkipNextBlock);
ElseBlock = CounterIncrBlock;
NextBlock = FalseBlock;
} else {
@@ -1811,11 +1818,17 @@ void CodeGenFunction::EmitBranchToCounterBlock(
// Emit Branch based on condition.
EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, TrueCount, LH);
+ if (SkipIncrBlock) {
+ EmitBlock(SkipIncrBlock);
+ incrementProfileCounter(true, CntrStmt);
+ EmitBranch(SkipNextBlock);
+ }
+
// Emit the block containing the counter increment(s).
EmitBlock(CounterIncrBlock);
// Increment corresponding counter; if index not provided, use Cond as index.
- incrementProfileCounter(CntrStmt);
+ incrementProfileCounter(false, CntrStmt);
// Go to the next block.
EmitBranch(NextBlock);
@@ -1834,6 +1847,8 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
Cond = Cond->IgnoreParens();
if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
+ auto HasSkip = getIsCounterPair(CondBOp);
+
// Handle X && Y in a condition.
if (CondBOp->getOpcode() == BO_LAnd) {
MCDCLogOpStack.push_back(CondBOp);
@@ -1865,6 +1880,8 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
// Emit the LHS as a conditional. If the LHS conditional is false, we
// want to jump to the FalseBlock.
llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
+ llvm::BasicBlock *LHSFalse =
+ (HasSkip.second ? createBasicBlock("land.lhsskip") : FalseBlock);
// The counter tells us how often we evaluate RHS, and all of TrueCount
// can be propagated to that branch.
uint64_t RHSCount = getProfileCount(CondBOp->getRHS());
@@ -1875,12 +1892,17 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
// Propagate the likelihood attribute like __builtin_expect
// __builtin_expect(X && Y, 1) -> X and Y are likely
// __builtin_expect(X && Y, 0) -> only Y is unlikely
- EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock, RHSCount,
+ EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, LHSFalse, RHSCount,
LH == Stmt::LH_Unlikely ? Stmt::LH_None : LH);
+ if (HasSkip.second) {
+ EmitBlock(LHSFalse);
+ incrementProfileCounter(true, CondBOp);
+ EmitBranch(FalseBlock);
+ }
EmitBlock(LHSTrue);
}
- incrementProfileCounter(CondBOp);
+ incrementProfileCounter(false, CondBOp);
setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
// Any temporaries created here are conditional.
@@ -1920,6 +1942,8 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
}
// Emit the LHS as a conditional. If the LHS conditional is true, we
// want to jump to the TrueBlock.
+ llvm::BasicBlock *LHSTrue =
+ (HasSkip.second ? createBasicBlock("lor.lhsskip") : TrueBlock);
llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
// We have the count for entry to the RHS and for the whole expression
// being true, so we can divy up True count between the short circuit and
@@ -1934,12 +1958,17 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
// __builtin_expect(X || Y, 1) -> only Y is likely
// __builtin_expect(X || Y, 0) -> both X and Y are unlikely
ApplyDebugLocation DL(*this, Cond);
- EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse, LHSCount,
+ EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, LHSFalse, LHSCount,
LH == Stmt::LH_Likely ? Stmt::LH_None : LH);
+ if (HasSkip.second) {
+ EmitBlock(LHSTrue);
+ incrementProfileCounter(true, CondBOp);
+ EmitBranch(TrueBlock);
+ }
EmitBlock(LHSFalse);
}
- incrementProfileCounter(CondBOp);
+ incrementProfileCounter(false, CondBOp);
setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
// Any temporaries created here are conditional.
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 6abf0b333b246b..9179410c706217 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -2185,9 +2185,6 @@ struct CounterCoverageMappingBuilder
extendRegion(E->getRHS());
propagateCounts(getRegionCounter(E), E->getRHS());
- if (llvm::EnableSingleByteCoverage)
- return;
-
// Track RHS True/False Decision.
const auto DecisionRHS = MCDCBuilder.back();
@@ -2246,9 +2243,6 @@ struct CounterCoverageMappingBuilder
extendRegion(E->getRHS());
propagateCounts(getRegionCounter(E), E->getRHS());
- if (llvm::EnableSingleByteCoverage)
- return;
-
// Track RHS True/False Decision.
const auto DecisionRHS = MCDCBuilder.back();
>From 03cfce188cb45adbe78f7e77c2fdd244650f5a3c Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 23 Oct 2024 14:39:35 +0000
Subject: [PATCH 18/24] CGF::markStmtAsUsed
---
clang/lib/CodeGen/CodeGenFunction.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 89ac3b342d0a7c..fcad1cbcae5e3d 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -1624,6 +1624,9 @@ class CodeGenFunction : public CodeGenTypeCache {
return PGO.getIsCounterPair(S);
}
+ void markStmtAsUsed(bool Skipped, const Stmt *S) {
+ PGO.markStmtAsUsed(Skipped, S);
+ }
void markStmtMaybeUsed(const Stmt *S) { PGO.markStmtMaybeUsed(S); }
/// Increment the profiler's counter for the given statement by \p StepV.
>From afc8481f7cf20da7de4e95a60bf3ccdb04bd08f3 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 23 Oct 2024 14:39:35 +0000
Subject: [PATCH 19/24] CGF.markStmtMaybeUsed for binop
---
clang/lib/CodeGen/CGExprScalar.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 74e93f889f4261..9e8533ca90a5cd 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4970,7 +4970,8 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
CGF.incrementProfileCounter(E->getRHS());
CGF.EmitBranch(FBlock);
CGF.EmitBlock(FBlock);
- }
+ } else
+ CGF.markStmtMaybeUsed(E->getRHS());
CGF.MCDCLogOpStack.pop_back();
// If the top of the logical operator nest, update the MCDC bitmap.
@@ -5112,7 +5113,8 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
CGF.incrementProfileCounter(E->getRHS());
CGF.EmitBranch(FBlock);
CGF.EmitBlock(FBlock);
- }
+ } else
+ CGF.markStmtMaybeUsed(E->getRHS());
CGF.MCDCLogOpStack.pop_back();
// If the top of the logical operator nest, update the MCDC bitmap.
>From ad997c2f539ab2622e61c106bb3d646930777712 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 23 Oct 2024 14:39:35 +0000
Subject: [PATCH 20/24] Fix cases when LHS is skipped
---
clang/lib/CodeGen/CGExprScalar.cpp | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index de85d4ad63833e..fd67622fe81548 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4949,7 +4949,7 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
bool LHSCondVal;
if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
if (LHSCondVal) { // If we have 1 && X, just emit X.
- CGF.incrementProfileCounter(E);
+ CGF.incrementProfileCounter(false, E, true);
// If the top of the logical operator nest, reset the MCDC temp to 0.
if (CGF.MCDCLogOpStack.empty())
@@ -4993,7 +4993,12 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
// 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
if (!CGF.ContainsLabel(E->getRHS())) {
+ CGF.markStmtAsUsed(false, E);
+ if (HasLHSSkip.second)
+ CGF.incrementProfileCounter(true, E);
+
CGF.markStmtMaybeUsed(E->getRHS());
+
return llvm::Constant::getNullValue(ResTy);
}
}
@@ -5119,7 +5124,7 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
bool LHSCondVal;
if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
if (!LHSCondVal) { // If we have 0 || X, just emit X.
- CGF.incrementProfileCounter(false, E);
+ CGF.incrementProfileCounter(false, E, true);
// If the top of the logical operator nest, reset the MCDC temp to 0.
if (CGF.MCDCLogOpStack.empty())
@@ -5163,7 +5168,12 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
// 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
if (!CGF.ContainsLabel(E->getRHS())) {
+ CGF.markStmtAsUsed(false, E);
+ if (HasLHSSkip.second)
+ CGF.incrementProfileCounter(true, E);
+
CGF.markStmtMaybeUsed(E->getRHS());
+
return llvm::ConstantInt::get(ResTy, 1);
}
}
>From ab84f17fc181cb4b38693dc6ea80ac44f44bf990 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 27 Oct 2024 20:28:26 +0900
Subject: [PATCH 21/24] Introduce skeleton getSwitchImplicitDefaultCounter()
---
clang/lib/CodeGen/CoverageMappingGen.cpp | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 1782434bdb9aa6..532f6e8ba18201 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -947,6 +947,11 @@ struct CounterCoverageMappingBuilder
return {ExecCnt, Builder.subtract(ParentCnt, ExecCnt)};
}
+ Counter getSwitchImplicitDefaultCounter(const Stmt *Cond, Counter ParentCount,
+ Counter CaseCountSum) {
+ return Builder.subtract(ParentCount, CaseCountSum);
+ }
+
bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
if (OutCount == ParentCount)
return true;
@@ -1903,7 +1908,8 @@ struct CounterCoverageMappingBuilder
// the hidden branch, which will be added later by the CodeGen. This region
// will be associated with the switch statement's condition.
if (!HasDefaultCase) {
- Counter DefaultCount = subtractCounters(ParentCount, CaseCountSum);
+ Counter DefaultCount = getSwitchImplicitDefaultCounter(
+ S->getCond(), ParentCount, CaseCountSum);
createBranchRegion(S->getCond(), Counter::getZero(), DefaultCount);
}
}
>From a4608854e1b727817db3cc01234f97e78285f8c7 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Sun, 27 Oct 2024 20:40:44 +0900
Subject: [PATCH 22/24] Update getSwitchImplicitDefaultCounter
---
clang/lib/CodeGen/CoverageMappingGen.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index d4a1b2613eab92..cb861e61d32727 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -960,7 +960,10 @@ struct CounterCoverageMappingBuilder
Counter getSwitchImplicitDefaultCounter(const Stmt *Cond, Counter ParentCount,
Counter CaseCountSum) {
- return Builder.subtract(ParentCount, CaseCountSum);
+ return (
+ llvm::EnableSingleByteCoverage
+ ? Counter::getCounter(CounterMap[Cond].second = NextCounterNum++)
+ : Builder.subtract(ParentCount, CaseCountSum));
}
bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
>From 02853943407a5c550554e4920586b8724dd63a76 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Mon, 28 Oct 2024 00:55:49 +0900
Subject: [PATCH 23/24] Don't allocate second if SkipExpr isn't Expr.
---
clang/lib/CodeGen/CoverageMappingGen.cpp | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index cb861e61d32727..8584f58548eae0 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -946,8 +946,12 @@ struct CounterCoverageMappingBuilder
auto ExecCnt = Counter::getCounter(TheMap.first);
auto SkipExpr = Builder.subtract(ParentCnt, ExecCnt);
- if (!llvm::EnableSingleByteCoverage)
+ if (!llvm::EnableSingleByteCoverage || !SkipExpr.isExpression()) {
+ assert(
+ !TheMap.getIsCounterPair().second &&
+ "SkipCnt shouldn't be allocated but refer to an existing counter.");
return {ExecCnt, SkipExpr};
+ }
// Assign second if second is not assigned yet.
if (!TheMap.getIsCounterPair().second)
>From 2842382fd41b6175502b72ae651829bbeaefa5b2 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Mon, 28 Oct 2024 02:26:36 +0900
Subject: [PATCH 24/24] update
---
clang/test/CoverageMapping/single-byte-counters.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/test/CoverageMapping/single-byte-counters.cpp b/clang/test/CoverageMapping/single-byte-counters.cpp
index 401b5d7dd8b84d..74aab4fc31f3df 100644
--- a/clang/test/CoverageMapping/single-byte-counters.cpp
+++ b/clang/test/CoverageMapping/single-byte-counters.cpp
@@ -58,7 +58,7 @@ int testWhile() { // CHECK-NEXT: File 0, [[@LINE]]:17 -> [[@LINE+12]]:2 =
int i = 0;
int sum = 0;
while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = ([[C40]] + [[C4T:#1]])
- // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:10 -> [[@LINE-1]]:16 = [[C4T]], [[C4F:#2]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:10 -> [[@LINE-1]]:16 = [[C4T]], [[C4F:#0]]
// CHECK-NEXT: Gap,File 0, [[@LINE-2]]:17 -> [[@LINE-2]]:18 = [[C4T]]
// CHECK-NEXT: File 0, [[@LINE-3]]:18 -> [[@LINE+3]]:4 = [[C4T]]
sum += i;
@@ -92,7 +92,7 @@ int testFor() { // CHECK-NEXT: File 0, [[@LINE]]:15 -> [[@LINE+13]]:2 = [[C60:#0
int i;
int sum = 0;
// CHECK-NEXT: File 0, [[@LINE+3]]:19 -> [[@LINE+3]]:25 = ([[C60]] + [[C6B:#1]])
- // CHECK-NEXT: Branch,File 0, [[@LINE+2]]:19 -> [[@LINE+2]]:25 = [[C6B]], [[C6E:#2]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE+2]]:19 -> [[@LINE+2]]:25 = [[C6B]], [[C6E:#0]]
// CHECK-NEXT: File 0, [[@LINE+1]]:27 -> [[@LINE+1]]:30 = [[C6B]]
for (int i = 0; i < 10; i++) {
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:31 -> [[@LINE-1]]:32 = [[C6B]]
@@ -108,7 +108,7 @@ int testForRange() { // CHECK-NEXT: File 0, [[@LINE]]:20 -> [[@LINE+12]]:2 =
int sum = 0;
int array[] = {1, 2, 3, 4, 5};
- // CHECK-NEXT: Branch,File 0, [[@LINE+1]]:20 -> [[@LINE+1]]:21 = [[C7B:#1]], [[C7E:#2]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE+1]]:20 -> [[@LINE+1]]:21 = [[C7B:#1]], [[C7E:#0]]
for (int element : array) {
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:28 -> [[@LINE-1]]:29 = [[C7B]]
// CHECK-NEXT: File 0, [[@LINE-2]]:29 -> [[@LINE+2]]:4 = [[C7B]]
@@ -126,7 +126,7 @@ int testDo() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+10]]:2 =
sum += i;
i++;
} while (i < 5); // CHECK-NEXT: File 0, [[@LINE]]:12 -> [[@LINE]]:17 = ([[C80]] + [[C8B]])
- // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:12 -> [[@LINE-1]]:17 = [[C8B]], [[C8E:#2]]
+ // CHECK-NEXT: Branch,File 0, [[@LINE-1]]:12 -> [[@LINE-1]]:17 = [[C8B]], [[C8E:#0]]
return sum; // #0
}
More information about the llvm-commits
mailing list