Skip to content

Commit 63f48fd

Browse files
authored
[CFGPrinter] Add node id formater (#164623)
This PR is part of the LLVM IR LSP server project ([RFC](https://discourse.llvm.org/t/rfc-ir-visualization-with-vs-code-extension-using-an-lsp-server/87773)) Sometimes it is nice to be able to specify IDs of nodes in the printed CFG. For better manipulation of the outputed CFG. In our case we will use it for navigation between IR and CFG views. This adds an argument to DOTFuncInfo - a function that takes a BasicBlock and returns a node ID, to be printed in the result dot.
1 parent e88a83a commit 63f48fd

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

llvm/include/llvm/Analysis/CFGPrinter.h

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "llvm/Support/DOTGraphTraits.h"
3232
#include "llvm/Support/FormatVariadic.h"
3333

34+
#include <functional>
35+
#include <sstream>
36+
3437
namespace llvm {
3538
class ModuleSlotTracker;
3639

@@ -69,13 +72,18 @@ class DOTFuncInfo {
6972
bool ShowHeat;
7073
bool EdgeWeights;
7174
bool RawWeights;
75+
using NodeIdFormatterTy =
76+
std::function<std::optional<std::string>(const BasicBlock *)>;
77+
std::optional<NodeIdFormatterTy> NodeIdFormatter;
7278

7379
public:
7480
DOTFuncInfo(const Function *F) : DOTFuncInfo(F, nullptr, nullptr, 0) {}
7581
LLVM_ABI ~DOTFuncInfo();
7682

77-
LLVM_ABI DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI,
78-
const BranchProbabilityInfo *BPI, uint64_t MaxFreq);
83+
LLVM_ABI
84+
DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI,
85+
const BranchProbabilityInfo *BPI, uint64_t MaxFreq,
86+
std::optional<NodeIdFormatterTy> NodeIdFormatter = std::nullopt);
7987

8088
const BlockFrequencyInfo *getBFI() const { return BFI; }
8189

@@ -102,6 +110,10 @@ class DOTFuncInfo {
102110
void setEdgeWeights(bool EdgeWeights) { this->EdgeWeights = EdgeWeights; }
103111

104112
bool showEdgeWeights() { return EdgeWeights; }
113+
114+
std::optional<NodeIdFormatterTy> getNodeIdFormatter() {
115+
return NodeIdFormatter;
116+
}
105117
};
106118

107119
template <>
@@ -311,21 +323,27 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
311323
}
312324

313325
std::string getNodeAttributes(const BasicBlock *Node, DOTFuncInfo *CFGInfo) {
326+
std::stringstream Attrs;
327+
328+
if (auto NodeIdFmt = CFGInfo->getNodeIdFormatter())
329+
if (auto NodeId = (*NodeIdFmt)(Node))
330+
Attrs << "id=\"" << *NodeId << "\"";
331+
332+
if (CFGInfo->showHeatColors()) {
333+
uint64_t Freq = CFGInfo->getFreq(Node);
334+
std::string Color = getHeatColor(Freq, CFGInfo->getMaxFreq());
335+
std::string EdgeColor = (Freq <= (CFGInfo->getMaxFreq() / 2))
336+
? (getHeatColor(0))
337+
: (getHeatColor(1));
338+
if (!Attrs.str().empty())
339+
Attrs << ",";
340+
Attrs << "color=\"" << EdgeColor << "ff\", style=filled, "
341+
<< "fillcolor=\"" << Color << "70\", " << "fontname=\"Courier\"";
342+
}
314343

315-
if (!CFGInfo->showHeatColors())
316-
return "";
317-
318-
uint64_t Freq = CFGInfo->getFreq(Node);
319-
std::string Color = getHeatColor(Freq, CFGInfo->getMaxFreq());
320-
std::string EdgeColor = (Freq <= (CFGInfo->getMaxFreq() / 2))
321-
? (getHeatColor(0))
322-
: (getHeatColor(1));
323-
324-
std::string Attrs = "color=\"" + EdgeColor + "ff\", style=filled," +
325-
" fillcolor=\"" + Color + "70\"" +
326-
" fontname=\"Courier\"";
327-
return Attrs;
344+
return Attrs.str();
328345
}
346+
329347
LLVM_ABI bool isNodeHidden(const BasicBlock *Node,
330348
const DOTFuncInfo *CFGInfo);
331349
LLVM_ABI void computeDeoptOrUnreachablePaths(const Function *F);

llvm/lib/Analysis/CFGPrinter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ static void viewCFG(Function &F, const BlockFrequencyInfo *BFI,
9292
}
9393

9494
DOTFuncInfo::DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI,
95-
const BranchProbabilityInfo *BPI, uint64_t MaxFreq)
96-
: F(F), BFI(BFI), BPI(BPI), MaxFreq(MaxFreq) {
95+
const BranchProbabilityInfo *BPI, uint64_t MaxFreq,
96+
std::optional<NodeIdFormatterTy> NodeIdFormatter)
97+
: F(F), BFI(BFI), BPI(BPI), MaxFreq(MaxFreq),
98+
NodeIdFormatter(NodeIdFormatter) {
9799
ShowHeat = false;
98100
EdgeWeights = !!BPI; // Print EdgeWeights when BPI is available.
99101
RawWeights = !!BFI; // Print RawWeights when BFI is available.

0 commit comments

Comments
 (0)