[PATCH] D31225: Use isFunctionHotInCallGraph to set the function section prefix.

Dehao Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 21 17:33:39 PDT 2017


danielcdh created this revision.

The current prefix based function layout algorithm only looks at function's entry count, which is not sufficient. A function should be grouped together if its entry count or any call edge count is hot.


https://reviews.llvm.org/D31225

Files:
  include/llvm/Analysis/ProfileSummaryInfo.h
  lib/Analysis/ProfileSummaryInfo.cpp
  lib/CodeGen/CodeGenPrepare.cpp
  test/Transforms/CodeGenPrepare/section.ll


Index: test/Transforms/CodeGenPrepare/section.ll
===================================================================
--- test/Transforms/CodeGenPrepare/section.ll
+++ test/Transforms/CodeGenPrepare/section.ll
@@ -9,6 +9,12 @@
   ret void
 }
 
+; CHECK: hot_call_func{{.*}}!section_prefix ![[HOT_ID]]
+define void @hot_call_func() !prof !16 {
+  call void @hot_func(), !prof !17
+  ret void
+}
+
 ; CHECK: cold_func{{.*}}!section_prefix ![[COLD_ID:[0-9]+]]
 define void @cold_func() !prof !16 {
   ret void
@@ -33,3 +39,4 @@
 !14 = !{i32 999999, i64 1, i32 2}
 !15 = !{!"function_entry_count", i64 1000}
 !16 = !{!"function_entry_count", i64 1}
+!17 = !{!"branch_weights", i32 1000}
Index: lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- lib/CodeGen/CodeGenPrepare.cpp
+++ lib/CodeGen/CodeGenPrepare.cpp
@@ -270,7 +270,7 @@
   if (ProfileGuidedSectionPrefix) {
     ProfileSummaryInfo *PSI =
         getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
-    if (PSI->isFunctionEntryHot(&F))
+    if (PSI->isFunctionHotInCallGraph(&F))
       F.setSectionPrefix(".hot");
     else if (PSI->isFunctionEntryCold(&F))
       F.setSectionPrefix(".cold");
Index: lib/Analysis/ProfileSummaryInfo.cpp
===================================================================
--- lib/Analysis/ProfileSummaryInfo.cpp
+++ lib/Analysis/ProfileSummaryInfo.cpp
@@ -98,6 +98,25 @@
   return FunctionCount && isHotCount(FunctionCount.getValue());
 }
 
+/// Returns true if the function's entry or any of its call edge is hot.
+/// If it returns false, it either means it is not hot or it is unknown
+/// whether it is hot or not (for example, no profile data is available).
+bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F) {
+  if (!F || !computeSummary())
+    return false;
+  if (auto FunctionCount = F->getEntryCount())
+    if (isHotCount(FunctionCount.getValue()))
+      return true;
+
+  for (const auto &BB : *F)
+    for (const auto &I : BB)
+      if (isa<CallInst>(I) || isa<InvokeInst>(I))
+        if (auto CallCount = getProfileCount(&I, nullptr))
+          if (isHotCount(CallCount.getValue()))
+            return true;
+  return false;
+}
+
 /// Returns true if the function's entry is a cold. If it returns false, it
 /// either means it is not cold or it is unknown whether it is cold or not (for
 /// example, no profile data is available).
Index: include/llvm/Analysis/ProfileSummaryInfo.h
===================================================================
--- include/llvm/Analysis/ProfileSummaryInfo.h
+++ include/llvm/Analysis/ProfileSummaryInfo.h
@@ -59,6 +59,8 @@
                                             BlockFrequencyInfo *BFI);
   /// \brief Returns true if \p F has hot function entry.
   bool isFunctionEntryHot(const Function *F);
+  /// Returns true if \p F has hot function entry or hot call edge.
+  bool isFunctionHotInCallGraph(const Function *F);
   /// \brief Returns true if \p F has cold function entry.
   bool isFunctionEntryCold(const Function *F);
   /// \brief Returns true if \p F is a hot function.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31225.92575.patch
Type: text/x-patch
Size: 3110 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170322/08ebd254/attachment.bin>


More information about the llvm-commits mailing list