[Mlir-commits] [mlir] 7801504 - [mlir][Math] Add constant folder for Exp2Op.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jul 25 17:57:31 PDT 2022


Author: jacquesguan
Date: 2022-07-26T08:48:48+08:00
New Revision: 78015047b22dd64f7c647ab7ce04d42e761e7b8f

URL: https://github.com/llvm/llvm-project/commit/78015047b22dd64f7c647ab7ce04d42e761e7b8f
DIFF: https://github.com/llvm/llvm-project/commit/78015047b22dd64f7c647ab7ce04d42e761e7b8f.diff

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

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

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

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 989e7d46eba60..4bd114c38f582 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -391,6 +391,7 @@ def Math_Exp2Op : Math_FloatUnaryOp<"exp2"> {
     %a = math.exp2 %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index ad40d24f268bd..829db91606381 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -228,6 +228,24 @@ OpFoldResult math::ExpOp::fold(ArrayRef<Attribute> operands) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// Exp2Op folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::Exp2Op::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(exp2(a.convertToDouble()));
+        case 32:
+          return APFloat(exp2f(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 06226e47a5124..40e4b5955cf23 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -246,3 +246,21 @@ func.func @exp_fold_vec() -> (vector<4xf32>) {
   %0 = math.exp %v1 : vector<4xf32>
   return %0 : vector<4xf32>
 }
+
+// CHECK-LABEL: @exp2_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 4.000000e+00 : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @exp2_fold() -> f32 {
+  %c = arith.constant 2.0 : f32
+  %r = math.exp2 %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @exp2_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[2.000000e+00, 4.000000e+00, 8.000000e+00, 1.600000e+01]> : vector<4xf32>
+// CHECK-NEXT:   return %[[cst]]
+func.func @exp2_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : vector<4xf32>
+  %0 = math.exp2 %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}


        


More information about the Mlir-commits mailing list