Debian Codegraph @

mlir/test/Dialect/Linalg/vectorization/pad-with-patterns.mlir

raw
1 // RUN: mlir-opt %s -transform-interpreter -split-input-file | FileCheck %s
2
3 ///----------------------------------------------------------------------------------------
4 /// Tests for tensor.pad
5 ///----------------------------------------------------------------------------------------
6
7 // CHECK-LABEL: func @pad_static(
8 // CHECK-SAME: %[[ARG0:.*]]: tensor<2x?x2xf32>, %[[PAD:.*]]: f32
9 // CHECK-NOT: tensor.pad
10 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
11 // CHECK-DAG: %[[C2:.*]] = arith.constant 2 : index
12 // 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]]
18 func.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 : f32
22 } : tensor<2x?x2xf32> to tensor<2x3x4xf32>
23 return %0 : tensor<2x3x4xf32>
24 }
25
26
27 module 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_op
30 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
31 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op
32 transform.yield
33 }
34 }
35
36 // -----
37
38 // CHECK-LABEL: func @pad_static_source(
39 // CHECK-SAME: %[[ARG0:.*]]: tensor<2x5x2xf32>, %[[PAD:.*]]: f32
40 // CHECK-NOT: tensor.pad
41 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
42 // CHECK-DAG: %[[C2:.*]] = arith.constant 2 : index
43 // 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]]
49 func.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 : f32
53 } : tensor<2x5x2xf32> to tensor<2x6x4xf32>
54 return %0 : tensor<2x6x4xf32>
55 }
56
57
58 module 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_op
61 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
62 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op
63 transform.yield
64 }
65 }
66
67
68 // -----
69
70 // CHECK-LABEL: func @pad_static_dynamic(
71 // CHECK-SAME: %[[SRC:.*]]: tensor<1x2x2x?xf32>, %[[LOW:.*]]: index, %[[HIGH:.*]]: index
72 // CHECK-NOT: tensor.pad
73 // CHECK-DAG: %[[C2:.*]] = arith.constant 2 : index
74 // CHECK-DAG: %[[C3:.*]] = arith.constant 3 : index
75 // CHECK-DAG: %[[C5:.*]] = arith.constant 5 : index
76 // CHECK: %[[V0:.*]] = arith.addi %[[LOW]], %[[C2]] : index
77 // CHECK: %[[V1:.*]] = arith.addi %[[V0]], %[[C3]] : index
78 // CHECK: %[[V2:.*]] = arith.addi %[[HIGH]], %[[C5]] : index
79 // CHECK: %[[DIM3:.*]] = tensor.dim %[[SRC]], %[[C3]] : tensor<1x2x2x?xf32>
80 // CHECK: %[[V4:.*]] = arith.addi %[[DIM3]], %[[C3]] : index
81 // CHECK: %[[V5:.*]] = arith.addi %[[V4]], %[[C2]] : index
82 // 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]]
87 func.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 : f32
92 } : tensor<1x2x2x?xf32> to tensor<6x?x?x?xf32>
93 return %0 : tensor<6x?x?x?xf32>
94 }
95
96
97 module 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_op
100 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
101 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op
102 transform.yield
103 }
104 }
105
106 // -----
107
108 // CHECK-LABEL: func @pad_static_complex(
109 // CHECK-NOT: vector<
110 func.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
119 module 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_op
122 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
123 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op
124 transform.yield
125 }
126 }
127
128 // -----
129
130 func.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 : f32
135 // CHECK: %[[CST:.*]] = arith.constant dense<5.000000e+00> : vector<1x12x13xf32>
136 // CHECK: %[[C0_IDX:.*]] = arith.constant 0 : index
137 // CHECK: %[[PAD_VAL:.*]] = arith.constant 5.000000e+00 : f32
138 // 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>
146 func.func @pad_and_insert_slice_dest(
147 %arg0: tensor<1x5x6xf32>) -> tensor<1x12x13xf32> {
148 %c5 = arith.constant 5.0 : f32
149 %0 = tensor.pad %arg0 low[0, 0, 0] high[0, 7, 7] {
150 ^bb0(%arg2: index, %arg3: index, %arg4: index):
151 tensor.yield %c5 : f32
152 } : 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
158 module 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_op
161 %4 = transform.get_parent_op %3 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
162 %5 = transform.structured.vectorize_children_and_apply_patterns %4 { vectorize_padding } : (!transform.any_op) -> !transform.any_op
163 transform.yield
164 }
165 }
166
167 // -----
168
169 // CHECK-LABEL: func @pad_tensor_non_const_pad_value
170 // CHECK-SAME: %[[ARG0:.*]]: tensor<5x6xf32>
171 // CHECK-NOT: tensor.pad
172 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
173 // CHECK-DAG: %[[C3:.*]] = arith.constant 3 : index
174 // CHECK-DAG: %[[C4:.*]] = arith.constant 4 : index
175 // CHECK: %[[FILL:.*]] = tensor.generate
176 // CHECK: %[[RES:.*]] = arith.mulf
177 // CHECK: tensor.yield %[[RES]] : f32
178 // 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]]
181 func.func @pad_tensor_non_const_pad_value(%arg0: tensor<5x6xf32>) -> tensor<12x13xf32> {
182 %c0 = arith.constant 0 : index
183 %c5 = arith.constant 5.0 : f32
184 %0 = tensor.pad %arg0 low[3, 4] high[4, 3] {
185 ^bb0(%arg1: index, %arg2: index):
186 %i1 = arith.index_cast %arg1 : index to i32
187 %i2 = arith.index_cast %arg2 : index to i32
188 %f1 = arith.sitofp %i1 : i32 to f32
189 %f2 = arith.sitofp %i2 : i32 to f32
190 %m = arith.mulf %f1, %f2 : f32
191 tensor.yield %m : f32
192 } : tensor<5x6xf32> to tensor<12x13xf32>
193 return %0 : tensor<12x13xf32>
194 }
195
196
197 module 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_op
200 %4 = transform.get_parent_op %3 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
201 %5 = transform.structured.vectorize_children_and_apply_patterns %4 { vectorize_padding } : (!transform.any_op) -> !transform.any_op
202 transform.yield
203 }
204 }
205
206 // -----
207
208 // CHECK-LABEL: func @test_masked_pad_static_dynamic
209 func.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.pad
212 %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 : f32
215 } : tensor<1x2x2x?xf32> to tensor<6x?x?x?xf32>
216 return %0 : tensor<6x?x?x?xf32>
217 }
218
219
220 module 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_op
223 %1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
224 %2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_padding } : (!transform.any_op) -> !transform.any_op
225 transform.yield
226 }
227 }