[Mlir-commits] [mlir] 6eebdc4 - [mlir][Math] Add constant folder for ErfOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Sep 18 19:55:32 PDT 2022


Author: jacquesguan
Date: 2022-09-19T10:55:16+08:00
New Revision: 6eebdc46e42e04dc0d88e5203c5bc6a0a37d9d27

URL: https://github.com/llvm/llvm-project/commit/6eebdc46e42e04dc0d88e5203c5bc6a0a37d9d27
DIFF: https://github.com/llvm/llvm-project/commit/6eebdc46e42e04dc0d88e5203c5bc6a0a37d9d27.diff

LOG: [mlir][Math] Add constant folder for ErfOp.

This patch adds constant folder for ErfOp by using erf/erff of libm.

Reviewed By: ftynse, Mogball

Differential Revision: https://reviews.llvm.org/D134017

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Math/IR/MathOps.td
    mlir/lib/Dialect/Math/IR/MathOps.cpp
    mlir/test/Dialect/Math/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index 0dcaa7808e24..149a0da2bb77 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -372,6 +372,7 @@ def Math_ErfOp : Math_FloatUnaryOp<"erf"> {
     %a = math.erf %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 1103068e475e..55f97bcfbe16 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -170,6 +170,24 @@ OpFoldResult math::CtPopOp::fold(ArrayRef<Attribute> operands) {
   });
 }
 
+//===----------------------------------------------------------------------===//
+// ErfOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::ErfOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(erf(a.convertToDouble()));
+        case 32:
+          return APFloat(erff(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
+}
+
 //===----------------------------------------------------------------------===//
 // IPowIOp folder
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 60fcd1210c95..d7c4bb712992 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -465,3 +465,21 @@ func.func @sin_fold_vec() -> (vector<4xf32>) {
   %0 = math.sin %v1 : vector<4xf32>
   return %0 : vector<4xf32>
 }
+
+// CHECK-LABEL: @erf_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 0.84{{[0-9]+}} : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @erf_fold() -> f32 {
+  %c = arith.constant 1.0 : f32
+  %r = math.erf %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @erf_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 0.84{{[0-9]+}}, 0.000000e+00, 0.84{{[0-9]+}}]> : vector<4xf32>
+// CHECK-NEXT:   return %[[cst]]
+func.func @erf_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[0.0, 1.0, 0.0, 1.0]> : vector<4xf32>
+  %0 = math.erf %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}


        


More information about the Mlir-commits mailing list