xref: /llvm-project/mlir/test/Dialect/Bufferization/Transforms/tensor-copy-insertion.mlir (revision 6f1e23b47d428d792866993ed26f4173d479d43d)
1// RUN: mlir-opt %s -test-tensor-copy-insertion -split-input-file | FileCheck %s
2// RUN: mlir-opt %s -test-tensor-copy-insertion="bufferize-function-boundaries" -split-input-file | FileCheck %s --check-prefix=CHECK-FUNC
3
4// CHECK-LABEL: func @read_after_write_conflict(
5//  CHECK-SAME:     %[[t:.*]]: tensor<?xf32>
6// CHECK-FUNC-LABEL: func @read_after_write_conflict(
7func.func @read_after_write_conflict(%t: tensor<?xf32>, %idx: index, %f: f32)
8  -> (tensor<?xf32>, tensor<?xf32>)
9{
10  // CHECK-FUNC: bufferization.alloc_tensor() copy(%{{.*}}) : tensor<?xf32>
11  // CHECK: %[[copy:.*]] =  bufferization.alloc_tensor() copy(%{{.*}}) : tensor<?xf32>
12  // CHECK: %[[insert:.*]] = tensor.insert %{{.*}} into %[[copy]]
13  %0 = tensor.insert %f into %t[%idx] : tensor<?xf32>
14  // CHECK: return %[[insert]], %[[t]]
15  return %0, %t : tensor<?xf32>, tensor<?xf32>
16}
17
18// -----
19
20// CHECK-LABEL: func @return_alloc_tensor
21// CHECK-FUNC-LABEL: func @return_alloc_tensor
22func.func @return_alloc_tensor() -> (tensor<5xf32>) {
23  // CHECK-FUNC: bufferization.alloc_tensor() : tensor<5xf32>
24  // CHECK: bufferization.alloc_tensor() : tensor<5xf32>
25  %0 = bufferization.alloc_tensor() : tensor<5xf32>
26  return %0 : tensor<5xf32>
27}
28
29// -----
30
31// CHECK-LABEL: func @do_not_copy_undefined_tensor
32func.func @do_not_copy_undefined_tensor(%f: f32, %idx: index)
33  -> (tensor<5xf32>, tensor<5xf32>)
34{
35  // The second alloc_tensor should not have a copy operand.
36  // CHECK: bufferization.alloc_tensor() : tensor<5xf32>
37  // CHECK: bufferization.alloc_tensor() : tensor<5xf32>
38  %0 = bufferization.alloc_tensor() : tensor<5xf32>
39  %1 = tensor.insert %f into %0[%idx] : tensor<5xf32>
40  return %0, %1 : tensor<5xf32>, tensor<5xf32>
41}
42
43// -----
44
45// CHECK-LABEL: func @do_not_copy_when_overwritten
46func.func @do_not_copy_when_overwritten(%t: tensor<5xf32>, %f: f32)
47  -> (tensor<5xf32>, tensor<5xf32>)
48{
49  // CHECK: %[[alloc:.*]] = bufferization.alloc_tensor() : tensor<5xf32>
50  // CHECK: linalg.generic {{.*}} outs(%[[alloc]] : tensor<5xf32>)
51  %r = linalg.generic {
52    indexing_maps = [affine_map<(d0) -> (d0)>],
53    iterator_types = ["parallel"]}
54    outs(%t : tensor<5xf32>) {
55      ^bb0(%arg0 : f32) :
56        linalg.yield %f : f32
57    } -> tensor<5xf32>
58  return %t, %r : tensor<5xf32>, tensor<5xf32>
59}
60
61// -----
62
63// CHECK-LABEL: func @do_not_copy_when_result_not_read
64func.func @do_not_copy_when_result_not_read(%t: tensor<5xf32>, %f: f32)
65  -> (tensor<3xf32>)
66{
67  %0 = tensor.extract_slice %t[0][3][1] : tensor<5xf32> to tensor<3xf32>
68  // CHECK: %[[alloc:.*]] = bufferization.alloc_tensor() : tensor<3xf32>
69  // CHECK: linalg.generic {{.*}} outs(%[[alloc]] : tensor<3xf32>)
70  %r = linalg.generic {
71    indexing_maps = [affine_map<(d0) -> (d0)>],
72    iterator_types = ["parallel"]}
73    outs(%0 : tensor<3xf32>) {
74      ^bb0(%arg0 : f32) :
75        linalg.yield %f : f32
76    } -> tensor<3xf32>
77  return %r : tensor<3xf32>
78}
79