-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
NodePrinter.cpp
390 lines (331 loc) · 12.4 KB
/
NodePrinter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//===- NodePrinter.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Tools/PDLL/AST/Context.h"
#include "mlir/Tools/PDLL/AST/Nodes.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/ScopedPrinter.h"
using namespace mlir;
using namespace mlir::pdll::ast;
//===----------------------------------------------------------------------===//
// NodePrinter
//===----------------------------------------------------------------------===//
namespace {
class NodePrinter {
public:
NodePrinter(raw_ostream &os) : os(os) {}
/// Print the given type to the stream.
void print(Type type);
/// Print the given node to the stream.
void print(const Node *node);
private:
/// Print a range containing children of a node.
template <typename RangeT,
std::enable_if_t<!std::is_convertible<RangeT, const Node *>::value>
* = nullptr>
void printChildren(RangeT &&range) {
if (range.empty())
return;
// Print the first N-1 elements with a prefix of "|-".
auto it = std::begin(range);
for (unsigned i = 0, e = llvm::size(range) - 1; i < e; ++i, ++it)
print(*it);
// Print the last element.
elementIndentStack.back() = true;
print(*it);
}
template <typename RangeT, typename... OthersT,
std::enable_if_t<std::is_convertible<RangeT, const Node *>::value>
* = nullptr>
void printChildren(RangeT &&range, OthersT &&...others) {
printChildren(ArrayRef<const Node *>({range, others...}));
}
/// Print a range containing children of a node, nesting the children under
/// the given label.
template <typename RangeT>
void printChildren(StringRef label, RangeT &&range) {
if (range.empty())
return;
elementIndentStack.reserve(elementIndentStack.size() + 1);
llvm::SaveAndRestore lastElement(elementIndentStack.back(), true);
printIndent();
os << label << "`\n";
elementIndentStack.push_back(/*isLastElt*/ false);
printChildren(std::forward<RangeT>(range));
elementIndentStack.pop_back();
}
/// Print the given derived node to the stream.
void printImpl(const CompoundStmt *stmt);
void printImpl(const EraseStmt *stmt);
void printImpl(const LetStmt *stmt);
void printImpl(const ReplaceStmt *stmt);
void printImpl(const ReturnStmt *stmt);
void printImpl(const RewriteStmt *stmt);
void printImpl(const AttributeExpr *expr);
void printImpl(const CallExpr *expr);
void printImpl(const DeclRefExpr *expr);
void printImpl(const MemberAccessExpr *expr);
void printImpl(const OperationExpr *expr);
void printImpl(const RangeExpr *expr);
void printImpl(const TupleExpr *expr);
void printImpl(const TypeExpr *expr);
void printImpl(const AttrConstraintDecl *decl);
void printImpl(const OpConstraintDecl *decl);
void printImpl(const TypeConstraintDecl *decl);
void printImpl(const TypeRangeConstraintDecl *decl);
void printImpl(const UserConstraintDecl *decl);
void printImpl(const ValueConstraintDecl *decl);
void printImpl(const ValueRangeConstraintDecl *decl);
void printImpl(const NamedAttributeDecl *decl);
void printImpl(const OpNameDecl *decl);
void printImpl(const PatternDecl *decl);
void printImpl(const UserRewriteDecl *decl);
void printImpl(const VariableDecl *decl);
void printImpl(const Module *module);
/// Print the current indent stack.
void printIndent() {
if (elementIndentStack.empty())
return;
for (bool isLastElt : llvm::makeArrayRef(elementIndentStack).drop_back())
os << (isLastElt ? " " : " |");
os << (elementIndentStack.back() ? " `" : " |");
}
/// The raw output stream.
raw_ostream &os;
/// A stack of indents and a flag indicating if the current element being
/// printed at that indent is the last element.
SmallVector<bool> elementIndentStack;
};
} // namespace
void NodePrinter::print(Type type) {
// Protect against invalid inputs.
if (!type) {
os << "Type<NULL>";
return;
}
TypeSwitch<Type>(type)
.Case([&](AttributeType) { os << "Attr"; })
.Case([&](ConstraintType) { os << "Constraint"; })
.Case([&](OperationType type) {
os << "Op";
if (Optional<StringRef> name = type.getName())
os << "<" << *name << ">";
})
.Case([&](RangeType type) {
print(type.getElementType());
os << "Range";
})
.Case([&](RewriteType) { os << "Rewrite"; })
.Case([&](TupleType type) {
os << "Tuple<";
llvm::interleaveComma(
llvm::zip(type.getElementNames(), type.getElementTypes()), os,
[&](auto it) {
if (!std::get<0>(it).empty())
os << std::get<0>(it) << ": ";
this->print(std::get<1>(it));
});
os << ">";
})
.Case([&](TypeType) { os << "Type"; })
.Case([&](ValueType) { os << "Value"; })
.Default([](Type) { llvm_unreachable("unknown AST type"); });
}
void NodePrinter::print(const Node *node) {
printIndent();
os << "-";
elementIndentStack.push_back(/*isLastElt*/ false);
TypeSwitch<const Node *>(node)
.Case<
// Statements.
const CompoundStmt, const EraseStmt, const LetStmt, const ReplaceStmt,
const ReturnStmt, const RewriteStmt,
// Expressions.
const AttributeExpr, const CallExpr, const DeclRefExpr,
const MemberAccessExpr, const OperationExpr, const RangeExpr,
const TupleExpr, const TypeExpr,
// Decls.
const AttrConstraintDecl, const OpConstraintDecl,
const TypeConstraintDecl, const TypeRangeConstraintDecl,
const UserConstraintDecl, const ValueConstraintDecl,
const ValueRangeConstraintDecl, const NamedAttributeDecl,
const OpNameDecl, const PatternDecl, const UserRewriteDecl,
const VariableDecl,
const Module>([&](auto derivedNode) { this->printImpl(derivedNode); })
.Default([](const Node *) { llvm_unreachable("unknown AST node"); });
elementIndentStack.pop_back();
}
void NodePrinter::printImpl(const CompoundStmt *stmt) {
os << "CompoundStmt " << stmt << "\n";
printChildren(stmt->getChildren());
}
void NodePrinter::printImpl(const EraseStmt *stmt) {
os << "EraseStmt " << stmt << "\n";
printChildren(stmt->getRootOpExpr());
}
void NodePrinter::printImpl(const LetStmt *stmt) {
os << "LetStmt " << stmt << "\n";
printChildren(stmt->getVarDecl());
}
void NodePrinter::printImpl(const ReplaceStmt *stmt) {
os << "ReplaceStmt " << stmt << "\n";
printChildren(stmt->getRootOpExpr());
printChildren("ReplValues", stmt->getReplExprs());
}
void NodePrinter::printImpl(const ReturnStmt *stmt) {
os << "ReturnStmt " << stmt << "\n";
printChildren(stmt->getResultExpr());
}
void NodePrinter::printImpl(const RewriteStmt *stmt) {
os << "RewriteStmt " << stmt << "\n";
printChildren(stmt->getRootOpExpr(), stmt->getRewriteBody());
}
void NodePrinter::printImpl(const AttributeExpr *expr) {
os << "AttributeExpr " << expr << " Value<\"" << expr->getValue() << "\">\n";
}
void NodePrinter::printImpl(const CallExpr *expr) {
os << "CallExpr " << expr << " Type<";
print(expr->getType());
os << ">\n";
printChildren(expr->getCallableExpr());
printChildren("Arguments", expr->getArguments());
}
void NodePrinter::printImpl(const DeclRefExpr *expr) {
os << "DeclRefExpr " << expr << " Type<";
print(expr->getType());
os << ">\n";
printChildren(expr->getDecl());
}
void NodePrinter::printImpl(const MemberAccessExpr *expr) {
os << "MemberAccessExpr " << expr << " Member<" << expr->getMemberName()
<< "> Type<";
print(expr->getType());
os << ">\n";
printChildren(expr->getParentExpr());
}
void NodePrinter::printImpl(const OperationExpr *expr) {
os << "OperationExpr " << expr << " Type<";
print(expr->getType());
os << ">\n";
printChildren(expr->getNameDecl());
printChildren("Operands", expr->getOperands());
printChildren("Result Types", expr->getResultTypes());
printChildren("Attributes", expr->getAttributes());
}
void NodePrinter::printImpl(const RangeExpr *expr) {
os << "RangeExpr " << expr << " Type<";
print(expr->getType());
os << ">\n";
printChildren(expr->getElements());
}
void NodePrinter::printImpl(const TupleExpr *expr) {
os << "TupleExpr " << expr << " Type<";
print(expr->getType());
os << ">\n";
printChildren(expr->getElements());
}
void NodePrinter::printImpl(const TypeExpr *expr) {
os << "TypeExpr " << expr << " Value<\"" << expr->getValue() << "\">\n";
}
void NodePrinter::printImpl(const AttrConstraintDecl *decl) {
os << "AttrConstraintDecl " << decl << "\n";
if (const auto *typeExpr = decl->getTypeExpr())
printChildren(typeExpr);
}
void NodePrinter::printImpl(const OpConstraintDecl *decl) {
os << "OpConstraintDecl " << decl << "\n";
printChildren(decl->getNameDecl());
}
void NodePrinter::printImpl(const TypeConstraintDecl *decl) {
os << "TypeConstraintDecl " << decl << "\n";
}
void NodePrinter::printImpl(const TypeRangeConstraintDecl *decl) {
os << "TypeRangeConstraintDecl " << decl << "\n";
}
void NodePrinter::printImpl(const UserConstraintDecl *decl) {
os << "UserConstraintDecl " << decl << " Name<" << decl->getName().getName()
<< "> ResultType<" << decl->getResultType() << ">";
if (Optional<StringRef> codeBlock = decl->getCodeBlock()) {
os << " Code<";
llvm::printEscapedString(*codeBlock, os);
os << ">";
}
os << "\n";
printChildren("Inputs", decl->getInputs());
printChildren("Results", decl->getResults());
if (const CompoundStmt *body = decl->getBody())
printChildren(body);
}
void NodePrinter::printImpl(const ValueConstraintDecl *decl) {
os << "ValueConstraintDecl " << decl << "\n";
if (const auto *typeExpr = decl->getTypeExpr())
printChildren(typeExpr);
}
void NodePrinter::printImpl(const ValueRangeConstraintDecl *decl) {
os << "ValueRangeConstraintDecl " << decl << "\n";
if (const auto *typeExpr = decl->getTypeExpr())
printChildren(typeExpr);
}
void NodePrinter::printImpl(const NamedAttributeDecl *decl) {
os << "NamedAttributeDecl " << decl << " Name<" << decl->getName().getName()
<< ">\n";
printChildren(decl->getValue());
}
void NodePrinter::printImpl(const OpNameDecl *decl) {
os << "OpNameDecl " << decl;
if (Optional<StringRef> name = decl->getName())
os << " Name<" << *name << ">";
os << "\n";
}
void NodePrinter::printImpl(const PatternDecl *decl) {
os << "PatternDecl " << decl;
if (const Name *name = decl->getName())
os << " Name<" << name->getName() << ">";
if (Optional<uint16_t> benefit = decl->getBenefit())
os << " Benefit<" << *benefit << ">";
if (decl->hasBoundedRewriteRecursion())
os << " Recursion";
os << "\n";
printChildren(decl->getBody());
}
void NodePrinter::printImpl(const UserRewriteDecl *decl) {
os << "UserRewriteDecl " << decl << " Name<" << decl->getName().getName()
<< "> ResultType<" << decl->getResultType() << ">";
if (Optional<StringRef> codeBlock = decl->getCodeBlock()) {
os << " Code<";
llvm::printEscapedString(*codeBlock, os);
os << ">";
}
os << "\n";
printChildren("Inputs", decl->getInputs());
printChildren("Results", decl->getResults());
if (const CompoundStmt *body = decl->getBody())
printChildren(body);
}
void NodePrinter::printImpl(const VariableDecl *decl) {
os << "VariableDecl " << decl << " Name<" << decl->getName().getName()
<< "> Type<";
print(decl->getType());
os << ">\n";
if (Expr *initExpr = decl->getInitExpr())
printChildren(initExpr);
auto constraints =
llvm::map_range(decl->getConstraints(),
[](const ConstraintRef &ref) { return ref.constraint; });
printChildren("Constraints", constraints);
}
void NodePrinter::printImpl(const Module *module) {
os << "Module " << module << "\n";
printChildren(module->getChildren());
}
//===----------------------------------------------------------------------===//
// Entry point
//===----------------------------------------------------------------------===//
void Node::print(raw_ostream &os) const { NodePrinter(os).print(this); }
void Type::print(raw_ostream &os) const { NodePrinter(os).print(*this); }