@@ -733,6 +733,37 @@ class ItoFCastOpConversion : public OpConversionPattern<CastOp> {
733733 }
734734};
735735
736+ // Floating-point to floating-point conversions.
737+ template <typename CastOp>
738+ class FpCastOpConversion : public OpConversionPattern <CastOp> {
739+ public:
740+ FpCastOpConversion (const TypeConverter &typeConverter, MLIRContext *context)
741+ : OpConversionPattern<CastOp>(typeConverter, context) {}
742+
743+ LogicalResult
744+ matchAndRewrite (CastOp castOp, typename CastOp::Adaptor adaptor,
745+ ConversionPatternRewriter &rewriter) const override {
746+ // Vectors in particular are not supported
747+ Type operandType = adaptor.getIn ().getType ();
748+ if (!emitc::isSupportedFloatType (operandType))
749+ return rewriter.notifyMatchFailure (castOp,
750+ " unsupported cast source type" );
751+
752+ Type dstType = this ->getTypeConverter ()->convertType (castOp.getType ());
753+ if (!dstType)
754+ return rewriter.notifyMatchFailure (castOp, " type conversion failed" );
755+
756+ if (!emitc::isSupportedFloatType (dstType))
757+ return rewriter.notifyMatchFailure (castOp,
758+ " unsupported cast destination type" );
759+
760+ Value fpCastOperand = adaptor.getIn ();
761+ rewriter.replaceOpWithNewOp <emitc::CastOp>(castOp, dstType, fpCastOperand);
762+
763+ return success ();
764+ }
765+ };
766+
736767} // namespace
737768
738769// ===----------------------------------------------------------------------===//
@@ -778,7 +809,9 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
778809 ItoFCastOpConversion<arith::SIToFPOp>,
779810 ItoFCastOpConversion<arith::UIToFPOp>,
780811 FtoICastOpConversion<arith::FPToSIOp>,
781- FtoICastOpConversion<arith::FPToUIOp>
812+ FtoICastOpConversion<arith::FPToUIOp>,
813+ FpCastOpConversion<arith::ExtFOp>,
814+ FpCastOpConversion<arith::TruncFOp>
782815 >(typeConverter, ctx);
783816 // clang-format on
784817}
0 commit comments