Skip to content

[Coverage][Single] Enable Branch coverage for IfStmt #113111

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 54 commits into
base: users/chapuni/cov/single/nextcount
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
178b57c
[Coverage] Move SingleByteCoverage out of CountedRegion
chapuni Oct 2, 2024
aacb50d
[Coverage] Make SingleByteCoverage work consistent to merging
chapuni Oct 2, 2024
b9bbc7c
Rework. (Also reverts "[Coverage] Move SingleByteCoverage out of Cou…
chapuni Oct 5, 2024
52f072e
clang/test/CoverageMapping/single-byte-counters.cpp: Rewrite counter …
chapuni Oct 17, 2024
78e33ba
Merge branches 'users/chapuni/cov/single/test' and 'users/chapuni/cov…
chapuni Oct 21, 2024
3ea6383
[Coverage][Single] Enable Branch coverage for IfStmt
chapuni Oct 20, 2024
bc708d5
Merge branch 'users/chapuni/cov/single/nextcount' into users/chapuni/…
chapuni Oct 27, 2024
97a4a8f
test/llvm-cov: Transform %.c* tests to {%.test, Inputs/%.c*}
chapuni Nov 20, 2024
c50c492
Introduce test/llvm-cov/Inputs/yaml.makefile for convenience.
chapuni Nov 20, 2024
d7c5b44
Add tests for SingleByteCoverage
chapuni Nov 20, 2024
6675226
Merge branch 'users/chapuni/cov/single/test' into users/chapuni/cov/s…
chapuni Nov 20, 2024
5fc3408
Fix a test to fix linecount=1
chapuni Nov 20, 2024
d086491
Merge branch 'users/chapuni/cov/single/merge' into users/chapuni/cov/…
chapuni Nov 20, 2024
2364252
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/s…
chapuni Nov 20, 2024
b2f7fdf
Update single tests
chapuni Nov 20, 2024
eb7fff9
threads.c: Fixup on the clean testdir
chapuni Nov 20, 2024
7543095
Rename threads.c to threads.test since it is no longer C source file.
chapuni Nov 21, 2024
00ac90d
llvm/test/tools/llvm-cov/Inputs: Avoid wildcards `rm -rf %t*.dir`
chapuni Nov 21, 2024
fcb3ee8
Merge branch 'main' into users/chapuni/cov/single/test
chapuni Dec 21, 2024
5fa862a
Use `[[#min(C,n)]]` for tests
chapuni Dec 21, 2024
805e9a9
llvm-cov: Introduce `--binary-counters`
chapuni Dec 21, 2024
68d7b3b
Merge branch 'users/chapuni/cov/single/test' into users/chapuni/cov/s…
chapuni Dec 21, 2024
24457a7
Update tests
chapuni Dec 21, 2024
805dbd9
Merge branches 'users/chapuni/cov/single/merge' and 'users/chapuni/co…
chapuni Dec 21, 2024
f96b435
New SingleByteCoverage
chapuni Dec 21, 2024
2968ea6
Merge branch 'users/chapuni/cov/single/refactor' into users/chapuni/c…
chapuni Dec 21, 2024
cb3f959
Merge branch 'users/chapuni/cov/single/nextcount' into users/chapuni/…
chapuni Dec 21, 2024
f178bf6
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/s…
chapuni Dec 21, 2024
47550d1
threads.c => threads.test (following #113114)
chapuni Dec 22, 2024
42c0c26
clang/test: Add tests that we missed while I was updating.
chapuni Dec 23, 2024
c0a93e2
Merge branches 'users/chapuni/cov/single/test' and 'users/chapuni/cov…
chapuni Dec 23, 2024
747478e
Update the test
chapuni Dec 23, 2024
b992ae4
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/s…
chapuni Dec 23, 2024
f4dc4eb
s/Count1/BinaryCount/
chapuni Dec 23, 2024
822620b
Update desc
chapuni Dec 23, 2024
658bd48
Merge branch 'main' into users/chapuni/cov/binary
chapuni Dec 24, 2024
0780993
Merge branch 'users/chapuni/cov/binary' into users/chapuni/cov/single…
chapuni Dec 24, 2024
5dfe3e7
Merge branch 'users/chapuni/cov/single/nextcount' into users/chapuni/…
chapuni Dec 24, 2024
1adf497
Eliminate (rewind) "refactor" changes
chapuni Dec 24, 2024
8169e09
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/s…
chapuni Dec 24, 2024
dfc99ba
Fix wrong merge resolutions
chapuni Dec 24, 2024
f3c9593
Reorganize CoverageMapping::SingleByteCoverage
chapuni Dec 24, 2024
3780e07
Prune commented-out line
chapuni Dec 24, 2024
894c383
Merge branch 'main' into users/chapuni/cov/single/refactor
chapuni Dec 27, 2024
7c26a2a
Merge branch 'main' into users/chapuni/cov/single/refactor
chapuni Dec 27, 2024
e11930a
Merge branch 'users/chapuni/cov/single/refactor' into users/chapuni/c…
chapuni Dec 27, 2024
ad7b753
Merge branch 'users/chapuni/cov/single/nextcount' into users/chapuni/…
chapuni Dec 27, 2024
3069477
Merge branch 'main' into users/chapuni/cov/single/base
chapuni Dec 28, 2024
e19c32e
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/s…
chapuni Dec 28, 2024
e7fd5cd
Merge branch 'users/chapuni/cov/single/nextcount' into users/chapuni/…
chapuni Jan 8, 2025
ed54cbb
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/s…
chapuni Jan 8, 2025
3c62522
Dissolve assert
chapuni Jan 8, 2025
bdcf47e
Merge branch 'users/chapuni/cov/single/nextcount' into users/chapuni/…
chapuni Jan 9, 2025
0e1a753
Merge branch 'users/chapuni/cov/single/base' into users/chapuni/cov/s…
chapuni Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,8 @@ 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 ? UseExecPath : UseSkipPath, &S,
/*UseBoth=*/true);
if (Executed) {
RunCleanupsScope ExecutedScope(*this);
EmitStmt(Executed);
Expand All @@ -881,14 +881,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.
Expand Down Expand Up @@ -921,10 +921,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {

// Emit the 'then' code.
EmitBlock(ThenBlock);
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(S.getThen());
else
incrementProfileCounter(&S);
incrementProfileCounter(UseExecPath, &S);
{
RunCleanupsScope ThenScope(*this);
EmitStmt(S.getThen());
Expand All @@ -938,9 +935,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(UseSkipPath, &S);
{
RunCleanupsScope ElseScope(*this);
EmitStmt(Else);
Expand All @@ -950,15 +947,14 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
auto NL = ApplyDebugLocation::CreateEmpty(*this);
EmitBranch(ContBlock);
}
} else if (HasSkip.second) {
EmitBlock(ElseBlock);
incrementProfileCounter(UseSkipPath, &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,
Expand Down
12 changes: 0 additions & 12 deletions clang/lib/CodeGen/CodeGenPGO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,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);

Expand Down
26 changes: 6 additions & 20 deletions clang/lib/CodeGen/CoverageMappingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,11 +966,8 @@ struct CounterCoverageMappingBuilder

// The old behavior of SingleByte is unaware of Branches.
// Will be pruned after the migration of SingleByte.
if (llvm::EnableSingleByteCoverage) {
assert(SkipCntForOld &&
"SingleByte must provide SkipCntForOld as a fake Skipped count.");
if (llvm::EnableSingleByteCoverage && SkipCntForOld)
return {ExecCnt, *SkipCntForOld};
}

BranchCounterPair Counters = {ExecCnt,
Builder.subtract(ParentCnt, ExecCnt)};
Expand Down Expand Up @@ -2093,12 +2090,7 @@ struct CounterCoverageMappingBuilder
extendRegion(S->getCond());

Counter ParentCount = getRegion().getCounter();
auto [ThenCount, ElseCount] =
(llvm::EnableSingleByteCoverage
? BranchCounterPair{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.
Expand All @@ -2123,26 +2115,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) {
Expand Down
48 changes: 24 additions & 24 deletions clang/test/CoverageMapping/single-byte-counters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@
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-2]]: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+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-2]]: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+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-2]]: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: testIfBothReturn
int testIfBothReturn(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-2]]: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]]
return 42; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:14 = [[C2T]]
else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:15 -> [[@LINE+1]]:5 = [[C2F:#2]]
else // CHECK-NEXT: Gap,File 0, [[@LINE-1]]:15 -> [[@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 = #3
return -1; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:12 = #3
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:14 -> [[@LINE+1]]:3 = 0
return -1; // CHECK-NEXT: File 0, [[@LINE]]:3 -> [[@LINE]]:12 = 0
}

// CHECK-NEXT: testSwitch
Expand Down Expand Up @@ -91,21 +91,21 @@ int testWhile() { // CHECK-NEXT: File 0, [[@LINE]]:17 -> [[@LINE+12]]:2 =
int testContinueBreak() { // CHECK-NEXT: File 0, [[@LINE]]:25 -> [[@LINE+23]]:2 = #0
int i = 0;
int sum = 0;
while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = #1
while (i < 10) { // CHECK-NEXT: File 0, [[@LINE]]:10 -> [[@LINE]]:16 = [[C5C:#1]]

// CHECK-NEXT: Gap,File 0, [[@LINE-2]]:17 -> [[@LINE-2]]:18 = [[C5B:#2]]
// CHECK-NEXT: File 0, [[@LINE-3]]:18 -> [[@LINE+14]]:4 = [[C5B]]
if (i == 4) // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:15 = [[C5B]]

// CHECK-NEXT: Gap,File 0, [[@LINE-2]]:16 -> [[@LINE+1]]:7 = [[C5T:#4]]
// CHECK-NEXT: Branch,File 0, [[@LINE-1]]:9 -> [[@LINE-1]]:15 = [[C5T:#4]], [[C5F:#6]]
// 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+2]]:5 = [[C5F:#5]]
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:16 -> [[@LINE+2]]:5 = [[C5F]]
// CHECK-NEXT: File 0, [[@LINE+1]]:5 -> [[@LINE+8]]:4 = [[C5F]]
if (i == 5) // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:15 = [[C5F]]

// CHECK-NEXT: Gap,File 0, [[@LINE-2]]:16 -> [[@LINE+1]]:7 = [[C5T1:#6]]
// CHECK-NEXT: Branch,File 0, [[@LINE-1]]:9 -> [[@LINE-1]]:15 = [[C5T1:#5]], [[C5F1:#7]]
// CHECK-NEXT: Gap,File 0, [[@LINE-2]]:16 -> [[@LINE+1]]:7 = [[C5T1]]
break; // CHECK-NEXT: File 0, [[@LINE]]:7 -> [[@LINE]]:12 = [[C5T1]]
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:13 -> [[@LINE+1]]:5 = [[C5F1:#7]]
// CHECK-NEXT: Gap,File 0, [[@LINE-1]]:13 -> [[@LINE+1]]:5 = [[C5F1]]
sum += i; // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE+2]]:4 = [[C5F1]]
i++;
}
Expand Down
41 changes: 19 additions & 22 deletions llvm/test/tools/llvm-cov/Inputs/branch-c-general-single.proftext
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ big_switch
1
1
1
1
1
1
1
1
0
0
0
0
0

boolean_operators
# Func Hash:
Expand Down Expand Up @@ -116,18 +116,14 @@ conditionals
# Func Hash:
4904767535850050386
# Num Counters:
25
23
# Counter Values:
1
1
1
1
1
1
0
1
1
1
1
1
1
Expand All @@ -139,7 +135,9 @@ conditionals
1
1
1
0
1
0
1
1
1
Expand All @@ -165,7 +163,7 @@ early_exits
# Func Hash:
2880354649761471549
# Num Counters:
20
19
# Counter Values:
1
0
Expand All @@ -177,15 +175,14 @@ early_exits
1
1
1
1
1
1
0
1
0
1
1
1
0
1
0

jumps
Expand All @@ -200,11 +197,9 @@ jumps
1
0
0
0
1
0
1
1
0
1
1
Expand All @@ -214,8 +209,6 @@ jumps
1
1
1
1
1
0
1
1
Expand All @@ -228,10 +221,14 @@ jumps
1
1
0
1
1
0
1
1
1
0
1

main
# Func Hash:
Expand Down Expand Up @@ -281,17 +278,17 @@ switches
1
1
1
1
1
1
1
0
1
1
1
1
0
1
1
0
0
0
1
0

Loading
Loading