Skip to content

Commit 50254b2

Browse files
gulfemsavrunluismarques
authored andcommitted
[InstrProf] Single byte counters in coverage (llvm#75425)
This patch inserts 1-byte counters instead of an 8-byte counters into llvm profiles for source-based code coverage. The origial idea was proposed as block-cov for PGO, and this patch repurposes that idea for coverage: https://groups.google.com/g/llvm-dev/c/r03Z6JoN7d4 The current 8-byte counters mechanism add counters to minimal regions, and infer the counters in the remaining regions via adding or subtracting counters. For example, it infers the counter in the if.else region by subtracting the counters between if.entry and if.then regions in an if statement. Whenever there is a control-flow merge, it adds the counters from all the incoming regions. However, we are not going to be able to infer counters by subtracting two execution counts when using single-byte counters. Therefore, this patch conservatively inserts additional counters for the cases where we need to add or subtract counters. RFC: https: //discourse.llvm.org/t/rfc-single-byte-counters-for-source-based-code-coverage/75685 (cherry picked from commit 23f895f) Change-Id: Ia949464dee61ad6d6a5364b53b7783be1437c7a6 Signed-off-by: Yi-Hsuan Deng <yhdeng@google.com>
1 parent d213f6b commit 50254b2

File tree

15 files changed

+620
-90
lines changed

15 files changed

+620
-90
lines changed

clang/lib/CodeGen/CGExprAgg.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ using namespace CodeGen;
3333
// Aggregate Expression Emitter
3434
//===----------------------------------------------------------------------===//
3535

36+
namespace llvm {
37+
extern cl::opt<bool> EnableSingleByteCoverage;
38+
} // namespace llvm
39+
3640
namespace {
3741
class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
3842
CodeGenFunction &CGF;
@@ -1272,7 +1276,10 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
12721276

12731277
eval.begin(CGF);
12741278
CGF.EmitBlock(LHSBlock);
1275-
CGF.incrementProfileCounter(E);
1279+
if (llvm::EnableSingleByteCoverage)
1280+
CGF.incrementProfileCounter(E->getTrueExpr());
1281+
else
1282+
CGF.incrementProfileCounter(E);
12761283
Visit(E->getTrueExpr());
12771284
eval.end(CGF);
12781285

@@ -1287,6 +1294,8 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
12871294

12881295
eval.begin(CGF);
12891296
CGF.EmitBlock(RHSBlock);
1297+
if (llvm::EnableSingleByteCoverage)
1298+
CGF.incrementProfileCounter(E->getFalseExpr());
12901299
Visit(E->getFalseExpr());
12911300
eval.end(CGF);
12921301

@@ -1295,6 +1304,8 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
12951304
E->getType());
12961305

12971306
CGF.EmitBlock(ContBlock);
1307+
if (llvm::EnableSingleByteCoverage)
1308+
CGF.incrementProfileCounter(E);
12981309
}
12991310

