Skip to content

Commit 871ecf4

Browse files
committed
PR cleanup
1 parent 85d37f3 commit 871ecf4

File tree

5 files changed

+76
-44
lines changed

5 files changed

+76
-44
lines changed

mlir/include/mlir/Interfaces/SideEffectInterfaces.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,18 @@ struct AlwaysSpeculatableImplTrait
347347
//===----------------------------------------------------------------------===//
348348

349349
namespace MemoryEffects {
350+
/// Defines the priority of the different memory effects.
351+
///
352+
/// 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.
350356
enum Priority {
351-
kDefaultPriority = 0,
352-
kAllocPriority = 1,
353-
kFreePriority = 2,
354-
kReadPriority = 3,
355-
kWritePriority = 4
357+
DefaultPriority = 0,
358+
AllocPriority = 1,
359+
FreePriority = 2,
360+
ReadPriority = 3,
361+
WritePriority = 4
356362
};
357363

358364
/// This class represents the base class used for memory effects.
@@ -370,41 +376,41 @@ struct Effect : public SideEffects::Effect {
370376

371377
protected:
372378
/// Priority value for this effect. Lower numbers indicate higher precedence.
373-
Priority priority = Priority::kDefaultPriority;
379+
Priority priority = Priority::DefaultPriority;
374380
};
375381
using EffectInstance = SideEffects::EffectInstance<Effect>;
376382

377-
/// Returns vector of effects sorted by effect stage then priority
378-
/// priority order: allocate -> free -> read -> write
383+
/// Returns vector of the op's memory effects sorted in increasing stage order
384+
/// and in increasing priority order within each stage.
379385
llvm::SmallVector<MemoryEffects::EffectInstance>
380386
getMemoryEffectsSorted(Operation *op);
381387

382388
/// The following effect indicates that the operation allocates from some
383389
/// resource. An 'allocate' effect implies only allocation of the resource, and
384390
/// not any visible mutation or dereference.
385391
struct Allocate : public Effect::Base<Allocate> {
386-
Allocate() : Effect::Base<Allocate>() { this->priority = Priority::kAllocPriority; }
392+
Allocate() : Effect::Base<Allocate>() { this->priority = Priority::AllocPriority; }
387393
};
388394

389395
/// The following effect indicates that the operation frees some resource that
390396
/// has been allocated. An 'allocate' effect implies only de-allocation of the
391397
/// resource, and not any visible allocation, mutation or dereference.
392398
struct Free : public Effect::Base<Free> {
393-
Free() : Effect::Base<Free>() { this->priority = Priority::kFreePriority; }
399+
Free() : Effect::Base<Free>() { this->priority = Priority::FreePriority; }
394400
};
395401

396402
/// The following effect indicates that the operation reads from some resource.
397403
/// A 'read' effect implies only dereferencing of the resource, and not any
398404
/// visible mutation.
399405
struct Read : public Effect::Base<Read> {
400-
Read() : Effect::Base<Read>() { this->priority = Priority::kReadPriority; }
406+
Read() : Effect::Base<Read>() { this->priority = Priority::ReadPriority; }
401407
};
402408

403409
/// The following effect indicates that the operation writes to some resource. A
404410
/// 'write' effect implies only mutating a resource, and not any visible
405411
/// dereference or read.
406412
struct Write : public Effect::Base<Write> {
407-
Write() : Effect::Base<Write>() { this->priority = Priority::kWritePriority; }
413+
Write() : Effect::Base<Write>() { this->priority = Priority::WritePriority; }
408414
};
409415
} // namespace MemoryEffects
410416

mlir/include/mlir/Transforms/LoopInvariantCodeMotionUtils.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,34 @@ class Region;
2323
class RewriterBase;
2424
class Value;
2525

26-
/// Gathers potential conflicts on all memory resources used within loop
26+
/// Gathers potential conflicts on all memory resources used within loop.
2727
///
2828
/// Given a target loop and an op within it (or the loop op itself),
2929
/// gathers op's memory effects and flags potential resource conflicts
3030
/// in a map and then recurses into the op's regions to gather nested
31-
/// resource conflicts
31+
/// resource conflicts.
3232
///
33+
/// Typical usage:
34+
/// \code
35+
/// LoopLikeOpInterface myLoop = ...;
36+
/// DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> myConflicts;
37+
/// gatherResourceConflicts(myLoop, myLoop.getOperation(), resourceConflicts);
38+
/// \endcode
39+
///
40+
/// \param loop The loop to gather resource conflicts for.
41+
/// \param op The operation to gather resource conflicts for,
42+
/// typically the loop op itself via loop.getOperation().
43+
/// \param resourceConflicts Map to store potential resource conflicts.
44+
/// Key is the resource ID that effects are applied to. Value is a pair of
45+
/// a boolean, indicating if the resource has a conflict, and the last effect
46+
/// 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+
/// check the effect that causes the conflict for debugging purposes.
3349
/// First call should use loop = someLoop and op = someLoop.getOperation()
50+
///
51+
/// resourceConflicts is modified by the function and will be non-empty
52+
/// as long as there are memory effects within the loop, even if there are
53+
/// no conflicts.
3454
void gatherResourceConflicts(LoopLikeOpInterface loop, Operation *op,
3555
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> &resourceConflicts);
3656

