[Mlir-commits] [mlir] 9c22853 - [mlir][Math] Add constant folder for LogOp.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jul 20 20:03:38 PDT 2022


Author: jacquesguan
Date: 2022-07-21T10:58:32+08:00
New Revision: 9c22853ec40a8a464dc00e22b78c73bde8c67b2d

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

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

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

Reviewed By: ftynse

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

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 1bdbe71541d1f..dd9debe188c1f 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -496,6 +496,7 @@ def Math_LogOp : Math_FloatUnaryOp<"log"> {
     %y = math.log %x : f64
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 1d212c191e789..803be4bb18d38 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -87,6 +87,26 @@ OpFoldResult math::CtPopOp::fold(ArrayRef<Attribute> operands) {
   });
 }
 
+//===----------------------------------------------------------------------===//
+// LogOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::LogOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        if (a.isNegative())
+          return {};
+
+        if (a.getSizeInBits(a.getSemantics()) == 64)
+          return APFloat(log(a.convertToDouble()));
+
+        if (a.getSizeInBits(a.getSemantics()) == 32)
+          return APFloat(logf(a.convertToFloat()));
+
+        return {};
+      });
+}
+
 //===----------------------------------------------------------------------===//
 // Log2Op folder
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 2cf36648526c1..f0366ae2bcfc7 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -210,3 +210,21 @@ func.func @log1p_fold_vec() -> (vector<4xf32>) {
   %0 = math.log1p %v1 : vector<4xf32>
   return %0 : vector<4xf32>
 }
+
+// CHECK-LABEL: @log_fold
+// CHECK: %[[cst:.+]] = arith.constant 0.693147182 : f32
+  // CHECK: return %[[cst]]
+func.func @log_fold() -> f32 {
+  %c = arith.constant 2.0 : f32
+  %r = math.log %c : f32
+  return %r : f32
+}
+
+// CHECK-LABEL: @log_fold_vec
+// CHECK: %[[cst:.+]] = arith.constant dense<[0.000000e+00, 0.693147182, 1.09861231, 1.38629436]> : vector<4xf32
+// CHECK: return %[[cst]]
+func.func @log_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : vector<4xf32>
+  %0 = math.log %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}


        


More information about the Mlir-commits mailing list