Skip to content

Commit

Permalink
PDLL: Allow to implictly convert Tuple<Attr> with a single element to…
Browse files Browse the repository at this point in the history
… Attr

Parenthesis in PDLL are parsed as tuples. This conversion allows to use parenthesis
in math expressions.
  • Loading branch information
mgehre-amd committed Jan 26, 2024
1 parent c4065a7 commit 846d97b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mlir/lib/Tools/PDLL/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,15 @@ LogicalResult Parser::convertTupleExpressionTo(
return convertToRange({valueTy, valueRangeTy}, valueRangeTy);
if (type == typeRangeTy)
return convertToRange({typeTy, typeRangeTy}, typeRangeTy);
if (type == attrTy && exprType.size() == 1 &&
exprType.getElementTypes()[0] == type) {
// Parenthesis become tuples. Allow to unpack single element tuples
// to expressions.
expr = ast::MemberAccessExpr::create(ctx, expr->getLoc(), expr,
llvm::to_string(0),
exprType.getElementTypes()[0]);
return success();
}

return emitErrorFn();
}
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/mlir-pdll/Parser/expr-failure.pdll
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ Pattern {

// -----

Constraint checkAttr(attr: Attr);
Pattern {
let tuple = (result1 = value: Value);
// CHECK: unable to convert expression of type `Tuple<result1: Value>` to the expected type of `Attr`
checkAttr(tuple);
erase _: Op;
}

// -----

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

// -----

// Implicitly convert single element Tuple<Attr> to Attr
// CHECK: Module
// CHECK: `-MemberAccessExpr {{.*}} Member<0> Type<Attr>
// CHECK: `-TupleExpr {{.*}} Type<Tuple<Attr>>
// CHECK: `-AttributeExpr {{.*}} Value<"10: i32">
Constraint checkAttr(attr: Attr);
Pattern {
let tuple = (attr<"10: i32">);
checkAttr(tuple);
erase _: Op;
}

// -----

#include "include/ops.td"

// CHECK: Module
Expand Down

0 comments on commit 846d97b

Please sign in to comment.