-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-mlir-emitc @llvm/pr-subscribers-mlir Author: Marius Brehler (marbre) ChangesThis adds operations for the unary minus and the unary plus operator. Full diff: https://github.com/llvm/llvm-project/pull/84329.diff 4 Files Affected:
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index db0e2d10960d72..8f5b79f00120fd 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -908,6 +908,42 @@ def EmitC_SubOp : EmitC_BinaryOp<"sub", [CExpression]> {
let hasVerifier = 1;
}
+def EmitC_UnaryMinusOp : EmitC_UnaryOp<"unary_minus", []> {
+ 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", []> {
+ 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 = [{
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 95513cb0fb2ebc..b99d0ede8bf4ff 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -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"); });
}
@@ -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();
@@ -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>(
diff --git a/mlir/test/Dialect/EmitC/ops.mlir b/mlir/test/Dialect/EmitC/ops.mlir
index f852390f03e298..122b1d9ef1059f 100644
--- a/mlir/test/Dialect/EmitC/ops.mlir
+++ b/mlir/test/Dialect/EmitC/ops.mlir
@@ -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
diff --git a/mlir/test/Target/Cpp/unary_operators.mlir b/mlir/test/Target/Cpp/unary_operators.mlir
new file mode 100644
index 00000000000000..8a89437a41cc50
--- /dev/null
+++ b/mlir/test/Target/Cpp/unary_operators.mlir
@@ -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]];
|
simon-camp
requested changes
Mar 7, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CExpression
is missing, otherwise 👍
This adds operations for the unary minus and the unary plus operator.
simon-camp
approved these changes
Mar 7, 2024
mgehre-amd
pushed a commit
to Xilinx/llvm-project
that referenced
this pull request
Mar 11, 2024
This adds operations for the unary minus and the unary plus operator.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds operations for the unary minus and the unary plus operator.