Skip to content

[Coverage] Make additional counters available for BranchRegion. NFC. #120930

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 62 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
618e639
Introduce CounterExpressionBuilder::replace(C, Map)
chapuni Oct 16, 2024
fc697f0
[Coverage] Introduce `getBranchCounterPair()`. NFC.
chapuni Oct 16, 2024
e4172ca
Introduce the type `CounterPair` for RegionCounterMap
chapuni Oct 16, 2024
12abd89
Merge branches 'users/chapuni/cov/single/getpair', 'users/chapuni/cov…
chapuni Oct 17, 2024
5e46059
[Coverage] Make additional counters available for BranchRegion. NFC.
chapuni Oct 17, 2024
ad13691
Rewind changes for folding
chapuni Oct 18, 2024
209ea4c
Update comments
chapuni Oct 20, 2024
f0afd04
Use initializer statements
chapuni Oct 20, 2024
d4518a4
Merge branches 'users/chapuni/cov/single/getpair' and 'users/chapuni/…
chapuni Oct 20, 2024
05df8df
Merge branch 'users/chapuni/cov/single/nextcount-base' into users/cha…
chapuni Oct 20, 2024
be516fa
`first` may be cancelled.
chapuni Oct 20, 2024
03cfce1
CGF::markStmtAsUsed
chapuni Oct 23, 2024
afc8481
CGF.markStmtMaybeUsed for binop
chapuni Oct 23, 2024
58feee3
Merge branch 'main' into users/chapuni/cov/single/getpair
chapuni Oct 27, 2024
ab84f17
Introduce skeleton getSwitchImplicitDefaultCounter()
chapuni Oct 27, 2024
804d330
Merge branches 'users/chapuni/cov/single/pair' and 'users/chapuni/cov…
chapuni Oct 27, 2024
2c29f5d
Merge branch 'users/chapuni/cov/single/nextcount-base' into HEAD
chapuni Oct 27, 2024
a460885
Update getSwitchImplicitDefaultCounter
chapuni Oct 27, 2024
0285394
Don't allocate second if SkipExpr isn't Expr.
chapuni Oct 27, 2024
ce7c17d
Merge branch 'main' into users/chapuni/cov/single/pair
chapuni Dec 21, 2024
631bc35
Merge branch 'main' into users/chapuni/cov/single/replace
chapuni Dec 21, 2024
ed700c2
s/replace/subst/
chapuni Dec 21, 2024
9d3c3b0
Merge branch 'main' into users/chapuni/cov/single/getpair
chapuni Dec 21, 2024
dbcf896
getSwitchImplicitDefaultCounterPair
chapuni Dec 21, 2024
2cb6395
Merge branch 'users/chapuni/cov/single/pair' into users/chapuni/cov/s…
chapuni Dec 21, 2024
702a72e
Merge branch 'users/chapuni/cov/single/getpair' into users/chapuni/co…
chapuni Dec 21, 2024
36465dc
Merge branch 'users/chapuni/cov/single/replace' into users/chapuni/co…
chapuni Dec 21, 2024
2413b83
Merge branch 'users/chapuni/cov/single/nextcount-base' into users/cha…
chapuni Dec 21, 2024
c0785e9
Fold either in switchcase
chapuni Dec 21, 2024
bd1d96b
Introduce CounterMappingRegion::isBranch(). NFC.
chapuni Dec 23, 2024
19edcd3
Merge branch 'main' into users/chapuni/cov/single/getpair
chapuni Dec 24, 2024
4e41b99
Introduce BranchCounterPair
chapuni Dec 24, 2024
63dbfb3
Merge branch 'main' into users/chapuni/cov/single/pair
chapuni Dec 24, 2024
306d77f
void verifyCounterMap() const
chapuni Dec 24, 2024
a4f05c7
Introduce the dedicated class CounterPair instead of std::pair
chapuni Dec 24, 2024
1f18ab9
Merge branches 'users/chapuni/cov/single/getpair' and 'users/chapuni/…
chapuni Dec 24, 2024
b7ae558
Merge branch 'users/chapuni/cov/single/nextcount-base' into users/cha…
chapuni Dec 24, 2024
f6c5f40
Catch up the merge
chapuni Dec 24, 2024
aca86d4
Introduce {UseExecPath, UseSkipPath} instead of {false, true}
chapuni Dec 24, 2024
28c568a
Add a test
chapuni Jan 6, 2025
d92a9d9
Prune redundant logic
chapuni Jan 6, 2025
93fb420
Merge branch 'users/chapuni/cov/single/replace' into users/chapuni/co…
chapuni Jan 6, 2025
6f8681c
Merge branch 'users/chapuni/cov/single/nextcount-base' into users/cha…
chapuni Jan 6, 2025
82f2e92
Expand RHS of MapToExpand. This will prevent recursion.
chapuni Jan 6, 2025
b90fdf6
Append an explanation in the comment
chapuni Jan 6, 2025
d854fb1
Rewind switch DefaultCase. (to #113112)
chapuni Jan 7, 2025
bac2967
Enable addCounters
chapuni Jan 7, 2025
6bae87d
Get rid of structual bindings
chapuni Jan 8, 2025
c8edf58
Flatten with getBranchCounterPair(SkipCntForOld)
chapuni Jan 8, 2025
ec6892d
Merge branch 'users/chapuni/cov/single/getpair' into users/chapuni/co…
chapuni Jan 8, 2025
9b99dde
Merge branch 'users/chapuni/cov/single/nextcount-base' into users/cha…
chapuni Jan 8, 2025
f2ba219
Update comments
chapuni Jan 8, 2025
97015cb
Decorate the mock
chapuni Jan 8, 2025
9a40d20
Will be pruned after the migration of SingleByte.
chapuni Jan 9, 2025
b548e71
Add comments
chapuni Jan 9, 2025
13fbcde
Merge branch 'main' into users/chapuni/cov/single/replace
chapuni Jan 9, 2025
789eeab
Merge branch 'main' into users/chapuni/cov/single/getpair
chapuni Jan 9, 2025
8683882
Merge branch 'users/chapuni/cov/single/replace' into users/chapuni/co…
chapuni Jan 9, 2025
de5756c
Merge branch 'main' into users/chapuni/cov/single/pair
chapuni Jan 9, 2025
6d16b1c
Merge branch 'users/chapuni/cov/single/getpair' into users/chapuni/co…
chapuni Jan 9, 2025
0aa930a
Merge branch 'users/chapuni/cov/single/pair' into users/chapuni/cov/s…
chapuni Jan 9, 2025
fea7da1
Merge branch 'users/chapuni/cov/single/nextcount-base' into users/cha…
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
19 changes: 18 additions & 1 deletion clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1627,14 +1627,31 @@ class CodeGenFunction : public CodeGenTypeCache {
}
void markStmtMaybeUsed(const Stmt *S) { PGO.markStmtMaybeUsed(S); }

enum CounterForIncrement {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doxygen comment would be useful here

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the meaning of "exec counter" and "skip counter" defined anywhere? if not, it would be good to do that here.

UseExecPath = 0,
UseSkipPath,
};

/// 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(UseExecPath, S, false, StepV);
}

