From dc9f037955127b8421c6e2d22e5a56de3f68b90e Mon Sep 17 00:00:00 2001 From: wlei Date: Mon, 1 Nov 2021 21:35:32 -0700 Subject: [PATCH] [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 --- llvm/tools/llvm-profgen/PerfReader.cpp | 5 ----- llvm/tools/llvm-profgen/PerfReader.h | 13 ++++++++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp index 3801b12..5798783 100644 --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -129,7 +129,6 @@ std::shared_ptr FrameStack::getContextKey() { KeyStr->Context = Binary->getExpandedContext(Stack, KeyStr->WasLeafInlined); if (KeyStr->Context.empty()) return nullptr; - KeyStr->genHashCode(); return KeyStr; } @@ -143,8 +142,6 @@ std::shared_ptr ProbeStack::getContextKey() { ProbeBasedKey->Probes); CSProfileGenerator::trimContext( ProbeBasedKey->Probes); - - ProbeBasedKey->genHashCode(); return ProbeBasedKey; } @@ -802,7 +799,6 @@ void UnsymbolizedProfileReader::readUnsymbolizedProfile(StringRef FileName) { SampleContext::createCtxVectorFromStr(*I.first, Key->Context); TraceIt.advance(); } - Key->genHashCode(); auto Ret = SampleCounters.emplace(Hashable(Key), SampleCounter()); readSampleCounters(TraceIt, Ret.first->second); @@ -851,7 +847,6 @@ void PerfScriptReader::generateUnsymbolizedProfile() { "Sample counter map should be empty before raw profile generation"); std::shared_ptr Key = std::make_shared(); - Key->genHashCode(); SampleCounters.emplace(Hashable(Key), SampleCounter()); for (const auto &Item : AggregatedSamples) { const PerfSample *Sample = Item.first.getPtr(); diff --git a/llvm/tools/llvm-profgen/PerfReader.h b/llvm/tools/llvm-profgen/PerfReader.h index 0df334d..a4e2fcc 100644 --- a/llvm/tools/llvm-profgen/PerfReader.h +++ b/llvm/tools/llvm-profgen/PerfReader.h @@ -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; }; @@ -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 @@ -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); } -- 2.7.4