MLIR 22.0.0git
Nodes.cpp
Go to the documentation of this file.
1//===- Nodes.cpp ----------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "llvm/ADT/SmallPtrSet.h"
12#include "llvm/ADT/TypeSwitch.h"
13#include <optional>
14
15using namespace mlir;
16using namespace mlir::pdll::ast;
17
18/// Copy a string reference into the context with a null terminator.
19static StringRef copyStringWithNull(Context &ctx, StringRef str) {
20 if (str.empty())
21 return str;
22
23 char *data = ctx.getAllocator().Allocate<char>(str.size() + 1);
24 llvm::copy(str, data);
25 data[str.size()] = 0;
26 return StringRef(data, str.size());
27}
28
29//===----------------------------------------------------------------------===//
30// Name
31//===----------------------------------------------------------------------===//
32
33const Name &Name::create(Context &ctx, StringRef name, SMRange location) {
34 return *new (ctx.getAllocator().Allocate<Name>())
35 Name(copyStringWithNull(ctx, name), location);
36}
37
38//===----------------------------------------------------------------------===//
39// Node
40//===----------------------------------------------------------------------===//
41
42namespace {
43class NodeVisitor {
44public:
45 explicit NodeVisitor(function_ref<void(const Node *)> visitFn)
46 : visitFn(visitFn) {}
47
48 void visit(const Node *node) {
49 if (!node || !alreadyVisited.insert(node).second)
50 return;
51
52 visitFn(node);
54 .Case<
55 // Statements.
56 const CompoundStmt, const EraseStmt, const LetStmt,
57 const ReplaceStmt, const ReturnStmt, const RewriteStmt,
58
59 // Expressions.
60 const AttributeExpr, const CallExpr, const DeclRefExpr,
61 const MemberAccessExpr, const OperationExpr, const RangeExpr,
62 const TupleExpr, const TypeExpr,
63
64 // Core Constraint Decls.
68
69 // Decls.
70 const NamedAttributeDecl, const OpNameDecl, const PatternDecl,
72
73 const Module>(
74 [&](auto derivedNode) { this->visitImpl(derivedNode); })
75 .DefaultUnreachable("unknown AST node");
76 }
77
78private:
79 void visitImpl(const CompoundStmt *stmt) {
80 for (const Node *child : stmt->getChildren())
81 visit(child);
82 }
83 void visitImpl(const EraseStmt *stmt) { visit(stmt->getRootOpExpr()); }
84 void visitImpl(const LetStmt *stmt) { visit(stmt->getVarDecl()); }
85 void visitImpl(const ReplaceStmt *stmt) {
86 visit(stmt->getRootOpExpr());
87 for (const Node *child : stmt->getReplExprs())
88 visit(child);
89 }
90 void visitImpl(const ReturnStmt *stmt) { visit(stmt->getResultExpr()); }
91 void visitImpl(const RewriteStmt *stmt) {
92 visit(stmt->getRootOpExpr());
93 visit(stmt->getRewriteBody());
94 }
95
96 void visitImpl(const AttributeExpr *expr) {}
97 void visitImpl(const CallExpr *expr) {
98 visit(expr->getCallableExpr());
99 for (const Node *child : expr->getArguments())
100 visit(child);
101 }
102 void visitImpl(const DeclRefExpr *expr) { visit(expr->getDecl()); }
103 void visitImpl(const MemberAccessExpr *expr) { visit(expr->getParentExpr()); }
104 void visitImpl(const OperationExpr *expr) {
105 visit(expr->getNameDecl());
106 for (const Node *child : expr->getOperands())
107 visit(child);
108 for (const Node *child : expr->getResultTypes())
109 visit(child);
110 for (const Node *child : expr->getAttributes())
111 visit(child);
112 }
113 void visitImpl(const RangeExpr *expr) {
114 for (const Node *child : expr->getElements())
115 visit(child);
116 }
117 void visitImpl(const TupleExpr *expr) {
118 for (const Node *child : expr->getElements())
119 visit(child);
120 }
121 void visitImpl(const TypeExpr *expr) {}
122
123 void visitImpl(const AttrConstraintDecl *decl) { visit(decl->getTypeExpr()); }
124 void visitImpl(const OpConstraintDecl *decl) { visit(decl->getNameDecl()); }
125 void visitImpl(const TypeConstraintDecl *decl) {}
126 void visitImpl(const TypeRangeConstraintDecl *decl) {}
127 void visitImpl(const ValueConstraintDecl *decl) {
128 visit(decl->getTypeExpr());
129 }
130 void visitImpl(const ValueRangeConstraintDecl *decl) {
131 visit(decl->getTypeExpr());
132 }
133
134 void visitImpl(const NamedAttributeDecl *decl) { visit(decl->getValue()); }
135 void visitImpl(const OpNameDecl *decl) {}
136 void visitImpl(const PatternDecl *decl) { visit(decl->getBody()); }
137 void visitImpl(const UserConstraintDecl *decl) {
138 for (const Node *child : decl->getInputs())
139 visit(child);
140 for (const Node *child : decl->getResults())
141 visit(child);
142 visit(decl->getBody());
143 }
144 void visitImpl(const UserRewriteDecl *decl) {
145 for (const Node *child : decl->getInputs())
146 visit(child);
147 for (const Node *child : decl->getResults())
148 visit(child);
149 visit(decl->getBody());
150 }
151 void visitImpl(const VariableDecl *decl) {
152 visit(decl->getInitExpr());
153 for (const ConstraintRef &child : decl->getConstraints())
154 visit(child.constraint);
155 }
156
157 void visitImpl(const Module *module) {
158 for (const Node *child : module->getChildren())
159 visit(child);
160 }
161
162 function_ref<void(const Node *)> visitFn;
163 SmallPtrSet<const Node *, 16> alreadyVisited;
164};
165} // namespace
166
167void Node::walk(function_ref<void(const Node *)> walkFn) const {
168 return NodeVisitor(walkFn).visit(this);
169}
170
171//===----------------------------------------------------------------------===//
172// DeclScope
173//===----------------------------------------------------------------------===//
174
175void DeclScope::add(Decl *decl) {
176 const Name *name = decl->getName();
177 assert(name && "expected a named decl");
178 assert(!decls.count(name->getName()) && "decl with this name already exists");
179 decls.try_emplace(name->getName(), decl);
180}
181
182Decl *DeclScope::lookup(StringRef name) {
183 if (Decl *decl = decls.lookup(name))
184 return decl;
185 return parent ? parent->lookup(name) : nullptr;
186}
187
188//===----------------------------------------------------------------------===//
189// CompoundStmt
190//===----------------------------------------------------------------------===//
191
192CompoundStmt *CompoundStmt::create(Context &ctx, SMRange loc,
193 ArrayRef<Stmt *> children) {
194 unsigned allocSize = CompoundStmt::totalSizeToAlloc<Stmt *>(children.size());
195 void *rawData = ctx.getAllocator().Allocate(allocSize, alignof(CompoundStmt));
196
197 CompoundStmt *stmt = new (rawData) CompoundStmt(loc, children.size());
198 llvm::uninitialized_copy(children, stmt->getChildren().begin());
199 return stmt;
200}
201
202//===----------------------------------------------------------------------===//
203// LetStmt
204//===----------------------------------------------------------------------===//
205
206LetStmt *LetStmt::create(Context &ctx, SMRange loc, VariableDecl *varDecl) {
207 return new (ctx.getAllocator().Allocate<LetStmt>()) LetStmt(loc, varDecl);
208}
209
210//===----------------------------------------------------------------------===//
211// OpRewriteStmt
212//===----------------------------------------------------------------------===//
213
214//===----------------------------------------------------------------------===//
215// EraseStmt
216//===----------------------------------------------------------------------===//
217
218EraseStmt *EraseStmt::create(Context &ctx, SMRange loc, Expr *rootOp) {
219 return new (ctx.getAllocator().Allocate<EraseStmt>()) EraseStmt(loc, rootOp);
220}
221
222//===----------------------------------------------------------------------===//
223// ReplaceStmt
224//===----------------------------------------------------------------------===//
225
226ReplaceStmt *ReplaceStmt::create(Context &ctx, SMRange loc, Expr *rootOp,
227 ArrayRef<Expr *> replExprs) {
228 unsigned allocSize = ReplaceStmt::totalSizeToAlloc<Expr *>(replExprs.size());
229 void *rawData = ctx.getAllocator().Allocate(allocSize, alignof(ReplaceStmt));
230
231 ReplaceStmt *stmt = new (rawData) ReplaceStmt(loc, rootOp, replExprs.size());
232 llvm::uninitialized_copy(replExprs, stmt->getReplExprs().begin());
233 return stmt;
234}
235
236//===----------------------------------------------------------------------===//
237// RewriteStmt
238//===----------------------------------------------------------------------===//
239
240RewriteStmt *RewriteStmt::create(Context &ctx, SMRange loc, Expr *rootOp,
241 CompoundStmt *rewriteBody) {
242 return new (ctx.getAllocator().Allocate<RewriteStmt>())
243 RewriteStmt(loc, rootOp, rewriteBody);
244}
245
246//===----------------------------------------------------------------------===//
247// ReturnStmt
248//===----------------------------------------------------------------------===//
249
250ReturnStmt *ReturnStmt::create(Context &ctx, SMRange loc, Expr *resultExpr) {
251 return new (ctx.getAllocator().Allocate<ReturnStmt>())
252 ReturnStmt(loc, resultExpr);
253}
254
255//===----------------------------------------------------------------------===//
256// AttributeExpr
257//===----------------------------------------------------------------------===//
258
259AttributeExpr *AttributeExpr::create(Context &ctx, SMRange loc,
260 StringRef value) {
261 return new (ctx.getAllocator().Allocate<AttributeExpr>())
262 AttributeExpr(ctx, loc, copyStringWithNull(ctx, value));
263}
264
265//===----------------------------------------------------------------------===//
266// CallExpr
267//===----------------------------------------------------------------------===//
268
269CallExpr *CallExpr::create(Context &ctx, SMRange loc, Expr *callable,
270 ArrayRef<Expr *> arguments, Type resultType,
271 bool isNegated) {
272 unsigned allocSize = CallExpr::totalSizeToAlloc<Expr *>(arguments.size());
273 void *rawData = ctx.getAllocator().Allocate(allocSize, alignof(CallExpr));
274
275 CallExpr *expr = new (rawData)
276 CallExpr(loc, resultType, callable, arguments.size(), isNegated);
277 llvm::uninitialized_copy(arguments, expr->getArguments().begin());
278 return expr;
279}
280
281//===----------------------------------------------------------------------===//
282// DeclRefExpr
283//===----------------------------------------------------------------------===//
284
285DeclRefExpr *DeclRefExpr::create(Context &ctx, SMRange loc, Decl *decl,
286 Type type) {
287 return new (ctx.getAllocator().Allocate<DeclRefExpr>())
288 DeclRefExpr(loc, decl, type);
289}
290
291//===----------------------------------------------------------------------===//
292// MemberAccessExpr
293//===----------------------------------------------------------------------===//
294
295MemberAccessExpr *MemberAccessExpr::create(Context &ctx, SMRange loc,
296 const Expr *parentExpr,
297 StringRef memberName, Type type) {
298 return new (ctx.getAllocator().Allocate<MemberAccessExpr>()) MemberAccessExpr(
299 loc, parentExpr, memberName.copy(ctx.getAllocator()), type);
300}
301
302//===----------------------------------------------------------------------===//
303// OperationExpr
304//===----------------------------------------------------------------------===//
305
307OperationExpr::create(Context &ctx, SMRange loc, const ods::Operation *odsOp,
308 const OpNameDecl *name, ArrayRef<Expr *> operands,
309 ArrayRef<Expr *> resultTypes,
311 unsigned allocSize =
312 OperationExpr::totalSizeToAlloc<Expr *, NamedAttributeDecl *>(
313 operands.size() + resultTypes.size(), attributes.size());
314 void *rawData =
315 ctx.getAllocator().Allocate(allocSize, alignof(OperationExpr));
316
317 Type resultType = OperationType::get(ctx, name->getName(), odsOp);
318 OperationExpr *opExpr = new (rawData)
319 OperationExpr(loc, resultType, name, operands.size(), resultTypes.size(),
320 attributes.size(), name->getLoc());
321 llvm::uninitialized_copy(operands, opExpr->getOperands().begin());
322 llvm::uninitialized_copy(resultTypes, opExpr->getResultTypes().begin());
323 llvm::uninitialized_copy(attributes, opExpr->getAttributes().begin());
324 return opExpr;
325}
326
327std::optional<StringRef> OperationExpr::getName() const {
328 return getNameDecl()->getName();
329}
330
331//===----------------------------------------------------------------------===//
332// RangeExpr
333//===----------------------------------------------------------------------===//
334
335RangeExpr *RangeExpr::create(Context &ctx, SMRange loc,
336 ArrayRef<Expr *> elements, RangeType type) {
337 unsigned allocSize = RangeExpr::totalSizeToAlloc<Expr *>(elements.size());
338 void *rawData = ctx.getAllocator().Allocate(allocSize, alignof(TupleExpr));
339
340 RangeExpr *expr = new (rawData) RangeExpr(loc, type, elements.size());
341 llvm::uninitialized_copy(elements, expr->getElements().begin());
342 return expr;
343}
344
345//===----------------------------------------------------------------------===//
346// TupleExpr
347//===----------------------------------------------------------------------===//
348
349TupleExpr *TupleExpr::create(Context &ctx, SMRange loc,
350 ArrayRef<Expr *> elements,
351 ArrayRef<StringRef> names) {
352 unsigned allocSize = TupleExpr::totalSizeToAlloc<Expr *>(elements.size());
353 void *rawData = ctx.getAllocator().Allocate(allocSize, alignof(TupleExpr));
354
355 auto elementTypes = llvm::map_range(
356 elements, [](const Expr *expr) { return expr->getType(); });
357 TupleType type = TupleType::get(ctx, llvm::to_vector(elementTypes), names);
358
359 TupleExpr *expr = new (rawData) TupleExpr(loc, type);
360 llvm::uninitialized_copy(elements, expr->getElements().begin());
361 return expr;
362}
363
364//===----------------------------------------------------------------------===//
365// TypeExpr
366//===----------------------------------------------------------------------===//
367
368TypeExpr *TypeExpr::create(Context &ctx, SMRange loc, StringRef value) {
369 return new (ctx.getAllocator().Allocate<TypeExpr>())
370 TypeExpr(ctx, loc, copyStringWithNull(ctx, value));
371}
372
373//===----------------------------------------------------------------------===//
374// Decl
375//===----------------------------------------------------------------------===//
376
377void Decl::setDocComment(Context &ctx, StringRef comment) {
378 docComment = comment.copy(ctx.getAllocator());
379}
380
381//===----------------------------------------------------------------------===//
382// AttrConstraintDecl
383//===----------------------------------------------------------------------===//
384
386 Expr *typeExpr) {
387 return new (ctx.getAllocator().Allocate<AttrConstraintDecl>())
389}
390
391//===----------------------------------------------------------------------===//
392// OpConstraintDecl
393//===----------------------------------------------------------------------===//
394
396 const OpNameDecl *nameDecl) {
397 if (!nameDecl)
398 nameDecl = OpNameDecl::create(ctx, SMRange());
399
400 return new (ctx.getAllocator().Allocate<OpConstraintDecl>())
402}
403
404std::optional<StringRef> OpConstraintDecl::getName() const {
405 return getNameDecl()->getName();
406}
407
408//===----------------------------------------------------------------------===//
409// TypeConstraintDecl
410//===----------------------------------------------------------------------===//
411
413 return new (ctx.getAllocator().Allocate<TypeConstraintDecl>())
415}
416
417//===----------------------------------------------------------------------===//
418// TypeRangeConstraintDecl
419//===----------------------------------------------------------------------===//
420
422 SMRange loc) {
423 return new (ctx.getAllocator().Allocate<TypeRangeConstraintDecl>())
425}
426
427//===----------------------------------------------------------------------===//
428// ValueConstraintDecl
429//===----------------------------------------------------------------------===//
430
432 Expr *typeExpr) {
433 return new (ctx.getAllocator().Allocate<ValueConstraintDecl>())
435}
436
437//===----------------------------------------------------------------------===//
438// ValueRangeConstraintDecl
439//===----------------------------------------------------------------------===//
440
443 return new (ctx.getAllocator().Allocate<ValueRangeConstraintDecl>())
445}
446
447//===----------------------------------------------------------------------===//
448// UserConstraintDecl
449//===----------------------------------------------------------------------===//
450
451std::optional<StringRef>
453 return hasNativeInputTypes ? getTrailingObjects<StringRef>()[index]
454 : std::optional<StringRef>();
455}
456
457UserConstraintDecl *UserConstraintDecl::createImpl(
458 Context &ctx, const Name &name, ArrayRef<VariableDecl *> inputs,
459 ArrayRef<StringRef> nativeInputTypes, ArrayRef<VariableDecl *> results,
460 std::optional<StringRef> codeBlock, const CompoundStmt *body,
461 Type resultType) {
462 bool hasNativeInputTypes = !nativeInputTypes.empty();
463 assert(!hasNativeInputTypes || nativeInputTypes.size() == inputs.size());
464
465 unsigned allocSize =
466 UserConstraintDecl::totalSizeToAlloc<VariableDecl *, StringRef>(
467 inputs.size() + results.size(),
468 hasNativeInputTypes ? inputs.size() : 0);
469 void *rawData =
470 ctx.getAllocator().Allocate(allocSize, alignof(UserConstraintDecl));
471 if (codeBlock)
472 codeBlock = codeBlock->copy(ctx.getAllocator());
473
474 UserConstraintDecl *decl = new (rawData)
475 UserConstraintDecl(name, inputs.size(), hasNativeInputTypes,
476 results.size(), codeBlock, body, resultType);
477 llvm::uninitialized_copy(inputs, decl->getInputs().begin());
478 llvm::uninitialized_copy(results, decl->getResults().begin());
479 if (hasNativeInputTypes) {
480 StringRef *nativeInputTypesPtr = decl->getTrailingObjects<StringRef>();
481 for (unsigned i = 0, e = inputs.size(); i < e; ++i)
482 nativeInputTypesPtr[i] = nativeInputTypes[i].copy(ctx.getAllocator());
483 }
484
485 return decl;
486}
487
488//===----------------------------------------------------------------------===//
489// NamedAttributeDecl
490//===----------------------------------------------------------------------===//
491
492NamedAttributeDecl *NamedAttributeDecl::create(Context &ctx, const Name &name,
493 Expr *value) {
494 return new (ctx.getAllocator().Allocate<NamedAttributeDecl>())
495 NamedAttributeDecl(name, value);
496}
497
498//===----------------------------------------------------------------------===//
499// OpNameDecl
500//===----------------------------------------------------------------------===//
501
502OpNameDecl *OpNameDecl::create(Context &ctx, const Name &name) {
503 return new (ctx.getAllocator().Allocate<OpNameDecl>()) OpNameDecl(name);
504}
505OpNameDecl *OpNameDecl::create(Context &ctx, SMRange loc) {
506 return new (ctx.getAllocator().Allocate<OpNameDecl>()) OpNameDecl(loc);
507}
508
509//===----------------------------------------------------------------------===//
510// PatternDecl
511//===----------------------------------------------------------------------===//
512
513PatternDecl *PatternDecl::create(Context &ctx, SMRange loc, const Name *name,
514 std::optional<uint16_t> benefit,
515 bool hasBoundedRecursion,
516 const CompoundStmt *body) {
517 return new (ctx.getAllocator().Allocate<PatternDecl>())
518 PatternDecl(loc, name, benefit, hasBoundedRecursion, body);
519}
520
521//===----------------------------------------------------------------------===//
522// UserRewriteDecl
523//===----------------------------------------------------------------------===//
524
525UserRewriteDecl *UserRewriteDecl::createImpl(Context &ctx, const Name &name,
528 std::optional<StringRef> codeBlock,
529 const CompoundStmt *body,
530 Type resultType) {
531 unsigned allocSize = UserRewriteDecl::totalSizeToAlloc<VariableDecl *>(
532 inputs.size() + results.size());
533 void *rawData =
534 ctx.getAllocator().Allocate(allocSize, alignof(UserRewriteDecl));
535 if (codeBlock)
536 codeBlock = codeBlock->copy(ctx.getAllocator());
537
538 UserRewriteDecl *decl = new (rawData) UserRewriteDecl(
539 name, inputs.size(), results.size(), codeBlock, body, resultType);
540 llvm::uninitialized_copy(inputs, decl->getInputs().begin());
541 llvm::uninitialized_copy(results, decl->getResults().begin());
542 return decl;
543}
544
545//===----------------------------------------------------------------------===//
546// VariableDecl
547//===----------------------------------------------------------------------===//
548
549VariableDecl *VariableDecl::create(Context &ctx, const Name &name, Type type,
550 Expr *initExpr,
551 ArrayRef<ConstraintRef> constraints) {
552 unsigned allocSize =
553 VariableDecl::totalSizeToAlloc<ConstraintRef>(constraints.size());
554 void *rawData = ctx.getAllocator().Allocate(allocSize, alignof(VariableDecl));
555
556 VariableDecl *varDecl =
557 new (rawData) VariableDecl(name, type, initExpr, constraints.size());
558 llvm::uninitialized_copy(constraints, varDecl->getConstraints().begin());
559 return varDecl;
560}
561
562//===----------------------------------------------------------------------===//
563// Module
564//===----------------------------------------------------------------------===//
565
566Module *Module::create(Context &ctx, SMLoc loc, ArrayRef<Decl *> children) {
567 unsigned allocSize = Module::totalSizeToAlloc<Decl *>(children.size());
568 void *rawData = ctx.getAllocator().Allocate(allocSize, alignof(Module));
569
570 Module *module = new (rawData) Module(loc, children.size());
571 llvm::uninitialized_copy(children, module->getChildren().begin());
572 return module;
573}
MemRefDependenceGraph::Node Node
Definition Utils.cpp:38
static void visit(Operation *op, DenseSet< Operation * > &visited)
Visits all the pdl.operand(s), pdl.result(s), and pdl.operation(s) connected to the given operation.
Definition PDL.cpp:62
static StringRef copyStringWithNull(Context &ctx, StringRef str)
Copy a string reference into the context with a null terminator.
Definition Nodes.cpp:19
The class represents an Attribute constraint, and constrains a variable to be an Attribute.
Definition Nodes.h:750
Expr * getTypeExpr()
Return the optional type the attribute is constrained to.
Definition Nodes.h:756
Expr * typeExpr
An optional type that the attribute is constrained to.
Definition Nodes.h:764
static AttrConstraintDecl * create(Context &ctx, SMRange loc, Expr *typeExpr=nullptr)
Definition Nodes.cpp:385
AttrConstraintDecl(SMRange loc, Expr *typeExpr)
Definition Nodes.h:760
This expression represents a literal MLIR Attribute, and contains the textual assembly format of that...
Definition Nodes.h:370
static AttributeExpr * create(Context &ctx, SMRange loc, StringRef value)
Definition Nodes.cpp:259
This expression represents a call to a decl, such as a UserConstraintDecl/UserRewriteDecl.
Definition Nodes.h:393
Expr * getCallableExpr() const
Return the callable of this call.
Definition Nodes.h:400
MutableArrayRef< Expr * > getArguments()
Return the arguments of this call.
Definition Nodes.h:403
static CallExpr * create(Context &ctx, SMRange loc, Expr *callable, ArrayRef< Expr * > arguments, Type resultType, bool isNegated=false)
Definition Nodes.cpp:269
This statement represents a compound statement, which contains a collection of other statements.
Definition Nodes.h:179
MutableArrayRef< Stmt * > getChildren()
Return the children of this compound statement.
Definition Nodes.h:185
static CompoundStmt * create(Context &ctx, SMRange location, ArrayRef< Stmt * > children)
Definition Nodes.cpp:192
This class represents the main context of the PDLL AST.
Definition Context.h:25
llvm::BumpPtrAllocator & getAllocator()
Return the allocator owned by this context.
Definition Context.h:32
This expression represents a reference to a Decl node.
Definition Nodes.h:433
Decl * getDecl() const
Get the decl referenced by this expression.
Definition Nodes.h:438
static DeclRefExpr * create(Context &ctx, SMRange loc, Decl *decl, Type type)
Definition Nodes.cpp:285
Decl * lookup(StringRef name)
Lookup a decl with the given name starting from this scope.
Definition Nodes.cpp:182
void add(Decl *decl)
Add a new decl to the scope.
Definition Nodes.cpp:175
This class represents the base Decl node.
Definition Nodes.h:669
void setDocComment(Context &ctx, StringRef comment)
Set the documentation comment for this decl.
Definition Nodes.cpp:377
const Name * getName() const
Return the name of the decl, or nullptr if it doesn't have one.
Definition Nodes.h:672
This statement represents the erase statement in PDLL.
Definition Nodes.h:255
static EraseStmt * create(Context &ctx, SMRange loc, Expr *rootOp)
Definition Nodes.cpp:218
This class represents a base AST Expression node.
Definition Nodes.h:348
Type getType() const
Return the type of this expression.
Definition Nodes.h:351
This statement represents a let statement in PDLL.
Definition Nodes.h:211
VariableDecl * getVarDecl() const
Return the variable defined by this statement.
Definition Nodes.h:216
static LetStmt * create(Context &ctx, SMRange loc, VariableDecl *varDecl)
Definition Nodes.cpp:206
This expression represents a named member or field access of a given parent expression.
Definition Nodes.h:454
static MemberAccessExpr * create(Context &ctx, SMRange loc, const Expr *parentExpr, StringRef memberName, Type type)
Definition Nodes.cpp:295
const Expr * getParentExpr() const
Get the parent expression of this access.
Definition Nodes.h:461
This class represents a top-level AST module.
Definition Nodes.h:1297
static Module * create(Context &ctx, SMLoc loc, ArrayRef< Decl * > children)
Definition Nodes.cpp:566
MutableArrayRef< Decl * > getChildren()
Return the children of this module.
Definition Nodes.h:1302
This Decl represents a NamedAttribute, and contains a string name and attribute value.
Definition Nodes.h:998
static NamedAttributeDecl * create(Context &ctx, const Name &name, Expr *value)
Definition Nodes.cpp:492
Expr * getValue() const
Return value of the attribute.
Definition Nodes.h:1007
This class represents a base AST node.
Definition Nodes.h:108
Node(TypeID typeID, SMRange loc)
Definition Nodes.h:149
SMRange getLoc() const
Return the location of this node.
Definition Nodes.h:131
The class represents an Operation constraint, and constrains a variable to be an Operation.
Definition Nodes.h:774
static OpConstraintDecl * create(Context &ctx, SMRange loc, const OpNameDecl *nameDecl=nullptr)
Definition Nodes.cpp:395
std::optional< StringRef > getName() const
Return the name of the operation, or std::nullopt if there isn't one.
Definition Nodes.cpp:404
OpConstraintDecl(SMRange loc, const OpNameDecl *nameDecl)
Definition Nodes.h:786
const OpNameDecl * getNameDecl() const
Return the declaration of the operation name.
Definition Nodes.h:783
const OpNameDecl * nameDecl
The operation name of this constraint.
Definition Nodes.h:790
This Decl represents an OperationName.
Definition Nodes.h:1022
std::optional< StringRef > getName() const
Return the name of this operation, or std::nullopt if the name is unknown.
Definition Nodes.h:1028
static OpNameDecl * create(Context &ctx, const Name &name)
Definition Nodes.cpp:502
Expr * rootOp
The root operation being rewritten.
Definition Nodes.h:245
Expr * getRootOpExpr() const
Return the root operation of this rewrite.
Definition Nodes.h:237
This expression represents the structural form of an MLIR Operation.
Definition Nodes.h:512
MutableArrayRef< Expr * > getOperands()
Return the operands of this operation.
Definition Nodes.h:532
MutableArrayRef< NamedAttributeDecl * > getAttributes()
Return the attributes of this operation.
Definition Nodes.h:548
const OpNameDecl * getNameDecl() const
Return the declaration of the operation name.
Definition Nodes.h:525
MutableArrayRef< Expr * > getResultTypes()
Return the result types of this operation.
Definition Nodes.h:540
static OperationExpr * create(Context &ctx, SMRange loc, const ods::Operation *odsOp, const OpNameDecl *nameDecl, ArrayRef< Expr * > operands, ArrayRef< Expr * > resultTypes, ArrayRef< NamedAttributeDecl * > attributes)
Definition Nodes.cpp:307
std::optional< StringRef > getName() const
Return the name of the operation, or std::nullopt if there isn't one.
Definition Nodes.cpp:327
static OperationType get(Context &context, std::optional< StringRef > name=std::nullopt, const ods::Operation *odsOp=nullptr)
Return an instance of the Operation type with an optional operation name.
Definition Types.cpp:73
This Decl represents a single Pattern.
Definition Nodes.h:1043
static PatternDecl * create(Context &ctx, SMRange location, const Name *name, std::optional< uint16_t > benefit, bool hasBoundedRecursion, const CompoundStmt *body)
Definition Nodes.cpp:513
const CompoundStmt * getBody() const
Return the body of this pattern.
Definition Nodes.h:1057
This expression builds a range from a set of element values (which may be ranges themselves).
Definition Nodes.h:586
static RangeExpr * create(Context &ctx, SMRange loc, ArrayRef< Expr * > elements, RangeType type)
Definition Nodes.cpp:335
MutableArrayRef< Expr * > getElements()
Return the element expressions of this range.
Definition Nodes.h:592
This class represents a PDLL type that corresponds to a range of elements with a given element type.
Definition Types.h:159
This statement represents the replace statement in PDLL.
Definition Nodes.h:271
MutableArrayRef< Expr * > getReplExprs()
Return the replacement values of this statement.
Definition Nodes.h:277
static ReplaceStmt * create(Context &ctx, SMRange loc, Expr *rootOp, ArrayRef< Expr * > replExprs)
Definition Nodes.cpp:226
This statement represents a return from a "callable" like decl, e.g.
Definition Nodes.h:324
static ReturnStmt * create(Context &ctx, SMRange loc, Expr *resultExpr)
Definition Nodes.cpp:250
Expr * getResultExpr()
Return the result expression of this statement.
Definition Nodes.h:329
This statement represents an operation rewrite that contains a block of nested rewrite commands.
Definition Nodes.h:302
CompoundStmt * getRewriteBody() const
Return the compound rewrite body.
Definition Nodes.h:308
static RewriteStmt * create(Context &ctx, SMRange loc, Expr *rootOp, CompoundStmt *rewriteBody)
Definition Nodes.cpp:240
This expression builds a tuple from a set of element values.
Definition Nodes.h:619
static TupleExpr * create(Context &ctx, SMRange loc, ArrayRef< Expr * > elements, ArrayRef< StringRef > elementNames)
Definition Nodes.cpp:349
MutableArrayRef< Expr * > getElements()
Return the element expressions of this tuple.
Definition Nodes.h:625
This class represents a PDLL tuple type, i.e.
Definition Types.h:222
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
Definition Types.cpp:144
The class represents a Type constraint, and constrains a variable to be a Type.
Definition Nodes.h:800
static TypeConstraintDecl * create(Context &ctx, SMRange loc)
Definition Nodes.cpp:412
This expression represents a literal MLIR Type, and contains the textual assembly format of that type...
Definition Nodes.h:648
static TypeExpr * create(Context &ctx, SMRange loc, StringRef value)
Definition Nodes.cpp:368
The class represents a TypeRange constraint, and constrains a variable to be a TypeRange.
Definition Nodes.h:815
static TypeRangeConstraintDecl * create(Context &ctx, SMRange loc)
Definition Nodes.cpp:421
This decl represents a user defined constraint.
Definition Nodes.h:888
const CompoundStmt * getBody() const
Return the body of this constraint if this constraint is a PDLL constraint, otherwise returns nullptr...
Definition Nodes.h:940
std::optional< StringRef > getNativeInputType(unsigned index) const
Return the explicit native type to use for the given input.
Definition Nodes.cpp:452
MutableArrayRef< VariableDecl * > getResults()
Return the explicit results of the constraint declaration.
Definition Nodes.h:927
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this constraint.
Definition Nodes.h:914
This decl represents a user defined rewrite.
Definition Nodes.h:1098
const CompoundStmt * getBody() const
Return the body of this rewrite if this rewrite is a PDLL rewrite, otherwise returns nullptr.
Definition Nodes.h:1146
MutableArrayRef< VariableDecl * > getResults()
Return the explicit results of the rewrite declaration.
Definition Nodes.h:1133
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this rewrite.
Definition Nodes.h:1124
The class represents a Value constraint, and constrains a variable to be a Value.
Definition Nodes.h:830
static ValueConstraintDecl * create(Context &ctx, SMRange loc, Expr *typeExpr)
Definition Nodes.cpp:431
Expr * typeExpr
An optional type that the value is constrained to.
Definition Nodes.h:843
ValueConstraintDecl(SMRange loc, Expr *typeExpr)
Definition Nodes.h:839
Expr * getTypeExpr()
Return the optional type the value is constrained to.
Definition Nodes.h:835
The class represents a ValueRange constraint, and constrains a variable to be a ValueRange.
Definition Nodes.h:853
static ValueRangeConstraintDecl * create(Context &ctx, SMRange loc, Expr *typeExpr=nullptr)
Definition Nodes.cpp:442
ValueRangeConstraintDecl(SMRange loc, Expr *typeExpr)
Definition Nodes.h:863
Expr * typeExpr
An optional type that the value range is constrained to.
Definition Nodes.h:867
Expr * getTypeExpr()
Return the optional type the value range is constrained to.
Definition Nodes.h:859
This Decl represents the definition of a PDLL variable.
Definition Nodes.h:1248
MutableArrayRef< ConstraintRef > getConstraints()
Return the constraints of this variable.
Definition Nodes.h:1255
static VariableDecl * create(Context &ctx, const Name &name, Type type, Expr *initExpr, ArrayRef< ConstraintRef > constraints)
Definition Nodes.cpp:549
Expr * getInitExpr() const
Return the initializer expression of this statement, or nullptr if there was no initializer.
Definition Nodes.h:1264
This class provides an ODS representation of a specific operation.
Definition Operation.h:125
Include the generated interface declarations.
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:144
llvm::function_ref< Fn > function_ref
Definition LLVM.h:152
This class provides a convenient API for interacting with source names.
Definition Nodes.h:37
StringRef getName() const
Return the raw string name.
Definition Nodes.h:41
static const Name & create(Context &ctx, StringRef name, SMRange location)
Definition Nodes.cpp:33