[PATCH] D154096: Tooltips for control flow dot graphs

Marek Sedláček via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 29 08:36:57 PDT 2023


mark-sed created this revision.
mark-sed added reviewers: cdevadas, rcorcs, knaumov, sfertile.
Herald added a project: All.
mark-sed requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This is 2. out of 3 changes to the CFG pass that aims to make graphs more readable and contain more information.

This change adds tooltips over graph edges so that when zoomed in one can easily determine from to where is the given edge going and also see the probability for this edge.

If all 3 changes will be accepted the final graphs should look as in the attached image:
F28074492: new_cfg.png <https://reviews.llvm.org/F28074492>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154096

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


Index: llvm/include/llvm/Analysis/CFGPrinter.h
===================================================================
--- llvm/include/llvm/Analysis/CFGPrinter.h
+++ llvm/include/llvm/Analysis/CFGPrinter.h
@@ -247,52 +247,70 @@
     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));
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154096.535818.patch
Type: text/x-patch
Size: 3380 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230629/f8362a63/attachment-0001.bin>


More information about the llvm-commits mailing list