Skip to content

Commit 6e4cb19

Browse files
committed
[MLIR][parser] Add token type and parser methods for forward slashes
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 b68b4f6 commit 6e4cb19

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
@@ -641,6 +641,12 @@ class AsmParser {
641641
/// Parse a '+' token if present.
642642
virtual ParseResult parseOptionalPlus() = 0;
643643

644+
/// Parse a '/' token.
645+
virtual ParseResult parseSlash() = 0;
646+
647+
/// Parse a '/' token if present.
648+
virtual ParseResult parseOptionalSlash() = 0;
649+
644650
/// Parse a '-' token.
645651
virtual ParseResult parseMinus() = 0;
646652

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
@@ -1219,6 +1219,13 @@ func.func @parse_base64_test() {
12191219
return
12201220
}
12211221

1222+
// CHECK-LABEL: func @parse_slash_test
1223+
func.func @parse_slash_test() {
1224+
// CHECK: "test.slash_attr"() <{attr = #test.slash_attr<1 / 2>}> : () -> ()
1225+
"test.slash_attr"() { attr = #test.slash_attr<1 / 2> } : () -> ()
1226+
return
1227+
}
1228+
12221229
// CHECK-LABEL: func @"\22_string_symbol_reference\22"
12231230
func.func @"\"_string_symbol_reference\""() {
12241231
// 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
@@ -395,4 +395,17 @@ def TestCustomLocationAttr : Test_LocAttr<"TestCustomLocation"> {
395395
let assemblyFormat = "`<` $file `*` $line `>`";
396396
}
397397

398+
// Test attribute containing a slash token
399+
def SlashAttr: Test_Attr<"Slash">{
400+
let mnemonic = "slash_attr";
401+
402+
let parameters = (
403+
ins
404+
"int":$lhs,
405+
"int":$rhs
406+
);
407+
408+
let hasCustomAssemblyFormat = 1;
409+
}
410+
398411
#endif // TEST_ATTRDEFS

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

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

396+
//===----------------------------------------------------------------------===//
397+
// SlashAttr
398+
//===----------------------------------------------------------------------===//
399+
400+
Attribute SlashAttr::parse(AsmParser &parser, Type type) {
401+
int lhs, rhs;
402+
403+
if (parser.parseLess() || parser.parseInteger(lhs) || parser.parseSlash() ||
404+
parser.parseInteger(rhs) || parser.parseGreater())
405+
return Attribute();
406+
407+
return SlashAttr::get(parser.getContext(), lhs, rhs);
408+
}
409+
410+
void SlashAttr::print(AsmPrinter &printer) const {
411+
printer << "<" << getLhs() << " / " << getRhs() << ">";
412+
}
413+
396414
//===----------------------------------------------------------------------===//
397415
// TestDialect
398416
//===----------------------------------------------------------------------===//

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> {
330330
}];
331331
}
332332

333+
def SlashAttrOp : TEST_Op<"slash_attr"> {
334+
let arguments = (ins SlashAttr:$attr);
335+
}
336+
333337
//===----------------------------------------------------------------------===//
334338
// Test Attributes Constraints
335339
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)