Skip to content

[mlir][arith] Fix multiplication canonicalizations #144787

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

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 11 additions & 3 deletions mlir/lib/Dialect/Arith/IR/ArithCanonicalization.td
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def RedundantSelectFalse :
Pat<(SelectOp $pred, $a, (SelectOp $pred, $b, $c)),
(SelectOp $pred, $a, $c)>;

// select(pred, false, true) => not(pred)
// select(pred, false, true) => not(pred)
def SelectI1ToNot :
Pat<(SelectOp $pred,
(ConstantLikeMatcher ConstantAttr<I1Attr, "0">),
Expand Down Expand Up @@ -376,6 +376,12 @@ def TruncationMatchesShiftAmount :
CPred<"(getScalarOrElementWidth($0) - getScalarOrElementWidth($1)) == "
"*getIntOrSplatIntValue($2)">]>>;

def ValueWidthMatchesShiftAmount :
Constraint<And<[
CPred<"succeeded(getIntOrSplatIntValue($1))">,
CPred<"getScalarOrElementWidth($0) == "
"*getIntOrSplatIntValue($1)">]>>;

// trunci(extsi(x)) -> extsi(x), when only the sign-extension bits are truncated
def TruncIExtSIToExtSI :
Pat<(Arith_TruncIOp:$tr (Arith_ExtSIOp:$ext $x)),
Expand Down Expand Up @@ -406,7 +412,8 @@ def TruncIShrUIMulIToMulSIExtended :
(Arith_MulSIExtendedOp:$res__1 $x, $y),
[(ValuesWithSameType $tr, $x, $y),
(ValueWiderThan $mul, $x),
(TruncationMatchesShiftAmount $mul, $x, $c0)]>;
(TruncationMatchesShiftAmount $mul, $x, $c0),
(ValueWidthMatchesShiftAmount $x, $c0)]>;

// trunci(shrui(mul(zext(x), zext(y)), c)) -> mului_extended(x, y)
def TruncIShrUIMulIToMulUIExtended :
Expand All @@ -417,7 +424,8 @@ def TruncIShrUIMulIToMulUIExtended :
(Arith_MulUIExtendedOp:$res__1 $x, $y),
[(ValuesWithSameType $tr, $x, $y),
(ValueWiderThan $mul, $x),
(TruncationMatchesShiftAmount $mul, $x, $c0)]>;
(TruncationMatchesShiftAmount $mul, $x, $c0),
(ValueWidthMatchesShiftAmount $x, $c0)]>;

//===----------------------------------------------------------------------===//
// TruncIOp
Expand Down
32 changes: 31 additions & 1 deletion mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ func.func @tripleAddAddOvf2(%arg0: index) -> index {


// CHECK-LABEL: @foldSubXX_tensor
// CHECK: %[[c0:.+]] = arith.constant dense<0> : tensor<10xi32>
// CHECK: %[[c0:.+]] = arith.constant dense<0> : tensor<10xi32>
// CHECK: %[[sub:.+]] = arith.subi
// CHECK: return %[[c0]], %[[sub]]
func.func @foldSubXX_tensor(%static : tensor<10xi32>, %dyn : tensor<?x?xi32>) -> (tensor<10xi32>, tensor<?x?xi32>) {
Expand Down Expand Up @@ -2966,6 +2966,21 @@ func.func @wideMulToMulSIExtended(%a: i32, %b: i32) -> i32 {
return %hi : i32
}

// Verify that the signed extended multiplication pattern does not match
// if the right shift does not match the bitwidth of the multipliers.

// CHECK-LABEL: @wideMulToMulSIExtendedWithWrongShift
// CHECK-NOT: arith.mulsi_extended
func.func @wideMulToMulSIExtendedWithWrongShift(%a: i32, %b: i32) -> i32 {
%x = arith.extsi %a: i32 to i33
%y = arith.extsi %b: i32 to i33
%m = arith.muli %x, %y: i33
%c1 = arith.constant 1: i33
%sh = arith.shrui %m, %c1 : i33
%hi = arith.trunci %sh: i33 to i32
return %hi : i32
}

// CHECK-LABEL: @wideMulToMulSIExtendedVector
// CHECK-SAME: (%[[A:.+]]: vector<3xi32>, %[[B:.+]]: vector<3xi32>)
// CHECK-NEXT: %[[LOW:.+]], %[[HIGH:.+]] = arith.mulsi_extended %[[A]], %[[B]] : vector<3xi32>
Expand Down Expand Up @@ -2994,6 +3009,21 @@ func.func @wideMulToMulUIExtended(%a: i32, %b: i32) -> i32 {
return %hi : i32
}

// Verify that the unsigned extended multiplication pattern does not match
// if the right shift does not match the bitwidth of the multipliers.

// CHECK-LABEL: @wideMulToMulUIExtendedWithWrongShift
// CHECK-NOT: arith.mului_extended
func.func @wideMulToMulUIExtendedWithWrongShift(%a: i32, %b: i32) -> i32 {
%x = arith.extui %a: i32 to i33
%y = arith.extui %b: i32 to i33
%m = arith.muli %x, %y: i33
%c1 = arith.constant 1: i33
%sh = arith.shrui %m, %c1 : i33
%hi = arith.trunci %sh: i33 to i32
return %hi : i32
}

// CHECK-LABEL: @wideMulToMulUIExtendedVector
// CHECK-SAME: (%[[A:.+]]: vector<3xi32>, %[[B:.+]]: vector<3xi32>)
// CHECK-NEXT: %[[LOW:.+]], %[[HIGH:.+]] = arith.mului_extended %[[A]], %[[B]] : vector<3xi32>
Expand Down
Loading