Skip to content

Commit dc9f037

Browse files
committed
[llvm-profgen] Refactor the code of getHashCode
Refactor to generate hash code lazily. Tested on clang self build, no observable generating time regression. Reviewed By: hoy, wenlei Differential Revision: https://reviews.llvm.org/D113059
1 parent 138202a commit dc9f037

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

llvm/tools/llvm-profgen/PerfReader.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ std::shared_ptr<StringBasedCtxKey> FrameStack::getContextKey() {
129129
KeyStr->Context = Binary->getExpandedContext(Stack, KeyStr->WasLeafInlined);
130130
if (KeyStr->Context.empty())
131131
return nullptr;
132-
KeyStr->genHashCode();
133132
return KeyStr;
134133
}
135134

@@ -143,8 +142,6 @@ std::shared_ptr<ProbeBasedCtxKey> ProbeStack::getContextKey() {
143142
ProbeBasedKey->Probes);
144143
CSProfileGenerator::trimContext<const MCDecodedPseudoProbe *>(
145144
ProbeBasedKey->Probes);
146-
147-
ProbeBasedKey->genHashCode();
148145
return ProbeBasedKey;
149146
}
150147

@@ -802,7 +799,6 @@ void UnsymbolizedProfileReader::readUnsymbolizedProfile(StringRef FileName) {
802799
SampleContext::createCtxVectorFromStr(*I.first, Key->Context);
803800
TraceIt.advance();
804801
}
805-
Key->genHashCode();
806802
auto Ret =
807803
SampleCounters.emplace(Hashable<ContextKey>(Key), SampleCounter());
808804
readSampleCounters(TraceIt, Ret.first->second);
@@ -851,7 +847,6 @@ void PerfScriptReader::generateUnsymbolizedProfile() {
851847
"Sample counter map should be empty before raw profile generation");
852848
std::shared_ptr<StringBasedCtxKey> Key =
853849
std::make_shared<StringBasedCtxKey>();
854-
Key->genHashCode();
855850
SampleCounters.emplace(Hashable<ContextKey>(Key), SampleCounter());
856851
for (const auto &Item : AggregatedSamples) {
857852
const PerfSample *Sample = Item.first.getPtr();

llvm/tools/llvm-profgen/PerfReader.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,12 @@ struct UnwindState {
314314
struct ContextKey {
315315
uint64_t HashCode = 0;
316316
virtual ~ContextKey() = default;
317-
uint64_t getHashCode() const { return HashCode; }
317+
uint64_t getHashCode() {
318+
if (HashCode == 0)
319+
genHashCode();
320+
return HashCode;
321+
}
322+
virtual void genHashCode() = 0;
318323
virtual bool isEqual(const ContextKey *K) const {
319324
return HashCode == K->HashCode;
320325
};
@@ -341,7 +346,9 @@ struct StringBasedCtxKey : public ContextKey {
341346
return Context == Other->Context;
342347
}
343348

344-
void genHashCode() { HashCode = hash_value(SampleContextFrames(Context)); }
349+
void genHashCode() override {
350+
HashCode = hash_value(SampleContextFrames(Context));
351+
}
345352
};
346353

347354
// Probe based context key as the intermediate key of context
@@ -364,7 +371,7 @@ struct ProbeBasedCtxKey : public ContextKey {
364371
O->Probes.end());
365372
}
366373

367-
void genHashCode() {
374+
void genHashCode() override {
368375
for (const auto *P : Probes) {
369376
HashCode = hash_combine(HashCode, P);
370377
}

0 commit comments

Comments
 (0)