20#define GEN_PASS_DEF_CONVERTCOMPLEXTOSTANDARDPASS
21#include "mlir/Conversion/Passes.h.inc"
28enum class AbsFn { abs, sqrt, rsqrt };
33 Value one = arith::ConstantOp::create(
b,
real.getType(),
34 b.getFloatAttr(
real.getType(), 1.0));
36 Value absReal = math::AbsFOp::create(
b,
real, fmf);
37 Value absImag = math::AbsFOp::create(
b,
imag, fmf);
39 Value max = arith::MaximumFOp::create(
b, absReal, absImag, fmf);
40 Value min = arith::MinimumFOp::create(
b, absReal, absImag, fmf);
43 arith::FastMathFlags fmfWithNaNInf = arith::bitEnumClear(
44 fmf, arith::FastMathFlags::nnan | arith::FastMathFlags::ninf);
45 Value ratio = arith::DivFOp::create(
b,
min,
max, fmfWithNaNInf);
46 Value ratioSq = arith::MulFOp::create(
b, ratio, ratio, fmfWithNaNInf);
47 Value ratioSqPlusOne = arith::AddFOp::create(
b, ratioSq, one, fmfWithNaNInf);
50 if (fn == AbsFn::rsqrt) {
51 ratioSqPlusOne = math::RsqrtOp::create(
b, ratioSqPlusOne, fmfWithNaNInf);
52 min = math::RsqrtOp::create(
b,
min, fmfWithNaNInf);
53 max = math::RsqrtOp::create(
b,
max, fmfWithNaNInf);
56 if (fn == AbsFn::sqrt) {
57 Value quarter = arith::ConstantOp::create(
58 b,
real.getType(),
b.getFloatAttr(
real.getType(), 0.25));
60 Value sqrt = math::SqrtOp::create(
b,
max, fmfWithNaNInf);
62 math::PowFOp::create(
b, ratioSqPlusOne, quarter, fmfWithNaNInf);
63 result = arith::MulFOp::create(
b, sqrt, p025, fmfWithNaNInf);
65 Value sqrt = math::SqrtOp::create(
b, ratioSqPlusOne, fmfWithNaNInf);
66 result = arith::MulFOp::create(
b,
max, sqrt, fmfWithNaNInf);
69 Value isNaN = arith::CmpFOp::create(
b, arith::CmpFPredicate::UNO,
result,
71 return arith::SelectOp::create(
b, isNaN,
min,
result);
74struct AbsOpConversion :
public OpConversionPattern<complex::AbsOp> {
75 using OpConversionPattern<complex::AbsOp>::OpConversionPattern;
78 matchAndRewrite(complex::AbsOp op, OpAdaptor adaptor,
79 ConversionPatternRewriter &rewriter)
const override {
80 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
82 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
84 Value
real = complex::ReOp::create(
b, adaptor.getComplex());
85 Value
imag = complex::ImOp::create(
b, adaptor.getComplex());
86 rewriter.replaceOp(op, computeAbs(
real,
imag, fmf,
b));
93struct Atan2OpConversion :
public OpConversionPattern<complex::Atan2Op> {
94 using OpConversionPattern<complex::Atan2Op>::OpConversionPattern;
97 matchAndRewrite(complex::Atan2Op op, OpAdaptor adaptor,
98 ConversionPatternRewriter &rewriter)
const override {
99 mlir::ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
101 auto type = cast<ComplexType>(op.getType());
102 Type elementType = type.getElementType();
103 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
105 Value
lhs = adaptor.getLhs();
106 Value
rhs = adaptor.getRhs();
108 Value rhsSquared = complex::MulOp::create(
b, type,
rhs,
rhs, fmf);
109 Value lhsSquared = complex::MulOp::create(
b, type,
lhs,
lhs, fmf);
110 Value rhsSquaredPlusLhsSquared =
111 complex::AddOp::create(
b, type, rhsSquared, lhsSquared, fmf);
112 Value sqrtOfRhsSquaredPlusLhsSquared =
113 complex::SqrtOp::create(
b, type, rhsSquaredPlusLhsSquared, fmf);
116 arith::ConstantOp::create(
b, elementType,
b.getZeroAttr(elementType));
117 Value one = arith::ConstantOp::create(
b, elementType,
118 b.getFloatAttr(elementType, 1));
119 Value i = complex::CreateOp::create(
b, type, zero, one);
120 Value iTimesLhs = complex::MulOp::create(
b, i,
lhs, fmf);
121 Value rhsPlusILhs = complex::AddOp::create(
b,
rhs, iTimesLhs, fmf);
123 Value divResult = complex::DivOp::create(
124 b, rhsPlusILhs, sqrtOfRhsSquaredPlusLhsSquared, fmf);
125 Value logResult = complex::LogOp::create(
b, divResult, fmf);
127 Value negativeOne = arith::ConstantOp::create(
128 b, elementType,
b.getFloatAttr(elementType, -1));
129 Value negativeI = complex::CreateOp::create(
b, type, zero, negativeOne);
131 rewriter.replaceOpWithNewOp<complex::MulOp>(op, negativeI, logResult, fmf);
136template <
typename ComparisonOp, arith::CmpFPredicate p>
137struct ComparisonOpConversion :
public OpConversionPattern<ComparisonOp> {
138 using OpConversionPattern<ComparisonOp>::OpConversionPattern;
139 using ResultCombiner =
140 std::conditional_t<std::is_same<ComparisonOp, complex::EqualOp>::value,
141 arith::AndIOp, arith::OrIOp>;
144 matchAndRewrite(ComparisonOp op,
typename ComparisonOp::Adaptor adaptor,
145 ConversionPatternRewriter &rewriter)
const override {
146 auto loc = op.getLoc();
147 auto type = cast<ComplexType>(adaptor.getLhs().getType()).getElementType();
150 complex::ReOp::create(rewriter, loc, type, adaptor.getLhs());
152 complex::ImOp::create(rewriter, loc, type, adaptor.getLhs());
154 complex::ReOp::create(rewriter, loc, type, adaptor.getRhs());
156 complex::ImOp::create(rewriter, loc, type, adaptor.getRhs());
157 Value realComparison =
158 arith::CmpFOp::create(rewriter, loc, p, realLhs, realRhs);
159 Value imagComparison =
160 arith::CmpFOp::create(rewriter, loc, p, imagLhs, imagRhs);
162 rewriter.replaceOpWithNewOp<ResultCombiner>(op, realComparison,
171template <
typename BinaryComplexOp,
typename BinaryStandardOp>
172struct BinaryComplexOpConversion :
public OpConversionPattern<BinaryComplexOp> {
173 using OpConversionPattern<BinaryComplexOp>::OpConversionPattern;
176 matchAndRewrite(BinaryComplexOp op,
typename BinaryComplexOp::Adaptor adaptor,
177 ConversionPatternRewriter &rewriter)
const override {
178 auto type = cast<ComplexType>(adaptor.getLhs().getType());
179 auto elementType = cast<FloatType>(type.getElementType());
180 mlir::ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
181 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
183 Value realLhs = complex::ReOp::create(
b, elementType, adaptor.getLhs());
184 Value realRhs = complex::ReOp::create(
b, elementType, adaptor.getRhs());
185 Value resultReal = BinaryStandardOp::create(
b, elementType, realLhs,
186 realRhs, fmf.getValue());
187 Value imagLhs = complex::ImOp::create(
b, elementType, adaptor.getLhs());
188 Value imagRhs = complex::ImOp::create(
b, elementType, adaptor.getRhs());
189 Value resultImag = BinaryStandardOp::create(
b, elementType, imagLhs,
190 imagRhs, fmf.getValue());
191 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
197template <
typename TrigonometricOp>
198struct TrigonometricOpConversion :
public OpConversionPattern<TrigonometricOp> {
199 using OpAdaptor =
typename OpConversionPattern<TrigonometricOp>::OpAdaptor;
201 using OpConversionPattern<TrigonometricOp>::OpConversionPattern;
204 matchAndRewrite(TrigonometricOp op, OpAdaptor adaptor,
205 ConversionPatternRewriter &rewriter)
const override {
206 auto loc = op.getLoc();
207 auto type = cast<ComplexType>(adaptor.getComplex().getType());
208 auto elementType = cast<FloatType>(type.getElementType());
209 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
212 complex::ReOp::create(rewriter, loc, elementType, adaptor.getComplex());
214 complex::ImOp::create(rewriter, loc, elementType, adaptor.getComplex());
219 Value half = arith::ConstantOp::create(
220 rewriter, loc, elementType, rewriter.getFloatAttr(elementType, 0.5));
221 Value exp = math::ExpOp::create(rewriter, loc,
imag, fmf);
222 Value scaledExp = arith::MulFOp::create(rewriter, loc, half, exp, fmf);
223 Value reciprocalExp = arith::DivFOp::create(rewriter, loc, half, exp, fmf);
224 Value sin = math::SinOp::create(rewriter, loc,
real, fmf);
225 Value cos = math::CosOp::create(rewriter, loc,
real, fmf);
228 combine(loc, scaledExp, reciprocalExp, sin, cos, rewriter, fmf);
230 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultPair.first,
235 virtual std::pair<Value, Value>
236 combine(Location loc, Value scaledExp, Value reciprocalExp, Value sin,
237 Value cos, ConversionPatternRewriter &rewriter,
238 arith::FastMathFlagsAttr fmf)
const = 0;
241struct CosOpConversion :
public TrigonometricOpConversion<complex::CosOp> {
242 using TrigonometricOpConversion<complex::CosOp>::TrigonometricOpConversion;
244 std::pair<Value, Value>
combine(Location loc, Value scaledExp,
245 Value reciprocalExp, Value sin, Value cos,
246 ConversionPatternRewriter &rewriter,
247 arith::FastMathFlagsAttr fmf)
const override {
258 arith::AddFOp::create(rewriter, loc, reciprocalExp, scaledExp, fmf);
259 Value resultReal = arith::MulFOp::create(rewriter, loc, sum, cos, fmf);
261 arith::SubFOp::create(rewriter, loc, reciprocalExp, scaledExp, fmf);
262 Value resultImag = arith::MulFOp::create(rewriter, loc, diff, sin, fmf);
263 return {resultReal, resultImag};
267struct DivOpConversion :
public OpConversionPattern<complex::DivOp> {
268 DivOpConversion(MLIRContext *context, complex::ComplexRangeFlags
target)
269 : OpConversionPattern<complex::DivOp>(context), complexRange(
target) {}
271 using OpConversionPattern<complex::DivOp>::OpConversionPattern;
274 matchAndRewrite(complex::DivOp op, OpAdaptor adaptor,
275 ConversionPatternRewriter &rewriter)
const override {
276 auto loc = op.getLoc();
277 auto type = cast<ComplexType>(adaptor.getLhs().getType());
278 auto elementType = cast<FloatType>(type.getElementType());
279 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
282 complex::ReOp::create(rewriter, loc, elementType, adaptor.getLhs());
284 complex::ImOp::create(rewriter, loc, elementType, adaptor.getLhs());
286 complex::ReOp::create(rewriter, loc, elementType, adaptor.getRhs());
288 complex::ImOp::create(rewriter, loc, elementType, adaptor.getRhs());
290 Value resultReal, resultImag;
292 if (complexRange == complex::ComplexRangeFlags::basic ||
293 complexRange == complex::ComplexRangeFlags::none) {
295 rewriter, loc, lhsReal, lhsImag, rhsReal, rhsImag, fmf, &resultReal,
297 }
else if (complexRange == complex::ComplexRangeFlags::improved) {
299 rewriter, loc, lhsReal, lhsImag, rhsReal, rhsImag, fmf, &resultReal,
303 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
310 complex::ComplexRangeFlags complexRange;
313struct ExpOpConversion :
public OpConversionPattern<complex::ExpOp> {
314 using OpConversionPattern<complex::ExpOp>::OpConversionPattern;
324 matchAndRewrite(complex::ExpOp op, OpAdaptor adaptor,
325 ConversionPatternRewriter &rewriter)
const override {
326 auto loc = op.getLoc();
327 auto type = cast<ComplexType>(adaptor.getComplex().getType());
328 auto ET = cast<FloatType>(type.getElementType());
329 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
330 const auto &floatSemantics = ET.getFloatSemantics();
331 ImplicitLocOpBuilder
b(loc, rewriter);
333 Value x = complex::ReOp::create(
b, ET, adaptor.getComplex());
334 Value y = complex::ImOp::create(
b, ET, adaptor.getComplex());
335 Value zero = arith::ConstantOp::create(
b, ET,
b.getZeroAttr(ET));
336 Value half = arith::ConstantOp::create(
b, ET,
b.getFloatAttr(ET, 0.5));
337 Value inf = arith::ConstantOp::create(
338 b, ET,
b.getFloatAttr(ET, APFloat::getInf(floatSemantics)));
341 arith::FastMathFlags fmfWithInf =
342 arith::bitEnumClear(fmf, arith::FastMathFlags::ninf);
343 Value exp = math::ExpOp::create(
b, x, fmfWithInf);
344 Value xHalf = arith::MulFOp::create(
b, x, half, fmf);
345 Value expHalf = math::ExpOp::create(
b, xHalf, fmf);
346 Value cos = math::CosOp::create(
b, y, fmf);
347 Value sin = math::SinOp::create(
b, y, fmf);
349 Value expIsInf = arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ, exp,
352 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ, y, zero);
355 Value realNormal = arith::MulFOp::create(
b, exp, cos, fmf);
356 Value expHalfCos = arith::MulFOp::create(
b, expHalf, cos, fmf);
357 Value realOverflow = arith::MulFOp::create(
b, expHalfCos, expHalf, fmf);
359 arith::SelectOp::create(
b, expIsInf, realOverflow, realNormal);
363 Value imagNormal = arith::MulFOp::create(
b, exp, sin, fmf);
364 Value expHalfSin = arith::MulFOp::create(
b, expHalf, sin, fmf);
365 Value imagOverflow = arith::MulFOp::create(
b, expHalfSin, expHalf, fmf);
367 arith::SelectOp::create(
b, expIsInf, imagOverflow, imagNormal);
368 Value resultImag = arith::SelectOp::create(
b, yIsZero, y, imagNonZero);
373 if (!arith::bitEnumContainsAll(fmf, arith::FastMathFlags::ninf)) {
374 Value absX = math::AbsFOp::create(
b, x);
375 Value absY = math::AbsFOp::create(
b, y);
377 arith::CmpFOp::create(
b, arith::CmpFPredicate::UGE, absY, inf);
379 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ, absX, inf);
381 arith::CmpFOp::create(
b, arith::CmpFPredicate::OLT, x, zero);
382 Value useExpX = arith::AndIOp::create(
b, xIsInf, yIsNotFinite);
383 Value useZeroImag = arith::AndIOp::create(
b, useExpX, xIsNeg);
384 resultReal = arith::SelectOp::create(
b, useExpX, exp, resultReal);
385 resultImag = arith::SelectOp::create(
b, useZeroImag, zero, resultImag);
388 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
396 arith::FastMathFlagsAttr fmf) {
397 auto argType = mlir::cast<FloatType>(arg.
getType());
399 arith::ConstantOp::create(
b,
b.getFloatAttr(argType, coefficients[0]));
400 for (
unsigned i = 1; i < coefficients.size(); ++i) {
401 poly = math::FmaOp::create(
403 arith::ConstantOp::create(
b,
b.getFloatAttr(argType, coefficients[i])),
409struct Expm1OpConversion :
public OpConversionPattern<complex::Expm1Op> {
410 using OpConversionPattern<complex::Expm1Op>::OpConversionPattern;
417 matchAndRewrite(complex::Expm1Op op, OpAdaptor adaptor,
418 ConversionPatternRewriter &rewriter)
const override {
420 auto elemType = mlir::cast<FloatType>(type.getElementType());
422 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
423 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
424 Value
real = complex::ReOp::create(
b, adaptor.getComplex());
425 Value
imag = complex::ImOp::create(
b, adaptor.getComplex());
427 Value zero = arith::ConstantOp::create(
b,
b.getFloatAttr(elemType, 0.0));
428 Value one = arith::ConstantOp::create(
b,
b.getFloatAttr(elemType, 1.0));
430 Value expm1Real = math::ExpM1Op::create(
b,
real, fmf);
431 Value expReal = arith::AddFOp::create(
b, expm1Real, one, fmf);
433 Value sinImag = math::SinOp::create(
b,
imag, fmf);
434 Value cosm1Imag = emitCosm1(
imag, fmf,
b);
435 Value cosImag = arith::AddFOp::create(
b, cosm1Imag, one, fmf);
437 Value realResult = arith::AddFOp::create(
438 b, arith::MulFOp::create(
b, expm1Real, cosImag, fmf), cosm1Imag, fmf);
440 Value imagIsZero = arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
imag,
441 zero, fmf.getValue());
442 Value imagResult = arith::SelectOp::create(
443 b, imagIsZero, zero, arith::MulFOp::create(
b, expReal, sinImag, fmf));
445 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, realResult,
451 Value emitCosm1(Value arg, arith::FastMathFlagsAttr fmf,
452 ImplicitLocOpBuilder &
b)
const {
453 auto argType = mlir::cast<FloatType>(arg.
getType());
454 auto negHalf = arith::ConstantOp::create(
b,
b.getFloatAttr(argType, -0.5));
455 auto one = arith::ConstantOp::create(
b,
b.getFloatAttr(argType, 1.0));
458 SmallVector<double, 7> kCoeffs{
459 4.7377507964246204691685E-14, -1.1470284843425359765671E-11,
460 2.0876754287081521758361E-9, -2.7557319214999787979814E-7,
461 2.4801587301570552304991E-5, -1.3888888888888872993737E-3,
462 4.1666666666666666609054E-2,
464 Value cos = math::CosOp::create(
b, arg, fmf);
465 Value forLargeArg = arith::SubFOp::create(
b, cos, one, fmf);
467 Value argPow2 = arith::MulFOp::create(
b, arg, arg, fmf);
468 Value argPow4 = arith::MulFOp::create(
b, argPow2, argPow2, fmf);
469 Value poly = evaluatePolynomial(
b, argPow2, kCoeffs, fmf);
472 arith::AddFOp::create(
b, arith::MulFOp::create(
b, argPow4, poly, fmf),
473 arith::MulFOp::create(
b, negHalf, argPow2, fmf));
477 arith::ConstantOp::create(
b,
b.getFloatAttr(argType, 0.61685));
478 Value cond = arith::CmpFOp::create(
b, arith::CmpFPredicate::OGE, argPow2,
479 piOver4Pow2, fmf.getValue());
480 return arith::SelectOp::create(
b, cond, forLargeArg, forSmallArg);
484struct LogOpConversion :
public OpConversionPattern<complex::LogOp> {
485 using OpConversionPattern<complex::LogOp>::OpConversionPattern;
488 matchAndRewrite(complex::LogOp op, OpAdaptor adaptor,
489 ConversionPatternRewriter &rewriter)
const override {
490 auto type = cast<ComplexType>(adaptor.getComplex().getType());
491 auto elementType = cast<FloatType>(type.getElementType());
492 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
493 mlir::ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
495 Value
abs = complex::AbsOp::create(
b, elementType, adaptor.getComplex(),
497 Value resultReal = math::LogOp::create(
b, elementType, abs, fmf.getValue());
498 Value
real = complex::ReOp::create(
b, elementType, adaptor.getComplex());
499 Value
imag = complex::ImOp::create(
b, elementType, adaptor.getComplex());
501 math::Atan2Op::create(
b, elementType,
imag,
real, fmf.getValue());
502 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
508struct Log1pOpConversion :
public OpConversionPattern<complex::Log1pOp> {
509 using OpConversionPattern<complex::Log1pOp>::OpConversionPattern;
512 matchAndRewrite(complex::Log1pOp op, OpAdaptor adaptor,
513 ConversionPatternRewriter &rewriter)
const override {
514 auto type = cast<ComplexType>(adaptor.getComplex().getType());
515 auto elementType = cast<FloatType>(type.getElementType());
516 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
517 mlir::ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
519 Value
real = complex::ReOp::create(
b, adaptor.getComplex());
520 Value
imag = complex::ImOp::create(
b, adaptor.getComplex());
522 Value half = arith::ConstantOp::create(
b, elementType,
523 b.getFloatAttr(elementType, 0.5));
524 Value one = arith::ConstantOp::create(
b, elementType,
525 b.getFloatAttr(elementType, 1));
526 Value realPlusOne = arith::AddFOp::create(
b,
real, one, fmf);
527 Value absRealPlusOne = math::AbsFOp::create(
b, realPlusOne, fmf);
528 Value absImag = math::AbsFOp::create(
b,
imag, fmf);
530 Value maxAbs = arith::MaximumFOp::create(
b, absRealPlusOne, absImag, fmf);
531 Value minAbs = arith::MinimumFOp::create(
b, absRealPlusOne, absImag, fmf);
533 Value useReal = arith::CmpFOp::create(
b, arith::CmpFPredicate::OGT,
534 realPlusOne, absImag, fmf);
535 Value maxMinusOne = arith::SubFOp::create(
b, maxAbs, one, fmf);
536 Value maxAbsOfRealPlusOneAndImagMinusOne =
537 arith::SelectOp::create(
b, useReal,
real, maxMinusOne);
538 arith::FastMathFlags fmfWithNaNInf = arith::bitEnumClear(
539 fmf, arith::FastMathFlags::nnan | arith::FastMathFlags::ninf);
540 Value minMaxRatio = arith::DivFOp::create(
b, minAbs, maxAbs, fmfWithNaNInf);
541 Value logOfMaxAbsOfRealPlusOneAndImag =
542 math::Log1pOp::create(
b, maxAbsOfRealPlusOneAndImagMinusOne, fmf);
543 Value logOfSqrtPart = math::Log1pOp::create(
544 b, arith::MulFOp::create(
b, minMaxRatio, minMaxRatio, fmfWithNaNInf),
546 Value r = arith::AddFOp::create(
547 b, arith::MulFOp::create(
b, half, logOfSqrtPart, fmfWithNaNInf),
548 logOfMaxAbsOfRealPlusOneAndImag, fmfWithNaNInf);
549 Value resultReal = arith::SelectOp::create(
551 arith::CmpFOp::create(
b, arith::CmpFPredicate::UNO, r, r,
554 Value resultImag = math::Atan2Op::create(
b,
imag, realPlusOne, fmf);
555 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
561struct MulOpConversion :
public OpConversionPattern<complex::MulOp> {
562 using OpConversionPattern<complex::MulOp>::OpConversionPattern;
565 matchAndRewrite(complex::MulOp op, OpAdaptor adaptor,
566 ConversionPatternRewriter &rewriter)
const override {
567 mlir::ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
568 auto type = cast<ComplexType>(adaptor.getLhs().getType());
569 auto elementType = cast<FloatType>(type.getElementType());
570 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
571 auto fmfValue = fmf.getValue();
572 Value lhsReal = complex::ReOp::create(
b, elementType, adaptor.getLhs());
573 Value lhsImag = complex::ImOp::create(
b, elementType, adaptor.getLhs());
574 Value rhsReal = complex::ReOp::create(
b, elementType, adaptor.getRhs());
575 Value rhsImag = complex::ImOp::create(
b, elementType, adaptor.getRhs());
578 if (arith::bitEnumContainsAll(fmfValue, arith::FastMathFlags::contract)) {
579 Value lhsImagTimesRhsImag =
580 arith::MulFOp::create(
b, lhsImag, rhsImag, fmfValue);
581 Value negLhsImagTimesRhsImag =
582 arith::NegFOp::create(
b, lhsImagTimesRhsImag, fmfValue);
583 real = math::FmaOp::create(
b, lhsReal, rhsReal, negLhsImagTimesRhsImag,
586 Value lhsImagTimesRhsReal =
587 arith::MulFOp::create(
b, lhsImag, rhsReal, fmfValue);
588 imag = math::FmaOp::create(
b, lhsReal, rhsImag, lhsImagTimesRhsReal,
591 Value lhsRealTimesRhsReal =
592 arith::MulFOp::create(
b, lhsReal, rhsReal, fmfValue);
593 Value lhsImagTimesRhsImag =
594 arith::MulFOp::create(
b, lhsImag, rhsImag, fmfValue);
595 Value lhsImagTimesRhsReal =
596 arith::MulFOp::create(
b, lhsImag, rhsReal, fmfValue);
597 Value lhsRealTimesRhsImag =
598 arith::MulFOp::create(
b, lhsReal, rhsImag, fmfValue);
600 real = arith::SubFOp::create(
b, lhsRealTimesRhsReal, lhsImagTimesRhsImag,
602 imag = arith::AddFOp::create(
b, lhsImagTimesRhsReal, lhsRealTimesRhsImag,
605 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type,
real,
imag);
610struct NegOpConversion :
public OpConversionPattern<complex::NegOp> {
611 using OpConversionPattern<complex::NegOp>::OpConversionPattern;
614 matchAndRewrite(complex::NegOp op, OpAdaptor adaptor,
615 ConversionPatternRewriter &rewriter)
const override {
616 auto loc = op.getLoc();
617 auto type = cast<ComplexType>(adaptor.getComplex().getType());
618 auto elementType = cast<FloatType>(type.getElementType());
619 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
622 complex::ReOp::create(rewriter, loc, elementType, adaptor.getComplex());
624 complex::ImOp::create(rewriter, loc, elementType, adaptor.getComplex());
625 Value negReal = arith::NegFOp::create(rewriter, loc,
real, fmf);
626 Value negImag = arith::NegFOp::create(rewriter, loc,
imag, fmf);
627 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, negReal, negImag);
632struct SinOpConversion :
public TrigonometricOpConversion<complex::SinOp> {
633 using TrigonometricOpConversion<complex::SinOp>::TrigonometricOpConversion;
635 std::pair<Value, Value>
combine(Location loc, Value scaledExp,
636 Value reciprocalExp, Value sin, Value cos,
637 ConversionPatternRewriter &rewriter,
638 arith::FastMathFlagsAttr fmf)
const override {
649 arith::AddFOp::create(rewriter, loc, scaledExp, reciprocalExp, fmf);
650 Value resultReal = arith::MulFOp::create(rewriter, loc, sum, sin, fmf);
652 arith::SubFOp::create(rewriter, loc, scaledExp, reciprocalExp, fmf);
653 Value resultImag = arith::MulFOp::create(rewriter, loc, diff, cos, fmf);
654 return {resultReal, resultImag};
659struct SqrtOpConversion :
public OpConversionPattern<complex::SqrtOp> {
660 using OpConversionPattern<complex::SqrtOp>::OpConversionPattern;
663 matchAndRewrite(complex::SqrtOp op, OpAdaptor adaptor,
664 ConversionPatternRewriter &rewriter)
const override {
665 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
667 auto type = cast<ComplexType>(op.getType());
668 auto elementType = cast<FloatType>(type.getElementType());
669 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
671 auto cst = [&](APFloat v) {
672 return arith::ConstantOp::create(
b, elementType,
673 b.getFloatAttr(elementType, v));
675 const auto &floatSemantics = elementType.getFloatSemantics();
676 Value zero = cst(APFloat::getZero(floatSemantics));
677 Value half = arith::ConstantOp::create(
b, elementType,
678 b.getFloatAttr(elementType, 0.5));
680 Value
real = complex::ReOp::create(
b, elementType, adaptor.getComplex());
681 Value
imag = complex::ImOp::create(
b, elementType, adaptor.getComplex());
682 Value absSqrt = computeAbs(
real,
imag, fmf,
b, AbsFn::sqrt);
683 Value argArg = math::Atan2Op::create(
b,
imag,
real, fmf);
684 Value sqrtArg = arith::MulFOp::create(
b, argArg, half, fmf);
685 Value cos = math::CosOp::create(
b, sqrtArg, fmf);
686 Value sin = math::SinOp::create(
b, sqrtArg, fmf);
690 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ, sin, zero, fmf);
692 Value resultReal = arith::MulFOp::create(
b, absSqrt, cos, fmf);
693 Value resultImag = arith::SelectOp::create(
694 b, sinIsZero, zero, arith::MulFOp::create(
b, absSqrt, sin, fmf));
695 if (!arith::bitEnumContainsAll(fmf, arith::FastMathFlags::nnan |
696 arith::FastMathFlags::ninf)) {
697 Value inf = cst(APFloat::getInf(floatSemantics));
698 Value negInf = cst(APFloat::getInf(floatSemantics,
true));
699 Value nan = cst(APFloat::getNaN(floatSemantics));
700 Value absImag = math::AbsFOp::create(
b, elementType,
imag, fmf);
702 Value absImagIsInf = arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
704 Value absImagIsNotInf = arith::CmpFOp::create(
705 b, arith::CmpFPredicate::ONE, absImag, inf, fmf);
707 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
real, inf, fmf);
708 Value realIsNegInf = arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
711 resultReal = arith::SelectOp::create(
712 b, arith::AndIOp::create(
b, realIsNegInf, absImagIsNotInf), zero,
714 resultReal = arith::SelectOp::create(
715 b, arith::OrIOp::create(
b, absImagIsInf, realIsInf), inf, resultReal);
717 Value imagSignInf = math::CopySignOp::create(
b, inf,
imag, fmf);
718 resultImag = arith::SelectOp::create(
720 arith::CmpFOp::create(
b, arith::CmpFPredicate::UNO, absSqrt, absSqrt),
722 resultImag = arith::SelectOp::create(
723 b, arith::OrIOp::create(
b, absImagIsInf, realIsNegInf), imagSignInf,
728 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ, absSqrt, zero, fmf);
729 resultReal = arith::SelectOp::create(
b, resultIsZero, zero, resultReal);
730 resultImag = arith::SelectOp::create(
b, resultIsZero, zero, resultImag);
732 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
738struct SignOpConversion :
public OpConversionPattern<complex::SignOp> {
739 using OpConversionPattern<complex::SignOp>::OpConversionPattern;
742 matchAndRewrite(complex::SignOp op, OpAdaptor adaptor,
743 ConversionPatternRewriter &rewriter)
const override {
744 auto type = cast<ComplexType>(adaptor.getComplex().getType());
745 auto elementType = cast<FloatType>(type.getElementType());
746 mlir::ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
747 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
749 Value
real = complex::ReOp::create(
b, elementType, adaptor.getComplex());
750 Value
imag = complex::ImOp::create(
b, elementType, adaptor.getComplex());
752 arith::ConstantOp::create(
b, elementType,
b.getZeroAttr(elementType));
754 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
real, zero);
756 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
imag, zero);
757 Value isZero = arith::AndIOp::create(
b, realIsZero, imagIsZero);
759 complex::AbsOp::create(
b, elementType, adaptor.getComplex(), fmf);
760 Value realSign = arith::DivFOp::create(
b,
real, abs, fmf);
761 Value imagSign = arith::DivFOp::create(
b,
imag, abs, fmf);
762 Value sign = complex::CreateOp::create(
b, type, realSign, imagSign);
763 rewriter.replaceOpWithNewOp<arith::SelectOp>(op, isZero,
764 adaptor.getComplex(), sign);
769template <
typename Op>
770struct TanTanhOpConversion :
public OpConversionPattern<Op> {
771 using OpConversionPattern<
Op>::OpConversionPattern;
774 matchAndRewrite(Op op,
typename Op::Adaptor adaptor,
775 ConversionPatternRewriter &rewriter)
const override {
776 ImplicitLocOpBuilder
b(op.
getLoc(), rewriter);
778 auto type = cast<ComplexType>(adaptor.getComplex().getType());
779 auto elementType = cast<FloatType>(type.getElementType());
780 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
781 const auto &floatSemantics = elementType.getFloatSemantics();
784 complex::ReOp::create(
b, loc, elementType, adaptor.getComplex());
786 complex::ImOp::create(
b, loc, elementType, adaptor.getComplex());
788 if constexpr (std::is_same_v<Op, complex::TanOp>) {
791 real = arith::NegFOp::create(
b,
real, fmf);
794 auto cst = [&](APFloat v) {
795 return arith::ConstantOp::create(
b, elementType,
796 b.getFloatAttr(elementType, v));
798 Value inf = cst(APFloat::getInf(floatSemantics));
799 Value four = arith::ConstantOp::create(
b, elementType,
800 b.getFloatAttr(elementType, 4.0));
802 arith::FastMathFlags fmfWithInf =
803 arith::bitEnumClear(fmf, arith::FastMathFlags::ninf);
804 Value twoReal = arith::AddFOp::create(
b,
real,
real, fmfWithInf);
805 Value negTwoReal = arith::NegFOp::create(
b, twoReal, fmfWithInf);
806 Value expTwoRealMinusOne = math::ExpM1Op::create(
b, twoReal, fmfWithInf);
807 Value expNegTwoRealMinusOne =
808 math::ExpM1Op::create(
b, negTwoReal, fmfWithInf);
809 Value realNum = arith::SubFOp::create(
b, expTwoRealMinusOne,
810 expNegTwoRealMinusOne, fmf);
811 Value expProduct = arith::MulFOp::create(
b, expTwoRealMinusOne,
812 expNegTwoRealMinusOne, fmfWithInf);
813 Value expSumMinusTwo = arith::NegFOp::create(
b, expProduct, fmfWithInf);
815 Value cosImag = math::CosOp::create(
b,
imag, fmf);
816 Value cosImagSq = arith::MulFOp::create(
b, cosImag, cosImag, fmf);
817 Value twoCosTwoImagPlusOne = arith::MulFOp::create(
b, cosImagSq, four, fmf);
818 Value sinImag = math::SinOp::create(
b,
imag, fmf);
820 Value imagNum = arith::MulFOp::create(
821 b, four, arith::MulFOp::create(
b, cosImag, sinImag, fmf), fmf);
823 Value denom = arith::AddFOp::create(
b, expSumMinusTwo, twoCosTwoImagPlusOne,
826 Value isInf = arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
827 expSumMinusTwo, inf, fmfWithInf);
828 Value negOne = arith::ConstantOp::create(
b, elementType,
829 b.getFloatAttr(elementType, -1.0));
830 Value realLimit = math::CopySignOp::create(
b, negOne,
real, fmf);
832 Value resultReal = arith::SelectOp::create(
833 b, isInf, realLimit, arith::DivFOp::create(
b, realNum, denom, fmf));
834 Value resultImag = arith::DivFOp::create(
b, imagNum, denom, fmfWithInf);
836 if (!arith::bitEnumContainsAll(fmf, arith::FastMathFlags::nnan |
837 arith::FastMathFlags::ninf)) {
838 Value zero = arith::ConstantOp::create(
b, elementType,
839 b.getFloatAttr(elementType, 0.0));
840 Value nan = cst(APFloat::getNaN(floatSemantics));
841 bool noInfs = arith::bitEnumContainsAll(fmf, arith::FastMathFlags::ninf);
842 Value imagIsNotFinite;
845 arith::CmpFOp::create(
b, arith::CmpFPredicate::UNO,
imag,
imag);
847 Value absImag = math::AbsFOp::create(
b,
imag, fmf);
849 arith::CmpFOp::create(
b, arith::CmpFPredicate::UGE, absImag, inf);
852 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
imag, zero);
854 Value resultRealIsNaN = imagIsNotFinite;
855 Value resultImagIsZero = imagIsZero;
857 Value absReal = math::AbsFOp::create(
b,
real, fmf);
859 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ, absReal, inf);
860 Value absRealIsNotInf = arith::XOrIOp::create(
864 arith::AndIOp::create(
b, imagIsNotFinite, absRealIsNotInf);
865 resultImagIsZero = arith::OrIOp::create(
867 arith::AndIOp::create(
b, absRealIsInf, imagIsNotFinite));
870 resultReal = arith::SelectOp::create(
b, resultRealIsNaN, nan, resultReal);
872 arith::SelectOp::create(
b, resultImagIsZero, zero, resultImag);
875 if constexpr (std::is_same_v<Op, complex::TanOp>) {
877 std::swap(resultReal, resultImag);
878 resultImag = arith::NegFOp::create(
b, resultImag, fmf);
881 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
887struct ConjOpConversion :
public OpConversionPattern<complex::ConjOp> {
888 using OpConversionPattern<complex::ConjOp>::OpConversionPattern;
891 matchAndRewrite(complex::ConjOp op, OpAdaptor adaptor,
892 ConversionPatternRewriter &rewriter)
const override {
894 auto type = cast<ComplexType>(adaptor.getComplex().getType());
895 auto elementType = cast<FloatType>(type.getElementType());
896 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
898 complex::ReOp::create(rewriter, loc, elementType, adaptor.getComplex());
900 complex::ImOp::create(rewriter, loc, elementType, adaptor.getComplex());
902 arith::NegFOp::create(rewriter, loc, elementType,
imag, fmf);
904 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type,
real, negImag);
915 arith::FastMathFlags fmf) {
916 auto elementType = cast<FloatType>(type.getElementType());
918 Value a = complex::ReOp::create(builder, lhs);
919 Value b = complex::ImOp::create(builder, lhs);
921 Value abs = complex::AbsOp::create(builder, lhs, fmf);
922 Value absToC = math::PowFOp::create(builder, abs, c, fmf);
924 Value negD = arith::NegFOp::create(builder, d, fmf);
925 Value argLhs = math::Atan2Op::create(builder,
b, a, fmf);
926 Value negDArgLhs = arith::MulFOp::create(builder, negD, argLhs, fmf);
927 Value expNegDArgLhs = math::ExpOp::create(builder, negDArgLhs, fmf);
929 Value coeff = arith::MulFOp::create(builder, absToC, expNegDArgLhs, fmf);
930 Value lnAbs = math::LogOp::create(builder, abs, fmf);
931 Value cArgLhs = arith::MulFOp::create(builder, c, argLhs, fmf);
932 Value dLnAbs = arith::MulFOp::create(builder, d, lnAbs, fmf);
933 Value q = arith::AddFOp::create(builder, cArgLhs, dLnAbs, fmf);
934 Value cosQ = math::CosOp::create(builder, q, fmf);
935 Value sinQ = math::SinOp::create(builder, q, fmf);
937 Value inf = arith::ConstantOp::create(
938 builder, elementType,
940 APFloat::getInf(elementType.getFloatSemantics())));
941 Value zero = arith::ConstantOp::create(
942 builder, elementType, builder.
getFloatAttr(elementType, 0.0));
943 Value one = arith::ConstantOp::create(builder, elementType,
945 Value complexOne = complex::CreateOp::create(builder, type, one, zero);
946 Value complexZero = complex::CreateOp::create(builder, type, zero, zero);
947 Value complexInf = complex::CreateOp::create(builder, type, inf, zero);
954 arith::CmpFOp::create(builder, arith::CmpFPredicate::OEQ, abs, zero, fmf);
956 arith::CmpFOp::create(builder, arith::CmpFPredicate::OEQ, d, zero, fmf);
958 arith::CmpFOp::create(builder, arith::CmpFPredicate::OEQ, c, zero, fmf);
960 arith::CmpFOp::create(builder, arith::CmpFPredicate::OEQ,
b, zero, fmf);
963 arith::CmpFOp::create(builder, arith::CmpFPredicate::OLE, zero, c, fmf);
964 Value coeffCosQ = arith::MulFOp::create(builder, coeff, cosQ, fmf);
965 Value coeffSinQ = arith::MulFOp::create(builder, coeff, sinQ, fmf);
966 Value complexOneOrZero =
967 arith::SelectOp::create(builder, cEqZero, complexOne, complexZero);
969 complex::CreateOp::create(builder, type, coeffCosQ, coeffSinQ);
970 Value cutoff0 = arith::SelectOp::create(
972 arith::AndIOp::create(
973 builder, arith::AndIOp::create(builder, absEqZero, dEqZero), zeroLeC),
974 complexOneOrZero, coeffCosSin);
980 Value rhsEqZero = arith::AndIOp::create(builder, cEqZero, dEqZero);
982 arith::SelectOp::create(builder, rhsEqZero, complexOne, cutoff0);
986 Value lhsEqOne = arith::AndIOp::create(
988 arith::CmpFOp::create(builder, arith::CmpFPredicate::OEQ, a, one, fmf),
991 arith::SelectOp::create(builder, lhsEqOne, complexOne, cutoff1);
995 Value lhsEqInf = arith::AndIOp::create(
997 arith::CmpFOp::create(builder, arith::CmpFPredicate::OEQ, a, inf, fmf),
999 Value rhsGt0 = arith::AndIOp::create(
1001 arith::CmpFOp::create(builder, arith::CmpFPredicate::OGT, c, zero, fmf));
1002 Value cutoff3 = arith::SelectOp::create(
1003 builder, arith::AndIOp::create(builder, lhsEqInf, rhsGt0), complexInf,
1008 Value rhsLt0 = arith::AndIOp::create(
1010 arith::CmpFOp::create(builder, arith::CmpFPredicate::OLT, c, zero, fmf));
1011 Value cutoff4 = arith::SelectOp::create(
1012 builder, arith::AndIOp::create(builder, lhsEqInf, rhsLt0), complexZero,
1018struct PowiOpConversion :
public OpConversionPattern<complex::PowiOp> {
1019 using OpConversionPattern<complex::PowiOp>::OpConversionPattern;
1022 matchAndRewrite(complex::PowiOp op, OpAdaptor adaptor,
1023 ConversionPatternRewriter &rewriter)
const override {
1024 ImplicitLocOpBuilder builder(op.getLoc(), rewriter);
1025 auto type = cast<ComplexType>(op.getType());
1026 auto elementType = cast<FloatType>(type.getElementType());
1028 Value floatExponent =
1029 arith::SIToFPOp::create(builder, elementType, adaptor.getRhs());
1030 Value zero = arith::ConstantOp::create(
1031 builder, elementType, builder.
getFloatAttr(elementType, 0.0));
1032 Value complexExponent =
1033 complex::CreateOp::create(builder, type, floatExponent, zero);
1035 auto pow = complex::PowOp::create(builder, type, adaptor.getLhs(),
1036 complexExponent, op.getFastmathAttr());
1037 rewriter.replaceOp(op, pow.getResult());
1042struct PowOpConversion :
public OpConversionPattern<complex::PowOp> {
1043 using OpConversionPattern<complex::PowOp>::OpConversionPattern;
1046 matchAndRewrite(complex::PowOp op, OpAdaptor adaptor,
1047 ConversionPatternRewriter &rewriter)
const override {
1048 mlir::ImplicitLocOpBuilder builder(op.getLoc(), rewriter);
1049 auto type = cast<ComplexType>(adaptor.getLhs().getType());
1050 auto elementType = cast<FloatType>(type.getElementType());
1052 Value c = complex::ReOp::create(builder, elementType, adaptor.getRhs());
1053 Value d = complex::ImOp::create(builder, elementType, adaptor.getRhs());
1055 rewriter.replaceOp(op, {powOpConversionImpl(builder, type, adaptor.getLhs(),
1056 c, d, op.getFastmath())});
1061struct RsqrtOpConversion :
public OpConversionPattern<complex::RsqrtOp> {
1062 using OpConversionPattern<complex::RsqrtOp>::OpConversionPattern;
1065 matchAndRewrite(complex::RsqrtOp op, OpAdaptor adaptor,
1066 ConversionPatternRewriter &rewriter)
const override {
1067 mlir::ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
1068 auto type = cast<ComplexType>(adaptor.getComplex().getType());
1069 auto elementType = cast<FloatType>(type.getElementType());
1071 arith::FastMathFlags fmf = op.getFastMathFlagsAttr().getValue();
1073 auto cst = [&](APFloat v) {
1074 return arith::ConstantOp::create(
b, elementType,
1075 b.getFloatAttr(elementType, v));
1077 const auto &floatSemantics = elementType.getFloatSemantics();
1078 Value zero = cst(APFloat::getZero(floatSemantics));
1079 Value inf = cst(APFloat::getInf(floatSemantics));
1080 Value negHalf = arith::ConstantOp::create(
1081 b, elementType,
b.getFloatAttr(elementType, -0.5));
1082 Value nan = cst(APFloat::getNaN(floatSemantics));
1084 Value
real = complex::ReOp::create(
b, elementType, adaptor.getComplex());
1085 Value
imag = complex::ImOp::create(
b, elementType, adaptor.getComplex());
1086 Value absRsqrt = computeAbs(
real,
imag, fmf,
b, AbsFn::rsqrt);
1087 Value argArg = math::Atan2Op::create(
b,
imag,
real, fmf);
1088 Value rsqrtArg = arith::MulFOp::create(
b, argArg, negHalf, fmf);
1089 Value cos = math::CosOp::create(
b, rsqrtArg, fmf);
1090 Value sin = math::SinOp::create(
b, rsqrtArg, fmf);
1092 Value resultReal = arith::MulFOp::create(
b, absRsqrt, cos, fmf);
1093 Value resultImag = arith::MulFOp::create(
b, absRsqrt, sin, fmf);
1095 if (!arith::bitEnumContainsAll(fmf, arith::FastMathFlags::nnan |
1096 arith::FastMathFlags::ninf)) {
1097 Value realSignedZero = math::CopySignOp::create(
b, zero,
real, fmf);
1098 Value imagSignedZero = math::CopySignOp::create(
b, zero,
imag, fmf);
1099 Value negImagSignedZero = arith::NegFOp::create(
b, imagSignedZero, fmf);
1101 Value absReal = math::AbsFOp::create(
b,
real, fmf);
1102 Value absImag = math::AbsFOp::create(
b,
imag, fmf);
1104 Value absImagIsInf = arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
1107 arith::CmpFOp::create(
b, arith::CmpFPredicate::UNO,
real,
real, fmf);
1108 Value realIsInf = arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
1110 Value inIsNanInf = arith::AndIOp::create(
b, absImagIsInf, realIsNan);
1112 Value resultIsZero = arith::OrIOp::create(
b, inIsNanInf, realIsInf);
1115 arith::SelectOp::create(
b, resultIsZero, realSignedZero, resultReal);
1116 resultImag = arith::SelectOp::create(
b, resultIsZero, negImagSignedZero,
1121 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
real, zero, fmf);
1123 arith::CmpFOp::create(
b, arith::CmpFPredicate::OEQ,
imag, zero, fmf);
1124 Value isZero = arith::AndIOp::create(
b, isRealZero, isImagZero);
1126 resultReal = arith::SelectOp::create(
b, isZero, inf, resultReal);
1127 resultImag = arith::SelectOp::create(
b, isZero, nan, resultImag);
1129 rewriter.replaceOpWithNewOp<complex::CreateOp>(op, type, resultReal,
1135struct AngleOpConversion :
public OpConversionPattern<complex::AngleOp> {
1136 using OpConversionPattern<complex::AngleOp>::OpConversionPattern;
1139 matchAndRewrite(complex::AngleOp op, OpAdaptor adaptor,
1140 ConversionPatternRewriter &rewriter)
const override {
1141 auto loc = op.getLoc();
1142 auto type = op.getType();
1143 arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr();
1146 complex::ReOp::create(rewriter, loc, type, adaptor.getComplex());
1148 complex::ImOp::create(rewriter, loc, type, adaptor.getComplex());
1150 rewriter.replaceOpWithNewOp<math::Atan2Op>(op,
imag,
real, fmf);
1165 BinaryComplexOpConversion<complex::AddOp, arith::AddFOp>,
1166 BinaryComplexOpConversion<complex::SubOp, arith::SubFOp>,
1167 ComparisonOpConversion<complex::EqualOp, arith::CmpFPredicate::OEQ>,
1168 ComparisonOpConversion<complex::NotEqualOp, arith::CmpFPredicate::UNE>,
1180 TanTanhOpConversion<complex::TanOp>,
1181 TanTanhOpConversion<complex::TanhOp>,
1187 patterns.
add<DivOpConversion>(patterns.
getContext(), complexRange);
1193struct ConvertComplexToStandardPass
1195 ConvertComplexToStandardPass> {
1198 void runOnOperation()
override;
1201void ConvertComplexToStandardPass::runOnOperation() {
1207 target.addLegalDialect<arith::ArithDialect, math::MathDialect>();
1208 target.addLegalOp<complex::CreateOp, complex::ImOp, complex::ReOp>();
1210 applyPartialConversion(getOperation(),
target, std::move(patterns))))
1211 signalPassFailure();
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
FloatAttr getFloatAttr(Type type, double value)
ImplicitLocOpBuilder maintains a 'current location', allowing use of the create<> method without spec...
Location getLoc()
The source location the operation was defined or derived from.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
static ConstantIntOp create(OpBuilder &builder, Location location, int64_t value, unsigned width)
NestedPattern Op(FilterFunctionType filter=defaultFilterFunction)
void convertDivToStandardUsingAlgebraic(ConversionPatternRewriter &rewriter, Location loc, Value lhsRe, Value lhsIm, Value rhsRe, Value rhsIm, arith::FastMathFlagsAttr fmf, Value *resultRe, Value *resultIm)
convert a complex division to the arith/math dialects using algebraic method
void convertDivToStandardUsingRangeReduction(ConversionPatternRewriter &rewriter, Location loc, Value lhsRe, Value lhsIm, Value rhsRe, Value rhsIm, arith::FastMathFlagsAttr fmf, Value *resultRe, Value *resultIm)
convert a complex division to the arith/math dialects using Smith's method
Fraction abs(const Fraction &f)
OwningOpRef< spirv::ModuleOp > combine(ArrayRef< spirv::ModuleOp > inputModules, OpBuilder &combinedModuleBuilder, SymbolRenameListener symRenameListener)
Combines a list of SPIR-V inputModules into one.
Include the generated interface declarations.
constexpr T real(const NonFloatComplex< T > &x)
void populateComplexToStandardConversionPatterns(RewritePatternSet &patterns, mlir::complex::ComplexRangeFlags complexRange=mlir::complex::ComplexRangeFlags::improved)
Populate the given list with patterns that convert from Complex to Standard.
constexpr T imag(const NonFloatComplex< T > &x)