Skip to content

Commit b39a27c

Browse files
committed
Revert "Revert "Merge pull request #407 from Xilinx/matthias.fix_non_monotonic_slice_params""
This reverts commit 018230a.
1 parent d59e6b9 commit b39a27c

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

mlir/include/mlir/IR/AffineExpr.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class AffineExpr {
110110
/// floordiv, ceildiv, and mod is only allowed w.r.t constants.
111111
bool isPureAffine() const;
112112

113+
/// Returns true if this expression is monotonicically increasing with respect
114+
/// to the AffineDimExprs, i.e. increasing the value of any AffineDimExpr will
115+
/// never decrease the value of the result.
116+
bool isMonotonicallyIncreasing() const;
117+
113118
/// Returns the greatest known integral divisor of this affine expression. The
114119
/// result is always positive.
115120
int64_t getLargestKnownDivisor() const;

mlir/include/mlir/IR/AffineMap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ class AffineMap {
382382
/// Returns true if the AffineMap represents a symbol-less permutation map.
383383
bool isPermutation() const;
384384

385+
// Returns true if every result is monotonically increasing.
386+
// See AffineExpr::isMonotonicallyIncreasing().
387+
bool isComponentWiseMonotonicallyIncreasing() const;
388+
385389
/// Returns the map consisting of the `resultPos` subset.
386390
AffineMap getSubMap(ArrayRef<unsigned> resultPos) const;
387391

mlir/lib/IR/AffineExpr.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,42 @@ bool AffineExpr::isPureAffine() const {
239239
llvm_unreachable("Unknown AffineExpr");
240240
}
241241

242+
static bool isNonNegativeConstant(AffineExpr expr) {
243+
auto constant = dyn_cast<AffineConstantExpr>(expr);
244+
return constant && constant.getValue() >= 0;
245+
}
246+
247+
bool AffineExpr::isMonotonicallyIncreasing() const {
248+
switch (getKind()) {
249+
case AffineExprKind::SymbolId:
250+
case AffineExprKind::DimId:
251+
case AffineExprKind::Constant:
252+
return true;
253+
case AffineExprKind::Add: {
254+
auto op = llvm::cast<AffineBinaryOpExpr>(*this);
255+
return op.getLHS().isMonotonicallyIncreasing() &&
256+
op.getRHS().isMonotonicallyIncreasing();
257+
}
258+
case AffineExprKind::Mul: {
259+
// One operand must be a non-negative constant.
260+
auto op = llvm::cast<AffineBinaryOpExpr>(*this);
261+
return op.getLHS().isMonotonicallyIncreasing() &&
262+
op.getRHS().isMonotonicallyIncreasing() &&
263+
(isNonNegativeConstant(op.getLHS()) ||
264+
isNonNegativeConstant(op.getRHS()));
265+
}
266+
case AffineExprKind::FloorDiv:
267+
case AffineExprKind::CeilDiv: {
268+
auto op = llvm::cast<AffineBinaryOpExpr>(*this);
269+
return op.getLHS().isMonotonicallyIncreasing() &&
270+
isNonNegativeConstant(op.getRHS());
271+
}
272+
case AffineExprKind::Mod:
273+
return false;
274+
}
275+
llvm_unreachable("Unknown AffineExpr");
276+
}
277+
242278
// Returns the greatest known integral divisor of this affine expression.
243279
int64_t AffineExpr::getLargestKnownDivisor() const {
244280
AffineBinaryOpExpr binExpr(nullptr);

mlir/lib/IR/AffineMap.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,11 @@ bool AffineMap::isPermutation() const {
651651
return isProjectedPermutation();
652652
}
653653

654+
bool AffineMap::isComponentWiseMonotonicallyIncreasing() const {
655+
return all_of(getResults(),
656+
[](auto expr) { return expr.isMonotonicallyIncreasing(); });
657+
}
658+
654659
AffineMap AffineMap::getSubMap(ArrayRef<unsigned> resultPos) const {
655660
SmallVector<AffineExpr, 4> exprs;
656661
exprs.reserve(resultPos.size());

0 commit comments

Comments
 (0)