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
|
// RUN: mlir-opt %s -test-parallel-loop-unrolling='unroll-factors=1,2' -split-input-file | FileCheck %s
// RUN: mlir-opt %s -test-parallel-loop-unrolling='unroll-factors=1,2 loop-depth=1' -split-input-file | FileCheck %s --check-prefix CHECK-UNROLL-INNER
// RUN: mlir-opt %s -test-parallel-loop-unrolling='unroll-factors=3,1' -split-input-file | FileCheck %s --check-prefix CHECK-UNROLL-BY-3
func.func @unroll_simple_parallel_loop(%src: memref<1x16x12xf32>, %dst: memref<1x16x12xf32>) {
%c12 = arith.constant 12 : index
%c16 = arith.constant 16 : index
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
scf.parallel (%arg2, %arg3, %arg4) = (%c0, %c0, %c0) to (%c1, %c16, %c12) step (%c1, %c1, %c1) {
%read = memref.load %src[%arg2, %arg3, %arg4] : memref<1x16x12xf32>
memref.store %read, %dst[%arg2, %arg3, %arg4] : memref<1x16x12xf32>
scf.reduce
}
return
}
// CHECK-LABEL: func @unroll_simple_parallel_loop
// CHECK-SAME: ([[ARG0:%.*]]: memref<1x16x12xf32>, [[ARG1:%.*]]: memref<1x16x12xf32>)
// CHECK-DAG: [[C0:%.*]] = arith.constant 0 : index
// CHECK-DAG: [[C1:%.*]] = arith.constant 1 : index
// CHECK-DAG: [[C2:%.*]] = arith.constant 2 : index
// CHECK-DAG: [[C12:%.*]] = arith.constant 12 : index
// CHECK-DAG: [[C16:%.*]] = arith.constant 16 : index
// CHECK: scf.parallel ([[IV0:%.*]], [[IV1:%.*]], [[IV2:%.*]]) = ([[C0]], [[C0]], [[C0]]) to ([[C1]], [[C16]], [[C12]]) step ([[C1]], [[C1]], [[C2]])
// CHECK: [[LOADED1:%.*]] = memref.load [[ARG0]][[[IV0]], [[IV1]], [[IV2]]] : memref<1x16x12xf32>
// CHECK: memref.store [[LOADED1]], [[ARG1]][[[IV0]], [[IV1]], [[IV2]]] : memref<1x16x12xf32>
// CHECK: [[UNR_IV2:%.*]] = affine.apply {{.*}}([[IV2]])
// CHECK: [[LOADED2:%.*]] = memref.load [[ARG0]][[[IV0]], [[IV1]], [[UNR_IV2]]] : memref<1x16x12xf32>
// CHECK: memref.store [[LOADED2]], [[ARG1]][[[IV0]], [[IV1]], [[UNR_IV2]]] : memref<1x16x12xf32>
// -----
func.func @negative_unroll_factors_dont_divide_evenly(%src: memref<1x16x12xf32>, %dst: memref<1x16x12xf32>) {
%c12 = arith.constant 12 : index
%c16 = arith.constant 16 : index
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
scf.parallel (%arg2, %arg3, %arg4) = (%c0, %c0, %c0) to (%c1, %c16, %c12) step (%c1, %c1, %c1) {
%read = memref.load %src[%arg2, %arg3, %arg4] : memref<1x16x12xf32>
memref.store %read, %dst[%arg2, %arg3, %arg4] : memref<1x16x12xf32>
scf.reduce
}
return
}
// CHECK-UNROLL-BY-3-LABEL: func @negative_unroll_factors_dont_divide_evenly
// CHECK-UNROLL-BY-3-SAME: ([[ARG0:%.*]]: memref<1x16x12xf32>, [[ARG1:%.*]]: memref<1x16x12xf32>)
// CHECK-UNROLL-BY-3: [[C1:%.*]] = arith.constant 1 : index
// CHECK-UNROLL-BY-3: scf.parallel ([[IV0:%.*]], [[IV1:%.*]], [[IV2:%.*]]) = {{.*}} step ([[C1]], [[C1]], [[C1]])
// CHECK-UNROLL-BY-3: [[LOADED:%.*]] = memref.load [[ARG0]][[[IV0]], [[IV1]], [[IV2]]] : memref<1x16x12xf32>
// CHECK-UNROLL-BY-3: memref.store [[LOADED]], [[ARG1]][[[IV0]], [[IV1]], [[IV2]]] : memref<1x16x12xf32>
// CHECK-UNROLL-BY-3-NOT: affine.apply
// CHECK-UNROLL-BY-3-NOT: memref.load
// CHECK-UNROLL-BY-3-NOT: memref.store
// -----
func.func @unroll_outer_nested_parallel_loop(%src: memref<5x16x12x4x4xf32>, %dst: memref<5x16x12x4x4xf32>) {
%c4 = arith.constant 4 : index
%c12 = arith.constant 12 : index
%c16 = arith.constant 16 : index
%c5 = arith.constant 5 : index
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
scf.parallel (%arg3, %arg4, %arg5) = (%c0, %c0, %c0) to (%c5, %c16, %c12) step (%c1, %c1, %c1) {
scf.parallel (%arg6, %arg7) = (%c0, %c0) to (%c4, %c4) step (%c1, %c1) {
%0 = affine.apply affine_map<(d0, d1) -> (d0 + (d1 floordiv 4) * 4)>(%arg4, %arg6)
%1 = affine.apply affine_map<(d0, d1) -> (d0 + (d1 floordiv 4) * 4)>(%arg5, %arg7)
%subv_in = memref.subview %src[%arg3, %0, %1, 0, 0] [1, 1, 1, 4, 4] [1, 1, 1, 1, 1] : memref<5x16x12x4x4xf32> to memref<4x4xf32, strided<[4, 1], offset: ?>>
%subv_out = memref.subview %dst[%arg3, %0, %1, 0, 0] [1, 1, 1, 4, 4] [1, 1, 1, 1, 1] : memref<5x16x12x4x4xf32> to memref<4x4xf32, strided<[4, 1], offset: ?>>
linalg.erf ins(%subv_in : memref<4x4xf32, strided<[4, 1], offset: ?>>) outs(%subv_out : memref<4x4xf32, strided<[4, 1], offset: ?>>)
scf.reduce
}
scf.reduce
}
return
}
// CHECK-UNROLL-BY-3-LABEL: func @unroll_outer_nested_parallel_loop
// CHECK-LABEL: func @unroll_outer_nested_parallel_loop
// CHECK-SAME: ([[ARG0:%.*]]: memref<5x16x12x4x4xf32>, [[ARG1:%.*]]: memref<5x16x12x4x4xf32>)
// CHECK-DAG: [[C0:%.*]] = arith.constant 0 : index
// CHECK-DAG: [[C1:%.*]] = arith.constant 1 : index
// CHECK-DAG: [[C2:%.*]] = arith.constant 2 : index
// CHECK-DAG: [[C4:%.*]] = arith.constant 4 : index
// CHECK-DAG: [[C5:%.*]] = arith.constant 5 : index
// CHECK-DAG: [[C12:%.*]] = arith.constant 12 : index
// CHECK-DAG: [[C16:%.*]] = arith.constant 16 : index
// CHECK: scf.parallel ([[OUTV0:%.*]], [[OUTV1:%.*]], [[OUTV2:%.*]]) = ([[C0]], [[C0]], [[C0]]) to ([[C5]], [[C16]], [[C12]]) step ([[C1]], [[C1]], [[C2]])
// CHECK: scf.parallel ([[INV0:%.*]], [[INV1:%.*]]) = ([[C0]], [[C0]]) to ([[C4]], [[C4]]) step ([[C1]], [[C1]])
// CHECK: affine.apply {{.*}}([[OUTV1]], [[INV0]])
// CHECK: affine.apply {{.*}}([[OUTV2]], [[INV1]])
// CHECK: linalg.erf
// CHECK: [[UNR_OUTV2:%.*]] = affine.apply {{.*}}([[OUTV2]])
// CHECK: scf.parallel ([[INV0B:%.*]], [[INV1B:%.*]]) = ([[C0]], [[C0]]) to ([[C4]], [[C4]]) step ([[C1]], [[C1]])
// CHECK: affine.apply {{.*}}([[OUTV1]], [[INV0B]])
// CHECK: affine.apply {{.*}}([[UNR_OUTV2]], [[INV1B]])
// CHECK: linalg.erf
// -----
func.func @negative_unroll_dynamic_parallel_loop(%src: memref<1x16x12xf32>, %dst: memref<1x16x12xf32>, %ub3: index) {
%c12 = arith.constant 12 : index
%c16 = arith.constant 16 : index
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
scf.parallel (%arg2, %arg3, %arg4) = (%c0, %c0, %c0) to (%c1, %c16, %ub3) step (%c1, %c1, %c1) {
%read = memref.load %src[%arg2, %arg3, %arg4] : memref<1x16x12xf32>
memref.store %read, %dst[%arg2, %arg3, %arg4] : memref<1x16x12xf32>
scf.reduce
}
return
}
// CHECK-LABEL: func @negative_unroll_dynamic_parallel_loop
// CHECK-SAME: ([[ARG0:%.*]]: memref<1x16x12xf32>, [[ARG1:%.*]]: memref<1x16x12xf32>, [[UB3:%.*]]: index)
// CHECK-DAG: [[C0:%.*]] = arith.constant 0 : index
// CHECK-DAG: [[C1:%.*]] = arith.constant 1 : index
// CHECK-DAG: [[C16:%.*]] = arith.constant 16 : index
// CHECK: scf.parallel ([[IV0:%.*]], [[IV1:%.*]], [[IV2:%.*]]) = ([[C0]], [[C0]], [[C0]]) to ([[C1]], [[C16]], [[UB3]]) step ([[C1]], [[C1]], [[C1]])
// CHECK: [[LOADED:%.*]] = memref.load [[ARG0]][[[IV0]], [[IV1]], [[IV2]]] : memref<1x16x12xf32>
// CHECK: memref.store [[LOADED]], [[ARG1]][[[IV0]], [[IV1]], [[IV2]]] : memref<1x16x12xf32>
// CHECK-NOT: affine.apply
// CHECK-NOT: memref.load
// CHECK-NOT: memref.store
// -----
func.func @unroll_inner_nested_parallel_loop(%src: memref<5x16x12x4x4xf32>, %dst: memref<5x16x12x4x4xf32>) {
%c4 = arith.constant 4 : index
%c12 = arith.constant 12 : index
%c16 = arith.constant 16 : index
%c5 = arith.constant 5 : index
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
scf.parallel (%arg3, %arg4, %arg5) = (%c0, %c0, %c0) to (%c5, %c16, %c12) step (%c1, %c1, %c1) {
scf.parallel (%arg6, %arg7) = (%c0, %c0) to (%c4, %c4) step (%c1, %c1) {
%0 = affine.apply affine_map<(d0, d1) -> (d0 + (d1 floordiv 4) * 4)>(%arg4, %arg6)
%1 = affine.apply affine_map<(d0, d1) -> (d0 + (d1 floordiv 4) * 4)>(%arg5, %arg7)
%subv_in = memref.subview %src[%arg3, %0, %1, 0, 0] [1, 1, 1, 4, 4] [1, 1, 1, 1, 1] : memref<5x16x12x4x4xf32> to memref<4x4xf32, strided<[4, 1], offset: ?>>
%subv_out = memref.subview %dst[%arg3, %0, %1, 0, 0] [1, 1, 1, 4, 4] [1, 1, 1, 1, 1] : memref<5x16x12x4x4xf32> to memref<4x4xf32, strided<[4, 1], offset: ?>>
linalg.erf ins(%subv_in : memref<4x4xf32, strided<[4, 1], offset: ?>>) outs(%subv_out : memref<4x4xf32, strided<[4, 1], offset: ?>>)
scf.reduce
}
scf.reduce
}
return
}
// CHECK-LABEL: func @unroll_inner_nested_parallel_loop
// CHECK-UNROLL-INNER-LABEL: func @unroll_inner_nested_parallel_loop
// CHECK-UNROLL-INNER-SAME: ([[ARG0:%.*]]: memref<5x16x12x4x4xf32>, [[ARG1:%.*]]: memref<5x16x12x4x4xf32>)
// CHECK-UNROLL-INNER-DAG: [[C0:%.*]] = arith.constant 0 : index
// CHECK-UNROLL-INNER-DAG: [[C1:%.*]] = arith.constant 1 : index
// CHECK-UNROLL-INNER-DAG: [[C4:%.*]] = arith.constant 4 : index
// CHECK-UNROLL-INNER-DAG: [[C5:%.*]] = arith.constant 5 : index
// CHECK-UNROLL-INNER-DAG: [[C12:%.*]] = arith.constant 12 : index
// CHECK-UNROLL-INNER-DAG: [[C16:%.*]] = arith.constant 16 : index
// CHECK-UNROLL-INNER: scf.parallel ([[OUTV0:%.*]], [[OUTV1:%.*]], [[OUTV2:%.*]]) = ([[C0]], [[C0]], [[C0]]) to ([[C5]], [[C16]], [[C12]]) step ([[C1]], [[C1]], [[C1]])
// CHECK-UNROLL-INNER-DAG: [[C2:%.*]] = arith.constant 2 : index
// CHECK-UNROLL-INNER: scf.parallel ([[INV0:%.*]], [[INV1:%.*]]) = ([[C0]], [[C0]]) to ([[C4]], [[C4]]) step ([[C1]], [[C2]])
// CHECK-UNROLL-INNER: affine.apply {{.*}}([[OUTV1]], [[INV0]])
// CHECK-UNROLL-INNER: affine.apply {{.*}}([[OUTV2]], [[INV1]])
// CHECK-UNROLL-INNER: linalg.erf
// CHECK-UNROLL-INNER: [[UNR_INV1:%.*]] = affine.apply {{.*}}([[INV1]])
// CHECK-UNROLL-INNER: affine.apply {{.*}}([[OUTV1]], [[INV0]])
// CHECK-UNROLL-INNER: affine.apply {{.*}}([[OUTV2]], [[UNR_INV1]])
// CHECK-UNROLL-INNER: linalg.erf
|