[mlir][Arith] Make integer max/min commutative.

Make MaxSI, MaxUI, MinSI and MinUI commutative, so they will be canonicalized to have its constants appear as the second operand. And the constant folder will match more cases.

Differential Revision: https://reviews.llvm.org/D122225
This commit is contained in:
jacquesguan 2022-03-22 21:00:33 +08:00
parent b7a4b67380
commit 75f0d12ebf
2 changed files with 68 additions and 4 deletions

View file

@ -679,7 +679,7 @@ def Arith_MaxFOp : Arith_FloatBinaryOp<"maxf", [Commutative]> {
// MaxSIOp
//===----------------------------------------------------------------------===//
def Arith_MaxSIOp : Arith_IntBinaryOp<"maxsi"> {
def Arith_MaxSIOp : Arith_IntBinaryOp<"maxsi", [Commutative]> {
let summary = "signed integer maximum operation";
let hasFolder = 1;
}
@ -688,7 +688,7 @@ def Arith_MaxSIOp : Arith_IntBinaryOp<"maxsi"> {
// MaxUIOp
//===----------------------------------------------------------------------===//
def Arith_MaxUIOp : Arith_IntBinaryOp<"maxui"> {
def Arith_MaxUIOp : Arith_IntBinaryOp<"maxui", [Commutative]> {
let summary = "unsigned integer maximum operation";
let hasFolder = 1;
}
@ -723,7 +723,7 @@ def Arith_MinFOp : Arith_FloatBinaryOp<"minf", [Commutative]> {
// MinSIOp
//===----------------------------------------------------------------------===//
def Arith_MinSIOp : Arith_IntBinaryOp<"minsi"> {
def Arith_MinSIOp : Arith_IntBinaryOp<"minsi", [Commutative]> {
let summary = "signed integer minimum operation";
let hasFolder = 1;
}
@ -732,7 +732,7 @@ def Arith_MinSIOp : Arith_IntBinaryOp<"minsi"> {
// MinUIOp
//===----------------------------------------------------------------------===//
def Arith_MinUIOp : Arith_IntBinaryOp<"minui"> {
def Arith_MinUIOp : Arith_IntBinaryOp<"minui", [Commutative]> {
let summary = "unsigned integer minimum operation";
let hasFolder = 1;
}

View file

@ -682,6 +682,22 @@ func @test_maxsi(%arg0 : i8) -> (i8, i8, i8, i8) {
return %0, %1, %2, %3: i8, i8, i8, i8
}
// CHECK-LABEL: test_maxsi2
// CHECK: %[[C0:.+]] = arith.constant 42
// CHECK: %[[MAX_INT_CST:.+]] = arith.constant 127
// CHECK: %[[X:.+]] = arith.maxsi %arg0, %[[C0]]
// CHECK: return %arg0, %[[MAX_INT_CST]], %arg0, %[[X]]
func @test_maxsi2(%arg0 : i8) -> (i8, i8, i8, i8) {
%maxIntCst = arith.constant 127 : i8
%minIntCst = arith.constant -128 : i8
%c0 = arith.constant 42 : i8
%0 = arith.maxsi %arg0, %arg0 : i8
%1 = arith.maxsi %maxIntCst, %arg0: i8
%2 = arith.maxsi %minIntCst, %arg0: i8
%3 = arith.maxsi %c0, %arg0 : i8
return %0, %1, %2, %3: i8, i8, i8, i8
}
// -----
// CHECK-LABEL: test_maxui
@ -700,6 +716,22 @@ func @test_maxui(%arg0 : i8) -> (i8, i8, i8, i8) {
return %0, %1, %2, %3: i8, i8, i8, i8
}
// CHECK-LABEL: test_maxui
// CHECK: %[[C0:.+]] = arith.constant 42
// CHECK: %[[MAX_INT_CST:.+]] = arith.constant -1
// CHECK: %[[X:.+]] = arith.maxui %arg0, %[[C0]]
// CHECK: return %arg0, %[[MAX_INT_CST]], %arg0, %[[X]]
func @test_maxui2(%arg0 : i8) -> (i8, i8, i8, i8) {
%maxIntCst = arith.constant 255 : i8
%minIntCst = arith.constant 0 : i8
%c0 = arith.constant 42 : i8
%0 = arith.maxui %arg0, %arg0 : i8
%1 = arith.maxui %maxIntCst, %arg0 : i8
%2 = arith.maxui %minIntCst, %arg0 : i8
%3 = arith.maxui %c0, %arg0 : i8
return %0, %1, %2, %3: i8, i8, i8, i8
}
// -----
// CHECK-LABEL: test_minsi
@ -718,6 +750,22 @@ func @test_minsi(%arg0 : i8) -> (i8, i8, i8, i8) {
return %0, %1, %2, %3: i8, i8, i8, i8
}
// CHECK-LABEL: test_minsi
// CHECK: %[[C0:.+]] = arith.constant 42
// CHECK: %[[MIN_INT_CST:.+]] = arith.constant -128
// CHECK: %[[X:.+]] = arith.minsi %arg0, %[[C0]]
// CHECK: return %arg0, %arg0, %[[MIN_INT_CST]], %[[X]]
func @test_minsi2(%arg0 : i8) -> (i8, i8, i8, i8) {
%maxIntCst = arith.constant 127 : i8
%minIntCst = arith.constant -128 : i8
%c0 = arith.constant 42 : i8
%0 = arith.minsi %arg0, %arg0 : i8
%1 = arith.minsi %maxIntCst, %arg0 : i8
%2 = arith.minsi %minIntCst, %arg0 : i8
%3 = arith.minsi %c0, %arg0 : i8
return %0, %1, %2, %3: i8, i8, i8, i8
}
// -----
// CHECK-LABEL: test_minui
@ -736,6 +784,22 @@ func @test_minui(%arg0 : i8) -> (i8, i8, i8, i8) {
return %0, %1, %2, %3: i8, i8, i8, i8
}
// CHECK-LABEL: test_minui
// CHECK: %[[C0:.+]] = arith.constant 42
// CHECK: %[[MIN_INT_CST:.+]] = arith.constant 0
// CHECK: %[[X:.+]] = arith.minui %arg0, %[[C0]]
// CHECK: return %arg0, %arg0, %[[MIN_INT_CST]], %[[X]]
func @test_minui2(%arg0 : i8) -> (i8, i8, i8, i8) {
%maxIntCst = arith.constant 255 : i8
%minIntCst = arith.constant 0 : i8
%c0 = arith.constant 42 : i8
%0 = arith.minui %arg0, %arg0 : i8
%1 = arith.minui %maxIntCst, %arg0 : i8
%2 = arith.minui %minIntCst, %arg0 : i8
%3 = arith.minui %c0, %arg0 : i8
return %0, %1, %2, %3: i8, i8, i8, i8
}
// -----
// CHECK-LABEL: @test_minf(