mlir/lib/Interfaces/CMakeLists.txt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,7 @@ add_mlir_interface_library(MemorySlotInterfaces)
8585
add_mlir_interface_library(ParallelCombiningOpInterface)
8686
add_mlir_interface_library(RuntimeVerifiableOpInterface)
8787
add_mlir_interface_library(ShapedOpInterfaces)
88-
89-
add_mlir_library(MLIRSideEffectInterfaces
90-
SideEffectInterfaces.cpp
91-
92-
ADDITIONAL_HEADER_DIRS
93-
${MLIR_MAIN_INCLUDE_DIR}/mlir/Interfaces
94-
95-
DEPENDS
96-
MLIRSideEffectInterfacesIncGen
97-
98-
LINK_LIBS PUBLIC
99-
MLIRIR
100-
MLIRDialectUtils
101-
MLIRLoopLikeInterface
102-
)
88+
add_mlir_interface_library(SideEffectInterfaces)
10389

10490
add_mlir_library(MLIRSubsetOpInterface
10591
SubsetOpInterface.cpp

mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,15 @@ static bool canBeHoisted(Operation *op,
6161
}
6262

6363
/// 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
6673
static void mergeResource(
6774
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> &resourceConflicts,
6875
const MemoryEffects::EffectInstance &srcEffect,
@@ -110,9 +117,9 @@ static bool hasLoopVariantInput(LoopLikeOpInterface loopLike, Operation *op) {
110117

111118
/// Returns true if:
112119
/// (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
114121
/// (b) op doesn't have a MemoryEffectOpInterface or has one but
115-
/// without any specific effects
122+
/// without any specific effects.
116123
static bool mayHaveMemoryEffectConflict(Operation *op,
117124
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> &resourceConflicts) {
118125

@@ -155,7 +162,7 @@ void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
155162

156163
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
157164
// gather all effects on op
158-
llvm::SmallVector<MemoryEffects::EffectInstance> effects =
165+
SmallVector<MemoryEffects::EffectInstance> effects =
159166
MemoryEffects::getMemoryEffectsSorted(op);
160167

161168
if (!effects.empty()) {
@@ -164,6 +171,8 @@ void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
164171
bool writesConflict = hasLoopVariantInput(loopLike, op);
165172

166173
for (const MemoryEffects::EffectInstance &effect : effects) {
174+
LDBG() << "Effect: " << effect.getEffect()->getPriority() << " with resource: " << effect.getResource()->getName() << " op " << op->getName();
175+
167176
bool conflict = false;
168177
bool isWrite = isa<MemoryEffects::Write>(effect.getEffect());
169178

@@ -173,8 +182,7 @@ void mlir::gatherResourceConflicts(LoopLikeOpInterface loopLike, Operation *op,
173182
if (isa<MemoryEffects::Read>(effect.getEffect()))
174183
writesConflict = true;
175184

176-
if (isWrite)
177-
if (writesConflict)
185+
if (isWrite && writesConflict)
178186
conflict = true;
179187

180188
mergeResource(resourceConflicts, effect, conflict);
@@ -194,19 +202,25 @@ size_t mlir::moveLoopInvariantCode(
194202
function_ref<void(Operation *, Region *)> moveOutOfRegion) {
195203
size_t numMoved = 0;
196204

205+
// TODO: see if this can be spec'd in a meaningful way to add back later.
206+
//
197207
// check that the loop isn't dead
198208
// auto isDead = loopLike.isZeroTrip();
199209
// if (!isDead.has_value() || isDead.value())
200210
// return numMoved;
201211

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.
206216
//
207-
// loop "do" and "then" regions also merged
217+
// loop "do" and "then" regions also merged.
208218
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+
}
210224

211225
auto regions = loopLike.getLoopRegions();
212226
for (Region *region : regions) {
@@ -233,7 +247,13 @@ size_t mlir::moveLoopInvariantCode(
233247

234248
bool noMemoryConflicts = isMemoryEffectFree(op)
235249
|| !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+
237257
if (!noMemoryConflicts
238258
|| !shouldMoveOutOfRegion(op, region)
239259
|| !canBeHoisted(op, definedOutside))

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2978,7 +2978,7 @@ def TestEffectsResult : TEST_Op<"test_effects_result"> {
29782978
}
29792979

29802980
//===----------------------------------------------------------------------===//
2981-
// Test Ops with multiple effects for Loop Invariant Code Motion
2981+
// Test Ops with multiple effects for Loop Invariant Code Motion.
29822982
//===----------------------------------------------------------------------===//
29832983

29842984
def TestResourceA : Resource<"TestResourceA">;

0 commit comments

Comments
 (0)