[Mlir-commits] [mlir] 4d7d5c5 - [mlir][Math] Support fold SqrtOp with constant dense.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Jul 12 01:46:37 PDT 2022


Author: jacquesguan
Date: 2022-07-12T16:46:19+08:00
New Revision: 4d7d5c5f0025dd9b1f32c30ab473d2095970fd18

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

LOG: [mlir][Math] Support fold SqrtOp with constant dense.

This patch uses constFoldUnaryOpConditional to replace current folder in order to support constant dense.

Reviewed By: rriddle

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

Added: 
    

Modified: 
    mlir/lib/Dialect/Math/IR/MathOps.cpp
    mlir/test/Dialect/Math/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 035e9b49ba5e2..ac6b1ae26284d 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -126,29 +126,25 @@ OpFoldResult math::PowFOp::fold(ArrayRef<Attribute> operands) {
       });
 }
 
-OpFoldResult math::SqrtOp::fold(ArrayRef<Attribute> operands) {
-  auto constOperand = operands.front();
-  if (!constOperand)
-    return {};
-
-  auto attr = constOperand.dyn_cast<FloatAttr>();
-  if (!attr)
-    return {};
-
-  auto ft = getType().cast<FloatType>();
-
-  APFloat apf = attr.getValue();
-
-  if (apf.isNegative())
-    return {};
-
-  if (ft.getWidth() == 64)
-    return FloatAttr::get(getType(), sqrt(apf.convertToDouble()));
+//===----------------------------------------------------------------------===//
+// SqrtOp folder
+//===----------------------------------------------------------------------===//
 
-  if (ft.getWidth() == 32)
-    return FloatAttr::get(getType(), sqrtf(apf.convertToFloat()));
+OpFoldResult math::SqrtOp::fold(ArrayRef<Attribute> operands) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      operands, [](const APFloat &a) -> Optional<APFloat> {
+        if (a.isNegative())
+          return {};
 
-  return {};
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(sqrt(a.convertToDouble()));
+        case 32:
+          return APFloat(sqrtf(a.convertToFloat()));
+        default:
+          return {};
+        }
+      });
 }
 
 /// Materialize an integer or floating point constant.

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 2ddd766d7d302..b78e3ad352610 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -111,6 +111,15 @@ func.func @sqrt_fold() -> f32 {
   return %r : f32
 }
 
+// CHECK-LABEL: @sqrt_fold_vec
+// CHECK: %[[cst:.+]] = arith.constant dense<[1.000000e+00, 1.41421354, 1.73205078, 2.000000e+00]> : vector<4xf32>
+// CHECK: return %[[cst]]
+func.func @sqrt_fold_vec() -> (vector<4xf32>) {
+  %v1 = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : vector<4xf32>
+  %0 = math.sqrt %v1 : vector<4xf32>
+  return %0 : vector<4xf32>
+}
+
 // CHECK-LABEL: @abs_fold
 // CHECK: %[[cst:.+]] = arith.constant 4.000000e+00 : f32
 // CHECK: return %[[cst]]


        


More information about the Mlir-commits mailing list