Skip to content

[MLIR][parser] Add token type and parser methods for forward slashes #125056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mlir/include/mlir/IR/OpImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,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;

Expand Down
10 changes: 10 additions & 0 deletions mlir/lib/AsmParser/AsmParserImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 '*'");
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/AsmParser/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Token Lexer::lexToken() {
skipComment();
continue;
}
return emitError(tokStart, "unexpected character");
return formToken(Token::slash, tokStart);

case '@':
return lexAtIdentifier(tokStart);
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/AsmParser/TokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -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, "|")

Expand Down
7 changes: 7 additions & 0 deletions mlir/test/IR/parser.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,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"
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/lib/Dialect/Test/TestAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,17 @@ def TestOpAsmAttrInterfaceTablegenDefaultAttr : Test_Attr<"TestOpAsmAttrInterfac
let genMnemonicAlias = 1;
}

// 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
18 changes: 18 additions & 0 deletions mlir/test/lib/Dialect/Test/TestAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,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
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> {
}];
}

def SlashAttrOp : TEST_Op<"slash_attr"> {
let arguments = (ins SlashAttr:$attr);
}

//===----------------------------------------------------------------------===//
// Test Attributes Constraints
//===----------------------------------------------------------------------===//
Expand Down