Skip to content

[mlir][complex] Support fast math flag in converting complex.atan2 op #82101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 6, 2024
21 changes: 11 additions & 10 deletions mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,35 @@ struct Atan2OpConversion : public OpConversionPattern<complex::Atan2Op> {

auto type = cast<ComplexType>(op.getType());
Type elementType = type.getElementType();
arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();

Value lhs = adaptor.getLhs();
Value rhs = adaptor.getRhs();

Value rhsSquared = b.create<complex::MulOp>(type, rhs, rhs);
Value lhsSquared = b.create<complex::MulOp>(type, lhs, lhs);
Value rhsSquared = b.create<complex::MulOp>(type, rhs, rhs, fmf);
Value lhsSquared = b.create<complex::MulOp>(type, lhs, lhs, fmf);
Value rhsSquaredPlusLhsSquared =
b.create<complex::AddOp>(type, rhsSquared, lhsSquared);
b.create<complex::AddOp>(type, rhsSquared, lhsSquared, fmf);
Value sqrtOfRhsSquaredPlusLhsSquared =
b.create<complex::SqrtOp>(type, rhsSquaredPlusLhsSquared);
b.create<complex::SqrtOp>(type, rhsSquaredPlusLhsSquared, fmf);

Value zero =
b.create<arith::ConstantOp>(elementType, b.getZeroAttr(elementType));
Value one = b.create<arith::ConstantOp>(elementType,
b.getFloatAttr(elementType, 1));
Value i = b.create<complex::CreateOp>(type, zero, one);
Value iTimesLhs = b.create<complex::MulOp>(i, lhs);
Value rhsPlusILhs = b.create<complex::AddOp>(rhs, iTimesLhs);
Value iTimesLhs = b.create<complex::MulOp>(i, lhs, fmf);
Value rhsPlusILhs = b.create<complex::AddOp>(rhs, iTimesLhs, fmf);

Value divResult =
b.create<complex::DivOp>(rhsPlusILhs, sqrtOfRhsSquaredPlusLhsSquared);
Value logResult = b.create<complex::LogOp>(divResult);
Value divResult = b.create<complex::DivOp>(
rhsPlusILhs, sqrtOfRhsSquaredPlusLhsSquared, fmf);
Value logResult = b.create<complex::LogOp>(divResult, fmf);

Value negativeOne = b.create<arith::ConstantOp>(
elementType, b.getFloatAttr(elementType, -1));
Value negativeI = b.create<complex::CreateOp>(type, zero, negativeOne);

rewriter.replaceOpWithNewOp<complex::MulOp>(op, negativeI, logResult);
rewriter.replaceOpWithNewOp<complex::MulOp>(op, negativeI, logResult, fmf);
return success();
}
};
Expand Down
Loading