Skip to content

[mlir][SCF] Allow canonicalization of zero-trip count scf.forall with empty mapping. #105793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
13 changes: 7 additions & 6 deletions mlir/lib/Dialect/SCF/IR/SCF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@ struct ForallOpSingleOrZeroIterationDimsFolder
LogicalResult matchAndRewrite(ForallOp op,
PatternRewriter &rewriter) const override {
// Do not fold dimensions if they are mapped to processing units.
if (op.getMapping().has_value())
if (op.getMapping().has_value() && !op.getMapping()->empty())
return failure();
Location loc = op.getLoc();

Expand Down Expand Up @@ -1729,18 +1729,19 @@ struct ForallOpSingleOrZeroIterationDimsFolder
newMixedUpperBounds.push_back(ub);
newMixedSteps.push_back(step);
}
// Exit if none of the loop dimensions perform a single iteration.
if (newMixedLowerBounds.size() == static_cast<unsigned>(op.getRank())) {
return rewriter.notifyMatchFailure(
op, "no dimensions have 0 or 1 iterations");
}

// All of the loop dimensions perform a single iteration. Inline loop body.
if (newMixedLowerBounds.empty()) {
promote(rewriter, op);
return success();
}

// Exit if none of the loop dimensions perform a single iteration.
if (newMixedLowerBounds.size() == static_cast<unsigned>(op.getRank())) {
return rewriter.notifyMatchFailure(
op, "no dimensions have 0 or 1 iterations");
}

// Replace the loop by a lower-dimensional loop.
ForallOp newOp;
newOp = rewriter.create<ForallOp>(loc, newMixedLowerBounds,
Expand Down
27 changes: 27 additions & 0 deletions mlir/test/Dialect/SCF/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,33 @@ func.func @do_not_inline_distributed_forall_loop(

// -----

func.func @inline_empty_loop_with_empty_mapping(
%in: tensor<16xf32>) -> tensor<16xf32> {
%cst = arith.constant 0.000000e+00 : f32
%0 = tensor.empty() : tensor<16xf32>
%1 = scf.forall () in () shared_outs (%out_ = %0) -> (tensor<16xf32>) {
%slice = tensor.extract_slice %out_[0] [16] [1]
: tensor<16xf32> to tensor<16xf32>
%generic = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]}
ins(%slice : tensor<16xf32>) outs(%0 : tensor<16xf32>) {
^bb0(%b0 : f32, %b1 : f32):
%2 = arith.addf %b0, %b0 : f32
linalg.yield %2 : f32
} -> tensor<16xf32>
scf.forall.in_parallel {
tensor.parallel_insert_slice %generic into %out_[0] [16] [1]
: tensor<16xf32> into tensor<16xf32>
}
}{ mapping = [] }
return %1 : tensor<16xf32>
}
// CHECK-LABEL: func @inline_empty_loop_with_empty_mapping
// CHECK-NOT: scf.forall

// -----

func.func @collapse_one_dim_parallel(%in: tensor<8x8xf32>) -> tensor<8x8xf32> {
%c8 = arith.constant 8 : index
%c0 = arith.constant 0 : index
Expand Down
21 changes: 0 additions & 21 deletions mlir/test/Dialect/Tensor/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2076,27 +2076,6 @@ func.func @canonicalize_parallel_insert_slice_indices(

// -----

// CHECK-LABEL: func.func @dont_fold_parallel_insert_slice(
// CHECK-SAME: %[[arg0:[0-9a-z]*]]: tensor<1x5xf32>,
// CHECK-SAME: %[[arg1:[0-9a-z]*]]: tensor<1x5xf32>)
func.func @dont_fold_parallel_insert_slice(
%arg0 : tensor<1x5xf32>, %arg1: tensor<1x5xf32>) -> tensor<1x5xf32>
{
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
// CHECK: scf.forall () in () shared_outs(%[[o:.*]] = %[[arg1]]) -> (tensor<1x5xf32>) {
// CHECK-NEXT: scf.forall.in_parallel {
// CHECK-NEXT: tensor.parallel_insert_slice %[[arg0]] into %[[o]][0, 0] [1, 5] [1, 1] : tensor<1x5xf32> into tensor<1x5xf32>
%2 = scf.forall () in () shared_outs(%o = %arg1) -> (tensor<1x5xf32>) {
scf.forall.in_parallel {
tensor.parallel_insert_slice %arg0 into %o[%c0, %c0] [1, 5] [%c1, %c1] : tensor<1x5xf32> into tensor<1x5xf32>
}
}
return %2 : tensor<1x5xf32>
}

// -----

// CHECK-LABEL: func.func @fold_insert_slice_after_extract_slice
// CHECK-SAME: (%[[INPUT:.+]]: tensor<1x2x2x4xf32>)
func.func @fold_insert_slice_after_extract_slice(%input: tensor<1x2x2x4xf32>) -> tensor<1x2x2x4xf32> {
Expand Down
Loading