xref: /llvm-project/mlir/test/Dialect/PDL/ops.mlir (revision 8ec28af8eaff5acd0df3e53340159c034f08533d)
1// RUN: mlir-opt -split-input-file %s | mlir-opt
2// RUN: mlir-opt -split-input-file -mlir-print-op-generic -mlir-print-local-scope %s | FileCheck %s --check-prefix=CHECK-GENERIC
3
4// -----
5
6pdl.pattern @operations : benefit(1) {
7  // Operation with attributes and results.
8  %attribute = attribute
9  %type = type
10  %op0 = operation {"attr" = %attribute} -> (%type : !pdl.type)
11  %op0_result = pdl.result 0 of %op0
12
13  // Operation with input.
14  %input = operand
15  %root = operation(%op0_result, %input : !pdl.value, !pdl.value)
16  rewrite %root with "rewriter"
17}
18
19// -----
20
21pdl.pattern @rewrite_with_args : benefit(1) {
22  %input = operand
23  %root = operation(%input : !pdl.value)
24  rewrite %root with "rewriter"(%input : !pdl.value)
25}
26
27// -----
28
29pdl.pattern @rewrite_multi_root_optimal : benefit(2) {
30  %input1 = operand
31  %input2 = operand
32  %type = type
33  %op1 = operation(%input1 : !pdl.value) -> (%type : !pdl.type)
34  %val1 = result 0 of %op1
35  %root1 = operation(%val1 : !pdl.value)
36  %op2 = operation(%input2 : !pdl.value) -> (%type : !pdl.type)
37  %val2 = result 0 of %op2
38  %root2 = operation(%val1, %val2 : !pdl.value, !pdl.value)
39  rewrite with "rewriter"(%root1, %root2 : !pdl.operation, !pdl.operation)
40}
41
42// -----
43
44pdl.pattern @rewrite_multi_root_forced : benefit(2) {
45  %input1 = operand
46  %input2 = operand
47  %type = type
48  %op1 = operation(%input1 : !pdl.value) -> (%type : !pdl.type)
49  %val1 = result 0 of %op1
50  %root1 = operation(%val1 : !pdl.value)
51  %op2 = operation(%input2 : !pdl.value) -> (%type : !pdl.type)
52  %val2 = result 0 of %op2
53  %root2 = operation(%val1, %val2 : !pdl.value, !pdl.value)
54  rewrite %root1 with "rewriter"(%root2 : !pdl.operation)
55}
56
57// -----
58
59// Check that the result type of an operation within a rewrite can be inferred
60// from a pdl.replace.
61pdl.pattern @infer_type_from_operation_replace : benefit(1) {
62  %type1 = type : i32
63  %type2 = type
64  %root = operation -> (%type1, %type2 : !pdl.type, !pdl.type)
65  rewrite %root {
66    %type3 = type
67    %newOp = operation "foo.op" -> (%type1, %type3 : !pdl.type, !pdl.type)
68    replace %root with %newOp
69  }
70}
71
72// -----
73
74// Check that the result type of an operation within a rewrite can be inferred
75// from the result types of an operation within the match block.
76pdl.pattern @infer_type_from_type_used_in_match : benefit(1) {
77  %type1 = type : i32
78  %type2 = type
79  %root = operation -> (%type1, %type2 : !pdl.type, !pdl.type)
80  rewrite %root {
81    %newOp = operation "foo.op" -> (%type1, %type2 : !pdl.type, !pdl.type)
82  }
83}
84
85// -----
86
87// Check that the result type of an operation within a rewrite can be inferred
88// from the result types of an operation within the match block.
89pdl.pattern @infer_type_from_type_used_in_match : benefit(1) {
90  %types = types
91  %root = operation -> (%types : !pdl.range<type>)
92  rewrite %root {
93    %otherTypes = types : [i32, i64]
94    %newOp = operation "foo.op" -> (%types, %otherTypes : !pdl.range<type>, !pdl.range<type>)
95  }
96}
97
98// -----
99
100// Check that the result type of an operation within a rewrite can be inferred
101// from the type of an operand within the match block.
102pdl.pattern @infer_type_from_type_used_in_match : benefit(1) {
103  %type1 = type
104  %type2 = type
105  %operand1 = operand : %type1
106  %operand2 = operand : %type2
107  %root = operation (%operand1, %operand2 : !pdl.value, !pdl.value)
108  rewrite %root {
109    %newOp = operation "foo.op" -> (%type1, %type2 : !pdl.type, !pdl.type)
110  }
111}
112
113// -----
114
115// Check that the result type of an operation within a rewrite can be inferred
116// from the types of operands within the match block.
117pdl.pattern @infer_type_from_type_used_in_match : benefit(1) {
118  %types = types
119  %operands = operands : %types
120  %root = operation (%operands : !pdl.range<value>)
121  rewrite %root {
122    %newOp = operation "foo.op" -> (%types : !pdl.range<type>)
123  }
124}
125
126// -----
127
128pdl.pattern @apply_rewrite_with_no_results : benefit(1) {
129  %root = operation
130  rewrite %root {
131    apply_native_rewrite "NativeRewrite"(%root : !pdl.operation)
132  }
133}
134
135// -----
136
137pdl.pattern @apply_constraint_with_no_results : benefit(1) {
138  %root = operation
139  apply_native_constraint "NativeConstraint"(%root : !pdl.operation)
140  rewrite %root with "rewriter"
141}
142
143// -----
144
145pdl.pattern @apply_constraint_with_results : benefit(1) {
146  %root = operation
147  %attr = apply_native_constraint "NativeConstraint"(%root : !pdl.operation) : !pdl.attribute
148  rewrite %root {
149    apply_native_rewrite "NativeRewrite"(%attr : !pdl.attribute)
150  }
151}
152
153// -----
154
155pdl.pattern @attribute_with_dict : benefit(1) {
156  %root = operation
157  rewrite %root {
158    %attr = attribute = {some_unit_attr} attributes {pdl.special_attribute}
159    apply_native_rewrite "NativeRewrite"(%attr : !pdl.attribute)
160  }
161}
162
163// -----
164
165// Check that we don't treat the trailing location of a pdl.attribute as the
166// attribute value.
167
168pdl.pattern @attribute_with_loc : benefit(1) {
169  // CHECK-GENERIC: "pdl.attribute"
170  // CHECK-GENERIC-NOT: value = loc
171  %attr = attribute loc("bar")
172
173  %root = operation {"attribute" = %attr}
174  rewrite %root with "rewriter"
175}
176