[Mlir-commits] [mlir] 16cb6ce - [mlir][Math] Add constant folder for ExpM1Op.
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sun Jul 31 23:41:18 PDT 2022
Author: jacquesguan
Date: 2022-08-01T14:40:50+08:00
New Revision: 16cb6ce554b7ed7b6f4df216ea4efcd782b00187
URL: https://github.com/llvm/llvm-project/commit/16cb6ce554b7ed7b6f4df216ea4efcd782b00187
DIFF: https://github.com/llvm/llvm-project/commit/16cb6ce554b7ed7b6f4df216ea4efcd782b00187.diff
LOG: [mlir][Math] Add constant folder for ExpM1Op.
This patch adds constant folder for ExpM1Op which only supports single and double precision floating-point.
Differential Revision: https://reviews.llvm.org/D130567
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 4bd114c38f582..75b8782d1d4ef 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -420,6 +420,7 @@ def Math_ExpM1Op : Math_FloatUnaryOp<"expm1"> {
%a = math.expm1 %b : f64
```
}];
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 829db91606381..2ba640b796b2c 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -246,6 +246,24 @@ OpFoldResult math::Exp2Op::fold(ArrayRef<Attribute> operands) {
});
}
+//===----------------------------------------------------------------------===//
+// ExpM1Op folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::ExpM1Op::fold(ArrayRef<Attribute> operands) {
+ return constFoldUnaryOpConditional<FloatAttr>(
+ operands, [](const APFloat &a) -> Optional<APFloat> {
+ switch (a.getSizeInBits(a.getSemantics())) {
+ case 64:
+ return APFloat(expm1(a.convertToDouble()));
+ case 32:
+ return APFloat(expm1f(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 40e4b5955cf23..1a2c01699e944 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -264,3 +264,21 @@ func.func @exp2_fold_vec() -> (vector<4xf32>) {
%0 = math.exp2 %v1 : vector<4xf32>
return %0 : vector<4xf32>
}
+
+// CHECK-LABEL: @expm1_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 6.3890562 : f32
+// CHECK-NEXT: return %[[cst]]
+func.func @expm1_fold() -> f32 {
+ %c = arith.constant 2.0 : f32
+ %r = math.expm1 %c : f32
+ return %r : f32
+}
+
+// CHECK-LABEL: @expm1_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 1.71828175, 0.000000e+00, 1.71828175]> : vector<4xf32>
+// CHECK-NEXT: return %[[cst]]
+func.func @expm1_fold_vec() -> (vector<4xf32>) {
+ %v1 = arith.constant dense<[0.0, 1.0, 0.0, 1.0]> : vector<4xf32>
+ %0 = math.expm1 %v1 : vector<4xf32>
+ return %0 : vector<4xf32>
+}
More information about the Mlir-commits
mailing list