Skip to content

[mlir][arith] Canonicalization patterns for arith.select #67809

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
Oct 9, 2023
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
44 changes: 44 additions & 0 deletions mlir/lib/Dialect/Arith/IR/ArithCanonicalization.td
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,50 @@ def CmpIExtUI :
CPred<"$0.getValue() == arith::CmpIPredicate::eq || "
"$0.getValue() == arith::CmpIPredicate::ne">> $pred)]>;

//===----------------------------------------------------------------------===//
// SelectOp
//===----------------------------------------------------------------------===//

// select(not(pred), a, b) => select(pred, b, a)
def SelectNotCond :
Pat<(SelectOp (Arith_XOrIOp $pred, (ConstantLikeMatcher APIntAttr:$ones)), $a, $b),
(SelectOp $pred, $b, $a),
[(IsScalarOrSplatNegativeOne $ones)]>;

// select(pred, select(pred, a, b), c) => select(pred, a, c)
def RedundantSelectTrue :
Pat<(SelectOp $pred, (SelectOp $pred, $a, $b), $c),
(SelectOp $pred, $a, $c)>;

// select(pred, a, select(pred, b, c)) => select(pred, a, c)
def RedundantSelectFalse :
Pat<(SelectOp $pred, $a, (SelectOp $pred, $b, $c)),
(SelectOp $pred, $a, $c)>;

// select(predA, select(predB, x, y), y) => select(and(predA, predB), x, y)
def SelectAndCond :
Pat<(SelectOp $predA, (SelectOp $predB, $x, $y), $y),
(SelectOp (Arith_AndIOp $predA, $predB), $x, $y)>;

