[PATCH] D28967: [PGO] Add option to view CFG with profile after profile annotation

David Li via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 20 13:36:22 PST 2017


davidxl created this revision.

Currently there is no way to view CFG with profile annotation right after the PGO annotation phase. This patch adds the support.

The profile displayed is based on BFI + entry profile count. This will be the profile which the downstream components see.   A followup patch will be created to display CFG with raw profile counts produced by PGOUse phase. The later will be useful to detect bugs/limitations in BFI.


https://reviews.llvm.org/D28967

Files:
  Analysis/BlockFrequencyInfo.cpp
  Transforms/Instrumentation/PGOInstrumentation.cpp


Index: Transforms/Instrumentation/PGOInstrumentation.cpp
===================================================================
--- Transforms/Instrumentation/PGOInstrumentation.cpp
+++ Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -58,7 +58,9 @@
 #include "llvm/Analysis/BranchProbabilityInfo.h"
 #include "llvm/Analysis/CFG.h"
 #include "llvm/Analysis/IndirectCallSiteVisitor.h"
+#include "llvm/Analysis/LoopInfo.h"
 #include "llvm/IR/CallSite.h"
+#include "llvm/IR/Dominators.h"
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/IR/IRBuilder.h"
@@ -143,6 +145,17 @@
 // Command line option to enable/disable select instruction instrumentation.
 static cl::opt<bool> PGOInstrSelect("pgo-instr-select", cl::init(true),
                                     cl::Hidden);
+
+// Command line option to specify the name of the function for CFG dump
+static cl::opt<std::string>
+    PGOViewCountsName("pgo-view-counts-name", cl::Hidden,
+                      cl::desc("The option to specify "
+                               "the name of the function "
+                               "whose CFG will be displayed."));
+
+// Command line option to turn on CFG dot dump after profile annotation.
+extern cl::opt<bool> PGOViewCounts;
+
 namespace {
 
 /// The select instruction visitor plays three roles specified
@@ -1202,6 +1215,18 @@
       ColdFunctions.push_back(&F);
     else if (FreqAttr == PGOUseFunc::FFA_Hot)
       HotFunctions.push_back(&F);
+#ifndef NDEBUG
+    if (PGOViewCounts &&
+        (PGOViewCountsName.empty() || F.getName().equals(PGOViewCountsName))) {
+      LoopInfo LI{DominatorTree(F)};
+      std::unique_ptr<BranchProbabilityInfo> NewBPI =
+          llvm::make_unique<BranchProbabilityInfo>(F, LI);
+      std::unique_ptr<BlockFrequencyInfo> NewBFI =
+          llvm::make_unique<BlockFrequencyInfo>(F, *NewBPI, LI);
+
+      NewBFI->view();
+    }
+#endif
   }
   M.setProfileSummary(PGOReader->getSummary().getMD(M.getContext()));
   // Set function hotness attribute from the profile.
Index: Analysis/BlockFrequencyInfo.cpp
===================================================================
--- Analysis/BlockFrequencyInfo.cpp
+++ Analysis/BlockFrequencyInfo.cpp
@@ -55,8 +55,18 @@
                                 "is no less than the max frequency of the "
                                 "function multiplied by this percent."));
 
+// Command line option to turn on CFG dot dump after profile annotation.
+cl::opt<bool> PGOViewCounts("pgo-view-counts", cl::init(false), cl::Hidden);
+
 namespace llvm {
 
+static GVDAGType getGVDT() {
+
+  if (PGOViewCounts)
+    return GVDT_Count;
+  return ViewBlockFreqPropagationDAG;
+}
+
 template <>
 struct GraphTraits<BlockFrequencyInfo *> {
   typedef const BasicBlock *NodeRef;
@@ -89,8 +99,7 @@
   std::string getNodeLabel(const BasicBlock *Node,
                            const BlockFrequencyInfo *Graph) {
 
-    return BFIDOTGTraitsBase::getNodeLabel(Node, Graph,
-                                           ViewBlockFreqPropagationDAG);
+    return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
   }
 
   std::string getNodeAttributes(const BasicBlock *Node,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28967.85182.patch
Type: text/x-patch
Size: 3199 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170120/32e0fd40/attachment.bin>


More information about the llvm-commits mailing list