[Mlir-commits] [mlir] 1ae2446 - [mlir] Add forall canonicalization to replace constant induction vars (#112764)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Oct 18 12:21:04 PDT 2024
Author: Max191
Date: 2024-10-18T15:21:01-04:00
New Revision: 1ae24460d21577858d034fd4f77f2a986ac062a9
URL: https://github.com/llvm/llvm-project/commit/1ae24460d21577858d034fd4f77f2a986ac062a9
DIFF: https://github.com/llvm/llvm-project/commit/1ae24460d21577858d034fd4f77f2a986ac062a9.diff
LOG: [mlir] Add forall canonicalization to replace constant induction vars (#112764)
Adds a canonicalization pattern for scf.forall that replaces constant
induction variables with a constant index. There is a similar
canonicalization that completely removes constant induction variables
from the loop, but that pattern does not apply on foralls with mappings,
so this one is necessary for those cases.
---------
Signed-off-by: Max Dawkins <max.dawkins at gmail.com>
Added:
Modified:
mlir/lib/Dialect/SCF/IR/SCF.cpp
mlir/test/Dialect/SCF/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/SCF/IR/SCF.cpp b/mlir/lib/Dialect/SCF/IR/SCF.cpp
index 2582d4e0df1920..6678878215c11f 100644
--- a/mlir/lib/Dialect/SCF/IR/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/IR/SCF.cpp
@@ -1767,6 +1767,31 @@ struct ForallOpSingleOrZeroIterationDimsFolder
}
};
+/// Replace all induction vars with a single trip count with their lower bound.
+struct ForallOpReplaceConstantInductionVar : public OpRewritePattern<ForallOp> {
+ using OpRewritePattern<ForallOp>::OpRewritePattern;
+
+ LogicalResult matchAndRewrite(ForallOp op,
+ PatternRewriter &rewriter) const override {
+ Location loc = op.getLoc();
+ bool changed = false;
+ for (auto [lb, ub, step, iv] :
+ llvm::zip(op.getMixedLowerBound(), op.getMixedUpperBound(),
+ op.getMixedStep(), op.getInductionVars())) {
+ if (iv.getUses().begin() == iv.getUses().end())
+ continue;
+ auto numIterations = constantTripCount(lb, ub, step);
+ if (!numIterations.has_value() || numIterations.value() != 1) {
+ continue;
+ }
+ rewriter.replaceAllUsesWith(
+ iv, getValueOrCreateConstantIndexOp(rewriter, loc, lb));
+ changed = true;
+ }
+ return success(changed);
+ }
+};
+
struct FoldTensorCastOfOutputIntoForallOp
: public OpRewritePattern<scf::ForallOp> {
using OpRewritePattern<scf::ForallOp>::OpRewritePattern;
@@ -1851,7 +1876,8 @@ void ForallOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<DimOfForallOp, FoldTensorCastOfOutputIntoForallOp,
ForallOpControlOperandsFolder, ForallOpIterArgsFolder,
- ForallOpSingleOrZeroIterationDimsFolder>(context);
+ ForallOpSingleOrZeroIterationDimsFolder,
+ ForallOpReplaceConstantInductionVar>(context);
}
/// Given the region at `index`, or the parent operation if `index` is None,
diff --git a/mlir/test/Dialect/SCF/canonicalize.mlir b/mlir/test/Dialect/SCF/canonicalize.mlir
index c68369a8e4fce7..8c4e7a41ee6bc4 100644
--- a/mlir/test/Dialect/SCF/canonicalize.mlir
+++ b/mlir/test/Dialect/SCF/canonicalize.mlir
@@ -1617,7 +1617,7 @@ func.func @do_not_inline_distributed_forall_loop(
%in: tensor<8x8xf32>) -> tensor<8x8xf32> {
%cst = arith.constant 0.000000e+00 : f32
%0 = tensor.empty() : tensor<8x8xf32>
- %1 = scf.forall (%i, %j) = (0, 0) to (1, 1) step (8, 8)
+ %1 = scf.forall (%i, %j) = (0, 4) to (1, 5) step (8, 8)
shared_outs (%out_ = %0) -> (tensor<8x8xf32>) {
%slice = tensor.extract_slice %out_[%i, %j] [2, 3] [1, 1]
: tensor<8x8xf32> to tensor<2x3xf32>
@@ -1632,6 +1632,8 @@ func.func @do_not_inline_distributed_forall_loop(
}
// CHECK-LABEL: @do_not_inline_distributed_forall_loop
// CHECK: scf.forall
+// CHECK: tensor.extract_slice %{{.*}}[0, 4] [2, 3] [1, 1]
+// CHECK: tensor.parallel_insert_slice %{{.*}}[0, 4] [2, 3] [1, 1]
// -----
More information about the Mlir-commits
mailing list