memref-dataflow-opt.mlir
9.79 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// RUN: mlir-opt %s -memref-dataflow-opt | FileCheck %s
// CHECK-DAG: [[MAP0:#map[0-9]+]] = affine_map<(d0, d1) -> (d1 + 1)>
// CHECK-DAG: [[MAP1:#map[0-9]+]] = affine_map<(d0, d1) -> (d0)>
// CHECK-DAG: [[MAP2:#map[0-9]+]] = affine_map<(d0, d1) -> (d1)>
// CHECK-DAG: [[MAP3:#map[0-9]+]] = affine_map<(d0, d1) -> (d0 - 1)>
// CHECK-DAG: [[MAP4:#map[0-9]+]] = affine_map<(d0) -> (d0 + 1)>
// CHECK-LABEL: func @simple_store_load() {
func @simple_store_load() {
%cf7 = constant 7.0 : f32
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
%v0 = affine.load %m[%i0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
}
return
// CHECK: %{{.*}} = constant 7.000000e+00 : f32
// CHECK-NEXT: affine.for %{{.*}} = 0 to 10 {
// CHECK-NEXT: %{{.*}} = addf %{{.*}}, %{{.*}} : f32
// CHECK-NEXT: }
// CHECK-NEXT: return
}
// CHECK-LABEL: func @multi_store_load() {
func @multi_store_load() {
%c0 = constant 0 : index
%cf7 = constant 7.0 : f32
%cf8 = constant 8.0 : f32
%cf9 = constant 9.0 : f32
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
%v0 = affine.load %m[%i0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
affine.store %cf8, %m[%i0] : memref<10xf32>
affine.store %cf9, %m[%i0] : memref<10xf32>
%v2 = affine.load %m[%i0] : memref<10xf32>
%v3 = affine.load %m[%i0] : memref<10xf32>
%v4 = mulf %v2, %v3 : f32
}
return
// CHECK: %{{.*}} = constant 0 : index
// CHECK-NEXT: %{{.*}} = constant 7.000000e+00 : f32
// CHECK-NEXT: %{{.*}} = constant 8.000000e+00 : f32
// CHECK-NEXT: %{{.*}} = constant 9.000000e+00 : f32
// CHECK-NEXT: affine.for %{{.*}} = 0 to 10 {
// CHECK-NEXT: %{{.*}} = addf %{{.*}}, %{{.*}} : f32
// CHECK-NEXT: %{{.*}} = mulf %{{.*}}, %{{.*}} : f32
// CHECK-NEXT: }
// CHECK-NEXT: return
}
// The store-load forwarding can see through affine apply's since it relies on
// dependence information.
// CHECK-LABEL: func @store_load_affine_apply
func @store_load_affine_apply() -> memref<10x10xf32> {
%cf7 = constant 7.0 : f32
%m = alloc() : memref<10x10xf32>
affine.for %i0 = 0 to 10 {
affine.for %i1 = 0 to 10 {
%t0 = affine.apply affine_map<(d0, d1) -> (d1 + 1)>(%i0, %i1)
%t1 = affine.apply affine_map<(d0, d1) -> (d0)>(%i0, %i1)
%idx0 = affine.apply affine_map<(d0, d1) -> (d1)> (%t0, %t1)
%idx1 = affine.apply affine_map<(d0, d1) -> (d0 - 1)> (%t0, %t1)
affine.store %cf7, %m[%idx0, %idx1] : memref<10x10xf32>
// CHECK-NOT: affine.load %{{[0-9]+}}
%v0 = affine.load %m[%i0, %i1] : memref<10x10xf32>
%v1 = addf %v0, %v0 : f32
}
}
// The memref and its stores won't be erased due to this memref return.
return %m : memref<10x10xf32>
// CHECK: %{{.*}} = constant 7.000000e+00 : f32
// CHECK-NEXT: %{{.*}} = alloc() : memref<10x10xf32>
// CHECK-NEXT: affine.for %{{.*}} = 0 to 10 {
// CHECK-NEXT: affine.for %{{.*}} = 0 to 10 {
// CHECK-NEXT: %{{.*}} = affine.apply [[MAP0]](%{{.*}}, %{{.*}})
// CHECK-NEXT: %{{.*}} = affine.apply [[MAP1]](%{{.*}}, %{{.*}})
// CHECK-NEXT: %{{.*}} = affine.apply [[MAP2]](%{{.*}}, %{{.*}})
// CHECK-NEXT: %{{.*}} = affine.apply [[MAP3]](%{{.*}}, %{{.*}})
// CHECK-NEXT: affine.store %{{.*}}, %{{.*}}[%{{.*}}, %{{.*}}] : memref<10x10xf32>
// CHECK-NEXT: %{{.*}} = addf %{{.*}}, %{{.*}} : f32
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: return %{{.*}} : memref<10x10xf32>
}
// CHECK-LABEL: func @store_load_nested
func @store_load_nested(%N : index) {
%cf7 = constant 7.0 : f32
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
affine.for %i1 = 0 to %N {
%v0 = affine.load %m[%i0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
}
}
return
// CHECK: %{{.*}} = constant 7.000000e+00 : f32
// CHECK-NEXT: affine.for %{{.*}} = 0 to 10 {
// CHECK-NEXT: affine.for %{{.*}} = 0 to %{{.*}} {
// CHECK-NEXT: %{{.*}} = addf %{{.*}}, %{{.*}} : f32
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: return
}
// No forwarding happens here since either of the two stores could be the last
// writer; store/load forwarding will however be possible here once loop live
// out SSA scalars are available.
// CHECK-LABEL: func @multi_store_load_nested_no_fwd
func @multi_store_load_nested_no_fwd(%N : index) {
%cf7 = constant 7.0 : f32
%cf8 = constant 8.0 : f32
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
affine.for %i1 = 0 to %N {
affine.store %cf8, %m[%i1] : memref<10xf32>
}
affine.for %i2 = 0 to %N {
// CHECK: %{{[0-9]+}} = affine.load %{{.*}}[%{{.*}}] : memref<10xf32>
%v0 = affine.load %m[%i0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
}
}
return
}
// No forwarding happens here since both stores have a value going into
// the load.
// CHECK-LABEL: func @store_load_store_nested_no_fwd
func @store_load_store_nested_no_fwd(%N : index) {
%cf7 = constant 7.0 : f32
%cf9 = constant 9.0 : f32
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
affine.for %i1 = 0 to %N {
// CHECK: %{{[0-9]+}} = affine.load %{{.*}}[%{{.*}}] : memref<10xf32>
%v0 = affine.load %m[%i0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
affine.store %cf9, %m[%i0] : memref<10xf32>
}
}
return
}
// Forwarding happens here since the last store postdominates all other stores
// and other forwarding criteria are satisfied.
// CHECK-LABEL: func @multi_store_load_nested_fwd
func @multi_store_load_nested_fwd(%N : index) {
%cf7 = constant 7.0 : f32
%cf8 = constant 8.0 : f32
%cf9 = constant 9.0 : f32
%cf10 = constant 10.0 : f32
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
affine.for %i1 = 0 to %N {
affine.store %cf8, %m[%i1] : memref<10xf32>
}
affine.for %i2 = 0 to %N {
affine.store %cf9, %m[%i2] : memref<10xf32>
}
affine.store %cf10, %m[%i0] : memref<10xf32>
affine.for %i3 = 0 to %N {
// CHECK-NOT: %{{[0-9]+}} = affine.load
%v0 = affine.load %m[%i0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
}
}
return
}
// There is no unique load location for the store to forward to.
// CHECK-LABEL: func @store_load_no_fwd
func @store_load_no_fwd() {
%cf7 = constant 7.0 : f32
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
affine.for %i1 = 0 to 10 {
affine.for %i2 = 0 to 10 {
// CHECK: affine.load %{{[0-9]+}}
%v0 = affine.load %m[%i2] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
}
}
}
return
}
// Forwarding happens here as there is a one-to-one store-load correspondence.
// CHECK-LABEL: func @store_load_fwd
func @store_load_fwd() {
%cf7 = constant 7.0 : f32
%c0 = constant 0 : index
%m = alloc() : memref<10xf32>
affine.store %cf7, %m[%c0] : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.for %i1 = 0 to 10 {
affine.for %i2 = 0 to 10 {
// CHECK-NOT: affine.load %{{[0-9]}}+
%v0 = affine.load %m[%c0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
}
}
}
return
}
// Although there is a dependence from the second store to the load, it is
// satisfied by the outer surrounding loop, and does not prevent the first
// store to be forwarded to the load.
func @store_load_store_nested_fwd(%N : index) -> f32 {
%cf7 = constant 7.0 : f32
%cf9 = constant 9.0 : f32
%c0 = constant 0 : index
%c1 = constant 1 : index
%m = alloc() : memref<10xf32>
affine.for %i0 = 0 to 10 {
affine.store %cf7, %m[%i0] : memref<10xf32>
affine.for %i1 = 0 to %N {
%v0 = affine.load %m[%i0] : memref<10xf32>
%v1 = addf %v0, %v0 : f32
%idx = affine.apply affine_map<(d0) -> (d0 + 1)> (%i0)
affine.store %cf9, %m[%idx] : memref<10xf32>
}
}
// Due to this load, the memref isn't optimized away.
%v3 = affine.load %m[%c1] : memref<10xf32>
return %v3 : f32
// CHECK: %{{.*}} = alloc() : memref<10xf32>
// CHECK-NEXT: affine.for %{{.*}} = 0 to 10 {
// CHECK-NEXT: affine.store %{{.*}}, %{{.*}}[%{{.*}}] : memref<10xf32>
// CHECK-NEXT: affine.for %{{.*}} = 0 to %{{.*}} {
// CHECK-NEXT: %{{.*}} = addf %{{.*}}, %{{.*}} : f32
// CHECK-NEXT: %{{.*}} = affine.apply [[MAP4]](%{{.*}})
// CHECK-NEXT: affine.store %{{.*}}, %{{.*}}[%{{.*}}] : memref<10xf32>
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: %{{.*}} = affine.load %{{.*}}[%{{.*}}] : memref<10xf32>
// CHECK-NEXT: return %{{.*}} : f32
}
// CHECK-LABEL: func @should_not_fwd
func @should_not_fwd(%A: memref<100xf32>, %M : index, %N : index) -> f32 {
%cf = constant 0.0 : f32
affine.store %cf, %A[%M] : memref<100xf32>
// CHECK: affine.load %{{.*}}[%{{.*}}]
%v = affine.load %A[%N] : memref<100xf32>
return %v : f32
}
// Can store forward to A[%j, %i], but no forwarding to load on %A[%i, %j]
// CHECK-LABEL: func @refs_not_known_to_be_equal
func @refs_not_known_to_be_equal(%A : memref<100 x 100 x f32>, %M : index) {
%N = affine.apply affine_map<(d0) -> (d0 + 1)> (%M)
%cf1 = constant 1.0 : f32
affine.for %i = 0 to 100 {
// CHECK: affine.for %[[I:.*]] =
affine.for %j = 0 to 100 {
// CHECK: affine.for %[[J:.*]] =
// CHECK: affine.load %{{.*}}[%[[I]], %[[J]]]
%u = affine.load %A[%i, %j] : memref<100x100xf32>
// CHECK-NEXT: affine.store %{{.*}}, %{{.*}}[%[[J]], %[[I]]]
affine.store %cf1, %A[%j, %i] : memref<100x100xf32>
// CHECK-NEXT: affine.load %{{.*}}[%[[I]], %[[J]]]
%v = affine.load %A[%i, %j] : memref<100x100xf32>
// This load should disappear.
%w = affine.load %A[%j, %i] : memref<100x100xf32>
// CHECK-NEXT: "foo"
"foo" (%u, %v, %w) : (f32, f32, f32) -> ()
}
}
return
}