Skip to content

Commit 911bc26

Browse files
committed
[mlir][arith] fix wrong floordivsi fold (#83079)
Fixs #83079
1 parent 512a8a7 commit 911bc26

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,19 +709,25 @@ OpFoldResult arith::FloorDivSIOp::fold(FoldAdaptor adaptor) {
709709
}
710710
if (!aGtZero && !bGtZero) {
711711
// Both negative, return -a / -b.
712-
APInt posA = zero.ssub_ov(a, overflowOrDiv0);
713-
APInt posB = zero.ssub_ov(b, overflowOrDiv0);
714-
return posA.sdiv_ov(posB, overflowOrDiv0);
712+
return a.sdiv_ov(b, overflowOrDiv0);
715713
}
716714
if (!aGtZero && bGtZero) {
717715
// A is negative, b is positive, return - ceil(-a, b).
718716
APInt posA = zero.ssub_ov(a, overflowOrDiv0);
717+
if (overflowOrDiv0)
718+
return a;
719719
APInt ceil = signedCeilNonnegInputs(posA, b, overflowOrDiv0);
720+
if (overflowOrDiv0)
721+
return a;
720722
return zero.ssub_ov(ceil, overflowOrDiv0);
721723
}
722724
// A is positive, b is negative, return - ceil(a, -b).
723725
APInt posB = zero.ssub_ov(b, overflowOrDiv0);
726+
if (overflowOrDiv0)
727+
return a;
724728
APInt ceil = signedCeilNonnegInputs(a, posB, overflowOrDiv0);
729+
if (overflowOrDiv0)
730+
return a;
725731
return zero.ssub_ov(ceil, overflowOrDiv0);
726732
});
727733

mlir/test/Transforms/canonicalize.mlir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,15 @@ func.func @tensor_arith.floordivsi_by_one(%arg0: tensor<4x5xi32>) -> tensor<4x5x
989989
return %res : tensor<4x5xi32>
990990
}
991991

992+
// CHECK-LABEL: func @arith.floordivsi_by_one_overflow
993+
func.func @arith.floordivsi_by_one_overflow() -> i64 {
994+
%neg_one = arith.constant -1 : i64
995+
%min_int = arith.constant -9223372036854775808 : i64
996+
// CHECK: arith.floordivsi
997+
%poision = arith.floordivsi %min_int, %neg_one : i64
998+
return %poision : i64
999+
}
1000+
9921001
// -----
9931002

9941003
// CHECK-LABEL: func @arith.ceildivsi_by_one

0 commit comments

Comments
 (0)