// select(predA, select(predB, y, x), y) => select(and(predA, not(predB)), x, y)
def SelectAndNotCond :
Pat<(SelectOp $predA, (SelectOp $predB, $y, $x), $y),
(SelectOp (Arith_AndIOp $predA,
(Arith_XOrIOp $predB, (Arith_ConstantOp ConstantAttr<I1Attr, "1">))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this is wrong because it could be vector of i1 types, e.g., vector<4xi1>.

This patch introduces failure in downstream project (e.g., IREE). I'm working on a fix now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a failure example:

  func.func @foo(%arg0: vector<4xf32>, %arg1: vector<4xf32>) -> vector<4xf32> {
    %0 = arith.cmpf ult, %arg0, %arg1 : vector<4xf32>
    %1 = arith.select %0, %arg0, %arg1 : vector<4xi1>, vector<4xf32>
    %2 = arith.cmpf uno, %arg1, %arg1 : vector<4xf32>
    %3 = arith.select %2, %arg1, %1 : vector<4xi1>, vector<4xf32>
    return %3 : vector<4xf32>
  }

$x, $y)>;

// select(predA, x, select(predB, x, y)) => select(or(predA, predB), x, y)
def SelectOrCond :
Pat<(SelectOp $predA, $x, (SelectOp $predB, $x, $y)),
(SelectOp (Arith_OrIOp $predA, $predB), $x, $y)>;

// select(predA, x, select(predB, y, x)) => select(or(predA, not(predB)), x, y)
def SelectOrNotCond :
Pat<(SelectOp $predA, $x, (SelectOp $predB, $y, $x)),
(SelectOp (Arith_OrIOp $predA,
(Arith_XOrIOp $predB, (Arith_ConstantOp ConstantAttr<I1Attr, "1">))),
$x, $y)>;

//===----------------------------------------------------------------------===//
// IndexCastOp
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 3 additions & 1 deletion mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,9 @@ struct SelectToExtUI : public OpRewritePattern<arith::SelectOp> {

void arith::SelectOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<SelectI1Simplify, SelectToExtUI>(context);
results.add<RedundantSelectFalse, RedundantSelectTrue, SelectI1Simplify,
SelectAndCond, SelectAndNotCond, SelectOrCond, SelectOrNotCond,
SelectNotCond, SelectToExtUI>(context);
}

OpFoldResult arith::SelectOp::fold(FoldAdaptor adaptor) {
Expand Down
76 changes: 76 additions & 0 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,82 @@ func.func @selToArith(%arg0: i1, %arg1 : i1, %arg2 : i1) -> i1 {
return %res : i1
}

// CHECK-LABEL: @redundantSelectTrue
// CHECK-NEXT: %[[res:.+]] = arith.select %arg0, %arg1, %arg3
// CHECK-NEXT: return %[[res]]
func.func @redundantSelectTrue(%arg0: i1, %arg1 : i32, %arg2 : i32, %arg3 : i32) -> i32 {
%0 = arith.select %arg0, %arg1, %arg2 : i32
%res = arith.select %arg0, %0, %arg3 : i32
return %res : i32
}

// CHECK-LABEL: @redundantSelectFalse
// CHECK-NEXT: %[[res:.+]] = arith.select %arg0, %arg3, %arg2
// CHECK-NEXT: return %[[res]]
func.func @redundantSelectFalse(%arg0: i1, %arg1 : i32, %arg2 : i32, %arg3 : i32) -> i32 {
%0 = arith.select %arg0, %arg1, %arg2 : i32
%res = arith.select %arg0, %arg3, %0 : i32
return %res : i32
}

// CHECK-LABEL: @selNotCond
// CHECK-NEXT: %[[res1:.+]] = arith.select %arg0, %arg2, %arg1
// CHECK-NEXT: %[[res2:.+]] = arith.select %arg0, %arg4, %arg3
// CHECK-NEXT: return %[[res1]], %[[res2]]
func.func @selNotCond(%arg0: i1, %arg1 : i32, %arg2 : i32, %arg3 : i32, %arg4 : i32) -> (i32, i32) {
%one = arith.constant 1 : i1
%cond1 = arith.xori %arg0, %one : i1
%cond2 = arith.xori %one, %arg0 : i1

%res1 = arith.select %cond1, %arg1, %arg2 : i32
%res2 = arith.select %cond2, %arg3, %arg4 : i32
return %res1, %res2 : i32, i32
}

// CHECK-LABEL: @selAndCond
// CHECK-NEXT: %[[and:.+]] = arith.andi %arg1, %arg0
// CHECK-NEXT: %[[res:.+]] = arith.select %[[and]], %arg2, %arg3
// CHECK-NEXT: return %[[res]]
func.func @selAndCond(%arg0: i1, %arg1: i1, %arg2 : i32, %arg3 : i32) -> i32 {
%sel = arith.select %arg0, %arg2, %arg3 : i32
%res = arith.select %arg1, %sel, %arg3 : i32
return %res : i32
}

// CHECK-LABEL: @selAndNotCond
// CHECK-NEXT: %[[one:.+]] = arith.constant true
// CHECK-NEXT: %[[not:.+]] = arith.xori %arg0, %[[one]]
// CHECK-NEXT: %[[and:.+]] = arith.andi %arg1, %[[not]]
// CHECK-NEXT: %[[res:.+]] = arith.select %[[and]], %arg3, %arg2
// CHECK-NEXT: return %[[res]]
func.func @selAndNotCond(%arg0: i1, %arg1: i1, %arg2 : i32, %arg3 : i32) -> i32 {
%sel = arith.select %arg0, %arg2, %arg3 : i32
%res = arith.select %arg1, %sel, %arg2 : i32
return %res : i32
}

// CHECK-LABEL: @selOrCond
// CHECK-NEXT: %[[or:.+]] = arith.ori %arg1, %arg0
// CHECK-NEXT: %[[res:.+]] = arith.select %[[or]], %arg2, %arg3
// CHECK-NEXT: return %[[res]]
func.func @selOrCond(%arg0: i1, %arg1: i1, %arg2 : i32, %arg3 : i32) -> i32 {
%sel = arith.select %arg0, %arg2, %arg3 : i32
%res = arith.select %arg1, %arg2, %sel : i32
return %res : i32
}

// CHECK-LABEL: @selOrNotCond
// CHECK-NEXT: %[[one:.+]] = arith.constant true
// CHECK-NEXT: %[[not:.+]] = arith.xori %arg0, %[[one]]
// CHECK-NEXT: %[[or:.+]] = arith.ori %arg1, %[[not]]
// CHECK-NEXT: %[[res:.+]] = arith.select %[[or]], %arg3, %arg2
// CHECK-NEXT: return %[[res]]
func.func @selOrNotCond(%arg0: i1, %arg1: i1, %arg2 : i32, %arg3 : i32) -> i32 {
%sel = arith.select %arg0, %arg2, %arg3 : i32
%res = arith.select %arg1, %arg3, %sel : i32
return %res : i32
}

// Test case: Folding of comparisons with equal operands.
// CHECK-LABEL: @cmpi_equal_operands
// CHECK-DAG: %[[T:.*]] = arith.constant true
Expand Down