12#include <mlir/IR/SymbolTable.h>
14#include <llvm/ADT/GraphTraits.h>
15#include <llvm/ADT/MapVector.h>
16#include <llvm/ADT/SetVector.h>
17#include <llvm/Support/DOTGraphTraits.h>
21class SymbolDefTreeNode {
23 mlir::SymbolOpInterface symbolDef;
26 SymbolDefTreeNode *parent;
27 mlir::SetVector<SymbolDefTreeNode *> children;
29 SymbolDefTreeNode(mlir::SymbolOpInterface symbolDefOp) : symbolDef(symbolDefOp), parent(
nullptr) {
30 assert(symbolDef &&
"must have a SymbolOpInterface node");
34 SymbolDefTreeNode() : symbolDef(
nullptr), parent(
nullptr) {}
37 void addChild(SymbolDefTreeNode *node);
45 mlir::SymbolOpInterface
getOp()
const {
return symbolDef; }
48 const SymbolDefTreeNode *
getParent()
const {
return parent; }
60 inline llvm::iterator_range<child_iterator>
childIter()
const {
61 return llvm::make_range(
begin(),
end());
66 void print(llvm::raw_ostream &os)
const;
73 using NodeMapT = llvm::MapVector<mlir::SymbolOpInterface, std::unique_ptr<SymbolDefTreeNode>>;
82 class NodeIterator final
83 :
public llvm::mapped_iterator<
84 NodeMapT::const_iterator, SymbolDefTreeNode *(*)(const NodeMapT::value_type &)> {
86 return value.second.get();
91 NodeIterator(NodeMapT::const_iterator it)
92 : llvm::mapped_iterator<
114 size_t size()
const {
return nodes.size(); }
121 inline llvm::iterator_range<iterator>
nodeIter()
const {
122 return llvm::make_range(
begin(),
end());
127 void print(llvm::raw_ostream &os)
const;
150 :
public GraphTraits<const llzk::SymbolDefTreeNode *> {
child_iterator begin() const
size_t numChildren() const
child_iterator end() const
friend class SymbolDefTree
mlir::SetVector< SymbolDefTreeNode * >::const_iterator child_iterator
Iterator over the children of this node.
bool hasChildren() const
Returns true if this node has any child edges.
std::string toString() const
Print the node in a human readable format.
llvm::iterator_range< child_iterator > childIter() const
Range over child nodes.
const SymbolDefTreeNode * getParent() const
Returns the parent node in the tree. The root node will return nullptr.
mlir::SymbolOpInterface getOp() const
Returns the Symbol operation referenced by this node.
void print(llvm::raw_ostream &os) const
Builds a tree structure representing the symbol table structure.
NodeIterator iterator
An iterator over the nodes of the tree.
const SymbolDefTreeNode * lookupNode(mlir::SymbolOpInterface symbolOp) const
Lookup the node for the given symbol Op, or nullptr if none exists.
llvm::iterator_range< iterator > nodeIter() const
void dump() const
Dump the tree in a human readable format.
SymbolDefTree(mlir::SymbolOpInterface root)
const SymbolDefTreeNode * getRoot() const
Returns the symbolic (i.e., no associated op) root node of the tree.
void dumpToDotFile(std::string filename="") const
Dump the tree to file in dot graph format.
void print(llvm::raw_ostream &os) const
size_t size() const
Return total number of nodes in the tree.
std::string getNodeLabel(NodeRef n, GraphType)
DOTGraphTraits(bool isSimple=false)
const llzk::SymbolDefTreeNode * NodeRef
const llzk::SymbolDefTree * GraphType
static std::string getGraphName(GraphType)
std::string getNodeLabel(NodeRef n, GraphType g)
DOTGraphTraits(bool isSimple=false)
static NodeRef getEntryNode(NodeRef node)
llzk::SymbolDefTreeNode::child_iterator ChildIteratorType
ChildIteratorType/begin/end - Allow iteration over all nodes in the graph.
static ChildIteratorType child_begin(NodeRef node)
const llzk::SymbolDefTreeNode * NodeRef
static ChildIteratorType child_end(NodeRef node)
static nodes_iterator nodes_begin(GraphType g)
static NodeRef getEntryNode(GraphType g)
The entry node into the graph is the external node.
static unsigned size(GraphType g)
Return total number of nodes in the graph.
static nodes_iterator nodes_end(GraphType g)
const llzk::SymbolDefTree * GraphType
llzk::SymbolDefTree::iterator nodes_iterator
nodes_iterator/begin/end - Allow iteration over all nodes in the graph.