Skip to content

Commit 469ab15

Browse files
committed
[MLIR] Fixes arith.sub folder crash on dynamically shaped tensors
We can't create a constant for a value with dynamic shape. Fixes #118772
1 parent 9ccde12 commit 469ab15

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

mlir/lib/Dialect/Arith/IR/ArithOps.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,12 @@ void arith::AddUIExtendedOp::getCanonicalizationPatterns(
393393

394394
OpFoldResult arith::SubIOp::fold(FoldAdaptor adaptor) {
395395
// subi(x,x) -> 0
396-
if (getOperand(0) == getOperand(1))
397-
return Builder(getContext()).getZeroAttr(getType());
396+
if (getOperand(0) == getOperand(1)) {
397+
auto tensorType = dyn_cast<TensorType>(getType());
398+
// We can't generate a constant with a dynamic shaped tensor.
399+
if (!tensorType || tensorType.hasStaticShape())
400+
return Builder(getContext()).getZeroAttr(getType());
401+
}
398402
// subi(x,0) -> x
399403
if (matchPattern(adaptor.getRhs(), m_Zero()))
400404
return getLhs();

mlir/test/Dialect/Arith/canonicalize.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,17 @@ func.func @tripleAddAddOvf2(%arg0: index) -> index {
869869
return %add2 : index
870870
}
871871

872+
873+
// CHECK-LABEL: @foldSubXX
874+
// CHECK: %[[c0:.+]] = arith.constant dense<0> : tensor<10xi32>
875+
// CHECK: %[[sub:.+]] = arith.subi
876+
// CHECK: return %[[c0]], %[[sub]]
877+
func.func @foldSubXX(%dyn : tensor<?x?xi32>, %static : tensor<10xi32>) -> (tensor<10xi32>, tensor<?x?xi32>) {
878+
%static_sub = arith.subi %static, %static : tensor<10xi32>
879+
%dyn_sub = arith.subi %dyn, %dyn : tensor<?x?xi32>
880+
return %static_sub, %dyn_sub : tensor<10xi32>, tensor<?x?xi32>
881+
}
882+
872883
// CHECK-LABEL: @tripleAddSub0
873884
// CHECK: %[[cres:.+]] = arith.constant 59 : index
874885
// CHECK: %[[add:.+]] = arith.subi %[[cres]], %arg0 : index

0 commit comments

Comments
 (0)