@@ -424,6 +424,38 @@ class ArithOpConversion final : public OpConversionPattern<ArithOp> {
424424 }
425425};
426426
427+ template <class ArithOp , class EmitCOp >
428+ class BinaryUIOpConversion final : public OpConversionPattern<ArithOp> {
429+ public:
430+ using OpConversionPattern<ArithOp>::OpConversionPattern;
431+
432+ LogicalResult
433+ matchAndRewrite (ArithOp uiBinOp, typename ArithOp::Adaptor adaptor,
434+ ConversionPatternRewriter &rewriter) const override {
435+ Type newRetTy = this ->getTypeConverter ()->convertType (uiBinOp.getType ());
436+ if (!newRetTy)
437+ return rewriter.notifyMatchFailure (uiBinOp,
438+ " converting result type failed" );
439+ if (!isa<IntegerType>(newRetTy)) {
440+ return rewriter.notifyMatchFailure (uiBinOp, " expected integer type" );
441+ }
442+ Type unsignedType =
443+ adaptIntegralTypeSignedness (newRetTy, /* needsUnsigned=*/ true );
444+ if (!unsignedType)
445+ return rewriter.notifyMatchFailure (uiBinOp,
446+ " converting result type failed" );
447+ Value lhsAdapted = adaptValueType (uiBinOp.getLhs (), rewriter, unsignedType);
448+ Value rhsAdapted = adaptValueType (uiBinOp.getRhs (), rewriter, unsignedType);
449+
450+ auto newDivOp =
451+ rewriter.create <EmitCOp>(uiBinOp.getLoc (), unsignedType,
452+ ArrayRef<Value>{lhsAdapted, rhsAdapted});
453+ Value resultAdapted = adaptValueType (newDivOp, rewriter, newRetTy);
454+ rewriter.replaceOp (uiBinOp, resultAdapted);
455+ return success ();
456+ }
457+ };
458+
427459template <typename ArithOp, typename EmitCOp>
428460class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
429461public:
@@ -728,6 +760,8 @@ void mlir::populateArithToEmitCPatterns(RewritePatternSet &patterns,
728760 ArithOpConversion<arith::RemSIOp, emitc::RemOp>,
729761 ArithOpConversion<arith::MulFOp, emitc::MulOp>,
730762 ArithOpConversion<arith::SubFOp, emitc::SubOp>,
763+ BinaryUIOpConversion<arith::DivUIOp, emitc::DivOp>,
764+ BinaryUIOpConversion<arith::RemUIOp, emitc::RemOp>,
731765 IntegerOpConversion<arith::AddIOp, emitc::AddOp>,
732766 IntegerOpConversion<arith::MulIOp, emitc::MulOp>,
733767 IntegerOpConversion<arith::SubIOp, emitc::SubOp>,
0 commit comments