[Mlir-commits] [mlir] [emitc] Support translate switchop with argument of expressionop (PR #123701)
Jianjian Guan
llvmlistbot at llvm.org
Tue Jan 21 19:19:58 PST 2025
https://github.com/jacquesguan updated https://github.com/llvm/llvm-project/pull/123701
>From 7e451ece070d5df9c2a90ed2ec06b2bb58d3aa5a Mon Sep 17 00:00:00 2001
From: Jianjian GUAN <jacquesguan at me.com>
Date: Tue, 21 Jan 2025 14:39:05 +0800
Subject: [PATCH 1/2] [emitc] Support translate switchop with argument of
expressionop
---
mlir/lib/Target/Cpp/TranslateToCpp.cpp | 5 +-
mlir/test/Target/Cpp/switch.mlir | 74 ++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index a91f5ab9311401..01de0e41f20353 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -475,7 +475,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
emitc::SwitchOp switchOp) {
raw_indented_ostream &os = emitter.ostream();
- os << "\nswitch (" << emitter.getOrCreateName(switchOp.getArg()) << ") {";
+ os << "\nswitch (";
+ if (failed(emitter.emitOperand(switchOp.getArg())))
+ return failure();
+ os << ") {";
for (auto pair : llvm::zip(switchOp.getCases(), switchOp.getCaseRegions())) {
os << "\ncase " << std::get<0>(pair) << ": {\n";
diff --git a/mlir/test/Target/Cpp/switch.mlir b/mlir/test/Target/Cpp/switch.mlir
index 1a8f5e2dfd2b61..222562b5c57e2b 100644
--- a/mlir/test/Target/Cpp/switch.mlir
+++ b/mlir/test/Target/Cpp/switch.mlir
@@ -882,3 +882,77 @@ func.func @emitc_switch_ui64() {
}
return
}
+
+// CPP-DEFAULT-LABEL: void emitc_switch_expression() {
+// CPP-DEFAULT: int64_t v1 = 42;
+// CPP-DEFAULT: int64_t v2 = 24;
+// CPP-DEFAULT: switch ((v1 + v2) * v2) {
+// CPP-DEFAULT: case 2: {
+// CPP-DEFAULT: int32_t v3 = func_b();
+// CPP-DEFAULT: break;
+// CPP-DEFAULT: }
+// CPP-DEFAULT: case 5: {
+// CPP-DEFAULT: int32_t v4 = func_a();
+// CPP-DEFAULT: break;
+// CPP-DEFAULT: }
+// CPP-DEFAULT: default: {
+// CPP-DEFAULT: float v5 = 4.200000000e+01f;
+// CPP-DEFAULT: func2(v5);
+// CPP-DEFAULT: break;
+// CPP-DEFAULT: }
+// CPP-DEFAULT: }
+// CPP-DEFAULT: return;
+// CPP-DEFAULT: }
+
+// CPP-DECLTOP-LABEL: void emitc_switch_expression() {
+// CPP-DECLTOP: int64_t v1;
+// CPP-DECLTOP: int64_t v2;
+// CPP-DECLTOP: float v3;
+// CPP-DECLTOP: int32_t v4;
+// CPP-DECLTOP: int32_t v5;
+// CPP-DECLTOP: v1 = 42;
+// CPP-DECLTOP: v2 = 24;
+// CPP-DECLTOP: switch ((v1 + v2) * v2) {
+// CPP-DECLTOP: case 2: {
+// CPP-DECLTOP: v4 = func_b();
+// CPP-DECLTOP: break;
+// CPP-DECLTOP: }
+// CPP-DECLTOP: case 5: {
+// CPP-DECLTOP: v5 = func_a();
+// CPP-DECLTOP: break;
+// CPP-DECLTOP: }
+// CPP-DECLTOP: default: {
+// CPP-DECLTOP: v3 = 4.200000000e+01f;
+// CPP-DECLTOP: func2(v3);
+// CPP-DECLTOP: break;
+// CPP-DECLTOP: }
+// CPP-DECLTOP: }
+// CPP-DECLTOP: return;
+// CPP-DECLTOP: }
+
+func.func @emitc_switch_expression() {
+ %x = "emitc.constant"(){value = 42 : i64} : () -> i64
+ %y = "emitc.constant"(){value = 24 : i64} : () -> i64
+
+ %0 = emitc.expression : i64 {
+ %a = emitc.add %x, %y : (i64, i64) -> i64
+ %b = emitc.mul %a, %y : (i64, i64) -> i64
+ emitc.yield %b : i64
+ }
+
+ emitc.switch %0 : i64
+ case 2 {
+ %1 = emitc.call_opaque "func_b" () : () -> i32
+ emitc.yield
+ }
+ case 5 {
+ %2 = emitc.call_opaque "func_a" () : () -> i32
+ emitc.yield
+ }
+ default {
+ %3 = "emitc.constant"(){value = 42.0 : f32} : () -> f32
+ emitc.call_opaque "func2" (%3) : (f32) -> ()
+ emitc.yield
+ }
+ return
+}
>From 7f34df08cb80ea1ce19460a3a9a0fe7bdf04055c Mon Sep 17 00:00:00 2001
From: Jianjian GUAN <jacquesguan at me.com>
Date: Wed, 22 Jan 2025 11:18:54 +0800
Subject: [PATCH 2/2] simplify test case
---
mlir/test/Target/Cpp/switch.mlir | 33 ++++++++++++++------------------
1 file changed, 14 insertions(+), 19 deletions(-)
diff --git a/mlir/test/Target/Cpp/switch.mlir b/mlir/test/Target/Cpp/switch.mlir
index 222562b5c57e2b..3c17fe93177717 100644
--- a/mlir/test/Target/Cpp/switch.mlir
+++ b/mlir/test/Target/Cpp/switch.mlir
@@ -885,19 +885,18 @@ func.func @emitc_switch_ui64() {
// CPP-DEFAULT-LABEL: void emitc_switch_expression() {
// CPP-DEFAULT: int64_t v1 = 42;
-// CPP-DEFAULT: int64_t v2 = 24;
-// CPP-DEFAULT: switch ((v1 + v2) * v2) {
+// CPP-DEFAULT: switch (-v1) {
// CPP-DEFAULT: case 2: {
-// CPP-DEFAULT: int32_t v3 = func_b();
+// CPP-DEFAULT: int32_t v2 = func_b();
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
// CPP-DEFAULT: case 5: {
-// CPP-DEFAULT: int32_t v4 = func_a();
+// CPP-DEFAULT: int32_t v3 = func_a();
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
// CPP-DEFAULT: default: {
-// CPP-DEFAULT: float v5 = 4.200000000e+01f;
-// CPP-DEFAULT: func2(v5);
+// CPP-DEFAULT: float v4 = 4.200000000e+01f;
+// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
// CPP-DEFAULT: }
@@ -906,24 +905,22 @@ func.func @emitc_switch_ui64() {
// CPP-DECLTOP-LABEL: void emitc_switch_expression() {
// CPP-DECLTOP: int64_t v1;
-// CPP-DECLTOP: int64_t v2;
-// CPP-DECLTOP: float v3;
+// CPP-DECLTOP: float v2;
+// CPP-DECLTOP: int32_t v3;
// CPP-DECLTOP: int32_t v4;
-// CPP-DECLTOP: int32_t v5;
// CPP-DECLTOP: v1 = 42;
-// CPP-DECLTOP: v2 = 24;
-// CPP-DECLTOP: switch ((v1 + v2) * v2) {
+// CPP-DECLTOP: switch (-v1) {
// CPP-DECLTOP: case 2: {
-// CPP-DECLTOP: v4 = func_b();
+// CPP-DECLTOP: v3 = func_b();
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
// CPP-DECLTOP: case 5: {
-// CPP-DECLTOP: v5 = func_a();
+// CPP-DECLTOP: v4 = func_a();
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
// CPP-DECLTOP: default: {
-// CPP-DECLTOP: v3 = 4.200000000e+01f;
-// CPP-DECLTOP: func2(v3);
+// CPP-DECLTOP: v2 = 4.200000000e+01f;
+// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
// CPP-DECLTOP: }
@@ -932,12 +929,10 @@ func.func @emitc_switch_ui64() {
func.func @emitc_switch_expression() {
%x = "emitc.constant"(){value = 42 : i64} : () -> i64
- %y = "emitc.constant"(){value = 24 : i64} : () -> i64
%0 = emitc.expression : i64 {
- %a = emitc.add %x, %y : (i64, i64) -> i64
- %b = emitc.mul %a, %y : (i64, i64) -> i64
- emitc.yield %b : i64
+ %a = emitc.unary_minus %x : (i64) -> i64
+ emitc.yield %a : i64
}
emitc.switch %0 : i64
More information about the Mlir-commits
mailing list