[Mlir-commits] [mlir] [mlir][math] Expand powfI operation for constant power operand. (PR #87081)

Jakub Kuderski llvmlistbot at llvm.org
Sat Mar 30 18:17:28 PDT 2024


================
@@ -202,6 +202,70 @@ static LogicalResult convertCeilOp(math::CeilOp op, PatternRewriter &rewriter) {
   rewriter.replaceOp(op, ret);
   return success();
 }
+
+// Convert `math.fpowi` to a series of `arith.mulf` operations.
+// If the power is negative, we divide one by the result.
+static LogicalResult convertFPowICstOp(math::FPowIOp op,
+                                       PatternRewriter &rewriter) {
+  ImplicitLocOpBuilder b(op->getLoc(), rewriter);
+  Value base = op.getOperand(0);
+  Value power = op.getOperand(1);
+  Type baseType = base.getType();
+  Value tempBase = op.getOperand(0);
+
+  Attribute cstAttr;
+  if (!matchPattern(power, m_Constant(&cstAttr)))
+    return failure();
+
+  int64_t powerInt;
+
+  // Check for Splat or Integer Attrs.
+  if (auto splatAttr = dyn_cast<SplatElementsAttr>(cstAttr)) {
+    powerInt = splatAttr.getSplatValue<int64_t>();
+  } else if (auto iAttr = dyn_cast<IntegerAttr>(cstAttr)) {
+    powerInt = iAttr.getInt();
+  } else {
+    return failure();
+  }
----------------
kuhar wrote:

You can match against a constant int:

```c++
  APInt value;
  if (!matchPattern(attr, m_ConstantInt(&value)))
    return failure();
```
  

https://github.com/llvm/llvm-project/pull/87081


More information about the Mlir-commits mailing list