Skip to content

Commit 20daa73

Browse files
[NFC]Codestyle changes for SampleFDO library (#147840)
* Introduce an error code for illegal_line_offset in sampleprof_error namespace, and use it for line offset parsing error. * Add `const` for `LineLocation::serialize`. * Use structured binding, make_first/second_range in loops. I'm working on a [sample-profile format change](https://github.com/llvm/llvm-project/compare/users/mingmingl-llvm/samplefdo-profile-format) to extend SampleFDO profile with vtable profiles. And this change splits the non-functional changes.
1 parent cd65f8b commit 20daa73

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ enum class sampleprof_error {
6161
ostream_seek_unsupported,
6262
uncompress_failed,
6363
zlib_unavailable,
64-
hash_mismatch
64+
hash_mismatch,
65+
illegal_line_offset
6566
};
6667

6768
inline std::error_code make_error_code(sampleprof_error E) {
@@ -286,7 +287,7 @@ struct LineLocation {
286287
LLVM_ABI void dump() const;
287288

288289
// Serialize the line location to the output stream using ULEB128 encoding.
289-
LLVM_ABI void serialize(raw_ostream &OS);
290+
LLVM_ABI void serialize(raw_ostream &OS) const;
290291

291292
bool operator<(const LineLocation &O) const {
292293
return std::tie(LineOffset, Discriminator) <

llvm/lib/ProfileData/SampleProf.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class SampleProfErrorCategoryType : public std::error_category {
9191
return "Zlib is unavailable";
9292
case sampleprof_error::hash_mismatch:
9393
return "Function hash mismatch";
94+
case sampleprof_error::illegal_line_offset:
95+
return "Illegal line offset in sample profile data";
9496
}
9597
llvm_unreachable("A value of sampleprof_error has no message.");
9698
}
@@ -150,7 +152,7 @@ std::error_code SampleRecord::serialize(
150152
LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
151153
#endif
152154

153-
void LineLocation::serialize(raw_ostream &OS) {
155+
void LineLocation::serialize(raw_ostream &OS) const {
154156
encodeULEB128(LineOffset, OS);
155157
encodeULEB128(Discriminator, OS);
156158
}
@@ -203,12 +205,14 @@ void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
203205
OS << "Samples collected in inlined callsites {\n";
204206
SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
205207
CallsiteSamples);
206-
for (const auto &CS : SortedCallsiteSamples.get()) {
207-
for (const auto &FS : CS->second) {
208+
for (const auto *Element : SortedCallsiteSamples.get()) {
209+
// Element is a pointer to a pair of LineLocation and FunctionSamplesMap.
210+
const auto &[Loc, FunctionSampleMap] = *Element;
211+
for (const FunctionSamples &FuncSample :
212+
llvm::make_second_range(FunctionSampleMap)) {
208213
OS.indent(Indent + 2);
209-
OS << CS->first << ": inlined callee: " << FS.second.getFunction()
210-
<< ": ";
211-
FS.second.print(OS, Indent + 4);
214+
OS << Loc << ": inlined callee: " << FuncSample.getFunction() << ": ";
215+
FuncSample.print(OS, Indent + 4);
212216
}
213217
}
214218
OS.indent(Indent);

llvm/lib/ProfileData/SampleProfReader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
609609
return EC;
610610

611611
if (!isOffsetLegal(*LineOffset)) {
612-
return std::error_code();
612+
return sampleprof_error::illegal_line_offset;
613613
}
614614

615615
auto Discriminator = readNumber<uint64_t>();
@@ -1236,7 +1236,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readCSNameTableSec() {
12361236
return EC;
12371237

12381238
if (!isOffsetLegal(*LineOffset))
1239-
return std::error_code();
1239+
return sampleprof_error::illegal_line_offset;
12401240

12411241
auto Discriminator = readNumber<uint64_t>();
12421242
if (std::error_code EC = Discriminator.getError())

llvm/lib/ProfileData/SampleProfWriter.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -592,16 +592,18 @@ std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
592592
SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
593593
S.getCallsiteSamples());
594594
Indent += 1;
595-
for (const auto &I : SortedCallsiteSamples.get())
596-
for (const auto &FS : I->second) {
597-
LineLocation Loc = I->first;
598-
const FunctionSamples &CalleeSamples = FS.second;
595+
for (const auto *Element : SortedCallsiteSamples.get()) {
596+
// Element is a pointer to a pair of LineLocation and FunctionSamplesMap.
597+
const auto &[Loc, FunctionSamplesMap] = *Element;
598+
for (const FunctionSamples &CalleeSamples :
599+
make_second_range(FunctionSamplesMap)) {
599600
OS.indent(Indent);
600601
Loc.print(OS);
601602
OS << ": ";
602603
if (std::error_code EC = writeSample(CalleeSamples))
603604
return EC;
604605
}
606+
}
605607
Indent -= 1;
606608

607609
if (FunctionSamples::ProfileIsProbeBased) {
@@ -836,12 +838,11 @@ std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
836838
for (const auto &J : S.getCallsiteSamples())
837839
NumCallsites += J.second.size();
838840
encodeULEB128(NumCallsites, OS);
839-
for (const auto &J : S.getCallsiteSamples())
840-
for (const auto &FS : J.second) {
841-
LineLocation Loc = J.first;
842-
const FunctionSamples &CalleeSamples = FS.second;
841+
for (const auto &[Loc, CalleeFunctionSampleMap] : S.getCallsiteSamples())
842+
for (const auto &FunctionSample :
843+
llvm::make_second_range(CalleeFunctionSampleMap)) {
843844
Loc.serialize(OS);
844-
if (std::error_code EC = writeBody(CalleeSamples))
845+
if (std::error_code EC = writeBody(FunctionSample))
845846
return EC;
846847
}
847848

0 commit comments

Comments
 (0)