[PATCH] D24993: Fix the bug when -compile-twice is specified, the PSI will be invalidated.

Dehao Chen via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 27 17:21:33 PDT 2016


danielcdh created this revision.
danielcdh added reviewers: eraman, davidxl.
danielcdh added a subscriber: llvm-commits.

When using llc with -compile-twice, module is generated twice, but getAnalysis<ProfileSummaryInfoWrapperPass>().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

https://reviews.llvm.org/D24993

Files:
  include/llvm/Analysis/ProfileSummaryInfo.h
  lib/Analysis/ProfileSummaryInfo.cpp

Index: lib/Analysis/ProfileSummaryInfo.cpp
===================================================================
--- lib/Analysis/ProfileSummaryInfo.cpp
+++ lib/Analysis/ProfileSummaryInfo.cpp
@@ -57,7 +57,7 @@
 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 @@
       getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
 }
 
+void ProfileSummaryInfo::resetM(Module *newM) {
+  if (newM == M)
+    return;
+  M = newM;
+  delete Summary.release();
+}
+
 bool ProfileSummaryInfo::isHotCount(uint64_t C) {
   if (!HotCountThreshold)
     computeThresholds();
@@ -127,7 +134,9 @@
 
 ProfileSummaryInfo *ProfileSummaryInfoWrapperPass::getPSI(Module &M) {
   if (!PSI)
-    PSI.reset(new ProfileSummaryInfo(M));
+    PSI.reset(new ProfileSummaryInfo(&M));
+  else
+    PSI->resetM(&M);
   return PSI.get();
 }
 
@@ -142,7 +151,7 @@
 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
Index: include/llvm/Analysis/ProfileSummaryInfo.h
===================================================================
--- include/llvm/Analysis/ProfileSummaryInfo.h
+++ include/llvm/Analysis/ProfileSummaryInfo.h
@@ -40,15 +40,15 @@
 // units. This would require making this depend on BFI.
 class ProfileSummaryInfo {
 private:
-  Module &M;
+  Module *M;
   std::unique_ptr<ProfileSummary> Summary;
   void computeSummary();
   void computeThresholds();
   // Count thresholds to answer isHotCount and isColdCount queries.
   Optional<uint64_t> 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 @@
   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 resetM(Module *NewM);
 };
 
 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24993.72739.patch
Type: text/x-patch
Size: 2553 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160928/396c1b5d/attachment.bin>


More information about the llvm-commits mailing list