[Mlir-commits] [mlir] [mlir][SCF] Do not peel already peeled loops (PR #71900)
Matthias Springer
llvmlistbot at llvm.org
Sun Nov 12 17:35:52 PST 2023
================
@@ -444,12 +444,88 @@ static AffineExpr symbolicDivide(AffineExpr expr, unsigned symbolPos,
llvm_unreachable("Unknown AffineExpr");
}
+/// Populate `result` with all summand operands of given (potentially nested)
+/// addition. If the given expression is not an addition, just populate the
+/// expression itself.
+/// Example: Add(Add(7, 8), Mul(9, 10)) will return [7, 8, Mul(9, 10)].
+static void getSummandExprs(AffineExpr expr, SmallVector<AffineExpr> &result) {
+ auto addExpr = expr.dyn_cast<AffineBinaryOpExpr>();
+ if (!addExpr || addExpr.getKind() != AffineExprKind::Add) {
+ result.push_back(expr);
+ return;
+ }
+ getSummandExprs(addExpr.getLHS(), result);
+ getSummandExprs(addExpr.getRHS(), result);
+}
+
+/// Return "true" if `candidate` is a negated expression, i.e., Mul(-1, expr).
+/// If so, also return the non-negated expression via `expr`.
+static bool isNegatedAffineExpr(AffineExpr candidate, AffineExpr &expr) {
+ auto mulExpr = candidate.dyn_cast<AffineBinaryOpExpr>();
+ if (!mulExpr || mulExpr.getKind() != AffineExprKind::Mul)
+ return false;
+ if (auto lhs = mulExpr.getLHS().dyn_cast<AffineConstantExpr>()) {
+ if (lhs.getValue() == -1) {
+ expr = mulExpr.getRHS();
+ return true;
+ }
+ }
+ if (auto rhs = mulExpr.getRHS().dyn_cast<AffineConstantExpr>()) {
+ if (rhs.getValue() == -1) {
+ expr = mulExpr.getLHS();
+ return true;
+ }
+ }
+ return false;
+}
+
+/// Return "true" if `lhs` % `rhs` is guaranteed to evaluate to zero based on
+/// the fact that `lhs` contains another modulo expression that ensures that
+/// `lhs` is divisible by `rhs`. This is a common pattern in the resulting IR
+/// after loop peeling.
+///
+/// Example: lhs = ub - ub % step
+/// rhs = step
+/// => (ub - ub % step) % step is guaranteed to evaluate to 0.
+static bool isModOfModSubtraction(AffineExpr lhs, AffineExpr rhs,
----------------
matthias-springer wrote:
That is tricky because we need to be able to handle cases such as:
```
Mod(Add(Add(a, Mul(-1, Mod(Add(a, b), c))), b), c)
```
Because of the nested `Mod` we cannot use the flattened representation to handle `Mul` and `Add`.
https://github.com/llvm/llvm-project/pull/71900
More information about the Mlir-commits
mailing list