Skip to content

Commit 78e33ba

Browse files
committed
Merge branches 'users/chapuni/cov/single/test' and 'users/chapuni/cov/single/nextcount' into users/chapuni/cov/single/base
2 parents 52f072e + be516fa commit 78e33ba

File tree

13 files changed

+272
-94
lines changed

13 files changed

+272
-94
lines changed

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
362362
return GV;
363363
}
364364

365+
PGO.markStmtMaybeUsed(D.getInit()); // FIXME: Too lazy
366+
365367
#ifndef NDEBUG
366368
CharUnits VarSize = CGM.getContext().getTypeSizeInChars(D.getType()) +
367369
D.getFlexibleArrayInitChars(getContext());
@@ -1869,7 +1871,10 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
18691871
// If we are at an unreachable point, we don't need to emit the initializer
18701872
// unless it contains a label.
18711873
if (!HaveInsertPoint()) {
1872-
if (!Init || !ContainsLabel(Init)) return;
1874+
if (!Init || !ContainsLabel(Init)) {
1875+
PGO.markStmtMaybeUsed(Init);
1876+
return;
1877+
}
18731878
EnsureInsertPoint();
18741879
}
18751880

@@ -1978,6 +1983,8 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
19781983
return EmitExprAsInit(Init, &D, lv, capturedByInit);
19791984
}
19801985

1986+
PGO.markStmtMaybeUsed(Init);
1987+
19811988
if (!emission.IsConstantAggregate) {
19821989
// For simple scalar/complex initialization, store the value directly.
19831990
LValue lv = MakeAddrLValue(Loc, type);

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5139,6 +5139,7 @@ std::optional<LValue> HandleConditionalOperatorLValueSimpleCase(
51395139
// If the true case is live, we need to track its region.
51405140
if (CondExprBool)
51415141
CGF.incrementProfileCounter(E);
5142+
CGF.markStmtMaybeUsed(Dead);
51425143
// If a throw expression we emit it and return an undefined lvalue
51435144
// because it can't be used.
51445145
if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Live->IgnoreParens())) {

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4986,8 +4986,10 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
49864986
}
49874987

49884988
// 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
4989-
if (!CGF.ContainsLabel(E->getRHS()))
4989+
if (!CGF.ContainsLabel(E->getRHS())) {
4990+
CGF.markStmtMaybeUsed(E->getRHS());
49904991
return llvm::Constant::getNullValue(ResTy);
4992+
}
49914993
}
49924994

49934995
// If the top of the logical operator nest, reset the MCDC temp to 0.
@@ -5126,8 +5128,10 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
51265128
}
51275129

51285130
// 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
5129-
if (!CGF.ContainsLabel(E->getRHS()))
5131+
if (!CGF.ContainsLabel(E->getRHS())) {
5132+
CGF.markStmtMaybeUsed(E->getRHS());
51305133
return llvm::ConstantInt::get(ResTy, 1);
5134+
}
51315135
}
51325136

51335137
// If the top of the logical operator nest, reset the MCDC temp to 0.
@@ -5251,6 +5255,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
52515255
CGF.incrementProfileCounter(E);
52525256
}
52535257
Value *Result = Visit(live);
5258+
CGF.markStmtMaybeUsed(dead);
52545259

52555260
// If the live part is a throw expression, it acts like it has a void
52565261
// type, so evaluating it returns a null Value*. However, a conditional

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
7676
// Verify that any decl statements were handled as simple, they may be in
7777
// scope of subsequent reachable statements.
7878
assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
79+
PGO.markStmtMaybeUsed(S);
7980
return;
8081
}
8182

@@ -845,6 +846,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
845846
RunCleanupsScope ExecutedScope(*this);
846847
EmitStmt(Executed);
847848
}
849+
PGO.markStmtMaybeUsed(Skipped);
848850
return;
849851
}
850852
}
@@ -2170,6 +2172,7 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
21702172
for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
21712173
EmitStmt(CaseStmts[i]);
21722174
incrementProfileCounter(&S);
2175+
PGO.markStmtMaybeUsed(S.getBody());
21732176

21742177
// Now we want to restore the saved switch instance so that nested
21752178
// switches continue to function properly

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,8 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
16091609
// Emit the standard function epilogue.
16101610
FinishFunction(BodyRange.getEnd());
16111611

