File: scalar-vector-transfer-to-memref.mlir

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (155 lines) | stat: -rw-r--r-- 7,021 bytes parent folder | download | duplicates (2)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// RUN: mlir-opt %s -test-scalar-vector-transfer-lowering -split-input-file | FileCheck %s
// RUN: mlir-opt %s -test-scalar-vector-transfer-lowering=allow-multiple-uses -split-input-file | FileCheck %s --check-prefix=MULTIUSE

// CHECK-LABEL: func @transfer_read_0d(
//  CHECK-SAME:     %[[m:.*]]: memref<?x?x?xf32>, %[[idx:.*]]: index
//       CHECK:   %[[r:.*]] = memref.load %[[m]][%[[idx]], %[[idx]], %[[idx]]]
//       CHECK:   return %[[r]]
func.func @transfer_read_0d(%m: memref<?x?x?xf32>, %idx: index) -> f32 {
  %cst = arith.constant 0.0 : f32
  %0 = vector.transfer_read %m[%idx, %idx, %idx], %cst : memref<?x?x?xf32>, vector<f32>
  %1 = vector.extractelement %0[] : vector<f32>
  return %1 : f32
}

// -----

//       CHECK: #[[$map:.*]] = affine_map<()[s0, s1] -> (s0 + s1)>
// CHECK-LABEL: func @transfer_read_1d(
//  CHECK-SAME:     %[[m:.*]]: memref<?x?x?xf32>, %[[idx:.*]]: index, %[[idx2:.*]]: index
//       CHECK:   %[[added:.*]] = affine.apply #[[$map]]()[%[[idx]], %[[idx2]]]
//       CHECK:   %[[r:.*]] = memref.load %[[m]][%[[idx]], %[[idx]], %[[added]]]
//       CHECK:   return %[[r]]
func.func @transfer_read_1d(%m: memref<?x?x?xf32>, %idx: index, %idx2: index) -> f32 {
  %cst = arith.constant 0.0 : f32
  %c0 = arith.constant 0 : index
  %0 = vector.transfer_read %m[%idx, %idx, %idx], %cst {in_bounds = [true]} : memref<?x?x?xf32>, vector<5xf32>
  %1 = vector.extractelement %0[%idx2 : index] : vector<5xf32>
  return %1 : f32
}

// -----

// CHECK-LABEL: func @tensor_transfer_read_0d(
//  CHECK-SAME:     %[[t:.*]]: tensor<?x?x?xf32>, %[[idx:.*]]: index
//       CHECK:   %[[r:.*]] = tensor.extract %[[t]][%[[idx]], %[[idx]], %[[idx]]]
//       CHECK:   return %[[r]]
func.func @tensor_transfer_read_0d(%t: tensor<?x?x?xf32>, %idx: index) -> f32 {
  %cst = arith.constant 0.0 : f32
  %0 = vector.transfer_read %t[%idx, %idx, %idx], %cst : tensor<?x?x?xf32>, vector<f32>
  %1 = vector.extractelement %0[] : vector<f32>
  return %1 : f32
}

// -----

// CHECK-LABEL: func @transfer_write_0d(
//  CHECK-SAME:     %[[m:.*]]: memref<?x?x?xf32>, %[[idx:.*]]: index, %[[f:.*]]: f32
//       CHECK:   %[[bc:.*]] = vector.broadcast %[[f]] : f32 to vector<f32>
//       CHECK:   %[[extract:.*]] = vector.extractelement %[[bc]][] : vector<f32>
//       CHECK:   memref.store %[[extract]], %[[m]][%[[idx]], %[[idx]], %[[idx]]]
func.func @transfer_write_0d(%m: memref<?x?x?xf32>, %idx: index, %f: f32) {
  %0 = vector.broadcast %f : f32 to vector<f32>
  vector.transfer_write %0, %m[%idx, %idx, %idx] : vector<f32>, memref<?x?x?xf32>
  return
}

// -----

// CHECK-LABEL: func @transfer_write_1d(
//  CHECK-SAME:     %[[m:.*]]: memref<?x?x?xf32>, %[[idx:.*]]: index, %[[f:.*]]: f32
//       CHECK:   memref.store %[[f]], %[[m]][%[[idx]], %[[idx]], %[[idx]]]
func.func @transfer_write_1d(%m: memref<?x?x?xf32>, %idx: index, %f: f32) {
  %0 = vector.broadcast %f : f32 to vector<1xf32>
  vector.transfer_write %0, %m[%idx, %idx, %idx] : vector<1xf32>, memref<?x?x?xf32>
  return
}

// -----

// CHECK-LABEL: func @tensor_transfer_write_0d(
//  CHECK-SAME:     %[[t:.*]]: tensor<?x?x?xf32>, %[[idx:.*]]: index, %[[f:.*]]: f32
//       CHECK:   %[[bc:.*]] = vector.broadcast %[[f]] : f32 to vector<f32>
//       CHECK:   %[[extract:.*]] = vector.extractelement %[[bc]][] : vector<f32>
//       CHECK:   %[[r:.*]] = tensor.insert %[[extract]] into %[[t]][%[[idx]], %[[idx]], %[[idx]]]
//       CHECK:   return %[[r]]
func.func @tensor_transfer_write_0d(%t: tensor<?x?x?xf32>, %idx: index, %f: f32) -> tensor<?x?x?xf32> {
  %0 = vector.broadcast %f : f32 to vector<f32>
  %1 = vector.transfer_write %0, %t[%idx, %idx, %idx] : vector<f32>, tensor<?x?x?xf32>
  return %1 : tensor<?x?x?xf32>
}

