@@ -61,8 +61,15 @@ static bool canBeHoisted(Operation *op,
61
61
}
62
62
63
63
// / Merges srcEffect's Memory Effect on its resource into the
64
- // / resourceConflicts map, flagging resources if the srcEffect
65
- // / results in a conflict
64
+ // / resourceConflicts map, flagging the resource if the srcEffect
65
+ // / results in a conflict.
66
+ // /
67
+ // / \param resourceConflicts The map to store resources' conflicts status.
68
+ // / \param srcEffect The effect to merge into the resourceConflicts map.
69
+ // / \param srcHasConflict Whether the srcEffect results in a conflict based
70
+ // / on higher level analysis.
71
+ // /
72
+ // / resourceConflicts is modified by the function and will be non-empty
66
73
static void mergeResource (
67
74
DenseMap<TypeID, std::pair<bool , MemoryEffects::EffectInstance>> &resourceConflicts,
68
75
const MemoryEffects::EffectInstance &srcEffect,
@@ -110,9 +117,9 @@ static bool hasLoopVariantInput(LoopLikeOpInterface loopLike, Operation *op) {
110
117
111
118
// / Returns true if:
112
119
// / (a) any of the resources used by op's Memory Effects have been
113
- // / flagged as having a conflict within the resourceConflicts map
120
+ // / flagged as having a conflict within the resourceConflicts map OR
114
121
// / (b) op doesn't have a MemoryEffectOpInterface or has one but
115
- // / without any specific effects
122
+ // / without any specific effects.
116
123
static bool mayHaveMemoryEffectConflict (Operation *op,
117
124
DenseMap<TypeID, std::pair<bool , MemoryEffects::EffectInstance>> &resourceConflicts) {
118
125
@@ -155,7 +162,7 @@ void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
155
162
156
163
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
157
164
// gather all effects on op
158
- llvm:: SmallVector<MemoryEffects::EffectInstance> effects =
165
+ SmallVector<MemoryEffects::EffectInstance> effects =
159
166
MemoryEffects::getMemoryEffectsSorted (op);
160
167
161
168
if (!effects.empty ()) {
@@ -164,6 +171,8 @@ void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
164
171
bool writesConflict = hasLoopVariantInput (loopLike, op);
165
172
166
173
for (const MemoryEffects::EffectInstance &effect : effects) {
174
+ LDBG () << " Effect: " << effect.getEffect ()->getPriority () << " with resource: " << effect.getResource ()->getName () << " op " << op->getName ();
175
+
167
176
bool conflict = false ;
168
177
bool isWrite = isa<MemoryEffects::Write>(effect.getEffect ());
169
178
@@ -173,8 +182,7 @@ void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
173
182
if (isa<MemoryEffects::Read>(effect.getEffect ()))
174
183
writesConflict = true ;
175
184
176
- if (isWrite)
177
- if (writesConflict)
185
+ if (isWrite && writesConflict)
178
186
conflict = true ;
179
187
180
188
mergeResource (resourceConflicts, effect, conflict);
@@ -194,19 +202,25 @@ size_t mlir::moveLoopInvariantCode(
194
202
function_ref<void(Operation *, Region *)> moveOutOfRegion) {
195
203
size_t numMoved = 0 ;
196
204
205
+ // TODO: see if this can be spec'd in a meaningful way to add back later.
206
+ //
197
207
// check that the loop isn't dead
198
208
// auto isDead = loopLike.isZeroTrip();
199
209
// if (!isDead.has_value() || isDead.value())
200
210
// return numMoved;
201
211
202
- // go through loop body and map out resource usages
203
- // op->regions are essentially merged sequentially
204
- // e .g. an if's "then" and "else" regions are treated like one
205
- // continuous region --> need to add fork checking
212
+ // Go through loop body and map out resource usages.
213
+ // op->regions are essentially merged sequentially.
214
+ // E .g., an if's "then" and "else" regions are treated like one
215
+ // continuous region --> need to add fork checking.
206
216
//
207
- // loop "do" and "then" regions also merged
217
+ // loop "do" and "then" regions also merged.
208
218
DenseMap<TypeID, std::pair<bool , MemoryEffects::EffectInstance>> resourceConflicts;
209
- mlir::gatherResourceConflicts (loopLike, loopLike.getOperation (), resourceConflicts);
219
+ gatherResourceConflicts (loopLike, loopLike.getOperation (), resourceConflicts);
220
+
221
+ for (auto &item : resourceConflicts) {
222
+ LDBG () << " Resource: " << item.second .second .getResource ()->getName () << " has conflict: " << item.second .first << " with effect: " << item.second .second .getEffect ()->getPriority ();
223
+ }
210
224
211
225
auto regions = loopLike.getLoopRegions ();
212
226
for (Region *region : regions) {
@@ -233,7 +247,13 @@ size_t mlir::moveLoopInvariantCode(
233
247
234
248
bool noMemoryConflicts = isMemoryEffectFree (op)
235
249
|| !mayHaveMemoryEffectConflict (op, resourceConflicts);
236
-
250
+
251
+ LDBG () << " isMemoryEffectFree: " << isMemoryEffectFree (op)
252
+ << " mayHaveMemoryEffectConflict: " << mayHaveMemoryEffectConflict (op, resourceConflicts)
253
+ << " noMemoryConflicts: " << noMemoryConflicts
254
+ << " shouldMoveOutOfRegion: " << shouldMoveOutOfRegion (op, region)
255
+ << " canBeHoisted: " << canBeHoisted (op, definedOutside);
256
+
237
257
if (!noMemoryConflicts
238
258
|| !shouldMoveOutOfRegion (op, region)
239
259
|| !canBeHoisted (op, definedOutside))
0 commit comments