Skip to content

Commit

Permalink
[CSSPGO] Fix redundant reading of profile metadata (#129609)
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
wlei-llvm authored Mar 4, 2025
1 parent ee4bc5a commit d38380d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/ProfileData/SampleProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
std::error_code readSecHdrTable();

std::error_code readFuncMetadata(bool ProfileHasAttribute,
SampleProfileMap &Profiles);
DenseSet<FunctionSamples *> &Profiles);
std::error_code readFuncMetadata(bool ProfileHasAttribute);
std::error_code readFuncMetadata(bool ProfileHasAttribute,
FunctionSamples *FProfile);
Expand Down
20 changes: 14 additions & 6 deletions llvm/lib/ProfileData/SampleProfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,23 @@ bool SampleProfileReaderExtBinaryBase::useFuncOffsetList() const {
std::error_code
SampleProfileReaderExtBinaryBase::read(const DenseSet<StringRef> &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<FunctionSamples *> 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;
}
Expand Down Expand Up @@ -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<FunctionSamples *> &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;
Expand Down

0 comments on commit d38380d

Please sign in to comment.