Skip to content

Commit 846d97b

Browse files
committed
PDLL: Allow to implictly convert Tuple<Attr> with a single element to Attr
Parenthesis in PDLL are parsed as tuples. This conversion allows to use parenthesis in math expressions.
1 parent c4065a7 commit 846d97b

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,15 @@ LogicalResult Parser::convertTupleExpressionTo(
766766
return convertToRange({valueTy, valueRangeTy}, valueRangeTy);
767767
if (type == typeRangeTy)
768768
return convertToRange({typeTy, typeRangeTy}, typeRangeTy);
769+
if (type == attrTy && exprType.size() == 1 &&
770+
exprType.getElementTypes()[0] == type) {
771+
// Parenthesis become tuples. Allow to unpack single element tuples
772+
// to expressions.
773+
expr = ast::MemberAccessExpr::create(ctx, expr->getLoc(), expr,
774+
llvm::to_string(0),
775+
exprType.getElementTypes()[0]);
776+
return success();
777+
}
769778

770779
return emitErrorFn();
771780
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ Pattern {
124124

125125
// -----
126126

127+
Constraint checkAttr(attr: Attr);
128+
Pattern {
129+
let tuple = (result1 = value: Value);
130+
// CHECK: unable to convert expression of type `Tuple<result1: Value>` to the expected type of `Attr`
131+
checkAttr(tuple);
132+
erase _: Op;
133+
}
134+
135+
// -----
136+
127137
//===----------------------------------------------------------------------===//
128138
// Range Expr
129139
//===----------------------------------------------------------------------===//

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ Pattern {
114114

115115
// -----
116116

117+
// Implicitly convert single element Tuple<Attr> to Attr
118+
// CHECK: Module
119+
// CHECK: `-MemberAccessExpr {{.*}} Member<0> Type<Attr>
120+
// CHECK: `-TupleExpr {{.*}} Type<Tuple<Attr>>
121+
// CHECK: `-AttributeExpr {{.*}} Value<"10: i32">
122+
Constraint checkAttr(attr: Attr);
123+
Pattern {
124+
let tuple = (attr<"10: i32">);
125+
checkAttr(tuple);
126+
erase _: Op;
127+
}
128+
129+
// -----
130+
117131
#include "include/ops.td"
118132

119133
// CHECK: Module

0 commit comments

Comments
 (0)