@@ -206,6 +206,37 @@ class CIRFuncLowering : public mlir::OpRewritePattern<mlir::cir::FuncOp> {
206206 }
207207};
208208
209+ class CIRUnaryOpLowering : public mlir ::OpRewritePattern<mlir::cir::UnaryOp> {
210+ public:
211+ using OpRewritePattern<mlir::cir::UnaryOp>::OpRewritePattern;
212+
213+ mlir::LogicalResult
214+ matchAndRewrite (mlir::cir::UnaryOp op,
215+ mlir::PatternRewriter &rewriter) const override {
216+ mlir::Type type = op.getInput ().getType ();
217+ assert (type.isa <mlir::IntegerType>() && " operand type not supported yet" );
218+
219+ switch (op.getKind ()) {
220+ case mlir::cir::UnaryOpKind::Inc: {
221+ auto One = rewriter.create <mlir::arith::ConstantOp>(
222+ op.getLoc (), type, mlir::IntegerAttr::get (type, 1 ));
223+ rewriter.replaceOpWithNewOp <mlir::arith::AddIOp>(op, op.getType (),
224+ op.getInput (), One);
225+ break ;
226+ }
227+ case mlir::cir::UnaryOpKind::Dec: {
228+ auto One = rewriter.create <mlir::arith::ConstantOp>(
229+ op.getLoc (), type, mlir::IntegerAttr::get (type, 1 ));
230+ rewriter.replaceOpWithNewOp <mlir::arith::SubIOp>(op, op.getType (),
231+ op.getInput (), One);
232+ break ;
233+ }
234+ }
235+
236+ return mlir::LogicalResult::success ();
237+ }
238+ };
239+
209240class CIRBinOpLowering : public mlir ::OpRewritePattern<mlir::cir::BinOp> {
210241public:
211242 using OpRewritePattern<mlir::cir::BinOp>::OpRewritePattern;
@@ -447,8 +478,8 @@ class CIRBrOpLowering : public mlir::OpRewritePattern<mlir::cir::BrOp> {
447478
448479void populateCIRToMemRefConversionPatterns (mlir::RewritePatternSet &patterns) {
449480 patterns.add <CIRAllocaLowering, CIRLoadLowering, CIRStoreLowering,
450- CIRConstantLowering, CIRBinOpLowering, CIRCmpOpLowering ,
451- CIRBrOpLowering>(patterns.getContext ());
481+ CIRConstantLowering, CIRUnaryOpLowering, CIRBinOpLowering ,
482+ CIRCmpOpLowering, CIRBrOpLowering>(patterns.getContext ());
452483}
453484
454485void ConvertCIRToLLVMPass::runOnOperation () {
0 commit comments