diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -142,11 +142,6 @@ cl::desc("Compute stale profile statistical metrics and write it into the " "native object file(.llvm_stats section).")); -static cl::opt FlattenProfileForMatching( - "flatten-profile-for-matching", cl::Hidden, cl::init(true), - cl::desc( - "Use flattened profile for stale profile detection and matching.")); - static cl::opt ProfileSampleAccurate( "profile-sample-accurate", cl::Hidden, cl::init(false), cl::desc("If the sample profile is accurate, we will mark all un-sampled " @@ -466,12 +461,7 @@ public: SampleProfileMatcher(Module &M, SampleProfileReader &Reader, const PseudoProbeManager *ProbeManager) - : M(M), Reader(Reader), ProbeManager(ProbeManager) { - if (FlattenProfileForMatching) { - ProfileConverter::flattenProfile(Reader.getProfiles(), FlattenedProfiles, - FunctionSamples::ProfileIsCS); - } - } + : M(M), Reader(Reader), ProbeManager(ProbeManager){}; void runOnModule(); private: @@ -482,7 +472,7 @@ return &It->second; return nullptr; } - void runOnFunction(const Function &F, const FunctionSamples &FS); + void runOnFunction(const Function &F); void findIRAnchors(const Function &F, std::map &IRAnchors); void findProfileAnchors(const FunctionSamples &FS, @@ -2385,8 +2375,15 @@ } } -void SampleProfileMatcher::runOnFunction(const Function &F, - const FunctionSamples &FS) { +void SampleProfileMatcher::runOnFunction(const Function &F) { + const auto *FS = Reader.getSamplesFor(F); + // Use flattened function samples to populate profile locations as function + // samples under different context may have different callsites, so merge them + // together for the matching. + const auto *FSFlattened = getFlattenedSamplesFor(F); + if (!FSFlattened) + return; + // Anchors for IR. It's a map from IR location to callee name, callee name is // empty for non-call instruction and use a dummy name(UnknownIndirectCallee) // for unknown indrect callee name. @@ -2395,17 +2392,17 @@ // Anchors for profile. It's a map from callsite location to a set of callee // name. std::map> ProfileAnchors; - findProfileAnchors(FS, ProfileAnchors); + findProfileAnchors(*FSFlattened, ProfileAnchors); // Detect profile mismatch for profile staleness metrics report. - if (ReportProfileStaleness || PersistProfileStaleness) { - countProfileMismatches(F, FS, IRAnchors, ProfileAnchors); + if (FS && (ReportProfileStaleness || PersistProfileStaleness)) { + countProfileMismatches(F, *FS, IRAnchors, ProfileAnchors); } // Run profile matching for checksum mismatched profile, currently only // support for pseudo-probe. if (SalvageStaleProfile && FunctionSamples::ProfileIsProbeBased && - !ProbeManager->profileIsValid(F, FS)) { + !ProbeManager->profileIsValid(F, *FSFlattened)) { // The matching result will be saved to IRToProfileLocationMap, create a new // map for each function. runStaleProfileMatching(F, IRAnchors, ProfileAnchors, @@ -2414,17 +2411,12 @@ } void SampleProfileMatcher::runOnModule() { + ProfileConverter::flattenProfile(Reader.getProfiles(), FlattenedProfiles, + FunctionSamples::ProfileIsCS); for (auto &F : M) { if (F.isDeclaration() || !F.hasFnAttribute("use-sample-profile")) continue; - FunctionSamples *FS = nullptr; - if (FlattenProfileForMatching) - FS = getFlattenedSamplesFor(F); - else - FS = Reader.getSamplesFor(F); - if (!FS) - continue; - runOnFunction(F, *FS); + runOnFunction(F); } if (SalvageStaleProfile) distributeIRToProfileLocationMap(); diff --git a/llvm/test/Transforms/SampleProfile/profile-mismatch-flattened-profile.ll b/llvm/test/Transforms/SampleProfile/profile-mismatch-flattened-profile.ll deleted file mode 100644 --- a/llvm/test/Transforms/SampleProfile/profile-mismatch-flattened-profile.ll +++ /dev/null @@ -1,13 +0,0 @@ -; REQUIRES: x86_64-linux -; RUN: opt < %S/profile-mismatch.ll -passes=sample-profile -sample-profile-file=%S/Inputs/profile-mismatch.prof -report-profile-staleness -persist-profile-staleness -flatten-profile-for-matching=1 -S 2>%t -o %t.ll -; RUN: FileCheck %s --input-file %t -; RUN: FileCheck %s --input-file %t.ll -check-prefix=CHECK-MD - -; RUN: opt < %S/profile-mismatch.ll -passes=sample-profile -sample-profile-file=%S/Inputs/profile-mismatch-cs.prof -report-profile-staleness -persist-profile-staleness -flatten-profile-for-matching=1 -S 2>%t -o %t.ll -; RUN: FileCheck %s --input-file %t -; RUN: FileCheck %s --input-file %t.ll -check-prefix=CHECK-MD - - -; CHECK: (3/4) of callsites' profile are invalid and (20/30) of samples are discarded due to callsite location mismatch. - -; CHECK-MD: ![[#]] = !{!"NumMismatchedCallsites", i64 3, !"TotalProfiledCallsites", i64 4, !"MismatchedCallsiteSamples", i64 20, !"TotalCallsiteSamples", i64 30} diff --git a/llvm/test/Transforms/SampleProfile/profile-mismatch.ll b/llvm/test/Transforms/SampleProfile/profile-mismatch.ll --- a/llvm/test/Transforms/SampleProfile/profile-mismatch.ll +++ b/llvm/test/Transforms/SampleProfile/profile-mismatch.ll @@ -1,5 +1,5 @@ ; REQUIRES: x86_64-linux -; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profile-mismatch.prof -report-profile-staleness -persist-profile-staleness -flatten-profile-for-matching=0 -S 2>%t -o %t.ll +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profile-mismatch.prof -report-profile-staleness -persist-profile-staleness -S 2>%t -o %t.ll ; RUN: FileCheck %s --input-file %t ; RUN: FileCheck %s --input-file %t.ll -check-prefix=CHECK-MD ; RUN: llc < %t.ll -filetype=obj -o %t.obj