Skip to content

Commit df9be01

Browse files
authored
[mlir][EmitC] Add unary_{minus,plus} operators (#84329)
This adds operations for the unary minus and the unary plus operator.
1 parent fb1be9b commit df9be01

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,42 @@ def EmitC_SubOp : EmitC_BinaryOp<"sub", [CExpression]> {
908908
let hasVerifier = 1;
909909
}
910910

911+
def EmitC_UnaryMinusOp : EmitC_UnaryOp<"unary_minus", [CExpression]> {
912+
let summary = "Unary minus operation";
913+
let description = [{
914+
With the `unary_minus` operation the unary operator - (minus) can be
915+
applied.
916+
917+
Example:
918+
919+
```mlir
920+
%0 = emitc.unary_plus %arg0 : (i32) -> i32
921+
```
922+
```c++
923+
// Code emitted for the operation above.
924+
int32_t v2 = -v1;
925+
```
926+
}];
927+
}
928+
929+
def EmitC_UnaryPlusOp : EmitC_UnaryOp<"unary_plus", [CExpression]> {
930+
let summary = "Unary plus operation";
931+
let description = [{
932+
With the `unary_plus` operation the unary operator + (plus) can be
933+
applied.
934+
935+
Example:
936+
937+
```mlir
938+
%0 = emitc.unary_plus %arg0 : (i32) -> i32
939+
```
940+
```c++
941+
// Code emitted for the operation above.
942+
int32_t v2 = +v1;
943+
```
944+
}];
945+
}
946+
911947
def EmitC_VariableOp : EmitC_Op<"variable", []> {
912948
let summary = "Variable operation";
913949
let description = [{

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
104104
.Case<emitc::MulOp>([&](auto op) { return 13; })
105105
.Case<emitc::RemOp>([&](auto op) { return 13; })
106106
.Case<emitc::SubOp>([&](auto op) { return 12; })
107+
.Case<emitc::UnaryMinusOp>([&](auto op) { return 15; })
108+
.Case<emitc::UnaryPlusOp>([&](auto op) { return 15; })
107109
.Default([](auto op) { return op->emitError("unsupported operation"); });
108110
}
109111

@@ -652,6 +654,18 @@ static LogicalResult printOperation(CppEmitter &emitter,
652654
return printBinaryOperation(emitter, operation, "^");
653655
}
654656

657+
static LogicalResult printOperation(CppEmitter &emitter,
658+
emitc::UnaryPlusOp unaryPlusOp) {
659+
Operation *operation = unaryPlusOp.getOperation();
660+
return printUnaryOperation(emitter, operation, "+");
661+
}
662+
663+
static LogicalResult printOperation(CppEmitter &emitter,
664+
emitc::UnaryMinusOp unaryMinusOp) {
665+
Operation *operation = unaryMinusOp.getOperation();
666+
return printUnaryOperation(emitter, operation, "-");
667+
}
668+
655669
static LogicalResult printOperation(CppEmitter &emitter, emitc::CastOp castOp) {
656670
raw_ostream &os = emitter.ostream();
657671
Operation &op = *castOp.getOperation();
@@ -1371,7 +1385,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
13711385
emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp, emitc::IfOp,
13721386
emitc::IncludeOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
13731387
emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
1374-
emitc::SubOp, emitc::VariableOp, emitc::VerbatimOp>(
1388+
emitc::SubOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
1389+
emitc::VariableOp, emitc::VerbatimOp>(
13751390
[&](auto op) { return printOperation(*this, op); })
13761391
// Func ops.
13771392
.Case<func::CallOp, func::FuncOp, func::ReturnOp>(

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func.func @logical(%arg0: i32, %arg1: i32) {
134134
return
135135
}
136136

137+
func.func @unary(%arg0: i32) {
138+
%0 = emitc.unary_minus %arg0 : (i32) -> i32
139+
%1 = emitc.unary_plus %arg0 : (i32) -> i32
140+
return
141+
}
142+
137143
func.func @test_if(%arg0: i1, %arg1: f32) {
138144
emitc.if %arg0 {
139145
%0 = emitc.call_opaque "func_const"(%arg1) : (f32) -> i32
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
2+
3+
func.func @unary(%arg0: i32) -> () {
4+
%0 = emitc.unary_minus %arg0 : (i32) -> i32
5+
%1 = emitc.unary_plus %arg0 : (i32) -> i32
6+
7+
return
8+
}
9+
10+
// CHECK-LABEL: void unary
11+
// CHECK-NEXT: int32_t [[V1:[^ ]*]] = -[[V0:[^ ]*]];
12+
// CHECK-NEXT: int32_t [[V2:[^ ]*]] = +[[V0]];

0 commit comments

Comments
 (0)