1 // RUN: mlir-opt %s -test-vector-sink-patterns -split-input-file | FileCheck %s
3 //-----------------------------------------------------------------------------
4 // [Pattern: ReorderElementwiseOpsOnBroadcast]
5 //-----------------------------------------------------------------------------
7 // CHECK-LABEL: func.func @broadcast_scalar_with_bcast(
8 // CHECK-SAME: %[[ARG_0:.*]]: index, %[[ARG_1:.*]]: index) -> vector<1x4xindex> {
9 // CHECK: %[[ADD:.*]] = arith.addi %[[ARG_0]], %[[ARG_1]] : index
10 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADD]] : index to vector<1x4xindex>
11 // CHECK: return %[[BCAST]] : vector<1x4xindex>
13 func.func @broadcast_scalar_with_bcast(%arg1: index, %arg2: index) -> vector<1x4xindex> {
14 %0 = vector.broadcast %arg1 : index to vector<1x4xindex>
15 %1 = vector.broadcast %arg2 : index to vector<1x4xindex>
16 %2 = arith.addi %0, %1 : vector<1x4xindex>
17 return %2 : vector<1x4xindex>
20 // CHECK-LABEL: func.func @broadcast_scalar_with_bcast_scalable(
21 // CHECK-SAME: %[[ARG_0:.*]]: index, %[[ARG_1:.*]]: index) -> vector<1x[4]xindex> {
22 // CHECK: %[[ADD:.*]] = arith.addi %[[ARG_0]], %[[ARG_1]] : index
23 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADD]] : index to vector<1x[4]xindex>
24 // CHECK: return %[[BCAST]] : vector<1x[4]xindex>
26 func.func @broadcast_scalar_with_bcast_scalable(%arg1: index, %arg2: index) -> vector<1x[4]xindex> {
27 %0 = vector.broadcast %arg1 : index to vector<1x[4]xindex>
28 %1 = vector.broadcast %arg2 : index to vector<1x[4]xindex>
29 %2 = arith.addi %0, %1 : vector<1x[4]xindex>
30 return %2 : vector<1x[4]xindex>
35 // CHECK-LABEL: func.func @broadcast_scalar_with_bcast_and_splat(
36 // CHECK-SAME: %[[ARG1:.*]]: index,
37 // CHECK-SAME: %[[ARG2:.*]]: index) -> vector<1x4xindex> {
38 // CHECK: %[[ADD:.*]] = arith.addi %[[ARG1]], %[[ARG2]] : index
39 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADD]] : index to vector<1x4xindex>
40 // CHECK: return %[[BCAST]] : vector<1x4xindex>
41 func.func @broadcast_scalar_with_bcast_and_splat(%arg1: index, %arg2: index) -> vector<1x4xindex> {
42 %0 = vector.splat %arg1 : vector<1x4xindex>
43 %1 = vector.broadcast %arg2 : index to vector<1x4xindex>
44 %2 = arith.addi %0, %1 : vector<1x4xindex>
45 return %2 : vector<1x4xindex>
48 // CHECK-LABEL: func.func @broadcast_scalar_with_bcast_and_splat_scalable(
49 // CHECK-SAME: %[[ARG1:.*]]: index,
50 // CHECK-SAME: %[[ARG2:.*]]: index) -> vector<1x[4]xindex> {
51 // CHECK: %[[ADD:.*]] = arith.addi %[[ARG1]], %[[ARG2]] : index
52 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADD]] : index to vector<1x[4]xindex>
53 // CHECK: return %[[BCAST]] : vector<1x[4]xindex>
54 func.func @broadcast_scalar_with_bcast_and_splat_scalable(%arg1: index, %arg2: index) -> vector<1x[4]xindex> {
55 %0 = vector.splat %arg1 : vector<1x[4]xindex>
56 %1 = vector.broadcast %arg2 : index to vector<1x[4]xindex>
57 %2 = arith.addi %0, %1 : vector<1x[4]xindex>
58 return %2 : vector<1x[4]xindex>
63 // CHECK-LABEL: func.func @broadcast_vector(
64 // CHECK-SAME: %[[ARG_0:.*]]: vector<4xf32>,
65 // CHECK-SAME: %[[ARG_1:.*]]: vector<4xf32>) -> vector<3x4xf32> {
66 // CHECK: %[[ADDF:.*]] = arith.addf %[[ARG_0]], %[[ARG_1]] : vector<4xf32>
67 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADDF]] : vector<4xf32> to vector<3x4xf32>
68 // CHECK: return %[[BCAST]] : vector<3x4xf32>
70 func.func @broadcast_vector(%arg1: vector<4xf32>, %arg2: vector<4xf32>) -> vector<3x4xf32> {
71 %arg1_bcast = vector.broadcast %arg1 : vector<4xf32> to vector<3x4xf32>
72 %arg2_bcast = vector.broadcast %arg2 : vector<4xf32> to vector<3x4xf32>
73 %2 = arith.addf %arg1_bcast, %arg2_bcast : vector<3x4xf32>
74 return %2 : vector<3x4xf32>
77 // CHECK-LABEL: func.func @broadcast_vector_scalable(
78 // CHECK-SAME: %[[ARG_0:.*]]: vector<[4]xf32>,
79 // CHECK-SAME: %[[ARG_1:.*]]: vector<[4]xf32>) -> vector<3x[4]xf32> {
80 // CHECK: %[[ADDF:.*]] = arith.addf %[[ARG_0]], %[[ARG_1]] : vector<[4]xf32>
81 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADDF]] : vector<[4]xf32> to vector<3x[4]xf32>
82 // CHECK: return %[[BCAST]] : vector<3x[4]xf32>
84 func.func @broadcast_vector_scalable(%arg1: vector<[4]xf32>, %arg2: vector<[4]xf32>) -> vector<3x[4]xf32> {
85 %arg1_bcast = vector.broadcast %arg1 : vector<[4]xf32> to vector<3x[4]xf32>
86 %arg2_bcast = vector.broadcast %arg2 : vector<[4]xf32> to vector<3x[4]xf32>
87 %2 = arith.addf %arg1_bcast, %arg2_bcast : vector<3x[4]xf32>
88 return %2 : vector<3x[4]xf32>
93 // CHECK-LABEL: func.func @broadcast_scalar_and_vec(
94 // CHECK-SAME: %[[ARG1:.*]]: index,
95 // CHECK-SAME: %[[ARG2:.*]]: vector<4xindex>) -> vector<1x4xindex> {
96 // CHECK: %[[SPLAT:.*]] = vector.splat %[[ARG1]] : vector<1x4xindex>
97 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ARG2]] : vector<4xindex> to vector<1x4xindex>
98 // CHECK: %[[ADD:.*]] = arith.addi %[[SPLAT]], %[[BCAST]] : vector<1x4xindex>
99 // CHECK: return %[[ADD]] : vector<1x4xindex>
100 func.func @broadcast_scalar_and_vec(%arg1: index, %arg2: vector<4xindex>) -> vector<1x4xindex> {
101 %0 = vector.splat %arg1 : vector<1x4xindex>
102 %1 = vector.broadcast %arg2 : vector<4xindex> to vector<1x4xindex>
103 %2 = arith.addi %0, %1 : vector<1x4xindex>
104 return %2 : vector<1x4xindex>
107 // CHECK-LABEL: func.func @broadcast_scalar_and_vec_scalable(
108 // CHECK-SAME: %[[ARG1:.*]]: index,
109 // CHECK-SAME: %[[ARG2:.*]]: vector<[4]xindex>) -> vector<1x[4]xindex> {
110 // CHECK: %[[SPLAT:.*]] = vector.splat %[[ARG1]] : vector<1x[4]xindex>
111 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ARG2]] : vector<[4]xindex> to vector<1x[4]xindex>
112 // CHECK: %[[ADD:.*]] = arith.addi %[[SPLAT]], %[[BCAST]] : vector<1x[4]xindex>
113 // CHECK: return %[[ADD]] : vector<1x[4]xindex>
114 func.func @broadcast_scalar_and_vec_scalable(%arg1: index, %arg2: vector<[4]xindex>) -> vector<1x[4]xindex> {
115 %0 = vector.splat %arg1 : vector<1x[4]xindex>
116 %1 = vector.broadcast %arg2 : vector<[4]xindex> to vector<1x[4]xindex>
117 %2 = arith.addi %0, %1 : vector<1x[4]xindex>
118 return %2 : vector<1x[4]xindex>
123 // CHECK-LABEL: func.func @broadcast_vector_and_scalar(
124 // CHECK-SAME: %[[ARG_0:.*]]: i32,
125 // CHECK-SAME: %[[ARG_1:.*]]: vector<4xi32>) -> vector<4xi32> {
126 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ARG_0]] : i32 to vector<4xi32>
127 // CHECK: %[[ADD:.*]] = arith.addi %[[BCAST]], %[[ARG_1]] : vector<4xi32>
128 // CHECK: return %[[ADD]] : vector<4xi32>
130 func.func @broadcast_vector_and_scalar(%arg1: i32, %arg2: vector<4xi32>) -> vector<4xi32> {
131 %arg1_bcast = vector.broadcast %arg1 : i32 to vector<4xi32>
132 %2 = arith.addi %arg1_bcast, %arg2 : vector<4xi32>
133 return %2 : vector<4xi32>
136 // CHECK-LABEL: func.func @broadcast_vector_and_scalar_scalable(
137 // CHECK-SAME: %[[ARG_0:.*]]: i32,
138 // CHECK-SAME: %[[ARG_1:.*]]: vector<[4]xi32>) -> vector<[4]xi32> {
139 // CHECK: %[[BCAST:.*]] = vector.broadcast %[[ARG_0]] : i32 to vector<[4]xi32>
140 // CHECK: %[[ADD:.*]] = arith.addi %[[BCAST]], %[[ARG_1]] : vector<[4]xi32>
141 // CHECK: return %[[ADD]] : vector<[4]xi32>
143 func.func @broadcast_vector_and_scalar_scalable(%arg1: i32, %arg2: vector<[4]xi32>) -> vector<[4]xi32> {
144 %arg1_bcast = vector.broadcast %arg1 : i32 to vector<[4]xi32>
145 %2 = arith.addi %arg1_bcast, %arg2 : vector<[4]xi32>
146 return %2 : vector<[4]xi32>
152 affine_map<(i, j, k) -> (i, k)>,
153 affine_map<(i, j, k) -> (k, j)>,
154 affine_map<(i, j, k) -> (i, j)>
157 indexing_maps = #matmat_accesses,
158 iterator_types = ["parallel", "parallel", "reduction"]
161 // CHECK-LABEL: func.func @negative_not_elementwise
162 // CHECK-DAG: %[[F1:.*]] = arith.constant dense<1.000000e+00> : vector<2x2xf32>
163 // CHECK-DAG: %[[F2:.*]] = arith.constant dense<2.000000e+00> : vector<2x2xf32>
164 // CHECK-DAG: %[[F3:.*]] = arith.constant dense<3.000000e+00> : vector<2x2xf32>
165 // CHECK: %[[RES:.*]] = vector.contract {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %[[F1]], %[[F2]], %[[F3]] : vector<2x2xf32>, vector<2x2xf32> into vector<2x2xf32>
166 func.func @negative_not_elementwise() -> vector<2x2xf32> {
167 %f1 = arith.constant 1.0: f32
168 %f2 = arith.constant 2.0: f32
169 %f3 = arith.constant 3.0: f32
171 %A = vector.broadcast %f1 : f32 to vector<2x2xf32>
172 %B = vector.broadcast %f2 : f32 to vector<2x2xf32>
173 %C = vector.broadcast %f3 : f32 to vector<2x2xf32>
174 %res = vector.contract #matmat_trait %A, %B, %C
175 : vector<2x2xf32>, vector<2x2xf32> into vector<2x2xf32>
177 return %res : vector<2x2xf32>
182 // The source and the result for arith.cmp have different types - not supported
184 // CHECK-LABEL: func.func @negative_source_and_result_mismatch
185 // CHECK: %[[BROADCAST:.+]] = vector.broadcast
186 // CHECK: %[[RETURN:.+]] = arith.cmpf uno, %[[BROADCAST]], %[[BROADCAST]]
187 // CHECK: return %[[RETURN]]
188 func.func @negative_source_and_result_mismatch(%arg0 : f32, %arg1 : vector<1xf32>) -> vector<1xi1> {
189 %0 = vector.broadcast %arg0 : f32 to vector<1xf32>
190 %1 = arith.cmpf uno, %0, %0 : vector<1xf32>
191 return %1 : vector<1xi1>
196 // vector.fma only supports vectors - currently it's not possible to replace this with e.g.:
197 // %scalar_res = vector.fma %scalar_1, %scalar2
198 // %vec_res = vector.broadcast %scalar_res
200 // TODO: It should be possible to support this case
202 // CHECK-LABEL: func.func @negative_op_only_supports_vectors
203 // CHECK: %[[BROADCAST:.+]] = vector.broadcast
204 // CHECK: %[[RESULT:.+]] = vector.fma %[[BROADCAST]]
205 // CHECK: return %[[RESULT]]
206 func.func @negative_op_only_supports_vectors(%arg0 : f32) -> vector<1xf32> {
207 %0 = vector.broadcast %arg0 : f32 to vector<1xf32>
208 %1 = vector.fma %0, %0, %0 : vector<1xf32>
209 return %1 : vector<1xf32>
212 //===----------------------------------------------------------------------===//
213 // [Pattern: ReorderCastOpsOnBroadcast]
215 // Reorder casting ops and vector ops. The casting ops have almost identical
216 // pattern, so only arith.extsi op is tested.
217 //===----------------------------------------------------------------------===//
221 func.func @broadcast_vector_extsi(%a : vector<4xi8>) -> vector<2x4xi32> {
222 // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : vector<4xi8> to vector<4xi32>
223 // CHECK: vector.broadcast %[[EXT:.+]] : vector<4xi32> to vector<2x4xi32>
224 %b = vector.broadcast %a : vector<4xi8> to vector<2x4xi8>
225 %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
226 return %r : vector<2x4xi32>
231 func.func @broadcast_scalar_extsi(%a : i8) -> vector<2x4xi32> {
232 // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : i8 to i32
233 // CHECK: vector.broadcast %[[EXT]] : i32 to vector<2x4xi32>
234 %b = vector.broadcast %a : i8 to vector<2x4xi8>
235 %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
236 return %r : vector<2x4xi32>
239 //===----------------------------------------------------------------------===//
240 // [Pattern: ReorderElementwiseOpsOnTranspose]
241 //===----------------------------------------------------------------------===//
243 func.func @transpose_extsi(%a : vector<4x2xi8>) -> vector<2x4xi32> {
244 // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : vector<4x2xi8> to vector<4x2xi32>
245 // CHECK: vector.transpose %[[EXT]], [1, 0] : vector<4x2xi32> to vector<2x4xi32>
246 %b = vector.transpose %a, [1, 0]: vector<4x2xi8> to vector<2x4xi8>
247 %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
248 return %r : vector<2x4xi32>
253 // CHECK-LABEL: func @transpose_elementwise_same_type
254 // CHECK-SAME: (%[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
255 // CHECK: %[[ADD:.+]] = arith.addf %[[A]], %[[B]] : vector<4x2xf32>
256 // CHECK: %[[T:.+]] = vector.transpose %[[ADD]], [1, 0]
257 // CHECK: return %[[T]]
259 func.func @transpose_elementwise_same_type(%a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xf32> {
260 %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
261 %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
262 %r = arith.addf %at, %bt : vector<2x4xf32>
263 return %r : vector<2x4xf32>
268 // CHECK-LABEL: func @transpose_elementwise_diff_operand_types
269 // CHECK-SAME: (%[[COND:.+]]: vector<4x2xi1>, %[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
270 // CHECK: %[[S:.+]] = arith.select %[[COND]], %[[A]], %[[B]] : vector<4x2xi1>, vector<4x2xf32>
271 // CHECK: %[[T:.+]] = vector.transpose %[[S]], [1, 0] : vector<4x2xf32> to vector<2x4xf32>
272 // CHECK: return %[[T]]
273 func.func @transpose_elementwise_diff_operand_types(%cond: vector<4x2xi1>, %a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xf32> {
274 %condt = vector.transpose %cond, [1, 0]: vector<4x2xi1> to vector<2x4xi1>
275 %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
276 %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
277 %r = arith.select %condt, %at, %bt : vector<2x4xi1>, vector<2x4xf32>
278 return %r : vector<2x4xf32>
283 // CHECK-LABEL: func @transpose_elementwise_diff_operand_result_type
284 // CHECK-SAME: (%[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
285 // CHECK: %[[CMP:.+]] = arith.cmpf olt, %[[A]], %[[B]] : vector<4x2xf32>
286 // CHECK: %[[T:.+]] = vector.transpose %[[CMP]], [1, 0] : vector<4x2xi1> to vector<2x4xi1>
287 // CHECK: return %[[T]]
288 func.func @transpose_elementwise_diff_operand_result_type(%a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xi1> {
289 %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
290 %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
291 %r = arith.cmpf olt, %at, %bt : vector<2x4xf32>
292 return %r : vector<2x4xi1>
297 // CHECK-LABEL: func @transpose_elementwise_splat_constant
298 // CHECK-SAME: (%[[A:.+]]: vector<4x6x3x2xf32>)
299 // CHECK: %[[B:.+]] = arith.constant dense<5.000000e+00> : vector<4x6x3x2xf32>
300 // CHECK: %[[ADD:.+]] = arith.addf %[[A]], %[[B]] : vector<4x6x3x2xf32>
301 // CHECK: %[[T:.+]] = vector.transpose %[[ADD]], [1, 0, 3, 2] : vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
302 // CHECK: return %[[T:.+]] : vector<6x4x2x3xf32>
304 func.func @transpose_elementwise_splat_constant(%a : vector<4x6x3x2xf32>) -> vector<6x4x2x3xf32> {
305 %b = arith.constant dense<5.0> : vector<6x4x2x3xf32>
306 %at = vector.transpose %a, [1, 0, 3, 2]: vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
307 %r = arith.addf %at, %b : vector<6x4x2x3xf32>
308 return %r : vector<6x4x2x3xf32>
313 // CHECK-LABEL: func @transpose_elementwise_diff_map
314 // CHECK: vector.transpose
315 // CHECK: vector.transpose
317 func.func @transpose_elementwise_diff_map(%a : vector<4x6x3x2xf32>, %b: vector<6x2x4x3xf32>) -> vector<6x4x2x3xf32> {
318 %at = vector.transpose %a, [1, 0, 3, 2]: vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
319 %bt = vector.transpose %b, [0, 2, 1, 3]: vector<6x2x4x3xf32> to vector<6x4x2x3xf32>
320 %r = arith.addf %at, %bt : vector<6x4x2x3xf32>
321 return %r : vector<6x4x2x3xf32>