/// Emit increment of Counter.
/// \param ExecSkip Use `Skipped` Counter if UseSkipPath is specified.
/// \param S The Stmt that Counter is associated.
/// \param UseBoth Mark both Exec/Skip as used. (for verification)
/// \param StepV The offset Value for adding to Counter.
void incrementProfileCounter(CounterForIncrement ExecSkip, 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, (ExecSkip == UseSkipPath),
UseBoth, StepV);
}
PGO.setCurrentStmt(S);
}
Expand Down
28 changes: 26 additions & 2 deletions clang/lib/CodeGen/CodeGenPGO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,16 @@ void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
if (CoverageMapping.empty())
return;

// Scan max(FalseCnt) and update NumRegionCounters.
unsigned MaxNumCounters = NumRegionCounters;
for (const auto [_, V] : *RegionCounterMap) {
assert((!V.Executed.hasValue() || MaxNumCounters > V.Executed) &&
"TrueCnt should not be reassigned");
if (V.Skipped.hasValue())
MaxNumCounters = std::max(MaxNumCounters, V.Skipped + 1);
}
NumRegionCounters = MaxNumCounters;

CGM.getCoverageMapping()->addFunctionMappingRecord(
FuncNameVar, FuncName, FunctionHash, CoverageMapping);
}
Expand Down Expand Up @@ -1197,11 +1207,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].Executed;
unsigned Counter;
auto &TheMap = (*RegionCounterMap)[S];
if (!UseSkipPath) {
if (!TheMap.Executed.hasValue())
return;
Counter = TheMap.Executed;
} else {
if (!TheMap.Skipped.hasValue())
return;
Counter = TheMap.Skipped;
}

