Skip to content

Commit 983c862

Browse files
cmarcelobcardosolopes
authored andcommitted
[CIR][CodeGen] Basic lowering of increment/decrement
1 parent 78df77b commit 983c862

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

clang/lib/CIR/CodeGen/LowerToLLVM.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,37 @@ class CIRFuncLowering : public mlir::OpRewritePattern<mlir::cir::FuncOp> {
207207
}
208208
};
209209

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+
210241
class CIRBinOpLowering : public mlir::OpRewritePattern<mlir::cir::BinOp> {
211242
public:
212243
using OpRewritePattern<mlir::cir::BinOp>::OpRewritePattern;
@@ -448,8 +479,8 @@ class CIRBrOpLowering : public mlir::OpRewritePattern<mlir::cir::BrOp> {
448479

449480
void populateCIRToMemRefConversionPatterns(mlir::RewritePatternSet &patterns) {
450481
patterns.add<CIRAllocaLowering, CIRLoadLowering, CIRStoreLowering,
451-
CIRConstantLowering, CIRBinOpLowering, CIRCmpOpLowering,
452-
CIRBrOpLowering>(patterns.getContext());
482+
CIRConstantLowering, CIRUnaryOpLowering, CIRBinOpLowering,
483+
CIRCmpOpLowering, CIRBrOpLowering>(patterns.getContext());
453484
}
454485

455486
void ConvertCIRToLLVMPass::runOnOperation() {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: cir-tool %s -cir-to-func -cir-to-memref -o - | FileCheck %s -check-prefix=MLIR
2+
// RUN: cir-tool %s -cir-to-func -cir-to-memref -cir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM
3+
4+
module {
5+
cir.func @foo() {
6+
%0 = cir.alloca i32, cir.ptr <i32>, ["a", cinit] {alignment = 4 : i64}
7+
%1 = cir.alloca i32, cir.ptr <i32>, ["b", cinit] {alignment = 4 : i64}
8+
%2 = cir.cst(2 : i32) : i32
9+
cir.store %2, %0 : i32, cir.ptr <i32>
10+
cir.store %2, %1 : i32, cir.ptr <i32>
11+
12+
%3 = cir.load %0 : cir.ptr <i32>, i32
13+
%4 = cir.unary(inc, %3) : i32, i32
14+
cir.store %4, %0 : i32, cir.ptr <i32>
15+
16+
%5 = cir.load %1 : cir.ptr <i32>, i32
17+
%6 = cir.unary(dec, %5) : i32, i32
18+
cir.store %6, %1 : i32, cir.ptr <i32>
19+
cir.return
20+
}
21+
}
22+
23+
// MLIR: = arith.constant 1
24+
// MLIR: = arith.addi
25+
// MLIR: = arith.constant 1
26+
// MLIR: = arith.subi
27+
28+
// LLVM: = add i32 %[[#]], 1
29+
// LLVM: = sub i32 %[[#]], 1

0 commit comments

Comments
 (0)