[Mlir-commits] [mlir] [MLIR][parser] Add token type and parser methods for forward slashes (PR #125056)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Jan 30 05:08:50 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-core
Author: Andi Drebes (andidr)
<details>
<summary>Changes</summary>
This adds a token for a forward slash to the token definition list and the methods to `AsmParser::parseSlash()` and
`AsmParser::parseOptionalSlash()`, similar to other tokens used as operators (e.g., star, plus, etc.). This allows implementations of attributes that contain arithmetic expressions to support operators with a forward slash, e.g., a division.
The newly added check tests trigger the parsing of a slash in an attribute.
---
Full diff: https://github.com/llvm/llvm-project/pull/125056.diff
8 Files Affected:
- (modified) mlir/include/mlir/IR/OpImplementation.h (+6)
- (modified) mlir/lib/AsmParser/AsmParserImpl.h (+10)
- (modified) mlir/lib/AsmParser/Lexer.cpp (+1-1)
- (modified) mlir/lib/AsmParser/TokenKinds.def (+1)
- (modified) mlir/test/IR/parser.mlir (+7)
- (modified) mlir/test/lib/Dialect/Test/TestAttrDefs.td (+13)
- (modified) mlir/test/lib/Dialect/Test/TestAttributes.cpp (+18)
- (modified) mlir/test/lib/Dialect/Test/TestOps.td (+4)
``````````diff
diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h
index d9c925a9c56e6c..493ee0a294ac94 100644
--- a/mlir/include/mlir/IR/OpImplementation.h
+++ b/mlir/include/mlir/IR/OpImplementation.h
@@ -641,6 +641,12 @@ class AsmParser {
/// Parse a '+' token if present.
virtual ParseResult parseOptionalPlus() = 0;
+ /// Parse a '/' token.
+ virtual ParseResult parseSlash() = 0;
+
+ /// Parse a '/' token if present.
+ virtual ParseResult parseOptionalSlash() = 0;
+
/// Parse a '-' token.
virtual ParseResult parseMinus() = 0;
diff --git a/mlir/lib/AsmParser/AsmParserImpl.h b/mlir/lib/AsmParser/AsmParserImpl.h
index d5b72d63813a4e..77617d6eda1cdf 100644
--- a/mlir/lib/AsmParser/AsmParserImpl.h
+++ b/mlir/lib/AsmParser/AsmParserImpl.h
@@ -206,6 +206,16 @@ class AsmParserImpl : public BaseT {
return success(parser.consumeIf(Token::question));
}
+ /// Parses a '/' token.
+ ParseResult parseSlash() override {
+ return parser.parseToken(Token::slash, "expected '/'");
+ }
+
+ /// Parses a '/' if present.
+ ParseResult parseOptionalSlash() override {
+ return success(parser.consumeIf(Token::slash));
+ }
+
/// Parses a '*' token.
ParseResult parseStar() override {
return parser.parseToken(Token::star, "expected '*'");
diff --git a/mlir/lib/AsmParser/Lexer.cpp b/mlir/lib/AsmParser/Lexer.cpp
index b4189181a84959..751bd63e537f86 100644
--- a/mlir/lib/AsmParser/Lexer.cpp
+++ b/mlir/lib/AsmParser/Lexer.cpp
@@ -157,7 +157,7 @@ Token Lexer::lexToken() {
skipComment();
continue;
}
- return emitError(tokStart, "unexpected character");
+ return formToken(Token::slash, tokStart);
case '@':
return lexAtIdentifier(tokStart);
diff --git a/mlir/lib/AsmParser/TokenKinds.def b/mlir/lib/AsmParser/TokenKinds.def
index 49da8c3dea5fa5..fe7c53753e1562 100644
--- a/mlir/lib/AsmParser/TokenKinds.def
+++ b/mlir/lib/AsmParser/TokenKinds.def
@@ -70,6 +70,7 @@ TOK_PUNCTUATION(question, "?")
TOK_PUNCTUATION(r_brace, "}")
TOK_PUNCTUATION(r_paren, ")")
TOK_PUNCTUATION(r_square, "]")
+TOK_PUNCTUATION(slash, "/")
TOK_PUNCTUATION(star, "*")
TOK_PUNCTUATION(vertical_bar, "|")
diff --git a/mlir/test/IR/parser.mlir b/mlir/test/IR/parser.mlir
index cace1fefa43d62..9be4e66e573913 100644
--- a/mlir/test/IR/parser.mlir
+++ b/mlir/test/IR/parser.mlir
@@ -1219,6 +1219,13 @@ func.func @parse_base64_test() {
return
}
+// CHECK-LABEL: func @parse_slash_test
+func.func @parse_slash_test() {
+ // CHECK: "test.slash_attr"() <{attr = #test.slash_attr<1 / 2>}> : () -> ()
+ "test.slash_attr"() { attr = #test.slash_attr<1 / 2> } : () -> ()
+ return
+}
+
// CHECK-LABEL: func @"\22_string_symbol_reference\22"
func.func @"\"_string_symbol_reference\""() {
// CHECK: ref = @"\22_string_symbol_reference\22"
diff --git a/mlir/test/lib/Dialect/Test/TestAttrDefs.td b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
index 0fd272f85d39bd..4434f49b71119b 100644
--- a/mlir/test/lib/Dialect/Test/TestAttrDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestAttrDefs.td
@@ -395,4 +395,17 @@ def TestCustomLocationAttr : Test_LocAttr<"TestCustomLocation"> {
let assemblyFormat = "`<` $file `*` $line `>`";
}
+// Test attribute containing a slash token
+def SlashAttr: Test_Attr<"Slash">{
+ let mnemonic = "slash_attr";
+
+ let parameters = (
+ ins
+ "int":$lhs,
+ "int":$rhs
+ );
+
+ let hasCustomAssemblyFormat = 1;
+}
+
#endif // TEST_ATTRDEFS
diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
index e09ea109061648..166630b39103ea 100644
--- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
@@ -393,6 +393,24 @@ getDynamicCustomAssemblyFormatAttr(TestDialect *testDialect) {
std::move(parser), std::move(printer));
}
+//===----------------------------------------------------------------------===//
+// SlashAttr
+//===----------------------------------------------------------------------===//
+
+Attribute SlashAttr::parse(AsmParser &parser, Type type) {
+ int lhs, rhs;
+
+ if (parser.parseLess() || parser.parseInteger(lhs) || parser.parseSlash() ||
+ parser.parseInteger(rhs) || parser.parseGreater())
+ return Attribute();
+
+ return SlashAttr::get(parser.getContext(), lhs, rhs);
+}
+
+void SlashAttr::print(AsmPrinter &printer) const {
+ printer << "<" << getLhs() << " / " << getRhs() << ">";
+}
+
//===----------------------------------------------------------------------===//
// TestDialect
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index 79840094686e19..7f5efa880dfcce 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -330,6 +330,10 @@ def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> {
}];
}
+def SlashAttrOp : TEST_Op<"slash_attr"> {
+ let arguments = (ins SlashAttr:$attr);
+}
+
//===----------------------------------------------------------------------===//
// Test Attributes Constraints
//===----------------------------------------------------------------------===//
``````````
</details>
https://github.com/llvm/llvm-project/pull/125056
More information about the Mlir-commits
mailing list