11 #include "llvm/ADT/SmallPtrSet.h"
12 #include "llvm/ADT/TypeSwitch.h"
23 char *data = ctx.
getAllocator().Allocate<
char>(str.size() + 1);
26 return StringRef(data, str.size());
49 if (!node || !alreadyVisited.insert(node).second)
74 [&](
auto derivedNode) { this->visitImpl(derivedNode); })
75 .Default([](
const Node *) { llvm_unreachable(
"unknown AST node"); });
83 void visitImpl(
const EraseStmt *stmt) {
visit(stmt->getRootOpExpr()); }
86 visit(stmt->getRootOpExpr());
92 visit(stmt->getRootOpExpr());
97 void visitImpl(
const CallExpr *expr) {
121 void visitImpl(
const TypeExpr *expr) {}
154 visit(child.constraint);
157 void visitImpl(
const Module *module) {
168 return NodeVisitor(walkFn).visit(
this);
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);
183 if (
Decl *decl = decls.lookup(name))
185 return parent ? parent->
lookup(name) :
nullptr;
194 unsigned allocSize = CompoundStmt::totalSizeToAlloc<Stmt *>(children.size());
198 std::uninitialized_copy(children.begin(), children.end(),
227 unsigned allocSize = ReplaceStmt::totalSizeToAlloc<Expr *>(replExprs.size());
231 std::uninitialized_copy(replExprs.begin(), replExprs.end(),
271 unsigned allocSize = CallExpr::totalSizeToAlloc<Expr *>(arguments.size());
275 CallExpr(loc, resultType, callable, arguments.size(), isNegated);
276 std::uninitialized_copy(arguments.begin(), arguments.end(),
296 const Expr *parentExpr,
297 StringRef memberName,
Type type) {
299 loc, parentExpr, memberName.copy(ctx.
getAllocator()), type);
312 OperationExpr::totalSizeToAlloc<Expr *, NamedAttributeDecl *>(
313 operands.size() + resultTypes.size(), attributes.size());
319 OperationExpr(loc, resultType, name, operands.size(), resultTypes.size(),
320 attributes.size(), name->getLoc());
321 std::uninitialized_copy(operands.begin(), operands.end(),
323 std::uninitialized_copy(resultTypes.begin(), resultTypes.end(),
325 std::uninitialized_copy(attributes.begin(), attributes.end(),
340 unsigned allocSize = RangeExpr::totalSizeToAlloc<Expr *>(elements.size());
344 std::uninitialized_copy(elements.begin(), elements.end(),
356 unsigned allocSize = TupleExpr::totalSizeToAlloc<Expr *>(elements.size());
359 auto elementTypes = llvm::map_range(
360 elements, [](
const Expr *expr) {
return expr->
getType(); });
364 std::uninitialized_copy(elements.begin(), elements.end(),
365 expr->getElements().begin());
456 std::optional<StringRef>
458 return hasNativeInputTypes ? getTrailingObjects<StringRef>()[index]
459 : std::optional<StringRef>();
465 std::optional<StringRef> codeBlock,
const CompoundStmt *body,
467 bool hasNativeInputTypes = !nativeInputTypes.empty();
468 assert(!hasNativeInputTypes || nativeInputTypes.size() == inputs.size());
471 UserConstraintDecl::totalSizeToAlloc<VariableDecl *, StringRef>(
472 inputs.size() + results.size(),
473 hasNativeInputTypes ? inputs.size() : 0);
481 results.size(), codeBlock, body, resultType);
482 std::uninitialized_copy(inputs.begin(), inputs.end(),
484 std::uninitialized_copy(results.begin(), results.end(),
486 if (hasNativeInputTypes) {
487 StringRef *nativeInputTypesPtr = decl->getTrailingObjects<StringRef>();
488 for (
unsigned i = 0, e = inputs.size(); i < e; ++i)
521 std::optional<uint16_t> benefit,
522 bool hasBoundedRecursion,
525 PatternDecl(loc, name, benefit, hasBoundedRecursion, body);
535 std::optional<StringRef> codeBlock,
538 unsigned allocSize = UserRewriteDecl::totalSizeToAlloc<VariableDecl *>(
539 inputs.size() + results.size());
546 name, inputs.size(), results.size(), codeBlock, body, resultType);
547 std::uninitialized_copy(inputs.begin(), inputs.end(),
549 std::uninitialized_copy(results.begin(), results.end(),
562 VariableDecl::totalSizeToAlloc<ConstraintRef>(constraints.size());
566 new (rawData)
VariableDecl(name, type, initExpr, constraints.size());
567 std::uninitialized_copy(constraints.begin(), constraints.end(),
577 unsigned allocSize = Module::totalSizeToAlloc<Decl *>(children.size());
580 Module *module =
new (rawData)
Module(loc, children.size());
581 std::uninitialized_copy(children.begin(), children.end(),
static void copy(Location loc, Value dst, Value src, Value size, OpBuilder &builder)
Copies the given number of bytes from src to dst pointers.
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.
static StringRef copyStringWithNull(Context &ctx, StringRef str)
Copy a string reference into the context with a null terminator.
The class represents an Attribute constraint, and constrains a variable to be an Attribute.
Expr * typeExpr
An optional type that the attribute is constrained to.
static AttrConstraintDecl * create(Context &ctx, SMRange loc, Expr *typeExpr=nullptr)
Expr * getTypeExpr()
Return the optional type the attribute is constrained to.
AttrConstraintDecl(SMRange loc, Expr *typeExpr)
This expression represents a literal MLIR Attribute, and contains the textual assembly format of that...
static AttributeExpr * create(Context &ctx, SMRange loc, StringRef value)
This expression represents a call to a decl, such as a UserConstraintDecl/UserRewriteDecl.
Expr * getCallableExpr() const
Return the callable of this call.
MutableArrayRef< Expr * > getArguments()
Return the arguments of this call.
static CallExpr * create(Context &ctx, SMRange loc, Expr *callable, ArrayRef< Expr * > arguments, Type resultType, bool isNegated=false)
This statement represents a compound statement, which contains a collection of other statements.
MutableArrayRef< Stmt * > getChildren()
Return the children of this compound statement.
static CompoundStmt * create(Context &ctx, SMRange location, ArrayRef< Stmt * > children)
This class represents the main context of the PDLL AST.
llvm::BumpPtrAllocator & getAllocator()
Return the allocator owned by this context.
This expression represents a reference to a Decl node.
static DeclRefExpr * create(Context &ctx, SMRange loc, Decl *decl, Type type)
Decl * getDecl() const
Get the decl referenced by this expression.
Decl * lookup(StringRef name)
Lookup a decl with the given name starting from this scope.
void add(Decl *decl)
Add a new decl to the scope.
This class represents the base Decl node.
const Name * getName() const
Return the name of the decl, or nullptr if it doesn't have one.
void setDocComment(Context &ctx, StringRef comment)
Set the documentation comment for this decl.
This statement represents the erase statement in PDLL.
static EraseStmt * create(Context &ctx, SMRange loc, Expr *rootOp)
This class represents a base AST Expression node.
Type getType() const
Return the type of this expression.
This statement represents a let statement in PDLL.
VariableDecl * getVarDecl() const
Return the variable defined by this statement.
static LetStmt * create(Context &ctx, SMRange loc, VariableDecl *varDecl)
This expression represents a named member or field access of a given parent expression.
static MemberAccessExpr * create(Context &ctx, SMRange loc, const Expr *parentExpr, StringRef memberName, Type type)
const Expr * getParentExpr() const
Get the parent expression of this access.
This class represents a top-level AST module.
MutableArrayRef< Decl * > getChildren()
Return the children of this module.
static Module * create(Context &ctx, SMLoc loc, ArrayRef< Decl * > children)
This Decl represents a NamedAttribute, and contains a string name and attribute value.
Expr * getValue() const
Return value of the attribute.
static NamedAttributeDecl * create(Context &ctx, const Name &name, Expr *value)
This class represents a base AST node.
void walk(function_ref< void(const Node *)> walkFn) const
Walk all of the nodes including, and nested under, this node in pre-order.
The class represents an Operation constraint, and constrains a variable to be an Operation.
static OpConstraintDecl * create(Context &ctx, SMRange loc, const OpNameDecl *nameDecl=nullptr)
std::optional< StringRef > getName() const
Return the name of the operation, or std::nullopt if there isn't one.
OpConstraintDecl(SMRange loc, const OpNameDecl *nameDecl)
const OpNameDecl * getNameDecl() const
Return the declaration of the operation name.
const OpNameDecl * nameDecl
The operation name of this constraint.
This Decl represents an OperationName.
std::optional< StringRef > getName() const
Return the name of this operation, or std::nullopt if the name is unknown.
static OpNameDecl * create(Context &ctx, const Name &name)
This expression represents the structural form of an MLIR Operation.
MutableArrayRef< Expr * > getResultTypes()
Return the result types of this operation.
MutableArrayRef< NamedAttributeDecl * > getAttributes()
Return the attributes of this operation.
const OpNameDecl * getNameDecl() const
Return the declaration of the operation name.
MutableArrayRef< Expr * > getOperands()
Return the operands of this operation.
static OperationExpr * create(Context &ctx, SMRange loc, const ods::Operation *odsOp, const OpNameDecl *nameDecl, ArrayRef< Expr * > operands, ArrayRef< Expr * > resultTypes, ArrayRef< NamedAttributeDecl * > attributes)
std::optional< StringRef > getName() const
Return the name of the operation, or std::nullopt if there isn't one.
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.
This Decl represents a single Pattern.
const CompoundStmt * getBody() const
Return the body of this pattern.
static PatternDecl * create(Context &ctx, SMRange location, const Name *name, std::optional< uint16_t > benefit, bool hasBoundedRecursion, const CompoundStmt *body)
This expression builds a range from a set of element values (which may be ranges themselves).
static RangeExpr * create(Context &ctx, SMRange loc, ArrayRef< Expr * > elements, RangeType type)
MutableArrayRef< Expr * > getElements()
Return the element expressions of this range.
This class represents a PDLL type that corresponds to a range of elements with a given element type.
This statement represents the replace statement in PDLL.
MutableArrayRef< Expr * > getReplExprs()
Return the replacement values of this statement.
static ReplaceStmt * create(Context &ctx, SMRange loc, Expr *rootOp, ArrayRef< Expr * > replExprs)
This statement represents a return from a "callable" like decl, e.g.
static ReturnStmt * create(Context &ctx, SMRange loc, Expr *resultExpr)
Expr * getResultExpr()
Return the result expression of this statement.
This statement represents an operation rewrite that contains a block of nested rewrite commands.
static RewriteStmt * create(Context &ctx, SMRange loc, Expr *rootOp, CompoundStmt *rewriteBody)
CompoundStmt * getRewriteBody() const
Return the compound rewrite body.
This expression builds a tuple from a set of element values.
MutableArrayRef< Expr * > getElements()
Return the element expressions of this tuple.
static TupleExpr * create(Context &ctx, SMRange loc, ArrayRef< Expr * > elements, ArrayRef< StringRef > elementNames)
This class represents a PDLL tuple type, i.e.
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
The class represents a Type constraint, and constrains a variable to be a Type.
static TypeConstraintDecl * create(Context &ctx, SMRange loc)
This expression represents a literal MLIR Type, and contains the textual assembly format of that type...
static TypeExpr * create(Context &ctx, SMRange loc, StringRef value)
The class represents a TypeRange constraint, and constrains a variable to be a TypeRange.
static TypeRangeConstraintDecl * create(Context &ctx, SMRange loc)
This decl represents a user defined constraint.
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this constraint.
std::optional< StringRef > getNativeInputType(unsigned index) const
Return the explicit native type to use for the given input.
MutableArrayRef< VariableDecl * > getResults()
Return the explicit results of the constraint declaration.
const CompoundStmt * getBody() const
Return the body of this constraint if this constraint is a PDLL constraint, otherwise returns nullptr...
This decl represents a user defined rewrite.
const CompoundStmt * getBody() const
Return the body of this rewrite if this rewrite is a PDLL rewrite, otherwise returns nullptr.
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this rewrite.
MutableArrayRef< VariableDecl * > getResults()
Return the explicit results of the rewrite declaration.
The class represents a Value constraint, and constrains a variable to be a Value.
static ValueConstraintDecl * create(Context &ctx, SMRange loc, Expr *typeExpr)
Expr * typeExpr
An optional type that the value is constrained to.
ValueConstraintDecl(SMRange loc, Expr *typeExpr)
Expr * getTypeExpr()
Return the optional type the value is constrained to.
The class represents a ValueRange constraint, and constrains a variable to be a ValueRange.
Expr * getTypeExpr()
Return the optional type the value range is constrained to.
static ValueRangeConstraintDecl * create(Context &ctx, SMRange loc, Expr *typeExpr=nullptr)
ValueRangeConstraintDecl(SMRange loc, Expr *typeExpr)
Expr * typeExpr
An optional type that the value range is constrained to.
This Decl represents the definition of a PDLL variable.
Expr * getInitExpr() const
Return the initializer expression of this statement, or nullptr if there was no initializer.
static VariableDecl * create(Context &ctx, const Name &name, Type type, Expr *initExpr, ArrayRef< ConstraintRef > constraints)
MutableArrayRef< ConstraintRef > getConstraints()
Return the constraints of this variable.
This class provides an ODS representation of a specific operation.
Include the generated interface declarations.
This class represents a reference to a constraint, and contains a constraint and the location of the ...
This class provides a convenient API for interacting with source names.
StringRef getName() const
Return the raw string name.
static const Name & create(Context &ctx, StringRef name, SMRange location)