Skip to content

Commit ba23eed

Browse files
authored
[MLIR][parser] Add token type and parser methods for forward slashes (#125056)
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.
1 parent 899f263 commit ba23eed

File tree

8 files changed

+60
-1
lines changed

8 files changed

+60
-1
lines changed

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,12 @@ class AsmParser {
656656
/// Parse a '+' token if present.
657657
virtual ParseResult parseOptionalPlus() = 0;
658658

659+
/// Parse a '/' token.
660+
virtual ParseResult parseSlash() = 0;
661+
662+
/// Parse a '/' token if present.
663+
virtual ParseResult parseOptionalSlash() = 0;
664+
659665
/// Parse a '-' token.
660666
virtual ParseResult parseMinus() = 0;
661667

mlir/lib/AsmParser/AsmParserImpl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ class AsmParserImpl : public BaseT {
206206
return success(parser.consumeIf(Token::question));
207207
}
208208

209+
/// Parses a '/' token.
210+
ParseResult parseSlash() override {
211+
return parser.parseToken(Token::slash, "expected '/'");
212+
}
213+
214+
/// Parses a '/' if present.
215+
ParseResult parseOptionalSlash() override {
216+
return success(parser.consumeIf(Token::slash));
217+
}
218+
209219
/// Parses a '*' token.
210220
ParseResult parseStar() override {
211221
return parser.parseToken(Token::star, "expected '*'");

mlir/lib/AsmParser/Lexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Token Lexer::lexToken() {
157157
skipComment();
158158
continue;
159159
}
160-
return emitError(tokStart, "unexpected character");
160+
return formToken(Token::slash, tokStart);
161161

162162
case '@':
163163
return lexAtIdentifier(tokStart);

mlir/lib/AsmParser/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ TOK_PUNCTUATION(question, "?")
7070
TOK_PUNCTUATION(r_brace, "}")
7171
TOK_PUNCTUATION(r_paren, ")")
7272
TOK_PUNCTUATION(r_square, "]")
73+
TOK_PUNCTUATION(slash, "/")
7374
TOK_PUNCTUATION(star, "*")
7475
TOK_PUNCTUATION(vertical_bar, "|")
7576

mlir/test/IR/parser.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,13 @@ func.func @parse_base64_test() {
12321232
return
12331233
}
12341234

1235+
// CHECK-LABEL: func @parse_slash_test
1236+
func.func @parse_slash_test() {
1237+
// CHECK: "test.slash_attr"() <{attr = #test.slash_attr<1 / 2>}> : () -> ()
1238+
"test.slash_attr"() { attr = #test.slash_attr<1 / 2> } : () -> ()
1239+
return
1240+
}
1241+
12351242
// CHECK-LABEL: func @"\22_string_symbol_reference\22"
12361243
func.func @"\"_string_symbol_reference\""() {
12371244
// CHECK: ref = @"\22_string_symbol_reference\22"

mlir/test/lib/Dialect/Test/TestAttrDefs.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,17 @@ def TestOpAsmAttrInterfaceTablegenDefaultAttr : Test_Attr<"TestOpAsmAttrInterfac
418418
let genMnemonicAlias = 1;
419419
}
420420

421+
// Test attribute containing a slash token
422+
def SlashAttr: Test_Attr<"Slash">{
423+
let mnemonic = "slash_attr";
424+
425+
let parameters = (
426+
ins
427+
"int":$lhs,
428+
"int":$rhs
429+
);
430+
431+
let hasCustomAssemblyFormat = 1;
432+
}
433+
421434
#endif // TEST_ATTRDEFS

mlir/test/lib/Dialect/Test/TestAttributes.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,24 @@ getDynamicCustomAssemblyFormatAttr(TestDialect *testDialect) {
497497
std::move(parser), std::move(printer));
498498
}
499499

500+
//===----------------------------------------------------------------------===//
501+
// SlashAttr
502+
//===----------------------------------------------------------------------===//
503+
504+
Attribute SlashAttr::parse(AsmParser &parser, Type type) {
505+
int lhs, rhs;
506+
507+
if (parser.parseLess() || parser.parseInteger(lhs) || parser.parseSlash() ||
508+
parser.parseInteger(rhs) || parser.parseGreater())
509+
return Attribute();
510+
511+
return SlashAttr::get(parser.getContext(), lhs, rhs);
512+
}
513+
514+
void SlashAttr::print(AsmPrinter &printer) const {
515+
printer << "<" << getLhs() << " / " << getRhs() << ">";
516+
}
517+
500518
//===----------------------------------------------------------------------===//
501519
// TestDialect
502520
//===----------------------------------------------------------------------===//

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> {
324324
}];
325325
}
326326

327+
def SlashAttrOp : TEST_Op<"slash_attr"> {
328+
let arguments = (ins SlashAttr:$attr);
329+
}
330+
327331
//===----------------------------------------------------------------------===//
328332
// Test Attributes Constraints
329333
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)