[Mlir-commits] [mlir] 164c7af - [MLIR][Math] Add constant folder for powf

William S. Moses llvmlistbot at llvm.org
Thu Mar 17 11:19:51 PDT 2022


Author: William S. Moses
Date: 2022-03-17T14:19:47-04:00
New Revision: 164c7afaf5cbf924806fef7c280e2d71bdac0037

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

LOG: [MLIR][Math] Add constant folder for powf

Constant fold powf, given two constant operands and a compatible type

Reviewed By: ftynse

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

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 c636c70ded7eb..ca91d5353b584 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -684,6 +684,7 @@ def Math_PowFOp : Math_FloatBinaryOp<"powf"> {
     %x = math.powf %y, %z : tensor<4x?xbf16>
     ```
   }];
+  let hasFolder = 1;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 4396c32eb3378..4410e93ef6e0f 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -63,7 +63,40 @@ OpFoldResult math::Log2Op::fold(ArrayRef<Attribute> operands) {
     return FloatAttr::get(getType(), log2(apf.convertToDouble()));
 
   if (ft.getWidth() == 32)
-    return FloatAttr::get(getType(), log2f(apf.convertToDouble()));
+    return FloatAttr::get(getType(), log2f(apf.convertToFloat()));
+
+  return {};
+}
+
+//===----------------------------------------------------------------------===//
+// PowFOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::PowFOp::fold(ArrayRef<Attribute> operands) {
+  auto ft = getType().dyn_cast<FloatType>();
+  if (!ft)
+    return {};
+
+  APFloat vals[2]{APFloat(ft.getFloatSemantics()),
+                  APFloat(ft.getFloatSemantics())};
+  for (int i = 0; i < 2; ++i) {
+    if (!operands[i])
+      return {};
+
+    auto attr = operands[i].dyn_cast<FloatAttr>();
+    if (!attr)
+      return {};
+
+    vals[i] = attr.getValue();
+  }
+
+  if (ft.getWidth() == 64)
+    return FloatAttr::get(
+        getType(), pow(vals[0].convertToDouble(), vals[1].convertToDouble()));
+
+  if (ft.getWidth() == 32)
+    return FloatAttr::get(
+        getType(), powf(vals[0].convertToFloat(), vals[1].convertToFloat()));
 
   return {};
 }

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index f62f0cf0cde53..5ee63b60bec1a 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -73,3 +73,12 @@ func @log2_nofold2_64() -> f64 {
   %r = math.log2 %c : f64
   return %r : f64
 }
+
+// CHECK-LABEL: @powf_fold
+// CHECK: %[[cst:.+]] = arith.constant 4.000000e+00 : f32
+// CHECK: return %[[cst]]
+func @powf_fold() -> f32 {
+  %c = arith.constant 2.0 : f32
+  %r = math.powf %c, %c : f32
+  return %r : f32
+}


        


More information about the Mlir-commits mailing list