LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
SymbolUseGraph.cpp
Go to the documentation of this file.
1//===-- SymbolUseGraph.cpp --------------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
13#include "llzk/Util/Compare.h"
14#include "llzk/Util/Constants.h"
18
19#include <mlir/IR/BuiltinOps.h>
20
21#include <llvm/ADT/SmallPtrSet.h>
22#include <llvm/ADT/SmallSet.h>
23#include <llvm/Support/GraphWriter.h>
24
25using namespace mlir;
26
27namespace llzk {
28
29//===----------------------------------------------------------------------===//
30// SymbolUseGraphNode
31//===----------------------------------------------------------------------===//
32
33void SymbolUseGraphNode::addSuccessor(SymbolUseGraphNode *node) {
34 if (this->successors.insert(node)) {
35 node->predecessors.insert(this);
36 }
37}
38
39void SymbolUseGraphNode::removeSuccessor(SymbolUseGraphNode *node) {
40 if (this->successors.remove(node)) {
41 node->predecessors.remove(this);
42 }
43}
44
45FailureOr<SymbolLookupResultUntyped>
46SymbolUseGraphNode::lookupSymbol(SymbolTableCollection &tables, bool reportMissing) const {
47 if (!isRealNode()) {
48 return failure();
49 }
50 Operation *lookupFrom = getSymbolPathRoot().getOperation();
51 auto res = lookupSymbolIn(tables, getSymbolPath(), lookupFrom, lookupFrom, reportMissing);
52 if (succeeded(res) || !reportMissing) {
53 return res;
54 }
55 // This is likely an error in the use graph and not a case that should ever happen.
56 return lookupFrom->emitError().append(
57 "Could not find symbol referenced in UseGraph: ", getSymbolPath()
58 );
59}
60
61//===----------------------------------------------------------------------===//
62// SymbolUseGraph
63//===----------------------------------------------------------------------===//
64
65namespace {
66
67template <typename R>
68R getPathAndCall(SymbolOpInterface defOp, llvm::function_ref<R(ModuleOp, SymbolRefAttr)> callback) {
69 assert(defOp); // pre-condition
70
71 ModuleOp foundRoot;
72 FailureOr<SymbolRefAttr> path = llzk::getPathFromRoot(defOp, &foundRoot);
73 if (failed(path)) {
74 // This occurs if there is no root module with LANG_ATTR_NAME attribute
75 // or there is an unnamed module between the root module and the symbol.
76 auto diag = defOp.emitError("in SymbolUseGraph, failed to build symbol path");
77 diag.attachNote(defOp.getLoc()).append("for this SymbolOp");
78 diag.report();
79 return nullptr;
80 }
81 return callback(foundRoot, path.value());
82}
83
84} // namespace
85
86SymbolUseGraph::SymbolUseGraph(SymbolOpInterface rootSymbolOp) {
87 assert(rootSymbolOp->hasTrait<OpTrait::SymbolTable>());
88 buildGraph(rootSymbolOp);
89}
90
92SymbolUseGraphNode *SymbolUseGraph::getSymbolUserNode(const SymbolTable::SymbolUse &u) {
93 SymbolOpInterface userSymbol = getSelfOrParentOfType<SymbolOpInterface>(u.getUser());
94 return getPathAndCall<SymbolUseGraphNode *>(
95 userSymbol,
96 [this, &userSymbol](ModuleOp r, SymbolRefAttr p) {
97 auto n = this->getOrAddNode(r, p, nullptr);
98 n->opsThatUseTheSymbol.insert(userSymbol);
99 return n;
100 }
101 );
102}
103
104void SymbolUseGraph::buildGraph(SymbolOpInterface symbolOp) {
105 auto walkFn = [this](Operation *op, bool) {
106 assert(op->hasTrait<OpTrait::SymbolTable>());
107 FailureOr<ModuleOp> opRootModule = llzk::getRootModule(op);
108 if (failed(opRootModule)) {
109 return;
110 }
111
112 SymbolTableCollection tables;
113 if (auto usesOpt = llzk::getSymbolUses(&op->getRegion(0))) {
114 // Create child node for each Symbol use, as successor of the user Symbol op.
115 for (SymbolTable::SymbolUse u : usesOpt.value()) {
116 bool isStructParam = false;
117 Operation *user = u.getUser();
118 SymbolRefAttr symRef = u.getSymbolRef();
119 // Pending [LLZK-272] only a heuristic approach is possible. Check for FlatSymbolRefAttr
120 // where the user is a FieldRefOpInterface or the user is located within a StructDefOp and
121 // append the StructDefOp path with the FlatSymbolRefAttr.
122 if (FlatSymbolRefAttr flatSymRef = llvm::dyn_cast<FlatSymbolRefAttr>(symRef)) {
123 if (auto fref = llvm::dyn_cast<component::FieldRefOpInterface>(user);
124 fref && fref.getFieldNameAttr() == flatSymRef) {
125 symRef = llzk::appendLeaf(fref.getStructType().getNameRef(), flatSymRef);
126 } else if (auto userStruct = getSelfOrParentOfType<component::StructDefOp>(user)) {
127 StringAttr localName = flatSymRef.getAttr();
128 isStructParam = userStruct.hasParamNamed(localName);
129 if (isStructParam || tables.getSymbolTable(userStruct).lookup(localName)) {
130 // If 'flatSymRef' is defined in the SymbolTable for 'userStruct' then it's
131 // a local symbol so prepend the full path of the struct itself.
132 auto parentPath = llzk::getPathFromRoot(userStruct);
133 assert(succeeded(parentPath));
134 symRef = llzk::appendLeaf(parentPath.value(), flatSymRef);
135 }
136 }
137 }
138 auto node = this->getOrAddNode(opRootModule.value(), symRef, getSymbolUserNode(u));
139 node->isStructConstParam = isStructParam;
140 node->opsThatUseTheSymbol.insert(user);
141 }
142 }
143 };
144 SymbolTable::walkSymbolTables(symbolOp.getOperation(), true, walkFn);
145
146 // Find all nodes with no successors and add the tail node as successor.
147 for (SymbolUseGraphNode *n : nodesIter()) {
148 if (!n->hasSuccessor()) {
149 n->addSuccessor(&tail);
150 }
151 }
152}
153
154SymbolUseGraphNode *SymbolUseGraph::getOrAddNode(
155 ModuleOp pathRoot, SymbolRefAttr path, SymbolUseGraphNode *predecessorNode
156) {
157 NodeMapKeyT key = std::make_pair(pathRoot, path);
158 std::unique_ptr<SymbolUseGraphNode> &nodeRef = nodes[key];
159 if (!nodeRef) {
160 nodeRef.reset(new SymbolUseGraphNode(pathRoot, path));
161 // When creating a new node, ensure it's attached to the graph, either as successor
162 // to the predecessor node (if given) else as successor to the root node.
163 if (predecessorNode) {
164 predecessorNode->addSuccessor(nodeRef.get());
165 } else {
166 root.addSuccessor(nodeRef.get());
167 }
168 } else if (predecessorNode) {
169 // When the node already exists and an additional predecessor node is given, add the node as a
170 // successor to the given predecessor node and detach from the 'root' (unless it's a self edge).
171 SymbolUseGraphNode *node = nodeRef.get();
172 predecessorNode->addSuccessor(node);
173 if (node != predecessorNode) {
174 root.removeSuccessor(node);
175 }
176 }
177 return nodeRef.get();
178}
179
180const SymbolUseGraphNode *SymbolUseGraph::lookupNode(ModuleOp pathRoot, SymbolRefAttr path) const {
181 NodeMapKeyT key = std::make_pair(pathRoot, path);
182 const auto *it = nodes.find(key);
183 return it == nodes.end() ? nullptr : it->second.get();
184}
185
186const SymbolUseGraphNode *SymbolUseGraph::lookupNode(SymbolOpInterface symbolDef) const {
187 return getPathAndCall<const SymbolUseGraphNode *>(symbolDef, [this](ModuleOp r, SymbolRefAttr p) {
188 return this->lookupNode(r, p);
189 });
190}
191
192//===----------------------------------------------------------------------===//
193// Printing
194//===----------------------------------------------------------------------===//
195
196std::string SymbolUseGraphNode::toString(bool showLocations) const {
197 return buildStringViaPrint(*this, showLocations);
198}
199
200namespace {
201
202inline void safeAppendPathRoot(llvm::raw_ostream &os, ModuleOp root) {
203 if (root) {
204 FailureOr<SymbolRefAttr> unambiguousRoot = getPathFromTopRoot(root);
205 if (succeeded(unambiguousRoot)) {
206 os << unambiguousRoot.value() << '\n';
207 } else {
208 os << "<<unknown path>>\n";
209 }
210 } else {
211 os << "<<NULL MODULE>>\n";
212 }
213}
214
215} // namespace
216
218 llvm::raw_ostream &os, bool showLocations, std::string locationLinePrefix
219) const {
220 os << '\'' << symbolPath << '\'';
221 if (isStructConstParam) {
222 os << " (struct param)";
223 }
224 os << " with root module ";
225 safeAppendPathRoot(os, symbolPathRoot);
226 if (showLocations) {
227 // Print the user op locations (sorted for stable output). Printing only the location rather
228 // than the full Operation gives a short (single-line) format that's still useful for human
229 // debugging.
230 llvm::SmallSet<mlir::Location, 3, LocationComparator> locations;
231 for (Operation *user : getUserOps()) {
232 locations.insert(user->getLoc());
233 }
234 for (Location loc : locations) {
235 os << locationLinePrefix << loc << '\n';
236 }
237 }
238}
239
240void SymbolUseGraph::print(llvm::raw_ostream &os) const {
241 const SymbolUseGraphNode *rootPtr = &this->root;
242
243 // Tracks nodes that have been printed to ensure they are only printed once.
244 SmallPtrSet<SymbolUseGraphNode *, 16> done;
245
246 std::function<void(SymbolUseGraphNode *)> printNode = [rootPtr, &printNode, &done,
247 &os](SymbolUseGraphNode *node) {
248 // Skip if the node has been printed before
249 if (!done.insert(node).second) {
250 return;
251 }
252 // Print the current node
253 os << "// - Node : [" << node << "] ";
254 node->print(os, true, "// --- ");
255 // Print list of IDs for the predecessors (excluding root) and successors
256 os << "// --- Predecessors : [";
257 llvm::interleaveComma(
258 llvm::make_filter_range(
259 node->predecessorIter(), [rootPtr](SymbolUseGraphNode *n) { return n != rootPtr; }
260 ),
261 os
262 );
263 os << "]\n";
264 os << "// --- Successors : [";
265 llvm::interleaveComma(node->successorIter(), os);
266 os << "]\n";
267 // Recursively print the successors
268 for (SymbolUseGraphNode *c : node->successorIter()) {
269 printNode(c);
270 }
271 };
272
273 os << "// ---- SymbolUseGraph ----\n";
274 for (SymbolUseGraphNode *r : rootPtr->successorIter()) {
275 printNode(r);
276 }
277 os << "// ------------------------\n";
278 assert(done.size() == this->size() && "All nodes were not printed!");
279}
280
281void SymbolUseGraph::dumpToDotFile(std::string filename) const {
283 llvm::WriteGraph(this, "SymbolUseGraph", /*ShortNames*/ false, title, filename);
284}
285
286} // namespace llzk
mlir::FailureOr< SymbolLookupResultUntyped > lookupSymbol(mlir::SymbolTableCollection &tables, bool reportMissing=true) const
bool isRealNode() const
Return 'false' iff this node is an artificial node created for the graph head/tail.
void print(llvm::raw_ostream &os, bool showLocations=false, std::string locationLinePrefix="") const
std::string toString(bool showLocations=false) const
Print the node in a human readable format.
mlir::SymbolRefAttr getSymbolPath() const
The symbol path+name relative to the closest root ModuleOp.
const OpSet & getUserOps() const
The set of operations that use the symbol.
mlir::ModuleOp getSymbolPathRoot() const
Return the root ModuleOp for the path.
llvm::iterator_range< iterator > successorIter() const
Range over successor nodes.
void dumpToDotFile(std::string filename="") const
Dump the graph to file in dot graph format.
SymbolUseGraph(mlir::SymbolOpInterface rootSymbolOp)
llvm::iterator_range< iterator > nodesIter() const
Range over all nodes in the graph.
void print(llvm::raw_ostream &os) const
const SymbolUseGraphNode * lookupNode(mlir::ModuleOp pathRoot, mlir::SymbolRefAttr path) const
Return the existing node for the symbol reference relative to the given module, else nullptr.
std::optional< mlir::SymbolTable::UseRange > getSymbolUses(mlir::Operation *from)
Get an iterator range for all of the uses, for any symbol, that are nested within the given operation...
FailureOr< ModuleOp > getRootModule(Operation *from)
SymbolRefAttr appendLeaf(SymbolRefAttr orig, FlatSymbolRefAttr newLeaf)
std::string buildStringViaPrint(const T &base, Args &&...args)
Generate a string by calling base.print(llvm::raw_ostream &) on a stream backed by the returned strin...
mlir::FailureOr< SymbolLookupResultUntyped > lookupSymbolIn(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, Within &&lookupWithin, mlir::Operation *origin, bool reportMissing=true)
FailureOr< SymbolRefAttr > getPathFromTopRoot(SymbolOpInterface to, ModuleOp *foundRoot)
OpClass getSelfOrParentOfType(mlir::Operation *op)
Return the closest operation that is of type 'OpClass', either the op itself or an ancestor.
Definition OpHelpers.h:32
FailureOr< SymbolRefAttr > getPathFromRoot(SymbolOpInterface to, ModuleOp *foundRoot)