Skip to content

Commit 070702c

Browse files
committed
[SampleFDO] Read top-level functions recovered by call-graph matching
1 parent c66d25d commit 070702c

File tree

6 files changed

+521
-96
lines changed

6 files changed

+521
-96
lines changed

llvm/include/llvm/ProfileData/SampleProfReader.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ class SampleProfileReader {
392392
/// which doesn't support loading function profiles on demand.
393393
virtual bool collectFuncsFromModule() { return false; }
394394

395+
virtual std::error_code readOnDemand(const DenseSet<StringRef> &FuncsToUse,
396+
SampleProfileMap &Profiles) {
397+
return sampleprof_error::not_implemented;
398+
};
399+
395400
/// Print all the profiles on stream \p OS.
396401
void dump(raw_ostream &OS = dbgs());
397402

@@ -413,6 +418,16 @@ class SampleProfileReader {
413418
if (It != Profiles.end())
414419
return &It->second;
415420

421+
if (FuncNameToProfNameMap && !FuncNameToProfNameMap->empty()) {
422+
auto R = FuncNameToProfNameMap->find(FunctionId(Fname));
423+
if (R != FuncNameToProfNameMap->end()) {
424+
Fname = R->second.stringRef();
425+
auto It = Profiles.find(FunctionId(Fname));
426+
if (It != Profiles.end())
427+
return &It->second;
428+
}
429+
}
430+
416431
if (Remapper) {
417432
if (auto NameInProfile = Remapper->lookUpNameInProfile(Fname)) {
418433
auto It = Profiles.find(FunctionId(*NameInProfile));
@@ -494,6 +509,11 @@ class SampleProfileReader {
494509

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

512+
void setFuncNameToProfNameMap(
513+
HashKeyMap<std::unordered_map, FunctionId, FunctionId> *FPMap) {
514+
FuncNameToProfNameMap = FPMap;
515+
}
516+
497517
protected:
498518
/// Map every function to its associated profile.
499519
///
@@ -522,6 +542,21 @@ class SampleProfileReader {
522542

523543
std::unique_ptr<SampleProfileReaderItaniumRemapper> Remapper;
524544

545+
// A map pointer to the FuncNameToProfNameMap in SampleProfileLoader,
546+
// which maps the function name to the matched profile name. This is used
547+
// for sample loader to look up profile using the new name.
548+
HashKeyMap<std::unordered_map, FunctionId, FunctionId>
549+
*FuncNameToProfNameMap = nullptr;
550+
551+
// A map from a function's context hash to its meta data section range, used
552+
// for on-demand read function profile metadata.
553+
std::unordered_map<uint64_t, std::pair<const uint8_t *, const uint8_t *>>
554+
FContextToMetaDataSecRange;
555+
556+
std::pair<const uint8_t *, const uint8_t *> LBRProfileSecRange;
557+
558+
bool ProfileHasAttribute = false;
559+
525560
/// \brief Whether samples are collected based on pseudo probes.
526561
bool ProfileIsProbeBased = false;
527562

@@ -621,6 +656,8 @@ class SampleProfileReaderBinary : public SampleProfileReader {
621656

622657
/// Read the next function profile instance.
623658
std::error_code readFuncProfile(const uint8_t *Start);
659+
std::error_code readFuncProfile(const uint8_t *Start,
660+
SampleProfileMap &Profiles);
624661

625662
/// Read the contents of the given profile instance.
626663
std::error_code readProfile(FunctionSamples &FProfile);
@@ -720,11 +757,15 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
720757
std::error_code readSecHdrTableEntry(uint64_t Idx);
721758
std::error_code readSecHdrTable();
722759

760+
std::error_code readFuncMetadataOnDemand(bool ProfileHasAttribute,
761+
SampleProfileMap &Profiles);
723762
std::error_code readFuncMetadata(bool ProfileHasAttribute);
724763
std::error_code readFuncMetadata(bool ProfileHasAttribute,
725764
FunctionSamples *FProfile);
726765
std::error_code readFuncOffsetTable();
727766
std::error_code readFuncProfiles();
767+
std::error_code readFuncProfiles(const DenseSet<StringRef> &FuncsToUse,
768+
SampleProfileMap &Profiles);
728769
std::error_code readNameTableSec(bool IsMD5, bool FixedLengthMD5);
729770
std::error_code readCSNameTableSec();
730771
std::error_code readProfileSymbolList();
@@ -776,6 +817,12 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
776817
/// the reader has been given a module.
777818
bool collectFuncsFromModule() override;
778819

820+
/// Read the profiles on-demand for the given functions. This is used after
821+
/// stale call graph matching finds new functions whose profiles aren't read
822+
/// at the beginning and we need to re-read the profiles.
823+
std::error_code readOnDemand(const DenseSet<StringRef> &FuncsToUse,
824+
SampleProfileMap &Profiles) override;
825+
779826
std::unique_ptr<ProfileSymbolList> getProfileSymbolList() override {
780827
return std::move(ProfSymList);
781828
};

llvm/include/llvm/Transforms/IPO/SampleProfileMatcher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ class SampleProfileMatcher {
198198
// function and all inlinees.
199199
void countMismatchedCallsiteSamples(const FunctionSamples &FS);
200200
void computeAndReportProfileStaleness();
201+
void UpdateSampleLoaderWithRecoveredProfiles();
201202

202203
LocToLocMap &getIRToProfileLocationMap(const Function &F) {
203204
auto Ret = FuncMappings.try_emplace(

0 commit comments

Comments
 (0)