Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion mlir/lib/Dialect/SCF/IR/SCF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion mlir/test/Dialect/SCF/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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]

// -----

Expand Down
Loading