Skip to content

[InstrProf] Single byte counters in coverage #75425

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ using namespace CodeGen;
// Aggregate Expression Emitter
//===----------------------------------------------------------------------===//

namespace llvm {
extern cl::opt<bool> EnableSingleByteCoverage;
} // namespace llvm

namespace {
class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
CodeGenFunction &CGF;
Expand Down Expand Up @@ -1279,7 +1283,10 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {

eval.begin(CGF);
CGF.EmitBlock(LHSBlock);
CGF.incrementProfileCounter(E);
if (llvm::EnableSingleByteCoverage)
CGF.incrementProfileCounter(E->getTrueExpr());
else
CGF.incrementProfileCounter(E);
Visit(E->getTrueExpr());
eval.end(CGF);

Expand All @@ -1294,6 +1301,8 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {

eval.begin(CGF);
CGF.EmitBlock(RHSBlock);
if (llvm::EnableSingleByteCoverage)
CGF.incrementProfileCounter(E->getFalseExpr());
Visit(E->getFalseExpr());
eval.end(CGF);

Expand All @@ -1302,6 +1311,8 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
E->getType());

CGF.EmitBlock(ContBlock);
if (llvm::EnableSingleByteCoverage)
CGF.incrementProfileCounter(E);
}

void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
Expand Down
14 changes: 13 additions & 1 deletion clang/lib/CodeGen/CGExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ 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.
Expand Down Expand Up @@ -1330,17 +1334,25 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {

eval.begin(CGF);
CGF.EmitBlock(LHSBlock);
CGF.incrementProfileCounter(E);
if (llvm::EnableSingleByteCoverage)
CGF.incrementProfileCounter(E->getTrueExpr());
else
CGF.incrementProfileCounter(E);

ComplexPairTy LHS = Visit(E->getTrueExpr());
LHSBlock = Builder.GetInsertBlock();
CGF.EmitBranch(ContBlock);
eval.end(CGF);

eval.begin(CGF);
CGF.EmitBlock(RHSBlock);
if (llvm::EnableSingleByteCoverage)
CGF.incrementProfileCounter(E->getFalseExpr());
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.
Expand Down
32 changes: 29 additions & 3 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ using llvm::Value;
// Scalar Expression Emitter
//===----------------------------------------------------------------------===//

namespace llvm {
extern cl::opt<bool> EnableSingleByteCoverage;
} // namespace llvm

namespace {

/// Determine whether the given binary operation may overflow.
Expand Down Expand Up @@ -4925,8 +4929,13 @@ 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 (CondExprBool) {
if (llvm::EnableSingleByteCoverage) {
CGF.incrementProfileCounter(lhsExpr);
CGF.incrementProfileCounter(rhsExpr);
}
CGF.incrementProfileCounter(E);
}
Value *Result = Visit(live);

// If the live part is a throw expression, it acts like it has a void
Expand Down Expand Up @@ -5005,7 +5014,12 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
llvm::Value *StepV = Builder.CreateZExtOrBitCast(CondV, CGF.Int64Ty);

CGF.incrementProfileCounter(E, StepV);
if (llvm::EnableSingleByteCoverage) {
CGF.incrementProfileCounter(lhsExpr);
CGF.incrementProfileCounter(rhsExpr);
CGF.incrementProfileCounter(E);
} else
CGF.incrementProfileCounter(E, StepV);

llvm::Value *LHS = Visit(lhsExpr);
llvm::Value *RHS = Visit(rhsExpr);
Expand Down Expand Up @@ -5037,7 +5051,11 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
if (CGF.MCDCLogOpStack.empty())
CGF.maybeUpdateMCDCTestVectorBitmap(condExpr);

CGF.incrementProfileCounter(E);
if (llvm::EnableSingleByteCoverage)
CGF.incrementProfileCounter(lhsExpr);
else
CGF.incrementProfileCounter(E);

eval.begin(CGF);
Value *LHS = Visit(lhsExpr);
eval.end(CGF);
Expand All @@ -5053,6 +5071,9 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
if (CGF.MCDCLogOpStack.empty())
CGF.maybeUpdateMCDCTestVectorBitmap(condExpr);

if (llvm::EnableSingleByteCoverage)
CGF.incrementProfileCounter(rhsExpr);

eval.begin(CGF);
Value *RHS = Visit(rhsExpr);
eval.end(CGF);
Expand All @@ -5071,6 +5092,11 @@ 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;
}

Expand Down
73 changes: 68 additions & 5 deletions clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ 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;
Expand Down Expand Up @@ -856,7 +860,10 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {

// Emit the 'then' code.
EmitBlock(ThenBlock);
incrementProfileCounter(&S);
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(S.getThen());
else
incrementProfileCounter(&S);
{
RunCleanupsScope ThenScope(*this);
EmitStmt(S.getThen());
Expand All @@ -870,6 +877,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);
{
RunCleanupsScope ElseScope(*this);
EmitStmt(Else);
Expand All @@ -883,6 +893,11 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {

// 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);
}

void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
Expand Down Expand Up @@ -927,6 +942,10 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
SourceLocToDebugLoc(R.getEnd()),
checkIfLoopMustProgress(CondIsConstInt));

// 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) {
Expand Down Expand Up @@ -959,7 +978,11 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
{
RunCleanupsScope BodyScope(*this);
EmitBlock(LoopBody);
incrementProfileCounter(&S);
// When single byte coverage mode is enabled, add a counter to the body.
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(S.getBody());
else
incrementProfileCounter(&S);
EmitStmt(S.getBody());
}

Expand All @@ -981,6 +1004,11 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
// a branch, try to erase it.
if (!EmitBoolCondBranch)
SimplifyForwardingBlocks(LoopHeader.getBlock());

// When single byte coverage mode is enabled, add a counter to continuation
// block.
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(&S);
}

void CodeGenFunction::EmitDoStmt(const DoStmt &S,
Expand All @@ -996,13 +1024,19 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
// Emit the body of the loop.
llvm::BasicBlock *LoopBody = createBasicBlock("do.body");

EmitBlockWithFallThrough(LoopBody, &S);
if (llvm::EnableSingleByteCoverage)
EmitBlockWithFallThrough(LoopBody, S.getBody());
else
EmitBlockWithFallThrough(LoopBody, &S);
{
RunCleanupsScope BodyScope(*this);
EmitStmt(S.getBody());
}

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."
Expand Down Expand Up @@ -1043,6 +1077,11 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S,
// emitting a branch, try to erase it.
if (!EmitBoolCondBranch)
SimplifyForwardingBlocks(LoopCond.getBlock());

// When single byte coverage mode is enabled, add a counter to continuation
// block.
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(&S);
}

void CodeGenFunction::EmitForStmt(const ForStmt &S,
Expand Down Expand Up @@ -1101,6 +1140,11 @@ 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.
Expand Down Expand Up @@ -1131,8 +1175,12 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
// Treat it as a non-zero constant. Don't even create a new block for the
// body, just fall into it.
}
incrementProfileCounter(&S);

// When single byte coverage mode is enabled, add a counter to the body.
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(S.getBody());
else
incrementProfileCounter(&S);
{
// Create a separate cleanup scope for the body, in case it is not
// a compound statement.
Expand All @@ -1144,6 +1192,8 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
if (S.getInc()) {
EmitBlock(Continue.getBlock());
EmitStmt(S.getInc());
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(S.getInc());
}

BreakContinueStack.pop_back();
Expand All @@ -1159,6 +1209,11 @@ 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);
}

void
Expand Down Expand Up @@ -1211,7 +1266,10 @@ CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
}

