xref: /llvm-project/mlir/test/mlir-pdll/CodeGen/MLIR/decl.pdll (revision 95b4e88b1db348fbb074c945bd85c777cf807cc0)
1// RUN: mlir-pdll %s -I %S -split-input-file -x mlir | FileCheck %s
2
3//===----------------------------------------------------------------------===//
4// PatternDecl
5//===----------------------------------------------------------------------===//
6
7// CHECK: pdl.pattern : benefit(0) {
8Pattern => erase _: Op;
9
10// -----
11
12// CHECK: pdl.pattern @NamedPattern : benefit(0) {
13Pattern NamedPattern => erase _: Op;
14
15// -----
16
17// CHECK: pdl.pattern @NamedPattern : benefit(10) {
18Pattern NamedPattern with benefit(10), recursion => erase _: Op;
19
20// -----
21
22//===----------------------------------------------------------------------===//
23// VariableDecl
24//===----------------------------------------------------------------------===//
25
26// Test the case of a variable with an initializer.
27
28// CHECK: pdl.pattern @VarWithInit
29// CHECK: %[[INIT:.*]] = operation "test.op"
30// CHECK: rewrite %[[INIT]] {
31// CHECK:   erase %[[INIT]]
32Pattern VarWithInit {
33  let var = op<test.op>;
34  erase var;
35}
36
37// -----
38
39// Test range based constraints.
40
41// CHECK: pdl.pattern @VarWithRangeConstraints
42// CHECK: %[[OPERAND_TYPES:.*]] = types
43// CHECK: %[[OPERANDS:.*]] = operands : %[[OPERAND_TYPES]]
44// CHECK: %[[RESULT_TYPES:.*]] = types
45// CHECK: operation(%[[OPERANDS]] : !pdl.range<value>)  -> (%[[RESULT_TYPES]] : !pdl.range<type>)
46Pattern VarWithRangeConstraints {
47  erase op<>(operands: ValueRange<operandTypes: TypeRange>) -> (results: TypeRange);
48}
49
50// -----
51
52// Test single entity constraints.
53
54// CHECK: pdl.pattern @VarWithConstraints
55// CHECK: %[[OPERAND_TYPE:.*]] = type
56// CHECK: %[[OPERAND:.*]] = operand : %[[OPERAND_TYPES]]
57// CHECK: %[[ATTR_TYPE:.*]] = type
58// CHECK: %[[ATTR:.*]] = attribute : %[[ATTR_TYPE]]
59// CHECK: %[[RESULT_TYPE:.*]] = type
60// CHECK: operation(%[[OPERAND]] : !pdl.value)  {"attr" = %[[ATTR]]} -> (%[[RESULT_TYPE]] : !pdl.type)
61Pattern VarWithConstraints {
62  erase op<>(operand: Value<operandType: Type>) { attr = _: Attr<attrType: Type>} -> (result: Type);
63}
64
65// -----
66
67// Test op constraint.
68
69// CHECK: pdl.pattern @VarWithNoNameOpConstraint
70// CHECK: %[[OPERANDS:.*]] = operands
71// CHECK: %[[RESULT_TYPES:.*]] = types
72// CHECK: operation(%[[OPERANDS]] : !pdl.range<value>)  -> (%[[RESULT_TYPES]] : !pdl.range<type>)
73Pattern VarWithNoNameOpConstraint => erase _: Op;
74
75// CHECK: pdl.pattern @VarWithNamedOpConstraint
76// CHECK: %[[OPERANDS:.*]] = operands
77// CHECK: %[[RESULT_TYPES:.*]] = types
78// CHECK: operation "test.op"(%[[OPERANDS]] : !pdl.range<value>)  -> (%[[RESULT_TYPES]] : !pdl.range<type>)
79Pattern VarWithNamedOpConstraint => erase _: Op<test.op>;
80
81// -----
82
83// Test user defined constraints.
84
85// CHECK: pdl.pattern @VarWithUserConstraint
86// CHECK: %[[OPERANDS:.*]] = operands
87// CHECK: %[[RESULT_TYPES:.*]] = types
88// CHECK: %[[OP:.*]] = operation(%[[OPERANDS]] : !pdl.range<value>)  -> (%[[RESULT_TYPES]] : !pdl.range<type>)
89// CHECK: apply_native_constraint "NestedArgCst"(%[[OP]] : !pdl.operation)
90// CHECK: apply_native_constraint "NestedResCst"(%[[OP]] : !pdl.operation)
91// CHECK: apply_native_constraint "OpCst"(%[[OP]] : !pdl.operation)
92// CHECK: rewrite %[[OP]]
93Constraint NestedArgCst(op: Op);
94Constraint NestedResCst(op: Op);
95Constraint TestArgResCsts(op: NestedArgCst) -> NestedResCst => op;
96Constraint OpCst(op: Op);
97Pattern VarWithUserConstraint => erase _: [TestArgResCsts, OpCst];
98