Skip to content
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

PDLL: Parse integer literals into AttributeExpr #94

Merged
merged 1 commit into from
Feb 1, 2024
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
2 changes: 2 additions & 0 deletions mlir/include/mlir/Tools/PDLL/AST/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class NamedAttributeDecl;
class OpNameDecl;
class VariableDecl;

StringRef copyStringWithNull(Context &ctx, StringRef str);

//===----------------------------------------------------------------------===//
// Name
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Tools/PDLL/AST/Nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using namespace mlir;
using namespace mlir::pdll::ast;

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

Expand Down
23 changes: 23 additions & 0 deletions mlir/lib/Tools/PDLL/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ class Parser {
FailureOr<ast::Expr *> parseInlineRewriteLambdaExpr();
FailureOr<ast::Expr *> parseMemberAccessExpr(ast::Expr *parentExpr);
FailureOr<ast::Expr *> parseNegatedExpr();
FailureOr<ast::Expr *> parseIntegerExpr();
FailureOr<ast::OpNameDecl *> parseOperationName(bool allowEmptyName = false);
FailureOr<ast::OpNameDecl *> parseWrappedOperationName(bool allowEmptyName);
FailureOr<ast::Expr *>
Expand Down Expand Up @@ -1834,6 +1835,9 @@ FailureOr<ast::Expr *> Parser::parseExpr() {
case Token::exclam:
lhsExpr = parseNegatedExpr();
break;
case Token::integer:
lhsExpr = parseIntegerExpr();
break;
case Token::string_block:
return emitError("expected expression. If you are trying to create an "
"ArrayAttr, use a space between `[` and `{`.");
Expand Down Expand Up @@ -2077,6 +2081,25 @@ FailureOr<ast::Expr *> Parser::parseNegatedExpr() {
return parseCallExpr(*identifierExpr, /*isNegated = */ true);
}

/// Parse
/// integer : identifier
/// into an AttributeExpr.
/// Examples: '4 : i32', '0 : si1'
FailureOr<ast::Expr *> Parser::parseIntegerExpr() {
SMRange loc = curToken.getLoc();
StringRef value = curToken.getSpelling();
consumeToken();
if (!consumeIf(Token::colon))
return emitError("expected colon after integer literal");
if (!curToken.is(Token::identifier))
return emitError("expected integer type");
StringRef type = curToken.getSpelling();
consumeToken();

auto allocated = copyStringWithNull(ctx, (Twine(value) + ":" + type).str());
return ast::AttributeExpr::create(ctx, loc, allocated);
}

FailureOr<ast::OpNameDecl *> Parser::parseOperationName(bool allowEmptyName) {
SMRange loc = curToken.getLoc();

Expand Down
19 changes: 18 additions & 1 deletion mlir/test/mlir-pdll/Parser/expr-failure.pdll
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Pattern {
// -----

Pattern {
// CHECK: expected expression
// CHECK: expected colon after integer literal
let tuple = (10 = _: Value);
erase op<>;
}
Expand Down Expand Up @@ -239,6 +239,23 @@ Pattern {
};;
}

// -----

Pattern {
let root = op<func.func> -> ();
3;
// CHECK: expected colon after integer literal
replace root with root;
}

// -----

Pattern {
let root = op<func.func> -> ();
3 :;
// CHECK: expected integer type
replace root with root;
}

// -----

Expand Down
9 changes: 9 additions & 0 deletions mlir/test/mlir-pdll/Parser/expr.pdll
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ Pattern {

// -----

// CHECK: Module
// CHECK: `-AttributeExpr {{.*}} Value<"10:i32">
Pattern {
let attr = 10 : i32;
erase _: Op;
}

// -----

// CHECK: |-NamedAttributeDecl {{.*}} Name<some_array>
// CHECK: `-UserRewriteDecl {{.*}} Name<addElemToArrayAttr> ResultType<Attr>
// CHECK: `Arguments`
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/mlir-pdll/Parser/stmt-failure.pdll
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Pattern {
// -----

Pattern {
// CHECK: expected expression
// CHECK: expected colon after integer literal
let foo: ValueRange<10>;
}

Expand Down
Loading