10#include "mlir/IR/SymbolTable.h"
18static bool isSymbolReference(Attribute value) {
19 auto array = dyn_cast<ArrayAttr>(value);
20 return isa<SymbolRefAttr>(value) ||
21 (array && llvm::all_of(array, llvm::IsaPred<SymbolRefAttr>));
28static void describe(Operation *op, llvm::raw_ostream &os) {
30 for (NamedAttribute attr : op->getAttrs()) {
31 if (isSymbolReference(attr.getValue()) ||
32 attr.getName() == SymbolTable::getSymbolAttrName() ||
33 attr.getName() ==
"consumed_by")
35 os <<
' ' << attr.getName();
36 if (attr.getName() !=
"fifoName")
37 os <<
'=' << attr.getValue();
43 llvm::raw_string_ostream os(
features[op]);
49 links[from].push_back({label.str(), to});
50 links[to].push_back({(label +
"~").str(), from});
54 DenseMap<Operation *, unsigned>
rank;
56 auto rerank = [&](
auto &signature) {
57 SmallVector<Operation *> order(
nodes);
58 llvm::stable_sort(order, [&](Operation *a, Operation *b) {
59 return signature.at(a) < signature.at(b);
62 for (
auto [index, op] : llvm::enumerate(order)) {
63 if (index && signature.at(order[index - 1]) < signature.at(op))
69 std::map<Operation *, std::string> initial;
70 for (Operation *op :
nodes)
73 for (
unsigned previous = 0; classes != previous;) {
76 std::pair<unsigned, SmallVector<std::pair<std::string, unsigned>>>>
78 for (Operation *op :
nodes) {
79 auto &[own, around] = signature[op];
81 for (
const auto &[label, other] :
links.lookup(op))
82 around.push_back({label,
rank[other]});
93 DenseMap<Operation *, std::pair<Operation *, unsigned>> position;
94 for (Operation &op : device.getBody()->without_terminator()) {
95 if (isa<RuntimeSequenceOp>(op))
97 llvm::raw_string_ostream os(graph.
add(&op));
99 op.walk<WalkOrder::PreOrder>([&](Operation *nested) {
100 position[nested] = {&op, index++};
103 describe(nested, os);
108 for (Operation *node : SmallVector<Operation *>(graph.
nodes)) {
109 node->walk<WalkOrder::PreOrder>([&](Operation *nested) {
110 std::string at = std::to_string(position.lookup(nested).second);
111 for (
auto [index, operand] : llvm::enumerate(nested->getOperands())) {
112 Operation *def = operand.getDefiningOp();
113 if (def && def->getParentOp() == device && graph.
features.count(def))
114 graph.
link(node, def, at +
":" + std::to_string(index));
116 for (NamedAttribute attr : nested->getAttrs()) {
117 if (!isSymbolReference(attr.getValue()))
119 SmallVector<SymbolRefAttr> refs;
120 if (
auto ref = dyn_cast<SymbolRefAttr>(attr.getValue()))
125 cast<ArrayAttr>(attr.getValue()).getAsRange<SymbolRefAttr>());
126 for (
auto [index, ref] : llvm::enumerate(refs)) {
128 position.find(SymbolTable::lookupNearestSymbolFrom(nested, ref));
129 if (it == position.end())
131 auto [target, within] = it->second;
132 graph.
link(node, target,
133 at +
":" + attr.getName().str() +
":" +
134 std::to_string(index) +
"@" + std::to_string(within));
144 for (DeviceOp device : module.getOps<DeviceOp>()) {
147 for (Operation *node : own.
nodes) {
148 graph.
nodes.push_back(node);
150 graph.
link(device, node,
"holds");
Ops and the labelled links between them, for ranking them by structure where a pass would otherwise f...
void link(mlir::Operation *from, mlir::Operation *to, llvm::StringRef label)
Link from to to under label, and to back to from.
llvm::SmallVector< mlir::Operation * > nodes
std::string & add(mlir::Operation *op)
Add op as a node whose features are its name and attributes, symbol names and references left out.
llvm::DenseMap< mlir::Operation *, unsigned > rank() const
Rank the nodes by their features and then, a round at a time until no class splits,...
llvm::DenseMap< mlir::Operation *, std::string > features
What each node is, before anything it links to is considered.
static StructuralGraph of(DeviceOp device)
The ops directly in device but its runtime sequences, each with the ops nested in it,...
llvm::DenseMap< mlir::Operation *, llvm::SmallVector< std::pair< std::string, mlir::Operation * > > > links