Skip to content

[mlir][scf] Fix for-loop-peeling crash #77697

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
merged 2 commits into from
Jan 12, 2024
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
5 changes: 3 additions & 2 deletions mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ static LogicalResult peelForLoop(RewriterBase &b, ForOp forOp,
auto ubInt = getConstantIntValue(forOp.getUpperBound());
auto stepInt = getConstantIntValue(forOp.getStep());

// No specialization necessary if step size is 1.
if (getConstantIntValue(forOp.getStep()) == static_cast<int64_t>(1))
// No specialization necessary if step size is 1. Also bail out in case of an
// invalid zero or negative step which might have happened during folding.
if (stepInt && *stepInt <= 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I thought that negative step sizes were allowed, but the documentations says otherwise: The step is a value of same type but required to be positive.

return failure();

// No specialization necessary if step already divides upper bound evenly.
Expand Down
19 changes: 17 additions & 2 deletions mlir/test/Dialect/SCF/for-loop-peeling.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: mlir-opt %s -scf-for-loop-peeling -canonicalize -split-input-file | FileCheck %s
// RUN: mlir-opt %s -scf-for-loop-peeling=skip-partial=false -canonicalize -split-input-file | FileCheck %s -check-prefix=CHECK-NO-SKIP
// RUN: mlir-opt %s -scf-for-loop-peeling -canonicalize -split-input-file -verify-diagnostics | FileCheck %s
// RUN: mlir-opt %s -scf-for-loop-peeling=skip-partial=false -canonicalize -split-input-file -verify-diagnostics | FileCheck %s -check-prefix=CHECK-NO-SKIP

// CHECK-DAG: #[[MAP0:.*]] = affine_map<()[s0, s1, s2] -> (s1 - (-s0 + s1) mod s2)>
// CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0)[s0] -> (-d0 + s0)>
Expand Down Expand Up @@ -289,3 +289,18 @@ func.func @regression(%arg0: memref<i64>, %arg1: index) {
}
return
}

// -----

// Check that this doesn't crash but trigger the verifier.
func.func @zero_step(%arg0: memref<i64>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%foldto0 = arith.subi %c1, %c1 : index
// expected-error @+1 {{'scf.for' op constant step operand must be positive}}
scf.for %arg2 = %c0 to %c1 step %foldto0 {
%2 = arith.index_cast %arg2 : index to i64
memref.store %2, %arg0[] : memref<i64>
}
return
}