Skip to content

Commit a87023b

Browse files
committed
working version without isZeroTrip
1 parent 9463b0c commit a87023b

File tree

6 files changed

+136
-101
lines changed

6 files changed

+136
-101
lines changed

mlir/include/mlir/Interfaces/SideEffectInterfaces.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "mlir/IR/Dominance.h"
1818
#include "mlir/IR/OpDefinition.h"
19-
#include "mlir/Interfaces/LoopLikeInterface.h"
2019

2120
namespace mlir {
2221
namespace SideEffects {
@@ -349,7 +348,7 @@ struct AlwaysSpeculatableImplTrait
349348

350349
namespace MemoryEffects {
351350
enum Priority {
352-
kDefault = 0,
351+
kDefaultPriority = 0,
353352
kAllocPriority = 1,
354353
kFreePriority = 2,
355354
kReadPriority = 3,
@@ -371,7 +370,7 @@ struct Effect : public SideEffects::Effect {
371370

372371
protected:
373372
/// Priority value for this effect. Lower numbers indicate higher precedence.
374-
Priority priority = kDefault;
373+
Priority priority = Priority::kDefaultPriority;
375374
};
376375
using EffectInstance = SideEffects::EffectInstance<Effect>;
377376

@@ -384,28 +383,28 @@ getMemoryEffectsSorted(Operation *op);
384383
/// resource. An 'allocate' effect implies only allocation of the resource, and
385384
/// not any visible mutation or dereference.
386385
struct Allocate : public Effect::Base<Allocate> {
387-
Allocate() : Effect::Base<Allocate>() { this->priority = kAllocPriority; }
386+
Allocate() : Effect::Base<Allocate>() { this->priority = Priority::kAllocPriority; }
388387
};
389388

390389
/// The following effect indicates that the operation frees some resource that
391390
/// has been allocated. An 'allocate' effect implies only de-allocation of the
392391
/// resource, and not any visible allocation, mutation or dereference.
393392
struct Free : public Effect::Base<Free> {
394-
Free() : Effect::Base<Free>() { this->priority = kFreePriority; }
393+
Free() : Effect::Base<Free>() { this->priority = Priority::kFreePriority; }
395394
};
396395

397396
/// The following effect indicates that the operation reads from some resource.
398397
/// A 'read' effect implies only dereferencing of the resource, and not any
399398
/// visible mutation.
400399
struct Read : public Effect::Base<Read> {
401-
Read() : Effect::Base<Read>() { this->priority = kReadPriority; }
400+
Read() : Effect::Base<Read>() { this->priority = Priority::kReadPriority; }
402401
};
403402

404403
/// The following effect indicates that the operation writes to some resource. A
405404
/// 'write' effect implies only mutating a resource, and not any visible
406405
/// dereference or read.
407406
struct Write : public Effect::Base<Write> {
408-
Write() : Effect::Base<Write>() { this->priority = kWritePriority; }
407+
Write() : Effect::Base<Write>() { this->priority = Priority::kWritePriority; }
409408
};
410409
} // namespace MemoryEffects
411410

@@ -489,15 +488,6 @@ bool isOpTriviallyDead(Operation *op);
489488
/// Note: Terminators and symbols are never considered to be trivially dead.
490489
bool wouldOpBeTriviallyDead(Operation *op);
491490

492-
/// Returns TRUE if the loop is dead/zero-trip,
493-
/// FALSE if loop has constant bounds/steps and has at least 1 iteration
494-
/// on every dimension, returns nullopt otherwise
495-
///
496-
/// Can only infer if loop is dead if it has constant loop bounds/steps.
497-
/// Otherwise we assume that it's dead to be conservative.
498-
///
499-
std::optional<bool> isZeroTrip(mlir::LoopLikeOpInterface &loop);
500-
501491
/// Returns true if the given operation is free of memory effects.
502492
///
503493
/// An operation is free of memory effects if its implementation of

mlir/include/mlir/Transforms/LoopInvariantCodeMotionUtils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "mlir/Support/LLVM.h"
1414
#include "mlir/Support/TypeID.h"
1515

16-
#include "llvm/ADT/SmallVector.h"
1716
#include <utility>
1817

1918
namespace mlir {

mlir/lib/Interfaces/SideEffectInterfaces.cpp

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "mlir/Interfaces/SideEffectInterfaces.h"
1010

11-
#include "mlir/Dialect/Utils/StaticValueUtils.h"
1211
#include "mlir/IR/Dominance.h"
1312
#include "mlir/IR/SymbolTable.h"
1413
#include <optional>
@@ -320,38 +319,15 @@ bool mlir::wouldOpBeTriviallyDead(Operation *op) {
320319
return wouldOpBeTriviallyDeadImpl(op);
321320
}
322321

323-
std::optional<bool> mlir::isZeroTrip(mlir::LoopLikeOpInterface &loop) {
324-
auto lbs = loop.getLoopLowerBounds();
325-
auto ubs = loop.getLoopUpperBounds();
326-
auto steps = loop.getLoopSteps();
327-
328-
if (!lbs || !ubs || !steps)
329-
return std::nullopt;
330-
331-
if (lbs->size() != ubs->size() || ubs->size() != steps->size())
332-
return std::nullopt;
333-
334-
for (size_t i = 0; i < steps->size(); ++i) {
335-
auto lb = getConstantIntValue((*lbs)[i]);
336-
auto ub = getConstantIntValue((*ubs)[i]);
337-
auto st = getConstantIntValue((*steps)[i]);
338-
339-
if (!lb || !ub || !st)
340-
return std::nullopt; // non-constant -> unknown
341-
342-
if (*st >= 0 && *lb >= *ub)
343-
return true;
344-
if (*st < 0 && *lb <= *ub)
345-
return true;
346-
}
347-
return false;
348-
}
349-
350322
llvm::SmallVector<MemoryEffects::EffectInstance>
351323
mlir::MemoryEffects::getMemoryEffectsSorted(Operation *op) {
324+
llvm::SmallVector<MemoryEffects::EffectInstance> effectsSorted;
325+
352326
auto memInterface = dyn_cast<MemoryEffectOpInterface>(op);
353327

354-
llvm::SmallVector<MemoryEffects::EffectInstance> effectsSorted;
328+
if (!memInterface)
329+
return effectsSorted; // return empty vec
330+
355331
memInterface.getEffects(effectsSorted);
356332

357333
auto sortEffects =

mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/IR/Operation.h"
1616
#include "mlir/IR/OperationSupport.h"
1717
#include "mlir/IR/PatternMatch.h"
18+
#include "mlir/IR/Value.h"
1819
#include "mlir/Interfaces/LoopLikeInterface.h"
1920
#include "mlir/Interfaces/SideEffectInterfaces.h"
2021
#include "mlir/Interfaces/SubsetOpInterface.h"
@@ -64,20 +65,18 @@ static void mergeResource(
6465
const MemoryEffects::EffectInstance &srcEffect,
6566
bool srcHasConflict) {
6667

67-
TypeID srcResourceID = srcEffect.getEffect()->getEffectID();
68+
TypeID srcResourceID = srcEffect.getResource()->getResourceID();
6869

6970
bool srcIsAllocOrFree = isa<MemoryEffects::Allocate>(srcEffect.getEffect())
7071
|| isa<MemoryEffects::Free>(srcEffect.getEffect());
7172

7273
bool conflict = srcHasConflict || srcIsAllocOrFree;
7374

74-
DenseMap<mlir::Value, std::pair<int, bool>> myMap;
75-
7675
auto dstIt = dstMap.find(srcResourceID);
7776

78-
// if it doesnt already exist, create entry for resource in map
77+
// if it doesn't already exist, create entry for resource in map
7978
if (dstIt == dstMap.end()) {
80-
dstMap.try_emplace(srcResourceID, std::make_pair(conflict, srcEffect));
79+
dstMap.insert(std::make_pair(srcResourceID, std::make_pair(conflict, srcEffect)));
8180
return;
8281
}
8382

@@ -94,12 +93,12 @@ static void mergeResource(
9493

9594
conflict = conflict || readBeforeWrite;
9695

97-
dstMap.try_emplace(srcResourceID, std::make_pair(conflict, srcEffect));
96+
dstIt->second =std::make_pair(conflict, srcEffect);
9897
}
9998

10099
static bool hasLoopVariantInput(LoopLikeOpInterface loopLike, Operation *op) {
101-
for (const auto &input : op->getOperands())
102-
if (loopLike.isDefinedOutsideOfLoop(input))
100+
for (OpOperand &input : op->getOpOperands())
101+
if (!loopLike.isDefinedOutsideOfLoop(input.get()))
103102
return true;
104103

105104
return false;
@@ -187,9 +186,9 @@ size_t mlir::moveLoopInvariantCode(
187186
size_t numMoved = 0;
188187

189188
// check that the loop isn't dead
190-
auto isDead = isZeroTrip(loopLike);
191-
if (!isDead.has_value() || isDead.value())
192-
return numMoved;
189+
// auto isDead = loopLike.isZeroTrip();
190+
// if (!isDead.has_value() || isDead.value())
191+
// return numMoved;
193192

194193
// go through loop body and map out resource usages
195194
// op->regions are essentially merged sequentially
@@ -200,8 +199,8 @@ size_t mlir::moveLoopInvariantCode(
200199
DenseMap<TypeID, std::pair<bool, MemoryEffects::EffectInstance>> resourceConflicts;
201200
mlir::gatherResourceConflicts(loopLike, loopLike.getOperation(), resourceConflicts);
202201

203-
204-
for (Region *region : loopLike.getLoopRegions()) {
202+
auto regions = loopLike.getLoopRegions();
203+
for (Region *region : regions) {
205204
LDBG() << "Original loop:\n" << *region->getParentOp();
206205

207206
std::queue<Operation *> worklist;

0 commit comments

Comments
 (0)