1 // RUN: mlir-opt -split-input-file -convert-pdl-to-pdl-interp %s | FileCheck %s
5 // CHECK-LABEL: module @external
7 // CHECK: module @rewriters
8 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation, %[[INPUT:.*]]: !pdl.value)
9 // CHECK: pdl_interp.apply_rewrite "rewriter"(%[[ROOT]], %[[INPUT]] : !pdl.operation, !pdl.value)
10 pdl.pattern : benefit(1) {
12 %root = operation "foo.op"(%input : !pdl.value)
13 rewrite %root with "rewriter"(%input : !pdl.value)
19 // CHECK-LABEL: module @erase
21 // CHECK: module @rewriters
22 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation)
23 // CHECK: pdl_interp.erase %[[ROOT]]
24 // CHECK: pdl_interp.finalize
25 pdl.pattern : benefit(1) {
26 %root = operation "foo.op"
35 // CHECK-LABEL: module @operation_attributes
36 module @operation_attributes {
37 // CHECK: module @rewriters
38 // CHECK: func @pdl_generated_rewriter(%[[ATTR:.*]]: !pdl.attribute, %[[ROOT:.*]]: !pdl.operation)
39 // CHECK: %[[ATTR1:.*]] = pdl_interp.create_attribute true
40 // CHECK: pdl_interp.create_operation "foo.op" {"attr" = %[[ATTR]], "attr1" = %[[ATTR1]]}
41 pdl.pattern : benefit(1) {
43 %root = operation "foo.op" {"attr" = %attr}
45 %attr1 = attribute = true
46 %newOp = operation "foo.op" {"attr" = %attr, "attr1" = %attr1}
54 // CHECK-LABEL: module @operation_operands
55 module @operation_operands {
56 // CHECK: module @rewriters
57 // CHECK: func @pdl_generated_rewriter(%[[OPERAND:.*]]: !pdl.value, %[[ROOT:.*]]: !pdl.operation)
58 // CHECK: %[[NEWOP:.*]] = pdl_interp.create_operation "foo.op"(%[[OPERAND]] : !pdl.value)
59 // CHECK: %[[OPERAND1:.*]] = pdl_interp.get_result 0 of %[[NEWOP]]
60 // CHECK: pdl_interp.create_operation "foo.op2"(%[[OPERAND1]] : !pdl.value)
61 pdl.pattern : benefit(1) {
63 %root = operation "foo.op"(%operand : !pdl.value)
66 %newOp = operation "foo.op"(%operand : !pdl.value) -> (%type : !pdl.type)
67 %result = result 0 of %newOp
68 %newOp1 = operation "foo.op2"(%result : !pdl.value)
76 // CHECK-LABEL: module @operation_infer_types_from_replaceop
77 module @operation_infer_types_from_replaceop {
78 // CHECK: module @rewriters
79 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation
80 // CHECK: %[[RESULTS:.*]] = pdl_interp.get_results of %[[ROOT]]
81 // CHECK: %[[RESULT_TYPES:.*]] = pdl_interp.get_value_type of %[[RESULTS]]
82 // CHECK: pdl_interp.create_operation "foo.op" -> (%[[RESULT_TYPES]] : !pdl.range<type>)
83 pdl.pattern : benefit(1) {
86 %root = operation "foo.op" -> (%rootType, %rootType1 : !pdl.type, !pdl.type)
89 %newOp = operation "foo.op"
90 replace %root with %newOp
97 // CHECK-LABEL: module @operation_infer_types_from_otherop_individual_results
98 module @operation_infer_types_from_otherop_individual_results {
99 // CHECK: module @rewriters
100 // CHECK: func @pdl_generated_rewriter(%[[TYPE:.*]]: !pdl.type, %[[TYPES:.*]]: !pdl.range<type>
101 // CHECK: pdl_interp.create_operation "foo.op" -> (%[[TYPE]], %[[TYPES]] : !pdl.type, !pdl.range<type>)
102 pdl.pattern : benefit(1) {
105 %root = operation "foo.op" -> (%rootType, %rootTypes : !pdl.type, !pdl.range<type>)
107 %newOp = operation "foo.op" -> (%rootType, %rootTypes : !pdl.type, !pdl.range<type>)
114 // CHECK-LABEL: module @operation_infer_types_from_otherop_results
115 module @operation_infer_types_from_otherop_results {
116 // CHECK: module @rewriters
117 // CHECK: func @pdl_generated_rewriter(%[[TYPES:.*]]: !pdl.range<type>
118 // CHECK: pdl_interp.create_operation "foo.op" -> (%[[TYPES]] : !pdl.range<type>)
119 pdl.pattern : benefit(1) {
121 %root = operation "foo.op" -> (%rootTypes : !pdl.range<type>)
123 %newOp = operation "foo.op" -> (%rootTypes : !pdl.range<type>)
130 // CHECK-LABEL: module @operation_infer_types_from_interface
131 module @operation_infer_types_from_interface {
132 // Unused operation that ensures the arithmetic dialect is loaded for use in the pattern.
135 // CHECK: module @rewriters
136 // CHECK: func @pdl_generated_rewriter
137 // CHECK: %[[CST:.*]] = pdl_interp.create_operation "arith.constant" -> <inferred>
138 // CHECK: %[[CST_RES:.*]] = pdl_interp.get_results of %[[CST]] : !pdl.range<value>
139 // CHECK: %[[CST_TYPE:.*]] = pdl_interp.get_value_type of %[[CST_RES]] : !pdl.range<type>
140 // CHECK: pdl_interp.create_operation "foo.op" -> (%[[CST_TYPE]] : !pdl.range<type>)
141 pdl.pattern : benefit(1) {
142 %root = operation "foo.op"
145 %newOp = operation "arith.constant" -> (%types : !pdl.range<type>)
146 %newOp2 = operation "foo.op" -> (%types : !pdl.range<type>)
153 // CHECK-LABEL: module @replace_with_op
154 module @replace_with_op {
155 // CHECK: module @rewriters
156 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation)
157 // CHECK: %[[NEWOP:.*]] = pdl_interp.create_operation
158 // CHECK: %[[RESULTS:.*]] = pdl_interp.get_results of %[[NEWOP]]
159 // CHECK: pdl_interp.replace %[[ROOT]] with (%[[RESULTS]] : !pdl.range<value>)
160 pdl.pattern : benefit(1) {
162 %root = operation "foo.op" -> (%type : !pdl.type)
164 %newOp = operation "foo.op" -> (%type : !pdl.type)
165 replace %root with %newOp
172 // CHECK-LABEL: module @replace_with_values
173 module @replace_with_values {
174 // CHECK: module @rewriters
175 // CHECK: func @pdl_generated_rewriter({{.*}}, %[[ROOT:.*]]: !pdl.operation)
176 // CHECK: %[[NEWOP:.*]] = pdl_interp.create_operation
177 // CHECK: %[[RESULT:.*]] = pdl_interp.get_result 0 of %[[NEWOP]]
178 // CHECK: %[[RESULTS:.*]] = pdl_interp.get_results 1 of %[[NEWOP]] : !pdl.range<value>
179 // CHECK: %[[RESULTS_2:.*]] = pdl_interp.get_results 2 of %[[NEWOP]] : !pdl.value
180 // CHECK: pdl_interp.replace %[[ROOT]] with (%[[RESULT]], %[[RESULTS]], %[[RESULTS_2]] : !pdl.value, !pdl.range<value>, !pdl.value)
181 pdl.pattern : benefit(1) {
183 %root = operation "foo.op" -> (%types : !pdl.range<type>)
185 %newOp = operation "foo.op" -> (%types : !pdl.range<type>)
186 %newResult = result 0 of %newOp
187 %newResults = results 1 of %newOp -> !pdl.range<value>
188 %newResults2 = results 2 of %newOp -> !pdl.value
189 replace %root with (%newResult, %newResults, %newResults2 : !pdl.value, !pdl.range<value>, !pdl.value)
196 // CHECK-LABEL: module @replace_with_no_results
197 module @replace_with_no_results {
198 // CHECK: module @rewriters
199 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation)
200 // CHECK: pdl_interp.create_operation "foo.op"
201 // CHECK: pdl_interp.erase %[[ROOT]]
202 pdl.pattern : benefit(1) {
203 %root = operation "foo.op"
205 %newOp = operation "foo.op"
206 replace %root with %newOp
213 // CHECK-LABEL: module @apply_native_rewrite
214 module @apply_native_rewrite {
215 // CHECK: module @rewriters
216 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation)
217 // CHECK: %[[TYPE:.*]] = pdl_interp.apply_rewrite "functor"(%[[ROOT]] : !pdl.operation) : !pdl.type
218 // CHECK: pdl_interp.create_operation "foo.op" -> (%[[TYPE]] : !pdl.type)
219 pdl.pattern : benefit(1) {
221 %root = operation "foo.op" -> (%type : !pdl.type)
223 %newType = apply_native_rewrite "functor"(%root : !pdl.operation) : !pdl.type
224 %newOp = operation "foo.op" -> (%newType : !pdl.type)
231 // CHECK-LABEL: module @unbound_rewrite_op
232 module @unbound_rewrite_op {
233 // CHECK: module @rewriters
234 // CHECK: func @pdl_generated_rewriter()
235 // CHECK: %[[UNUSED:.*]] = pdl_interp.create_operation "bar.op"
236 // CHECK: pdl_interp.finalize
237 pdl.pattern : benefit(1) {
238 %root = operation "foo.op"
240 %unused = operation "bar.op"
247 // CHECK-LABEL: module @range_op
249 // CHECK: module @rewriters
250 // CHECK: func @pdl_generated_rewriter(%[[OPERAND:.*]]: !pdl.value)
251 // CHECK: %[[RANGE1:.*]] = pdl_interp.create_range : !pdl.range<value>
252 // CHECK: %[[RANGE2:.*]] = pdl_interp.create_range %[[OPERAND]], %[[RANGE1]] : !pdl.value, !pdl.range<value>
253 // CHECK: pdl_interp.finalize
254 pdl.pattern : benefit(1) {
255 %operand = pdl.operand
256 %root = operation "foo.op"(%operand : !pdl.value)
258 %emptyRange = pdl.range : !pdl.range<value>
259 %range = pdl.range %operand, %emptyRange : !pdl.value, !pdl.range<value>