[llvm] [Analysis] Fix null ptr dereference when using WriteGraph without branch probability info (PR #104102)

Tyler Nowicki via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 10:59:01 PDT 2024


https://github.com/TylerNowicki created https://github.com/llvm/llvm-project/pull/104102

The call to  'CFGInfo->getBPI()->getEdgeProbability(Node, SuccBB);' fails when BPI is not provided. In this case we can give up and not print any edge attributes.

>From 8fd9fe5e2eb68f3b5d69e3afe9350064e54ab792 Mon Sep 17 00:00:00 2001
From: tnowicki <tnowicki.nowicki at amd.com>
Date: Wed, 14 Aug 2024 13:49:33 -0400
Subject: [PATCH] [Analysis] Fix null ptr dereference when using WriteGraph
 without branch probability info

---
 llvm/include/llvm/Analysis/CFGPrinter.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Analysis/CFGPrinter.h b/llvm/include/llvm/Analysis/CFGPrinter.h
index 7d4d3a574b9e81..cd785331d1f146 100644
--- a/llvm/include/llvm/Analysis/CFGPrinter.h
+++ b/llvm/include/llvm/Analysis/CFGPrinter.h
@@ -265,18 +265,19 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
   /// Display the raw branch weights from PGO.
   std::string getEdgeAttributes(const BasicBlock *Node, const_succ_iterator I,
                                 DOTFuncInfo *CFGInfo) {
+    // If BPI is not provided do not display any edge attributes
+    if (!CFGInfo->showEdgeWeights())
+      return "";
+
     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 TTAttr;
 
     if (TI->getNumSuccessors() == 1)
       return TTAttr + "penwidth=2";



More information about the llvm-commits mailing list