228 lines · plain
1// RUN: mlir-opt %s -transform-interpreter -split-input-file | FileCheck %s2 3///----------------------------------------------------------------------------------------4/// Tests for tensor.pad5///----------------------------------------------------------------------------------------6 7// CHECK-LABEL: func @pad_static(8// CHECK-SAME: %[[ARG0:.*]]: tensor<2x?x2xf32>, %[[PAD:.*]]: f329// CHECK-NOT: tensor.pad10// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index11// CHECK-DAG: %[[C2:.*]] = arith.constant 2 : index12// CHECK-DAG: %[[INIT:.*]] = tensor.empty() : tensor<2x3x4xf32>13// CHECK-DAG: %[[VEC:.*]] = vector.broadcast %[[PAD]] : f32 to vector<2x3x4xf32>14// CHECK: %[[FILL:.*]] = vector.transfer_write %[[VEC]], %[[INIT]]{{.*}} : vector<2x3x4xf32>, tensor<2x3x4xf32>15// CHECK: %[[READ:.*]] = vector.transfer_read %[[ARG0]][%[[C0]], %[[C0]], %[[C0]]], %[[PAD]] {in_bounds = [true, false, true]} : tensor<2x?x2xf32>, vector<2x3x2xf32>16// CHECK: %[[RESULT:.*]] = vector.transfer_write %[[READ]], %[[FILL]][%[[C0]], %[[C0]], %[[C2]]] {in_bounds = [true, true, true]} : vector<2x3x2xf32>, tensor<2x3x4xf32>17// CHECK: return %[[RESULT]]18func.func @pad_static(%arg0: tensor<2x?x2xf32>, %pad_value: f32) -> tensor<2x3x4xf32> {19 %0 = tensor.pad %arg0 low[0, 0, 2] high[0, 1, 0] {20 ^bb0(%arg1: index, %arg2: index, %arg3: index):21 tensor.yield %pad_value : f3222 } : tensor<2x?x2xf32> to tensor<2x3x4xf32>23 return %0 : tensor<2x3x4xf32>24}25 26 27module attributes {transform.with_named_sequence} {28 transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {29 %0 = transform.structured.match ops{["tensor.pad"]} in %arg1 : (!transform.any_op) -> !transform.any_op30 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op31 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op32 transform.yield33 }34}35 36// -----37 38// CHECK-LABEL: func @pad_static_source(39// CHECK-SAME: %[[ARG0:.*]]: tensor<2x5x2xf32>, %[[PAD:.*]]: f3240// CHECK-NOT: tensor.pad41// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index42// CHECK-DAG: %[[C2:.*]] = arith.constant 2 : index43// CHECK: %[[INIT:.*]] = tensor.empty() : tensor<2x6x4xf32>44// CHECK: %[[VEC:.*]] = vector.broadcast %[[PAD]] : f32 to vector<2x6x4xf32>45// CHECK: %[[FILL:.*]] = vector.transfer_write %[[VEC]], %[[INIT]][%[[C0]], %[[C0]], %[[C0]]] {in_bounds = [true, true, true]} : vector<2x6x4xf32>, tensor<2x6x4xf32>46// CHECK: %[[READ:.*]] = vector.transfer_read %[[ARG0]][%[[C0]], %[[C0]], %[[C0]]], %{{.*}} {in_bounds = [true, true, true]} : tensor<2x5x2xf32>, vector<2x5x2xf32>47// CHECK: %[[WRITE:.*]] = vector.transfer_write %[[READ]], %[[FILL]][%[[C0]], %[[C0]], %[[C2]]] {in_bounds = [true, true, true]} : vector<2x5x2xf32>, tensor<2x6x4xf32>48// CHECK: return %[[WRITE]]49func.func @pad_static_source(%arg0: tensor<2x5x2xf32>, %pad_value: f32) -> tensor<2x6x4xf32> {50 %0 = tensor.pad %arg0 low[0, 0, 2] high[0, 1, 0] {51 ^bb0(%arg1: index, %arg2: index, %arg3: index):52 tensor.yield %pad_value : f3253 } : tensor<2x5x2xf32> to tensor<2x6x4xf32>54 return %0 : tensor<2x6x4xf32>55}56 57 58module attributes {transform.with_named_sequence} {59 transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {60 %0 = transform.structured.match ops{["tensor.pad"]} in %arg1 : (!transform.any_op) -> !transform.any_op61 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op62 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op63 transform.yield64 }65}66 67 68// -----69 70// CHECK-LABEL: func @pad_static_dynamic(71// CHECK-SAME: %[[SRC:.*]]: tensor<1x2x2x?xf32>, %[[LOW:.*]]: index, %[[HIGH:.*]]: index72// CHECK-NOT: tensor.pad73// CHECK-DAG: %[[C2:.*]] = arith.constant 2 : index74// CHECK-DAG: %[[C3:.*]] = arith.constant 3 : index75// CHECK-DAG: %[[C5:.*]] = arith.constant 5 : index76// CHECK: %[[V0:.*]] = arith.addi %[[LOW]], %[[C2]] : index77// CHECK: %[[V1:.*]] = arith.addi %[[V0]], %[[C3]] : index78// CHECK: %[[V2:.*]] = arith.addi %[[HIGH]], %[[C5]] : index79// CHECK: %[[DIM3:.*]] = tensor.dim %[[SRC]], %[[C3]] : tensor<1x2x2x?xf32>80// CHECK: %[[V4:.*]] = arith.addi %[[DIM3]], %[[C3]] : index81// CHECK: %[[V5:.*]] = arith.addi %[[V4]], %[[C2]] : index82// CHECK: %[[INIT:.*]] = tensor.empty(%[[V1]], %[[V2]], %[[V5]]) : tensor<6x?x?x?xf32>83// CHECK: %[[FILL:.*]] = linalg.fill ins(%{{.*}} : f32) outs(%[[INIT]] : tensor<6x?x?x?xf32>) -> tensor<6x?x?x?xf32>84// CHECK: %[[SRCDIM:.*]] = tensor.dim %[[SRC]], %[[C3]] : tensor<1x2x2x?xf32>85// CHECK: %[[RESULT:.*]] = tensor.insert_slice %[[SRC]] into %[[FILL]][2, %[[LOW]], 3, 3] [1, 2, 2, %[[SRCDIM]]] [1, 1, 1, 1] : tensor<1x2x2x?xf32> into tensor<6x?x?x?xf32>86// CHECK: return %[[RESULT]]87func.func @pad_static_dynamic(%arg0: tensor<1x2x2x?xf32>, %low: index, %high: index,88 %pad_value: f32) -> tensor<6x?x?x?xf32> {89 %0 = tensor.pad %arg0 low[2, %low, 3, 3] high[3, 3, %high, 2] {90 ^bb0(%arg1: index, %arg2: index, %arg3: index, %arg4: index):91 tensor.yield %pad_value : f3292 } : tensor<1x2x2x?xf32> to tensor<6x?x?x?xf32>93 return %0 : tensor<6x?x?x?xf32>94}95 96 97module attributes {transform.with_named_sequence} {98 transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {99 %0 = transform.structured.match ops{["tensor.pad"]} in %arg1 : (!transform.any_op) -> !transform.any_op100 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op101 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op102 transform.yield103 }104}105 106// -----107 108// CHECK-LABEL: func @pad_static_complex(109// CHECK-NOT: vector<110func.func @pad_static_complex(%arg0: tensor<2x5x2xcomplex<f32>>, %pad_value: complex<f32>) -> tensor<2x6x4xcomplex<f32>> {111 %0 = tensor.pad %arg0 low[0, 0, 2] high[0, 1, 0] {112 ^bb0(%arg1: index, %arg2: index, %arg3: index):113 tensor.yield %pad_value : complex<f32>114 } : tensor<2x5x2xcomplex<f32>> to tensor<2x6x4xcomplex<f32>>115 return %0 : tensor<2x6x4xcomplex<f32>>116}117 118 119module attributes {transform.with_named_sequence} {120 transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {121 %0 = transform.structured.match ops{["tensor.pad"]} in %arg1 : (!transform.any_op) -> !transform.any_op122 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op123 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op124 transform.yield125 }126}127 128// -----129 130func.func private @make_vector() -> tensor<12x13xf32>131 132// CHECK-LABEL: func.func @pad_and_insert_slice_dest(133// CHECK-SAME: %[[ARG_0:.*]]: tensor<1x5x6xf32>) -> tensor<1x12x13xf32> {134// CHECK: %[[C0:.*]] = arith.constant 0.000000e+00 : f32135// CHECK: %[[CST:.*]] = arith.constant dense<5.000000e+00> : vector<1x12x13xf32>136// CHECK: %[[C0_IDX:.*]] = arith.constant 0 : index137// CHECK: %[[PAD_VAL:.*]] = arith.constant 5.000000e+00 : f32138// CHECK: %[[EMPTY:.*]] = tensor.empty() : tensor<1x12x13xf32>139// CHECK: %[[WRITE_1:.*]] = vector.transfer_write %[[CST]], %[[EMPTY]]{{\[}}%[[C0_IDX]], %[[C0_IDX]], %[[C0_IDX]]] {in_bounds = [true, true, true]} : vector<1x12x13xf32>, tensor<1x12x13xf32>140// CHECK: %[[READ_1:.*]] = vector.transfer_read %[[ARG_0]]{{\[}}%[[C0_IDX]], %[[C0_IDX]], %[[C0_IDX]]], %[[PAD_VAL]] {in_bounds = [true, true, true]} : tensor<1x5x6xf32>, vector<1x5x6xf32>141// CHECK: %[[WRITE_2:.*]] = vector.transfer_write %[[READ_1]], %[[WRITE_1]]{{\[}}%[[C0_IDX]], %[[C0_IDX]], %[[C0_IDX]]] {in_bounds = [true, true, true]} : vector<1x5x6xf32>, tensor<1x12x13xf32>142// CHECK: %[[MAKE_VEC:.*]] = call @make_vector() : () -> tensor<12x13xf32>143// CHECK: %[[READ_2:.*]] = vector.transfer_read %[[MAKE_VEC]]{{\[}}%[[C0_IDX]], %[[C0_IDX]]], %[[C0]] {in_bounds = [true, true]} : tensor<12x13xf32>, vector<12x13xf32>144// CHECK: %[[RES:.*]] = vector.transfer_write %[[READ_2]], %[[WRITE_2]]{{\[}}%[[C0_IDX]], %[[C0_IDX]], %[[C0_IDX]]] {in_bounds = [true, true]} : vector<12x13xf32>, tensor<1x12x13xf32>145// CHECK: return %[[RES]] : tensor<1x12x13xf32>146func.func @pad_and_insert_slice_dest(147 %arg0: tensor<1x5x6xf32>) -> tensor<1x12x13xf32> {148 %c5 = arith.constant 5.0 : f32149 %0 = tensor.pad %arg0 low[0, 0, 0] high[0, 7, 7] {150 ^bb0(%arg2: index, %arg3: index, %arg4: index):151 tensor.yield %c5 : f32152 } : tensor<1x5x6xf32> to tensor<1x12x13xf32>153 %1 = call @make_vector() : () -> tensor<12x13xf32>154 %r = tensor.insert_slice %1 into %0[0, 0, 0][1, 12, 13][1, 1, 1] : tensor<12x13xf32> into tensor<1x12x13xf32>155 return %r : tensor<1x12x13xf32>156}157 158module attributes {transform.with_named_sequence} {159 transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {160 %3 = transform.structured.match ops{["tensor.pad"]} in %arg1 : (!transform.any_op) -> !transform.any_op161 %4 = transform.get_parent_op %3 {isolated_from_above} : (!transform.any_op) -> !transform.any_op162 %5 = transform.structured.vectorize_children_and_apply_patterns %4 { vectorize_padding } : (!transform.any_op) -> !transform.any_op163 transform.yield164 }165}166 167// -----168 169// CHECK-LABEL: func @pad_tensor_non_const_pad_value170// CHECK-SAME: %[[ARG0:.*]]: tensor<5x6xf32>171// CHECK-NOT: tensor.pad172// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index173// CHECK-DAG: %[[C3:.*]] = arith.constant 3 : index174// CHECK-DAG: %[[C4:.*]] = arith.constant 4 : index175// CHECK: %[[FILL:.*]] = tensor.generate176// CHECK: %[[RES:.*]] = arith.mulf177// CHECK: tensor.yield %[[RES]] : f32178// CHECK: %[[READ:.*]] = vector.transfer_read %[[ARG0]][%[[C0]], %[[C0]]], %{{.*}} {in_bounds = [true, true]} : tensor<5x6xf32>, vector<5x6xf32>179// CHECK: %[[WRITE:.*]] = vector.transfer_write %[[READ]], %[[FILL]][%[[C3]], %[[C4]]] {in_bounds = [true, true]} : vector<5x6xf32>, tensor<12x13xf32>180// CHECK: return %[[WRITE]]181func.func @pad_tensor_non_const_pad_value(%arg0: tensor<5x6xf32>) -> tensor<12x13xf32> {182 %c0 = arith.constant 0 : index183 %c5 = arith.constant 5.0 : f32184 %0 = tensor.pad %arg0 low[3, 4] high[4, 3] {185 ^bb0(%arg1: index, %arg2: index):186 %i1 = arith.index_cast %arg1 : index to i32187 %i2 = arith.index_cast %arg2 : index to i32188 %f1 = arith.sitofp %i1 : i32 to f32189 %f2 = arith.sitofp %i2 : i32 to f32190 %m = arith.mulf %f1, %f2 : f32191 tensor.yield %m : f32192 } : tensor<5x6xf32> to tensor<12x13xf32>193 return %0 : tensor<12x13xf32>194}195 196 197module attributes {transform.with_named_sequence} {198 transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {199 %3 = transform.structured.match ops{["tensor.pad"]} in %arg1 : (!transform.any_op) -> !transform.any_op200 %4 = transform.get_parent_op %3 {isolated_from_above} : (!transform.any_op) -> !transform.any_op201 %5 = transform.structured.vectorize_children_and_apply_patterns %4 { vectorize_padding } : (!transform.any_op) -> !transform.any_op202 transform.yield203 }204}205 206// -----207 208// CHECK-LABEL: func @test_masked_pad_static_dynamic209func.func @test_masked_pad_static_dynamic(%arg0: tensor<1x2x2x?xf32>, %low: index, %high: index,210 %pad_value: f32) -> tensor<6x?x?x?xf32> {211 // CHECK: tensor.pad212 %0 = tensor.pad %arg0 low[2, %low, 3, 3] high[3, 3, %high, 2] {213 ^bb0(%arg1: index, %arg2: index, %arg3: index, %arg4: index):214 tensor.yield %pad_value : f32215 } : tensor<1x2x2x?xf32> to tensor<6x?x?x?xf32>216 return %0 : tensor<6x?x?x?xf32>217}218 219 220module attributes {transform.with_named_sequence} {221 transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {222 %0 = transform.structured.match ops{["tensor.pad"]} in %arg1 : (!transform.any_op) -> !transform.any_op223 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op224 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op225 transform.yield226 }227}228