@@ -85,21 +85,18 @@ std::unique_ptr<OperationPass<FuncOp>> mlir::createMemRefDataFlowOptPass() {
85
85
// This is a straightforward implementation not optimized for speed. Optimize
86
86
// if needed.
87
87
void MemRefDataFlowOpt::forwardStoreToLoad (AffineLoadOp loadOp) {
88
- Operation *loadOpInst = loadOp.getOperation ();
89
-
90
- // First pass over the use list to get minimum number of surrounding
88
+ // First pass over the use list to get the minimum number of surrounding
91
89
// loops common between the load op and the store op, with min taken across
92
90
// all store ops.
93
91
SmallVector<Operation *, 8 > storeOps;
94
- unsigned minSurroundingLoops = getNestingDepth (*loadOpInst );
92
+ unsigned minSurroundingLoops = getNestingDepth (loadOp );
95
93
for (auto *user : loadOp.getMemRef ().getUsers ()) {
96
94
auto storeOp = dyn_cast<AffineStoreOp>(user);
97
95
if (!storeOp)
98
96
continue ;
99
- auto *storeOpInst = storeOp.getOperation ();
100
- unsigned nsLoops = getNumCommonSurroundingLoops (*loadOpInst, *storeOpInst);
97
+ unsigned nsLoops = getNumCommonSurroundingLoops (*loadOp, *storeOp);
101
98
minSurroundingLoops = std::min (nsLoops, minSurroundingLoops);
102
- storeOps.push_back (storeOpInst );
99
+ storeOps.push_back (storeOp );
103
100
}
104
101
105
102
// The list of store op candidates for forwarding that satisfy conditions
@@ -111,12 +108,12 @@ void MemRefDataFlowOpt::forwardStoreToLoad(AffineLoadOp loadOp) {
111
108
// post-dominance on these. 'fwdingCandidates' are a subset of depSrcStores.
112
109
SmallVector<Operation *, 8 > depSrcStores;
113
110
114
- for (auto *storeOpInst : storeOps) {
115
- MemRefAccess srcAccess (storeOpInst );
116
- MemRefAccess destAccess (loadOpInst );
111
+ for (auto *storeOp : storeOps) {
112
+ MemRefAccess srcAccess (storeOp );
113
+ MemRefAccess destAccess (loadOp );
117
114
// Find stores that may be reaching the load.
118
115
FlatAffineConstraints dependenceConstraints;
119
- unsigned nsLoops = getNumCommonSurroundingLoops (*loadOpInst , *storeOpInst );
116
+ unsigned nsLoops = getNumCommonSurroundingLoops (*loadOp , *storeOp );
120
117
unsigned d;
121
118
// Dependences at loop depth <= minSurroundingLoops do NOT matter.
122
119
for (d = nsLoops + 1 ; d > minSurroundingLoops; d--) {
@@ -130,7 +127,7 @@ void MemRefDataFlowOpt::forwardStoreToLoad(AffineLoadOp loadOp) {
130
127
continue ;
131
128
132
129
// Stores that *may* be reaching the load.
133
- depSrcStores.push_back (storeOpInst );
130
+ depSrcStores.push_back (storeOp );
134
131
135
132
// 1. Check if the store and the load have mathematically equivalent
136
133
// affine access functions; this implies that they statically refer to the
@@ -144,11 +141,11 @@ void MemRefDataFlowOpt::forwardStoreToLoad(AffineLoadOp loadOp) {
144
141
continue ;
145
142
146
143
// 2. The store has to dominate the load op to be candidate.
147
- if (!domInfo->dominates (storeOpInst, loadOpInst ))
144
+ if (!domInfo->dominates (storeOp, loadOp ))
148
145
continue ;
149
146
150
147
// We now have a candidate for forwarding.
151
- fwdingCandidates.push_back (storeOpInst );
148
+ fwdingCandidates.push_back (storeOp );
152
149
}
153
150
154
151
// 3. Of all the store op's that meet the above criteria, the store that
@@ -158,11 +155,11 @@ void MemRefDataFlowOpt::forwardStoreToLoad(AffineLoadOp loadOp) {
158
155
// Note: this can be implemented in a cleaner way with postdominator tree
159
156
// traversals. Consider this for the future if needed.
160
157
Operation *lastWriteStoreOp = nullptr ;
161
- for (auto *storeOpInst : fwdingCandidates) {
158
+ for (auto *storeOp : fwdingCandidates) {
162
159
if (llvm::all_of (depSrcStores, [&](Operation *depStore) {
163
- return postDomInfo->postDominates (storeOpInst , depStore);
160
+ return postDomInfo->postDominates (storeOp , depStore);
164
161
})) {
165
- lastWriteStoreOp = storeOpInst ;
162
+ lastWriteStoreOp = storeOp ;
166
163
break ;
167
164
}
168
165
}
@@ -175,7 +172,7 @@ void MemRefDataFlowOpt::forwardStoreToLoad(AffineLoadOp loadOp) {
175
172
// Record the memref for a later sweep to optimize away.
176
173
memrefsToErase.insert (loadOp.getMemRef ());
177
174
// Record this to erase later.
178
- loadOpsToErase.push_back (loadOpInst );
175
+ loadOpsToErase.push_back (loadOp );
179
176
}
180
177
181
178
void MemRefDataFlowOpt::runOnFunction () {
@@ -192,32 +189,31 @@ void MemRefDataFlowOpt::runOnFunction() {
192
189
loadOpsToErase.clear ();
193
190
memrefsToErase.clear ();
194
191
195
- // Walk all load's and perform load/ store forwarding.
192
+ // Walk all load's and perform store to load forwarding.
196
193
f.walk ([&](AffineLoadOp loadOp) { forwardStoreToLoad (loadOp); });
197
194
198
195
// Erase all load op's whose results were replaced with store fwd'ed ones.
199
- for (auto *loadOp : loadOpsToErase) {
196
+ for (auto *loadOp : loadOpsToErase)
200
197
loadOp->erase ();
201
- }
202
198
203
199
// Check if the store fwd'ed memrefs are now left with only stores and can
204
200
// thus be completely deleted. Note: the canonicalize pass should be able
205
201
// to do this as well, but we'll do it here since we collected these anyway.
206
202
for (auto memref : memrefsToErase) {
207
203
// If the memref hasn't been alloc'ed in this function, skip.
208
- Operation *defInst = memref.getDefiningOp ();
209
- if (!defInst || !isa<AllocOp>(defInst ))
204
+ Operation *defOp = memref.getDefiningOp ();
205
+ if (!defOp || !isa<AllocOp>(defOp ))
210
206
// TODO(mlir-team): if the memref was returned by a 'call' operation, we
211
207
// could still erase it if the call had no side-effects.
212
208
continue ;
213
- if (llvm::any_of (memref.getUsers (), [&](Operation *ownerInst ) {
214
- return (!isa<AffineStoreOp>(ownerInst ) && !isa<DeallocOp>(ownerInst ));
209
+ if (llvm::any_of (memref.getUsers (), [&](Operation *ownerOp ) {
210
+ return (!isa<AffineStoreOp>(ownerOp ) && !isa<DeallocOp>(ownerOp ));
215
211
}))
216
212
continue ;
217
213
218
214
// Erase all stores, the dealloc, and the alloc on the memref.
219
215
for (auto *user : llvm::make_early_inc_range (memref.getUsers ()))
220
216
user->erase ();
221
- defInst ->erase ();
217
+ defOp ->erase ();
222
218
}
223
219
}
0 commit comments