https://github.com/snehasish updated https://github.com/llvm/llvm-project/pull/140503
>From 1f2f80df22f154bc317ae9298cf20c9f91574f55 Mon Sep 17 00:00:00 2001 From: Snehasish Kumar <snehasi...@google.com> Date: Fri, 16 May 2025 23:41:29 -0700 Subject: [PATCH 1/2] [NFC][MemProf] Move IndexedMemProfData to its own header. --- .../llvm/ProfileData/IndexedMemProfData.h | 70 ++++++++++++++++++- .../llvm/ProfileData/InstrProfWriter.h | 2 +- llvm/include/llvm/ProfileData/MemProf.h | 51 -------------- .../llvm/ProfileData/MemProfRadixTree.h | 1 + llvm/include/llvm/ProfileData/MemProfReader.h | 1 + llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 1 + llvm/lib/ProfileData/InstrProfWriter.cpp | 1 - llvm/lib/ProfileData/MemProf.cpp | 13 ---- llvm/unittests/ProfileData/InstrProfTest.cpp | 1 + llvm/unittests/ProfileData/MemProfTest.cpp | 4 +- .../Instrumentation/MemProfUseTest.cpp | 1 + 11 files changed, 76 insertions(+), 70 deletions(-) diff --git a/llvm/include/llvm/ProfileData/IndexedMemProfData.h b/llvm/include/llvm/ProfileData/IndexedMemProfData.h index 3c6c329d1c49d..94a16227477cb 100644 --- a/llvm/include/llvm/ProfileData/IndexedMemProfData.h +++ b/llvm/include/llvm/ProfileData/IndexedMemProfData.h @@ -6,18 +6,84 @@ // //===----------------------------------------------------------------------===// // -// MemProf data is serialized in writeMemProf provided in this header file. +// This file implements IndexedMemProfData, a data structure to hold MemProf +// in a space optimized format. It also provides utility methods for writing +// MemProf data. // //===----------------------------------------------------------------------===// +#ifndef LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H +#define LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H + #include "llvm/ProfileData/InstrProf.h" #include "llvm/ProfileData/MemProf.h" namespace llvm { +namespace memprof { +struct IndexedMemProfData { + // A map to hold memprof data per function. The lower 64 bits obtained from + // the md5 hash of the function name is used to index into the map. + llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records; + + // A map to hold frame id to frame mappings. The mappings are used to + // convert IndexedMemProfRecord to MemProfRecords with frame information + // inline. + llvm::MapVector<FrameId, Frame> Frames; + + // A map to hold call stack id to call stacks. + llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks; + + FrameId addFrame(const Frame &F) { + const FrameId Id = hashFrame(F); + Frames.try_emplace(Id, F); + return Id; + } + + CallStackId addCallStack(ArrayRef<FrameId> CS) { + CallStackId CSId = hashCallStack(CS); + CallStacks.try_emplace(CSId, CS); + return CSId; + } + + CallStackId addCallStack(SmallVector<FrameId> &&CS) { + CallStackId CSId = hashCallStack(CS); + CallStacks.try_emplace(CSId, std::move(CS)); + return CSId; + } + +private: + // Return a hash value based on the contents of the frame. Here we use a + // cryptographic hash function to minimize the chance of hash collisions. We + // do persist FrameIds as part of memprof formats up to Version 2, inclusive. + // However, the deserializer never calls this function; it uses FrameIds + // merely as keys to look up Frames proper. + FrameId hashFrame(const Frame &F) const { + llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> + HashBuilder; + HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame); + llvm::BLAKE3Result<8> Hash = HashBuilder.final(); + FrameId Id; + std::memcpy(&Id, Hash.data(), sizeof(Hash)); + return Id; + } + + // Compute a CallStackId for a given call stack. + CallStackId hashCallStack(ArrayRef<FrameId> CS) const { + llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> + HashBuilder; + for (FrameId F : CS) + HashBuilder.add(F); + llvm::BLAKE3Result<8> Hash = HashBuilder.final(); + CallStackId CSId; + std::memcpy(&CSId, Hash.data(), sizeof(Hash)); + return CSId; +} +}; +} // namespace memprof // Write the MemProf data to OS. Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData, memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema); - } // namespace llvm +#endif diff --git a/llvm/include/llvm/ProfileData/InstrProfWriter.h b/llvm/include/llvm/ProfileData/InstrProfWriter.h index 67d85daa81623..16d2ef3fab3e3 100644 --- a/llvm/include/llvm/ProfileData/InstrProfWriter.h +++ b/llvm/include/llvm/ProfileData/InstrProfWriter.h @@ -20,7 +20,7 @@ #include "llvm/IR/GlobalValue.h" #include "llvm/Object/BuildID.h" #include "llvm/ProfileData/InstrProf.h" -#include "llvm/ProfileData/MemProf.h" +#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/Support/Error.h" #include <cstdint> #include <memory> diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h index 215102c131fff..ce5cd5ee4856b 100644 --- a/llvm/include/llvm/ProfileData/MemProf.h +++ b/llvm/include/llvm/ProfileData/MemProf.h @@ -842,57 +842,6 @@ struct LineLocation { // A pair of a call site location and its corresponding callee GUID. using CallEdgeTy = std::pair<LineLocation, uint64_t>; - -struct IndexedMemProfData { - // A map to hold memprof data per function. The lower 64 bits obtained from - // the md5 hash of the function name is used to index into the map. - llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records; - - // A map to hold frame id to frame mappings. The mappings are used to - // convert IndexedMemProfRecord to MemProfRecords with frame information - // inline. - llvm::MapVector<FrameId, Frame> Frames; - - // A map to hold call stack id to call stacks. - llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks; - - FrameId addFrame(const Frame &F) { - const FrameId Id = hashFrame(F); - Frames.try_emplace(Id, F); - return Id; - } - - CallStackId addCallStack(ArrayRef<FrameId> CS) { - CallStackId CSId = hashCallStack(CS); - CallStacks.try_emplace(CSId, CS); - return CSId; - } - - CallStackId addCallStack(SmallVector<FrameId> &&CS) { - CallStackId CSId = hashCallStack(CS); - CallStacks.try_emplace(CSId, std::move(CS)); - return CSId; - } - -private: - // Return a hash value based on the contents of the frame. Here we use a - // cryptographic hash function to minimize the chance of hash collisions. We - // do persist FrameIds as part of memprof formats up to Version 2, inclusive. - // However, the deserializer never calls this function; it uses FrameIds - // merely as keys to look up Frames proper. - FrameId hashFrame(const Frame &F) const { - llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> - HashBuilder; - HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame); - llvm::BLAKE3Result<8> Hash = HashBuilder.final(); - FrameId Id; - std::memcpy(&Id, Hash.data(), sizeof(Hash)); - return Id; - } - - // Compute a CallStackId for a given call stack. - CallStackId hashCallStack(ArrayRef<FrameId> CS) const; -}; } // namespace memprof } // namespace llvm diff --git a/llvm/include/llvm/ProfileData/MemProfRadixTree.h b/llvm/include/llvm/ProfileData/MemProfRadixTree.h index 7afa660885400..ec3d2a61a76e7 100644 --- a/llvm/include/llvm/ProfileData/MemProfRadixTree.h +++ b/llvm/include/llvm/ProfileData/MemProfRadixTree.h @@ -14,6 +14,7 @@ #define LLVM_PROFILEDATA_MEMPROFRADIXTREE_H #include "llvm/ProfileData/MemProf.h" +#include "llvm/ProfileData/IndexedMemProfData.h" namespace llvm { namespace memprof { diff --git a/llvm/include/llvm/ProfileData/MemProfReader.h b/llvm/include/llvm/ProfileData/MemProfReader.h index 9aa55554fdf72..130493ec77c08 100644 --- a/llvm/include/llvm/ProfileData/MemProfReader.h +++ b/llvm/include/llvm/ProfileData/MemProfReader.h @@ -21,6 +21,7 @@ #include "llvm/IR/GlobalValue.h" #include "llvm/Object/Binary.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/ProfileData/InstrProfReader.h" #include "llvm/ProfileData/MemProfData.inc" #include "llvm/ProfileData/MemProfRadixTree.h" diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index f8748babb1625..427a161bcef08 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -62,6 +62,7 @@ #include "llvm/MC/TargetRegistry.h" #include "llvm/Object/IRSymtab.h" #include "llvm/ProfileData/MemProf.h" +#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/ProfileData/MemProfRadixTree.h" #include "llvm/Support/AtomicOrdering.h" #include "llvm/Support/Casting.h" diff --git a/llvm/lib/ProfileData/InstrProfWriter.cpp b/llvm/lib/ProfileData/InstrProfWriter.cpp index 39451c3d64870..2c6640eedebd9 100644 --- a/llvm/lib/ProfileData/InstrProfWriter.cpp +++ b/llvm/lib/ProfileData/InstrProfWriter.cpp @@ -18,7 +18,6 @@ #include "llvm/IR/ProfileSummary.h" #include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/ProfileData/InstrProf.h" -#include "llvm/ProfileData/MemProf.h" #include "llvm/ProfileData/ProfileCommon.h" #include "llvm/Support/Compression.h" #include "llvm/Support/Endian.h" diff --git a/llvm/lib/ProfileData/MemProf.cpp b/llvm/lib/ProfileData/MemProf.cpp index 795e97bee38f5..9955e6a6b2641 100644 --- a/llvm/lib/ProfileData/MemProf.cpp +++ b/llvm/lib/ProfileData/MemProf.cpp @@ -3,10 +3,8 @@ #include "llvm/IR/Function.h" #include "llvm/ProfileData/InstrProf.h" #include "llvm/ProfileData/SampleProf.h" -#include "llvm/Support/BLAKE3.h" #include "llvm/Support/Endian.h" #include "llvm/Support/EndianStream.h" -#include "llvm/Support/HashBuilder.h" namespace llvm { namespace memprof { @@ -384,16 +382,5 @@ Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) { Buffer = Ptr; return Result; } - -CallStackId IndexedMemProfData::hashCallStack(ArrayRef<FrameId> CS) const { - llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> - HashBuilder; - for (FrameId F : CS) - HashBuilder.add(F); - llvm::BLAKE3Result<8> Hash = HashBuilder.final(); - CallStackId CSId; - std::memcpy(&CSId, Hash.data(), sizeof(Hash)); - return CSId; -} } // namespace memprof } // namespace llvm diff --git a/llvm/unittests/ProfileData/InstrProfTest.cpp b/llvm/unittests/ProfileData/InstrProfTest.cpp index 439c602672718..7ac0f67e1273d 100644 --- a/llvm/unittests/ProfileData/InstrProfTest.cpp +++ b/llvm/unittests/ProfileData/InstrProfTest.cpp @@ -15,6 +15,7 @@ #include "llvm/ProfileData/InstrProfReader.h" #include "llvm/ProfileData/InstrProfWriter.h" #include "llvm/ProfileData/MemProf.h" +#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/ProfileData/MemProfData.inc" #include "llvm/ProfileData/MemProfRadixTree.h" #include "llvm/Support/Compression.h" diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp index 201ee2d7272cf..9c5bcc8160997 100644 --- a/llvm/unittests/ProfileData/MemProfTest.cpp +++ b/llvm/unittests/ProfileData/MemProfTest.cpp @@ -6,7 +6,6 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ProfileData/MemProf.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/STLForwardCompat.h" @@ -14,10 +13,11 @@ #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" #include "llvm/IR/Value.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/ProfileData/IndexedMemProfData.h" +#include "llvm/ProfileData/MemProf.h" #include "llvm/ProfileData/MemProfData.inc" #include "llvm/ProfileData/MemProfRadixTree.h" #include "llvm/ProfileData/MemProfReader.h" -#include "llvm/ProfileData/MemProfYAML.h" #include "llvm/Support/raw_ostream.h" #include "gmock/gmock.h" #include "gtest/gtest.h" diff --git a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp index 2ed32c6ea0eac..23e9a0f43edf4 100644 --- a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp +++ b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp @@ -14,6 +14,7 @@ #include "llvm/ProfileData/InstrProfReader.h" #include "llvm/ProfileData/InstrProfWriter.h" #include "llvm/ProfileData/MemProf.h" +#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Testing/Support/Error.h" #include "llvm/Transforms/Instrumentation/MemProfiler.h" >From 02c867e6e9805ee2b0736f3f0dc9f0094b89d022 Mon Sep 17 00:00:00 2001 From: Snehasish Kumar <snehasi...@google.com> Date: Mon, 19 May 2025 15:14:33 -0700 Subject: [PATCH 2/2] Fix formatting. --- .../llvm/ProfileData/IndexedMemProfData.h | 18 +++++++++--------- .../include/llvm/ProfileData/InstrProfWriter.h | 2 +- .../llvm/ProfileData/MemProfRadixTree.h | 2 +- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 2 +- llvm/unittests/ProfileData/InstrProfTest.cpp | 2 +- llvm/unittests/ProfileData/MemProfTest.cpp | 2 +- .../Instrumentation/MemProfUseTest.cpp | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/llvm/include/llvm/ProfileData/IndexedMemProfData.h b/llvm/include/llvm/ProfileData/IndexedMemProfData.h index 94a16227477cb..f33b160e0b6a9 100644 --- a/llvm/include/llvm/ProfileData/IndexedMemProfData.h +++ b/llvm/include/llvm/ProfileData/IndexedMemProfData.h @@ -69,15 +69,15 @@ struct IndexedMemProfData { // Compute a CallStackId for a given call stack. CallStackId hashCallStack(ArrayRef<FrameId> CS) const { - llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> - HashBuilder; - for (FrameId F : CS) - HashBuilder.add(F); - llvm::BLAKE3Result<8> Hash = HashBuilder.final(); - CallStackId CSId; - std::memcpy(&CSId, Hash.data(), sizeof(Hash)); - return CSId; -} + llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> + HashBuilder; + for (FrameId F : CS) + HashBuilder.add(F); + llvm::BLAKE3Result<8> Hash = HashBuilder.final(); + CallStackId CSId; + std::memcpy(&CSId, Hash.data(), sizeof(Hash)); + return CSId; + } }; } // namespace memprof diff --git a/llvm/include/llvm/ProfileData/InstrProfWriter.h b/llvm/include/llvm/ProfileData/InstrProfWriter.h index 16d2ef3fab3e3..b72c901dbb5b2 100644 --- a/llvm/include/llvm/ProfileData/InstrProfWriter.h +++ b/llvm/include/llvm/ProfileData/InstrProfWriter.h @@ -19,8 +19,8 @@ #include "llvm/ADT/StringMap.h" #include "llvm/IR/GlobalValue.h" #include "llvm/Object/BuildID.h" -#include "llvm/ProfileData/InstrProf.h" #include "llvm/ProfileData/IndexedMemProfData.h" +#include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/Error.h" #include <cstdint> #include <memory> diff --git a/llvm/include/llvm/ProfileData/MemProfRadixTree.h b/llvm/include/llvm/ProfileData/MemProfRadixTree.h index ec3d2a61a76e7..fd6dce4536c6b 100644 --- a/llvm/include/llvm/ProfileData/MemProfRadixTree.h +++ b/llvm/include/llvm/ProfileData/MemProfRadixTree.h @@ -13,8 +13,8 @@ #ifndef LLVM_PROFILEDATA_MEMPROFRADIXTREE_H #define LLVM_PROFILEDATA_MEMPROFRADIXTREE_H -#include "llvm/ProfileData/MemProf.h" #include "llvm/ProfileData/IndexedMemProfData.h" +#include "llvm/ProfileData/MemProf.h" namespace llvm { namespace memprof { diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 427a161bcef08..f8d80c1fc7798 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -61,8 +61,8 @@ #include "llvm/MC/StringTableBuilder.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Object/IRSymtab.h" -#include "llvm/ProfileData/MemProf.h" #include "llvm/ProfileData/IndexedMemProfData.h" +#include "llvm/ProfileData/MemProf.h" #include "llvm/ProfileData/MemProfRadixTree.h" #include "llvm/Support/AtomicOrdering.h" #include "llvm/Support/Casting.h" diff --git a/llvm/unittests/ProfileData/InstrProfTest.cpp b/llvm/unittests/ProfileData/InstrProfTest.cpp index 7ac0f67e1273d..dcdacb903791d 100644 --- a/llvm/unittests/ProfileData/InstrProfTest.cpp +++ b/llvm/unittests/ProfileData/InstrProfTest.cpp @@ -12,10 +12,10 @@ #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" +#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/ProfileData/InstrProfReader.h" #include "llvm/ProfileData/InstrProfWriter.h" #include "llvm/ProfileData/MemProf.h" -#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/ProfileData/MemProfData.inc" #include "llvm/ProfileData/MemProfRadixTree.h" #include "llvm/Support/Compression.h" diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp index 9c5bcc8160997..7a2c4aff32c53 100644 --- a/llvm/unittests/ProfileData/MemProfTest.cpp +++ b/llvm/unittests/ProfileData/MemProfTest.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ProfileData/MemProf.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/STLForwardCompat.h" @@ -14,7 +15,6 @@ #include "llvm/IR/Value.h" #include "llvm/Object/ObjectFile.h" #include "llvm/ProfileData/IndexedMemProfData.h" -#include "llvm/ProfileData/MemProf.h" #include "llvm/ProfileData/MemProfData.inc" #include "llvm/ProfileData/MemProfRadixTree.h" #include "llvm/ProfileData/MemProfReader.h" diff --git a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp index 23e9a0f43edf4..c4f38863e6f0f 100644 --- a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp +++ b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp @@ -11,10 +11,10 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Passes/PassBuilder.h" +#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/ProfileData/InstrProfReader.h" #include "llvm/ProfileData/InstrProfWriter.h" #include "llvm/ProfileData/MemProf.h" -#include "llvm/ProfileData/IndexedMemProfData.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Testing/Support/Error.h" #include "llvm/Transforms/Instrumentation/MemProfiler.h" _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits