Skip to content

Commit a5e0730

Browse files
cmarcelobcardosolopes
authored andcommitted
[CIR][CodeGen] Integer lowering of unary plus and minus
1 parent 7f314ab commit a5e0730

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

clang/lib/CIR/CodeGen/LowerToLLVM.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ class CIRUnaryOpLowering : public mlir::OpRewritePattern<mlir::cir::UnaryOp> {
232232
op.getInput(), One);
233233
break;
234234
}
235+
case mlir::cir::UnaryOpKind::Plus: {
236+
rewriter.replaceOp(op, op.getInput());
237+
break;
238+
}
239+
case mlir::cir::UnaryOpKind::Minus: {
240+
auto Zero = rewriter.create<mlir::arith::ConstantOp>(
241+
op.getLoc(), type, mlir::IntegerAttr::get(type, 0));
242+
rewriter.replaceOpWithNewOp<mlir::arith::SubIOp>(op, op.getType(), Zero,
243+
op.getInput());
244+
break;
245+
}
235246
}
236247

237248
return mlir::LogicalResult::success();
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", init] {alignment = 4 : i64}
7+
%1 = cir.alloca i32, cir.ptr <i32>, ["b", init] {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(plus, %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(minus, %5) : i32, i32
18+
cir.store %6, %1 : i32, cir.ptr <i32>
19+
cir.return
20+
}
21+
}
22+
23+
// MLIR: %[[#INPUT_PLUS:]] = memref.load
24+
// MLIR: memref.store %[[#INPUT_PLUS]]
25+
// MLIR: %[[#INPUT_MINUS:]] = memref.load
26+
// MLIR: %[[ZERO:[a-z0-9_]+]] = arith.constant 0
27+
// MLIR: arith.subi %[[ZERO]], %[[#INPUT_MINUS]]
28+
29+
// LLVM: = sub i32 0, %[[#]]

0 commit comments

Comments
 (0)