[llvm] 7ffcea5 - Tooltips for CFG dot graphs

Artur Pilipenko via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 16 21:06:42 PDT 2023


Author: Marek Sedláček
Date: 2023-08-16T21:06:24-07:00
New Revision: 7ffcea5488ebbbfab6f853b28f441cd9dc5121d0

URL: https://github.com/llvm/llvm-project/commit/7ffcea5488ebbbfab6f853b28f441cd9dc5121d0
DIFF: https://github.com/llvm/llvm-project/commit/7ffcea5488ebbbfab6f853b28f441cd9dc5121d0.diff

LOG: Tooltips for CFG dot graphs

This change adds tooltips over graph edges that indicate the to and
the from of the edge together with the probability for this edge.

Differential Revision: https://reviews.llvm.org/D154096

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/CFGPrinter.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/CFGPrinter.h b/llvm/include/llvm/Analysis/CFGPrinter.h
index 893440908ac5af..2d334c81bbdcd1 100644
--- a/llvm/include/llvm/Analysis/CFGPrinter.h
+++ b/llvm/include/llvm/Analysis/CFGPrinter.h
@@ -247,52 +247,70 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
     return "";
   }
 
+  static std::string getBBName(const BasicBlock *Node) {
+    std::string NodeName = Node->getName().str();
+    if (NodeName.empty()) {
+      raw_string_ostream NodeOS(NodeName);
+      Node->printAsOperand(NodeOS, false);
+      NodeName = NodeOS.str();
+      // Removing %
+      NodeName.erase(NodeName.begin());
+    }
+    return NodeName;
+  }
+
   /// Display the raw branch weights from PGO.
   std::string getEdgeAttributes(const BasicBlock *Node, const_succ_iterator I,
                                 DOTFuncInfo *CFGInfo) {
+    unsigned OpNo = I.getSuccessorIndex();
+    const Instruction *TI = Node->getTerminator();
+    BasicBlock *SuccBB = TI->getSuccessor(OpNo);
+    auto BranchProb = CFGInfo->getBPI()->getEdgeProbability(Node, SuccBB);
+    double WeightPercent = ((double)BranchProb.getNumerator()) /
+                           ((double)BranchProb.getDenominator());
+
+    std::string TTAttr =
+        formatv("tooltip=\"{0} -> {1}\\nProbability {2:P}\" ", getBBName(Node),
+                getBBName(SuccBB), WeightPercent);
     if (!CFGInfo->showEdgeWeights())
-      return "";
+      return TTAttr;
 
-    const Instruction *TI = Node->getTerminator();
     if (TI->getNumSuccessors() == 1)
-      return "penwidth=2";
-
-    unsigned OpNo = I.getSuccessorIndex();
+      return TTAttr + "penwidth=2";
 
     if (OpNo >= TI->getNumSuccessors())
-      return "";
+      return TTAttr;
 
-    BasicBlock *SuccBB = TI->getSuccessor(OpNo);
-    auto BranchProb = CFGInfo->getBPI()->getEdgeProbability(Node, SuccBB);
-    double WeightPercent = ((double)BranchProb.getNumerator()) /
-                           ((double)BranchProb.getDenominator());
     double Width = 1 + WeightPercent;
 
     if (!CFGInfo->useRawEdgeWeights())
-      return formatv("label=\"{0:P}\" penwidth={1}", WeightPercent, Width)
-          .str();
+      return TTAttr +
+             formatv("label=\"{0:P}\" penwidth={1}", WeightPercent, Width)
+                 .str();
 
     // Prepend a 'W' to indicate that this is a weight rather than the actual
     // profile count (due to scaling).
 
     uint64_t Freq = CFGInfo->getFreq(Node);
-    std::string Attrs = formatv("label=\"W:{0}\" penwidth={1}",
-                                (uint64_t)(Freq * WeightPercent), Width);
+    std::string Attrs =
+        TTAttr + formatv("label=\"W:{0}\" penwidth={1}",
+                         (uint64_t)(Freq * WeightPercent), Width)
+                     .str();
     if (Attrs.size())
       return Attrs;
 
     MDNode *WeightsNode = getBranchWeightMDNode(*TI);
     if (!WeightsNode)
-      return "";
+      return TTAttr;
 
     OpNo = I.getSuccessorIndex() + 1;
     if (OpNo >= WeightsNode->getNumOperands())
-      return "";
+      return TTAttr;
     ConstantInt *Weight =
         mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(OpNo));
     if (!Weight)
-      return "";
-    return ("label=\"W:" + std::to_string(Weight->getZExtValue()) +
+      return TTAttr;
+    return (TTAttr + "label=\"W:" + std::to_string(Weight->getZExtValue()) +
             "\" penwidth=" + std::to_string(Width));
   }
 


        


More information about the llvm-commits mailing list