From: Thomas Raoux Date: Fri, 15 Jan 2021 22:03:57 +0000 (-0800) Subject: [mlir][NFC] Move helper substWithMin into Affine utils X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=3afbfb4145bea3796f1137c377774848093c3435;p=android-x86%2Fexternal-llvm-project.git [mlir][NFC] Move helper substWithMin into Affine utils This allow using this helper outside of the linalg canonicalization. Differential Revision: https://reviews.llvm.org/D94826 --- diff --git a/mlir/include/mlir/Dialect/Affine/Utils.h b/mlir/include/mlir/Dialect/Affine/Utils.h index 4f36bb800dd..be6985dfe40 100644 --- a/mlir/include/mlir/Dialect/Affine/Utils.h +++ b/mlir/include/mlir/Dialect/Affine/Utils.h @@ -13,6 +13,7 @@ #ifndef MLIR_DIALECT_AFFINE_UTILS_H #define MLIR_DIALECT_AFFINE_UTILS_H +#include "mlir/IR/AffineExpr.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" @@ -130,6 +131,15 @@ vectorizeAffineLoopNest(std::vector> &loops, /// early if the op is already in a normalized form. void normalizeAffineParallel(AffineParallelOp op); +/// Traverse `e` and return an AffineExpr where all occurrences of `dim` have +/// been replaced by either: +/// - `min` if `positivePath` is true when we reach an occurrence of `dim` +/// - `max` if `positivePath` is true when we reach an occurrence of `dim` +/// `positivePath` is negated each time we hit a multiplicative or divisive +/// binary op with a constant negative coefficient. +AffineExpr substWithMin(AffineExpr e, AffineExpr dim, AffineExpr min, + AffineExpr max, bool positivePath = true); + } // namespace mlir #endif // MLIR_DIALECT_AFFINE_UTILS_H diff --git a/mlir/lib/Dialect/Affine/Utils/Utils.cpp b/mlir/lib/Dialect/Affine/Utils/Utils.cpp index 844a02f8aa2..c66e111fb9c 100644 --- a/mlir/lib/Dialect/Affine/Utils/Utils.cpp +++ b/mlir/lib/Dialect/Affine/Utils/Utils.cpp @@ -226,3 +226,30 @@ LogicalResult mlir::hoistAffineIfOp(AffineIfOp ifOp, bool *folded) { return success(); } + +// Return the min expr after replacing the given dim. +AffineExpr mlir::substWithMin(AffineExpr e, AffineExpr dim, AffineExpr min, + AffineExpr max, bool positivePath) { + if (e == dim) + return positivePath ? min : max; + if (auto bin = e.dyn_cast()) { + AffineExpr lhs = bin.getLHS(); + AffineExpr rhs = bin.getRHS(); + if (bin.getKind() == mlir::AffineExprKind::Add) + return substWithMin(lhs, dim, min, max, positivePath) + + substWithMin(rhs, dim, min, max, positivePath); + + auto c1 = bin.getLHS().dyn_cast(); + auto c2 = bin.getRHS().dyn_cast(); + if (c1 && c1.getValue() < 0) + return getAffineBinaryOpExpr( + bin.getKind(), c1, substWithMin(rhs, dim, min, max, !positivePath)); + if (c2 && c2.getValue() < 0) + return getAffineBinaryOpExpr( + bin.getKind(), substWithMin(lhs, dim, min, max, !positivePath), c2); + return getAffineBinaryOpExpr( + bin.getKind(), substWithMin(lhs, dim, min, max, positivePath), + substWithMin(rhs, dim, min, max, positivePath)); + } + return e; +} diff --git a/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp b/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp index 5b6302a7e5a..b6171ff9c5b 100644 --- a/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "mlir/Dialect/Linalg/Transforms/Transforms.h" +#include "mlir/Dialect/Affine/Utils.h" #include "mlir/Dialect/Linalg/Analysis/DependenceAnalysis.h" #include "mlir/Dialect/Linalg/IR/LinalgOps.h" #include "mlir/Dialect/Linalg/Utils/Utils.h" @@ -332,38 +333,6 @@ LogicalResult mlir::linalg::applyStagedPatterns( return success(); } -/// Traverse `e` and return an AffineExpr where all occurrences of `dim` have -/// been replaced by either: -/// - `min` if `positivePath` is true when we reach an occurrence of `dim` -/// - `max` if `positivePath` is true when we reach an occurrence of `dim` -/// `positivePath` is negated each time we hit a multiplicative or divisive -/// binary op with a constant negative coefficient. -static AffineExpr substWithMin(AffineExpr e, AffineExpr dim, AffineExpr min, - AffineExpr max, bool positivePath = true) { - if (e == dim) - return positivePath ? min : max; - if (auto bin = e.dyn_cast()) { - AffineExpr lhs = bin.getLHS(); - AffineExpr rhs = bin.getRHS(); - if (bin.getKind() == mlir::AffineExprKind::Add) - return substWithMin(lhs, dim, min, max, positivePath) + - substWithMin(rhs, dim, min, max, positivePath); - - auto c1 = bin.getLHS().dyn_cast(); - auto c2 = bin.getRHS().dyn_cast(); - if (c1 && c1.getValue() < 0) - return getAffineBinaryOpExpr( - bin.getKind(), c1, substWithMin(rhs, dim, min, max, !positivePath)); - if (c2 && c2.getValue() < 0) - return getAffineBinaryOpExpr( - bin.getKind(), substWithMin(lhs, dim, min, max, !positivePath), c2); - return getAffineBinaryOpExpr( - bin.getKind(), substWithMin(lhs, dim, min, max, positivePath), - substWithMin(rhs, dim, min, max, positivePath)); - } - return e; -} - /// Given the `lbVal`, `ubVal` and `stepVal` of a loop, append `lbVal` and /// `ubVal` to `dims` and `stepVal` to `symbols`. /// Create new AffineDimExpr (`%lb` and `%ub`) and AffineSymbolExpr (`%step`)