File: tensor-copy-insertion.mlir

package info (click to toggle)
swiftlang 6.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,604 kB
  • sloc: cpp: 9,901,740; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (78 lines) | stat: -rw-r--r-- 2,898 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// RUN: mlir-opt %s -test-tensor-copy-insertion -split-input-file | FileCheck %s
// RUN: mlir-opt %s -test-tensor-copy-insertion="bufferize-function-boundaries" -split-input-file | FileCheck %s --check-prefix=CHECK-FUNC

// CHECK-LABEL: func @read_after_write_conflict(
//  CHECK-SAME:     %[[t:.*]]: tensor<?xf32>
// CHECK-FUNC-LABEL: func @read_after_write_conflict(
func.func @read_after_write_conflict(%t: tensor<?xf32>, %idx: index, %f: f32)
  -> (tensor<?xf32>, tensor<?xf32>)
{
  // CHECK-FUNC: bufferization.alloc_tensor() copy(%{{.*}}) : tensor<?xf32>
  // CHECK: %[[copy:.*]] =  bufferization.alloc_tensor() copy(%{{.*}}) : tensor<?xf32>
  // CHECK: %[[insert:.*]] = tensor.insert %{{.*}} into %[[copy]]
  %0 = tensor.insert %f into %t[%idx] : tensor<?xf32>
  // CHECK: return %[[insert]], %[[t]]
  return %0, %t : tensor<?xf32>, tensor<?xf32>
}

// -----

// CHECK-LABEL: func @return_alloc_tensor
// CHECK-FUNC-LABEL: func @return_alloc_tensor
func.func @return_alloc_tensor() -> (tensor<5xf32>) {
  // CHECK-FUNC: bufferization.alloc_tensor() : tensor<5xf32>
  // CHECK: bufferization.alloc_tensor() : tensor<5xf32>
  %0 = bufferization.alloc_tensor() : tensor<5xf32>
  return %0 : tensor<5xf32>
}

// -----

// CHECK-LABEL: func @do_not_copy_undefined_tensor
func.func @do_not_copy_undefined_tensor(%f: f32, %idx: index)
  -> (tensor<5xf32>, tensor<5xf32>)
{
  // The second alloc_tensor should not have a copy operand.
  // CHECK: bufferization.alloc_tensor() : tensor<5xf32>
  // CHECK: bufferization.alloc_tensor() : tensor<5xf32>
  %0 = bufferization.alloc_tensor() : tensor<5xf32>
  %1 = tensor.insert %f into %0[%idx] : tensor<5xf32>
  return %0, %1 : tensor<5xf32>, tensor<5xf32>
}

// -----

// CHECK-LABEL: func @do_not_copy_when_overwritten
func.func @do_not_copy_when_overwritten(%t: tensor<5xf32>, %f: f32)
  -> (tensor<5xf32>, tensor<5xf32>)
{
  // CHECK: %[[alloc:.*]] = bufferization.alloc_tensor() : tensor<5xf32>
  // CHECK: linalg.generic {{.*}} outs(%[[alloc]] : tensor<5xf32>)
  %r = linalg.generic {
    indexing_maps = [affine_map<(d0) -> (d0)>],
    iterator_types = ["parallel"]}
    outs(%t : tensor<5xf32>) {
      ^bb0(%arg0 : f32) :
        linalg.yield %f : f32
    } -> tensor<5xf32>
  return %t, %r : tensor<5xf32>, tensor<5xf32>
}

// -----

// CHECK-LABEL: func @do_not_copy_when_result_not_read
func.func @do_not_copy_when_result_not_read(%t: tensor<5xf32>, %f: f32)
  -> (tensor<3xf32>)
{
  %0 = tensor.extract_slice %t[0][3][1] : tensor<5xf32> to tensor<3xf32>
  // CHECK: %[[alloc:.*]] = bufferization.alloc_tensor() : tensor<3xf32>
  // CHECK: linalg.generic {{.*}} outs(%[[alloc]] : tensor<3xf32>)
  %r = linalg.generic {
    indexing_maps = [affine_map<(d0) -> (d0)>],
    iterator_types = ["parallel"]}
    outs(%0 : tensor<3xf32>) {
      ^bb0(%arg0 : f32) :
        linalg.yield %f : f32
    } -> tensor<3xf32>
  return %r : tensor<3xf32>
}