diff --git a/mlir/include/mlir/Tools/PDLL/AST/Nodes.h b/mlir/include/mlir/Tools/PDLL/AST/Nodes.h index 27abccf72964e79..c2ca71c8fecbde0 100644 --- a/mlir/include/mlir/Tools/PDLL/AST/Nodes.h +++ b/mlir/include/mlir/Tools/PDLL/AST/Nodes.h @@ -28,6 +28,8 @@ class NamedAttributeDecl; class OpNameDecl; class VariableDecl; +StringRef copyStringWithNull(Context &ctx, StringRef str); + //===----------------------------------------------------------------------===// // Name //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Tools/PDLL/AST/Nodes.cpp b/mlir/lib/Tools/PDLL/AST/Nodes.cpp index 7c838e103ac02d2..c7cc8708859d5b1 100644 --- a/mlir/lib/Tools/PDLL/AST/Nodes.cpp +++ b/mlir/lib/Tools/PDLL/AST/Nodes.cpp @@ -16,7 +16,7 @@ using namespace mlir; using namespace mlir::pdll::ast; /// Copy a string reference into the context with a null terminator. -static StringRef copyStringWithNull(Context &ctx, StringRef str) { +StringRef mlir::pdll::ast::copyStringWithNull(Context &ctx, StringRef str) { if (str.empty()) return str; diff --git a/mlir/lib/Tools/PDLL/Parser/Parser.cpp b/mlir/lib/Tools/PDLL/Parser/Parser.cpp index bf035bb07b3634b..9ce4cb30fb2b9a9 100644 --- a/mlir/lib/Tools/PDLL/Parser/Parser.cpp +++ b/mlir/lib/Tools/PDLL/Parser/Parser.cpp @@ -325,6 +325,7 @@ class Parser { FailureOr parseInlineRewriteLambdaExpr(); FailureOr parseMemberAccessExpr(ast::Expr *parentExpr); FailureOr parseNegatedExpr(); + FailureOr parseIntegerExpr(); FailureOr parseOperationName(bool allowEmptyName = false); FailureOr parseWrappedOperationName(bool allowEmptyName); FailureOr @@ -1834,6 +1835,9 @@ FailureOr Parser::parseExpr() { case Token::exclam: lhsExpr = parseNegatedExpr(); break; + case Token::integer: + lhsExpr = parseIntegerExpr(); + break; case Token::string_block: return emitError("expected expression. If you are trying to create an " "ArrayAttr, use a space between `[` and `{`."); @@ -2077,6 +2081,25 @@ FailureOr Parser::parseNegatedExpr() { return parseCallExpr(*identifierExpr, /*isNegated = */ true); } +/// Parse +/// integer : identifier +/// into an AttributeExpr. +/// Examples: '4 : i32', '0 : si1' +FailureOr Parser::parseIntegerExpr() { + SMRange loc = curToken.getLoc(); + StringRef value = curToken.getSpelling(); + consumeToken(); + if (!consumeIf(Token::colon)) + return emitError("expected colon after integer literal"); + if (!curToken.is(Token::identifier)) + return emitError("expected integer type"); + StringRef type = curToken.getSpelling(); + consumeToken(); + + auto allocated = copyStringWithNull(ctx, (Twine(value) + ":" + type).str()); + return ast::AttributeExpr::create(ctx, loc, allocated); +} + FailureOr Parser::parseOperationName(bool allowEmptyName) { SMRange loc = curToken.getLoc(); diff --git a/mlir/test/mlir-pdll/Parser/expr-failure.pdll b/mlir/test/mlir-pdll/Parser/expr-failure.pdll index 9c299c55fc3115f..aec4875480dd251 100644 --- a/mlir/test/mlir-pdll/Parser/expr-failure.pdll +++ b/mlir/test/mlir-pdll/Parser/expr-failure.pdll @@ -196,7 +196,7 @@ Pattern { // ----- Pattern { - // CHECK: expected expression + // CHECK: expected colon after integer literal let tuple = (10 = _: Value); erase op<>; } @@ -239,6 +239,23 @@ Pattern { };; } +// ----- + +Pattern { + let root = op -> (); + 3; + // CHECK: expected colon after integer literal + replace root with root; +} + +// ----- + +Pattern { + let root = op -> (); + 3 :; + // CHECK: expected integer type + replace root with root; +} // ----- diff --git a/mlir/test/mlir-pdll/Parser/expr.pdll b/mlir/test/mlir-pdll/Parser/expr.pdll index 938e18158703052..0ad283c7446f33e 100644 --- a/mlir/test/mlir-pdll/Parser/expr.pdll +++ b/mlir/test/mlir-pdll/Parser/expr.pdll @@ -14,6 +14,15 @@ Pattern { // ----- +// CHECK: Module +// CHECK: `-AttributeExpr {{.*}} Value<"10:i32"> +Pattern { + let attr = 10 : i32; + erase _: Op; +} + +// ----- + // CHECK: |-NamedAttributeDecl {{.*}} Name // CHECK: `-UserRewriteDecl {{.*}} Name ResultType // CHECK: `Arguments` diff --git a/mlir/test/mlir-pdll/Parser/stmt-failure.pdll b/mlir/test/mlir-pdll/Parser/stmt-failure.pdll index e9afa29921be2e4..01c545a0020fdb3 100644 --- a/mlir/test/mlir-pdll/Parser/stmt-failure.pdll +++ b/mlir/test/mlir-pdll/Parser/stmt-failure.pdll @@ -147,7 +147,7 @@ Pattern { // ----- Pattern { - // CHECK: expected expression + // CHECK: expected colon after integer literal let foo: ValueRange<10>; }