Skip to content

Commit a4f05c7

Browse files
committed
Introduce the dedicated class CounterPair instead of std::pair
1 parent 306d77f commit a4f05c7

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,9 +1620,7 @@ 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-
}
1623+
auto getIsCounterPair(const Stmt *S) const { return PGO.getIsCounterPair(S); }
16261624

16271625
void markStmtAsUsed(bool Skipped, const Stmt *S) {
16281626
PGO.markStmtAsUsed(Skipped, S);

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,48 @@ 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-
105+
/// The Counter with an optional additional Counter for
106+
/// branches. `Skipped` counter can be calculated with `Executed` and
107+
/// a common Counter (like `Parent`) as `(Parent-Executed)`.
108+
///
109+
/// In SingleByte mode, Counters are binary. Subtraction is not
110+
/// applicable (but addition is capable). In this case, both
111+
/// `Executed` and `Skipped` counters are required. `Skipped` is
112+
/// `None` by default. It is allocated in the coverage mapping.
113+
///
114+
/// There might be cases that `Parent` could be induced with
115+
/// `(Executed+Skipped)`. This is not always applicable.
116+
class CounterPair {
109117
public:
110-
static constexpr uint32_t Mask = None - 1;
118+
/// Optional value.
119+
class ValueOpt {
120+
private:
121+
static constexpr uint32_t None = (1u << 31); /// None is allocated.
122+
static constexpr uint32_t Mask = None - 1;
111123

112-
public:
113-
CounterPair(unsigned Val = 0) {
114-
assert(!(Val & ~Mask));
115-
first = Val;
116-
second = None;
117-
}
124+
uint32_t Val;
118125

119-
std::pair<bool, bool> getIsCounterPair() const {
120-
return {!(first & None), !(second & None)};
121-
}
126+
public:
127+
ValueOpt() : Val(None) {}
128+
129+
ValueOpt(unsigned InitVal) {
130+
assert(!(InitVal & ~Mask));
131+
Val = InitVal;
132+
}
133+
134+
bool hasValue() const { return !(Val & None); }
135+
136+
operator uint32_t() const { return Val; }
137+
};
138+
139+
ValueOpt Executed;
140+
ValueOpt Skipped; /// May be None.
141+
142+
/// Initialized with Skipped=None.
143+
CounterPair(unsigned Val) : Executed(Val) {}
144+
145+
// FIXME: Should work with {None, None}
146+
CounterPair() : Executed(0) {}
122147
};
123148

124149
struct OrderGlobalInitsOrStermFinalizers {

clang/lib/CodeGen/CodeGenPGO.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,17 +1186,22 @@ CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
11861186
}
11871187

11881188
std::pair<bool, bool> CodeGenPGO::getIsCounterPair(const Stmt *S) const {
1189-
if (!RegionCounterMap || RegionCounterMap->count(S) == 0)
1189+
if (!RegionCounterMap)
11901190
return {false, false};
1191-
return (*RegionCounterMap)[S].getIsCounterPair();
1191+
1192+
auto I = RegionCounterMap->find(S);
1193+
if (I == RegionCounterMap->end())
1194+
return {false, false};
1195+
1196+
return {I->second.Executed.hasValue(), I->second.Skipped.hasValue()};
11921197
}
11931198

11941199
void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
11951200
llvm::Value *StepV) {
11961201
if (!RegionCounterMap || !Builder.GetInsertBlock())
11971202
return;
11981203

1199-
unsigned Counter = (*RegionCounterMap)[S].first;
1204+
unsigned Counter = (*RegionCounterMap)[S].Executed;
12001205

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

clang/lib/CodeGen/CodeGenPGO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class CodeGenPGO {
143143
return 0;
144144
// With profiles from a differing version of clang we can have mismatched
145145
// decl counts. Don't crash in such a case.
146-
auto Index = (*RegionCounterMap)[S].first;
146+
auto Index = (*RegionCounterMap)[S].Executed;
147147
if (Index >= RegionCounts.size())
148148
return 0;
149149
return RegionCounts[Index];

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ struct CounterCoverageMappingBuilder
935935
///
936936
/// This should only be called on statements that have a dedicated counter.
937937
Counter getRegionCounter(const Stmt *S) {
938-
return Counter::getCounter(CounterMap[S].first);
938+
return Counter::getCounter(CounterMap[S].Executed);
939939
}
940940

941941
/// Push a region onto the stack.

0 commit comments

Comments
 (0)