@@ -781,6 +781,69 @@ struct ConvertShRUI final : OpConversionPattern<arith::ShRUIOp> {
781781 }
782782};
783783
784+ // ===----------------------------------------------------------------------===//
785+ // ConvertShRSI
786+ // ===----------------------------------------------------------------------===//
787+
788+ struct ConvertShRSI final : OpConversionPattern<arith::ShRSIOp> {
789+ using OpConversionPattern::OpConversionPattern;
790+
791+ LogicalResult
792+ matchAndRewrite (arith::ShRSIOp op, OpAdaptor adaptor,
793+ ConversionPatternRewriter &rewriter) const override {
794+ Location loc = op->getLoc ();
795+
796+ Type oldTy = op.getType ();
797+ auto newTy =
798+ getTypeConverter ()->convertType (oldTy).dyn_cast_or_null <VectorType>();
799+ if (!newTy)
800+ return rewriter.notifyMatchFailure (
801+ loc, llvm::formatv (" unsupported type: {0}" , op.getType ()));
802+
803+ Value lhsElem1 = extractLastDimSlice (rewriter, loc, adaptor.getLhs (), 1 );
804+ Value rhsElem0 = extractLastDimSlice (rewriter, loc, adaptor.getRhs (), 0 );
805+
806+ Type narrowTy = rhsElem0.getType ();
807+ int64_t origBitwidth = newTy.getElementTypeBitWidth () * 2 ;
808+
809+ // Rewrite this as an bitwise or of `arith.shrui` and sign extension bits.
810+ // Perform as many ops over the narrow integer type as possible and let the
811+ // other emulation patterns convert the rest.
812+ Value elemZero =
813+ createScalarOrSplatConstant (rewriter, loc, narrowTy, 0 );
814+ Value signBit = rewriter.create <arith::CmpIOp>(
815+ loc, arith::CmpIPredicate::slt, lhsElem1, elemZero);
816+ signBit = dropTrailingX1Dim (rewriter, loc, signBit);
817+
818+ // Create a bit pattern of either all ones or all zeros. Then shift it left
819+ // to calculate the sign extension bits created by shifting the original
820+ // sign bit right.
821+ Value allSign = rewriter.create <arith::ExtSIOp>(loc, oldTy, signBit);
822+ Value maxShift =
823+ createScalarOrSplatConstant (rewriter, loc, narrowTy, origBitwidth);
824+ Value numNonSignExtBits =
825+ rewriter.create <arith::SubIOp>(loc, maxShift, rhsElem0);
826+ numNonSignExtBits = dropTrailingX1Dim (rewriter, loc, numNonSignExtBits);
827+ numNonSignExtBits =
828+ rewriter.create <arith::ExtUIOp>(loc, oldTy, numNonSignExtBits);
829+ Value signBits =
830+ rewriter.create <arith::ShLIOp>(loc, allSign, numNonSignExtBits);
831+
832+ // Use original arguments to create the right shift.
833+ Value shrui = rewriter.create <arith::ShRUIOp>(loc, op.getLhs (), op.getRhs ());
834+ Value shrsi = rewriter.create <arith::OrIOp>(loc, shrui, signBits);
835+
836+ // Handle shifting by zero. This is necessary when the `signBits` shift is
837+ // invalid.
838+ Value isNoop = rewriter.create <arith::CmpIOp>(loc, arith::CmpIPredicate::eq,
839+ rhsElem0, elemZero);
840+ isNoop = dropTrailingX1Dim (rewriter, loc, isNoop);
841+ rewriter.replaceOpWithNewOp <arith::SelectOp>(op, isNoop, op.getLhs (), shrsi);
842+
843+ return success ();
844+ }
845+ };
846+
784847// ===----------------------------------------------------------------------===//
785848// ConvertTruncI
786849// ===----------------------------------------------------------------------===//
@@ -799,7 +862,8 @@ struct ConvertTruncI final : OpConversionPattern<arith::TruncIOp> {
799862 loc, llvm::formatv (" unsupported truncation result type: {0}" ,
800863 op.getType ()));
801864
802- // Discard the high half of the input. Truncate the low half, if necessary.
865+ // Discard the high half of the input. Truncate the low half, if
866+ // necessary.
803867 Value extracted = extractLastDimSlice (rewriter, loc, adaptor.getIn (), 0 );
804868 extracted = dropTrailingX1Dim (rewriter, loc, extracted);
805869 Value truncated =
@@ -940,7 +1004,7 @@ void arith::populateArithWideIntEmulationPatterns(
9401004 // Misc ops.
9411005 ConvertConstant, ConvertCmpI, ConvertSelect, ConvertVectorPrint,
9421006 // Binary ops.
943- ConvertAddI, ConvertMulI, ConvertShLI, ConvertShRUI,
1007+ ConvertAddI, ConvertMulI, ConvertShLI, ConvertShRSI, ConvertShRUI,
9441008 // Bitwise binary ops.
9451009 ConvertBitwiseBinary<arith::AndIOp>, ConvertBitwiseBinary<arith::OrIOp>,
9461010 ConvertBitwiseBinary<arith::XOrIOp>,
0 commit comments