[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.
This commit is contained in:
Mingming Liu 2025-07-09 16:48:17 -07:00 committed by GitHub
parent cd65f8bf17
commit 20daa73a09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 19 deletions

View File

@ -61,7 +61,8 @@ enum class sampleprof_error {
ostream_seek_unsupported,
uncompress_failed,
zlib_unavailable,
hash_mismatch
hash_mismatch,
illegal_line_offset
};
inline std::error_code make_error_code(sampleprof_error E) {
@ -286,7 +287,7 @@ struct LineLocation {
LLVM_ABI void dump() const;
// Serialize the line location to the output stream using ULEB128 encoding.
LLVM_ABI void serialize(raw_ostream &OS);
LLVM_ABI void serialize(raw_ostream &OS) const;
bool operator<(const LineLocation &O) const {
return std::tie(LineOffset, Discriminator) <

View File

@ -91,6 +91,8 @@ class SampleProfErrorCategoryType : public std::error_category {
return "Zlib is unavailable";
case sampleprof_error::hash_mismatch:
return "Function hash mismatch";
case sampleprof_error::illegal_line_offset:
return "Illegal line offset in sample profile data";
}
llvm_unreachable("A value of sampleprof_error has no message.");
}
@ -150,7 +152,7 @@ std::error_code SampleRecord::serialize(
LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
#endif
void LineLocation::serialize(raw_ostream &OS) {
void LineLocation::serialize(raw_ostream &OS) const {
encodeULEB128(LineOffset, OS);
encodeULEB128(Discriminator, OS);
}
@ -203,12 +205,14 @@ void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
OS << "Samples collected in inlined callsites {\n";
SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
CallsiteSamples);
for (const auto &CS : SortedCallsiteSamples.get()) {
for (const auto &FS : CS->second) {
for (const auto *Element : SortedCallsiteSamples.get()) {
// Element is a pointer to a pair of LineLocation and FunctionSamplesMap.
const auto &[Loc, FunctionSampleMap] = *Element;
for (const FunctionSamples &FuncSample :
llvm::make_second_range(FunctionSampleMap)) {
OS.indent(Indent + 2);
OS << CS->first << ": inlined callee: " << FS.second.getFunction()
<< ": ";
FS.second.print(OS, Indent + 4);
OS << Loc << ": inlined callee: " << FuncSample.getFunction() << ": ";
FuncSample.print(OS, Indent + 4);
}
}
OS.indent(Indent);

View File

@ -609,7 +609,7 @@ SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
return EC;
if (!isOffsetLegal(*LineOffset)) {
return std::error_code();
return sampleprof_error::illegal_line_offset;
}
auto Discriminator = readNumber<uint64_t>();
@ -1236,7 +1236,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readCSNameTableSec() {
return EC;
if (!isOffsetLegal(*LineOffset))
return std::error_code();
return sampleprof_error::illegal_line_offset;
auto Discriminator = readNumber<uint64_t>();
if (std::error_code EC = Discriminator.getError())

View File

@ -592,16 +592,18 @@ std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
S.getCallsiteSamples());
Indent += 1;
for (const auto &I : SortedCallsiteSamples.get())
for (const auto &FS : I->second) {
LineLocation Loc = I->first;
const FunctionSamples &CalleeSamples = FS.second;
for (const auto *Element : SortedCallsiteSamples.get()) {
// Element is a pointer to a pair of LineLocation and FunctionSamplesMap.
const auto &[Loc, FunctionSamplesMap] = *Element;
for (const FunctionSamples &CalleeSamples :
make_second_range(FunctionSamplesMap)) {
OS.indent(Indent);
Loc.print(OS);
OS << ": ";
if (std::error_code EC = writeSample(CalleeSamples))
return EC;
}
}
Indent -= 1;
if (FunctionSamples::ProfileIsProbeBased) {
@ -836,12 +838,11 @@ std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
for (const auto &J : S.getCallsiteSamples())
NumCallsites += J.second.size();
encodeULEB128(NumCallsites, OS);
for (const auto &J : S.getCallsiteSamples())
for (const auto &FS : J.second) {
LineLocation Loc = J.first;
const FunctionSamples &CalleeSamples = FS.second;
for (const auto &[Loc, CalleeFunctionSampleMap] : S.getCallsiteSamples())
for (const auto &FunctionSample :
llvm::make_second_range(CalleeFunctionSampleMap)) {
Loc.serialize(OS);
if (std::error_code EC = writeBody(CalleeSamples))
if (std::error_code EC = writeBody(FunctionSample))
return EC;
}