263 lines · plain
1// RUN: mlir-opt -split-input-file -convert-pdl-to-pdl-interp %s | FileCheck %s2 3// -----4 5// CHECK-LABEL: module @external6module @external {7 // CHECK: module @rewriters8 // 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) {11 %input = operand12 %root = operation "foo.op"(%input : !pdl.value)13 rewrite %root with "rewriter"(%input : !pdl.value)14 }15}16 17// -----18 19// CHECK-LABEL: module @erase20module @erase {21 // CHECK: module @rewriters22 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation)23 // CHECK: pdl_interp.erase %[[ROOT]]24 // CHECK: pdl_interp.finalize25 pdl.pattern : benefit(1) {26 %root = operation "foo.op"27 rewrite %root {28 erase %root29 }30 }31}32 33// -----34 35// CHECK-LABEL: module @operation_attributes36module @operation_attributes {37 // CHECK: module @rewriters38 // CHECK: func @pdl_generated_rewriter(%[[ATTR:.*]]: !pdl.attribute, %[[ROOT:.*]]: !pdl.operation)39 // CHECK: %[[ATTR1:.*]] = pdl_interp.create_attribute true40 // CHECK: pdl_interp.create_operation "foo.op" {"attr" = %[[ATTR]], "attr1" = %[[ATTR1]]}41 pdl.pattern : benefit(1) {42 %attr = attribute43 %root = operation "foo.op" {"attr" = %attr}44 rewrite %root {45 %attr1 = attribute = true46 %newOp = operation "foo.op" {"attr" = %attr, "attr1" = %attr1}47 erase %root48 }49 }50}51 52// -----53 54// CHECK-LABEL: module @operation_operands55module @operation_operands {56 // CHECK: module @rewriters57 // 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) {62 %operand = operand63 %root = operation "foo.op"(%operand : !pdl.value)64 rewrite %root {65 %type = type : i3266 %newOp = operation "foo.op"(%operand : !pdl.value) -> (%type : !pdl.type)67 %result = result 0 of %newOp68 %newOp1 = operation "foo.op2"(%result : !pdl.value)69 erase %root70 }71 }72}73 74// -----75 76// CHECK-LABEL: module @operation_infer_types_from_replaceop77module @operation_infer_types_from_replaceop {78 // CHECK: module @rewriters79 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation80 // 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) {84 %rootType = type85 %rootType1 = type86 %root = operation "foo.op" -> (%rootType, %rootType1 : !pdl.type, !pdl.type)87 rewrite %root {88 %newType1 = type89 %newOp = operation "foo.op"90 replace %root with %newOp91 }92 }93}94 95// -----96 97// CHECK-LABEL: module @operation_infer_types_from_otherop_individual_results98module @operation_infer_types_from_otherop_individual_results {99 // CHECK: module @rewriters100 // 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) {103 %rootType = type104 %rootTypes = types105 %root = operation "foo.op" -> (%rootType, %rootTypes : !pdl.type, !pdl.range<type>)106 rewrite %root {107 %newOp = operation "foo.op" -> (%rootType, %rootTypes : !pdl.type, !pdl.range<type>)108 }109 }110}111 112// -----113 114// CHECK-LABEL: module @operation_infer_types_from_otherop_results115module @operation_infer_types_from_otherop_results {116 // CHECK: module @rewriters117 // 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) {120 %rootTypes = types121 %root = operation "foo.op" -> (%rootTypes : !pdl.range<type>)122 rewrite %root {123 %newOp = operation "foo.op" -> (%rootTypes : !pdl.range<type>)124 }125 }126}127 128// -----129 130// CHECK-LABEL: module @operation_infer_types_from_interface131module @operation_infer_types_from_interface {132 // Unused operation that ensures the arithmetic dialect is loaded for use in the pattern.133 arith.constant true134 135 // CHECK: module @rewriters136 // CHECK: func @pdl_generated_rewriter137 // 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"143 rewrite %root {144 %types = types145 %newOp = operation "arith.constant" -> (%types : !pdl.range<type>)146 %newOp2 = operation "foo.op" -> (%types : !pdl.range<type>)147 }148 }149}150 151// -----152 153// CHECK-LABEL: module @replace_with_op154module @replace_with_op {155 // CHECK: module @rewriters156 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation)157 // CHECK: %[[NEWOP:.*]] = pdl_interp.create_operation158 // CHECK: %[[RESULTS:.*]] = pdl_interp.get_results of %[[NEWOP]]159 // CHECK: pdl_interp.replace %[[ROOT]] with (%[[RESULTS]] : !pdl.range<value>)160 pdl.pattern : benefit(1) {161 %type = type : i32162 %root = operation "foo.op" -> (%type : !pdl.type)163 rewrite %root {164 %newOp = operation "foo.op" -> (%type : !pdl.type)165 replace %root with %newOp166 }167 }168}169 170// -----171 172// CHECK-LABEL: module @replace_with_values173module @replace_with_values {174 // CHECK: module @rewriters175 // CHECK: func @pdl_generated_rewriter({{.*}}, %[[ROOT:.*]]: !pdl.operation)176 // CHECK: %[[NEWOP:.*]] = pdl_interp.create_operation177 // 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.value180 // CHECK: pdl_interp.replace %[[ROOT]] with (%[[RESULT]], %[[RESULTS]], %[[RESULTS_2]] : !pdl.value, !pdl.range<value>, !pdl.value)181 pdl.pattern : benefit(1) {182 %types = types183 %root = operation "foo.op" -> (%types : !pdl.range<type>)184 rewrite %root {185 %newOp = operation "foo.op" -> (%types : !pdl.range<type>)186 %newResult = result 0 of %newOp187 %newResults = results 1 of %newOp -> !pdl.range<value>188 %newResults2 = results 2 of %newOp -> !pdl.value189 replace %root with (%newResult, %newResults, %newResults2 : !pdl.value, !pdl.range<value>, !pdl.value)190 }191 }192}193 194// -----195 196// CHECK-LABEL: module @replace_with_no_results197module @replace_with_no_results {198 // CHECK: module @rewriters199 // 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"204 rewrite %root {205 %newOp = operation "foo.op"206 replace %root with %newOp207 }208 }209}210 211// -----212 213// CHECK-LABEL: module @apply_native_rewrite214module @apply_native_rewrite {215 // CHECK: module @rewriters216 // CHECK: func @pdl_generated_rewriter(%[[ROOT:.*]]: !pdl.operation)217 // CHECK: %[[TYPE:.*]] = pdl_interp.apply_rewrite "functor"(%[[ROOT]] : !pdl.operation) : !pdl.type218 // CHECK: pdl_interp.create_operation "foo.op" -> (%[[TYPE]] : !pdl.type)219 pdl.pattern : benefit(1) {220 %type = type221 %root = operation "foo.op" -> (%type : !pdl.type)222 rewrite %root {223 %newType = apply_native_rewrite "functor"(%root : !pdl.operation) : !pdl.type224 %newOp = operation "foo.op" -> (%newType : !pdl.type)225 }226 }227}228 229// -----230 231// CHECK-LABEL: module @unbound_rewrite_op232module @unbound_rewrite_op {233 // CHECK: module @rewriters234 // CHECK: func @pdl_generated_rewriter()235 // CHECK: %[[UNUSED:.*]] = pdl_interp.create_operation "bar.op"236 // CHECK: pdl_interp.finalize237 pdl.pattern : benefit(1) {238 %root = operation "foo.op"239 rewrite %root {240 %unused = operation "bar.op"241 }242 }243}244 245// -----246 247// CHECK-LABEL: module @range_op248module @range_op {249 // CHECK: module @rewriters250 // 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.finalize254 pdl.pattern : benefit(1) {255 %operand = pdl.operand256 %root = operation "foo.op"(%operand : !pdl.value)257 rewrite %root {258 %emptyRange = pdl.range : !pdl.range<value>259 %range = pdl.range %operand, %emptyRange : !pdl.value, !pdl.range<value>260 }261 }262}263