[llvm] [SelectionDAG] Add opcode class metadata to DOT nodes (PR #228965)
Jonathan L'Work via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 4 19:44:57 PDT 2026
https://github.com/Jonathan03ant created https://github.com/llvm/llvm-project/pull/228965
## Background
[I have intentionally left out a test for now because I would like feedback on the approach first; I will add one once the design is agreed on.]
I ran into this while working on [llvmviz.org](https://llvmviz.org), which parses SelectionDAG DOT output for visualization.
The DOT output contains the printed name of each node, but it does not say whether that node is:
- a generic SelectionDAG operation;
- a target-specific SelectionDAG operation;
- a target-independent machine opcode; or
- a target machine instruction.
[llvmviz.org](https://llvmviz.org) previously tried to infer this from opcode names. For example, AMDGPU instructions beginning with `SI_` or `V_` were considered target-specific. That incorrectly classified instructions such as `S_ENDPGM`, and the approach could not work consistently across other targets.
## Change
This patch adds an `llvm_node_class` attribute to each SelectionDAG DOT node.
The value is determined using LLVM's existing opcode information:
- `generic-isd`
- `target-isd`
- `target-independent-machine`
- `target-machine`
The classification uses `SDNode::isMachineOpcode()`, `SDNode::isTargetOpcode()`, and `isTargetSpecificOpcode()` rather than the printed node name.
For example:
```dot
llvm_node_class="generic-isd"
llvm_node_class="target-machine"
````
The attribute does not change how Graphviz renders the existing graph. It gives tools consuming the DOT output a reliable way to understand what kind of node they are looking at without adding target-specific naming rules.
## Testing
I have not added a test yet because I would first like feedback on the general approach, the four categories, and the `llvm_node_class` attribute name.
If this direction looks reasonable, I will add regression coverage before marking the PR ready for review.
>From 8a5dc74540b5208d01eb5422ba4715d0c01a1aff Mon Sep 17 00:00:00 2001
From: jonathan03ant <jonathan03ant at gmail.com>
Date: Sun, 4 Oct 2026 22:27:23 -0400
Subject: [PATCH] [SelectionDAG] Add opcode class metadata to DOT nodes
---
.../SelectionDAG/SelectionDAGPrinter.cpp | 24 ++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
index 871385f8ddf2880..dd12c6e9dde8539 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
@@ -15,6 +15,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/SelectionDAG.h"
+#include "llvm/CodeGen/TargetOpcodes.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/GraphWriter.h"
#include "llvm/Support/raw_ostream.h"
@@ -99,6 +100,20 @@ namespace llvm {
return "";
}
+ /// Classify a node according to its SelectionDAG or machine opcode domain.
+ static const char *getNodeOpcodeClass(const SDNode *Node) {
+ if (Node->isMachineOpcode()) {
+ unsigned Opcode = Node->getMachineOpcode();
+
+ if (isTargetSpecificOpcode(Opcode))
+ return "target-machine";
+ return "target-independent-machine";
+ }
+
+ if (Node->isTargetOpcode())
+ return "target-isd";
+ return "generic-isd";
+ }
static std::string getSimpleNodeLabel(const SDNode *Node,
const SelectionDAG *G) {
@@ -109,19 +124,22 @@ namespace llvm {
}
return Result;
}
+
std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
static std::string getNodeAttributes(const SDNode *N,
const SelectionDAG *Graph) {
+ const std::string ClassAttr =
+ std::string("llvm_node_class=\"") + getNodeOpcodeClass(N) + "\"";
#ifndef NDEBUG
const std::string &Attrs = Graph->getGraphAttrs(N);
if (!Attrs.empty()) {
if (Attrs.find("shape=") == std::string::npos)
- return std::string("shape=Mrecord,") + Attrs;
+ return std::string("shape=Mrecord,") + ClassAttr + "," + Attrs;
else
- return Attrs;
+ return Attrs + "," + ClassAttr;
}
#endif
- return "shape=Mrecord";
+ return std::string("shape=Mrecord,") + ClassAttr;
}
static void addCustomGraphFeatures(SelectionDAG *G,
More information about the llvm-commits
mailing list