Skip to content

[mlir][arith] Add constraints to the MulIOp for preventing type mismatch while folding #136093

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 1 commit into from
Apr 17, 2025
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
4 changes: 3 additions & 1 deletion mlir/lib/Dialect/Arith/IR/ArithCanonicalization.td
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def MulIMulIConstant :
(Arith_MulIOp $x, (ConstantLikeMatcher APIntAttr:$c0), $ovf1),
(ConstantLikeMatcher APIntAttr:$c1), $ovf2),
(Arith_MulIOp $x, (Arith_ConstantOp (MulIntAttrs $res, $c0, $c1)),
(MergeOverflow $ovf1, $ovf2))>;
(MergeOverflow $ovf1, $ovf2)),
[(Constraint<CPred<"$0.getType() == cast<IntegerAttr>($1).getType()">> $res, $c0),
(Constraint<CPred<"$0.getType() == cast<IntegerAttr>($1).getType()">> $res, $c1)]>;

//===----------------------------------------------------------------------===//
// AddUIExtendedOp
Expand Down
18 changes: 18 additions & 0 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,24 @@ func.func @doubleAddSub2(%arg0: index, %arg1 : index) -> index {
return %add : index
}

// Negative test case to ensure no further folding is performed when there's a type mismatch between the values and the result.
// CHECK-LABEL: func.func @nested_muli() -> i32 {
// CHECK: %[[VAL_0:.*]] = "test.constant"() <{value = 2147483647 : i64}> : () -> i32
// CHECK: %[[VAL_1:.*]] = "test.constant"() <{value = -2147483648 : i64}> : () -> i32
// CHECK: %[[VAL_2:.*]] = "test.constant"() <{value = 2147483648 : i64}> : () -> i32
// CHECK: %[[VAL_3:.*]] = arith.muli %[[VAL_0]], %[[VAL_1]] : i32
// CHECK: %[[VAL_4:.*]] = arith.muli %[[VAL_3]], %[[VAL_2]] : i32
// CHECK: return %[[VAL_4]] : i32
// CHECK: }
func.func @nested_muli() -> (i32) {
%0 = "test.constant"() {value = 0x7fffffff} : () -> i32
%1 = "test.constant"() {value = -2147483648} : () -> i32
%2 = "test.constant"() {value = 0x80000000} : () -> i32
%4 = arith.muli %0, %1 : i32
%5 = arith.muli %4, %2 : i32
return %5 : i32
}

// CHECK-LABEL: @tripleMulIMulIIndex
// CHECK: %[[cres:.+]] = arith.constant 15 : index
// CHECK: %[[muli:.+]] = arith.muli %arg0, %[[cres]] : index
Expand Down
Loading