@@ -206,6 +206,37 @@ class CIRFuncLowering : public mlir::OpRewritePattern<mlir::cir::FuncOp> {
206
206
}
207
207
};
208
208
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
+
209
240
class CIRBinOpLowering : public mlir ::OpRewritePattern<mlir::cir::BinOp> {
210
241
public:
211
242
using OpRewritePattern<mlir::cir::BinOp>::OpRewritePattern;
@@ -447,8 +478,8 @@ class CIRBrOpLowering : public mlir::OpRewritePattern<mlir::cir::BrOp> {
447
478
448
479
void populateCIRToMemRefConversionPatterns (mlir::RewritePatternSet &patterns) {
449
480
patterns.add <CIRAllocaLowering, CIRLoadLowering, CIRStoreLowering,
450
- CIRConstantLowering, CIRBinOpLowering, CIRCmpOpLowering ,
451
- CIRBrOpLowering>(patterns.getContext ());
481
+ CIRConstantLowering, CIRUnaryOpLowering, CIRBinOpLowering ,
482
+ CIRCmpOpLowering, CIRBrOpLowering>(patterns.getContext ());
452
483
}
453
484
454
485
void ConvertCIRToLLVMPass::runOnOperation () {
0 commit comments