Skip to content

Commit

Permalink
[CIR][CodeGen] Integer lowering of unary plus and minus
Browse files Browse the repository at this point in the history
  • Loading branch information
cmarcelo committed Oct 18, 2022
1 parent 50d2a7f commit bd50559
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clang/lib/CIR/CodeGen/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ class CIRUnaryOpLowering : public mlir::OpRewritePattern<mlir::cir::UnaryOp> {
op.getInput(), One);
break;
}
case mlir::cir::UnaryOpKind::Plus: {
rewriter.replaceOp(op, op.getInput());
break;
}
case mlir::cir::UnaryOpKind::Minus: {
auto Zero = rewriter.create<mlir::arith::ConstantOp>(
op.getLoc(), type, mlir::IntegerAttr::get(type, 0));
rewriter.replaceOpWithNewOp<mlir::arith::SubIOp>(op, op.getType(), Zero,
op.getInput());
break;
}
}

return mlir::LogicalResult::success();
Expand Down
29 changes: 29 additions & 0 deletions clang/test/CIR/CIRToLLVM/unary-plus-minus.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: cir-tool %s -cir-to-func -cir-to-memref -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-tool %s -cir-to-func -cir-to-memref -cir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

module {
cir.func @foo() {
%0 = cir.alloca i32, cir.ptr <i32>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca i32, cir.ptr <i32>, ["b", init] {alignment = 4 : i64}
%2 = cir.cst(2 : i32) : i32
cir.store %2, %0 : i32, cir.ptr <i32>
cir.store %2, %1 : i32, cir.ptr <i32>

%3 = cir.load %0 : cir.ptr <i32>, i32
%4 = cir.unary(plus, %3) : i32, i32
cir.store %4, %0 : i32, cir.ptr <i32>

%5 = cir.load %1 : cir.ptr <i32>, i32
%6 = cir.unary(minus, %5) : i32, i32
cir.store %6, %1 : i32, cir.ptr <i32>
cir.return
}
}

// MLIR: %[[#INPUT_PLUS:]] = memref.load
// MLIR: memref.store %[[#INPUT_PLUS]]
// MLIR: %[[#INPUT_MINUS:]] = memref.load
// MLIR: %[[ZERO:[a-z0-9_]+]] = arith.constant 0
// MLIR: arith.subi %[[ZERO]], %[[#INPUT_MINUS]]

// LLVM: = sub i32 0, %[[#]]

0 comments on commit bd50559

Please sign in to comment.