Skip to content

Commit 3a807b5

Browse files
authored
[llvm-profdata] Move error handling logic out of normal input's code path (#67177)
Based on disassembly and profiling results, it looks like the cost of std::error_code and llvm::ErrorOr<> are non-trivial, as it involves virtual function calls that are not optimized in -O3. This patch moves error handling logic into the conditional branch in error checking, only generates std::error_code on actual error instead of the default case to generate sampleprof_error::success. In the next patch I am looking to change the API to uses a plain old enum for error checking instead, because based on profiling results error handling related code accounts for 7-8% of the profile loading time even if there's no error at all.
1 parent 78c8ab5 commit 3a807b5

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

llvm/lib/ProfileData/SampleProfReader.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,14 @@ bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
465465

466466
template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
467467
unsigned NumBytesRead = 0;
468-
std::error_code EC;
469468
uint64_t Val = decodeULEB128(Data, &NumBytesRead);
470469

471-
if (Val > std::numeric_limits<T>::max())
472-
EC = sampleprof_error::malformed;
473-
else if (Data + NumBytesRead > End)
474-
EC = sampleprof_error::truncated;
475-
else
476-
EC = sampleprof_error::success;
477-
478-
if (EC) {
470+
if (Val > std::numeric_limits<T>::max()) {
471+
std::error_code EC = sampleprof_error::malformed;
472+
reportError(0, EC.message());
473+
return EC;
474+
} else if (Data + NumBytesRead > End) {
475+
std::error_code EC = sampleprof_error::truncated;
479476
reportError(0, EC.message());
480477
return EC;
481478
}
@@ -485,10 +482,9 @@ template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
485482
}
486483

487484
ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
488-
std::error_code EC;
489485
StringRef Str(reinterpret_cast<const char *>(Data));
490486
if (Data + Str.size() + 1 > End) {
491-
EC = sampleprof_error::truncated;
487+
std::error_code EC = sampleprof_error::truncated;
492488
reportError(0, EC.message());
493489
return EC;
494490
}
@@ -499,10 +495,8 @@ ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
499495

500496
template <typename T>
501497
ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
502-
std::error_code EC;
503-
504498
if (Data + sizeof(T) > End) {
505-
EC = sampleprof_error::truncated;
499+
std::error_code EC = sampleprof_error::truncated;
506500
reportError(0, EC.message());
507501
return EC;
508502
}
@@ -514,7 +508,6 @@ ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
514508

515509
template <typename T>
516510
inline ErrorOr<size_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
517-
std::error_code EC;
518511
auto Idx = readNumber<size_t>();
519512
if (std::error_code EC = Idx.getError())
520513
return EC;

0 commit comments

Comments
 (0)