@@ -207,6 +207,37 @@ class CIRFuncLowering : public mlir::OpRewritePattern<mlir::cir::FuncOp> {
207
207
}
208
208
};
209
209
210
+ class CIRUnaryOpLowering : public mlir ::OpRewritePattern<mlir::cir::UnaryOp> {
211
+ public:
212
+ using OpRewritePattern<mlir::cir::UnaryOp>::OpRewritePattern;
213
+
214
+ mlir::LogicalResult
215
+ matchAndRewrite (mlir::cir::UnaryOp op,
216
+ mlir::PatternRewriter &rewriter) const override {
217
+ mlir::Type type = op.getInput ().getType ();
218
+ assert (type.isa <mlir::IntegerType>() && " operand type not supported yet" );
219
+
220
+ switch (op.getKind ()) {
221
+ case mlir::cir::UnaryOpKind::Inc: {
222
+ auto One = rewriter.create <mlir::arith::ConstantOp>(
223
+ op.getLoc (), type, mlir::IntegerAttr::get (type, 1 ));
224
+ rewriter.replaceOpWithNewOp <mlir::arith::AddIOp>(op, op.getType (),
225
+ op.getInput (), One);
226
+ break ;
227
+ }
228
+ case mlir::cir::UnaryOpKind::Dec: {
229
+ auto One = rewriter.create <mlir::arith::ConstantOp>(
230
+ op.getLoc (), type, mlir::IntegerAttr::get (type, 1 ));
231
+ rewriter.replaceOpWithNewOp <mlir::arith::SubIOp>(op, op.getType (),
232
+ op.getInput (), One);
233
+ break ;
234
+ }
235
+ }
236
+
237
+ return mlir::LogicalResult::success ();
238
+ }
239
+ };
240
+
210
241
class CIRBinOpLowering : public mlir ::OpRewritePattern<mlir::cir::BinOp> {
211
242
public:
212
243
using OpRewritePattern<mlir::cir::BinOp>::OpRewritePattern;
@@ -448,8 +479,8 @@ class CIRBrOpLowering : public mlir::OpRewritePattern<mlir::cir::BrOp> {
448
479
449
480
void populateCIRToMemRefConversionPatterns (mlir::RewritePatternSet &patterns) {
450
481
patterns.add <CIRAllocaLowering, CIRLoadLowering, CIRStoreLowering,
451
- CIRConstantLowering, CIRBinOpLowering, CIRCmpOpLowering ,
452
- CIRBrOpLowering>(patterns.getContext ());
482
+ CIRConstantLowering, CIRUnaryOpLowering, CIRBinOpLowering ,
483
+ CIRCmpOpLowering, CIRBrOpLowering>(patterns.getContext ());
453
484
}
454
485
455
486
void ConvertCIRToLLVMPass::runOnOperation () {
0 commit comments