[Mlir-commits] [mlir] [mlir][scf]: Expose emitNormalizedLoopBounds/denormalizeInductionVariable util functions (NFC) (PR #94429)
Aviad Cohen
llvmlistbot at llvm.org
Fri Jun 7 03:48:21 PDT 2024
https://github.com/AviadCo updated https://github.com/llvm/llvm-project/pull/94429
>From 34feac8c499652ac5317537b62e41a1e36169c92 Mon Sep 17 00:00:00 2001
From: Aviad Cohen <aviad.cohen2 at mobileye.com>
Date: Wed, 5 Jun 2024 08:07:16 +0300
Subject: [PATCH] [mlir][scf]: Expose
emitNormalizedLoopBounds/denormalizeInductionVariable util functions
* Also updated normarlize/denormalize loop bounds to be folded if
possible.
---
mlir/include/mlir/Dialect/SCF/Utils/Utils.h | 25 ++++++++
mlir/lib/Dialect/SCF/Utils/Utils.cpp | 58 +++++++------------
mlir/test/Dialect/Affine/loop-coalescing.mlir | 15 ++---
3 files changed, 51 insertions(+), 47 deletions(-)
diff --git a/mlir/include/mlir/Dialect/SCF/Utils/Utils.h b/mlir/include/mlir/Dialect/SCF/Utils/Utils.h
index bc09cc7f7fa5e..8eb4bd6bceff1 100644
--- a/mlir/include/mlir/Dialect/SCF/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/SCF/Utils/Utils.h
@@ -120,6 +120,31 @@ LogicalResult loopUnrollByFactor(
scf::ForOp forOp, uint64_t unrollFactor,
function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr);
+/// This structure is to pass and return sets of loop parameters without
+/// confusing the order.
+struct LoopParams {
+ OpFoldResult lowerBound;
+ OpFoldResult upperBound;
+ OpFoldResult step;
+};
+
+/// Transform a loop with a strictly positive step
+/// for %i = %lb to %ub step %s
+/// into a 0-based loop with step 1
+/// for %ii = 0 to ceildiv(%ub - %lb, %s) step 1 {
+/// %i = %ii * %s + %lb
+/// Insert the induction variable remapping in the body of `inner`, which is
+/// expected to be either `loop` or another loop perfectly nested under `loop`.
+/// Insert the definition of new bounds immediate before `outer`, which is
+/// expected to be either `loop` or its parent in the loop nest.
+LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
+ Value lb, Value ub, Value step);
+
+/// Get back the original induction variable values after loop normalization.
+void denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
+ Value normalizedIv, Value origLb,
+ Value origStep);
+
/// Tile a nest of standard for loops rooted at `rootForOp` by finding such
/// parametric tile sizes that the outer loops have a fixed number of iterations
/// as defined in `sizes`.
diff --git a/mlir/lib/Dialect/SCF/Utils/Utils.cpp b/mlir/lib/Dialect/SCF/Utils/Utils.cpp
index 6658cca03eba7..7651d1cde0731 100644
--- a/mlir/lib/Dialect/SCF/Utils/Utils.cpp
+++ b/mlir/lib/Dialect/SCF/Utils/Utils.cpp
@@ -18,6 +18,7 @@
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/IRMapping.h"
+#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "mlir/Support/MathExtras.h"
@@ -29,16 +30,6 @@
using namespace mlir;
-namespace {
-// This structure is to pass and return sets of loop parameters without
-// confusing the order.
-struct LoopParams {
- Value lowerBound;
- Value upperBound;
- Value step;
-};
-} // namespace
-
SmallVector<scf::ForOp> mlir::replaceLoopNestWithNewYields(
RewriterBase &rewriter, MutableArrayRef<scf::ForOp> loopNest,
ValueRange newIterOperands, const NewYieldValuesFn &newYieldValuesFn,
@@ -473,17 +464,8 @@ LogicalResult mlir::loopUnrollByFactor(
return success();
}
-/// Transform a loop with a strictly positive step
-/// for %i = %lb to %ub step %s
-/// into a 0-based loop with step 1
-/// for %ii = 0 to ceildiv(%ub - %lb, %s) step 1 {
-/// %i = %ii * %s + %lb
-/// Insert the induction variable remapping in the body of `inner`, which is
-/// expected to be either `loop` or another loop perfectly nested under `loop`.
-/// Insert the definition of new bounds immediate before `outer`, which is
-/// expected to be either `loop` or its parent in the loop nest.
-static LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
- Value lb, Value ub, Value step) {
+LoopParams mlir::emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
+ Value lb, Value ub, Value step) {
// For non-index types, generate `arith` instructions
// Check if the loop is already known to have a constant zero lower bound or
// a constant one step.
@@ -501,26 +483,27 @@ static LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
if (isZeroBased && isStepOne)
return {lb, ub, step};
- Value diff = isZeroBased ? ub : rewriter.create<arith::SubIOp>(loc, ub, lb);
+ Value diff =
+ isZeroBased ? ub : rewriter.createOrFold<arith::SubIOp>(loc, ub, lb);
Value newUpperBound =
- isStepOne ? diff : rewriter.create<arith::CeilDivSIOp>(loc, diff, step);
+ isStepOne ? diff
+ : rewriter.createOrFold<arith::CeilDivSIOp>(loc, diff, step);
Value newLowerBound = isZeroBased
? lb
- : rewriter.create<arith::ConstantOp>(
+ : rewriter.createOrFold<arith::ConstantOp>(
loc, rewriter.getZeroAttr(lb.getType()));
Value newStep = isStepOne
? step
- : rewriter.create<arith::ConstantOp>(
+ : rewriter.createOrFold<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(step.getType(), 1));
return {newLowerBound, newUpperBound, newStep};
}
-/// Get back the original induction variable values after loop normalization
-static void denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
- Value normalizedIv, Value origLb,
- Value origStep) {
+void mlir::denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
+ Value normalizedIv, Value origLb,
+ Value origStep) {
Value denormalizedIv;
SmallPtrSet<Operation *, 2> preserve;
bool isStepOne = isConstantIntValue(origStep, 1);
@@ -638,9 +621,12 @@ LogicalResult mlir::coalesceLoops(RewriterBase &rewriter,
emitNormalizedLoopBounds(rewriter, loop.getLoc(), lb, ub, step);
rewriter.modifyOpInPlace(loop, [&]() {
- loop.setLowerBound(newLoopParams.lowerBound);
- loop.setUpperBound(newLoopParams.upperBound);
- loop.setStep(newLoopParams.step);
+ loop.setLowerBound(getValueOrCreateConstantIndexOp(
+ rewriter, loop.getLoc(), newLoopParams.lowerBound));
+ loop.setUpperBound(getValueOrCreateConstantIndexOp(
+ rewriter, loop.getLoc(), newLoopParams.upperBound));
+ loop.setStep(getValueOrCreateConstantIndexOp(rewriter, loop.getLoc(),
+ newLoopParams.step));
});
rewriter.setInsertionPointToStart(innermost.getBody());
@@ -778,8 +764,7 @@ void mlir::collapseParallelLoops(
llvm::sort(dims);
// Normalize ParallelOp's iteration pattern.
- SmallVector<Value, 3> normalizedLowerBounds, normalizedSteps,
- normalizedUpperBounds;
+ SmallVector<Value, 3> normalizedUpperBounds;
for (unsigned i = 0, e = loops.getNumLoops(); i < e; ++i) {
OpBuilder::InsertionGuard g2(rewriter);
rewriter.setInsertionPoint(loops);
@@ -787,9 +772,8 @@ void mlir::collapseParallelLoops(
Value ub = loops.getUpperBound()[i];
Value step = loops.getStep()[i];
auto newLoopParams = emitNormalizedLoopBounds(rewriter, loc, lb, ub, step);
- normalizedLowerBounds.push_back(newLoopParams.lowerBound);
- normalizedUpperBounds.push_back(newLoopParams.upperBound);
- normalizedSteps.push_back(newLoopParams.step);
+ normalizedUpperBounds.push_back(getValueOrCreateConstantIndexOp(
+ rewriter, loops.getLoc(), newLoopParams.upperBound));
rewriter.setInsertionPointToStart(loops.getBody());
denormalizeInductionVariable(rewriter, loc, loops.getInductionVars()[i], lb,
diff --git a/mlir/test/Dialect/Affine/loop-coalescing.mlir b/mlir/test/Dialect/Affine/loop-coalescing.mlir
index ae0adf5a0a02d..0235000aeac53 100644
--- a/mlir/test/Dialect/Affine/loop-coalescing.mlir
+++ b/mlir/test/Dialect/Affine/loop-coalescing.mlir
@@ -74,11 +74,10 @@ func.func @multi_use() {
func.func @unnormalized_loops() {
// CHECK: %[[orig_step_i:.*]] = arith.constant 2
- // CHECK: %[[orig_step_j:.*]] = arith.constant 3
+
+ // CHECK: %[[orig_step_j_and_numiter_i:.*]] = arith.constant 3
// CHECK: %[[orig_lb_i:.*]] = arith.constant 5
// CHECK: %[[orig_lb_j:.*]] = arith.constant 7
- // CHECK: %[[orig_ub_i:.*]] = arith.constant 10
- // CHECK: %[[orig_ub_j:.*]] = arith.constant 17
%c2 = arith.constant 2 : index
%c3 = arith.constant 3 : index
%c5 = arith.constant 5 : index
@@ -86,20 +85,16 @@ func.func @unnormalized_loops() {
%c10 = arith.constant 10 : index
%c17 = arith.constant 17 : index
- // Number of iterations in the outer scf.
- // CHECK: %[[diff_i:.*]] = arith.subi %[[orig_ub_i]], %[[orig_lb_i]]
- // CHECK: %[[numiter_i:.*]] = arith.ceildivsi %[[diff_i]], %[[orig_step_i]]
-
// Normalized lower bound and step for the outer scf.
// CHECK: %[[lb_i:.*]] = arith.constant 0
// CHECK: %[[step_i:.*]] = arith.constant 1
// Number of iterations in the inner loop, the pattern is the same as above,
// only capture the final result.
- // CHECK: %[[numiter_j:.*]] = arith.ceildivsi {{.*}}, %[[orig_step_j]]
+ // CHECK: %[[numiter_j:.*]] = arith.constant 4
// New bounds of the outer scf.
- // CHECK: %[[range:.*]] = arith.muli %[[numiter_i]], %[[numiter_j]]
+ // CHECK: %[[range:.*]] = arith.muli %[[orig_step_j_and_numiter_i:.*]], %[[numiter_j]]
// CHECK: scf.for %[[i:.*]] = %[[lb_i]] to %[[range]] step %[[step_i]]
scf.for %i = %c5 to %c10 step %c2 {
// The inner loop has been removed.
@@ -108,7 +103,7 @@ func.func @unnormalized_loops() {
// The IVs are rewritten.
// CHECK: %[[normalized_j:.*]] = arith.remsi %[[i]], %[[numiter_j]]
// CHECK: %[[normalized_i:.*]] = arith.divsi %[[i]], %[[numiter_j]]
- // CHECK: %[[scaled_j:.*]] = arith.muli %[[normalized_j]], %[[orig_step_j]]
+ // CHECK: %[[scaled_j:.*]] = arith.muli %[[normalized_j]], %[[orig_step_j_and_numiter_i]]
// CHECK: %[[orig_j:.*]] = arith.addi %[[scaled_j]], %[[orig_lb_j]]
// CHECK: %[[scaled_i:.*]] = arith.muli %[[normalized_i]], %[[orig_step_i]]
// CHECK: %[[orig_i:.*]] = arith.addi %[[scaled_i]], %[[orig_lb_i]]
More information about the Mlir-commits
mailing list