[PATCH] D34795: Supporting -f(no)-reorder-functions flag, llvm side change

Taewook Oh via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 28 18:05:49 PDT 2017


twoh created this revision.

When profile data is given, .hot/.unlikely section prefix is
added to hot/cold functions for linker to improve code locality. GCC
controls this behavior with '-f(no)-reorder-functions' flag, while LLVM
uses opt tool option '-profile-guided-section-prefix=true/false'. This
patch is for LLVM to support the same driver flag with GCC. Clang side
patch with test cases will follow.


https://reviews.llvm.org/D34795

Files:
  include/llvm/Target/TargetMachine.h
  include/llvm/Target/TargetOptions.h
  lib/CodeGen/CodeGenPrepare.cpp


Index: lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- lib/CodeGen/CodeGenPrepare.cpp
+++ lib/CodeGen/CodeGenPrepare.cpp
@@ -302,13 +302,17 @@
   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
   OptSize = F.optForSize();
 
-  if (ProfileGuidedSectionPrefix) {
-    ProfileSummaryInfo *PSI =
-        getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
-    if (PSI->isFunctionHotInCallGraph(&F))
-      F.setSectionPrefix(".hot");
-    else if (PSI->isFunctionColdInCallGraph(&F))
-      F.setSectionPrefix(".unlikely");
+  bool AddProfileGuidedSectionPrefix =
+      (!TM || ProfileGuidedSectionPrefix.getNumOccurrences())
+          ? ProfileGuidedSectionPrefix
+          : TM->getReorderFunctions();
+  if (AddProfileGuidedSectionPrefix) {
+    if (auto *PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI()) {
+      if (PSI->isFunctionHotInCallGraph(&F))
+        F.setSectionPrefix(".hot");
+      else if (PSI->isFunctionColdInCallGraph(&F))
+        F.setSectionPrefix(".unlikely");
+    }
   }
 
   /// This optimization identifies DIV instructions that can be
Index: include/llvm/Target/TargetOptions.h
===================================================================
--- include/llvm/Target/TargetOptions.h
+++ include/llvm/Target/TargetOptions.h
@@ -107,8 +107,8 @@
           EnableFastISel(false), UseInitArray(false),
           DisableIntegratedAS(false), RelaxELFRelocations(false),
           FunctionSections(false), DataSections(false),
-          UniqueSectionNames(true), TrapUnreachable(false), EmulatedTLS(false),
-          EnableIPRA(false) {}
+          UniqueSectionNames(true), ReorderFunctions(true),
+          TrapUnreachable(false), EmulatedTLS(false), EnableIPRA(false) {}
 
     /// PrintMachineCode - This flag is enabled when the -print-machineinstrs
     /// option is specified on the command line, and should enable debugging
@@ -206,6 +206,9 @@
 
     unsigned UniqueSectionNames : 1;
 
+    /// Add section prefix for hot/cold functions.
+    unsigned ReorderFunctions : 1;
+
     /// Emit target-specific trap instruction for 'unreachable' IR instructions.
     unsigned TrapUnreachable : 1;
 
Index: include/llvm/Target/TargetMachine.h
===================================================================
--- include/llvm/Target/TargetMachine.h
+++ include/llvm/Target/TargetMachine.h
@@ -186,6 +186,10 @@
 
   bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
 
+  /// Return true if profile-guided section prefix that indicates hotness of
+  /// the function should be added, corresponds to -freorder-functions.
+  bool getReorderFunctions() const { return Options.ReorderFunctions; }
+
   /// Return true if data objects should be emitted into their own section,
   /// corresponds to -fdata-sections.
   bool getDataSections() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34795.104567.patch
Type: text/x-patch
Size: 2885 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170629/77e9918f/attachment.bin>


More information about the llvm-commits mailing list