From 80c8ebb4d82ed6ee64ae77c2c49085d5b153d8f4 Mon Sep 17 00:00:00 2001 From: Dehao Chen Date: Wed, 28 Sep 2016 18:41:14 +0000 Subject: [PATCH] Fix the bug when -compile-twice is specified, the PSI will be invalidated. Summary: When using llc with -compile-twice, module is generated twice, but getAnalysis().getPSI will still get the old PSI with the original (invalidated) Module. This patch checks if the module has changed when calling getPSI, if yes, update the module and invalidate the Summary. The bug does not show up in the current llc because PSI is not used in CodeGen yet. But with https://reviews.llvm.org/D24989, the bug will be exposed by test/CodeGen/PowerPC/pr26378.ll Reviewers: eraman, davidxl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D24993 llvm-svn: 282616 --- llvm/include/llvm/Analysis/ProfileSummaryInfo.h | 6 ++++-- llvm/lib/Analysis/ProfileSummaryInfo.cpp | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/llvm/include/llvm/Analysis/ProfileSummaryInfo.h b/llvm/include/llvm/Analysis/ProfileSummaryInfo.h index 61fc417..decb0ad 100644 --- a/llvm/include/llvm/Analysis/ProfileSummaryInfo.h +++ b/llvm/include/llvm/Analysis/ProfileSummaryInfo.h @@ -40,7 +40,7 @@ class ProfileSummary; // units. This would require making this depend on BFI. class ProfileSummaryInfo { private: - Module &M; + Module *M; std::unique_ptr Summary; void computeSummary(); void computeThresholds(); @@ -48,7 +48,7 @@ private: Optional HotCountThreshold, ColdCountThreshold; public: - ProfileSummaryInfo(Module &M) : M(M) {} + ProfileSummaryInfo(Module *M) : M(M) {} ProfileSummaryInfo(ProfileSummaryInfo &&Arg) : M(Arg.M), Summary(std::move(Arg.Summary)) {} /// \brief Returns true if \p F is a hot function. @@ -59,6 +59,8 @@ public: bool isHotCount(uint64_t C); /// \brief Returns true if count \p C is considered cold. bool isColdCount(uint64_t C); + /// \brief Checks if \p NewM is up-to-date, if not, invalidate Summary. + void resetModule(Module *NewM); }; /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo. diff --git a/llvm/lib/Analysis/ProfileSummaryInfo.cpp b/llvm/lib/Analysis/ProfileSummaryInfo.cpp index 300b265..ecfa5c2 100644 --- a/llvm/lib/Analysis/ProfileSummaryInfo.cpp +++ b/llvm/lib/Analysis/ProfileSummaryInfo.cpp @@ -57,7 +57,7 @@ static uint64_t getMinCountForPercentile(SummaryEntryVector &DS, void ProfileSummaryInfo::computeSummary() { if (Summary) return; - auto *SummaryMD = M.getProfileSummary(); + auto *SummaryMD = M->getProfileSummary(); if (!SummaryMD) return; Summary.reset(ProfileSummary::getFromMD(SummaryMD)); @@ -113,6 +113,13 @@ void ProfileSummaryInfo::computeThresholds() { getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold); } +void ProfileSummaryInfo::resetModule(Module *NewM) { + if (NewM == M) + return; + M = NewM; + Summary.reset(nullptr); +} + bool ProfileSummaryInfo::isHotCount(uint64_t C) { if (!HotCountThreshold) computeThresholds(); @@ -127,7 +134,9 @@ bool ProfileSummaryInfo::isColdCount(uint64_t C) { ProfileSummaryInfo *ProfileSummaryInfoWrapperPass::getPSI(Module &M) { if (!PSI) - PSI.reset(new ProfileSummaryInfo(M)); + PSI.reset(new ProfileSummaryInfo(&M)); + else + PSI->resetModule(&M); return PSI.get(); } @@ -142,7 +151,7 @@ ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass() char ProfileSummaryAnalysis::PassID; ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M, ModuleAnalysisManager &) { - return ProfileSummaryInfo(M); + return ProfileSummaryInfo(&M); } // FIXME: This only tests isHotFunction and isColdFunction and not the -- 2.7.4