[Mlir-commits] [mlir] a5cae20 - [mlir][Math] Add constant folder for Log10Op.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Jul 17 19:26:08 PDT 2022


Author: jacquesguan
Date: 2022-07-18T10:19:25+08:00
New Revision: a5cae20bdb189dcd043c41e2076dc0df3a55b771

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

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

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

Reviewed By: Mogball

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

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 94103ff61edf..163e6874039d 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -517,6 +517,7 @@ def Math_Log10Op : Math_FloatUnaryOp<"log10"> {
     %y = math.log10 %x : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index ac6b1ae26284..b389f437493b 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -107,6 +107,27 @@ OpFoldResult math::Log2Op::fold(ArrayRef<Attribute> operands) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// Log10Op folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::Log10Op::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        if (a.isNegative())
+          return {};
+
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(log10(a.convertToDouble()));
+        case 32:
+          return APFloat(log10f(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
+}
+
 //===----------------------------------------------------------------------===//
 // PowFOp folder
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index b78e3ad35261..0aabd6281bca 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -174,3 +174,21 @@ func.func @ctpop_fold() -> i32 {
   %r = math.ctpop %c : i32
   return %r : i32
 }
+
+// CHECK-LABEL: @log10_fold
+// CHECK: %[[cst:.+]] = arith.constant 2.000000e+00 : f32
+  // CHECK: return %[[cst]]
+func.func @log10_fold() -> f32 {
+  %c = arith.constant 100.0 : f32
+  %r = math.log10 %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @log10_fold_vec
+// CHECK: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 1.000000e+00, 2.000000e+00, 2.301030e+00]> : vector<4xf32>
+// CHECK: return %[[cst]]
+func.func @log10_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[1.0, 10.0, 100.0, 200.0]> : vector<4xf32>
+  %0 = math.log10 %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}


        


More information about the Mlir-commits mailing list