13001311
void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {

clang/lib/CodeGen/CGExprComplex.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ using namespace CodeGen;
2828
// Complex Expression Emitter
2929
//===----------------------------------------------------------------------===//
3030

31+
namespace llvm {
32+
extern cl::opt<bool> EnableSingleByteCoverage;
33+
} // namespace llvm
34+
3135
typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
3236

3337
/// Return the complex type that we are meant to emit.
@@ -1214,17 +1218,25 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
12141218

12151219
eval.begin(CGF);
12161220
CGF.EmitBlock(LHSBlock);
1217-
CGF.incrementProfileCounter(E);
1221+
if (llvm::EnableSingleByteCoverage)
1222+
CGF.incrementProfileCounter(E->getTrueExpr());
1223+
else
1224+
CGF.incrementProfileCounter(E);
1225+
12181226
ComplexPairTy LHS = Visit(E->getTrueExpr());
12191227
LHSBlock = Builder.GetInsertBlock();
12201228
CGF.EmitBranch(ContBlock);
12211229
eval.end(CGF);
12221230

12231231
eval.begin(CGF);
12241232
CGF.EmitBlock(RHSBlock);
1233+
if (llvm::EnableSingleByteCoverage)
1234+
CGF.incrementProfileCounter(E->getFalseExpr());
12251235
ComplexPairTy RHS = Visit(E->getFalseExpr());
12261236
RHSBlock = Builder.GetInsertBlock();
12271237
CGF.EmitBlock(ContBlock);
1238+
if (llvm::EnableSingleByteCoverage)
1239+
CGF.incrementProfileCounter(E);
12281240
eval.end(CGF);
12291241

12301242
// Create a PHI node for the real part.

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ using llvm::Value;
5252
// Scalar Expression Emitter
5353
//===----------------------------------------------------------------------===//
5454

55+
namespace llvm {
56+
extern cl::opt<bool> EnableSingleByteCoverage;
57+
} // namespace llvm
58+
5559
namespace {
5660

5761
/// Determine whether the given binary operation may overflow.
@@ -4757,8 +4761,13 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
47574761

47584762
// If the dead side doesn't have labels we need, just emit the Live part.
47594763
if (!CGF.ContainsLabel(dead)) {
4760-
if (CondExprBool)
4764+
if (CondExprBool) {
4765+
if (llvm::EnableSingleByteCoverage) {
4766+
CGF.incrementProfileCounter(lhsExpr);
4767+
CGF.incrementProfileCounter(rhsExpr);
4768+
}
47614769
CGF.incrementProfileCounter(E);
4770+
}
47624771
Value *Result = Visit(live);
47634772

47644773
// If the live part is a throw expression, it acts like it has a void
@@ -4837,7 +4846,12 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
48374846
llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
48384847
llvm::Value *StepV = Builder.CreateZExtOrBitCast(CondV, CGF.Int64Ty);
48394848

4840-
CGF.incrementProfileCounter(E, StepV);
4849+
if (llvm::EnableSingleByteCoverage) {
4850+
CGF.incrementProfileCounter(lhsExpr);
4851+
CGF.incrementProfileCounter(rhsExpr);
4852+
CGF.incrementProfileCounter(E);
4853+
} else
4854+
CGF.incrementProfileCounter(E, StepV);
48414855

48424856
llvm::Value *LHS = Visit(lhsExpr);
48434857
llvm::Value *RHS = Visit(rhsExpr);
@@ -4858,7 +4872,12 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
48584872
CGF.getProfileCount(lhsExpr));
48594873

48604874
CGF.EmitBlock(LHSBlock);
4861-
CGF.incrementProfileCounter(E);
4875+
4876+
if (llvm::EnableSingleByteCoverage)
4877+
CGF.incrementProfileCounter(lhsExpr);
4878+
else
4879+
CGF.incrementProfileCounter(E);
4880+
48624881
eval.begin(CGF);
48634882
Value *LHS = Visit(lhsExpr);
48644883
eval.end(CGF);
@@ -4867,6 +4886,10 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
48674886
Builder.CreateBr(ContBlock);
48684887

48694888
CGF.EmitBlock(RHSBlock);
4889+
4890+
if (llvm::EnableSingleByteCoverage)
4891+
CGF.incrementProfileCounter(rhsExpr);
4892+
48704893
eval.begin(CGF);
48714894
Value *RHS = Visit(rhsExpr);
48724895
eval.end(CGF);
@@ -4884,6 +4907,12 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
48844907
llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond");
48854908
PN->addIncoming(LHS, LHSBlock);
48864909
PN->addIncoming(RHS, RHSBlock);
4910+
4911+
// When single byte coverage mode is enabled, add a counter to continuation
4912+
// block.
4913+
if (llvm::EnableSingleByteCoverage)
4914+
CGF.incrementProfileCounter(E);
4915+
48874916
return PN;
48884917
}
48894918

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ using namespace CodeGen;
4141
// Statement Emission
4242
//===----------------------------------------------------------------------===//
4343

44+
namespace llvm {
45+
extern cl::opt<bool> EnableSingleByteCoverage;
46+
} // namespace llvm
47+
4448
void CodeGenFunction::EmitStopPoint(const Stmt *S) {
4549
if (CGDebugInfo *DI = getDebugInfo()) {
4650
SourceLocation Loc;
@@ -835,7 +839,10 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
835839

836840
// Emit the 'then' code.
837841
EmitBlock(ThenBlock);
838-
incrementProfileCounter(&S);
842+
if (llvm::EnableSingleByteCoverage)
843+
incrementProfileCounter(S.getThen());
844+
else
845+
incrementProfileCounter(&S);
839846
{
840847
RunCleanupsScope ThenScope(*this);
841848
EmitStmt(S.getThen());
@@ -849,6 +856,9 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
849856
auto NL = ApplyDebugLocation::CreateEmpty(*this);
850857
EmitBlock(ElseBlock);
851858
}
859+
// When single byte coverage mode is enabled, add a counter to else block.
860+
if (llvm::EnableSingleByteCoverage)
861+
incrementProfileCounter(Else);
852862
{
853863
RunCleanupsScope ElseScope(*this);
854864
EmitStmt(Else);
@@ -862,6 +872,11 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
862872

863873
// Emit the continuation block for code after the if.
864874
EmitBlock(ContBlock, true);
875+
876+
// When single byte coverage mode is enabled, add a counter to continuation
877+
// block.
878+
if (llvm::EnableSingleByteCoverage)
879+
incrementProfileCounter(&S);
865880
}
866881

867882
void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
@@ -906,6 +921,10 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
906921
SourceLocToDebugLoc(R.getEnd()),
907922
checkIfLoopMustProgress(CondIsConstInt));
908923

924+
// When single byte coverage mode is enabled, add a counter to loop condition.
925+
if (llvm::EnableSingleByteCoverage)
926+
incrementProfileCounter(S.getCond());
927+
909928
// As long as the condition is true, go to the loop body.
910929
llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
911930
if (EmitBoolCondBranch) {
@@ -938,7 +957,11 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
938957
{
939958
RunCleanupsScope BodyScope(*this);
940959
EmitBlock(LoopBody);
941-
incrementProfileCounter(&S);
960+
// When single byte coverage mode is enabled, add a counter to the body.
961+
if (llvm::EnableSingleByteCoverage)
962+
incrementProfileCounter(S.getBody());
963+
else
964+
incrementProfileCounter(&S);
942965
EmitStmt(S.getBody());
943966
}
944967

@@ -960,6 +983,11 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
960983
// a branch, try to erase it.
961984
if (!EmitBoolCondBranch)
962985
SimplifyForwardingBlocks(LoopHeader.getBlock());
986+
987+
// When single byte coverage mode is enabled, add a counter to continuation
988+
// block.
989+
if (llvm::EnableSingleByteCoverage)
990+
incrementProfileCounter(&S);
963991
}
964992

965993
void CodeGenFunction::EmitDoStmt(const DoStmt &S,
@@ -975,13 +1003,19 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
9751003
// Emit the body of the loop.
9761004
llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
9771005

978-
EmitBlockWithFallThrough(LoopBody, &S);
1006+
if (llvm::EnableSingleByteCoverage)
1007+
EmitBlockWithFallThrough(LoopBody, S.getBody());
1008+
else
1009+
EmitBlockWithFallThrough(LoopBody, &S);
9791010
{
9801011
RunCleanupsScope BodyScope(*this);
9811012
EmitStmt(S.getBody());
9821013
}
9831014

9841015
EmitBlock(LoopCond.getBlock());
1016+
// When single byte coverage mode is enabled, add a counter to loop condition.
1017+
if (llvm::EnableSingleByteCoverage)
1018+
incrementProfileCounter(S.getCond());
9851019

9861020
// C99 6.8.5.2: "The evaluation of the controlling expression takes place
9871021
// after each execution of the loop body."
@@ -1022,6 +1056,11 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
10221056
// emitting a branch, try to erase it.
10231057
if (!EmitBoolCondBranch)
10241058
SimplifyForwardingBlocks(LoopCond.getBlock());
1059+
1060+
// When single byte coverage mode is enabled, add a counter to continuation
1061+
// block.
1062+
if (llvm::EnableSingleByteCoverage)
1063+
incrementProfileCounter(&S);
10251064
}
10261065

10271066
void CodeGenFunction::EmitForStmt(const ForStmt &S,
@@ -1080,6 +1119,11 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
10801119
BreakContinueStack.back().ContinueBlock = Continue;
10811120
}
10821121

1122+
// When single byte coverage mode is enabled, add a counter to loop
1123+
// condition.
1124+
if (llvm::EnableSingleByteCoverage)
1125+
incrementProfileCounter(S.getCond());
1126+
10831127
llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
10841128
// If there are any cleanups between here and the loop-exit scope,
10851129
// create a block to stage a loop exit along.
@@ -1110,8 +1154,12 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
11101154
// Treat it as a non-zero constant. Don't even create a new block for the
11111155
// body, just fall into it.
11121156
}
1113-
incrementProfileCounter(&S);
11141157

1158+
// When single byte coverage mode is enabled, add a counter to the body.
1159+
if (llvm::EnableSingleByteCoverage)
1160+
incrementProfileCounter(S.getBody());
1161+
else
1162+
incrementProfileCounter(&S);
11151163
{
11161164
// Create a separate cleanup scope for the body, in case it is not
11171165
// a compound statement.
@@ -1123,6 +1171,8 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
11231171
if (S.getInc()) {
11241172
EmitBlock(Continue.getBlock());
11251173
EmitStmt(S.getInc());
1174+
if (llvm::EnableSingleByteCoverage)
1175+
incrementProfileCounter(S.getInc());
11261176
}
11271177

11281178
BreakContinueStack.pop_back();
@@ -1138,6 +1188,11 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
11381188

11391189
// Emit the fall-through block.
11401190
EmitBlock(LoopExit.getBlock(), true);
1191+
1192+
// When single byte coverage mode is enabled, add a counter to continuation
1193+
// block.
1194+
if (llvm::EnableSingleByteCoverage)
1195+
incrementProfileCounter(&S);
11411196
}
11421197

11431198
void
@@ -1190,7 +1245,10 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
11901245
}
11911246