// -----

//       CHECK: #[[$map:.*]] = affine_map<()[s0] -> (s0 + 8)>
//       CHECK: #[[$map1:.*]] = affine_map<()[s0] -> (s0 + 1)>
// CHECK-LABEL: func @transfer_read_2d_extract(
//  CHECK-SAME:     %[[m:.*]]: memref<?x?x?x?xf32>, %[[idx:.*]]: index, %[[idx2:.*]]: index
//       CHECK:   %[[added:.*]] = affine.apply #[[$map]]()[%[[idx]]]
//       CHECK:   %[[added1:.*]] = affine.apply #[[$map1]]()[%[[idx]]]
//       CHECK:   %[[r:.*]] = memref.load %[[m]][%[[idx]], %[[idx]], %[[added]], %[[added1]]]
//       CHECK:   return %[[r]]
func.func @transfer_read_2d_extract(%m: memref<?x?x?x?xf32>, %idx: index, %idx2: index) -> f32 {
  %cst = arith.constant 0.0 : f32
  %c0 = arith.constant 0 : index
  %0 = vector.transfer_read %m[%idx, %idx, %idx, %idx], %cst {in_bounds = [true, true]} : memref<?x?x?x?xf32>, vector<10x5xf32>
  %1 = vector.extract %0[8, 1] : vector<10x5xf32>
  return %1 : f32
}

// -----

// CHECK-LABEL: func @transfer_write_arith_constant(
//  CHECK-SAME:     %[[m:.*]]: memref<?x?x?xf32>, %[[idx:.*]]: index
//       CHECK:   %[[cst:.*]] = arith.constant dense<5.000000e+00> : vector<1x1xf32>
//       CHECK:   %[[extract:.*]] = vector.extract %[[cst]][0, 0] : vector<1x1xf32>
//       CHECK:   memref.store %[[extract]], %[[m]][%[[idx]], %[[idx]], %[[idx]]]
func.func @transfer_write_arith_constant(%m: memref<?x?x?xf32>, %idx: index) {
  %cst = arith.constant dense<5.000000e+00> : vector<1x1xf32>
  vector.transfer_write %cst, %m[%idx, %idx, %idx] : vector<1x1xf32>, memref<?x?x?xf32>
  return
}

// -----

// CHECK-LABEL: func @transfer_read_multi_use(
//  CHECK-SAME:   %[[m:.*]]: memref<?xf32>, %[[idx:.*]]: index
//   CHECK-NOT:   memref.load
//       CHECK:   %[[r:.*]] = vector.transfer_read %[[m]][%[[idx]]]
//       CHECK:   %[[e0:.*]] = vector.extract %[[r]][0]
//       CHECK:   %[[e1:.*]] = vector.extract %[[r]][1]
//       CHECK:   return %[[e0]], %[[e1]]

// MULTIUSE-LABEL: func @transfer_read_multi_use(
//  MULTIUSE-SAME:   %[[m:.*]]: memref<?xf32>, %[[idx0:.*]]: index
//   MULTIUSE-NOT:   vector.transfer_read
//       MULTIUSE:   %[[r0:.*]] = memref.load %[[m]][%[[idx0]]
//       MULTIUSE:   %[[idx1:.*]] = affine.apply
//       MULTIUSE:   %[[r1:.*]] = memref.load %[[m]][%[[idx1]]
//       MULTIUSE:   return %[[r0]], %[[r1]]

func.func @transfer_read_multi_use(%m: memref<?xf32>, %idx: index) -> (f32, f32) {
  %cst = arith.constant 0.0 : f32
  %0 = vector.transfer_read %m[%idx], %cst {in_bounds = [true]} : memref<?xf32>, vector<16xf32>
  %1 = vector.extract %0[0] : vector<16xf32>
  %2 = vector.extract %0[1] : vector<16xf32>
  return %1, %2 : f32, f32
}

// -----

// Check that patterns don't trigger for an sub-vector (not scalar) extraction.
// CHECK-LABEL: func @subvector_extract(
//  CHECK-SAME:   %[[m:.*]]: memref<?x?xf32>, %[[idx:.*]]: index
//   CHECK-NOT:   memref.load
//       CHECK:   %[[r:.*]] = vector.transfer_read %[[m]][%[[idx]], %[[idx]]]
//       CHECK:   %[[e0:.*]] = vector.extract %[[r]][0]
//       CHECK:   return %[[e0]]

func.func @subvector_extract(%m: memref<?x?xf32>, %idx: index) -> vector<16xf32> {
  %cst = arith.constant 0.0 : f32
  %0 = vector.transfer_read %m[%idx, %idx], %cst {in_bounds = [true, true]} : memref<?x?xf32>, vector<8x16xf32>
  %1 = vector.extract %0[0] : vector<8x16xf32>
  return %1 : vector<16xf32>
}