if (!Builder.GetInsertBlock())
return;

// Make sure that pointer to global is passed in with zero addrspace
// This is relevant during GPU profiling
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CodeGenPGO.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 44 additions & 6 deletions clang/lib/CodeGen/CoverageMappingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,9 @@ struct CounterCoverageMappingBuilder
/// The map of statements to count values.
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;

CounterExpressionBuilder::SubstMap MapToExpand;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MapToExpand could use a doxygen comment explaining what it is

unsigned NextCounterNum;

MCDC::State &MCDCState;

/// A stack of currently live regions.
Expand Down Expand Up @@ -958,7 +961,8 @@ struct CounterCoverageMappingBuilder
BranchCounterPair
getBranchCounterPair(const Stmt *S, Counter ParentCnt,
std::optional<Counter> SkipCntForOld = std::nullopt) {
Counter ExecCnt = getRegionCounter(S);
auto &TheMap = CounterMap[S];
auto ExecCnt = Counter::getCounter(TheMap.Executed);

// The old behavior of SingleByte is unaware of Branches.
// Will be pruned after the migration of SingleByte.
Expand All @@ -968,13 +972,44 @@ struct CounterCoverageMappingBuilder
return {ExecCnt, *SkipCntForOld};
}

return {ExecCnt, Builder.subtract(ParentCnt, ExecCnt)};
BranchCounterPair Counters = {ExecCnt,
Builder.subtract(ParentCnt, ExecCnt)};

if (!llvm::EnableSingleByteCoverage || !Counters.Skipped.isExpression()) {
assert(
!TheMap.Skipped.hasValue() &&
"SkipCnt shouldn't be allocated but refer to an existing counter.");
return Counters;
}

// Assign second if second is not assigned yet.
if (!TheMap.Skipped.hasValue())
TheMap.Skipped = NextCounterNum++;

// Replace an expression (ParentCnt - ExecCnt) with SkipCnt.
Counter SkipCnt = Counter::getCounter(TheMap.Skipped);
MapToExpand[SkipCnt] = Builder.subst(Counters.Skipped, MapToExpand);
Counters.Skipped = SkipCnt;
return Counters;
}

bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
if (OutCount == ParentCount)
return true;

// Try comaparison with pre-replaced expressions.
//
// For example, getBranchCounterPair(#0) returns {#1, #0 - #1}.
// The sum of the pair should be equivalent to the Parent, #0.
// OTOH when (#0 - #1) is replaced with the new counter #2,
// The sum is (#1 + #2). If the reverse substitution #2 => (#0 - #1)
// can be applied, the sum can be transformed to (#1 + (#0 - #1)).
// To apply substitutions to both hand expressions, transform (LHS - RHS)
// and check isZero.
if (Builder.subst(Builder.subtract(OutCount, ParentCount), MapToExpand)
.isZero())
return true;

return false;
}

Expand Down Expand Up @@ -1188,12 +1223,14 @@ struct CounterCoverageMappingBuilder
/// and add it to the function's SourceRegions.
/// Returns Counter that corresponds to SC.
Counter createSwitchCaseRegion(const SwitchCase *SC, Counter ParentCount) {
Counter TrueCnt = getRegionCounter(SC);
Counter FalseCnt = (llvm::EnableSingleByteCoverage
? Counter::getZero() // Folded
: subtractCounters(ParentCount, TrueCnt));
// Push region onto RegionStack but immediately pop it (which adds it to
// the function's SourceRegions) because it doesn't apply to any other
// source other than the SwitchCase.
Counter TrueCnt = getRegionCounter(SC);
popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(),
subtractCounters(ParentCount, TrueCnt)));
popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(), FalseCnt));
return TrueCnt;
}

Expand Down Expand Up @@ -1460,7 +1497,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) {
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,9 @@ 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.isBranch())
MaxCounterID =
std::max(MaxCounterID, Ctx.getMaxCounterID(Region.FalseCount));
}
return MaxCounterID;
}
Expand Down
Loading