11921247
EmitBlock(ForBody);
1193-
incrementProfileCounter(&S);
1248+
if (llvm::EnableSingleByteCoverage)
1249+
incrementProfileCounter(S.getBody());
1250+
else
1251+
incrementProfileCounter(&S);
11941252

11951253
// Create a block for the increment. In case of a 'continue', we jump there.
11961254
JumpDest Continue = getJumpDestInCurrentScope("for.inc");
@@ -1220,6 +1278,11 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
12201278

12211279
// Emit the fall-through block.
12221280
EmitBlock(LoopExit.getBlock(), true);
1281+
1282+
// When single byte coverage mode is enabled, add a counter to continuation
1283+
// block.
1284+
if (llvm::EnableSingleByteCoverage)
1285+
incrementProfileCounter(&S);
12231286
}
12241287

12251288
void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
using namespace clang;
5353
using namespace CodeGen;
5454

55+
namespace llvm {
56+
extern cl::opt<bool> EnableSingleByteCoverage;
57+
} // namespace llvm
58+
5559
/// shouldEmitLifetimeMarkers - Decide whether we need emit the life-time
5660
/// markers.
5761
static bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts,
@@ -1263,7 +1267,10 @@ void CodeGenFunction::EmitFunctionBody(const Stmt *Body) {
12631267
void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
12641268
const Stmt *S) {
12651269
llvm::BasicBlock *SkipCountBB = nullptr;
1266-
if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) {
1270+
// Do not skip over the instrumentation when single byte coverage mode is
1271+
// enabled.
1272+
if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr() &&
1273+
!llvm::EnableSingleByteCoverage) {
12671274
// When instrumenting for profiling, the fallthrough to certain
12681275
// statements needs to skip over the instrumentation code so that we
12691276
// get an accurate count.

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ class CodeGenFunction : public CodeGenTypeCache {
15301530
if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
15311531
!CurFn->hasFnAttribute(llvm::Attribute::NoProfile) &&
15321532
!CurFn->hasFnAttribute(llvm::Attribute::SkipProfile))
1533-
PGO.emitCounterIncrement(Builder, S, StepV);
1533+
PGO.emitCounterSetOrIncrement(Builder, S, StepV);
15341534
PGO.setCurrentStmt(S);
15351535
}
15361536

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ void CodeGenModule::Release() {
566566
checkAliases();
567567
EmitDeferredUnusedCoverageMappings();
568568
CodeGenPGO(*this).setValueProfilingFlag(getModule());
569+
CodeGenPGO(*this).setProfileVersion(getModule());
569570
if (CoverageMapping)
570571
CoverageMapping->emit();
571572
if (CodeGenOpts.SanitizeCfiCrossDso) {

0 commit comments

Comments
 (0)