11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/TypeSwitch.h"
13 #include "llvm/Support/SaveAndRestore.h"
14 #include "llvm/Support/ScopedPrinter.h"
27 NodePrinter(raw_ostream &os) : os(os) {}
37 template <
typename RangeT,
38 std::enable_if_t<!std::is_convertible<RangeT, const Node *>::value>
40 void printChildren(RangeT &&range) {
45 auto it = std::begin(range);
46 for (
unsigned i = 0, e = llvm::size(range) - 1; i < e; ++i, ++it)
50 elementIndentStack.back() =
true;
53 template <
typename RangeT,
typename... OthersT,
54 std::enable_if_t<std::is_convertible<RangeT, const Node *>::value>
56 void printChildren(RangeT &&range, OthersT &&...others) {
61 template <
typename RangeT>
62 void printChildren(StringRef label, RangeT &&range) {
65 elementIndentStack.reserve(elementIndentStack.size() + 1);
66 llvm::SaveAndRestore lastElement(elementIndentStack.back(),
true);
70 elementIndentStack.push_back(
false);
71 printChildren(std::forward<RangeT>(range));
72 elementIndentStack.pop_back();
78 void printImpl(
const LetStmt *stmt);
84 void printImpl(
const CallExpr *expr);
90 void printImpl(
const TypeExpr *expr);
104 void printImpl(
const Module *module);
108 if (elementIndentStack.empty())
111 for (
bool isLastElt :
llvm::ArrayRef(elementIndentStack).drop_back())
112 os << (isLastElt ?
" " :
" |");
113 os << (elementIndentStack.back() ?
" `" :
" |");
137 if (std::optional<StringRef> name = type.
getName())
138 os <<
"<" << *name <<
">";
147 llvm::interleaveComma(
150 if (!std::get<0>(it).empty())
151 os << std::get<0>(it) <<
": ";
152 this->print(std::get<1>(it));
156 .Case([&](
TypeType) { os <<
"Type"; })
158 .Default([](
Type) { llvm_unreachable(
"unknown AST type"); });
165 elementIndentStack.push_back(
false);
185 const Module>([&](
auto derivedNode) { this->printImpl(derivedNode); })
186 .Default([](
const Node *) { llvm_unreachable(
"unknown AST node"); });
187 elementIndentStack.pop_back();
191 os <<
"CompoundStmt " << stmt <<
"\n";
195 void NodePrinter::printImpl(
const EraseStmt *stmt) {
196 os <<
"EraseStmt " << stmt <<
"\n";
197 printChildren(stmt->getRootOpExpr());
200 void NodePrinter::printImpl(
const LetStmt *stmt) {
201 os <<
"LetStmt " << stmt <<
"\n";
205 void NodePrinter::printImpl(
const ReplaceStmt *stmt) {
206 os <<
"ReplaceStmt " << stmt <<
"\n";
207 printChildren(stmt->getRootOpExpr());
211 void NodePrinter::printImpl(
const ReturnStmt *stmt) {
212 os <<
"ReturnStmt " << stmt <<
"\n";
216 void NodePrinter::printImpl(
const RewriteStmt *stmt) {
217 os <<
"RewriteStmt " << stmt <<
"\n";
222 os <<
"AttributeExpr " << expr <<
" Value<\"" << expr->
getValue() <<
"\">\n";
225 void NodePrinter::printImpl(
const CallExpr *expr) {
226 os <<
"CallExpr " << expr <<
" Type<";
227 print(expr->getType());
236 void NodePrinter::printImpl(
const DeclRefExpr *expr) {
237 os <<
"DeclRefExpr " << expr <<
" Type<";
238 print(expr->getType());
240 printChildren(expr->
getDecl());
244 os <<
"MemberAccessExpr " << expr <<
" Member<" << expr->
getMemberName()
246 print(expr->getType());
252 os <<
"OperationExpr " << expr <<
" Type<";
253 print(expr->getType());
262 void NodePrinter::printImpl(
const RangeExpr *expr) {
263 os <<
"RangeExpr " << expr <<
" Type<";
270 void NodePrinter::printImpl(
const TupleExpr *expr) {
271 os <<
"TupleExpr " << expr <<
" Type<";
278 void NodePrinter::printImpl(
const TypeExpr *expr) {
279 os <<
"TypeExpr " << expr <<
" Value<\"" << expr->
getValue() <<
"\">\n";
283 os <<
"AttrConstraintDecl " << decl <<
"\n";
285 printChildren(typeExpr);
289 os <<
"OpConstraintDecl " << decl <<
"\n";
294 os <<
"TypeConstraintDecl " << decl <<
"\n";
298 os <<
"TypeRangeConstraintDecl " << decl <<
"\n";
302 os <<
"UserConstraintDecl " << decl <<
" Name<" << decl->
getName().
getName()
304 if (std::optional<StringRef> codeBlock = decl->
getCodeBlock()) {
306 llvm::printEscapedString(*codeBlock, os);
310 printChildren(
"Inputs", decl->
getInputs());
317 os <<
"ValueConstraintDecl " << decl <<
"\n";
319 printChildren(typeExpr);
323 os <<
"ValueRangeConstraintDecl " << decl <<
"\n";
325 printChildren(typeExpr);
329 os <<
"NamedAttributeDecl " << decl <<
" Name<" << decl->
getName().
getName()
334 void NodePrinter::printImpl(
const OpNameDecl *decl) {
335 os <<
"OpNameDecl " << decl;
336 if (std::optional<StringRef> name = decl->
getName())
337 os <<
" Name<" << *name <<
">";
341 void NodePrinter::printImpl(
const PatternDecl *decl) {
342 os <<
"PatternDecl " << decl;
343 if (
const Name *name = decl->getName())
344 os <<
" Name<" << name->getName() <<
">";
345 if (std::optional<uint16_t> benefit = decl->
getBenefit())
346 os <<
" Benefit<" << *benefit <<
">";
351 printChildren(decl->
getBody());
355 os <<
"UserRewriteDecl " << decl <<
" Name<" << decl->
getName().
getName()
357 if (std::optional<StringRef> codeBlock = decl->
getCodeBlock()) {
359 llvm::printEscapedString(*codeBlock, os);
363 printChildren(
"Inputs", decl->
getInputs());
370 os <<
"VariableDecl " << decl <<
" Name<" << decl->
getName().
getName()
375 printChildren(initExpr);
380 printChildren(
"Constraints", constraints);
383 void NodePrinter::printImpl(
const Module *module) {
384 os <<
"Module " << module <<
"\n";
392 void Node::print(raw_ostream &os)
const { NodePrinter(os).print(
this); }
394 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.