Add utility to promote single iteration loops. Add methods for getting constant
loop counts. Improve / refactor loop unroll / loop unroll and jam.
- add utility to remove single iteration loops.
- use this utility to promote single iteration loops after unroll/unroll-and-jam
- use loopUnrollByFactor for loopUnrollFull and remove most of the latter.
- add methods for getting constant loop trip count
PiperOrigin-RevId: 212039569
diff --git a/lib/IR/Statement.cpp b/lib/IR/Statement.cpp
index 65ca340..1f7604f 100644
--- a/lib/IR/Statement.cpp
+++ b/lib/IR/Statement.cpp
@@ -343,6 +343,24 @@
return ubMap->getSingleConstantValue();
}
+Optional<uint64_t> ForStmt::getConstantTripCount() const {
+ // TODO(bondhugula): handle arbitrary lower/upper bounds.
+ if (!hasConstantBounds())
+ return None;
+ int64_t lb = getConstantLowerBound();
+ int64_t ub = getConstantUpperBound();
+ int64_t step = getStep();
+
+ // 0 iteration loops.
+ if ((step >= 1 && lb > ub) || (step <= -1 && lb < ub))
+ return 0;
+
+ uint64_t tripCount = static_cast<uint64_t>((ub - lb + 1) % step == 0
+ ? (ub - lb + 1) / step
+ : (ub - lb + 1) / step + 1);
+ return tripCount;
+}
+
void ForStmt::setConstantLowerBound(int64_t value) {
MLIRContext *context = getContext();
auto *expr = AffineConstantExpr::get(value, context);