Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions mlir/lib/Dialect/Math/Transforms/ExpandPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ static LogicalResult convertPowfOp(math::PowFOp op, PatternRewriter &rewriter) {
auto &sem =
cast<mlir::FloatType>(getElementTypeOrSelf(typeB)).getFloatSemantics();
APFloat valueB(sem);
auto mulf = [&](Value x, Value y) -> Value {
return b.create<arith::MulFOp>(x, y);
};
if (matchPattern(operandB, m_ConstantFloat(&valueB))) {
if (valueB.isZero()) {
// a^0 -> 1
Expand Down Expand Up @@ -358,19 +361,21 @@ static LogicalResult convertPowfOp(math::PowFOp op, PatternRewriter &rewriter) {
}
if (valueB.isExactlyValue(2.0)) {
// a^2 -> a * a
Value mul = b.create<arith::MulFOp>(operandA, operandA);
rewriter.replaceOp(op, mul);
rewriter.replaceOp(op, mulf(operandA, operandA));
return success();
}
if (valueB.isExactlyValue(-2.0)) {
// a^(-2) -> 1 / (a * a)
Value mul = b.create<arith::MulFOp>(operandA, operandA);
Value one =
createFloatConst(op->getLoc(), operandA.getType(), 1.0, rewriter);
Value div = b.create<arith::DivFOp>(one, mul);
Value div = b.create<arith::DivFOp>(one, mulf(operandA, operandA));
rewriter.replaceOp(op, div);
return success();
}
if (valueB.isExactlyValue(3.0)) {
rewriter.replaceOp(op, mulf(mulf(operandA, operandA), operandA));
return success();
}
}

Value logA = b.create<math::LogOp>(operandA);
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/Dialect/Math/expand-math.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ func.func @powf_func_negtwo(%a: f64) -> f64{
return %ret : f64
}

// CHECK-LABEL: func @powf_func_three
// CHECK-SAME: (%[[ARG0:.+]]: f64) -> f64
func.func @powf_func_three(%a: f64) -> f64{
// CHECK: %[[MUL:.+]] = arith.mulf %[[ARG0]], %[[ARG0]] : f64
// CHECK: %[[MUL2:.+]] = arith.mulf %[[MUL]], %[[ARG0]] : f64
// CHECK: return %[[MUL2]] : f64
%b = arith.constant 3.0 : f64
%ret = math.powf %a, %b : f64
return %ret : f64
}

// -----

// CHECK-LABEL: func.func @roundeven64
Expand Down