[PATCH] D24144: [CFGPrinter] Display branch weight on the edges

Adam Nemet via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 10:22:55 PDT 2016


anemet created this revision.
anemet added reviewers: davidxl, dexonsmith, bogner.
anemet added a subscriber: llvm-commits.

This is pretty useful especially in connection with
BFI's -view-block-freq-propagation-dags.  It helped me to track down the
bug that is being fixed in D24118.

While -view-block-freq-propagation-dags displays the high-level
information with static heuristics included (and block frequencies), the
new thing only shows the raw weight as presented by PGO without any of
the static estimates.  This helps to distinguished what has been
measured vs. estimated.

For the sample loop in D24118, -view-block-freq-propagation-dags=integer
looks like this:

https://reviews.llvm.org/F2381352

While with -view-cfg-only you can see the underlying branch weights:

https://reviews.llvm.org/F2381349

https://reviews.llvm.org/D24144

Files:
  include/llvm/Analysis/CFGPrinter.h

Index: include/llvm/Analysis/CFGPrinter.h
===================================================================
--- include/llvm/Analysis/CFGPrinter.h
+++ include/llvm/Analysis/CFGPrinter.h
@@ -118,6 +118,32 @@
     }
     return "";
   }
+
+  /// Display the raw branch weights from PGO.
+  std::string getEdgeAttributes(const BasicBlock *Node, succ_const_iterator I,
+                                const Function *F) {
+  const TerminatorInst *TI = Node->getTerminator();
+  if (TI->getNumSuccessors() == 1)
+    return "";
+
+  MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
+  if (!WeightsNode)
+    return "";
+
+  // Ensure there are weights for all of the successors. Note that the first
+  // operand to the metadata node is a name, not a weight.
+  if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
+    return "";
+
+  unsigned SuccNo = I.getSuccessorIndex();
+  ConstantInt *Weight =
+    mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(SuccNo + 1));
+  if (!Weight)
+    return "";
+
+  Twine Attrs = "label=\"" + Twine(Weight->getZExtValue()) + "\"";
+  return Attrs.str();
+  }
 };
 } // End llvm namespace
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24144.70026.patch
Type: text/x-patch
Size: 1162 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160901/63040069/attachment.bin>


More information about the llvm-commits mailing list