20#define GEN_PASS_DEF_ARITHEXPANDOPSPASS
21#include "mlir/Dialect/Arith/Transforms/Passes.h.inc"
31 if (
auto shapedTy = dyn_cast<ShapedType>(type)) {
32 return arith::ConstantOp::create(rewriter, loc,
35 return arith::ConstantOp::create(rewriter, loc, attr);
42 if (
auto shapedTy = dyn_cast<ShapedType>(type)) {
43 return arith::ConstantOp::create(rewriter, loc,
46 return arith::ConstantOp::create(rewriter, loc, attr);
53 if (
auto shapedTy = dyn_cast<ShapedType>(type)) {
54 return arith::ConstantOp::create(rewriter, loc,
58 return arith::ConstantOp::create(rewriter, loc, attr);
63 if (
auto shapedTy = dyn_cast<ShapedType>(cloneFrom)) {
64 return shapedTy.clone(cloneTo);
71 arith::FastMathFlagsAttr fastmath) {
72 if (fastmath && arith::bitEnumContainsAll(fastmath.getValue(),
73 arith::FastMathFlags::nnan))
74 return arith::FastMathFlagsAttr::get(ctx, arith::FastMathFlags::nnan);
80template <
typename OpTy, arith::CmpIPredicate pred>
83 using OpRewritePattern<OpTy>::OpRewritePattern;
85 LogicalResult matchAndRewrite(OpTy op,
86 PatternRewriter &rewriter)
const final {
87 Value
lhs = op.getLhs();
88 Value
rhs = op.getRhs();
90 Value cmp = arith::CmpIOp::create(rewriter, op.getLoc(), pred,
lhs,
rhs);
91 rewriter.replaceOpWithNewOp<arith::SelectOp>(op, cmp,
lhs,
rhs);
96template <
typename OpTy, arith::CmpFPredicate pred>
99 using OpRewritePattern<OpTy>::OpRewritePattern;
101 LogicalResult matchAndRewrite(OpTy op,
102 PatternRewriter &rewriter)
const final {
103 Value
lhs = op.getLhs();
104 Value
rhs = op.getRhs();
106 Location loc = op.getLoc();
108 static_assert(pred == arith::CmpFPredicate::UGT ||
109 pred == arith::CmpFPredicate::ULT,
110 "pred must be either UGT or ULT");
111 Value cmp = arith::CmpFOp::create(rewriter, loc, pred,
lhs,
rhs);
112 Value select = arith::SelectOp::create(rewriter, loc, cmp,
lhs,
rhs);
115 Value isNaN = arith::CmpFOp::create(rewriter, loc,
116 arith::CmpFPredicate::UNO,
rhs,
rhs);
117 rewriter.replaceOpWithNewOp<arith::SelectOp>(op, isNaN,
rhs, select);
122template <
typename OpTy, arith::CmpFPredicate pred>
125 using OpRewritePattern<OpTy>::OpRewritePattern;
127 LogicalResult matchAndRewrite(OpTy op,
128 PatternRewriter &rewriter)
const final {
129 Value
lhs = op.getLhs();
130 Value
rhs = op.getRhs();
132 Location loc = op.getLoc();
134 static_assert(pred == arith::CmpFPredicate::UGT ||
135 pred == arith::CmpFPredicate::ULT,
136 "pred must be either UGT or ULT");
137 Value cmp = arith::CmpFOp::create(rewriter, loc, pred,
lhs,
rhs);
138 Value select = arith::SelectOp::create(rewriter, loc, cmp,
lhs,
rhs);
141 Value isNaN = arith::CmpFOp::create(rewriter, loc,
142 arith::CmpFPredicate::UNO,
lhs,
lhs);
143 rewriter.replaceOpWithNewOp<arith::SelectOp>(op, isNaN,
rhs, select);
150 LogicalResult matchAndRewrite(arith::ExtFOp op,
151 PatternRewriter &rewriter)
const final {
152 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
153 auto operand = op.getOperand();
154 Type operandTy = operand.getType();
155 Type resultTy = op.getType();
160 return rewriter.notifyMatchFailure(op,
"not a ext of bf16 to f32.");
166 Value bitcast = arith::BitcastOp::create(
b, i16Ty, operand);
167 Value exti = arith::ExtUIOp::create(
b, i32Ty, bitcast);
169 Value c16 =
createConst(op.getLoc(), i32Ty, 16, rewriter);
170 Value shl = arith::ShLIOp::create(
b, exti, c16);
171 Value
result = arith::BitcastOp::create(
b, resultTy, shl);
173 rewriter.replaceOp(op,
result);
178struct BFloat16TruncFOpConverter :
public OpRewritePattern<arith::TruncFOp> {
180 LogicalResult matchAndRewrite(arith::TruncFOp op,
181 PatternRewriter &rewriter)
const final {
182 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
183 auto operand = op.getOperand();
184 Type operandTy = operand.getType();
185 Type resultTy = op.getType();
190 return rewriter.notifyMatchFailure(op,
"not a trunc of f32 to bf16.");
193 if (op.getRoundingmodeAttr()) {
194 return rewriter.notifyMatchFailure(
195 op,
"only applicable to default rounding mode.");
215 arith::CmpFOp::create(
b, arith::CmpFPredicate::UNE, operand, operand);
217 Value c7FFF =
createConst(op.getLoc(), i32Ty, 0x7fff, rewriter);
219 Value c7FC0I16 =
createConst(op.getLoc(), i16Ty, 0x7fc0, rewriter);
221 Value c16 =
createConst(op.getLoc(), i32Ty, 16, rewriter);
222 Value c1 =
createConst(op.getLoc(), i32Ty, 1, rewriter);
224 Value bitcast = arith::BitcastOp::create(
b, i32Ty, operand);
227 arith::AndIOp::create(
b, arith::ShRUIOp::create(
b, bitcast, c16), c1);
230 Value roundingBias = arith::AddIOp::create(
b, bit16, c7FFF);
237 Value biased = arith::AddIOp::create(
b, bitcast, roundingBias);
240 Value biasedAndShifted = arith::ShRUIOp::create(
b, biased, c16);
241 Value normalCaseResultI16 =
242 arith::TruncIOp::create(
b, i16Ty, biasedAndShifted);
246 arith::SelectOp::create(
b, isNan, c7FC0I16, normalCaseResultI16);
247 Value
result = arith::BitcastOp::create(
b, resultTy, select);
248 rewriter.replaceOp(op,
result);
284 LogicalResult matchAndRewrite(arith::ExtFOp op,
288 Value operand = op.getOperand();
290 Type resultTy = op.getType();
294 if (!isa<Float4E2M1FNType>(operandETy))
300 Value i4Bits = arith::BitcastOp::create(
b, i4Ty, operand);
308 Value i4BitsNoSign = arith::AndIOp::create(
b, i4Bits, c0x7);
312 Value bits1To24 = arith::ShLIOp::create(
b, i4BitsNoSign, c0x2);
314 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, i4BitsNoSign, c0x1);
315 bits1To24 = arith::SelectOp::create(
b, isHalf, c0x0, bits1To24);
316 bits1To24 = arith::ExtUIOp::create(
b, i32Ty, bits1To24);
317 bits1To24 = arith::ShLIOp::create(
b, bits1To24, c0x00000014);
324 arith::CmpIOp::create(
b, arith::CmpIPredicate::uge, i4BitsNoSign, c0x4);
326 arith::SelectOp::create(
b, useLargerExp, highExpBits, lowExpBits);
328 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, i4BitsNoSign, c0x0);
329 bits25To31 = arith::SelectOp::create(
b, zeroExp, zeroExpBits, bits25To31);
335 arith::CmpIOp::create(
b, arith::CmpIPredicate::uge, i4Bits, c0x8);
337 arith::SelectOp::create(
b, negative, c0x80000000, zeroExpBits);
340 Value bits1To31 = arith::AddIOp::create(
b, bits1To24, bits25To31);
341 Value bits1To32 = arith::AddIOp::create(
b, bits1To31, bit32);
342 Value result = arith::BitcastOp::create(
b, f32Ty, bits1To32);
343 if (!isa<Float32Type>(resultETy))
353 LogicalResult matchAndRewrite(arith::ExtFOp op,
356 Value operand = op.getOperand();
358 Type resultTy = op.getType();
362 if (!llvm::isa<Float8E8M0FNUType>(operandETy)) {
370 Value bitcast = arith::BitcastOp::create(
b, i8Ty, operand);
371 Value cF32MantissaWidth =
createConst(op->getLoc(), i32Ty, 23, rewriter);
372 Value exti = arith::ExtUIOp::create(
b, i32Ty, bitcast);
373 Value f32Bits = arith::ShLIOp::create(
b, exti, cF32MantissaWidth);
377 Value cF8Min =
createConst(op.getLoc(), i8Ty, 0, rewriter);
378 Value cF32Min =
createConst(op.getLoc(), i32Ty, 0x00400000, rewriter);
380 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, bitcast, cF8Min);
381 f32Bits = arith::SelectOp::create(
b, isMin, cF32Min, f32Bits);
384 auto fastMath = op.getFastmathAttr();
385 bool NoNaN = fastMath
386 ? (fastMath.getValue() & arith::FastMathFlags::nnan) ==
387 arith::FastMathFlags::nnan
390 Value cF8NaN =
createConst(op.getLoc(), i8Ty, 0xff, rewriter);
391 Value cF32NaN =
createConst(op.getLoc(), i32Ty, 0x7fc00000, rewriter);
393 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, bitcast, cF8NaN);
395 f32Bits = arith::SelectOp::create(
b, isNan, cF32NaN, f32Bits);
398 Value
result = arith::BitcastOp::create(
b, f32Ty, f32Bits);
400 result = arith::TruncFOp::create(
b, resultTy,
result,
nullptr,
401 op.getFastmathAttr());
403 result = arith::ExtFOp::create(
b, resultTy,
result, op.getFastmathAttr());
440 LogicalResult matchAndRewrite(arith::TruncFOp op,
441 PatternRewriter &rewriter)
const final {
442 Location loc = op.getLoc();
443 ImplicitLocOpBuilder
b(loc, rewriter);
444 Value operand = op.getOperand();
445 Type operandTy = operand.
getType();
446 Type resultTy = op.getType();
455 if (!isa<Float4E2M1FNType>(resultETy))
457 if (!isa<Float32Type>(operandETy))
459 arith::ExtFOp::create(
b, f32Ty, operand, arith::FastMathFlagsAttr{});
463 Value c0x00000016 =
createConst(loc, i32Ty, 22, rewriter);
464 Value c0x00 =
createConst(loc, i8Ty, 0x00, rewriter);
465 Value c0xff =
createConst(loc, i8Ty, 0xff, rewriter);
466 Value zeroExpBits =
createConst(loc, i32Ty, 0, rewriter);
471 Value operandClamped = arith::MinNumFOp::create(
b, cHigherBound, operand);
472 operandClamped = arith::MaxNumFOp::create(
b, cLowerBound, operandClamped);
473 Value f32Bits = arith::BitcastOp::create(
b, i32Ty, operandClamped);
476 Value cF32ExpManWidth =
createConst(loc, i32Ty, 31, rewriter);
477 Value f32Sign = arith::ShRUIOp::create(
b, f32Bits, cF32ExpManWidth);
478 Value f4Sign = arith::TruncIOp::create(
b, i4Ty, f32Sign);
479 Value f4Bits = arith::ShLIOp::create(
b, f4Sign, c0x3);
482 Value biasAdjustment =
createConst(loc, i32Ty, 0x7e, rewriter);
483 Value cF4MantissaWidth = c0x1;
484 Value cF32MantissaWidth =
createConst(loc, i32Ty, 23, rewriter);
485 Value f32SignExp = arith::ShRUIOp::create(
b, f32Bits, cF32MantissaWidth);
486 Value biasAdjustedSignExp =
487 arith::SubIOp::create(
b, f32SignExp, biasAdjustment);
488 Value f4Exp = arith::TruncIOp::create(
b, i4Ty, biasAdjustedSignExp);
489 f4Exp = arith::ShLIOp::create(
b, f4Exp, cF4MantissaWidth);
490 f4Bits = arith::AddIOp::create(
b, f4Bits, f4Exp);
493 Value cF32FirstBitMask =
createConst(loc, i32Ty, 0x400000, rewriter);
494 Value man1Bit = arith::AndIOp::create(
b, f32Bits, cF32FirstBitMask);
495 man1Bit = arith::ShRUIOp::create(
b, man1Bit, c0x00000016);
496 Value f4Man = arith::TruncIOp::create(
b, i4Ty, man1Bit);
497 f4Bits = arith::AddIOp::create(
b, f4Bits, f4Man);
500 Value cF32MantissaMask =
createConst(loc, i32Ty, 0x7fffff, rewriter);
501 Value f8Exp = arith::TruncIOp::create(
b, i8Ty, biasAdjustedSignExp);
503 arith::CmpIOp::create(
b, arith::CmpIPredicate::sle, f8Exp, c0x00);
505 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, f8Exp, c0xff);
506 Value man23Bits = arith::AndIOp::create(
b, f32Bits, cF32MantissaMask);
507 Value isNonZeroMan = arith::CmpIOp::create(
b, arith::CmpIPredicate::ugt,
508 man23Bits, zeroExpBits);
509 Value roundToHalf = arith::AndIOp::create(
b, isNegOneExp, isNonZeroMan);
511 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, f8Exp, c0x00);
512 Value subnormalF4Bits =
createConst(loc, i4Ty, 0xf, rewriter);
513 Value halfF4Bits =
createConst(loc, i4Ty, 0x0, rewriter);
515 arith::SelectOp::create(
b, isSubnormal, subnormalF4Bits, f4Bits);
516 subResult = arith::SelectOp::create(
b, roundToHalf, halfF4Bits, subResult);
517 f4Bits = arith::SelectOp::create(
b, isZeroExp, f4Bits, subResult);
520 Value cF32Last22BitMask =
createConst(loc, i32Ty, 0x3fffff, rewriter);
521 Value cRound =
createConst(loc, i32Ty, 0x200000, rewriter);
522 Value man22Bits = arith::AndIOp::create(
b, f32Bits, cF32Last22BitMask);
524 arith::CmpIOp::create(
b, arith::CmpIPredicate::uge, man22Bits, cRound);
525 shouldRound = arith::OrIOp::create(
b, shouldRound, isSubnormal);
526 Value roundedF4Bits = arith::AddIOp::create(
b, f4Bits, c0x1);
527 f4Bits = arith::SelectOp::create(
b, shouldRound, roundedF4Bits, f4Bits);
529 Value
result = arith::BitcastOp::create(
b, resultTy, f4Bits);
542 LogicalResult matchAndRewrite(arith::TruncFOp op,
543 PatternRewriter &rewriter)
const final {
544 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
545 Value operand = op.getOperand();
546 Type operandTy = operand.
getType();
548 Type resultTy = op.getType();
550 if (!llvm::isa<Float8E8M0FNUType>(resultETy)) {
554 if (op.getRoundingmodeAttr()) {
556 op,
"only applicable to default rounding mode.");
566 operand = arith::ExtFOp::create(
567 b, f32Ty, operand,
getNNaNOnly(
b.getContext(), op.getFastmathAttr()));
569 operand = arith::TruncFOp::create(
570 b, f32Ty, operand, op.getRoundingmodeAttr(), op.getFastmathAttr());
572 Value f32Bits = arith::BitcastOp::create(
b, i32Ty, operand);
573 Value cF32MantissaWidth =
createConst(op->getLoc(), i32Ty, 23, rewriter);
574 Value f32SignExp = arith::ShRUIOp::create(
b, f32Bits, cF32MantissaWidth);
575 Value exp8Bits = arith::TruncIOp::create(
b, i8Ty, f32SignExp);
576 Value
result = arith::BitcastOp::create(
b, resultTy, exp8Bits);
590 LogicalResult matchAndRewrite(arith::ExtFOp op,
591 PatternRewriter &rewriter)
const final {
592 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
593 Value operand = op.getOperand();
594 Type operandTy = operand.
getType();
595 Type resultTy = op.getType();
599 if (!llvm::isa<Float8E5M2Type>(operandETy))
606 Value bitcast = arith::BitcastOp::create(
b, i8Ty, operand);
607 Value exti = arith::ExtUIOp::create(
b, i16Ty, bitcast);
608 Value c8 =
createConst(op.getLoc(), i16Ty, 8, rewriter);
609 Value f16Bits = arith::ShLIOp::create(
b, exti, c8);
610 Value f16 = arith::BitcastOp::create(
b, f16Ty, f16Bits);
613 if (!resultETy.
isF16()) {
615 result = arith::TruncFOp::create(
b, resultTy, f16,
nullptr,
616 op.getFastmathAttr());
618 result = arith::ExtFOp::create(
b, resultTy, f16, op.getFastmathAttr());
634 LogicalResult matchAndRewrite(arith::TruncFOp op,
635 PatternRewriter &rewriter)
const final {
636 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
637 Value operand = op.getOperand();
638 Type operandTy = operand.
getType();
639 Type resultTy = op.getType();
643 if (!llvm::isa<Float8E5M2Type>(resultETy))
645 if (op.getRoundingmodeAttr())
647 op,
"only applicable to default rounding mode.");
654 if (!operandETy.
isF16())
655 h16 = arith::TruncFOp::create(
b, f16Ty, operand,
nullptr,
656 op.getFastmathAttr());
658 Value isNan = arith::CmpFOp::create(
b, arith::CmpFPredicate::UNE, h16, h16);
659 Value h16Bits = arith::BitcastOp::create(
b, i16Ty, h16);
661 Value c7F =
createConst(op.getLoc(), i16Ty, 0x7f, rewriter);
662 Value c8 =
createConst(op.getLoc(), i16Ty, 8, rewriter);
663 Value c1 =
createConst(op.getLoc(), i16Ty, 1, rewriter);
665 arith::AndIOp::create(
b, arith::ShRUIOp::create(
b, h16Bits, c8), c1);
666 Value roundingBias = arith::AddIOp::create(
b, bit8, c7F);
667 Value biased = arith::AddIOp::create(
b, h16Bits, roundingBias);
668 Value biasedAndShifted = arith::ShRUIOp::create(
b, biased, c8);
669 Value normalCaseResult = arith::TruncIOp::create(
b, i8Ty, biasedAndShifted);
671 Value cNan =
createConst(op.getLoc(), i8Ty, 0x7e, rewriter);
672 Value select = arith::SelectOp::create(
b, isNan, cNan, normalCaseResult);
673 Value
result = arith::BitcastOp::create(
b, resultTy, select);
689 LogicalResult matchAndRewrite(arith::ExtFOp op,
690 PatternRewriter &rewriter)
const final {
691 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
692 Value operand = op.getOperand();
693 Type operandTy = operand.
getType();
694 Type resultTy = op.getType();
698 if (!llvm::isa<Float8E4M3FNType>(operandETy))
707 Value bits = arith::BitcastOp::create(
b, i8Ty, operand);
708 Value c7F8 =
createConst(op.getLoc(), i8Ty, 0x7f, rewriter);
709 Value mag8 = arith::AndIOp::create(
b, bits, c7F8);
711 Value mag16 = arith::ExtUIOp::create(
b, i16Ty, mag8);
712 Value c7 =
createConst(op.getLoc(), i16Ty, 7, rewriter);
713 Value g16Bits = arith::ShLIOp::create(
b, mag16, c7);
714 Value g16 = arith::BitcastOp::create(
b, f16Ty, g16Bits);
715 Value gF32 = arith::ExtFOp::create(
b, f32Ty, g16, op.getFastmathAttr());
718 Value magF32 = arith::MulFOp::create(
b, gF32, c256, op.getFastmathAttr());
720 Value magI32 = arith::BitcastOp::create(
b, i32Ty, magF32);
721 Value c80I8 =
createConst(op.getLoc(), i8Ty, 0x80, rewriter);
722 Value sign8 = arith::AndIOp::create(
b, bits, c80I8);
723 Value sign32 = arith::ExtUIOp::create(
b, i32Ty, sign8);
724 Value c24 =
createConst(op.getLoc(), i32Ty, 24, rewriter);
725 Value signBit = arith::ShLIOp::create(
b, sign32, c24);
726 Value signedI32 = arith::OrIOp::create(
b, magI32, signBit);
727 Value signedF32 = arith::BitcastOp::create(
b, f32Ty, signedI32);
730 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, mag8, c7F8);
731 Value cNan32 =
createConst(op.getLoc(), i32Ty, 0x7fc00000, rewriter);
732 Value nanSigned = arith::OrIOp::create(
b, cNan32, signBit);
733 Value nanF32 = arith::BitcastOp::create(
b, f32Ty, nanSigned);
734 Value resultF32 = arith::SelectOp::create(
b, isNan, nanF32, signedF32);
737 if (!resultETy.
isF32()) {
739 result = arith::TruncFOp::create(
b, resultTy, resultF32,
nullptr,
740 op.getFastmathAttr());
743 arith::ExtFOp::create(
b, resultTy, resultF32, op.getFastmathAttr());
758struct F8E4M3FNTruncFOpConverter :
public OpRewritePattern<arith::TruncFOp> {
760 LogicalResult matchAndRewrite(arith::TruncFOp op,
761 PatternRewriter &rewriter)
const final {
762 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
763 Value operand = op.getOperand();
764 Type operandTy = operand.
getType();
765 Type resultTy = op.getType();
769 if (!llvm::isa<Float8E4M3FNType>(resultETy))
771 if (op.getRoundingmodeAttr())
773 op,
"only applicable to default rounding mode.");
782 if (!operandETy.
isF32()) {
784 f32 = arith::ExtFOp::create(
b, f32Ty, operand, op.getFastmathAttr());
786 f32 = arith::TruncFOp::create(
b, f32Ty, operand,
nullptr,
787 op.getFastmathAttr());
790 Value isNan = arith::CmpFOp::create(
b, arith::CmpFPredicate::UNE, f32, f32);
792 Value f32Bits = arith::BitcastOp::create(
b, i32Ty, f32);
793 Value cSignMask =
createConst(op.getLoc(), i32Ty, 0x80000000, rewriter);
794 Value cAbsMask =
createConst(op.getLoc(), i32Ty, 0x7fffffff, rewriter);
795 Value signBits = arith::AndIOp::create(
b, f32Bits, cSignMask);
796 Value absBits = arith::AndIOp::create(
b, f32Bits, cAbsMask);
797 Value absF32 = arith::BitcastOp::create(
b, f32Ty, absBits);
803 arith::CmpFOp::create(
b, arith::CmpFPredicate::OGT, absF32, cOverflow);
808 absF32 = arith::MinNumFOp::create(
b, absF32, cMax);
811 Value scaled = arith::MulFOp::create(
b, absF32, cInv256,
nullptr);
812 Value h16 = arith::TruncFOp::create(
b, f16Ty, scaled,
nullptr,
813 op.getFastmathAttr());
814 Value h16Bits = arith::BitcastOp::create(
b, i16Ty, h16);
816 Value c3F =
createConst(op.getLoc(), i16Ty, 0x3f, rewriter);
817 Value c7 =
createConst(op.getLoc(), i16Ty, 7, rewriter);
818 Value c1 =
createConst(op.getLoc(), i16Ty, 1, rewriter);
820 arith::AndIOp::create(
b, arith::ShRUIOp::create(
b, h16Bits, c7), c1);
821 Value roundingBias = arith::AddIOp::create(
b, bit7, c3F);
822 Value biased = arith::AddIOp::create(
b, h16Bits, roundingBias);
823 Value shifted = arith::ShRUIOp::create(
b, biased, c7);
824 Value mag8 = arith::TruncIOp::create(
b, i8Ty, shifted);
825 Value c7F8 =
createConst(op.getLoc(), i8Ty, 0x7f, rewriter);
826 mag8 = arith::AndIOp::create(
b, mag8, c7F8);
828 Value c24 =
createConst(op.getLoc(), i32Ty, 24, rewriter);
829 Value sign8 = arith::TruncIOp::create(
830 b, i8Ty, arith::ShRUIOp::create(
b, signBits, c24));
831 Value res8 = arith::OrIOp::create(
b, mag8, sign8);
833 Value isNanOrOverflow = arith::OrIOp::create(
b, isNan, isOverflow);
834 Value cNan8 =
createConst(op.getLoc(), i8Ty, 0x7f, rewriter);
835 Value res = arith::SelectOp::create(
b, isNanOrOverflow, cNan8, res8);
836 Value
result = arith::BitcastOp::create(
b, resultTy, res);
842struct ScalingExtFOpConverter :
public OpRewritePattern<arith::ScalingExtFOp> {
844 LogicalResult matchAndRewrite(arith::ScalingExtFOp op,
845 PatternRewriter &rewriter)
const final {
846 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
847 Value inputOperand = op.getIn();
848 Value scaleOperand = op.getScale();
849 Type scaleTy = scaleOperand.
getType();
853 scaleETy =
b.getF8E8M0Type();
855 scaleOperand = arith::TruncFOp::create(
b, scaleTy, scaleOperand,
nullptr,
856 op.getFastmathAttr());
859 if (!llvm::isa<Float8E8M0FNUType>(scaleETy)) {
861 op,
"scaling_extf is using scales of type which can not be converted "
864 Type resultTy = op.getType();
868 arith::ExtFOp::create(
b, resultTy, scaleOperand, op.getFastmathAttr());
870 arith::ExtFOp::create(
b, resultTy, inputOperand, op.getFastmathAttr());
872 arith::MulFOp::create(
b, inputExt, scaleExt, op.getFastmathAttr());
883struct ScalingTruncFOpConverter
886 LogicalResult matchAndRewrite(arith::ScalingTruncFOp op,
887 PatternRewriter &rewriter)
const final {
888 ImplicitLocOpBuilder
b(op.getLoc(), rewriter);
889 Value inputOperand = op.getIn();
890 Value scaleOperand = op.getScale();
891 Type scaleTy = scaleOperand.
getType();
895 scaleETy =
b.getF8E8M0Type();
897 scaleOperand = arith::TruncFOp::create(
b, scaleTy, scaleOperand,
nullptr,
898 op.getFastmathAttr());
900 if (!llvm::isa<Float8E8M0FNUType>(scaleETy)) {
902 op,
"scaling_truncf is using scales type which can not be converted "
905 Type resultTy = op.getType();
906 Type inputTy = inputOperand.
getType();
910 arith::ExtFOp::create(
b, inputTy, scaleOperand, op.getFastmathAttr());
911 Value
result = arith::DivFOp::create(
b, inputOperand, scaleOperand,
912 op.getFastmathAttr());
913 Value resultCast = arith::TruncFOp::create(
914 b, resultTy,
result, op.getRoundingmodeAttr(), op.getFastmathAttr());
936struct FlushDenormalsOpConverter
939 LogicalResult matchAndRewrite(arith::FlushDenormalsOp op,
940 PatternRewriter &rewriter)
const final {
941 Location loc = op.getLoc();
942 ImplicitLocOpBuilder
b(loc, rewriter);
943 Value operand = op.getOperand();
944 Type operandTy = operand.
getType();
949 const llvm::fltSemantics &sem = floatTy.getFloatSemantics();
952 if (!llvm::APFloatBase::isIEEELikeFP(sem))
954 op,
"only IEEE-like floating-point types are supported");
956 unsigned totalBits = llvm::APFloatBase::semanticsSizeInBits(sem);
957 unsigned precision = llvm::APFloatBase::semanticsPrecision(sem);
960 if (precision < 1 || precision > totalBits)
962 unsigned mantissaBits = precision - 1;
963 unsigned expBits = totalBits - 1 - mantissaBits;
964 if (expBits == 0 || mantissaBits == 0)
966 op,
"degenerate float encoding has no exponent or mantissa");
970 Value bits = arith::BitcastOp::create(
b, intTy, operand);
972 APInt::getBitsSet(totalBits, mantissaBits, mantissaBits + expBits);
973 APInt clearMantissaMaskVal = ~APInt::getLowBitsSet(totalBits, mantissaBits);
974 APInt zeroVal = APInt::getZero(totalBits);
976 Value clearMantissaMask =
981 Value expField = arith::AndIOp::create(
b, bits, expMask);
983 arith::CmpIOp::create(
b, arith::CmpIPredicate::eq, expField, zero);
986 Value cleared = arith::AndIOp::create(
b, bits, clearMantissaMask);
987 Value resultBits = arith::SelectOp::create(
b, expIsZero, cleared, bits);
988 Value
result = arith::BitcastOp::create(
b, operandTy, resultBits);
995struct ArithExpandOpsPass
997 using ArithExpandOpsPassBase::ArithExpandOpsPassBase;
999 void runOnOperation()
override {
1003 arith::populateCeilFloorDivExpandOpsPatterns(patterns);
1004 arith::populateExpandScalingExtTruncPatterns(patterns);
1006 target.addLegalDialect<arith::ArithDialect>();
1007 target.addLegalDialect<vector::VectorDialect>();
1013 arith::FloorDivSIOp,
1014 arith::ScalingExtFOp,
1015 arith::ScalingTruncFOp
1025 if (includeMinMaxF) {
1026 arith::populateExpandMinMaxFPatterns(patterns);
1036 if (includeMinMaxI) {
1037 arith::populateExpandMinMaxIPatterns(patterns);
1049 arith::populateExpandBFloat16Patterns(patterns);
1051 arith::populateExpandF8E8M0Patterns(patterns);
1053 arith::populateExpandF4E2M1Patterns(patterns);
1055 arith::populateExpandF8E5M2Patterns(patterns);
1056 if (includeF8E4M3FN)
1057 arith::populateExpandF8E4M3FNPatterns(patterns);
1058 if (includeFlushDenormals) {
1059 arith::populateExpandFlushDenormalsPatterns(patterns);
1062 target.addDynamicallyLegalOp<arith::FlushDenormalsOp>(
1063 [](arith::FlushDenormalsOp op) {
1068 return !llvm::APFloatBase::isIEEELikeFP(
1069 floatTy.getFloatSemantics());
1073 target.addDynamicallyLegalOp<arith::ExtFOp>([=](arith::ExtFOp op) {
1076 bool legalTypes =
true;
1078 legalTypes &= !(inETy.
isBF16() && outETy.
isF32());
1080 legalTypes &= !llvm::isa<Float8E8M0FNUType>(inETy);
1082 legalTypes &= !llvm::isa<Float4E2M1FNType>(inETy);
1084 legalTypes &= !llvm::isa<Float8E5M2Type>(inETy);
1085 if (includeF8E4M3FN)
1086 legalTypes &= !llvm::isa<Float8E4M3FNType>(inETy);
1090 target.addDynamicallyLegalOp<arith::TruncFOp>([=](arith::TruncFOp op) {
1093 bool legalTypes =
true;
1095 legalTypes &= !(inETy.
isF32() && outETy.
isBF16());
1097 legalTypes &= !(llvm::isa<Float8E8M0FNUType>(outETy));
1099 legalTypes &= !llvm::isa<Float4E2M1FNType>(outETy);
1101 legalTypes &= !llvm::isa<Float8E5M2Type>(outETy);
1102 if (includeF8E4M3FN)
1103 legalTypes &= !llvm::isa<Float8E4M3FNType>(outETy);
1108 if (
failed(applyPartialConversion(getOperation(),
target,
1109 std::move(patterns))))
1110 signalPassFailure();
1117 patterns.
add<BFloat16ExtFOpConverter, BFloat16TruncFOpConverter>(
1122 patterns.
add<F4E2M1ExtFOpConverter, F4E2M1TruncFOpConverter>(
1127 patterns.
add<F8E5M2ExtFOpConverter, F8E5M2TruncFOpConverter>(
1132 patterns.
add<F8E4M3FNExtFOpConverter, F8E4M3FNTruncFOpConverter>(
1137 patterns.
add<F8E8M0ExtFOpConverter, F8E8M0TruncFOpConverter>(
1143 patterns.
add<ScalingExtFOpConverter, ScalingTruncFOpConverter>(
1149 patterns.
add<FlushDenormalsOpConverter>(patterns.
getContext());
1155 MaximumMinimumFOpConverter<MaximumFOp, arith::CmpFPredicate::UGT>,
1156 MaximumMinimumFOpConverter<MinimumFOp, arith::CmpFPredicate::ULT>,
1157 MaxNumMinNumFOpConverter<MaxNumFOp, arith::CmpFPredicate::UGT>,
1158 MaxNumMinNumFOpConverter<MinNumFOp, arith::CmpFPredicate::ULT>
1166 MaxMinIOpConverter<MaxSIOp, arith::CmpIPredicate::sgt>,
1167 MaxMinIOpConverter<MaxUIOp, arith::CmpIPredicate::ugt>,
1168 MaxMinIOpConverter<MinSIOp, arith::CmpIPredicate::slt>,
1169 MaxMinIOpConverter<MinUIOp, arith::CmpIPredicate::ult>
IntegerAttr getIntegerAttr(Type type, int64_t value)
FloatAttr getFloatAttr(Type type, double value)
IntegerType getIntegerType(unsigned width)
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
ImplicitLocOpBuilder maintains a 'current location', allowing use of the create<> method without spec...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
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.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
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.
ArithExpandOpsPassBase Base
void populateExpandF8E4M3FNPatterns(RewritePatternSet &patterns)
Add patterns to expand Arith f8e4m3fn patterns to lower level bitcasts/shifts.
void populateExpandBFloat16Patterns(RewritePatternSet &patterns)
Add patterns to expand Arith bf16 patterns to lower level bitcasts/shifts.
void populateExpandScalingExtTruncPatterns(RewritePatternSet &patterns)
Add patterns to expand scaling ExtF/TruncF ops to equivalent arith ops.
void populateExpandF8E8M0Patterns(RewritePatternSet &patterns)
Add patterns to expand Arith f8e8m0 patterns to lower level bitcasts/shifts.
void populateCeilFloorDivExpandOpsPatterns(RewritePatternSet &patterns)
void populateExpandF4E2M1Patterns(RewritePatternSet &patterns)
Add patterns to expand Arith f4e2m1 patterns to lower level bitcasts/shifts.
void populateExpandFlushDenormalsPatterns(RewritePatternSet &patterns)
Add patterns to expand arith.flush_denormals into integer arithmetic (bitcast + bit masks + compare +...
void populateExpandMinMaxFPatterns(RewritePatternSet &patterns)
Add patterns to expand the floating-point min/max ops (arith.maximumf/ minimumf/maxnumf/minnumf) into...
void populateExpandMinMaxIPatterns(RewritePatternSet &patterns)
Add patterns to expand the signed/unsigned integer min/max ops (arith.maxsi/maxui/minsi/minui) into c...
void populateExpandMinMaxPatterns(RewritePatternSet &patterns)
Add patterns to expand both the floating-point and integer min/max ops into cmpf/cmpi + select sequen...
void populateArithExpandOpsPatterns(RewritePatternSet &patterns)
Add patterns to expand Arith ops.
void populateExpandF8E5M2Patterns(RewritePatternSet &patterns)
Add patterns to expand Arith f8e5m2 patterns to lower level bitcasts/shifts.
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...