176 lines · plain
1// RUN: mlir-opt -split-input-file %s | mlir-opt2// RUN: mlir-opt -split-input-file -mlir-print-op-generic -mlir-print-local-scope %s | FileCheck %s --check-prefix=CHECK-GENERIC3 4// -----5 6pdl.pattern @operations : benefit(1) {7 // Operation with attributes and results.8 %attribute = attribute9 %type = type10 %op0 = operation {"attr" = %attribute} -> (%type : !pdl.type)11 %op0_result = pdl.result 0 of %op012 13 // Operation with input.14 %input = operand15 %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 = operand23 %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 = operand31 %input2 = operand32 %type = type33 %op1 = operation(%input1 : !pdl.value) -> (%type : !pdl.type)34 %val1 = result 0 of %op135 %root1 = operation(%val1 : !pdl.value)36 %op2 = operation(%input2 : !pdl.value) -> (%type : !pdl.type)37 %val2 = result 0 of %op238 %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 = operand46 %input2 = operand47 %type = type48 %op1 = operation(%input1 : !pdl.value) -> (%type : !pdl.type)49 %val1 = result 0 of %op150 %root1 = operation(%val1 : !pdl.value)51 %op2 = operation(%input2 : !pdl.value) -> (%type : !pdl.type)52 %val2 = result 0 of %op253 %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 inferred60// from a pdl.replace.61pdl.pattern @infer_type_from_operation_replace : benefit(1) {62 %type1 = type : i3263 %type2 = type64 %root = operation -> (%type1, %type2 : !pdl.type, !pdl.type)65 rewrite %root {66 %type3 = type67 %newOp = operation "foo.op" -> (%type1, %type3 : !pdl.type, !pdl.type)68 replace %root with %newOp69 }70}71 72// -----73 74// Check that the result type of an operation within a rewrite can be inferred75// 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 : i3278 %type2 = type79 %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 inferred88// from the result types of an operation within the match block.89pdl.pattern @infer_type_from_type_used_in_match : benefit(1) {90 %types = types91 %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 inferred101// from the type of an operand within the match block.102pdl.pattern @infer_type_from_type_used_in_match : benefit(1) {103 %type1 = type104 %type2 = type105 %operand1 = operand : %type1106 %operand2 = operand : %type2107 %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 inferred116// from the types of operands within the match block.117pdl.pattern @infer_type_from_type_used_in_match : benefit(1) {118 %types = types119 %operands = operands : %types120 %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 = operation130 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 = operation139 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 = operation147 %attr = apply_native_constraint "NativeConstraint"(%root : !pdl.operation) : !pdl.attribute148 rewrite %root {149 apply_native_rewrite "NativeRewrite"(%attr : !pdl.attribute)150 }151}152 153// -----154 155pdl.pattern @attribute_with_dict : benefit(1) {156 %root = operation157 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 the166// attribute value.167 168pdl.pattern @attribute_with_loc : benefit(1) {169 // CHECK-GENERIC: "pdl.attribute"170 // CHECK-GENERIC-NOT: value = loc171 %attr = attribute loc("bar")172 173 %root = operation {"attribute" = %attr}174 rewrite %root with "rewriter"175}176