Skip to content

Commit

Permalink
[llvm-profgen] Refactor the code of getHashCode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wlei-llvm committed Nov 3, 2021
1 parent 138202a commit dc9f037
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 0 additions & 5 deletions llvm/tools/llvm-profgen/PerfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ std::shared_ptr<StringBasedCtxKey> FrameStack::getContextKey() {
KeyStr->Context = Binary->getExpandedContext(Stack, KeyStr->WasLeafInlined);
if (KeyStr->Context.empty())
return nullptr;
KeyStr->genHashCode();
return KeyStr;
}

Expand All @@ -143,8 +142,6 @@ std::shared_ptr<ProbeBasedCtxKey> ProbeStack::getContextKey() {
ProbeBasedKey->Probes);
CSProfileGenerator::trimContext<const MCDecodedPseudoProbe *>(
ProbeBasedKey->Probes);

ProbeBasedKey->genHashCode();
return ProbeBasedKey;
}

Expand Down Expand Up @@ -802,7 +799,6 @@ void UnsymbolizedProfileReader::readUnsymbolizedProfile(StringRef FileName) {
SampleContext::createCtxVectorFromStr(*I.first, Key->Context);
TraceIt.advance();
}
Key->genHashCode();
auto Ret =
SampleCounters.emplace(Hashable<ContextKey>(Key), SampleCounter());
readSampleCounters(TraceIt, Ret.first->second);
Expand Down Expand Up @@ -851,7 +847,6 @@ void PerfScriptReader::generateUnsymbolizedProfile() {
"Sample counter map should be empty before raw profile generation");
std::shared_ptr<StringBasedCtxKey> Key =
std::make_shared<StringBasedCtxKey>();
Key->genHashCode();
SampleCounters.emplace(Hashable<ContextKey>(Key), SampleCounter());
for (const auto &Item : AggregatedSamples) {
const PerfSample *Sample = Item.first.getPtr();
Expand Down
13 changes: 10 additions & 3 deletions llvm/tools/llvm-profgen/PerfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ struct UnwindState {
struct ContextKey {
uint64_t HashCode = 0;
virtual ~ContextKey() = default;
uint64_t getHashCode() const { return HashCode; }
uint64_t getHashCode() {
if (HashCode == 0)
genHashCode();
return HashCode;
}
virtual void genHashCode() = 0;
virtual bool isEqual(const ContextKey *K) const {
return HashCode == K->HashCode;
};
Expand All @@ -341,7 +346,9 @@ struct StringBasedCtxKey : public ContextKey {
return Context == Other->Context;
}

void genHashCode() { HashCode = hash_value(SampleContextFrames(Context)); }
void genHashCode() override {
HashCode = hash_value(SampleContextFrames(Context));
}
};

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

void genHashCode() {
void genHashCode() override {
for (const auto *P : Probes) {
HashCode = hash_combine(HashCode, P);
}
Expand Down

0 comments on commit dc9f037

Please sign in to comment.