From d38380d3d808183652c1e9be34e3a2476ed6ea70 Mon Sep 17 00:00:00 2001 From: Lei Wang Date: Tue, 4 Mar 2025 11:39:59 -0800 Subject: [PATCH] [CSSPGO] Fix redundant reading of profile metadata (#129609) Fix a build speed regression due to repeated reading of profile metadata. Before the function `readFuncMetadata(ProfileHasAttribute, Profiles)` reads the metadata for all the functions(`Profiles`), however, it's actually used for on-demand loading, it can be called for multiple times, which leads to redundant reading that causes the build speed regression. Now fix it to read the metadata only for the new loaded functions(functions in the `FuncsToUse`). --- .../llvm/ProfileData/SampleProfReader.h | 2 +- llvm/lib/ProfileData/SampleProfReader.cpp | 20 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/llvm/include/llvm/ProfileData/SampleProfReader.h b/llvm/include/llvm/ProfileData/SampleProfReader.h index 5301f23def3f3..76c7cecded629 100644 --- a/llvm/include/llvm/ProfileData/SampleProfReader.h +++ b/llvm/include/llvm/ProfileData/SampleProfReader.h @@ -777,7 +777,7 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary { std::error_code readSecHdrTable(); std::error_code readFuncMetadata(bool ProfileHasAttribute, - SampleProfileMap &Profiles); + DenseSet &Profiles); std::error_code readFuncMetadata(bool ProfileHasAttribute); std::error_code readFuncMetadata(bool ProfileHasAttribute, FunctionSamples *FProfile); diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp index 98c7844378527..d97cc479442e4 100644 --- a/llvm/lib/ProfileData/SampleProfReader.cpp +++ b/llvm/lib/ProfileData/SampleProfReader.cpp @@ -826,13 +826,23 @@ bool SampleProfileReaderExtBinaryBase::useFuncOffsetList() const { std::error_code SampleProfileReaderExtBinaryBase::read(const DenseSet &FuncsToUse, SampleProfileMap &Profiles) { + if (FuncsToUse.empty()) + return sampleprof_error::success; + Data = ProfileSecRange.first; End = ProfileSecRange.second; if (std::error_code EC = readFuncProfiles(FuncsToUse, Profiles)) return EC; End = Data; + DenseSet ProfilesToReadMetadata; + for (auto FName : FuncsToUse) { + auto I = Profiles.find(FName); + if (I != Profiles.end()) + ProfilesToReadMetadata.insert(&I->second); + } - if (std::error_code EC = readFuncMetadata(ProfileHasAttribute, Profiles)) + if (std::error_code EC = + readFuncMetadata(ProfileHasAttribute, ProfilesToReadMetadata)) return EC; return sampleprof_error::success; } @@ -1300,14 +1310,12 @@ SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute, return sampleprof_error::success; } -std::error_code -SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute, - SampleProfileMap &Profiles) { +std::error_code SampleProfileReaderExtBinaryBase::readFuncMetadata( + bool ProfileHasAttribute, DenseSet &Profiles) { if (FuncMetadataIndex.empty()) return sampleprof_error::success; - for (auto &I : Profiles) { - FunctionSamples *FProfile = &I.second; + for (auto *FProfile : Profiles) { auto R = FuncMetadataIndex.find(FProfile->getContext().getHashCode()); if (R == FuncMetadataIndex.end()) continue;