1612+
PGO.verifyCounterMap();
1613+
16121614
// If we haven't marked the function nothrow through other means, do
16131615
// a quick pass now to see if we can.
16141616
if (!CurFn->doesNotThrow())
@@ -1731,6 +1733,7 @@ bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
17311733
if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond))
17321734
return false; // Contains a label.
17331735

1736+
PGO.markStmtMaybeUsed(Cond);
17341737
ResultInt = Int;
17351738
return true;
17361739
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,14 +1620,26 @@ class CodeGenFunction : public CodeGenTypeCache {
16201620
uint64_t LoopCount) const;
16211621

16221622
public:
1623+
std::pair<bool, bool> getIsCounterPair(const Stmt *S) const {
1624+
return PGO.getIsCounterPair(S);
1625+
}
1626+
1627+
void markStmtMaybeUsed(const Stmt *S) { PGO.markStmtMaybeUsed(S); }
1628+
16231629
/// Increment the profiler's counter for the given statement by \p StepV.
16241630
/// If \p StepV is null, the default increment is 1.
16251631
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) {
1632+
incrementProfileCounter(false, S, false, StepV);
1633+
}
1634+
1635+
void incrementProfileCounter(bool UseSkipPath, const Stmt *S,
1636+
bool UseBoth = false,
1637+
llvm::Value *StepV = nullptr) {
16261638
if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
16271639
!CurFn->hasFnAttribute(llvm::Attribute::NoProfile) &&
16281640
!CurFn->hasFnAttribute(llvm::Attribute::SkipProfile)) {
16291641
auto AL = ApplyDebugLocation::CreateArtificial(*this);
1630-
PGO.emitCounterSetOrIncrement(Builder, S, StepV);
1642+
PGO.emitCounterSetOrIncrement(Builder, S, UseSkipPath, UseBoth, StepV);
16311643
}
16321644
PGO.setCurrentStmt(S);
16331645
}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ enum ForDefinition_t : bool {
102102
ForDefinition = true
103103
};
104104

105+
class CounterPair : public std::pair<uint32_t, uint32_t> {
106+
private:
107+
static constexpr uint32_t None = (1u << 31); /// None is set
108+
109+
public:
110+
static constexpr uint32_t Mask = None - 1;
111+
112+
public:
113+
CounterPair(unsigned Val = 0) {
114+
assert(!(Val & ~Mask));
115+
first = Val;
116+
second = None;
117+
}
118+
119+
std::pair<bool, bool> getIsCounterPair() const {
120+
return {!(first & None), !(second & None)};
121+
}
122+
};
123+
105124
struct OrderGlobalInitsOrStermFinalizers {
106125
unsigned int priority;
107126
unsigned int lex_order;

clang/lib/CodeGen/CodeGenPGO.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
164164
/// The function hash.
165165
PGOHash Hash;
166166
/// The map of statements to counters.
167-
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
167+
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;
168168
/// The state of MC/DC Coverage in this function.
169169
MCDC::State &MCDCState;
170170
/// Maximum number of supported MC/DC conditions in a boolean expression.
@@ -175,7 +175,7 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
175175
DiagnosticsEngine &Diag;
176176

177177
MapRegionCounters(PGOHashVersion HashVersion, uint64_t ProfileVersion,
178-
llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
178+
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap,
179179
MCDC::State &MCDCState, unsigned MCDCMaxCond,
180180
DiagnosticsEngine &Diag)
181181
: NextCounter(0), Hash(HashVersion), CounterMap(CounterMap),
@@ -1084,7 +1084,7 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
10841084
(CGM.getCodeGenOpts().MCDCCoverage ? CGM.getCodeGenOpts().MCDCMaxConds
10851085
: 0);
10861086

1087-
RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
1087+
RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, CounterPair>);
10881088
RegionMCDCState.reset(new MCDC::State);
10891089
MapRegionCounters Walker(HashVersion, ProfileVersion, *RegionCounterMap,
10901090
*RegionMCDCState, MCDCMaxConditions, CGM.getDiags());
@@ -1138,6 +1138,19 @@ void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
11381138
if (CoverageMapping.empty())
11391139
return;
11401140

