-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterchange.mlir
226 lines (193 loc) · 6.98 KB
/
interchange.mlir
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
// RUN: mlir-opt %s -affine-loop-interchange | FileCheck %s
func @interchange_for_spatial_locality(%A: memref<2048x2048xf64>) {
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 2048 {
%v = affine.load %A[%j, %i] : memref<2048x2048xf64>
affine.store %v, %A[%j, %i] : memref<2048x2048xf64>
}
}
return
}
// Interchanged for spatial locality.
// CHECK: affine.load %arg0[%arg1, %arg2] : memref<2048x2048xf64>
// CHECK-NEXT: affine.store %{{.*}}, %arg0[%arg1, %arg2] : memref<2048x2048xf64>
// -----
func @interchange_for_spatial_temporal(%A: memref<2048xf64>) {
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 2048 {
%v = affine.load %A[%j] : memref<2048xf64>
affine.load %A[%j] : memref<2048xf64>
affine.load %A[%i] : memref<2048xf64>
}
}
return
}
// More reuse with %j, %i order.
// CHECK: affine.load %arg0[%arg1] : memref<2048xf64>
// CHECK-NEXT: affine.load %0, %arg0[%arg1] : memref<2048xf64>
// CHECK-NEXT: affine.load %0, %arg0[%arg2] : memref<2048xf64>
// -----
// CHECK-LABEL: func @matmul_ijk
func @matmul_ijk(%A: memref<2048x2048xf64>, %B: memref<2048x2048xf64>, %C: memref<2048x2048xf64>) {
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 2048 {
affine.for %k = 0 to 2048 {
%a = affine.load %A[%i, %k] : memref<2048x2048xf64>
%b = affine.load %B[%k, %j] : memref<2048x2048xf64>
%ci = affine.load %C[%i, %j] : memref<2048x2048xf64>
%p = mulf %a, %b : f64
%co = addf %ci, %p : f64
affine.store %co, %C[%i, %j] : memref<2048x2048xf64>
}
}
}
return
}
// Test whether the ikj permutation has been found.
// CHECK: affine.load %arg0[%arg3, %arg4] : memref<2048x2048xf64>
// CHECK-NEXT: affine.load %arg1[%arg4, %arg5] : memref<2048x2048xf64>
// CHECK-NEXT: affine.load %arg2[%arg3, %arg5] : memref<2048x2048xf64>
// CHECK-NEXT: mulf %0, %1 : f64
// CHECK-NEXT: addf %2, %3 : f64
// CHECK-NEXT: affine.store %4, %arg2[%arg3, %arg5] : memref<2048x2048xf64>
// -----
// CHECK-LABEL: func @interchange_for_outer_parallelism
func @interchange_for_outer_parallelism(%A: memref<2048x2048x2048xf64>) {
affine.for %i = 1 to 2048 {
affine.for %j = 0 to 2048 {
affine.for %k = 0 to 2048 {
%v = affine.load %A[%i, %j, %k] : memref<2048x2048x2048xf64>
%p = mulf %v, %v : f64
affine.store %p, %A[%i - 1, %j, %k] : memref<2048x2048x2048xf64>
}
}
}
return
}
// %j should become outermost - provides outer parallelism and locality.
// CHECK-NEXT: affine.load %arg0[%arg2, %arg1, %arg3]
// CHECK-NEXT: mulf %0, %0 : f64
// CHECK-NEXT: affine.store %1, %arg0[%arg2 - 1, %arg1, %arg3]
// -----
func @test_group_reuse(%A: memref<2048x2048xf64>, %B: memref<?x?xf64>, %C: memref<?x?xf64>) {
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 2048 {
%v = affine.load %A[%i, %j] : memref<2048x2048xf64>
affine.store %v, %C[%i, %j] : memref<?x?xf64>
%u1 = affine.load %A[%j, %i] : memref<2048x2048xf64>
%u2 = affine.load %A[%j - 1, %i] : memref<2048x2048xf64>
%u3 = affine.load %A[%j + 1, %i] : memref<2048x2048xf64>
%s1 = addf %u1, %u2 : f64
%s2 = addf %s1, %u3 : f64
affine.store %s2, %B[%j, %i] : memref<?x?xf64>
}
}
return
}
// Interchanged for better reuse.
// CHECK: affine.store %{{.*}}[%arg1, %arg2] : memref<2048x2048xf64>
// -----
func @interchange_invalid(%A: memref<2048x2048xf64>) {
affine.for %t = 0 to 2048 {
affine.for %i = 0 to 2048 {
%u1 = affine.load %A[%t - 1, %i] : memref<2048x2048xf64>
%u2 = affine.load %A[%t - 1, %i + 1] : memref<2048x2048xf64>
%u3 = affine.load %A[%t - 1, %i - 1] : memref<2048x2048xf64>
%s1 = addf %u1, %u2 : f64
%s2 = addf %s1, %u3 : f64
affine.store %s2, %A[%t, %i] : memref<2048x2048xf64>
}
}
return
}
// Interchange is invalid.
// CHECK: affine.store %{{.*}}[%arg1, %arg2] : memref<2048x2048xf64>
// -----
// Test for handling other than add/mul.
func @interchange_for_spatial_locality_mod(%A: memref<2048x2048x2048xf64>) {
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 2048 {
affine.for %k = 0 to 2048 {
%v = affine.load %A[%i mod 2, %k, %j] : memref<2048x2048x2048xf64>
affine.store %v, %A[%i mod 2, %k, %j] : memref<2048x2048x2048xf64>
// Interchanged for spatial locality.
// CHECK: affine.load %arg0[%{{.*}} mod 2, %arg1, %arg3] : memref<2048x2048x2048xf64>
// CHECK-NEXT: affine.store %0, %arg0[%{{.*}} mod 2, %arg1, %arg3] : memref<2048x2048x2048xf64>
}
}
}
return
}
// -----
// Test to make sure there are no crashes/aborts on things that aren't handled.
func @if_else(%A: memref<2048x2048xf64>) {
%c0 = constant 0.0 : f64
%c1 = constant 1.0 : f64
affine.for %i = 0 to 2048 {
affine.if affine_set<(d0) : (d0 - 1024 >= 0)> (%i) {
affine.for %j = 0 to 2048 {
affine.store %c0, %A[%i, %j] : memref<2048x2048xf64>
}
} else {
affine.for %j = 0 to 2048 {
affine.store %c1, %A[%i, %j] : memref<2048x2048xf64>
}
}
}
return
}
// -----
// Test for interchange on imperfect nests.
func @imperfect_nest(%A: memref<2048x2048xf64>) {
%c0 = constant 0.0 : f64
%c1 = constant 1.0 : f64
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 1024 {
affine.store %c0, %A[%j, %i] : memref<2048x2048xf64>
}
affine.for %j = 1024 to 2048 {
affine.store %c1, %A[%j, %i] : memref<2048x2048xf64>
}
}
return
}
// CHECK: for %{{.*}} = 0 to 1024 {
// CHECK-NEXT: for %{{.*}} = 0 to 2048 {
// CHECK-NEXT: affine.store %{{.*}}, %arg0[%arg1, %arg2]
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: for %{{.*}} = 1024 to 2048 {
// CHECK-NEXT: for %{{.*}} = 0 to 2048 {
// CHECK-NEXT: affine.store %{{.*}}, %arg0[%arg1, %arg2]
// CHECK-NEXT: }
// CHECK-NEXT: }
// -----
func @multi_nest_seq(%A: memref<2048x2048xf64>, %B: memref<2048x2048xf64>, %C: memref<2048x2048xf64>) {
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 2048 {
%v = affine.load %A[%j, %i] : memref<2048x2048xf64>
affine.store %v, %A[%j, %i] : memref<2048x2048xf64>
// CHECK: affine.load %arg0[%arg3, %arg4] : memref<2048x2048xf64>
// CHECK-NEXT: affine.store %{{.*}}, %arg0[%arg3, %arg4] : memref<2048x2048xf64>
}
}
affine.for %i = 0 to 2048 {
affine.for %j = 0 to 2048 {
affine.for %k = 0 to 2048 {
%a = affine.load %A[%i, %k] : memref<2048x2048xf64>
%b = affine.load %B[%k, %j] : memref<2048x2048xf64>
%ci = affine.load %C[%i, %j] : memref<2048x2048xf64>
%p = mulf %a, %b : f64
%co = addf %ci, %p : f64
affine.store %co, %C[%i, %j] : memref<2048x2048xf64>
}
}
}
// CHECK: affine.load %arg0[%arg3, %arg4] : memref<2048x2048xf64>
// CHECK-NEXT: affine.load %arg1[%arg4, %arg5] : memref<2048x2048xf64>
// CHECK-NEXT: affine.load %arg2[%arg3, %arg5] : memref<2048x2048xf64>
// CHECK-NEXT: mulf
// CHECK-NEXT: addf
// CHECK-NEXT: affine.store %{{.*}}, %arg2[%arg3, %arg5] : memref<2048x2048xf64>
return
}