Skip to content

Commit 1049aba

Browse files
committed
PDLL: Parse integer literals into AttributeExpr
1 parent c4065a7 commit 1049aba

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

mlir/include/mlir/Tools/PDLL/AST/Nodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class NamedAttributeDecl;
2828
class OpNameDecl;
2929
class VariableDecl;
3030

31+
StringRef copyStringWithNull(Context &ctx, StringRef str);
32+
3133
//===----------------------------------------------------------------------===//
3234
// Name
3335
//===----------------------------------------------------------------------===//

mlir/lib/Tools/PDLL/AST/Nodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using namespace mlir;
1616
using namespace mlir::pdll::ast;
1717

1818
/// Copy a string reference into the context with a null terminator.
19-
static StringRef copyStringWithNull(Context &ctx, StringRef str) {
19+
StringRef mlir::pdll::ast::copyStringWithNull(Context &ctx, StringRef str) {
2020
if (str.empty())
2121
return str;
2222

mlir/lib/Tools/PDLL/Parser/Parser.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class Parser {
325325
FailureOr<ast::Expr *> parseInlineRewriteLambdaExpr();
326326
FailureOr<ast::Expr *> parseMemberAccessExpr(ast::Expr *parentExpr);
327327
FailureOr<ast::Expr *> parseNegatedExpr();
328+
FailureOr<ast::Expr *> parseIntegerExpr();
328329
FailureOr<ast::OpNameDecl *> parseOperationName(bool allowEmptyName = false);
329330
FailureOr<ast::OpNameDecl *> parseWrappedOperationName(bool allowEmptyName);
330331
FailureOr<ast::Expr *>
@@ -1834,6 +1835,9 @@ FailureOr<ast::Expr *> Parser::parseExpr() {
18341835
case Token::exclam:
18351836
lhsExpr = parseNegatedExpr();
18361837
break;
1838+
case Token::integer:
1839+
lhsExpr = parseIntegerExpr();
1840+
break;
18371841
case Token::string_block:
18381842
return emitError("expected expression. If you are trying to create an "
18391843
"ArrayAttr, use a space between `[` and `{`.");
@@ -2077,6 +2081,25 @@ FailureOr<ast::Expr *> Parser::parseNegatedExpr() {
20772081
return parseCallExpr(*identifierExpr, /*isNegated = */ true);
20782082
}
20792083

2084+
/// Parse
2085+
/// integer : identifier
2086+
/// into an AttributeExpr.
2087+
/// Examples: '4 : i32', '0 : si1'
2088+
FailureOr<ast::Expr *> Parser::parseIntegerExpr() {
2089+
SMRange loc = curToken.getLoc();
2090+
StringRef value = curToken.getSpelling();
2091+
consumeToken();
2092+
if (!consumeIf(Token::colon))
2093+
return emitError("expected colon after integer literal");
2094+
if (!curToken.is(Token::identifier))
2095+
return emitError("expected integer type");
2096+
StringRef type = curToken.getSpelling();
2097+
consumeToken();
2098+
2099+
auto allocated = copyStringWithNull(ctx, (Twine(value) + ":" + type).str());
2100+
return ast::AttributeExpr::create(ctx, loc, allocated);
2101+
}
2102+
20802103
FailureOr<ast::OpNameDecl *> Parser::parseOperationName(bool allowEmptyName) {
20812104
SMRange loc = curToken.getLoc();
20822105

mlir/test/mlir-pdll/Parser/expr-failure.pdll

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Pattern {
196196
// -----
197197

198198
Pattern {
199-
// CHECK: expected expression
199+
// CHECK: expected colon after integer literal
200200
let tuple = (10 = _: Value);
201201
erase op<>;
202202
}
@@ -239,6 +239,23 @@ Pattern {
239239
};;
240240
}
241241

242+
// -----
243+
244+
Pattern {
245+
let root = op<func.func> -> ();
246+
3;
247+
// CHECK: expected colon after integer literal
248+
replace root with root;
249+
}
250+
251+
// -----
252+
253+
Pattern {
254+
let root = op<func.func> -> ();
255+
3 :;
256+
// CHECK: expected integer type
257+
replace root with root;
258+
}
242259

243260
// -----
244261

mlir/test/mlir-pdll/Parser/expr.pdll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ Pattern {
1414

1515
// -----
1616

17+
// CHECK: Module
18+
// CHECK: `-AttributeExpr {{.*}} Value<"10:i32">
19+
Pattern {
20+
let attr = 10 : i32;
21+
erase _: Op;
22+
}
23+
24+
// -----
25+
1726
// CHECK: |-NamedAttributeDecl {{.*}} Name<some_array>
1827
// CHECK: `-UserRewriteDecl {{.*}} Name<addElemToArrayAttr> ResultType<Attr>
1928
// CHECK: `Arguments`

mlir/test/mlir-pdll/Parser/stmt-failure.pdll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Pattern {
147147
// -----
148148

149149
Pattern {
150-
// CHECK: expected expression
150+
// CHECK: expected colon after integer literal
151151
let foo: ValueRange<10>;
152152
}
153153

0 commit comments

Comments
 (0)