Skip to content

Commit

Permalink
[mlir][Arithmetic] fold overlapping negf.
Browse files Browse the repository at this point in the history
This patch folds negf(negf(x)) to x.

Differential Revision: https://reviews.llvm.org/D125955
  • Loading branch information
jacquesguan authored and jacquesguan committed May 20, 2022
1 parent 6e00a34 commit 0e02bf6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@ void arith::XOrIOp::getCanonicalizationPatterns(
//===----------------------------------------------------------------------===//

OpFoldResult arith::NegFOp::fold(ArrayRef<Attribute> operands) {
/// negf(negf(x)) -> x
if (auto op = this->getOperand().getDefiningOp<arith::NegFOp>())
return op.getOperand();
return constFoldUnaryOp<FloatAttr>(operands,
[](const APFloat &a) { return -a; });
}
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Dialect/Arithmetic/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,15 @@ func.func @test_negf() -> (f32) {
return %0: f32
}

// CHECK-LABEL: @test_negf1(
// CHECK-SAME: %[[arg0:.+]]:
// CHECK: return %[[arg0]]
func.func @test_negf1(%f : f32) -> (f32) {
%0 = arith.negf %f : f32
%1 = arith.negf %0 : f32
return %1: f32
}

// -----

// CHECK-LABEL: @test_remui(
Expand Down

0 comments on commit 0e02bf6

Please sign in to comment.