12
12
13
13
#include " mlir/Dialect/Affine/Passes.h"
14
14
15
- #include " mlir/Analysis/SliceAnalysis.h"
16
- #include " mlir/Dialect/Affine/Analysis/AffineAnalysis.h"
17
- #include " mlir/Dialect/Affine/Analysis/AffineStructures.h"
18
- #include " mlir/Dialect/Affine/Analysis/LoopAnalysis.h"
19
15
#include " mlir/Dialect/Affine/Analysis/Utils.h"
20
- #include " mlir/Dialect/Affine/IR/AffineOps.h"
21
- #include " mlir/Dialect/Affine/LoopUtils.h"
22
- #include " mlir/Dialect/Affine/Utils.h"
23
- #include " mlir/Dialect/Arith/IR/Arith.h"
24
16
#include " mlir/Dialect/Func/IR/FuncOps.h"
25
- #include " mlir/IR/AffineExpr.h"
26
- #include " mlir/IR/AffineMap.h"
27
- #include " mlir/IR/Builders.h"
28
- #include " mlir/IR/Matchers.h"
29
17
#include " mlir/Interfaces/SideEffectInterfaces.h"
30
- #include " llvm/ADT/DenseMap.h"
31
- #include " llvm/ADT/DenseSet.h"
32
- #include " llvm/ADT/SmallPtrSet.h"
33
- #include " llvm/Support/CommandLine.h"
34
18
#include " llvm/Support/Debug.h"
35
19
#include " llvm/Support/raw_ostream.h"
36
20
@@ -41,17 +25,21 @@ namespace affine {
41
25
} // namespace affine
42
26
} // namespace mlir
43
27
44
- #define DEBUG_TYPE " licm"
28
+ #define DEBUG_TYPE " affine- licm"
45
29
46
30
using namespace mlir ;
47
31
using namespace mlir ::affine;
48
32
49
33
namespace {
50
34
51
35
// / Affine loop invariant code motion (LICM) pass.
52
- // / TODO: The pass is missing zero-trip tests.
53
- // / TODO: This code should be removed once the new LICM pass can handle its
54
- // / uses.
36
+ // / TODO: The pass is missing zero tripcount tests.
37
+ // / TODO: When compared to the other standard LICM pass, this pass
38
+ // / has some special handling for affine read/write ops but such handling
39
+ // / requires aliasing to be sound, and as such this pass is unsound. In
40
+ // / addition, this handling is nothing particular to affine memory ops but would
41
+ // / apply to any memory read/write effect ops. Either aliasing should be handled
42
+ // / or this pass can be removed and the standard LICM can be used.
55
43
struct LoopInvariantCodeMotion
56
44
: public affine::impl::AffineLoopInvariantCodeMotionBase<
57
45
LoopInvariantCodeMotion> {
@@ -61,100 +49,80 @@ struct LoopInvariantCodeMotion
61
49
} // namespace
62
50
63
51
static bool
64
- checkInvarianceOfNestedIfOps (AffineIfOp ifOp, Value indVar, ValueRange iterArgs ,
52
+ checkInvarianceOfNestedIfOps (AffineIfOp ifOp, AffineForOp loop ,
65
53
SmallPtrSetImpl<Operation *> &opsWithUsers,
66
54
SmallPtrSetImpl<Operation *> &opsToHoist);
67
- static bool isOpLoopInvariant (Operation &op, Value indVar, ValueRange iterArgs ,
55
+ static bool isOpLoopInvariant (Operation &op, AffineForOp loop ,
68
56
SmallPtrSetImpl<Operation *> &opsWithUsers,
69
57
SmallPtrSetImpl<Operation *> &opsToHoist);
70
58
71
59
static bool
72
- areAllOpsInTheBlockListInvariant (Region &blockList, Value indVar,
73
- ValueRange iterArgs,
60
+ areAllOpsInTheBlockListInvariant (Region &blockList, AffineForOp loop,
74
61
SmallPtrSetImpl<Operation *> &opsWithUsers,
75
62
SmallPtrSetImpl<Operation *> &opsToHoist);
76
63
77
- // Returns true if the individual op is loop invariant.
78
- static bool isOpLoopInvariant (Operation &op, Value indVar, ValueRange iterArgs ,
64
+ // / Returns true if `op` is invariant on `loop` .
65
+ static bool isOpLoopInvariant (Operation &op, AffineForOp loop ,
79
66
SmallPtrSetImpl<Operation *> &opsWithUsers,
80
67
SmallPtrSetImpl<Operation *> &opsToHoist) {
81
- LLVM_DEBUG ( llvm::dbgs () << " iterating on op: " << op; );
68
+ Value iv = loop. getInductionVar ( );
82
69
83
70
if (auto ifOp = dyn_cast<AffineIfOp>(op)) {
84
- if (!checkInvarianceOfNestedIfOps (ifOp, indVar, iterArgs, opsWithUsers,
85
- opsToHoist))
71
+ if (!checkInvarianceOfNestedIfOps (ifOp, loop, opsWithUsers, opsToHoist))
86
72
return false ;
87
73
} else if (auto forOp = dyn_cast<AffineForOp>(op)) {
88
- if (!areAllOpsInTheBlockListInvariant (forOp.getRegion (), indVar, iterArgs ,
89
- opsWithUsers, opsToHoist))
74
+ if (!areAllOpsInTheBlockListInvariant (forOp.getRegion (), loop, opsWithUsers ,
75
+ opsToHoist))
90
76
return false ;
91
77
} else if (auto parOp = dyn_cast<AffineParallelOp>(op)) {
92
- if (!areAllOpsInTheBlockListInvariant (parOp.getRegion (), indVar, iterArgs ,
93
- opsWithUsers, opsToHoist))
78
+ if (!areAllOpsInTheBlockListInvariant (parOp.getRegion (), loop, opsWithUsers ,
79
+ opsToHoist))
94
80
return false ;
95
81
} else if (!isMemoryEffectFree (&op) &&
96
- !isa<AffineReadOpInterface, AffineWriteOpInterface,
97
- AffinePrefetchOp>(&op)) {
82
+ !isa<AffineReadOpInterface, AffineWriteOpInterface>(&op)) {
98
83
// Check for side-effecting ops. Affine read/write ops are handled
99
84
// separately below.
100
85
return false ;
101
- } else if (! matchPattern (&op, m_Constant () )) {
86
+ } else if (isa<AffineReadOpInterface, AffineWriteOpInterface>(op )) {
102
87
// Register op in the set of ops that have users.
103
88
opsWithUsers.insert (&op);
104
- if (isa<AffineReadOpInterface, AffineWriteOpInterface>(op)) {
105
- auto read = dyn_cast<AffineReadOpInterface>(op);
106
- Value memref = read ? read .getMemRef ()
107
- : cast<AffineWriteOpInterface>(op).getMemRef ();
108
- for (auto *user : memref.getUsers ()) {
109
- // If this memref has a user that is a DMA, give up because these
110
- // operations write to this memref.
111
- if (isa<AffineDmaStartOp, AffineDmaWaitOp>(user))
89
+ SmallVector<AffineForOp, 8 > userIVs;
90
+ auto read = dyn_cast<AffineReadOpInterface>(op);
91
+ Value memref =
92
+ read ? read .getMemRef () : cast<AffineWriteOpInterface>(op).getMemRef ();
93
+ for (auto *user : memref.getUsers ()) {
94
+ // If the memref used by the load/store is used in a store elsewhere in
95
+ // the loop nest, we do not hoist. Similarly, if the memref used in a
96
+ // load is also being stored too, we do not hoist the load.
97
+ // FIXME: This is missing checking aliases.
98
+ if (&op == user)
99
+ continue ;
100
+ if (hasEffect<MemoryEffects::Write>(user, memref) ||
101
+ (hasEffect<MemoryEffects::Read>(user, memref) &&
102
+ isa<AffineWriteOpInterface>(op))) {
103
+ userIVs.clear ();
104
+ getAffineForIVs (*user, &userIVs);
105
+ // Check that userIVs don't contain the for loop around the op.
106
+ if (llvm::is_contained (userIVs, loop))
112
107
return false ;
113
- // If the memref used by the load/store is used in a store elsewhere in
114
- // the loop nest, we do not hoist. Similarly, if the memref used in a
115
- // load is also being stored too, we do not hoist the load.
116
- if (isa<AffineWriteOpInterface>(user) ||
117
- (isa<AffineReadOpInterface>(user) &&
118
- isa<AffineWriteOpInterface>(op))) {
119
- if (&op != user) {
120
- SmallVector<AffineForOp, 8 > userIVs;
121
- getAffineForIVs (*user, &userIVs);
122
- // Check that userIVs don't contain the for loop around the op.
123
- if (llvm::is_contained (userIVs, getForInductionVarOwner (indVar)))
124
- return false ;
125
- }
126
- }
127
108
}
128
109
}
129
-
130
- if (op.getNumOperands () == 0 && !isa<AffineYieldOp>(op)) {
131
- LLVM_DEBUG (llvm::dbgs () << " Non-constant op with 0 operands\n " );
132
- return false ;
133
- }
134
110
}
135
111
136
112
// Check operands.
113
+ ValueRange iterArgs = loop.getRegionIterArgs ();
137
114
for (unsigned int i = 0 ; i < op.getNumOperands (); ++i) {
138
115
auto *operandSrc = op.getOperand (i).getDefiningOp ();
139
116
140
- LLVM_DEBUG (
141
- op.getOperand (i).print (llvm::dbgs () << " Iterating on operand\n " ));
142
-
143
117
// If the loop IV is the operand, this op isn't loop invariant.
144
- if (indVar == op.getOperand (i)) {
145
- LLVM_DEBUG (llvm::dbgs () << " Loop IV is the operand\n " );
118
+ if (iv == op.getOperand (i))
146
119
return false ;
147
- }
148
120
149
121
// If the one of the iter_args is the operand, this op isn't loop invariant.
150
- if (llvm::is_contained (iterArgs, op.getOperand (i))) {
151
- LLVM_DEBUG (llvm::dbgs () << " One of the iter_args is the operand\n " );
122
+ if (llvm::is_contained (iterArgs, op.getOperand (i)))
152
123
return false ;
153
- }
154
124
155
125
if (operandSrc) {
156
- LLVM_DEBUG (llvm::dbgs () << *operandSrc << " Iterating on operand src\n " );
157
-
158
126
// If the value was defined in the loop (outside of the if/else region),
159
127
// and that operation itself wasn't meant to be hoisted, then mark this
160
128
// operation loop dependent.
@@ -170,14 +138,13 @@ static bool isOpLoopInvariant(Operation &op, Value indVar, ValueRange iterArgs,
170
138
171
139
// Checks if all ops in a region (i.e. list of blocks) are loop invariant.
172
140
static bool
173
- areAllOpsInTheBlockListInvariant (Region &blockList, Value indVar,
174
- ValueRange iterArgs,
141
+ areAllOpsInTheBlockListInvariant (Region &blockList, AffineForOp loop,
175
142
SmallPtrSetImpl<Operation *> &opsWithUsers,
176
143
SmallPtrSetImpl<Operation *> &opsToHoist) {
177
144
178
145
for (auto &b : blockList) {
179
146
for (auto &op : b) {
180
- if (!isOpLoopInvariant (op, indVar, iterArgs , opsWithUsers, opsToHoist))
147
+ if (!isOpLoopInvariant (op, loop , opsWithUsers, opsToHoist))
181
148
return false ;
182
149
}
183
150
}
@@ -187,40 +154,36 @@ areAllOpsInTheBlockListInvariant(Region &blockList, Value indVar,
187
154
188
155
// Returns true if the affine.if op can be hoisted.
189
156
static bool
190
- checkInvarianceOfNestedIfOps (AffineIfOp ifOp, Value indVar, ValueRange iterArgs ,
157
+ checkInvarianceOfNestedIfOps (AffineIfOp ifOp, AffineForOp loop ,
191
158
SmallPtrSetImpl<Operation *> &opsWithUsers,
192
159
SmallPtrSetImpl<Operation *> &opsToHoist) {
193
- if (!areAllOpsInTheBlockListInvariant (ifOp.getThenRegion (), indVar, iterArgs ,
160
+ if (!areAllOpsInTheBlockListInvariant (ifOp.getThenRegion (), loop ,
194
161
opsWithUsers, opsToHoist))
195
162
return false ;
196
163
197
- if (!areAllOpsInTheBlockListInvariant (ifOp.getElseRegion (), indVar, iterArgs ,
164
+ if (!areAllOpsInTheBlockListInvariant (ifOp.getElseRegion (), loop ,
198
165
opsWithUsers, opsToHoist))
199
166
return false ;
200
167
201
168
return true ;
202
169
}
203
170
204
171
void LoopInvariantCodeMotion::runOnAffineForOp (AffineForOp forOp) {
205
- auto *loopBody = forOp.getBody ();
206
- auto indVar = forOp.getInductionVar ();
207
- ValueRange iterArgs = forOp.getRegionIterArgs ();
208
-
209
172
// This is the place where hoisted instructions would reside.
210
173
OpBuilder b (forOp.getOperation ());
211
174
212
175
SmallPtrSet<Operation *, 8 > opsToHoist;
213
176
SmallVector<Operation *, 8 > opsToMove;
214
177
SmallPtrSet<Operation *, 8 > opsWithUsers;
215
178
216
- for (auto &op : *loopBody ) {
179
+ for (Operation &op : *forOp. getBody () ) {
217
180
// Register op in the set of ops that have users. This set is used
218
181
// to prevent hoisting ops that depend on these ops that are
219
182
// not being hoisted.
220
183
if (!op.use_empty ())
221
184
opsWithUsers.insert (&op);
222
185
if (!isa<AffineYieldOp>(op)) {
223
- if (isOpLoopInvariant (op, indVar, iterArgs , opsWithUsers, opsToHoist)) {
186
+ if (isOpLoopInvariant (op, forOp , opsWithUsers, opsToHoist)) {
224
187
opsToMove.push_back (&op);
225
188
}
226
189
}
@@ -231,18 +194,13 @@ void LoopInvariantCodeMotion::runOnAffineForOp(AffineForOp forOp) {
231
194
for (auto *op : opsToMove) {
232
195
op->moveBefore (forOp);
233
196
}
234
-
235
- LLVM_DEBUG (forOp->print (llvm::dbgs () << " Modified loop\n " ));
236
197
}
237
198
238
199
void LoopInvariantCodeMotion::runOnOperation () {
239
200
// Walk through all loops in a function in innermost-loop-first order. This
240
201
// way, we first LICM from the inner loop, and place the ops in
241
202
// the outer loop, which in turn can be further LICM'ed.
242
- getOperation ().walk ([&](AffineForOp op) {
243
- LLVM_DEBUG (op->print (llvm::dbgs () << " \n Original loop\n " ));
244
- runOnAffineForOp (op);
245
- });
203
+ getOperation ().walk ([&](AffineForOp op) { runOnAffineForOp (op); });
246
204
}
247
205
248
206
std::unique_ptr<OperationPass<func::FuncOp>>
0 commit comments