10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/ADT/TypeSwitch.h"
12 #include "llvm/Support/SaveAndRestore.h"
13 #include "llvm/Support/ScopedPrinter.h"
26 NodePrinter(raw_ostream &os) : os(os) {}
36 template <
typename RangeT,
37 std::enable_if_t<!std::is_convertible<RangeT, const Node *>::value>
39 void printChildren(RangeT &&range) {
44 auto it = std::begin(range);
45 for (
unsigned i = 0, e = llvm::size(range) - 1; i < e; ++i, ++it)
49 elementIndentStack.back() =
true;
52 template <
typename RangeT,
typename... OthersT,
53 std::enable_if_t<std::is_convertible<RangeT, const Node *>::value>
55 void printChildren(RangeT &&range, OthersT &&...others) {
60 template <
typename RangeT>
61 void printChildren(StringRef label, RangeT &&range) {
64 elementIndentStack.reserve(elementIndentStack.size() + 1);
65 llvm::SaveAndRestore lastElement(elementIndentStack.back(),
true);
69 elementIndentStack.push_back(
false);
70 printChildren(std::forward<RangeT>(range));
71 elementIndentStack.pop_back();
77 void printImpl(
const LetStmt *stmt);
83 void printImpl(
const CallExpr *expr);
89 void printImpl(
const TypeExpr *expr);
103 void printImpl(
const Module *module);
107 if (elementIndentStack.empty())
110 for (
bool isLastElt :
llvm::ArrayRef(elementIndentStack).drop_back())
111 os << (isLastElt ?
" " :
" |");
112 os << (elementIndentStack.back() ?
" `" :
" |");
136 if (std::optional<StringRef> name = type.
getName())
137 os <<
"<" << *name <<
">";
146 llvm::interleaveComma(
149 if (!std::get<0>(it).empty())
150 os << std::get<0>(it) <<
": ";
151 this->print(std::get<1>(it));
155 .Case([&](
TypeType) { os <<
"Type"; })
157 .Default([](
Type) { llvm_unreachable(
"unknown AST type"); });
164 elementIndentStack.push_back(
false);
184 const Module>([&](
auto derivedNode) { this->printImpl(derivedNode); })
185 .Default([](
const Node *) { llvm_unreachable(
"unknown AST node"); });
186 elementIndentStack.pop_back();
190 os <<
"CompoundStmt " << stmt <<
"\n";
194 void NodePrinter::printImpl(
const EraseStmt *stmt) {
195 os <<
"EraseStmt " << stmt <<
"\n";
196 printChildren(stmt->getRootOpExpr());
199 void NodePrinter::printImpl(
const LetStmt *stmt) {
200 os <<
"LetStmt " << stmt <<
"\n";
204 void NodePrinter::printImpl(
const ReplaceStmt *stmt) {
205 os <<
"ReplaceStmt " << stmt <<
"\n";
206 printChildren(stmt->getRootOpExpr());
210 void NodePrinter::printImpl(
const ReturnStmt *stmt) {
211 os <<
"ReturnStmt " << stmt <<
"\n";
215 void NodePrinter::printImpl(
const RewriteStmt *stmt) {
216 os <<
"RewriteStmt " << stmt <<
"\n";
221 os <<
"AttributeExpr " << expr <<
" Value<\"" << expr->
getValue() <<
"\">\n";
224 void NodePrinter::printImpl(
const CallExpr *expr) {
225 os <<
"CallExpr " << expr <<
" Type<";
226 print(expr->getType());
235 void NodePrinter::printImpl(
const DeclRefExpr *expr) {
236 os <<
"DeclRefExpr " << expr <<
" Type<";
237 print(expr->getType());
239 printChildren(expr->
getDecl());
243 os <<
"MemberAccessExpr " << expr <<
" Member<" << expr->
getMemberName()
245 print(expr->getType());
251 os <<
"OperationExpr " << expr <<
" Type<";
252 print(expr->getType());
261 void NodePrinter::printImpl(
const RangeExpr *expr) {
262 os <<
"RangeExpr " << expr <<
" Type<";
269 void NodePrinter::printImpl(
const TupleExpr *expr) {
270 os <<
"TupleExpr " << expr <<
" Type<";
277 void NodePrinter::printImpl(
const TypeExpr *expr) {
278 os <<
"TypeExpr " << expr <<
" Value<\"" << expr->
getValue() <<
"\">\n";
282 os <<
"AttrConstraintDecl " << decl <<
"\n";
284 printChildren(typeExpr);
288 os <<
"OpConstraintDecl " << decl <<
"\n";
293 os <<
"TypeConstraintDecl " << decl <<
"\n";
297 os <<
"TypeRangeConstraintDecl " << decl <<
"\n";
301 os <<
"UserConstraintDecl " << decl <<
" Name<" << decl->
getName().
getName()
303 if (std::optional<StringRef> codeBlock = decl->
getCodeBlock()) {
305 llvm::printEscapedString(*codeBlock, os);
309 printChildren(
"Inputs", decl->
getInputs());
316 os <<
"ValueConstraintDecl " << decl <<
"\n";
318 printChildren(typeExpr);
322 os <<
"ValueRangeConstraintDecl " << decl <<
"\n";
324 printChildren(typeExpr);
328 os <<
"NamedAttributeDecl " << decl <<
" Name<" << decl->
getName().
getName()
333 void NodePrinter::printImpl(
const OpNameDecl *decl) {
334 os <<
"OpNameDecl " << decl;
335 if (std::optional<StringRef> name = decl->
getName())
336 os <<
" Name<" << *name <<
">";
340 void NodePrinter::printImpl(
const PatternDecl *decl) {
341 os <<
"PatternDecl " << decl;
342 if (
const Name *name = decl->getName())
343 os <<
" Name<" << name->getName() <<
">";
344 if (std::optional<uint16_t> benefit = decl->
getBenefit())
345 os <<
" Benefit<" << *benefit <<
">";
350 printChildren(decl->
getBody());
354 os <<
"UserRewriteDecl " << decl <<
" Name<" << decl->
getName().
getName()
356 if (std::optional<StringRef> codeBlock = decl->
getCodeBlock()) {
358 llvm::printEscapedString(*codeBlock, os);
362 printChildren(
"Inputs", decl->
getInputs());
369 os <<
"VariableDecl " << decl <<
" Name<" << decl->
getName().
getName()
374 printChildren(initExpr);
379 printChildren(
"Constraints", constraints);
382 void NodePrinter::printImpl(
const Module *module) {
383 os <<
"Module " << module <<
"\n";
391 void Node::print(raw_ostream &os)
const { NodePrinter(os).print(
this); }
393 void Type::print(raw_ostream &os)
const { NodePrinter(os).print(*
this); }
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
The class represents an Attribute constraint, and constrains a variable to be an Attribute.
Expr * getTypeExpr()
Return the optional type the attribute is constrained to.
This expression represents a literal MLIR Attribute, and contains the textual assembly format of that...
StringRef getValue() const
Get the raw value of this expression.
This class represents a PDLL type that corresponds to an mlir::Attribute.
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.
bool getIsNegated() const
Returns whether the result of this call is to be negated.
This statement represents a compound statement, which contains a collection of other statements.
MutableArrayRef< Stmt * > getChildren()
Return the children of this compound statement.
This class represents a PDLL type that corresponds to a constraint.
This expression represents a reference to a Decl node.
Decl * getDecl() const
Get the decl referenced by this expression.
This statement represents the erase statement in PDLL.
This class represents a base AST Expression node.
This statement represents a let statement in PDLL.
VariableDecl * getVarDecl() const
Return the variable defined by this statement.
This expression represents a named member or field access of a given parent expression.
const Expr * getParentExpr() const
Get the parent expression of this access.
StringRef getMemberName() const
Return the name of the member being accessed.
This class represents a top-level AST module.
MutableArrayRef< Decl * > getChildren()
Return the children of this module.
This Decl represents a NamedAttribute, and contains a string name and attribute value.
Expr * getValue() const
Return value of the attribute.
const Name & getName() const
Return the name of the attribute.
This class represents a base AST node.
void print(raw_ostream &os) const
Print this node to the given stream.
The class represents an Operation constraint, and constrains a variable to be an Operation.
const OpNameDecl * getNameDecl() const
Return the declaration of the operation name.
This Decl represents an OperationName.
std::optional< StringRef > getName() const
Return the name of this operation, or std::nullopt if the name is unknown.
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.
This class represents a PDLL type that corresponds to an mlir::Operation.
std::optional< StringRef > getName() const
Return the name of this operation type, or std::nullopt if it doesn't have on.
This Decl represents a single Pattern.
const CompoundStmt * getBody() const
Return the body of this pattern.
std::optional< uint16_t > getBenefit() const
Return the benefit of this pattern if specified, or std::nullopt.
bool hasBoundedRewriteRecursion() const
Return if this pattern has bounded rewrite recursion.
This expression builds a range from a set of element values (which may be ranges themselves).
MutableArrayRef< Expr * > getElements()
Return the element expressions of this range.
RangeType getType() const
Return the range result type of this expression.
This class represents a PDLL type that corresponds to a range of elements with a given element type.
Type getElementType() const
Return the element type of this range.
This statement represents the replace statement in PDLL.
MutableArrayRef< Expr * > getReplExprs()
Return the replacement values of this statement.
This statement represents a return from a "callable" like decl, e.g.
Expr * getResultExpr()
Return the result expression of this statement.
This statement represents an operation rewrite that contains a block of nested rewrite commands.
CompoundStmt * getRewriteBody() const
Return the compound rewrite body.
This class represents a PDLL type that corresponds to a rewrite reference.
This expression builds a tuple from a set of element values.
TupleType getType() const
Return the tuple result type of this expression.
MutableArrayRef< Expr * > getElements()
Return the element expressions of this tuple.
This class represents a PDLL tuple type, i.e.
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
The class represents a Type constraint, and constrains a variable to be a Type.
This expression represents a literal MLIR Type, and contains the textual assembly format of that type...
StringRef getValue() const
Get the raw value of this expression.
The class represents a TypeRange constraint, and constrains a variable to be a TypeRange.
This class represents a PDLL type that corresponds to an mlir::Type.
void print(raw_ostream &os) const
Print this type to the given stream.
This decl represents a user defined constraint.
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this constraint.
const Name & getName() const
Return the name of the constraint.
std::optional< StringRef > getCodeBlock() const
Return the optional code block of this constraint, if this is a native constraint with a provided imp...
Type getResultType() const
Return the result type of this constraint.
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.
std::optional< StringRef > getCodeBlock() const
Return the optional code block of this rewrite, if this is a native rewrite with a provided implement...
Type getResultType() const
Return the result type of this rewrite.
const Name & getName() const
Return the name of the 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.
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.
This class represents a PDLL type that corresponds to an mlir::Value.
This Decl represents the definition of a PDLL variable.
const Name & getName() const
Return the name of the decl.
Expr * getInitExpr() const
Return the initializer expression of this statement, or nullptr if there was no initializer.
MutableArrayRef< ConstraintRef > getConstraints()
Return the constraints of this variable.
Type getType() const
Return the type of the decl.
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.