Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SampleFDO] Read call-graph matching recovered top-level function profile #101053

Merged
merged 10 commits into from
Sep 4, 2024
60 changes: 60 additions & 0 deletions llvm/include/llvm/ProfileData/SampleProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,22 @@ class SampleProfileReader {
return sampleprof_error::success;
}

/// Read sample profiles for the given functions. Currently it's only used for
/// extended binary format to load the profiles on-demand.
std::error_code read(const DenseSet<StringRef> &FuncsToUse) {
if (std::error_code EC = read(FuncsToUse, Profiles))
return EC;
return sampleprof_error::success;
};

/// Read sample profiles for the given functions and write them to the given
/// profile map. Currently it's only used for extended binary format to load
/// the profiles on-demand.
virtual std::error_code read(const DenseSet<StringRef> &FuncsToUse,
SampleProfileMap &Profiles) {
wlei-llvm marked this conversation as resolved.
Show resolved Hide resolved
return sampleprof_error::not_implemented;
};

/// The implementaion to read sample profiles from the associated file.
virtual std::error_code readImpl() = 0;

Expand Down Expand Up @@ -413,6 +429,16 @@ class SampleProfileReader {
if (It != Profiles.end())
return &It->second;

if (FuncNameToProfNameMap && !FuncNameToProfNameMap->empty()) {
auto R = FuncNameToProfNameMap->find(FunctionId(Fname));
if (R != FuncNameToProfNameMap->end()) {
Fname = R->second.stringRef();
auto It = Profiles.find(FunctionId(Fname));
if (It != Profiles.end())
return &It->second;
}
}
wlei-llvm marked this conversation as resolved.
Show resolved Hide resolved

if (Remapper) {
if (auto NameInProfile = Remapper->lookUpNameInProfile(Fname)) {
auto It = Profiles.find(FunctionId(*NameInProfile));
Expand Down Expand Up @@ -494,6 +520,11 @@ class SampleProfileReader {

void setModule(const Module *Mod) { M = Mod; }

void setFuncNameToProfNameMap(
const HashKeyMap<std::unordered_map, FunctionId, FunctionId> &FPMap) {
FuncNameToProfNameMap = &FPMap;
}

protected:
/// Map every function to its associated profile.
///
Expand Down Expand Up @@ -522,6 +553,22 @@ class SampleProfileReader {

std::unique_ptr<SampleProfileReaderItaniumRemapper> Remapper;

// A map pointer to the FuncNameToProfNameMap in SampleProfileLoader,
// which maps the function name to the matched profile name. This is used
// for sample loader to look up profile using the new name.
const HashKeyMap<std::unordered_map, FunctionId, FunctionId>
*FuncNameToProfNameMap = nullptr;

// A map from a function's context hash to its meta data section range, used
// for on-demand read function profile metadata.
std::unordered_map<uint64_t, std::pair<const uint8_t *, const uint8_t *>>
FuncMetadataIndex;

std::pair<const uint8_t *, const uint8_t *> LBRProfileSecRange;

/// Whether the profile has attribute metadata.
bool ProfileHasAttribute = false;

/// \brief Whether samples are collected based on pseudo probes.
bool ProfileIsProbeBased = false;

Expand Down Expand Up @@ -621,6 +668,8 @@ class SampleProfileReaderBinary : public SampleProfileReader {

/// Read the next function profile instance.
std::error_code readFuncProfile(const uint8_t *Start);
std::error_code readFuncProfile(const uint8_t *Start,
SampleProfileMap &Profiles);

/// Read the contents of the given profile instance.
std::error_code readProfile(FunctionSamples &FProfile);
Expand Down Expand Up @@ -720,11 +769,15 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
std::error_code readSecHdrTableEntry(uint64_t Idx);
std::error_code readSecHdrTable();

std::error_code readFuncMetadata(bool ProfileHasAttribute,
SampleProfileMap &Profiles);
std::error_code readFuncMetadata(bool ProfileHasAttribute);
std::error_code readFuncMetadata(bool ProfileHasAttribute,
FunctionSamples *FProfile);
std::error_code readFuncOffsetTable();
std::error_code readFuncProfiles();
std::error_code readFuncProfiles(const DenseSet<StringRef> &FuncsToUse,
SampleProfileMap &Profiles);
std::error_code readNameTableSec(bool IsMD5, bool FixedLengthMD5);
std::error_code readCSNameTableSec();
std::error_code readProfileSymbolList();
Expand Down Expand Up @@ -776,6 +829,13 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
/// the reader has been given a module.
bool collectFuncsFromModule() override;

/// Read the profiles on-demand for the given functions. This is used after
/// stale call graph matching finds new functions whose profiles aren't loaded
/// at the beginning and we need to loaded the profiles explicitly for
/// potential matching.
std::error_code read(const DenseSet<StringRef> &FuncsToUse,
SampleProfileMap &Profiles) override;

std::unique_ptr<ProfileSymbolList> getProfileSymbolList() override {
return std::move(ProfSymList);
};
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/Transforms/IPO/SampleProfileMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class SampleProfileMatcher {
// function and all inlinees.
void countMismatchedCallsiteSamples(const FunctionSamples &FS);
void computeAndReportProfileStaleness();
void UpdateWithSalvagedProfiles();

LocToLocMap &getIRToProfileLocationMap(const Function &F) {
auto Ret = FuncMappings.try_emplace(
Expand Down
Loading
Loading