-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[NFCI]Add SampleRecord::serialize and LineLocation::serialize to simplify FunctionSamples serialization #141669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mingmingl-llvm
merged 2 commits into
main
from
users/mingmingl-llvm/sprof-serialize-mini
May 27, 2025
Merged
[NFCI]Add SampleRecord::serialize and LineLocation::serialize to simplify FunctionSamples serialization #141669
mingmingl-llvm
merged 2 commits into
main
from
users/mingmingl-llvm/sprof-serialize-mini
May 27, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… simplify FunctionSamples serialization
@llvm/pr-subscribers-pgo Author: Mingming Liu (mingmingl-llvm) ChangesFull diff: https://github.com/llvm/llvm-project/pull/141669.diff 3 Files Affected:
diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h
index c7155b3f982c6..3704de6e49f40 100644
--- a/llvm/include/llvm/ProfileData/SampleProf.h
+++ b/llvm/include/llvm/ProfileData/SampleProf.h
@@ -15,17 +15,18 @@
#define LLVM_PROFILEDATA_SAMPLEPROF_H
#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/ProfileData/FunctionId.h"
+#include "llvm/ProfileData/HashKeyMap.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MathExtras.h"
-#include "llvm/ProfileData/HashKeyMap.h"
#include <algorithm>
#include <cstdint>
#include <list>
@@ -283,6 +284,9 @@ struct LineLocation {
void print(raw_ostream &OS) const;
void dump() const;
+ // Serialize the line location to the output stream using ULEB128 encoding.
+ void serialize(raw_ostream &OS);
+
bool operator<(const LineLocation &O) const {
return LineOffset < O.LineOffset ||
(LineOffset == O.LineOffset && Discriminator < O.Discriminator);
@@ -427,6 +431,11 @@ class SampleRecord {
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1);
void print(raw_ostream &OS, unsigned Indent) const;
void dump() const;
+ /// Serialize the sample record to the output stream using ULEB128 encoding.
+ /// The \p NameTable is used to map function names to their IDs.
+ std::error_code
+ serialize(raw_ostream &OS,
+ const MapVector<FunctionId, uint32_t> &NameTable) const;
bool operator==(const SampleRecord &Other) const {
return NumSamples == Other.NumSamples && CallTargets == Other.CallTargets;
diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp
index 4d48de9bc7d63..0c47df3c55dec 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;
diff --git a/llvm/lib/ProfileData/SampleProfWriter.cpp b/llvm/lib/ProfileData/SampleProfWriter.cpp
index 8b164f24e06e1..71d2f522fc295 100644
--- a/llvm/lib/ProfileData/SampleProfWriter.cpp
+++ b/llvm/lib/ProfileData/SampleProfWriter.cpp
@@ -827,17 +827,8 @@ std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
for (const auto &I : S.getBodySamples()) {
LineLocation Loc = I.first;
const SampleRecord &Sample = I.second;
- encodeULEB128(Loc.LineOffset, OS);
- encodeULEB128(Loc.Discriminator, OS);
- encodeULEB128(Sample.getSamples(), OS);
- encodeULEB128(Sample.getCallTargets().size(), OS);
- for (const auto &J : Sample.getSortedCallTargets()) {
- FunctionId Callee = J.first;
- uint64_t CalleeSamples = J.second;
- if (std::error_code EC = writeNameIdx(Callee))
- return EC;
- encodeULEB128(CalleeSamples, OS);
- }
+ Loc.serialize(OS);
+ Sample.serialize(OS, getNameTable());
}
// Recursively emit all the callsite samples.
@@ -849,8 +840,7 @@ std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
for (const auto &FS : J.second) {
LineLocation Loc = J.first;
const FunctionSamples &CalleeSamples = FS.second;
- encodeULEB128(Loc.LineOffset, OS);
- encodeULEB128(Loc.Discriminator, OS);
+ Loc.serialize(OS);
if (std::error_code EC = writeBody(CalleeSamples))
return EC;
}
|
david-xl
approved these changes
May 27, 2025
Thanks for the reviews! Test failure in |
sivan-shani
pushed a commit
to sivan-shani/llvm-project
that referenced
this pull request
Jun 3, 2025
…lify FunctionSamples serialization (llvm#141669)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.