[Mlir-commits] [mlir] 71e52a1 - [mlir][Math] Add constant folder for SinOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Sep 15 23:30:16 PDT 2022


Author: jacquesguan
Date: 2022-09-16T14:30:05+08:00
New Revision: 71e52a125cb5f532192c40cf13692c18ede18cb4

URL: https://github.com/llvm/llvm-project/commit/71e52a125cb5f532192c40cf13692c18ede18cb4
DIFF: https://github.com/llvm/llvm-project/commit/71e52a125cb5f532192c40cf13692c18ede18cb4.diff

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

This patch adds constant folder for SinOp by using sin/sinf of libm.

Reviewed By: ftynse

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

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 2747c8e02faf8..0dcaa7808e241 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -285,6 +285,7 @@ def Math_SinOp : Math_FloatUnaryOp<"sin"> {
     %a = math.sin %b : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index b5300745f26ab..1103068e475e4 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -122,6 +122,24 @@ OpFoldResult math::CosOp::fold(ArrayRef<Attribute> operands) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// SinOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::SinOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(sin(a.convertToDouble()));
+        case 32:
+          return APFloat(sinf(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
+}
+
 //===----------------------------------------------------------------------===//
 // CountLeadingZerosOp folder
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index a539383867f42..60fcd1210c95e 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -447,3 +447,21 @@ func.func @trunc_fold_vec() -> (vector<4xf32>) {
   %0 = math.trunc %v : vector<4xf32>
   return %0 : vector<4xf32>
 }
+
+// CHECK-LABEL: @sin_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 0.84{{[0-9]+}} : f32
+// CHECK-NEXT:   return %[[cst]]
+func.func @sin_fold() -> f32 {
+  %c = arith.constant 1.0 : f32
+  %r = math.sin %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @sin_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 @sin_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[0.0, 1.0, 0.0, 1.0]> : vector<4xf32>
+  %0 = math.sin %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}


        


More information about the Mlir-commits mailing list