Skip to content

[mlir][EmitC] Add unary_{minus,plus} operators #84329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,42 @@ def EmitC_SubOp : EmitC_BinaryOp<"sub", [CExpression]> {
let hasVerifier = 1;
}

def EmitC_UnaryMinusOp : EmitC_UnaryOp<"unary_minus", [CExpression]> {
let summary = "Unary minus operation";
let description = [{
With the `unary_minus` operation the unary operator - (minus) can be
applied.

Example:

```mlir
%0 = emitc.unary_plus %arg0 : (i32) -> i32
```
```c++
// Code emitted for the operation above.
int32_t v2 = -v1;
```
}];
}

def EmitC_UnaryPlusOp : EmitC_UnaryOp<"unary_plus", [CExpression]> {
let summary = "Unary plus operation";
let description = [{
With the `unary_plus` operation the unary operator + (plus) can be
applied.

Example:

```mlir
%0 = emitc.unary_plus %arg0 : (i32) -> i32
```
```c++
// Code emitted for the operation above.
int32_t v2 = +v1;
```
}];
}

def EmitC_VariableOp : EmitC_Op<"variable", []> {
let summary = "Variable operation";
let description = [{
Expand Down
17 changes: 16 additions & 1 deletion mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
.Case<emitc::MulOp>([&](auto op) { return 13; })
.Case<emitc::RemOp>([&](auto op) { return 13; })
.Case<emitc::SubOp>([&](auto op) { return 12; })
.Case<emitc::UnaryMinusOp>([&](auto op) { return 15; })
.Case<emitc::UnaryPlusOp>([&](auto op) { return 15; })
.Default([](auto op) { return op->emitError("unsupported operation"); });
}

Expand Down Expand Up @@ -652,6 +654,18 @@ static LogicalResult printOperation(CppEmitter &emitter,
return printBinaryOperation(emitter, operation, "^");
}

static LogicalResult printOperation(CppEmitter &emitter,
emitc::UnaryPlusOp unaryPlusOp) {
Operation *operation = unaryPlusOp.getOperation();
return printUnaryOperation(emitter, operation, "+");
}

static LogicalResult printOperation(CppEmitter &emitter,
emitc::UnaryMinusOp unaryMinusOp) {
Operation *operation = unaryMinusOp.getOperation();
return printUnaryOperation(emitter, operation, "-");
}

static LogicalResult printOperation(CppEmitter &emitter, emitc::CastOp castOp) {
raw_ostream &os = emitter.ostream();
Operation &op = *castOp.getOperation();
Expand Down Expand Up @@ -1371,7 +1385,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp, emitc::IfOp,
emitc::IncludeOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
emitc::SubOp, emitc::VariableOp, emitc::VerbatimOp>(
emitc::SubOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
emitc::VariableOp, emitc::VerbatimOp>(
[&](auto op) { return printOperation(*this, op); })
// Func ops.
.Case<func::CallOp, func::FuncOp, func::ReturnOp>(
Expand Down
6 changes: 6 additions & 0 deletions mlir/test/Dialect/EmitC/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func.func @logical(%arg0: i32, %arg1: i32) {
return
}

func.func @unary(%arg0: i32) {
%0 = emitc.unary_minus %arg0 : (i32) -> i32
%1 = emitc.unary_plus %arg0 : (i32) -> i32
return
}

func.func @test_if(%arg0: i1, %arg1: f32) {
emitc.if %arg0 {
%0 = emitc.call_opaque "func_const"(%arg1) : (f32) -> i32
Expand Down
12 changes: 12 additions & 0 deletions mlir/test/Target/Cpp/unary_operators.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s

func.func @unary(%arg0: i32) -> () {
%0 = emitc.unary_minus %arg0 : (i32) -> i32
%1 = emitc.unary_plus %arg0 : (i32) -> i32

return
}

// CHECK-LABEL: void unary
// CHECK-NEXT: int32_t [[V1:[^ ]*]] = -[[V0:[^ ]*]];
// CHECK-NEXT: int32_t [[V2:[^ ]*]] = +[[V0]];