Skip to content

Commit 30307b6

Browse files
committed
fixed formatting
1 parent 5c0c100 commit 30307b6

File tree

4 files changed

+74
-64
lines changed

4 files changed

+74
-64
lines changed

mlir/include/mlir/Interfaces/SideEffectInterfaces.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,11 @@ struct AlwaysSpeculatableImplTrait
348348

349349
namespace MemoryEffects {
350350
/// Defines the priority of the different memory effects.
351-
///
351+
///
352352
/// Sorting/ordering memory effects of an operation is done based on
353-
/// their defined stage and priority, in that order. If stage values for two effect instances are
354-
/// equal, they are then sorted by priority. Lower priority values indicate higher
355-
/// precedence.
353+
/// their defined stage and priority, in that order. If stage values for two
354+
/// effect instances are equal, they are then sorted by priority. Lower priority
355+
/// values indicate higher precedence.
356356
enum Priority {
357357
DefaultPriority = 0,
358358
AllocPriority = 1,
@@ -389,7 +389,9 @@ getMemoryEffectsSorted(Operation *op);
389389
/// resource. An 'allocate' effect implies only allocation of the resource, and
390390
/// not any visible mutation or dereference.
391391
struct Allocate : public Effect::Base<Allocate> {
392-
Allocate() : Effect::Base<Allocate>() { this->priority = Priority::AllocPriority; }
392+
Allocate() : Effect::Base<Allocate>() {
393+
this->priority = Priority::AllocPriority;
394+
}
393395
};
394396

395397
/// The following effect indicates that the operation frees some resource that

mlir/include/mlir/Transforms/LoopInvariantCodeMotionUtils.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ class Value;
2525

2626
/// Gathers potential conflicts on all memory resources used within loop.
2727
///
28-
/// Given a target loop and an op within it (or the loop op itself),
29-
/// gathers op's memory effects and flags potential resource conflicts
30-
/// in a map and then recurses into the op's regions to gather nested
31-
/// resource conflicts.
28+
/// Given a target loop and an op within it (or the loop op itself),
29+
/// gathers op's memory effects and flags potential resource conflicts
30+
/// in a map and then recurses into the op's regions to gather nested
31+
/// resource conflicts.
3232
///
3333
/// Typical usage:
3434
/// \code
3535
/// LoopLikeOpInterface myLoop = ...;
36-
/// DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> myConflicts;
37-
/// gatherResourceConflicts(myLoop, myLoop.getOperation(), resourceConflicts);
36+
/// DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>>
37+
/// myConflicts; gatherResourceConflicts(myLoop, myLoop.getOperation(),
38+
/// resourceConflicts);
3839
/// \endcode
3940
///
4041
/// \param loop The loop to gather resource conflicts for.
@@ -44,15 +45,17 @@ class Value;
4445
/// Key is the resource ID that effects are applied to. Value is a pair of
4546
/// a boolean, indicating if the resource has a conflict, and the last effect
4647
/// that was applied to the resource (if no conflicts exist) or the effect
47-
/// that caused the conflict (if conflicts exist). This was done so we
48+
/// that caused the conflict (if conflicts exist). This was done so we
4849
/// check the effect that causes the conflict for debugging purposes.
4950
/// First call should use loop = someLoop and op = someLoop.getOperation()
5051
///
5152
/// resourceConflicts is modified by the function and will be non-empty
5253
/// as long as there are memory effects within the loop, even if there are
5354
/// no conflicts.
54-
void gatherResourceConflicts(LoopLikeOpInterface loop, Operation *op,
55-
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> &resourceConflicts);
55+
void gatherResourceConflicts(
56+
LoopLikeOpInterface loop, Operation *op,
57+
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>>
58+
&resourceConflicts);
5659

5760
/// Given a list of regions, perform loop-invariant code motion. An operation is
5861
/// loop-invariant if it depends only of values defined outside of the loop.

mlir/lib/Interfaces/SideEffectInterfaces.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,19 +330,19 @@ mlir::MemoryEffects::getMemoryEffectsSorted(Operation *op) {
330330

331331
memInterface.getEffects(effectsSorted);
332332

333-
auto sortEffects =
334-
[](llvm::SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
335-
llvm::stable_sort(effects, [](const MemoryEffects::EffectInstance &a,
336-
const MemoryEffects::EffectInstance &b) {
337-
if (a.getStage() < b.getStage())
338-
return true;
339-
340-
if (a.getStage() == b.getStage())
341-
return a.getEffect()->getPriority() < b.getEffect()->getPriority();
342-
343-
return false; // b before a
344-
});
345-
};
333+
auto sortEffects =
334+
[](llvm::SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
335+
llvm::stable_sort(effects, [](const MemoryEffects::EffectInstance &a,
336+
const MemoryEffects::EffectInstance &b) {
337+
if (a.getStage() < b.getStage())
338+
return true;
339+
340+
if (a.getStage() == b.getStage())
341+
return a.getEffect()->getPriority() < b.getEffect()->getPriority();
342+
343+
return false; // b before a
344+
});
345+
};
346346
sortEffects(effectsSorted);
347347

348348
return effectsSorted;
@@ -352,12 +352,12 @@ bool mlir::isMemoryEffectFree(Operation *op) {
352352
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
353353
if (!memInterface.hasNoEffect())
354354
return false;
355-
355+
356356
// If the op does not have recursive side effects, then it is memory effect
357357
// free.
358358
if (!op->hasTrait<OpTrait::HasRecursiveMemoryEffects>())
359359
return true;
360-
360+
361361
} else if (!op->hasTrait<OpTrait::HasRecursiveMemoryEffects>()) {
362362
// Otherwise, if the op does not implement the memory effect interface and
363363
// it does not have recursive side effects, then it cannot be known that the

mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,35 @@ static bool canBeHoisted(Operation *op,
6060
op, [&](OpOperand &operand) { return definedOutside(operand.get()); });
6161
}
6262

63-
/// Merges srcEffect's Memory Effect on its resource into the
63+
/// Merges srcEffect's Memory Effect on its resource into the
6464
/// resourceConflicts map, flagging the resource if the srcEffect
6565
/// results in a conflict.
6666
///
6767
/// \param resourceConflicts The map to store resources' conflicts status.
6868
/// \param srcEffect The effect to merge into the resourceConflicts map.
69-
/// \param srcHasConflict Whether the srcEffect results in a conflict based
69+
/// \param srcHasConflict Whether the srcEffect results in a conflict based
7070
/// on higher level analysis.
7171
///
7272
/// resourceConflicts is modified by the function and will be non-empty
73-
static void mergeResource(
74-
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> &resourceConflicts,
75-
const MemoryEffects::EffectInstance &srcEffect,
76-
bool srcHasConflict) {
73+
static void
74+
mergeResource(DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>>
75+
&resourceConflicts,
76+
const MemoryEffects::EffectInstance &srcEffect,
77+
bool srcHasConflict) {
7778

7879
TypeID srcResourceID = srcEffect.getResource()->getResourceID();
7980

80-
bool srcIsAllocOrFree = isa<MemoryEffects::Allocate>(srcEffect.getEffect())
81-
|| isa<MemoryEffects::Free>(srcEffect.getEffect());
81+
bool srcIsAllocOrFree = isa<MemoryEffects::Allocate>(srcEffect.getEffect()) ||
82+
isa<MemoryEffects::Free>(srcEffect.getEffect());
8283

8384
bool conflict = srcHasConflict || srcIsAllocOrFree;
8485

8586
auto dstIt = resourceConflicts.find(srcResourceID);
8687

8788
// if it doesn't already exist, create entry for resource in map
8889
if (dstIt == resourceConflicts.end()) {
89-
resourceConflicts.insert(std::make_pair(srcResourceID, std::make_pair(conflict, srcEffect)));
90+
resourceConflicts.insert(
91+
std::make_pair(srcResourceID, std::make_pair(conflict, srcEffect)));
9092
return;
9193
}
9294

@@ -100,10 +102,10 @@ static void mergeResource(
100102
bool srcWrite = isa<MemoryEffects::Write>(srcEffect.getEffect());
101103
bool dstRead = isa<MemoryEffects::Read>(dstEffect.getEffect());
102104
bool readBeforeWrite = dstRead && srcWrite;
103-
105+
104106
conflict = conflict || readBeforeWrite;
105107

106-
dstIt->second =std::make_pair(conflict, srcEffect);
108+
dstIt->second = std::make_pair(conflict, srcEffect);
107109
}
108110

109111
/// Returns true if any of op's OpOperands are defined outside of loopLike
@@ -120,22 +122,24 @@ static bool hasLoopVariantInput(LoopLikeOpInterface loopLike, Operation *op) {
120122
/// flagged as having a conflict within the resourceConflicts map OR
121123
/// (b) op doesn't have a MemoryEffectOpInterface or has one but
122124
/// without any specific effects.
123-
static bool mayHaveMemoryEffectConflict(Operation *op,
124-
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> &resourceConflicts) {
125+
static bool mayHaveMemoryEffectConflict(
126+
Operation *op,
127+
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>>
128+
&resourceConflicts) {
125129

126130
auto memInterface = dyn_cast<MemoryEffectOpInterface>(op);
127-
131+
128132
// op does not implement the memory effect op interface
129133
// shouldn't be flagged as movable to be conservative
130-
if (!memInterface)
134+
if (!memInterface)
131135
return true;
132136

133137
// gather all effects on op
134138
llvm::SmallVector<MemoryEffects::EffectInstance> effects;
135139
memInterface.getEffects(effects);
136140

137141
// op has interface but no effects, be conservative
138-
if (effects.empty())
142+
if (effects.empty())
139143
return true;
140144

141145
// RFC moving ops with HasRecursiveMemoryEffects that have nested ops
@@ -145,10 +149,10 @@ static bool mayHaveMemoryEffectConflict(Operation *op,
145149
for (const MemoryEffects::EffectInstance &effect : effects) {
146150
auto resourceID = effect.getResource()->getResourceID();
147151

148-
auto resConIt = resourceConflicts.find(resourceID);
152+
auto resConIt = resourceConflicts.find(resourceID);
149153
if (resConIt == resourceConflicts.end())
150154
return true; // RFC realistically shouldn't reach here but just in case?
151-
155+
152156
bool hasConflict = resConIt->second.first;
153157
if (hasConflict)
154158
return true;
@@ -157,13 +161,15 @@ static bool mayHaveMemoryEffectConflict(Operation *op,
157161
return false;
158162
}
159163

160-
void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
161-
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> &resourceConflicts) {
164+
void mlir::gatherResourceConflicts(
165+
LoopLikeOpInterface loopLike, Operation *op,
166+
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>>
167+
&resourceConflicts) {
162168

163169
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
164170
// gather all effects on op
165171
SmallVector<MemoryEffects::EffectInstance> effects =
166-
MemoryEffects::getMemoryEffectsSorted(op);
172+
MemoryEffects::getMemoryEffectsSorted(op);
167173

168174
if (!effects.empty()) {
169175
// any variant input to the op could be the data source
@@ -173,24 +179,24 @@ void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
173179
for (const MemoryEffects::EffectInstance &effect : effects) {
174180
bool conflict = false;
175181
bool isWrite = isa<MemoryEffects::Write>(effect.getEffect());
176-
182+
177183
// all writes to a resource that follow a read on any other resource
178184
// have to be considered a conflict as guaranteeing that the read
179185
// is invariant and won't affect the write requires more robust logic
180186
if (isa<MemoryEffects::Read>(effect.getEffect()))
181187
writesConflict = true;
182188

183189
if (isWrite && writesConflict)
184-
conflict = true;
190+
conflict = true;
185191

186192
mergeResource(resourceConflicts, effect, conflict);
187193
}
188194
}
189195
}
190196

191197
for (Region &region : op->getRegions())
192-
for (Operation &opInner : region.getOps())
193-
gatherResourceConflicts(loopLike, &opInner, resourceConflicts);
198+
for (Operation &opInner : region.getOps())
199+
gatherResourceConflicts(loopLike, &opInner, resourceConflicts);
194200
}
195201

196202
size_t mlir::moveLoopInvariantCode(
@@ -213,7 +219,8 @@ size_t mlir::moveLoopInvariantCode(
213219
// continuous region --> need to add fork checking.
214220
//
215221
// loop "do" and "then" regions also merged.
216-
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> resourceConflicts;
222+
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>>
223+
resourceConflicts;
217224
gatherResourceConflicts(loopLike, loopLike.getOperation(), resourceConflicts);
218225

219226
auto regions = loopLike.getLoopRegions();
@@ -239,12 +246,12 @@ size_t mlir::moveLoopInvariantCode(
239246
LDBG() << "Checking op: "
240247
<< OpWithFlags(op, OpPrintingFlags().skipRegions());
241248

242-
bool noMemoryConflicts = isMemoryEffectFree(op)
243-
|| !mayHaveMemoryEffectConflict(op, resourceConflicts);
244-
245-
if (!noMemoryConflicts
246-
|| !shouldMoveOutOfRegion(op, region)
247-
|| !canBeHoisted(op, definedOutside))
249+
bool noMemoryConflicts =
250+
isMemoryEffectFree(op) ||
251+
!mayHaveMemoryEffectConflict(op, resourceConflicts);
252+
253+
if (!noMemoryConflicts || !shouldMoveOutOfRegion(op, region) ||
254+
!canBeHoisted(op, definedOutside))
248255
continue;
249256

250257
LDBG() << "Moving loop-invariant op: " << *op;
@@ -268,9 +275,7 @@ size_t mlir::moveLoopInvariantCode(LoopLikeOpInterface loopLike) {
268275
[&](Value value, Region *) {
269276
return loopLike.isDefinedOutsideOfLoop(value);
270277
},
271-
[&](Operation *op, Region *) {
272-
return isSpeculatable(op);
273-
},
278+
[&](Operation *op, Region *) { return isSpeculatable(op); },
274279
[&](Operation *op, Region *) { loopLike.moveOutOfLoop(op); });
275280
}
276281

0 commit comments

Comments
 (0)