1141+
// Scan max(FalseCnt) and update NumRegionCounters.
1142+
unsigned MaxNumCounters = NumRegionCounters;
1143+
for (const auto [_, V] : *RegionCounterMap) {
1144+
auto HasCounters = V.getIsCounterPair();
1145+
assert((!HasCounters.first ||
1146+
MaxNumCounters > (V.first & CounterPair::Mask)) &&
1147+
"TrueCnt should not be reassigned");
1148+
if (HasCounters.second)
1149+
MaxNumCounters =
1150+
std::max(MaxNumCounters, (V.second & CounterPair::Mask) + 1);
1151+
}
1152+
NumRegionCounters = MaxNumCounters;
1153+
11411154
CGM.getCoverageMapping()->addFunctionMappingRecord(
11421155
FuncNameVar, FuncName, FunctionHash, CoverageMapping);
11431156
}
@@ -1186,12 +1199,33 @@ CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
11861199
Fn->setEntryCount(FunctionCount);
11871200
}
11881201

1202+
std::pair<bool, bool> CodeGenPGO::getIsCounterPair(const Stmt *S) const {
1203+
if (!RegionCounterMap || RegionCounterMap->count(S) == 0)
1204+
return {false, false};
1205+
return (*RegionCounterMap)[S].getIsCounterPair();
1206+
}
1207+
11891208
void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
1209+
bool UseSkipPath, bool UseBoth,
11901210
llvm::Value *StepV) {
1191-
if (!RegionCounterMap || !Builder.GetInsertBlock())
1211+
if (!RegionCounterMap)
11921212
return;
11931213

1194-
unsigned Counter = (*RegionCounterMap)[S];
1214+
unsigned Counter;
1215+
auto &TheMap = (*RegionCounterMap)[S];
1216+
auto IsCounter = TheMap.getIsCounterPair();
1217+
if (!UseSkipPath) {
1218+
if (!IsCounter.first)
1219+
return;
1220+
Counter = (TheMap.first & CounterPair::Mask);
1221+
} else {
1222+
if (!IsCounter.second)
1223+
return;
1224+
Counter = (TheMap.second & CounterPair::Mask);
1225+
}
1226+
1227+
if (!Builder.GetInsertBlock())
1228+
return;
11951229

11961230
// Make sure that pointer to global is passed in with zero addrspace
11971231
// This is relevant during GPU profiling

clang/lib/CodeGen/CodeGenPGO.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CodeGenPGO {
3535
std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
3636
unsigned NumRegionCounters;
3737
uint64_t FunctionHash;
38-
std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
38+
std::unique_ptr<llvm::DenseMap<const Stmt *, CounterPair>> RegionCounterMap;
3939
std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
4040
std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
4141
std::unique_ptr<MCDC::State> RegionMCDCState;
@@ -110,7 +110,9 @@ class CodeGenPGO {
110110
bool canEmitMCDCCoverage(const CGBuilderTy &Builder);
111111

112112
public:
113+
std::pair<bool, bool> getIsCounterPair(const Stmt *S) const;
113114
void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
115+
bool UseFalsePath, bool UseBoth,
114116
llvm::Value *StepV);
115117
void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
116118
Address MCDCCondBitmapAddr,
@@ -122,6 +124,18 @@ class CodeGenPGO {
122124
Address MCDCCondBitmapAddr, llvm::Value *Val,
123125
CodeGenFunction &CGF);
124126

127+
void markStmtAsUsed(bool Skipped, const Stmt *S) {
128+
// Do nothing.
129+
}
130+
131+
void markStmtMaybeUsed(const Stmt *S) {
132+
// Do nothing.
133+
}
134+
135+
void verifyCounterMap() {
136+
// Do nothing.
137+
}
138+
125139
/// Return the region count for the counter at the given index.
126140
uint64_t getRegionCount(const Stmt *S) {
127141
if (!RegionCounterMap)
@@ -130,7 +144,7 @@ class CodeGenPGO {
130144
return 0;
131145
// With profiles from a differing version of clang we can have mismatched
132146
// decl counts. Don't crash in such a case.
133-
auto Index = (*RegionCounterMap)[S];
147+
auto Index = (*RegionCounterMap)[S].first;
134148
if (Index >= RegionCounts.size())
135149
return 0;
136150
return RegionCounts[Index];

0 commit comments

Comments
 (0)