Skip to content

Commit 2186c95

Browse files
[NFCI]Add SampleRecord::serialize and LineLocation::serialize to simplify FunctionSamples serialization (#141669)
1 parent e210dc8 commit 2186c95

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
#define LLVM_PROFILEDATA_SAMPLEPROF_H
1616

1717
#include "llvm/ADT/DenseSet.h"
18+
#include "llvm/ADT/MapVector.h"
1819
#include "llvm/ADT/SmallVector.h"
1920
#include "llvm/ADT/StringExtras.h"
2021
#include "llvm/ADT/StringRef.h"
2122
#include "llvm/IR/Function.h"
2223
#include "llvm/IR/GlobalValue.h"
2324
#include "llvm/ProfileData/FunctionId.h"
25+
#include "llvm/ProfileData/HashKeyMap.h"
2426
#include "llvm/Support/Allocator.h"
2527
#include "llvm/Support/Debug.h"
2628
#include "llvm/Support/ErrorOr.h"
2729
#include "llvm/Support/MathExtras.h"
28-
#include "llvm/ProfileData/HashKeyMap.h"
2930
#include <algorithm>
3031
#include <cstdint>
3132
#include <list>
@@ -283,6 +284,9 @@ struct LineLocation {
283284
void print(raw_ostream &OS) const;
284285
void dump() const;
285286

287+
// Serialize the line location to the output stream using ULEB128 encoding.
288+
void serialize(raw_ostream &OS);
289+
286290
bool operator<(const LineLocation &O) const {
287291
return LineOffset < O.LineOffset ||
288292
(LineOffset == O.LineOffset && Discriminator < O.Discriminator);
@@ -427,6 +431,11 @@ class SampleRecord {
427431
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1);
428432
void print(raw_ostream &OS, unsigned Indent) const;
429433
void dump() const;
434+
/// Serialize the sample record to the output stream using ULEB128 encoding.
435+
/// The \p NameTable is used to map function names to their IDs.
436+
std::error_code
437+
serialize(raw_ostream &OS,
438+
const MapVector<FunctionId, uint32_t> &NameTable) const;
430439

431440
bool operator==(const SampleRecord &Other) const {
432441
return NumSamples == Other.NumSamples && CallTargets == Other.CallTargets;

llvm/lib/ProfileData/SampleProf.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/Support/Compiler.h"
2121
#include "llvm/Support/Debug.h"
2222
#include "llvm/Support/ErrorHandling.h"
23+
#include "llvm/Support/LEB128.h"
2324
#include "llvm/Support/raw_ostream.h"
2425
#include <string>
2526
#include <system_error>
@@ -126,10 +127,34 @@ sampleprof_error SampleRecord::merge(const SampleRecord &Other,
126127
return Result;
127128
}
128129

130+
std::error_code SampleRecord::serialize(
131+
raw_ostream &OS, const MapVector<FunctionId, uint32_t> &NameTable) const {
132+
encodeULEB128(getSamples(), OS);
133+
encodeULEB128(getCallTargets().size(), OS);
134+
for (const auto &J : getSortedCallTargets()) {
135+
FunctionId Callee = J.first;
136+
uint64_t CalleeSamples = J.second;
137+
if (auto NameIndexIter = NameTable.find(Callee);
138+
NameIndexIter != NameTable.end()) {
139+
encodeULEB128(NameIndexIter->second, OS);
140+
} else {
141+
// If the callee is not in the name table, we cannot serialize it.
142+
return sampleprof_error::truncated_name_table;
143+
}
144+
encodeULEB128(CalleeSamples, OS);
145+
}
146+
return sampleprof_error::success;
147+
}
148+
129149
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
130150
LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
131151
#endif
132152

153+
void LineLocation::serialize(raw_ostream &OS) {
154+
encodeULEB128(LineOffset, OS);
155+
encodeULEB128(Discriminator, OS);
156+
}
157+
133158
/// Print the sample record to the stream \p OS indented by \p Indent.
134159
void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
135160
OS << NumSamples;

llvm/lib/ProfileData/SampleProfWriter.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -827,17 +827,8 @@ std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
827827
for (const auto &I : S.getBodySamples()) {
828828
LineLocation Loc = I.first;
829829
const SampleRecord &Sample = I.second;
830-
encodeULEB128(Loc.LineOffset, OS);
831-
encodeULEB128(Loc.Discriminator, OS);
832-
encodeULEB128(Sample.getSamples(), OS);
833-
encodeULEB128(Sample.getCallTargets().size(), OS);
834-
for (const auto &J : Sample.getSortedCallTargets()) {
835-
FunctionId Callee = J.first;
836-
uint64_t CalleeSamples = J.second;
837-
if (std::error_code EC = writeNameIdx(Callee))
838-
return EC;
839-
encodeULEB128(CalleeSamples, OS);
840-
}
830+
Loc.serialize(OS);
831+
Sample.serialize(OS, getNameTable());
841832
}
842833

843834
// Recursively emit all the callsite samples.
@@ -849,8 +840,7 @@ std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
849840
for (const auto &FS : J.second) {
850841
LineLocation Loc = J.first;
851842
const FunctionSamples &CalleeSamples = FS.second;
852-
encodeULEB128(Loc.LineOffset, OS);
853-
encodeULEB128(Loc.Discriminator, OS);
843+
Loc.serialize(OS);
854844
if (std::error_code EC = writeBody(CalleeSamples))
855845
return EC;
856846
}

0 commit comments

Comments
 (0)