diff options
author | Mingming Liu <mingmingl@google.com> | 2025-05-27 16:32:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-27 16:32:35 -0700 |
commit | 2186c95a6f59d1b87c8becea2af6e437f02bf7cb (patch) | |
tree | 8ba67d6a72267842ba12d02b6dd8a9b538c79255 /llvm/lib/ProfileData/SampleProf.cpp | |
parent | e210dc8cf4f9e919ef02b04d27e4dacb0faf656e (diff) | |
download | llvm-2186c95a6f59d1b87c8becea2af6e437f02bf7cb.zip llvm-2186c95a6f59d1b87c8becea2af6e437f02bf7cb.tar.gz llvm-2186c95a6f59d1b87c8becea2af6e437f02bf7cb.tar.bz2 |
[NFCI]Add SampleRecord::serialize and LineLocation::serialize to simplify FunctionSamples serialization (#141669)
Diffstat (limited to 'llvm/lib/ProfileData/SampleProf.cpp')
-rw-r--r-- | llvm/lib/ProfileData/SampleProf.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp index 4d48de9..0c47df3 100644 --- a/llvm/lib/ProfileData/SampleProf.cpp +++ b/llvm/lib/ProfileData/SampleProf.cpp @@ -20,6 +20,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/LEB128.h" #include "llvm/Support/raw_ostream.h" #include <string> #include <system_error> @@ -126,10 +127,34 @@ sampleprof_error SampleRecord::merge(const SampleRecord &Other, return Result; } +std::error_code SampleRecord::serialize( + raw_ostream &OS, const MapVector<FunctionId, uint32_t> &NameTable) const { + encodeULEB128(getSamples(), OS); + encodeULEB128(getCallTargets().size(), OS); + for (const auto &J : getSortedCallTargets()) { + FunctionId Callee = J.first; + uint64_t CalleeSamples = J.second; + if (auto NameIndexIter = NameTable.find(Callee); + NameIndexIter != NameTable.end()) { + encodeULEB128(NameIndexIter->second, OS); + } else { + // If the callee is not in the name table, we cannot serialize it. + return sampleprof_error::truncated_name_table; + } + encodeULEB128(CalleeSamples, OS); + } + return sampleprof_error::success; +} + #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } #endif +void LineLocation::serialize(raw_ostream &OS) { + encodeULEB128(LineOffset, OS); + encodeULEB128(Discriminator, OS); +} + /// Print the sample record to the stream \p OS indented by \p Indent. void SampleRecord::print(raw_ostream &OS, unsigned Indent) const { OS << NumSamples; |