Skip to content

Commit 12abd89

Browse files
committed
Merge branches 'users/chapuni/cov/single/getpair', 'users/chapuni/cov/single/replace' and 'users/chapuni/cov/single/pair' into HEAD
4 parents 6c331e5 + fc697f0 + 618e639 + e4172ca commit 12abd89

File tree

13 files changed

+218
-89
lines changed

13 files changed

+218
-89
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
@@ -5134,6 +5134,7 @@ std::optional<LValue> HandleConditionalOperatorLValueSimpleCase(
51345134
// If the true case is live, we need to track its region.
51355135
if (CondExprBool)
51365136
CGF.incrementProfileCounter(E);
5137+
CGF.markStmtMaybeUsed(Dead);
51375138
// If a throw expression we emit it and return an undefined lvalue
51385139
// because it can't be used.
51395140
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
@@ -4982,8 +4982,10 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
49824982
}
49834983

49844984
// 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
4985-
if (!CGF.ContainsLabel(E->getRHS()))
4985+
if (!CGF.ContainsLabel(E->getRHS())) {
4986+
CGF.markStmtMaybeUsed(E->getRHS());
49864987
return llvm::Constant::getNullValue(ResTy);
4988+
}
49874989
}
49884990

49894991
// If the top of the logical operator nest, reset the MCDC temp to 0.
@@ -5122,8 +5124,10 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
51225124
}
51235125

51245126
// 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
5125-
if (!CGF.ContainsLabel(E->getRHS()))
5127+
if (!CGF.ContainsLabel(E->getRHS())) {
5128+
CGF.markStmtMaybeUsed(E->getRHS());
51265129
return llvm::ConstantInt::get(ResTy, 1);
5130+
}
51275131
}
51285132

51295133
// If the top of the logical operator nest, reset the MCDC temp to 0.
@@ -5247,6 +5251,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
52475251
CGF.incrementProfileCounter(E);
52485252
}
52495253
Value *Result = Visit(live);
5254+
CGF.markStmtMaybeUsed(dead);
52505255

52515256
// If the live part is a throw expression, it acts like it has a void
52525257
// 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
@@ -1606,6 +1606,8 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
16061606
// Emit the standard function epilogue.
16071607
FinishFunction(BodyRange.getEnd());
16081608

1609+
PGO.verifyCounterMap();
1610+
16091611
// If we haven't marked the function nothrow through other means, do
16101612
// a quick pass now to see if we can.
16111613
if (!CurFn->doesNotThrow())
@@ -1728,6 +1730,7 @@ bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
17281730
if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond))
17291731
return false; // Contains a label.
17301732

1733+
PGO.markStmtMaybeUsed(Cond);
17311734
ResultInt = Int;
17321735
return true;
17331736
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,12 @@ 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) {

clang/lib/CodeGen/CodeGenModule.h

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

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

clang/lib/CodeGen/CodeGenPGO.cpp

Lines changed: 10 additions & 4 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());
@@ -1186,12 +1186,18 @@ CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
11861186
Fn->setEntryCount(FunctionCount);
11871187
}
11881188

1189+
std::pair<bool, bool> CodeGenPGO::getIsCounterPair(const Stmt *S) const {
1190+
if (!RegionCounterMap || RegionCounterMap->count(S) == 0)
1191+
return {false, false};
1192+
return (*RegionCounterMap)[S].getIsCounterPair();
1193+
}
1194+
11891195
void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
11901196
llvm::Value *StepV) {
11911197
if (!RegionCounterMap || !Builder.GetInsertBlock())
11921198
return;
11931199

1194-
unsigned Counter = (*RegionCounterMap)[S];
1200+
unsigned Counter = (*RegionCounterMap)[S].first;
11951201

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

clang/lib/CodeGen/CodeGenPGO.h

Lines changed: 15 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,6 +110,7 @@ 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,
114115
llvm::Value *StepV);
115116
void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
@@ -122,6 +123,18 @@ class CodeGenPGO {
122123
Address MCDCCondBitmapAddr, llvm::Value *Val,
123124
CodeGenFunction &CGF);
124125

126+
void markStmtAsUsed(bool Skipped, const Stmt *S) {
127+
// Do nothing.
128+
}
129+
130+
void markStmtMaybeUsed(const Stmt *S) {
131+
// Do nothing.
132+
}
133+
134+
void verifyCounterMap() {
135+
// Do nothing.
136+
}
137+
125138
/// Return the region count for the counter at the given index.
126139
uint64_t getRegionCount(const Stmt *S) {
127140
if (!RegionCounterMap)
@@ -130,7 +143,7 @@ class CodeGenPGO {
130143
return 0;
131144
// With profiles from a differing version of clang we can have mismatched
132145
// decl counts. Don't crash in such a case.
133-
auto Index = (*RegionCounterMap)[S];
146+
auto Index = (*RegionCounterMap)[S].first;
134147
if (Index >= RegionCounts.size())
135148
return 0;
136149
return RegionCounts[Index];

0 commit comments

Comments
 (0)