[llvm] [CFGPrinter] Allow set hide paths at runtime (PR #172206)

via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 14 04:57:37 PST 2025


https://github.com/Bertik23 created https://github.com/llvm/llvm-project/pull/172206

Add HideDeoptimizePaths and HideUnreachablePaths to DOTFunctInfo.
This allows the modification of these settings in an alternative way to the CLI arguments.
If no option is passes, the CLI argument value is used.

This is helpful when creating multiple graphs with different settings in the same process.

>From 44f9e23c0785a3f323eaed266887e490aa4c8eae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Albert=20Havli=C4=8Dek?= <ahavlicek at azul.com>
Date: Sun, 14 Dec 2025 12:48:29 +0000
Subject: [PATCH] [CFGPrinter] Allow set hide paths at runtime

---
 llvm/include/llvm/Analysis/CFGPrinter.h | 14 ++++++++++++--
 llvm/lib/Analysis/CFGPrinter.cpp        | 20 +++++++++++++-------
 2 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/llvm/include/llvm/Analysis/CFGPrinter.h b/llvm/include/llvm/Analysis/CFGPrinter.h
index aa711642a3a6d..80f9e3f7ecd8b 100644
--- a/llvm/include/llvm/Analysis/CFGPrinter.h
+++ b/llvm/include/llvm/Analysis/CFGPrinter.h
@@ -32,6 +32,7 @@
 #include "llvm/Support/FormatVariadic.h"
 
 #include <functional>
+#include <optional>
 #include <sstream>
 
 namespace llvm {
@@ -75,6 +76,8 @@ class DOTFuncInfo {
   using NodeIdFormatterTy =
       std::function<std::optional<std::string>(const BasicBlock *)>;
   std::optional<NodeIdFormatterTy> NodeIdFormatter;
+  bool HideDeoptimizePaths;
+  bool HideUnreachablePaths;
 
 public:
   DOTFuncInfo(const Function *F) : DOTFuncInfo(F, nullptr, nullptr, 0) {}
@@ -83,7 +86,9 @@ class DOTFuncInfo {
   LLVM_ABI
   DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI,
               const BranchProbabilityInfo *BPI, uint64_t MaxFreq,
-              std::optional<NodeIdFormatterTy> NodeIdFormatter = std::nullopt);
+              std::optional<NodeIdFormatterTy> NodeIdFormatter = std::nullopt,
+              std::optional<bool> HideDeoptimizePaths = std::nullopt,
+              std::optional<bool> HideUnreachablePaths = std::nullopt);
 
   const BlockFrequencyInfo *getBFI() const { return BFI; }
 
@@ -114,6 +119,10 @@ class DOTFuncInfo {
   std::optional<NodeIdFormatterTy> getNodeIdFormatter() {
     return NodeIdFormatter;
   }
+
+  bool hideDeoptimizePaths() const { return HideDeoptimizePaths; }
+
+  bool hideUnreachablePaths() const { return HideUnreachablePaths; }
 };
 
 template <>
@@ -346,7 +355,8 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
 
   LLVM_ABI bool isNodeHidden(const BasicBlock *Node,
                              const DOTFuncInfo *CFGInfo);
-  LLVM_ABI void computeDeoptOrUnreachablePaths(const Function *F);
+  LLVM_ABI void computeDeoptOrUnreachablePaths(const Function *F,
+                                               const DOTFuncInfo *CFGInfo);
 };
 } // namespace llvm
 
diff --git a/llvm/lib/Analysis/CFGPrinter.cpp b/llvm/lib/Analysis/CFGPrinter.cpp
index 39108a906f081..6650b9d655527 100644
--- a/llvm/lib/Analysis/CFGPrinter.cpp
+++ b/llvm/lib/Analysis/CFGPrinter.cpp
@@ -93,9 +93,14 @@ static void viewCFG(Function &F, const BlockFrequencyInfo *BFI,
 
 DOTFuncInfo::DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI,
                          const BranchProbabilityInfo *BPI, uint64_t MaxFreq,
-                         std::optional<NodeIdFormatterTy> NodeIdFormatter)
+                         std::optional<NodeIdFormatterTy> NodeIdFormatter,
+                         std::optional<bool> HideDeoptimizePaths,
+                         std::optional<bool> HideUnreachablePaths)
     : F(F), BFI(BFI), BPI(BPI), MaxFreq(MaxFreq),
-      NodeIdFormatter(NodeIdFormatter) {
+      NodeIdFormatter(NodeIdFormatter),
+      HideDeoptimizePaths(HideDeoptimizePaths.value_or(::HideDeoptimizePaths)),
+      HideUnreachablePaths(
+          HideUnreachablePaths.value_or(::HideUnreachablePaths)) {
   ShowHeat = false;
   EdgeWeights = !!BPI; // Print EdgeWeights when BPI is available.
   RawWeights = !!BFI;  // Print RawWeights when BFI is available.
@@ -190,13 +195,14 @@ void Function::viewCFGOnly(const BlockFrequencyInfo *BFI,
 /// or unreachable). These paths are hidden if the corresponding cl::opts
 /// are enabled.
 void DOTGraphTraits<DOTFuncInfo *>::computeDeoptOrUnreachablePaths(
-    const Function *F) {
+    const Function *F, const DOTFuncInfo *CFGInfo) {
   auto evaluateBB = [&](const BasicBlock *Node) {
     if (succ_empty(Node)) {
       const Instruction *TI = Node->getTerminator();
       isOnDeoptOrUnreachablePath[Node] =
-          (HideUnreachablePaths && isa<UnreachableInst>(TI)) ||
-          (HideDeoptimizePaths && Node->getTerminatingDeoptimizeCall());
+          (CFGInfo->hideUnreachablePaths() && isa<UnreachableInst>(TI)) ||
+          (CFGInfo->hideDeoptimizePaths() &&
+           Node->getTerminatingDeoptimizeCall());
       return;
     }
     isOnDeoptOrUnreachablePath[Node] =
@@ -220,9 +226,9 @@ bool DOTGraphTraits<DOTFuncInfo *>::isNodeHidden(const BasicBlock *Node,
           HideColdPaths)
         return true;
     }
-  if (HideUnreachablePaths || HideDeoptimizePaths) {
+  if (CFGInfo->hideUnreachablePaths() || CFGInfo->hideDeoptimizePaths()) {
     if (!isOnDeoptOrUnreachablePath.contains(Node))
-      computeDeoptOrUnreachablePaths(Node->getParent());
+      computeDeoptOrUnreachablePaths(Node->getParent(), CFGInfo);
     return isOnDeoptOrUnreachablePath[Node];
   }
   return false;



More information about the llvm-commits mailing list