1
1
// RUN: mlir-opt %s -test-sink-vector-broadcast -split-input-file | FileCheck %s
2
2
3
+ //-----------------------------------------------------------------------------
4
+ // [Pattern: ReorderElementwiseOpsOnBroadcast]
5
+ //-----------------------------------------------------------------------------
6
+
3
7
// CHECK-LABEL: func.func @broadcast_scalar_with_bcast(
4
8
// CHECK-SAME: %[[ARG_0:.*]]: index, %[[ARG_1:.*]]: index) -> vector<1x4xindex> {
5
9
// CHECK: %[[ADD:.*]] = arith.addi %[[ARG_0]], %[[ARG_1]] : index
6
10
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADD]] : index to vector<1x4xindex>
7
11
// CHECK: return %[[BCAST]] : vector<1x4xindex>
8
12
9
- func.func @broadcast_scalar_with_bcast ( %arg1: index , %arg2: index ) -> vector <1 x4 xindex > {
13
+ func.func @broadcast_scalar_with_bcast (%arg1: index , %arg2: index ) -> vector <1 x4 xindex > {
10
14
%0 = vector.broadcast %arg1 : index to vector <1 x4 xindex >
11
15
%1 = vector.broadcast %arg2 : index to vector <1 x4 xindex >
12
16
%2 = arith.addi %0 , %1 : vector <1 x4 xindex >
13
17
return %2 : vector <1 x4 xindex >
14
18
}
15
19
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>
25
+
26
+ func.func @broadcast_scalar_with_bcast_scalable (%arg1: index , %arg2: index ) -> vector <1 x[4 ]xindex > {
27
+ %0 = vector.broadcast %arg1 : index to vector <1 x[4 ]xindex >
28
+ %1 = vector.broadcast %arg2 : index to vector <1 x[4 ]xindex >
29
+ %2 = arith.addi %0 , %1 : vector <1 x[4 ]xindex >
30
+ return %2 : vector <1 x[4 ]xindex >
31
+ }
32
+
16
33
// -----
17
34
18
35
// CHECK-LABEL: func.func @broadcast_scalar_with_bcast_and_splat(
@@ -21,13 +38,26 @@ func.func @broadcast_scalar_with_bcast( %arg1: index, %arg2: index) -> vector<1x
21
38
// CHECK: %[[ADD:.*]] = arith.addi %[[ARG1]], %[[ARG2]] : index
22
39
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADD]] : index to vector<1x4xindex>
23
40
// CHECK: return %[[BCAST]] : vector<1x4xindex>
24
- func.func @broadcast_scalar_with_bcast_and_splat ( %arg1: index , %arg2: index ) -> vector <1 x4 xindex > {
41
+ func.func @broadcast_scalar_with_bcast_and_splat (%arg1: index , %arg2: index ) -> vector <1 x4 xindex > {
25
42
%0 = vector.splat %arg1 : vector <1 x4 xindex >
26
43
%1 = vector.broadcast %arg2 : index to vector <1 x4 xindex >
27
44
%2 = arith.addi %0 , %1 : vector <1 x4 xindex >
28
45
return %2 : vector <1 x4 xindex >
29
46
}
30
47
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 <1 x[4 ]xindex > {
55
+ %0 = vector.splat %arg1 : vector <1 x[4 ]xindex >
56
+ %1 = vector.broadcast %arg2 : index to vector <1 x[4 ]xindex >
57
+ %2 = arith.addi %0 , %1 : vector <1 x[4 ]xindex >
58
+ return %2 : vector <1 x[4 ]xindex >
59
+ }
60
+
31
61
// -----
32
62
33
63
// CHECK-LABEL: func.func @broadcast_vector(
@@ -37,13 +67,27 @@ func.func @broadcast_scalar_with_bcast_and_splat( %arg1: index, %arg2: index) ->
37
67
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[ADDF]] : vector<4xf32> to vector<3x4xf32>
38
68
// CHECK: return %[[BCAST]] : vector<3x4xf32>
39
69
40
- func.func @broadcast_vector ( %arg1: vector <4 xf32 >, %arg2: vector <4 xf32 >) -> vector <3 x4 xf32 > {
70
+ func.func @broadcast_vector (%arg1: vector <4 xf32 >, %arg2: vector <4 xf32 >) -> vector <3 x4 xf32 > {
41
71
%arg1_bcast = vector.broadcast %arg1 : vector <4 xf32 > to vector <3 x4 xf32 >
42
72
%arg2_bcast = vector.broadcast %arg2 : vector <4 xf32 > to vector <3 x4 xf32 >
43
73
%2 = arith.addf %arg1_bcast , %arg2_bcast : vector <3 x4 xf32 >
44
74
return %2 : vector <3 x4 xf32 >
45
75
}
46
76
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>
83
+
84
+ func.func @broadcast_vector_scalable (%arg1: vector <[4 ]xf32 >, %arg2: vector <[4 ]xf32 >) -> vector <3 x[4 ]xf32 > {
85
+ %arg1_bcast = vector.broadcast %arg1 : vector <[4 ]xf32 > to vector <3 x[4 ]xf32 >
86
+ %arg2_bcast = vector.broadcast %arg2 : vector <[4 ]xf32 > to vector <3 x[4 ]xf32 >
87
+ %2 = arith.addf %arg1_bcast , %arg2_bcast : vector <3 x[4 ]xf32 >
88
+ return %2 : vector <3 x[4 ]xf32 >
89
+ }
90
+
47
91
// -----
48
92
49
93
// CHECK-LABEL: func.func @broadcast_scalar_and_vec(
@@ -53,13 +97,27 @@ func.func @broadcast_vector( %arg1: vector<4xf32>, %arg2: vector<4xf32>) -> vect
53
97
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[ARG2]] : vector<4xindex> to vector<1x4xindex>
54
98
// CHECK: %[[ADD:.*]] = arith.addi %[[SPLAT]], %[[BCAST]] : vector<1x4xindex>
55
99
// CHECK: return %[[ADD]] : vector<1x4xindex>
56
- func.func @broadcast_scalar_and_vec ( %arg1: index , %arg2: vector <4 xindex >) -> vector <1 x4 xindex > {
100
+ func.func @broadcast_scalar_and_vec (%arg1: index , %arg2: vector <4 xindex >) -> vector <1 x4 xindex > {
57
101
%0 = vector.splat %arg1 : vector <1 x4 xindex >
58
102
%1 = vector.broadcast %arg2 : vector <4 xindex > to vector <1 x4 xindex >
59
103
%2 = arith.addi %0 , %1 : vector <1 x4 xindex >
60
104
return %2 : vector <1 x4 xindex >
61
105
}
62
106
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 <1 x[4 ]xindex > {
115
+ %0 = vector.splat %arg1 : vector <1 x[4 ]xindex >
116
+ %1 = vector.broadcast %arg2 : vector <[4 ]xindex > to vector <1 x[4 ]xindex >
117
+ %2 = arith.addi %0 , %1 : vector <1 x[4 ]xindex >
118
+ return %2 : vector <1 x[4 ]xindex >
119
+ }
120
+
63
121
// -----
64
122
65
123
// CHECK-LABEL: func.func @broadcast_vector_and_scalar(
@@ -69,12 +127,25 @@ func.func @broadcast_scalar_and_vec( %arg1: index, %arg2: vector<4xindex>) -> ve
69
127
// CHECK: %[[ADD:.*]] = arith.addi %[[BCAST]], %[[ARG_1]] : vector<4xi32>
70
128
// CHECK: return %[[ADD]] : vector<4xi32>
71
129
72
- func.func @broadcast_vector_and_scalar ( %arg1: i32 , %arg2: vector <4 xi32 >) -> vector <4 xi32 > {
130
+ func.func @broadcast_vector_and_scalar (%arg1: i32 , %arg2: vector <4 xi32 >) -> vector <4 xi32 > {
73
131
%arg1_bcast = vector.broadcast %arg1 : i32 to vector <4 xi32 >
74
132
%2 = arith.addi %arg1_bcast , %arg2 : vector <4 xi32 >
75
133
return %2 : vector <4 xi32 >
76
134
}
77
135
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>
142
+
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 >
147
+ }
148
+
78
149
// -----
79
150
80
151
#matmat_accesses = [
@@ -87,40 +158,52 @@ func.func @broadcast_vector_and_scalar( %arg1: i32, %arg2: vector<4xi32>) -> vec
87
158
iterator_types = [" parallel" , " parallel" , " reduction" ]
88
159
}
89
160
90
- // CHECK-LABEL: func.func @broadcast_not_elementwise() -> vector<2x2xf32> {
91
- // CHECK-DAG: %[[VAL_0 :.*]] = arith.constant dense<1.000000e+00> : vector<2x2xf32>
92
- // CHECK-DAG: %[[VAL_1 :.*]] = arith.constant dense<2.000000e+00> : vector<2x2xf32>
93
- // CHECK-DAG: %[[VAL_2 :.*]] = arith.constant dense<3.000000e+00> : vector<2x2xf32>
94
- // CHECK: %[[VAL_3 :.*]] = vector.contract {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %[[VAL_0 ]], %[[VAL_1 ]], %[[VAL_2 ]] : vector<2x2xf32>, vector<2x2xf32> into vector<2x2xf32>
95
- func.func @broadcast_not_elementwise () -> vector <2 x2 xf32 > {
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 <2 x2 xf32 > {
96
167
%f1 = arith.constant 1.0 : f32
97
168
%f2 = arith.constant 2.0 : f32
98
169
%f3 = arith.constant 3.0 : f32
99
170
100
171
%A = vector.broadcast %f1 : f32 to vector <2 x2 xf32 >
101
172
%B = vector.broadcast %f2 : f32 to vector <2 x2 xf32 >
102
173
%C = vector.broadcast %f3 : f32 to vector <2 x2 xf32 >
103
- %mm1 = vector.contract #matmat_trait %A , %B , %C
174
+ %res = vector.contract #matmat_trait %A , %B , %C
104
175
: vector <2 x2 xf32 >, vector <2 x2 xf32 > into vector <2 x2 xf32 >
105
176
106
- return %mm1 : vector <2 x2 xf32 >
177
+ return %res : vector <2 x2 xf32 >
107
178
}
108
179
109
- // CHECK-LABEL: func.func @dont_sink_cmp(
180
+ // -----
181
+
182
+ // The source and the result for arith.cmp have different types - not supported
183
+
184
+ // CHECK-LABEL: func.func @negative_source_and_result_mismatch
110
185
// CHECK: %[[BROADCAST:.+]] = vector.broadcast
111
186
// CHECK: %[[RETURN:.+]] = arith.cmpf uno, %[[BROADCAST]], %[[BROADCAST]]
112
187
// CHECK: return %[[RETURN]]
113
- func.func @dont_sink_cmp (%arg0 : f32 , %arg1 : vector <1 xf32 >) -> vector <1 xi1 > {
188
+ func.func @negative_source_and_result_mismatch (%arg0 : f32 , %arg1 : vector <1 xf32 >) -> vector <1 xi1 > {
114
189
%0 = vector.broadcast %arg0 : f32 to vector <1 xf32 >
115
190
%1 = arith.cmpf uno , %0 , %0 : vector <1 xf32 >
116
191
return %1 : vector <1 xi1 >
117
192
}
118
193
119
- // CHECK-LABEL: func.func @dont_sink_fma(
194
+ // -----
195
+
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
199
+ //
200
+ // TODO: It should be possible to support this case
201
+
202
+ // CHECK-LABEL: func.func @negative_op_only_supports_vectors
120
203
// CHECK: %[[BROADCAST:.+]] = vector.broadcast
121
204
// CHECK: %[[RESULT:.+]] = vector.fma %[[BROADCAST]]
122
205
// CHECK: return %[[RESULT]]
123
- func.func @dont_sink_fma (%arg0 : f32 ) -> vector <1 xf32 > {
206
+ func.func @negative_op_only_supports_vectors (%arg0 : f32 ) -> vector <1 xf32 > {
124
207
%0 = vector.broadcast %arg0 : f32 to vector <1 xf32 >
125
208
%1 = vector.fma %0 , %0 , %0 : vector <1 xf32 >
126
209
return %1 : vector <1 xf32 >
0 commit comments