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

Adam Nemet via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 17:14:53 PDT 2016


anemet updated this revision to Diff 70110.
anemet added a comment.

Addressed David's comments.  I still kept the check of the operand number
around to make sure we don't index out of the MD node.


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,36 @@
     }
     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 "";
+
+    MDString *MDName = cast<MDString>(WeightsNode->getOperand(0));
+    if (MDName->getString() != "branch_weights")
+      return "";
+
+    unsigned OpNo = I.getSuccessorIndex() + 1;
+    if (OpNo >= WeightsNode->getNumOperands())
+      return "";
+    ConstantInt *Weight =
+        mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(OpNo));
+    if (!Weight)
+      return "";
+
+    // Append a 'W' to indicate that these are weights rather than actual
+    // profile
+    // count (due to scaling).
+    Twine Attrs = "label=\"W:" + Twine(Weight->getZExtValue()) + "\"";
+    return Attrs.str();
+  }
 };
 } // End llvm namespace
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24144.70110.patch
Type: text/x-patch
Size: 1291 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160902/98b7f9b0/attachment.bin>


More information about the llvm-commits mailing list