1// RUN: mlir-opt %s -tensor-bufferize | FileCheck %s
2
3// CHECK-DAG: #[[$MAP0:.*]] = affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>
4// CHECK-DAG: #[[$MAP1:.*]] = affine_map<(d0, d1)[s0] -> (d0 * 20 + s0 + d1)>
5// CHECK-DAG: #[[$MAP2:.*]] = affine_map<(d0, d1, d2, d3)[s0] -> (d0 * 140 + d1 * 20 + d2 * 5 + d3 + s0)>
6// CHECK-DAG: #[[$MAP3:.*]] = affine_map<(d0) -> (d0 + 1)>
7// CHECK-DAG: #[[$MAP4:.*]] = affine_map<() -> (1)>
8// CHECK-DAG: #[[$MAP5:.*]] = affine_map<(d0, d1) -> (d0 * 2 + d1)>
9
10// CHECK-LABEL:   func @dim(
11// CHECK-SAME:              %[[TENSOR:.*]]: tensor<f32>,
12// CHECK-SAME:              %[[INDEX:.*]]: index) -> index {
13// CHECK:           %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<f32>
14// CHECK:           %[[EXTENT:.*]] = memref.dim %[[MEMREF]], %[[INDEX]] : memref<f32>
15// CHECK:           return %[[EXTENT]] : index
16func.func @dim(%arg0: tensor<f32>, %arg1: index) -> index {
17  %0 = tensor.dim %arg0, %arg1 : tensor<f32>
18  return %0 : index
19}
20
21// CHECK-LABEL: func @rank(
22// CHECK-SAME:    %[[TENSOR:.*]]: tensor<*xf32>) -> index {
23// CHECK:           %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]]
24// CHECK:           %[[EXTENT:.*]] = memref.rank %[[MEMREF]] : memref<*xf32>
25func.func @rank(%arg0: tensor<*xf32>) -> index {
26  %0 = tensor.rank %arg0 : tensor<*xf32>
27  return %0 : index
28}
29
30// CHECK-LABEL:   func @tensor.cast(
31// CHECK-SAME:                      %[[TENSOR:.*]]: tensor<?xindex>) -> tensor<2xindex> {
32// CHECK:           %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]]
33// CHECK:           %[[CASTED:.*]] = memref.cast %[[MEMREF]] : memref<?xindex> to memref<2xindex>
34// CHECK:           %[[RET:.*]] = bufferization.to_tensor %[[CASTED]]
35// CHECK:           return %[[RET]] : tensor<2xindex>
36func.func @tensor.cast(%arg0: tensor<?xindex>) -> tensor<2xindex> {
37  %0 = tensor.cast %arg0 : tensor<?xindex> to tensor<2xindex>
38  return %0 : tensor<2xindex>
39}
40
41// CHECK-LABEL:   func @tensor.cast_from_unranked(
42// CHECK-SAME:                                    %[[TENSOR:.*]]: tensor<*xf32>) -> tensor<2xf32> {
43// CHECK:           %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<*xf32>
44// CHECK:           %[[CASTED_MEMREF:.*]] = memref.cast %[[MEMREF]] : memref<*xf32> to memref<2xf32>
45// CHECK:           %[[RET:.*]] = bufferization.to_tensor %[[CASTED_MEMREF]] : memref<2xf32>
46// CHECK:           return %[[RET]] : tensor<2xf32>
47func.func @tensor.cast_from_unranked(%arg0: tensor<*xf32>) -> tensor<2xf32> {
48  %0 = tensor.cast %arg0 : tensor<*xf32> to tensor<2xf32>
49  return %0 : tensor<2xf32>
50}
51
52// CHECK-LABEL:   func @tensor.cast_to_unranked(
53// CHECK-SAME:                                  %[[TENSOR:.*]]: tensor<2xf32>) -> tensor<*xf32> {
54// CHECK:           %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<2xf32>
55// CHECK:           %[[CASTED_MEMREF:.*]] = memref.cast %[[MEMREF]] : memref<2xf32> to memref<*xf32>
56// CHECK:           %[[RET:.*]] = bufferization.to_tensor %[[CASTED_MEMREF]] : memref<*xf32>
57// CHECK:           return %[[RET]] : tensor<*xf32>
58func.func @tensor.cast_to_unranked(%arg0: tensor<2xf32>) -> tensor<*xf32> {
59  %0 = tensor.cast %arg0 : tensor<2xf32> to tensor<*xf32>
60  return %0 : tensor<*xf32>
61}
62
63// CHECK-LABEL:   func @tensor.extract(
64// CHECK-SAME:                  %[[TENSOR:.*]]: tensor<?xf32>,
65// CHECK-SAME:                  %[[IDX:.*]]: index) -> f32 {
66// CHECK:           %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<?xf32>
67// CHECK:           %[[RET:.*]] = memref.load %[[MEMREF]][%[[IDX]]] : memref<?xf32>
68// CHECK:           return %[[RET]] : f32
69// CHECK:         }
70func.func @tensor.extract(%arg0: tensor<?xf32>, %arg1: index) -> f32 {
71  %0 = tensor.extract %arg0[%arg1] : tensor<?xf32>
72  return %0 : f32
73}
74
75// CHECK-LABEL:   func @tensor.from_elements_no_elements() -> tensor<0xindex> {
76// CHECK:           %[[RET:.*]] = arith.constant dense<> : tensor<0xindex>
77// CHECK:           return %[[RET]] : tensor<0xindex>
78func.func @tensor.from_elements_no_elements() -> tensor<0xindex> {
79  %0 = tensor.from_elements : tensor<0xindex>
80  return %0 : tensor<0xindex>
81}
82
83// CHECK-LABEL:   func @tensor.from_elements_0d(
84// CHECK-SAME:        %[[ELEM0:.*]]: index) -> tensor<index> {
85// CHECK:           %[[MEMREF:.*]] = memref.alloc() {{.*}} : memref<index>
86// CHECK:           store %[[ELEM0]], %[[MEMREF]]
87// CHECK:           %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]]
88// CHECK:           return %[[RET]] : tensor<index>
89func.func @tensor.from_elements_0d(%arg0: index) -> tensor<index> {
90  %0 = tensor.from_elements %arg0 : tensor<index>
91  return %0 : tensor<index>
92}
93
94// CHECK-LABEL:   func @tensor.from_elements_1d(
95// CHECK-SAME:                               %[[ELEM0:.*]]: index,
96// CHECK-SAME:                               %[[ELEM1:.*]]: index) -> tensor<2xindex> {
97// CHECK-DAG:       %[[C0:.*]] = arith.constant 0 : index
98// CHECK-DAG:       %[[C1:.*]] = arith.constant 1 : index
99// CHECK-DAG:       %[[MEMREF:.*]] = memref.alloc() {{.*}} : memref<2xindex>
100// CHECK:           store %[[ELEM0]], %[[MEMREF]][%[[C0]]]
101// CHECK:           store %[[ELEM1]], %[[MEMREF]][%[[C1]]]
102// CHECK:           %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]]
103// CHECK:           return %[[RET]] : tensor<2xindex>
104func.func @tensor.from_elements_1d(%arg0: index, %arg1: index) -> tensor<2xindex> {
105  %0 = tensor.from_elements %arg0, %arg1 : tensor<2xindex>
106  return %0 : tensor<2xindex>
107}
108
109// CHECK-LABEL: func @tensor.from_elements_2d(
110// CHECK-SAME:      %[[ELEM0:.*]]: index, %[[ELEM1:.*]]: index)
111// CHECK-SAME:      -> tensor<3x2xindex> {
112// CHECK-DAG:     %[[C0:.*]] = arith.constant 0 : index
113// CHECK-DAG:     %[[C1:.*]] = arith.constant 1 : index
114// CHECK-DAG:     %[[C2:.*]] = arith.constant 2 : index
115// CHECK-DAG:     %[[MEMREF:.*]] = memref.alloc() {{.*}} : memref<3x2xindex>
116// CHECK:         store %[[ELEM0]], %[[MEMREF]][%[[C0]], %[[C0]]]
117// CHECK:         store %[[ELEM1]], %[[MEMREF]][%[[C0]], %[[C1]]]
118// CHECK:         store %[[ELEM0]], %[[MEMREF]][%[[C1]], %[[C0]]]
119// CHECK:         store %[[ELEM1]], %[[MEMREF]][%[[C1]], %[[C1]]]
120// CHECK:         store %[[ELEM0]], %[[MEMREF]][%[[C2]], %[[C0]]]
121// CHECK:         store %[[ELEM1]], %[[MEMREF]][%[[C2]], %[[C1]]]
122// CHECK:         %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]]
123// CHECK:         return %[[RET]] : tensor<3x2xindex>
124func.func @tensor.from_elements_2d(%arg0: index, %arg1: index) -> tensor<3x2xindex> {
125  %0 = tensor.from_elements %arg0, %arg1, %arg0, %arg1, %arg0, %arg1
126         : tensor<3x2xindex>
127  return %0 : tensor<3x2xindex>
128}
129
130// CHECK-LABEL: func @tensor.from_elements_3d(
131//  CHECK-SAME:     %[[F0:.*]]: f32
132
133// CHECK-DAG: %[[F1:.*]] = arith.constant 1.0{{0+}}e+00
134// CHECK-DAG: %[[F2:.*]] = arith.constant 2.0
135// CHECK-DAG: %[[F3:.*]] = arith.constant 3.0
136// CHECK-DAG: %[[F4:.*]] = arith.constant 4.0
137// CHECK-DAG: %[[F5:.*]] = arith.constant 5.0
138// CHECK-DAG: %[[F6:.*]] = arith.constant 6.0
139// CHECK-DAG: %[[F7:.*]] = arith.constant 7.0
140// CHECK-DAG: %[[F8:.*]] = arith.constant 8.0
141// CHECK-DAG: %[[F9:.*]] = arith.constant 9.0
142// CHECK-DAG: %[[F10:.*]] = arith.constant 1.0{{0+}}e+01
143// CHECK-DAG: %[[F11:.*]] = arith.constant 1.1{{0+}}e+01
144
145// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
146// CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index
147// CHECK-DAG: %[[C2:.*]] = arith.constant 2 : index
148
149// CHECK-DAG: %[[MEMREF:.*]] = memref.alloc() {{.*}} : memref<3x2x2xf32>
150
151// CHECK: store %[[F0]], %[[MEMREF]][%[[C0]], %[[C0]], %[[C0]]]
152// CHECK: store %[[F1]], %[[MEMREF]][%[[C0]], %[[C0]], %[[C1]]]
153// CHECK: store %[[F2]], %[[MEMREF]][%[[C0]], %[[C1]], %[[C0]]]
154// CHECK: store %[[F3]], %[[MEMREF]][%[[C0]], %[[C1]], %[[C1]]]
155// CHECK: store %[[F4]], %[[MEMREF]][%[[C1]], %[[C0]], %[[C0]]]
156// CHECK: store %[[F5]], %[[MEMREF]][%[[C1]], %[[C0]], %[[C1]]]
157// CHECK: store %[[F6]], %[[MEMREF]][%[[C1]], %[[C1]], %[[C0]]]
158// CHECK: store %[[F7]], %[[MEMREF]][%[[C1]], %[[C1]], %[[C1]]]
159// CHECK: store %[[F8]], %[[MEMREF]][%[[C2]], %[[C0]], %[[C0]]]
160// CHECK: store %[[F9]], %[[MEMREF]][%[[C2]], %[[C0]], %[[C1]]]
161// CHECK: store %[[F10]], %[[MEMREF]][%[[C2]], %[[C1]], %[[C0]]]
162// CHECK: store %[[F11]], %[[MEMREF]][%[[C2]], %[[C1]], %[[C1]]]
163
164// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]]
165// CHECK: return %[[RET]] : tensor<3x2x2xf32>
166func.func @tensor.from_elements_3d(%f0 : f32) -> tensor<3x2x2xf32> {
167  %f1 = arith.constant 1.0 : f32
168  %f2 = arith.constant 2.0 : f32
169  %f3 = arith.constant 3.0 : f32
170  %f4 = arith.constant 4.0 : f32
171  %f5 = arith.constant 5.0 : f32
172  %f6 = arith.constant 6.0 : f32
173  %f7 = arith.constant 7.0 : f32
174  %f8 = arith.constant 8.0 : f32
175  %f9 = arith.constant 9.0 : f32
176  %f10 = arith.constant 10.0 : f32
177  %f11 = arith.constant 11.0 : f32
178  %0 = tensor.from_elements %f0,%f1,%f2,%f3,%f4,%f5,%f6,%f7,%f8,%f9,%f10,%f11
179         : tensor<3x2x2xf32>
180  return %0 : tensor<3x2x2xf32>
181}
182
183// CHECK-LABEL:   func @tensor.generate(
184// CHECK-SAME:                                       %[[ARG:.*]]: tensor<*xf32>,
185// CHECK-SAME:                                       %[[DYNAMIC_EXTENT:.*]]: index) -> tensor<?xindex> {
186// CHECK-DAG:       %[[C0:.*]] = arith.constant 0 : index
187// CHECK-DAG:       %[[C1:.*]] = arith.constant 1 : index
188// CHECK:           %[[CASTED:.*]] = bufferization.to_memref %[[ARG]] : memref<*xf32>
189// CHECK:           %[[MEMREF:.*]] = memref.alloc(%[[DYNAMIC_EXTENT]]) {{.*}} : memref<?xindex>
190// CHECK:           scf.parallel (%[[I:.*]]) = (%[[C0]]) to (%[[DYNAMIC_EXTENT]]) step (%[[C1]]) {
191// CHECK:             %[[ELEM:.*]] = memref.dim %[[CASTED]], %[[I]] : memref<*xf32>
192// CHECK:             store %[[ELEM]], %[[MEMREF]][%[[I]]] : memref<?xindex>
193// CHECK:             scf.yield
194// CHECK:           }
195// CHECK:           %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]] : memref<?xindex>
196// CHECK:           return %[[RET]] : tensor<?xindex>
197// CHECK:         }
198func.func @tensor.generate(%arg: tensor<*xf32>, %dynamic_extent: index) -> tensor<?xindex> {
199  %result = tensor.generate %dynamic_extent {
200  ^bb0(%i : index):
201    %elem = tensor.dim %arg, %i : tensor<*xf32>
202    tensor.yield %elem : index
203  } : tensor<?xindex>
204  return %result : tensor<?xindex>
205}
206
207// Additional test that checks the logic for intermixed static and dynamic
208// extents.
209//
210// CHECK-LABEL:   func @tensor.generate_static_and_dynamic(
211// CHECK-SAME:        %[[DYNAMIC_EXTENT:.*]]: index) -> tensor<16x?xindex> {
212// CHECK-DAG:       %[[C0:.*]] = arith.constant 0 : index
213// CHECK-DAG:       %[[C1:.*]] = arith.constant 1 : index
214// CHECK-DAG:       %[[C16:.*]] = arith.constant 16 : index
215// CHECK:           %[[MEMREF:.*]] = memref.alloc(%[[DYNAMIC_EXTENT]]) {{.*}} : memref<16x?xindex>
216// CHECK:           scf.parallel (%[[I:.*]], %[[J:.*]]) = (%[[C0]], %[[C0]]) to (%[[C16]], %[[DYNAMIC_EXTENT]]) step (%[[C1]], %[[C1]]) {
217// CHECK:             %[[VAL_7:.*]] = arith.addi %[[I]], %[[J]] : index
218// CHECK:             store %[[VAL_7]], %[[MEMREF]][%[[I]], %[[J]]] : memref<16x?xindex>
219// CHECK:             scf.yield
220// CHECK:           }
221// CHECK:           %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]] : memref<16x?xindex>
222// CHECK:           return %[[RET]] : tensor<16x?xindex>
223// CHECK:         }
224func.func @tensor.generate_static_and_dynamic(%arg0: index) -> tensor<16x?xindex> {
225  %result = tensor.generate %arg0 {
226  ^bb0(%i: index, %j: index):
227    %sum = arith.addi %i, %j : index
228    tensor.yield %sum : index
229  } : tensor<16x?xindex>
230  return %result : tensor<16x?xindex>
231}
232
233// CHECK-LABEL: func @tensor.generate_unknown_ops_in_body
234func.func @tensor.generate_unknown_ops_in_body(%arg0: index) -> tensor<?xindex> {
235  // CHECK-NOT: tensor.generate
236  %tensor = tensor.generate %arg0 {
237  ^bb0(%iv: index):
238    // CHECK: test.source
239    %0 = "test.source"() : () -> index
240    tensor.yield %0 : index
241  } : tensor<?xindex>
242  return %tensor : tensor<?xindex>
243}
244
245// CHECK-LABEL: func @tensor.extract_slice(
246//  CHECK-SAME:     %[[t1:.*]]: tensor<?x?xf32>, %[[idx1:.*]]: index, %[[idx2:.*]]: index
247func.func @tensor.extract_slice(
248    %t1: tensor<?x?xf32>, %idx1: index, %idx2: index) -> tensor<?x10xf32> {
249  // CHECK: %[[m:.*]] = bufferization.to_memref %[[t1]] : memref<?x?xf32>
250  // CHECK: %[[r:.*]] = memref.subview %[[m]][5, %[[idx2]]] [%[[idx1]], 10] [1, 1] : memref<?x?xf32> to memref<?x10xf32, #[[$MAP0]]>
251  %0 = tensor.extract_slice %t1[5, %idx2][%idx1, 10][1, 1]
252      : tensor<?x?xf32> to tensor<?x10xf32>
253  // CHECK: %[[r_tensor:.*]] = bufferization.to_tensor %[[r]]
254  // CHECK: return %[[r_tensor]]
255  return %0 : tensor<?x10xf32>
256}
257
258// CHECK-LABEL: func @tensor.extract_slice_rank_reducing(
259//  CHECK-SAME:     %[[t1:.*]]: tensor<?x10x?xf32>, %[[idx1:.*]]: index,
260//  CHECK-SAME:     %[[idx2:.*]]: index
261func.func @tensor.extract_slice_rank_reducing(
262    %t1: tensor<?x10x?xf32>, %idx1: index, %idx2: index) -> tensor<?x15xf32> {
263  // CHECK: %[[m1:.*]] = bufferization.to_memref %[[t1]] : memref<?x10x?xf32>
264  // CHECK: %[[r:.*]] = memref.subview %[[m1]][5, %[[idx1]], 10] [%[[idx2]], 1, 15] [1, 1, 1] : memref<?x10x?xf32> to memref<?x15xf32, #[[$MAP0]]>
265  %0 = tensor.extract_slice %t1[5, %idx1, 10][%idx2, 1, 15][1, 1, 1]
266      : tensor<?x10x?xf32> to tensor<?x15xf32>
267  // CHECK: %[[r_tensor:.*]] = bufferization.to_tensor %[[r]]
268  // CHECK: return %[[r_tensor]]
269  return %0 : tensor<?x15xf32>
270}
271
272// CHECK-LABEL: func @tensor.insert_slice(
273//  CHECK-SAME:     %[[t1:.*]]: tensor<?x?xf32>, %[[t2:.*]]: tensor<?x10xf32>,
274//  CHECK-SAME:     %[[idx1:.*]]: index, %[[idx2:.*]]: index
275func.func @tensor.insert_slice(%t1: tensor<?x?xf32>, %t2: tensor<?x10xf32>,
276                          %idx1: index, %idx2: index) -> tensor<?x?xf32> {
277  // CHECK-DAG: %[[c0:.*]] = arith.constant 0 : index
278  // CHECK-DAG: %[[c1:.*]] = arith.constant 1 : index
279  // CHECK-DAG: %[[m1:.*]] = bufferization.to_memref %[[t1]] : memref<?x?xf32>
280  // CHECK-DAG: %[[m2:.*]] = bufferization.to_memref %[[t2]] : memref<?x10xf32>
281  //     CHECK: %[[dim0:.*]] = memref.dim %[[m1]], %[[c0]]
282  //     CHECK: %[[dim1:.*]] = memref.dim %[[m1]], %[[c1]]
283  //     CHECK: %[[alloc:.*]] = memref.alloc(%[[dim0]], %[[dim1]])
284  //     CHECK: memref.copy %[[m1]], %[[alloc]]
285  //     CHECK: %[[subview:.*]] = memref.subview %[[alloc]][%[[idx1]], 5] [%[[idx2]], 10] [1, 1]
286  //     CHECK: memref.copy %[[m2]], %[[subview]]
287  %0 = tensor.insert_slice %t2 into %t1[%idx1, 5][%idx2, 10][1, 1]
288      : tensor<?x10xf32> into tensor<?x?xf32>
289
290  //     CHECK: %[[r:.*]] = bufferization.to_tensor %[[alloc]]
291  //     CHECK: return %[[r]]
292  return %0 : tensor<?x?xf32>
293}
294
295// CHECK-LABEL: func @tensor.insert(
296//  CHECK-SAME:     %[[t1:.*]]: tensor<5xf32>, %[[idx1:.*]]: index,
297//  CHECK-SAME:     %[[f:.*]]: f32
298func.func @tensor.insert(%t1: tensor<5xf32>, %idx1: index, %f: f32) -> tensor<5xf32> {
299  // CHECK-DAG: %[[alloc:.*]] = memref.alloc() {{.*}} : memref<5xf32>
300  // CHECK-DAG: %[[m1:.*]] = bufferization.to_memref %[[t1]] : memref<5xf32>
301  // CHECK: memref.copy %[[m1]], %[[alloc]]
302  // CHECK: memref.store %[[f]], %[[alloc]][%[[idx1]]]
303  %0 = tensor.insert %f into %t1[%idx1] : tensor<5xf32>
304
305  // CHECK: %[[r:.*]] = bufferization.to_tensor %[[alloc]]
306  // CHECK: return %[[r]]
307  return %0 : tensor<5xf32>
308}
309
310// CHECK-LABEL: func @tensor.expand_shape(
311//  CHECK-SAME:     %[[t1:.*]]: tensor<?x10xf32>
312func.func @tensor.expand_shape(%t1: tensor<?x10xf32>) -> tensor<2x?x10xf32> {
313  // CHECK: %[[m1:.*]] = bufferization.to_memref %[[t1]] : memref<?x10xf32>
314  // CHECK: %[[expanded:.*]] = memref.expand_shape %[[m1]] [
315  // CHECK-SAME: [0, 1], [2]] : memref<?x10xf32> into memref<2x?x10xf32>
316  %0 = tensor.expand_shape %t1 [[0, 1], [2]]
317      : tensor<?x10xf32> into tensor<2x?x10xf32>
318
319  // CHECK: %[[r:.*]] = bufferization.to_tensor %[[expanded]]
320  // CHECK: return %[[r]]
321  return %0 : tensor<2x?x10xf32>
322}
323
324// CHECK-LABEL: func @tensor.expand_shape_of_slice(
325//  CHECK-SAME:     %[[t1:.*]]: tensor<?x20xf32>
326func.func @tensor.expand_shape_of_slice(
327    %t1: tensor<?x20xf32>, %o1: index, %s1: index) -> tensor<?x7x2x5xf32> {
328  // CHECK: %[[m1:.*]] = bufferization.to_memref %[[t1]] : memref<?x20xf32>
329  // CHECK: %[[subview:.*]] = memref.subview %[[m1]][%{{.*}}, 5] [%{{.*}}, 10] [1, 1] : memref<?x20xf32> to memref<?x10xf32, #[[$MAP1]]>
330  %0 = tensor.extract_slice %t1[%o1, 5][%s1, 10][1, 1] :
331      tensor<?x20xf32> to tensor<?x10xf32>
332  // CHECK: %[[expanded:.*]] = memref.expand_shape %[[subview]] [
333  // CHECK-SAME: [0, 1], [2, 3]] : memref<?x10xf32, #[[$MAP1]]> into memref<?x7x2x5xf32, #[[$MAP2]]>
334  %1 = tensor.expand_shape %0 [[0, 1], [2, 3]] :
335      tensor<?x10xf32> into tensor<?x7x2x5xf32>
336  // CHECK: %[[r:.*]] = bufferization.to_tensor %[[expanded]]
337  // CHECK: return %[[r]]
338  return %1 : tensor<?x7x2x5xf32>
339}
340
341// CHECK-LABEL: func @tensor.expand_shape_of_slice2(
342//  CHECK-SAME:     %[[t1:.*]]: tensor<1x2xf32>
343func.func @tensor.expand_shape_of_slice2(%t1: tensor<1x2xf32>) -> tensor<1xf32> {
344  // CHECK: memref.subview {{.*}} : memref<1x2xf32> to memref<1x1xf32, #[[$MAP5]]>
345  %0 = tensor.extract_slice %t1[0, 0][1, 1][1, 1] : tensor<1x2xf32> to tensor<1x1xf32>
346  // CHECK: memref.collapse_shape %{{.*}} [
347  // CHECK-SAME: [0, 1]] : memref<1x1xf32, #[[$MAP5]]> into memref<1xf32>
348  %1 = tensor.collapse_shape %0 [[0, 1]] : tensor<1x1xf32> into tensor<1xf32>
349  return %1 : tensor<1xf32>
350}
351
352// CHECK-LABEL: func @tensor.collapse_shape(
353//  CHECK-SAME:     %[[t1:.*]]: tensor<2x?x?xf32>
354func.func @tensor.collapse_shape(%t1: tensor<2x?x?xf32>) -> tensor<?x?xf32> {
355  // CHECK: %[[m1:.*]] = bufferization.to_memref %[[t1]] : memref<2x?x?xf32>
356  // CHECK: %[[collapsed:.*]] = memref.collapse_shape %[[m1]] [
357  // CHECK-SAME: [0, 1], [2]] : memref<2x?x?xf32> into memref<?x?xf32>
358  %0 = tensor.collapse_shape %t1 [[0, 1], [2]]
359      : tensor<2x?x?xf32> into tensor<?x?xf32>
360
361  // CHECK: %[[r:.*]] = bufferization.to_tensor %[[collapsed]]
362  // CHECK: return %[[r]]
363  return %0 : tensor<?x?xf32>
364}
365
366// CHECK-LABEL: func @tensor.collapse_shape_to_scalar(
367//  CHECK-SAME:     %[[t1:.*]]: tensor<1x1x1xf32>
368func.func @tensor.collapse_shape_to_scalar(%t1: tensor<1x1x1xf32>) -> tensor<f32> {
369  // CHECK: %[[m1:.*]] = bufferization.to_memref %[[t1]] : memref<1x1x1xf32>
370  // CHECK: %[[collapsed:.*]] = memref.collapse_shape %[[m1]] [] : memref<1x1x1xf32> into memref<f32>
371  %0 = tensor.collapse_shape %t1 []
372      : tensor<1x1x1xf32> into tensor<f32>
373
374  // CHECK: %[[r:.*]] = bufferization.to_tensor %[[collapsed]]
375  // CHECK: return %[[r]]
376  return %0 : tensor<f32>
377}
378
379// CHECK-LABEL: func @tensor.collapse_shape_of_slice(
380func.func @tensor.collapse_shape_of_slice(%arg0: tensor<2xi32>) -> tensor<i32> {
381  // CHECK: memref.subview %{{.*}}[1] [1] [1] : memref<2xi32> to memref<1xi32, #[[$MAP3]]>
382  %0 = tensor.extract_slice %arg0[1] [1] [1] : tensor<2xi32> to tensor<1xi32>
383  // CHECK: memref.collapse_shape %{{.*}} [] : memref<1xi32, #[[$MAP3]]> into memref<i32, #[[$MAP4]]>
384  %1 = tensor.collapse_shape %0 [] : tensor<1xi32> into tensor<i32>
385  return %1 : tensor<i32>
386}
387
388// CHECK-LABEL: func @tensor.collapse_shape_of_slice2(
389func.func @tensor.collapse_shape_of_slice2(
390    %arg0: tensor<?x?x?x?xi64>, %o1: index, %o2: index, %o3: index, %o4: index)
391    -> tensor<87x63648xi64> {
392  // CHECK: %[[subview:.*]] = memref.subview %{{.*}} : memref<?x?x?x?xi64> to memref<87x78x68x12xi64, #{{.*}}>
393  %0 = tensor.extract_slice %arg0[%o1, %o2, %o3, %o4] [87, 78, 68, 12] [1, 1, 1, 1] : tensor<?x?x?x?xi64> to tensor<87x78x68x12xi64>
394
395  // This memref may not be collapsible, so the buffer must be copied to get rid
396  // of the layout map.
397  // CHECK: %[[alloc:.*]] = memref.alloc() {{.*}} : memref<87x78x68x12xi64>
398  // CHECK: memref.copy %[[subview]], %[[alloc]]
399  // CHECK: memref.collapse_shape %[[alloc]] [
400  // CHECK-SAME: [0], [1, 2, 3]] : memref<87x78x68x12xi64> into memref<87x63648xi64>
401  %1 = tensor.collapse_shape %0 [[0], [1, 2, 3]] : tensor<87x78x68x12xi64> into tensor<87x63648xi64>
402  return %1 : tensor<87x63648xi64>
403}
404