From e47805c995525a13a0dc7b36d5e8a0f8816a62e8 Mon Sep 17 00:00:00 2001 From: Jean-Michel Gorius Date: Wed, 11 Nov 2020 18:01:39 +0100 Subject: [PATCH] [mlir] Add plus, star and optional less/greater parsing The tokens are already handled by the lexer. This revision exposes them through the parser interface. This revision also adds missing functions for question mark parsing and completes the list of valid punctuation tokens in the documentation. Differential Revision: https://reviews.llvm.org/D90907 --- mlir/docs/OpDefinitions.md | 2 +- mlir/include/mlir/IR/OpImplementation.h | 27 +++++++++++++-- mlir/lib/Parser/Parser.cpp | 45 ++++++++++++++++++++++--- mlir/test/lib/Dialect/Test/TestOps.td | 2 +- mlir/test/mlir-tblgen/op-format-spec.td | 2 +- mlir/test/mlir-tblgen/op-format.mlir | 4 +-- mlir/tools/mlir-tblgen/OpFormatGen.cpp | 6 ++-- 7 files changed, 73 insertions(+), 15 deletions(-) diff --git a/mlir/docs/OpDefinitions.md b/mlir/docs/OpDefinitions.md index ac2b51a4ac21..d4f3f4a03eba 100644 --- a/mlir/docs/OpDefinitions.md +++ b/mlir/docs/OpDefinitions.md @@ -768,7 +768,7 @@ A literal is either a keyword or punctuation surrounded by \`\`. The following are the set of valid punctuation: -`:`, `,`, `=`, `<`, `>`, `(`, `)`, `{`, `}`, `[`, `]`, `->` +`:`, `,`, `=`, `<`, `>`, `(`, `)`, `{`, `}`, `[`, `]`, `->`, `?`, `+`, `*` #### Variables diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h index 48bd03eb46a9..f918c605d7e9 100644 --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -326,9 +326,33 @@ public: /// Parse a '<' token. virtual ParseResult parseLess() = 0; + /// Parse a '<' token if present. + virtual ParseResult parseOptionalLess() = 0; + /// Parse a '>' token. virtual ParseResult parseGreater() = 0; + /// Parse a '>' token if present. + virtual ParseResult parseOptionalGreater() = 0; + + /// Parse a '?' token. + virtual ParseResult parseQuestion() = 0; + + /// Parse a '?' token if present. + virtual ParseResult parseOptionalQuestion() = 0; + + /// Parse a '+' token. + virtual ParseResult parsePlus() = 0; + + /// Parse a '+' token if present. + virtual ParseResult parseOptionalPlus() = 0; + + /// Parse a '*' token. + virtual ParseResult parseStar() = 0; + + /// Parse a '*' token if present. + virtual ParseResult parseOptionalStar() = 0; + /// Parse a given keyword. ParseResult parseKeyword(StringRef keyword, const Twine &msg = "") { auto loc = getCurrentLocation(); @@ -369,9 +393,6 @@ public: /// Parse a `)` token if present. virtual ParseResult parseOptionalRParen() = 0; - /// Parses a '?' if present. - virtual ParseResult parseOptionalQuestion() = 0; - /// Parse a `[` token. virtual ParseResult parseLSquare() = 0; diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp index 739e37b8c078..d8f6ebd8ef96 100644 --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -994,11 +994,21 @@ public: return parser.parseToken(Token::less, "expected '<'"); } + /// Parse a '<' token if present. + ParseResult parseOptionalLess() override { + return success(parser.consumeIf(Token::less)); + } + /// Parse a '>' token. ParseResult parseGreater() override { return parser.parseToken(Token::greater, "expected '>'"); } + /// Parse a '>' token if present. + ParseResult parseOptionalGreater() override { + return success(parser.consumeIf(Token::greater)); + } + /// Parse a `(` token. ParseResult parseLParen() override { return parser.parseToken(Token::l_paren, "expected '('"); @@ -1019,11 +1029,6 @@ public: return success(parser.consumeIf(Token::r_paren)); } - /// Parses a '?' if present. - ParseResult parseOptionalQuestion() override { - return success(parser.consumeIf(Token::question)); - } - /// Parse a `[` token. ParseResult parseLSquare() override { return parser.parseToken(Token::l_square, "expected '['"); @@ -1044,6 +1049,36 @@ public: return success(parser.consumeIf(Token::r_square)); } + /// Parses a '?' token. + ParseResult parseQuestion() override { + return parser.parseToken(Token::question, "expected '?'"); + } + + /// Parses a '?' token if present. + ParseResult parseOptionalQuestion() override { + return success(parser.consumeIf(Token::question)); + } + + /// Parses a '+' token. + ParseResult parsePlus() override { + return parser.parseToken(Token::plus, "expected '+'"); + } + + /// Parses a '+' token if present. + ParseResult parseOptionalPlus() override { + return success(parser.consumeIf(Token::plus)); + } + + /// Parses a '*' token. + ParseResult parseStar() override { + return parser.parseToken(Token::star, "expected '*'"); + } + + /// Parses a '*' token if present. + ParseResult parseOptionalStar() override { + return success(parser.consumeIf(Token::star)); + } + //===--------------------------------------------------------------------===// // Attribute Parsing //===--------------------------------------------------------------------===// diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td index 0db8e048f927..d50db48148a6 100644 --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -1380,7 +1380,7 @@ def AsmDialectInterfaceOp : TEST_Op<"asm_dialect_interface_op"> { def FormatLiteralOp : TEST_Op<"format_literal_op"> { let assemblyFormat = [{ - `keyword_$.` `->` `:` `,` `=` `<` `>` `(` `)` `[` `]` `` `(` ` ` `)` attr-dict + `keyword_$.` `->` `:` `,` `=` `<` `>` `(` `)` `[` `]` `` `(` ` ` `)` `?` `+` `*` attr-dict }]; } diff --git a/mlir/test/mlir-tblgen/op-format-spec.td b/mlir/test/mlir-tblgen/op-format-spec.td index b0172ccfd4a7..7817920f8955 100644 --- a/mlir/test/mlir-tblgen/op-format-spec.td +++ b/mlir/test/mlir-tblgen/op-format-spec.td @@ -309,7 +309,7 @@ def LiteralInvalidB : TestFormat_Op<"literal_invalid_b", [{ }]>; // CHECK-NOT: error def LiteralValid : TestFormat_Op<"literal_valid", [{ - `_` `:` `,` `=` `<` `>` `(` `)` `[` `]` ` ` `` `->` `abc$._` + `_` `:` `,` `=` `<` `>` `(` `)` `[` `]` `?` `+` `*` ` ` `` `->` `abc$._` attr-dict }]>; diff --git a/mlir/test/mlir-tblgen/op-format.mlir b/mlir/test/mlir-tblgen/op-format.mlir index e5a5f58dc0da..34919513aca6 100644 --- a/mlir/test/mlir-tblgen/op-format.mlir +++ b/mlir/test/mlir-tblgen/op-format.mlir @@ -5,8 +5,8 @@ // CHECK: %[[MEMREF:.*]] = %memref = "foo.op"() : () -> (memref<1xf64>) -// CHECK: test.format_literal_op keyword_$. -> :, = <> () []( ) {foo.some_attr} -test.format_literal_op keyword_$. -> :, = <> () []( ) {foo.some_attr} +// CHECK: test.format_literal_op keyword_$. -> :, = <> () []( ) ? + * {foo.some_attr} +test.format_literal_op keyword_$. -> :, = <> () []( ) ? + * {foo.some_attr} // CHECK: test.format_attr_op 10 // CHECK-NOT: {attr diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp index 44e3030bab06..937ee7747af3 100644 --- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp +++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp @@ -281,7 +281,7 @@ bool LiteralElement::isValidLiteral(StringRef value) { // If there is only one character, this must either be punctuation or a // single character bare identifier. if (value.size() == 1) - return isalpha(front) || StringRef("_:,=<>()[]{}?").contains(front); + return isalpha(front) || StringRef("_:,=<>()[]{}?+*").contains(front); // Check the punctuation that are larger than a single character. if (value == "->") @@ -762,7 +762,9 @@ static void genLiteralParser(StringRef value, OpMethodBody &body) { .Case(")", "RParen()") .Case("[", "LSquare()") .Case("]", "RSquare()") - .Case("?", "Question()"); + .Case("?", "Question()") + .Case("+", "Plus()") + .Case("*", "Star()"); } /// Generate the storage code required for parsing the given element.