[Mlir-commits] [mlir] 9e241c7 - [mlir][Math] Add constant folder for ExpOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jul 25 01:27:06 PDT 2022


Author: jacquesguan
Date: 2022-07-25T16:26:48+08:00
New Revision: 9e241c70f7795eaddf2bab88f5c3f9538d690f90

URL: https://github.com/llvm/llvm-project/commit/9e241c70f7795eaddf2bab88f5c3f9538d690f90
DIFF: https://github.com/llvm/llvm-project/commit/9e241c70f7795eaddf2bab88f5c3f9538d690f90.diff

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

This patch adds constant folder for ExpOp which only supports single and double precision floating-point.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Math/IR/MathOps.td
    mlir/lib/Dialect/Math/IR/MathOps.cpp
    mlir/test/Dialect/Math/canonicalize.mlir
    mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index dd9debe188c1f..989e7d46eba60 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -363,6 +363,7 @@ def Math_ExpOp : Math_FloatUnaryOp<"exp"> {
     %a = math.exp %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 803be4bb18d38..ad40d24f268bd 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -210,6 +210,24 @@ OpFoldResult math::SqrtOp::fold(ArrayRef<Attribute> operands) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// ExpOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::ExpOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(exp(a.convertToDouble()));
+        case 32:
+          return APFloat(expf(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
+}
+
 /// Materialize an integer or floating point constant.
 Operation *math::MathDialect::materializeConstant(OpBuilder &builder,
                                                   Attribute value, Type type,

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index f0366ae2bcfc7..06226e47a5124 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -228,3 +228,21 @@ func.func @log_fold_vec() -> (vector<4xf32>) {
   %0 = math.log %v1 : vector<4xf32>
   return %0 : vector<4xf32>
 }
+
+// CHECK-LABEL: @exp_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 7.3890562 : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @exp_fold() -> f32 {
+  %c = arith.constant 2.0 : f32
+  %r = math.exp %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @exp_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[2.71828175, 7.3890562, 20.085537, 54.5981483]> : vector<4xf32>
+// CHECK-NEXT:   return %[[cst]]
+func.func @exp_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : vector<4xf32>
+  %0 = math.exp %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}

diff  --git a/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir b/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
index e0fe1a8a6f99e..4514abd0ea850 100644
--- a/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
+++ b/mlir/test/mlir-cpu-runner/math-polynomial-approx.mlir
@@ -233,7 +233,7 @@ func.func @exp() {
   %1 = math.exp %0 : f32
   vector.print %1 : f32
 
-  // CHECK: 0.778802, 2.117, 2.71828, 3.85742
+  // CHECK: 0.778801, 2.117, 2.71828, 3.85743
   %2 = arith.constant dense<[-0.25, 0.75, 1.0, 1.35]> : vector<4xf32>
   %3 = math.exp %2 : vector<4xf32>
   vector.print %3 : vector<4xf32>
@@ -243,7 +243,7 @@ func.func @exp() {
   %exp_zero = math.exp %zero : f32
   vector.print %exp_zero : f32
 
-  // CHECK: 1.17549e-38, 1.38879e-11, 7.20049e+10, inf
+  // CHECK: 2.22736e-39, 1.38879e-11, 7.20049e+10, inf
   %special_vec = arith.constant dense<[-89.0, -25.0, 25.0, 89.0]> : vector<4xf32>
   %exp_special_vec = math.exp %special_vec : vector<4xf32>
   vector.print %exp_special_vec : vector<4xf32>
@@ -272,7 +272,7 @@ func.func @expm1() {
   %1 = math.expm1 %0 : f32
   vector.print %1 : f32
 
-  // CHECK: -0.00995016, 0.0100502, 0.648721, 6.38905
+  // CHECK: -0.00995017, 0.0100502, 0.648721, 6.38906
   %2 = arith.constant dense<[-0.01, 0.01, 0.5, 2.0]> : vector<4xf32>
   %3 = math.expm1 %2 : vector<4xf32>
   vector.print %3 : vector<4xf32>


        


More information about the Mlir-commits mailing list