EmitBlock(ForBody);
incrementProfileCounter(&S);
if (llvm::EnableSingleByteCoverage)
incrementProfileCounter(S.getBody());
else
incrementProfileCounter(&S);

// Create a block for the increment. In case of a 'continue', we jump there.
JumpDest Continue = getJumpDestInCurrentScope("for.inc");
Expand Down Expand Up @@ -1241,6 +1299,11 @@ 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);
}

void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
Expand Down
9 changes: 8 additions & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
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,
Expand Down Expand Up @@ -1270,7 +1274,10 @@ void CodeGenFunction::EmitFunctionBody(const Stmt *Body) {
void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
const Stmt *S) {
llvm::BasicBlock *SkipCountBB = nullptr;
if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) {
// Do not skip over the instrumentation when single byte coverage mode is
// enabled.
if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr() &&
!llvm::EnableSingleByteCoverage) {
// When instrumenting for profiling, the fallthrough to certain
// statements needs to skip over the instrumentation code so that we
// get an accurate count.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ class CodeGenFunction : public CodeGenTypeCache {
if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
!CurFn->hasFnAttribute(llvm::Attribute::NoProfile) &&
!CurFn->hasFnAttribute(llvm::Attribute::SkipProfile))
PGO.emitCounterIncrement(Builder, S, StepV);
PGO.emitCounterSetOrIncrement(Builder, S, StepV);
PGO.setCurrentStmt(S);
}

Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ void CodeGenModule::Release() {
checkAliases();
EmitDeferredUnusedCoverageMappings();
CodeGenPGO(*this).setValueProfilingFlag(getModule());
CodeGenPGO(*this).setProfileVersion(getModule());
if (CoverageMapping)
CoverageMapping->emit();
if (CodeGenOpts.SanitizeCfiCrossDso) {
Expand Down
Loading