[Mlir-commits] [mlir] c3d856b - [mlir][Math] Add constant folder for Log1pOp.
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Jul 20 00:18:34 PDT 2022
Author: jacquesguan
Date: 2022-07-20T14:59:35+08:00
New Revision: c3d856bf58bf227ffb475fcbb51a1e21b026a7ee
URL: https://github.com/llvm/llvm-project/commit/c3d856bf58bf227ffb475fcbb51a1e21b026a7ee
DIFF: https://github.com/llvm/llvm-project/commit/c3d856bf58bf227ffb475fcbb51a1e21b026a7ee.diff
LOG: [mlir][Math] Add constant folder for Log1pOp.
This patch adds constant folder for Log1pOp which only supports single and double precision floating-point.
Differential Revision: https://reviews.llvm.org/D129979
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 163e6874039dd..1bdbe71541d1f 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -541,6 +541,7 @@ def Math_Log1pOp : Math_FloatUnaryOp<"log1p"> {
%y = math.log1p %x : f64
```
}];
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index b389f437493b9..1d212c191e789 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -128,6 +128,28 @@ OpFoldResult math::Log10Op::fold(ArrayRef<Attribute> operands) {
});
}
+//===----------------------------------------------------------------------===//
+// Log1pOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::Log1pOp::fold(ArrayRef<Attribute> operands) {
+ return constFoldUnaryOpConditional<FloatAttr>(
+ operands, [](const APFloat &a) -> Optional<APFloat> {
+ switch (a.getSizeInBits(a.getSemantics())) {
+ case 64:
+ if ((a + APFloat(1.0)).isNegative())
+ return {};
+ return APFloat(log1p(a.convertToDouble()));
+ case 32:
+ if ((a + APFloat(1.0f)).isNegative())
+ return {};
+ return APFloat(log1pf(a.convertToFloat()));
+ default:
+ return {};
+ }
+ });
+}
+
//===----------------------------------------------------------------------===//
// PowFOp folder
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 0aabd6281bcae..2cf36648526c1 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -192,3 +192,21 @@ func.func @log10_fold_vec() -> (vector<4xf32>) {
%0 = math.log10 %v1 : vector<4xf32>
return %0 : vector<4xf32>
}
+
+// CHECK-LABEL: @log1p_fold
+// CHECK: %[[cst:.+]] = arith.constant 2.8903718 : f32
+ // CHECK: return %[[cst]]
+func.func @log1p_fold() -> f32 {
+ %c = arith.constant 17.0 : f32
+ %r = math.log1p %c : f32
+ return %r : f32
+}
+
+// CHECK-LABEL: @log1p_fold_vec
+// CHECK: %[[cst:.+]] = arith.constant dense<[1.38629436, 1.79175949, 2.07944155, 2.48490667]> : vector<4xf32>
+// CHECK: return %[[cst]]
+func.func @log1p_fold_vec() -> (vector<4xf32>) {
+ %v1 = arith.constant dense<[3.0, 5.0, 7.0, 11.0]> : vector<4xf32>
+ %0 = math.log1p %v1 : vector<4xf32>
+ return %0 : vector<4xf32>
+}
More information about the Mlir-commits
mailing list